map.h
author tron
Sun, 16 Jan 2005 14:50:01 +0000
changeset 1044 9b73df700a7c
parent 1041 be151b7bc909
child 1059 c28c6be74291
permissions -rw-r--r--
(svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
Replace some direct references to _map_type_and_height with TileHeight()/IsTileType()
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     1
#ifndef MAP_H
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     2
#define MAP_H
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     3
927
28f45a22a564 (svn r1415) Move TILE_FROM_XY and TILE_XY to map.h and push TILE_[XY] bits from map.h into map.c.
tron
parents: 926
diff changeset
     4
#define TILE_FROM_XY(x,y) (int)((((y) >> 4) << MapLogX()) + ((x) >> 4))
28f45a22a564 (svn r1415) Move TILE_FROM_XY and TILE_XY to map.h and push TILE_[XY] bits from map.h into map.c.
tron
parents: 926
diff changeset
     5
#define TILE_XY(x,y) (int)(((y) << MapLogX()) + (x))
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     6
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 955
diff changeset
     7
#define TILE_MASK(x) ((x) & ((1 << (MapLogX() + MapLogY())) - 1))
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
     8
817
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
     9
extern byte   _map_type_and_height[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    10
extern byte   _map5[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    11
extern byte   _map3_lo[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    12
extern byte   _map3_hi[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    13
extern byte   _map_owner[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    14
extern uint16 _map2[];
4f9377b7fd2b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    15
extern byte   _map_extra_bits[];
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
    16
689
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    17
// binary logarithm of the map size, try to avoid using this one
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    18
static inline uint MapLogX(void)  { extern uint _map_log_x; return _map_log_x; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    19
static inline uint MapLogY(void)  { extern uint _map_log_y; return _map_log_y; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    20
/* The size of the map */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    21
static inline uint MapSizeX(void) { return 1 << MapLogX(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    22
static inline uint MapSizeY(void) { return 1 << MapLogY(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    23
/* The maximum coordinates */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    24
static inline uint MapMaxX(void) { return MapSizeX() - 1; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    25
static inline uint MapMaxY(void) { return MapSizeY() - 1; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    26
/* The number of tiles in the map */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    27
static inline uint MapSize(void) { return MapSizeX() * MapSizeY(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    28
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    29
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    30
typedef uint16 TileIndex;
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    31
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    32
static inline uint TileX(TileIndex tile)
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    33
{
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    34
	return tile & MapMaxX();
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    35
}
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    36
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    37
static inline uint TileY(TileIndex tile)
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    38
{
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    39
	return tile >> MapLogX();
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    40
}
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    41
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    42
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    43
typedef int16 TileIndexDiff;
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    44
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    45
typedef struct TileIndexDiffC {
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    46
	int16 x;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    47
	int16 y;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    48
} TileIndexDiffC;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    49
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    50
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    51
{
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    52
	return (tidc.y << MapLogX()) + tidc.x;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    53
}
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    54
955
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    55
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    56
#ifndef _DEBUG
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    57
	#define TILE_ADD(x,y) ((x) + (y))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    58
#else
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    59
	extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    60
		const char *exp, const char *file, int line);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    61
	#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    62
#endif
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    63
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    64
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x, y))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    65
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    66
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    67
static inline TileIndexDiff TileOffsByDir(uint dir)
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    68
{
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    69
	extern const TileIndexDiffC _tileoffs_by_dir[4];
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    70
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    71
	assert(dir < lengthof(_tileoffs_by_dir));
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    72
	return ToTileIndexDiff(_tileoffs_by_dir[dir]);
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    73
}
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    74
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    75
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    76
static inline uint TileHeight(TileIndex tile)
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    77
{
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    78
	assert(tile < MapSize());
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    79
	return _map_type_and_height[tile] & 0xf;
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    80
}
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    81
1041
be151b7bc909 (svn r1542) Rename TileHeight to TilePixelHeight, because this is what it actually returns
tron
parents: 1035
diff changeset
    82
static inline uint TilePixelHeight(TileIndex tile)
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    83
{
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    84
	return TileHeight(tile) * 8;
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    85
}
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    86
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    87
static inline int TileType(TileIndex tile)
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    88
{
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    89
	assert(tile < MapSize());
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    90
	return _map_type_and_height[tile] >> 4;
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    91
}
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    92
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    93
static inline bool IsTileType(TileIndex tile, int type)
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    94
{
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    95
	return TileType(tile) == type;
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    96
}
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 979
diff changeset
    97
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
    98
#endif