map.h
author tron
Fri, 28 Jan 2005 15:31:04 +0000
changeset 1202 4d2a20c50760
parent 1174 6a5e747f3ba6
child 1209 2e00193652b2
permissions -rw-r--r--
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
These scale a number relative to the map size/circumference.
Use them to scale the amount of map objects.
Of course at the moment they return just the input, because there are no bigger/smaller maps yet.
679
04ca2cd69420 (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
04ca2cd69420 (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
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     3
927
94fec9843fd3 (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))
1174
6a5e747f3ba6 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
     5
#define TILE_XY(x,y) (((y) << MapLogX()) + (x))
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     6
979
11ea18598e16 (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
a6d140a6a4de (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
238bbdaa228b (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[];
238bbdaa228b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    10
extern byte   _map5[];
238bbdaa228b (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[];
238bbdaa228b (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[];
238bbdaa228b (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[];
238bbdaa228b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 695
diff changeset
    14
extern uint16 _map2[];
238bbdaa228b (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
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
    16
689
5a4b1536db82 (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
5a4b1536db82 (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; }
5a4b1536db82 (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; }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    20
/* The size of the map */
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    21
static inline uint MapSizeX(void) { return 1 << MapLogX(); }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    22
static inline uint MapSizeY(void) { return 1 << MapLogY(); }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    23
/* The maximum coordinates */
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    24
static inline uint MapMaxX(void) { return MapSizeX() - 1; }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    25
static inline uint MapMaxY(void) { return MapSizeY() - 1; }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    26
/* The number of tiles in the map */
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    27
static inline uint MapSize(void) { return MapSizeX() * MapSizeY(); }
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    28
1202
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    29
// Scale a number relative to the map size
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    30
uint ScaleByMapSize(uint); // Scale relative to the number of tiles
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    31
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    32
1174
6a5e747f3ba6 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
    33
typedef uint32 TileIndex;
926
a6d140a6a4de (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
a6d140a6a4de (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
a6d140a6a4de (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
static inline uint TileX(TileIndex tile)
a6d140a6a4de (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
{
a6d140a6a4de (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
	return tile & MapMaxX();
a6d140a6a4de (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
}
a6d140a6a4de (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
a6d140a6a4de (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
static inline uint TileY(TileIndex tile)
a6d140a6a4de (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
{
a6d140a6a4de (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
    43
	return tile >> MapLogX();
a6d140a6a4de (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
    44
}
a6d140a6a4de (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
    45
a6d140a6a4de (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
    46
1174
6a5e747f3ba6 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
    47
typedef int32 TileIndexDiff;
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    48
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    49
typedef struct TileIndexDiffC {
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    50
	int16 x;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    51
	int16 y;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    52
} TileIndexDiffC;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    53
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    54
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    55
{
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    56
	return (tidc.y << MapLogX()) + tidc.x;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    57
}
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    58
955
62b8588f50c8 (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
62b8588f50c8 (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
#ifndef _DEBUG
62b8588f50c8 (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) ((x) + (y))
62b8588f50c8 (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
#else
62b8588f50c8 (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
	extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
62b8588f50c8 (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
		const char *exp, const char *file, int line);
62b8588f50c8 (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
	#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
62b8588f50c8 (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
#endif
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    67
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    68
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x, y))
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    69
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    70
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    71
static inline TileIndexDiff TileOffsByDir(uint dir)
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    72
{
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    73
	extern const TileIndexDiffC _tileoffs_by_dir[4];
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    74
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    75
	assert(dir < lengthof(_tileoffs_by_dir));
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    76
	return ToTileIndexDiff(_tileoffs_by_dir[dir]);
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    77
}
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    78
1035
812f837ee03f (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
    79
1044
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    80
static inline uint TileHeight(TileIndex tile)
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    81
{
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    82
	assert(tile < MapSize());
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    83
	return _map_type_and_height[tile] & 0xf;
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    84
}
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    85
1059
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    86
static inline void SetTileHeight(TileIndex tile, uint height)
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    87
{
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    88
	assert(tile < MapSize());
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    89
	assert(height < 16);
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    90
	_map_type_and_height[tile] &= ~0x0F;
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    91
	_map_type_and_height[tile] |= height;
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    92
}
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    93
1041
94db182c3614 (svn r1542) Rename TileHeight to TilePixelHeight, because this is what it actually returns
tron
parents: 1035
diff changeset
    94
static inline uint TilePixelHeight(TileIndex tile)
1035
812f837ee03f (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
{
1044
63e0601a43cc (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    96
	return TileHeight(tile) * 8;
1035
812f837ee03f (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
}
812f837ee03f (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
    98
812f837ee03f (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
    99
static inline int TileType(TileIndex tile)
812f837ee03f (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
   100
{
812f837ee03f (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
   101
	assert(tile < MapSize());
812f837ee03f (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
   102
	return _map_type_and_height[tile] >> 4;
812f837ee03f (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
   103
}
812f837ee03f (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
   104
1059
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   105
static inline void SetTileType(TileIndex tile, uint type)
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   106
{
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   107
	assert(tile < MapSize());
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   108
	_map_type_and_height[tile] &= ~0xF0;
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   109
	_map_type_and_height[tile] |= type << 4;
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   110
}
fe97d81a1b4f (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
   111
1035
812f837ee03f (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
   112
static inline bool IsTileType(TileIndex tile, int type)
812f837ee03f (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
   113
{
812f837ee03f (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
   114
	return TileType(tile) == type;
812f837ee03f (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
   115
}
812f837ee03f (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
   116
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
   117
#endif