2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/** @file map.h */
|
|
|
|
|
2004-12-15 22:18:54 +00:00
|
|
|
#ifndef MAP_H
|
|
|
|
#define MAP_H
|
|
|
|
|
2005-01-29 13:33:14 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2005-07-13 19:51:31 +00:00
|
|
|
extern uint _map_tile_mask;
|
2007-04-27 21:29:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Wraps' the given tile to it is within the map. It does
|
|
|
|
* this by masking the 'high' bits of.
|
|
|
|
* @param x the tile to 'wrap'
|
|
|
|
*/
|
2005-07-13 19:51:31 +00:00
|
|
|
|
|
|
|
#define TILE_MASK(x) ((x) & _map_tile_mask)
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Asserts when the tile is outside of the map.
|
|
|
|
* @param x the tile to check
|
|
|
|
*/
|
2005-02-22 12:48:03 +00:00
|
|
|
#define TILE_ASSERT(x) assert(TILE_MASK(x) == (x));
|
2005-01-07 17:02:43 +00:00
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Data that is stored per tile. Also used TileExtended for this.
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the members.
|
|
|
|
*/
|
2007-03-07 12:11:48 +00:00
|
|
|
struct Tile {
|
2007-04-27 21:29:36 +00:00
|
|
|
byte type_height; ///< The type (bits 4..7) and height of the northern corner
|
|
|
|
byte m1; ///< Primarily used for ownership information
|
|
|
|
uint16 m2; ///< Primarily used for indices to towns, industries and stations
|
|
|
|
byte m3; ///< General purpose
|
|
|
|
byte m4; ///< General purpose
|
|
|
|
byte m5; ///< General purpose
|
|
|
|
byte m6; ///< Primarily used for bridges and rainforest/desert
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-07-13 18:04:01 +00:00
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Data that is stored per tile. Also used Tile for this.
|
|
|
|
* Look at docs/landscape.html for the exact meaning of the members.
|
|
|
|
*/
|
2007-03-19 11:27:30 +00:00
|
|
|
struct TileExtended {
|
2007-04-27 21:29:36 +00:00
|
|
|
byte m7; ///< Primarily used for newgrf support
|
2007-03-19 11:27:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Tile *_m;
|
|
|
|
extern TileExtended *_me;
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2005-07-13 19:51:31 +00:00
|
|
|
void AllocateMap(uint size_x, uint size_y);
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Logarithm of the map size along the X side.
|
|
|
|
* @note try to avoid using this one
|
|
|
|
* @return 2^"return value" == MapSizeX()
|
|
|
|
*/
|
|
|
|
static inline uint MapLogX()
|
|
|
|
{
|
|
|
|
extern uint _map_log_x;
|
|
|
|
return _map_log_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map along the X
|
|
|
|
* @return the number of tiles along the X of the map
|
|
|
|
*/
|
|
|
|
static inline uint MapSizeX()
|
|
|
|
{
|
|
|
|
extern uint _map_size_x;
|
|
|
|
return _map_size_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map along the Y
|
|
|
|
* @return the number of tiles along the Y of the map
|
|
|
|
*/
|
|
|
|
static inline uint MapSizeY()
|
|
|
|
{
|
|
|
|
extern uint _map_size_y;
|
|
|
|
return _map_size_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the map
|
|
|
|
* @return the number of tiles of the map
|
|
|
|
*/
|
|
|
|
static inline uint MapSize()
|
|
|
|
{
|
|
|
|
extern uint _map_size;
|
|
|
|
return _map_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum X coordinate within the map, including MP_VOID
|
|
|
|
* @return the maximum X coordinate
|
|
|
|
*/
|
|
|
|
static inline uint MapMaxX()
|
|
|
|
{
|
|
|
|
return MapSizeX() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum X coordinate within the map, including MP_VOID
|
|
|
|
* @return the maximum X coordinate
|
|
|
|
*/
|
|
|
|
static inline uint MapMaxY()
|
|
|
|
{
|
|
|
|
return MapSizeY() - 1;
|
|
|
|
}
|
2004-12-16 12:30:13 +00:00
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Scale a number relative to the map size */
|
2005-01-28 15:31:04 +00:00
|
|
|
uint ScaleByMapSize(uint); // Scale relative to the number of tiles
|
|
|
|
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
|
|
|
|
|
2005-01-25 21:43:57 +00:00
|
|
|
typedef uint32 TileIndex;
|
2005-06-25 16:44:57 +00:00
|
|
|
typedef int32 TileIndexDiff;
|
|
|
|
|
|
|
|
static inline TileIndex TileXY(uint x, uint y)
|
|
|
|
{
|
2005-07-13 19:51:31 +00:00
|
|
|
return (y * MapSizeX()) + x;
|
2005-06-25 16:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline TileIndexDiff TileDiffXY(int x, int y)
|
|
|
|
{
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Multiplication gives much better optimization on MSVC than shifting.
|
|
|
|
* 0 << shift isn't optimized to 0 properly.
|
|
|
|
* Typically x and y are constants, and then this doesn't result
|
|
|
|
* in any actual multiplication in the assembly code.. */
|
2005-07-13 19:51:31 +00:00
|
|
|
return (y * MapSizeX()) + x;
|
2005-06-25 16:44:57 +00:00
|
|
|
}
|
2005-02-06 22:36:08 +00:00
|
|
|
|
2005-06-25 06:15:43 +00:00
|
|
|
static inline TileIndex TileVirtXY(uint x, uint y)
|
|
|
|
{
|
|
|
|
return (y >> 4 << MapLogX()) + (x >> 4);
|
|
|
|
}
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
enum {
|
2007-04-27 21:29:36 +00:00
|
|
|
INVALID_TILE = (TileIndex)-1 ///< The very nice invalid tile marker
|
2005-01-31 11:23:10 +00:00
|
|
|
};
|
2005-01-07 17:02:43 +00:00
|
|
|
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
enum {
|
2007-03-03 04:04:22 +00:00
|
|
|
TILE_SIZE = 16, ///< Tiles are 16x16 "units" in size
|
|
|
|
TILE_PIXELS = 32, ///< a tile is 32x32 pixels
|
|
|
|
TILE_HEIGHT = 8, ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Get the X component of a tile
|
|
|
|
* @param tile the tile to get the X component of
|
|
|
|
* @return the X component
|
|
|
|
*/
|
2005-01-07 17:02:43 +00:00
|
|
|
static inline uint TileX(TileIndex tile)
|
|
|
|
{
|
|
|
|
return tile & MapMaxX();
|
|
|
|
}
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* Get the Y component of a tile
|
|
|
|
* @param tile the tile to get the Y component of
|
|
|
|
* @return the Y component
|
|
|
|
*/
|
2005-01-07 17:02:43 +00:00
|
|
|
static inline uint TileY(TileIndex tile)
|
|
|
|
{
|
|
|
|
return tile >> MapLogX();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct TileIndexDiffC {
|
2005-01-06 11:39:00 +00:00
|
|
|
int16 x;
|
|
|
|
int16 y;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-01-06 11:39:00 +00:00
|
|
|
|
|
|
|
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
|
|
|
|
{
|
|
|
|
return (tidc.y << MapLogX()) + tidc.x;
|
|
|
|
}
|
2005-01-05 13:32:03 +00:00
|
|
|
|
2005-01-09 17:55:11 +00:00
|
|
|
|
|
|
|
#ifndef _DEBUG
|
|
|
|
#define TILE_ADD(x,y) ((x) + (y))
|
|
|
|
#else
|
|
|
|
extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
|
|
|
|
const char *exp, const char *file, int line);
|
|
|
|
#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
|
|
|
|
#endif
|
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
|
2005-01-09 17:55:11 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
uint TileAddWrap(TileIndex tile, int addx, int addy);
|
|
|
|
|
2006-09-06 01:56:01 +00:00
|
|
|
static inline TileIndexDiffC TileIndexDiffCByDiagDir(uint dir) {
|
|
|
|
extern const TileIndexDiffC _tileoffs_by_diagdir[4];
|
|
|
|
|
|
|
|
assert(dir < lengthof(_tileoffs_by_diagdir));
|
|
|
|
return _tileoffs_by_diagdir[dir];
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns tile + the diff given in diff. If the result tile would end up
|
|
|
|
* outside of the map, INVALID_TILE is returned instead.
|
|
|
|
*/
|
|
|
|
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) {
|
|
|
|
int x = TileX(tile) + diff.x;
|
|
|
|
int y = TileY(tile) + diff.y;
|
|
|
|
if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY())
|
|
|
|
return INVALID_TILE;
|
|
|
|
else
|
2005-06-25 16:44:57 +00:00
|
|
|
return TileXY(x, y);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2005-01-09 17:55:11 +00:00
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the diff between two tiles
|
|
|
|
*
|
|
|
|
* @param tile_a from tile
|
|
|
|
* @param tile_b to tile
|
|
|
|
* @return the difference between tila_a and tile_b
|
|
|
|
*/
|
|
|
|
static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
|
|
|
|
{
|
|
|
|
TileIndexDiffC difference;
|
|
|
|
|
|
|
|
difference.x = TileX(tile_a) - TileX(tile_b);
|
|
|
|
difference.y = TileY(tile_a) - TileY(tile_b);
|
|
|
|
|
|
|
|
return difference;
|
|
|
|
}
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Functions to calculate distances */
|
|
|
|
uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
|
|
|
|
uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared
|
|
|
|
uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm
|
|
|
|
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan
|
|
|
|
uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map
|
2005-01-31 07:23:15 +00:00
|
|
|
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
#define BEGIN_TILE_LOOP(var, w, h, tile) \
|
2005-07-21 22:15:02 +00:00
|
|
|
{ \
|
|
|
|
int h_cur = h; \
|
|
|
|
uint var = tile; \
|
|
|
|
do { \
|
|
|
|
int w_cur = w; \
|
|
|
|
do {
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
#define END_TILE_LOOP(var, w, h, tile) \
|
2005-07-21 22:15:02 +00:00
|
|
|
} while (++var, --w_cur != 0); \
|
|
|
|
} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \
|
|
|
|
}
|
|
|
|
|
2006-09-06 01:56:01 +00:00
|
|
|
static inline TileIndexDiff TileOffsByDiagDir(uint dir)
|
2006-09-05 23:21:41 +00:00
|
|
|
{
|
|
|
|
extern const TileIndexDiffC _tileoffs_by_diagdir[4];
|
|
|
|
|
|
|
|
assert(dir < lengthof(_tileoffs_by_diagdir));
|
|
|
|
return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
|
|
|
|
}
|
2005-07-21 22:15:02 +00:00
|
|
|
|
2006-09-06 01:56:01 +00:00
|
|
|
static inline TileIndexDiff TileOffsByDir(uint dir)
|
2005-01-05 13:32:03 +00:00
|
|
|
{
|
2006-09-05 23:21:41 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[8];
|
2005-01-05 13:32:03 +00:00
|
|
|
|
|
|
|
assert(dir < lengthof(_tileoffs_by_dir));
|
2005-01-06 11:39:00 +00:00
|
|
|
return ToTileIndexDiff(_tileoffs_by_dir[dir]);
|
2005-01-05 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 23:01:58 +00:00
|
|
|
typedef bool TestTileOnSearchProc(TileIndex tile, uint32 data);
|
|
|
|
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data);
|
|
|
|
|
2005-04-11 19:14:48 +00:00
|
|
|
/* Approximation of the length of a straight track, relative to a diagonal
|
|
|
|
* track (ie the size of a tile side). #defined instead of const so it can
|
|
|
|
* stay integer. (no runtime float operations) Is this needed?
|
2005-04-11 20:19:41 +00:00
|
|
|
* Watch out! There are _no_ brackets around here, to prevent intermediate
|
|
|
|
* rounding! Be careful when using this!
|
2005-04-11 19:14:48 +00:00
|
|
|
* This value should be sqrt(2)/2 ~ 0.7071 */
|
2005-04-11 20:19:41 +00:00
|
|
|
#define STRAIGHT_TRACK_LENGTH 7071/10000
|
2005-04-11 19:14:48 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
#endif /* MAP_H */
|