tile.h
author matthijs
Thu, 16 Jun 2005 18:04:02 +0000
changeset 1942 634961366cdc
parent 1927 4126e0e3e884
child 1944 012fa5e69118
permissions -rw-r--r--
(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.
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1202
diff changeset
     1
#ifndef TILE_H
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1202
diff changeset
     2
#define TILE_H
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
     3
1394
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
     4
#include "macros.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1202
diff changeset
     5
#include "map.h"
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
     6
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
     7
typedef enum TileType {
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
     8
	MP_CLEAR,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
     9
	MP_RAILWAY,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    10
	MP_STREET,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    11
	MP_HOUSE,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    12
	MP_TREES,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    13
	MP_STATION,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    14
	MP_WATER,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    15
	MP_VOID, // invisible tiles at the SW and SE border
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    16
	MP_INDUSTRY,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    17
	MP_TUNNELBRIDGE,
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    18
	MP_UNMOVABLE
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    19
} TileType;
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    20
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    21
/* TODO: Find out values */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    22
/* Direction as commonly used in v->direction, 8 way. */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    23
typedef enum Directions {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    24
	DIR_N   = 0,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    25
	DIR_NE  = 1,      /* Northeast, upper right on your monitor */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    26
	DIR_E   = 2,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    27
	DIR_SE  = 3,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    28
	DIR_S   = 4,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    29
	DIR_SW  = 5,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    30
	DIR_W   = 6,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    31
	DIR_NW  = 7,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    32
	DIR_END,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    33
	INVALID_DIR = 0xFF,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    34
} Direction;
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1394
diff changeset
    35
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    36
/* Direction commonly used as the direction of entering and leaving tiles, 4-way */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    37
typedef enum DiagonalDirections {
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    38
	DIAGDIR_NE  = 0,      /* Northeast, upper right on your monitor */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    39
	DIAGDIR_SE  = 1,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    40
	DIAGDIR_SW  = 2,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    41
	DIAGDIR_NW  = 3,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    42
	DIAGDIR_END,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    43
	INVALID_DIAGDIR = 0xFF,
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1927
diff changeset
    44
} DiagDirection;
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1394
diff changeset
    45
1211
6f2f723e5c36 (svn r1715) Move [GS]etMapExtraBits to tile.[ch]
tron
parents: 1209
diff changeset
    46
void SetMapExtraBits(TileIndex tile, byte flags);
6f2f723e5c36 (svn r1715) Move [GS]etMapExtraBits to tile.[ch]
tron
parents: 1209
diff changeset
    47
uint GetMapExtraBits(TileIndex tile);
6f2f723e5c36 (svn r1715) Move [GS]etMapExtraBits to tile.[ch]
tron
parents: 1209
diff changeset
    48
1335
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1333
diff changeset
    49
uint GetTileSlope(TileIndex tile, uint *h);
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1333
diff changeset
    50
uint GetTileZ(TileIndex tile);
a635854c23b6 (svn r1839) Move GetTileSlope() and GetTileZ() into tile.[ch] and use more explicit types as parameters
tron
parents: 1333
diff changeset
    51
1394
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    52
static inline bool CorrectZ(uint tileh)
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    53
{
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    54
	/* tile height must be corrected if the north corner is not raised, but
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    55
	 * any other corner is. These are the cases 1 till 7 */
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    56
	return IS_INT_INSIDE(tileh, 1, 8);
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    57
}
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1335
diff changeset
    58
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    59
static inline uint TileHeight(TileIndex tile)
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    60
{
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    61
	assert(tile < MapSize());
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1749
diff changeset
    62
	return GB(_map_type_and_height[tile], 0, 4);
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    63
}
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    64
1059
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    65
static inline void SetTileHeight(TileIndex tile, uint height)
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    66
{
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    67
	assert(tile < MapSize());
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    68
	assert(height < 16);
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1749
diff changeset
    69
	SB(_map_type_and_height[tile], 0, 4, height);
1059
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    70
}
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    71
1041
be151b7bc909 (svn r1542) Rename TileHeight to TilePixelHeight, because this is what it actually returns
tron
parents: 1035
diff changeset
    72
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
    73
{
1044
9b73df700a7c (svn r1545) Add TileHeight() which returns the height (not multiplied by 8)
tron
parents: 1041
diff changeset
    74
	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
    75
}
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
    76
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    77
static inline TileType GetTileType(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
    78
{
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
    79
	assert(tile < MapSize());
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1749
diff changeset
    80
	return GB(_map_type_and_height[tile], 4, 4);
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
    81
}
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
    82
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    83
static inline void SetTileType(TileIndex tile, TileType type)
1059
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    84
{
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    85
	assert(tile < MapSize());
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1749
diff changeset
    86
	SB(_map_type_and_height[tile], 4, 4, type);
1059
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    87
}
c28c6be74291 (svn r1560) Introduce SetTileType() and SetTileHeight()
tron
parents: 1044
diff changeset
    88
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    89
static inline bool IsTileType(TileIndex tile, TileType type)
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
    90
{
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1211
diff changeset
    91
	return GetTileType(tile) == type;
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
    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
1333
9ac74f431ed2 (svn r1837) GetTileOwner returns Owner, not bool
tron
parents: 1330
diff changeset
    94
static inline Owner GetTileOwner(TileIndex tile)
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
    95
{
1333
9ac74f431ed2 (svn r1837) GetTileOwner returns Owner, not bool
tron
parents: 1330
diff changeset
    96
	assert(tile < MapSize());
1898
b9fe2a5b7e13 (svn r2404) assert that GetTileOwner() isn't called for tiles, which don't store owner information
tron
parents: 1852
diff changeset
    97
	assert(!IsTileType(tile, MP_HOUSE));
b9fe2a5b7e13 (svn r2404) assert that GetTileOwner() isn't called for tiles, which don't store owner information
tron
parents: 1852
diff changeset
    98
	assert(!IsTileType(tile, MP_VOID));
b9fe2a5b7e13 (svn r2404) assert that GetTileOwner() isn't called for tiles, which don't store owner information
tron
parents: 1852
diff changeset
    99
	assert(!IsTileType(tile, MP_INDUSTRY));
b9fe2a5b7e13 (svn r2404) assert that GetTileOwner() isn't called for tiles, which don't store owner information
tron
parents: 1852
diff changeset
   100
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   101
	return _map_owner[tile];
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   102
}
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   103
1902
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   104
static inline void SetTileOwner(TileIndex tile, Owner owner)
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   105
{
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   106
	assert(tile < MapSize());
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   107
	assert(!IsTileType(tile, MP_HOUSE));
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   108
	assert(!IsTileType(tile, MP_VOID));
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   109
	assert(!IsTileType(tile, MP_INDUSTRY));
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   110
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   111
	_map_owner[tile] = owner;
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   112
}
5d653da1abb7 (svn r2408) Introduce SetTileOwner() and use it
tron
parents: 1898
diff changeset
   113
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   114
static inline bool IsTileOwner(TileIndex tile, Owner owner)
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   115
{
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   116
	return GetTileOwner(tile) == owner;
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   117
}
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1214
diff changeset
   118
1927
4126e0e3e884 (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1902
diff changeset
   119
static inline bool IsLevelCrossing(TileIndex tile)
4126e0e3e884 (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1902
diff changeset
   120
{
4126e0e3e884 (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1902
diff changeset
   121
	return (_map5[tile] & 0xF0) == 0x10;
4126e0e3e884 (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1902
diff changeset
   122
}
4126e0e3e884 (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1902
diff changeset
   123
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
   124
#endif