src/tile_map.cpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:32:07 +0200
changeset 10438 51bff16a04c9
parent 10268 df4d17274ec6
permissions -rw-r--r--
initial mini-map stuff
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2049
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2049
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8348
diff changeset
     3
/** @file tile_map.cpp Global tile accessors. */
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 5587
diff changeset
     4
1213
fc87a2ee4161 (svn r1717) -Fix: some compilation problems for braindead VS6 and added missing files to project (thx bociusz)
darkvater
parents: 1211
diff changeset
     5
#include "stdafx.h"
8108
b42a0e5c67ef (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 7324
diff changeset
     6
#include "openttd.h"
b42a0e5c67ef (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 7324
diff changeset
     7
#include "tile_map.h"
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8108
diff changeset
     8
#include "core/math_func.hpp"
1211
43ae74415fdd (svn r1715) Move [GS]etMapExtraBits to tile.[ch]
tron
parents:
diff changeset
     9
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    10
/**
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    11
 * Return the slope of a given tile
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    12
 * @param tile Tile to compute slope of
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    13
 * @param h    If not \c NULL, pointer to storage of z height
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    14
 * @return Slope of the tile, except for the HALFTILE part */
3636
a36cc46e754d (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3422
diff changeset
    15
Slope GetTileSlope(TileIndex tile, uint *h)
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    16
{
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    17
	assert(tile < MapSize());
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    18
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    19
	if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) {
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    20
		if (h != NULL) *h = 0;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    21
		return SLOPE_FLAT;
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    22
	}
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    23
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    24
	uint a = TileHeight(tile); // Height of the N corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    25
	uint min = a; // Minimal height of all corners examined so far
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    26
	uint b = TileHeight(tile + TileDiffXY(1, 0)); // Height of the W corner
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    27
	if (min > b) min = b;
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    28
	uint c = TileHeight(tile + TileDiffXY(0, 1)); // Height of the E corner
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    29
	if (min > c) min = c;
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    30
	uint d = TileHeight(tile + TileDiffXY(1, 1)); // Height of the S corner
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    31
	if (min > d) min = d;
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    32
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    33
	/* Due to the fact that tiles must connect with each other without leaving gaps, the
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    34
	 * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    35
	 *
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    36
	 * Also, there is at most 1 corner with height difference of 2.
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    37
	 */
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    38
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    39
	uint r = SLOPE_FLAT; // Computed slope of the tile
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    40
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    41
	/* For each corner if not equal to minimum height:
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    42
	 *  - set the SLOPE_STEEP flag if the difference is 2
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    43
	 *  - add the corresponding SLOPE_X constant to the computed slope
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    44
	 */
4067
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    45
	if ((a -= min) != 0) r += (--a << 4) + SLOPE_N;
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    46
	if ((c -= min) != 0) r += (--c << 4) + SLOPE_E;
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    47
	if ((d -= min) != 0) r += (--d << 4) + SLOPE_S;
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    48
	if ((b -= min) != 0) r += (--b << 4) + SLOPE_W;
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    49
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    50
	if (h != NULL) *h = min * TILE_HEIGHT;
5ef297943eff (svn r5363) Revert 5312, 5288, 5248, 3992, 3249, 3228
tron
parents: 3773
diff changeset
    51
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    52
	return (Slope)r;
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    53
}
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    54
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    55
/**
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    56
 * Get bottom height of the tile
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    57
 * @param tile Tile to compute height of
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    58
 * @return Minimum height of the tile */
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    59
uint GetTileZ(TileIndex tile)
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    60
{
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    61
	if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    62
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    63
	uint h = TileHeight(tile); // N corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    64
	h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    65
	h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    66
	h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    67
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    68
	return h * TILE_HEIGHT;
1335
a5f223b9f549 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1213
diff changeset
    69
}
3773
0019b5f70ea9 (svn r4765) Add GetTileMaxZ(), which returns the height of the highest corner of a tile, and use it to simplify the code in a few places
tron
parents: 3636
diff changeset
    70
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    71
/**
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    72
 * Get top height of the tile
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    73
 * @param tile Tile to compute height of
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 9111
diff changeset
    74
 * @return Maximum height of the tile */
3773
0019b5f70ea9 (svn r4765) Add GetTileMaxZ(), which returns the height of the highest corner of a tile, and use it to simplify the code in a few places
tron
parents: 3636
diff changeset
    75
uint GetTileMaxZ(TileIndex t)
0019b5f70ea9 (svn r4765) Add GetTileMaxZ(), which returns the height of the highest corner of a tile, and use it to simplify the code in a few places
tron
parents: 3636
diff changeset
    76
{
7324
5bbb17fe83f7 (svn r10687) -Fix (r10686): subversion does not like it when you change code when you are typing the commit message.
rubidium
parents: 7323
diff changeset
    77
	if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return 0;
3773
0019b5f70ea9 (svn r4765) Add GetTileMaxZ(), which returns the height of the highest corner of a tile, and use it to simplify the code in a few places
tron
parents: 3636
diff changeset
    78
10268
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    79
	uint h = TileHeight(t); // N corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    80
	h = max(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    81
	h = max(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
df4d17274ec6 (svn r14502) -Codechange: add some inline comments and declare variable on use (Alberth)
rubidium
parents: 10260
diff changeset
    82
	h = max(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    83
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 6422
diff changeset
    84
	return h * TILE_HEIGHT;
3773
0019b5f70ea9 (svn r4765) Add GetTileMaxZ(), which returns the height of the highest corner of a tile, and use it to simplify the code in a few places
tron
parents: 3636
diff changeset
    85
}