OpenTTD
map_func.h
Go to the documentation of this file.
1 /* $Id: map_func.h 24900 2013-01-08 22:46:42Z planetmaker $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef MAP_FUNC_H
13 #define MAP_FUNC_H
14 
15 #include "core/math_func.hpp"
16 #include "tile_type.h"
17 #include "map_type.h"
18 #include "direction_func.h"
19 
20 extern uint _map_tile_mask;
21 
28 #define TILE_MASK(x) ((x) & _map_tile_mask)
29 
36 extern Tile *_m;
37 
44 extern TileExtended *_me;
45 
46 void AllocateMap(uint size_x, uint size_y);
47 
53 static inline uint MapLogX()
54 {
55  extern uint _map_log_x;
56  return _map_log_x;
57 }
58 
64 static inline uint MapLogY()
65 {
66  extern uint _map_log_y;
67  return _map_log_y;
68 }
69 
74 static inline uint MapSizeX()
75 {
76  extern uint _map_size_x;
77  return _map_size_x;
78 }
79 
84 static inline uint MapSizeY()
85 {
86  extern uint _map_size_y;
87  return _map_size_y;
88 }
89 
94 static inline uint MapSize()
95 {
96  extern uint _map_size;
97  return _map_size;
98 }
99 
104 static inline uint MapMaxX()
105 {
106  return MapSizeX() - 1;
107 }
108 
113 static inline uint MapMaxY()
114 {
115  return MapSizeY() - 1;
116 }
117 
124 static inline uint ScaleByMapSize(uint n)
125 {
126  /* Subtract 12 from shift in order to prevent integer overflow
127  * for large values of n. It's safe since the min mapsize is 64x64. */
128  return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
129 }
130 
131 
138 static inline uint ScaleByMapSize1D(uint n)
139 {
140  /* Normal circumference for the X+Y is 256+256 = 1<<9
141  * Note, not actually taking the full circumference into account,
142  * just half of it. */
143  return CeilDiv((n << MapLogX()) + (n << MapLogY()), 1 << 9);
144 }
145 
156 typedef int32 TileIndexDiff;
157 
165 static inline TileIndex TileXY(uint x, uint y)
166 {
167  return (y << MapLogX()) + x;
168 }
169 
181 static inline TileIndexDiff TileDiffXY(int x, int y)
182 {
183  /* Multiplication gives much better optimization on MSVC than shifting.
184  * 0 << shift isn't optimized to 0 properly.
185  * Typically x and y are constants, and then this doesn't result
186  * in any actual multiplication in the assembly code.. */
187  return (y * MapSizeX()) + x;
188 }
189 
196 static inline TileIndex TileVirtXY(uint x, uint y)
197 {
198  return (y >> 4 << MapLogX()) + (x >> 4);
199 }
200 
201 
207 static inline uint TileX(TileIndex tile)
208 {
209  return tile & MapMaxX();
210 }
211 
217 static inline uint TileY(TileIndex tile)
218 {
219  return tile >> MapLogX();
220 }
221 
233 {
234  return (tidc.y << MapLogX()) + tidc.x;
235 }
236 
237 
238 #ifndef _DEBUG
239 
246  #define TILE_ADD(x, y) ((x) + (y))
247 #else
248  extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
249  const char *exp, const char *file, int line);
250  #define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
251 #endif
252 
260 #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
261 
262 TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
263 
271 {
273 
274  assert(IsValidDiagDirection(dir));
275  return _tileoffs_by_diagdir[dir];
276 }
277 
285 {
287 
288  assert(IsValidDirection(dir));
289  return _tileoffs_by_dir[dir];
290 }
291 
303 {
304  int x = TileX(tile) + diff.x;
305  int y = TileY(tile) + diff.y;
306  /* Negative value will become big positive value after cast */
307  if ((uint)x >= MapSizeX() || (uint)y >= MapSizeY()) return INVALID_TILE;
308  return TileXY(x, y);
309 }
310 
319 {
320  TileIndexDiffC difference;
321 
322  difference.x = TileX(tile_a) - TileX(tile_b);
323  difference.y = TileY(tile_a) - TileY(tile_b);
324 
325  return difference;
326 }
327 
328 /* Functions to calculate distances */
335 
344 {
346 
347  assert(IsValidDiagDirection(dir));
348  return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
349 }
350 
358 {
360 
361  assert(IsValidDirection(dir));
362  return ToTileIndexDiff(_tileoffs_by_dir[dir]);
363 }
364 
373 {
374  return TILE_ADD(tile, TileOffsByDiagDir(dir));
375 }
376 
384 static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
385 {
386  int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
387  int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
388  if (dx == 0) {
389  if (dy == 0) return INVALID_DIAGDIR;
390  return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
391  } else {
392  if (dy != 0) return INVALID_DIAGDIR;
393  return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
394  }
395 }
396 
404 typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
405 
406 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
407 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
408 
414 static inline TileIndex RandomTileSeed(uint32 r)
415 {
416  return TILE_MASK(r);
417 }
418 
425 #define RandomTile() RandomTileSeed(Random())
426 
427 uint GetClosestWaterDistance(TileIndex tile, bool water);
428 
429 #endif /* MAP_FUNC_H */
uint _map_size
The number of tiles on the map.
Definition: map.cpp:29
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:74
Tile * _m
Pointer to the tile-array.
Definition: map.cpp:32
uint _map_size_x
Size of the map along the X.
Definition: map.cpp:27
static bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:84
TileExtended * _me
Pointer to the extended tile-array.
Definition: map.cpp:33
Types related to maps.
static TileIndexDiffC TileIndexDiffCByDir(Direction dir)
Returns the TileIndexDiffC offset from a Direction.
Definition: map_func.h:284
Flag for an invalid DiagDirection.
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
Function performing a search around a center tile and going outward, thus in circle.
Definition: map.cpp:260
static uint MapLogX()
Logarithm of the map size along the X side.
Definition: map_func.h:53
static uint ScaleByMapSize(uint n)
Scales the given value by the map size, where the given value is for a 256 by 256 map...
Definition: map_func.h:124
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:372
Northeast, upper right on your monitor.
static uint MapLogY()
Logarithm of the map size along the y side.
Definition: map_func.h:64
uint DistanceManhattan(TileIndex, TileIndex)
also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads) ...
Definition: map.cpp:159
Direction
Defines the 8 directions on the map.
const TileIndexDiffC _tileoffs_by_dir[]
&#39;Lookup table&#39; for tile offsets given a Direction
int16 y
The y value of the coordinate.
Definition: map_type.h:60
static DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
Definition: map_func.h:384
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
uint _map_size_y
Size of the map along the Y.
Definition: map.cpp:28
uint DistanceMaxPlusManhattan(TileIndex, TileIndex)
Max + Manhattan.
Definition: map.cpp:207
static TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
Definition: map_func.h:270
Southeast.
Southwest.
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges. ...
Definition: map.cpp:116
int16 x
The x value of the coordinate.
Definition: map_type.h:59
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:316
Used for iterations.
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
void AllocateMap(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition: map.cpp:41
Data that is stored per tile.
Definition: map_type.h:19
Northwest.
const TileIndexDiffC _tileoffs_by_diagdir[]
&#39;Lookup table&#39; for tile offsets given a DiagDirection
DiagDirection
Enumeration for diagonal directions.
Data that is stored per tile.
Definition: map_type.h:35
Integer math functions.
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition: map.cpp:342
Used to iterate.
static uint ScaleByMapSize1D(uint n)
Scales the given value by the maps circumference, where the given value is for a 256 by 256 map...
Definition: map_func.h:138
A pair-construct of a TileIndexDiff.
Definition: map_type.h:58
uint DistanceFromEdge(TileIndex)
shortest distance from any edge of the map
Definition: map.cpp:219
uint DistanceSquare(TileIndex, TileIndex)
euclidian- or L2-Norm squared
Definition: map.cpp:176
static TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between to tiles from a TileIndexDiffC struct.
Definition: map_func.h:232
uint DistanceFromEdgeDir(TileIndex, DiagDirection)
distance from the map edge in given direction
Definition: map.cpp:236
static TileIndex RandomTileSeed(uint32 r)
Get a random tile out of a given seed.
Definition: map_func.h:414
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
uint DistanceMax(TileIndex, TileIndex)
also known as L-Infinity-Norm
Definition: map.cpp:191
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
static TileIndexDiff TileOffsByDir(Direction dir)
Convert a Direction to a TileIndexDiff.
Definition: map_func.h:357
bool TestTileOnSearchProc(TileIndex tile, void *user_data)
A callback function type for searching tiles.
Definition: map_func.h:404
uint _map_log_x
2^_map_log_x == _map_size_x
Definition: map.cpp:25
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:181
uint _map_tile_mask
_map_size - 1 (to mask the mapsize)
Definition: map.cpp:30
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
Different functions related to conversions between directions.
Types related to tiles.
static TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
Definition: map_func.h:302
#define TILE_ADD(x, y)
Adds to tiles together.
Definition: map_func.h:246
#define TILE_MASK(x)
&#39;Wraps&#39; the given tile to it is within the map.
Definition: map_func.h:28
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
static TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
Returns the diff between two tiles.
Definition: map_func.h:318
uint _map_log_y
2^_map_log_y == _map_size_y
Definition: map.cpp:26