2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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
|
|
|
// Putting externs inside inline functions seems to confuse the aliasing
|
|
|
|
// checking on MSVC6. Never use those variables directly.
|
|
|
|
extern uint _map_log_x;
|
|
|
|
extern uint _map_size_x;
|
|
|
|
extern uint _map_size_y;
|
|
|
|
extern uint _map_tile_mask;
|
|
|
|
extern uint _map_size;
|
|
|
|
|
|
|
|
#define TILE_MASK(x) ((x) & _map_tile_mask)
|
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
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
typedef struct Tile {
|
|
|
|
byte type_height;
|
2005-08-23 18:47:04 +00:00
|
|
|
byte m1;
|
2005-07-13 18:04:01 +00:00
|
|
|
uint16 m2;
|
|
|
|
byte m3;
|
|
|
|
byte m4;
|
|
|
|
byte m5;
|
|
|
|
byte extra;
|
|
|
|
} Tile;
|
|
|
|
|
|
|
|
extern Tile* _m;
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2005-07-13 19:51:31 +00:00
|
|
|
void AllocateMap(uint size_x, uint size_y);
|
|
|
|
|
2004-12-16 12:30:13 +00:00
|
|
|
// binary logarithm of the map size, try to avoid using this one
|
2005-07-13 19:51:31 +00:00
|
|
|
static inline uint MapLogX(void) { return _map_log_x; }
|
2004-12-16 12:30:13 +00:00
|
|
|
/* The size of the map */
|
2005-07-13 19:51:31 +00:00
|
|
|
static inline uint MapSizeX(void) { return _map_size_x; }
|
|
|
|
static inline uint MapSizeY(void) { return _map_size_y; }
|
2004-12-16 12:30:13 +00:00
|
|
|
/* The maximum coordinates */
|
2005-07-13 19:51:31 +00:00
|
|
|
static inline uint MapMaxX(void) { return _map_size_x - 1; }
|
|
|
|
static inline uint MapMaxY(void) { return _map_size_y - 1; }
|
2004-12-16 12:30:13 +00:00
|
|
|
/* The number of tiles in the map */
|
2005-07-13 19:51:31 +00:00
|
|
|
static inline uint MapSize(void) { return _map_size; }
|
2004-12-16 12:30:13 +00:00
|
|
|
|
2005-01-28 15:31:04 +00:00
|
|
|
// Scale a number relative to the map size
|
|
|
|
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)
|
|
|
|
{
|
2005-07-13 19:51:31 +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..
|
|
|
|
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
|
|
|
typedef enum {
|
|
|
|
OWNER_TOWN = 0xf, // a town owns the tile
|
|
|
|
OWNER_NONE = 0x10, // nobody owns the tile
|
|
|
|
OWNER_WATER = 0x11, // "water" owns the tile
|
|
|
|
OWNER_SPECTATOR = 0xff, // spectator in MP or in scenario editor
|
|
|
|
} Owner;
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
enum {
|
2005-06-24 12:38:35 +00:00
|
|
|
INVALID_TILE = (TileIndex)-1
|
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 {
|
|
|
|
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) */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
static inline uint TileX(TileIndex tile)
|
|
|
|
{
|
|
|
|
return tile & MapMaxX();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint TileY(TileIndex tile)
|
|
|
|
{
|
|
|
|
return tile >> MapLogX();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
typedef struct TileIndexDiffC {
|
|
|
|
int16 x;
|
|
|
|
int16 y;
|
|
|
|
} TileIndexDiffC;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
static inline TileIndexDiffC TileIndexDiffCByDir(uint dir) {
|
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[4];
|
|
|
|
return _tileoffs_by_dir[dir];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
// Functions to calculate distances
|
2005-04-11 19:14:48 +00:00
|
|
|
uint DistanceManhattan(TileIndex, TileIndex); // also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
|
2005-01-31 07:23:15 +00:00
|
|
|
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-07-21 22:15:02 +00:00
|
|
|
#define BEGIN_TILE_LOOP(var,w,h,tile) \
|
|
|
|
{ \
|
|
|
|
int h_cur = h; \
|
|
|
|
uint var = tile; \
|
|
|
|
do { \
|
|
|
|
int w_cur = w; \
|
|
|
|
do {
|
|
|
|
|
|
|
|
#define END_TILE_LOOP(var,w,h,tile) \
|
|
|
|
} while (++var, --w_cur != 0); \
|
|
|
|
} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
static inline TileIndexDiff TileOffsByDir(uint dir)
|
|
|
|
{
|
2005-01-06 11:39:00 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[4];
|
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
|
|
|
}
|
|
|
|
|
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 */
|