src/map.h
branchgamebalance
changeset 9911 0b8b245a2391
parent 9910 0b2aebc8283e
equal deleted inserted replaced
9910:0b2aebc8283e 9911:0b8b245a2391
     5 #ifndef MAP_H
     5 #ifndef MAP_H
     6 #define MAP_H
     6 #define MAP_H
     7 
     7 
     8 #include "stdafx.h"
     8 #include "stdafx.h"
     9 
     9 
    10 /* Putting externs inside inline functions seems to confuse the aliasing
       
    11  * checking on MSVC6. Never use those variables directly. */
       
    12 extern uint _map_log_x;
       
    13 extern uint _map_size_x;
       
    14 extern uint _map_size_y;
       
    15 extern uint _map_tile_mask;
    10 extern uint _map_tile_mask;
    16 extern uint _map_size;
    11 
       
    12 /**
       
    13  * 'Wraps' the given tile to it is within the map. It does
       
    14  * this by masking the 'high' bits of.
       
    15  * @param x the tile to 'wrap'
       
    16  */
    17 
    17 
    18 #define TILE_MASK(x) ((x) & _map_tile_mask)
    18 #define TILE_MASK(x) ((x) & _map_tile_mask)
       
    19 /**
       
    20  * Asserts when the tile is outside of the map.
       
    21  * @param x the tile to check
       
    22  */
    19 #define TILE_ASSERT(x) assert(TILE_MASK(x) == (x));
    23 #define TILE_ASSERT(x) assert(TILE_MASK(x) == (x));
    20 
    24 
       
    25 /**
       
    26  * Data that is stored per tile. Also used TileExtended for this.
       
    27  * Look at docs/landscape.html for the exact meaning of the members.
       
    28  */
    21 struct Tile {
    29 struct Tile {
    22 	byte type_height;
    30 	byte type_height; ///< The type (bits 4..7) and height of the northern corner
    23 	byte m1;
    31 	byte m1;   ///< Primarily used for ownership information
    24 	uint16 m2;
    32 	uint16 m2; ///< Primarily used for indices to towns, industries and stations
    25 	byte m3;
    33 	byte m3;   ///< General purpose
    26 	byte m4;
    34 	byte m4;   ///< General purpose
    27 	byte m5;
    35 	byte m5;   ///< General purpose
    28 	byte m6;
    36 	byte m6;   ///< Primarily used for bridges and rainforest/desert
    29 };
    37 };
    30 
    38 
       
    39 /**
       
    40  * Data that is stored per tile. Also used Tile for this.
       
    41  * Look at docs/landscape.html for the exact meaning of the members.
       
    42  */
    31 struct TileExtended {
    43 struct TileExtended {
    32 	byte m7;
    44 	byte m7; ///< Primarily used for newgrf support
    33 };
    45 };
    34 
    46 
    35 extern Tile *_m;
    47 extern Tile *_m;
    36 extern TileExtended *_me;
    48 extern TileExtended *_me;
    37 
    49 
    38 void AllocateMap(uint size_x, uint size_y);
    50 void AllocateMap(uint size_x, uint size_y);
    39 
    51 
    40 /* binary logarithm of the map size, try to avoid using this one */
    52 /**
    41 static inline uint MapLogX()  { return _map_log_x; }
    53  * Logarithm of the map size along the X side.
    42 /* The size of the map */
    54  * @note try to avoid using this one
    43 static inline uint MapSizeX() { return _map_size_x; }
    55  * @return 2^"return value" == MapSizeX()
    44 static inline uint MapSizeY() { return _map_size_y; }
    56  */
    45 /* The maximum coordinates */
    57 static inline uint MapLogX()
    46 static inline uint MapMaxX() { return _map_size_x - 1; }
    58 {
    47 static inline uint MapMaxY() { return _map_size_y - 1; }
    59 	extern uint _map_log_x;
    48 /* The number of tiles in the map */
    60 	return _map_log_x;
    49 static inline uint MapSize() { return _map_size; }
    61 }
       
    62 
       
    63 /**
       
    64  * Get the size of the map along the X
       
    65  * @return the number of tiles along the X of the map
       
    66  */
       
    67 static inline uint MapSizeX()
       
    68 {
       
    69 	extern uint _map_size_x;
       
    70 	return _map_size_x;
       
    71 }
       
    72 
       
    73 /**
       
    74  * Get the size of the map along the Y
       
    75  * @return the number of tiles along the Y of the map
       
    76  */
       
    77 static inline uint MapSizeY()
       
    78 {
       
    79 	extern uint _map_size_y;
       
    80 	return _map_size_y;
       
    81 }
       
    82 
       
    83 /**
       
    84  * Get the size of the map
       
    85  * @return the number of tiles of the map
       
    86  */
       
    87 static inline uint MapSize()
       
    88 {
       
    89 	extern uint _map_size;
       
    90 	return _map_size;
       
    91 }
       
    92 
       
    93 /**
       
    94  * Gets the maximum X coordinate within the map, including MP_VOID
       
    95  * @return the maximum X coordinate
       
    96  */
       
    97 static inline uint MapMaxX()
       
    98 {
       
    99 	return MapSizeX() - 1;
       
   100 }
       
   101 
       
   102 /**
       
   103  * Gets the maximum X coordinate within the map, including MP_VOID
       
   104  * @return the maximum X coordinate
       
   105  */
       
   106 static inline uint MapMaxY()
       
   107 {
       
   108 	return MapSizeY() - 1;
       
   109 }
    50 
   110 
    51 /* Scale a number relative to the map size */
   111 /* Scale a number relative to the map size */
    52 uint ScaleByMapSize(uint); // Scale relative to the number of tiles
   112 uint ScaleByMapSize(uint); // Scale relative to the number of tiles
    53 uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
   113 uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
    54 
   114 
    74 	return (y >> 4 << MapLogX()) + (x >> 4);
   134 	return (y >> 4 << MapLogX()) + (x >> 4);
    75 }
   135 }
    76 
   136 
    77 
   137 
    78 enum {
   138 enum {
    79 	INVALID_TILE = (TileIndex)-1
   139 	INVALID_TILE = (TileIndex)-1 ///< The very nice invalid tile marker
    80 };
   140 };
    81 
   141 
    82 enum {
   142 enum {
    83 	TILE_SIZE   = 16,   ///< Tiles are 16x16 "units" in size
   143 	TILE_SIZE   = 16,   ///< Tiles are 16x16 "units" in size
    84 	TILE_PIXELS = 32,   ///< a tile is 32x32 pixels
   144 	TILE_PIXELS = 32,   ///< a tile is 32x32 pixels
    85 	TILE_HEIGHT =  8,   ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
   145 	TILE_HEIGHT =  8,   ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
    86 };
   146 };
    87 
   147 
    88 
   148 
       
   149 /**
       
   150  * Get the X component of a tile
       
   151  * @param tile the tile to get the X component of
       
   152  * @return the X component
       
   153  */
    89 static inline uint TileX(TileIndex tile)
   154 static inline uint TileX(TileIndex tile)
    90 {
   155 {
    91 	return tile & MapMaxX();
   156 	return tile & MapMaxX();
    92 }
   157 }
    93 
   158 
       
   159 /**
       
   160  * Get the Y component of a tile
       
   161  * @param tile the tile to get the Y component of
       
   162  * @return the Y component
       
   163  */
    94 static inline uint TileY(TileIndex tile)
   164 static inline uint TileY(TileIndex tile)
    95 {
   165 {
    96 	return tile >> MapLogX();
   166 	return tile >> MapLogX();
    97 }
   167 }
    98 
   168 
   137 		return INVALID_TILE;
   207 		return INVALID_TILE;
   138 	else
   208 	else
   139 		return TileXY(x, y);
   209 		return TileXY(x, y);
   140 }
   210 }
   141 
   211 
       
   212 /**
       
   213  * Returns the diff between two tiles
       
   214  *
       
   215  * @param tile_a from tile
       
   216  * @param tile_b to tile
       
   217  * @return the difference between tila_a and tile_b
       
   218  */
       
   219 static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
       
   220 {
       
   221 	TileIndexDiffC difference;
       
   222 
       
   223 	difference.x = TileX(tile_a) - TileX(tile_b);
       
   224 	difference.y = TileY(tile_a) - TileY(tile_b);
       
   225 
       
   226 	return difference;
       
   227 }
       
   228 
   142 /* Functions to calculate distances */
   229 /* Functions to calculate distances */
   143 uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
   230 uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
   144 uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared
   231 uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared
   145 uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm
   232 uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm
   146 uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan
   233 uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan