OpenTTD Source  1.11.0-beta2
map_func.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef MAP_FUNC_H
11 #define MAP_FUNC_H
12 
13 #include "core/math_func.hpp"
14 #include "tile_type.h"
15 #include "map_type.h"
16 #include "direction_func.h"
17 
18 extern uint _map_tile_mask;
19 
26 #define TILE_MASK(x) ((x) & _map_tile_mask)
27 
34 extern Tile *_m;
35 
42 extern TileExtended *_me;
43 
44 void AllocateMap(uint size_x, uint size_y);
45 
51 static inline uint MapLogX()
52 {
53  extern uint _map_log_x;
54  return _map_log_x;
55 }
56 
62 static inline uint MapLogY()
63 {
64  extern uint _map_log_y;
65  return _map_log_y;
66 }
67 
72 static inline uint MapSizeX()
73 {
74  extern uint _map_size_x;
75  return _map_size_x;
76 }
77 
82 static inline uint MapSizeY()
83 {
84  extern uint _map_size_y;
85  return _map_size_y;
86 }
87 
92 static inline uint MapSize()
93 {
94  extern uint _map_size;
95  return _map_size;
96 }
97 
102 static inline uint MapMaxX()
103 {
104  return MapSizeX() - 1;
105 }
106 
111 static inline uint MapMaxY()
112 {
113  return MapSizeY() - 1;
114 }
115 
122 static inline uint ScaleByMapSize(uint n)
123 {
124  /* Subtract 12 from shift in order to prevent integer overflow
125  * for large values of n. It's safe since the min mapsize is 64x64. */
126  return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
127 }
128 
129 
136 static inline uint ScaleByMapSize1D(uint n)
137 {
138  /* Normal circumference for the X+Y is 256+256 = 1<<9
139  * Note, not actually taking the full circumference into account,
140  * just half of it. */
141  return CeilDiv((n << MapLogX()) + (n << MapLogY()), 1 << 9);
142 }
143 
154 typedef int32 TileIndexDiff;
155 
163 static inline TileIndex TileXY(uint x, uint y)
164 {
165  return (y << MapLogX()) + x;
166 }
167 
179 static inline TileIndexDiff TileDiffXY(int x, int y)
180 {
181  /* Multiplication gives much better optimization on MSVC than shifting.
182  * 0 << shift isn't optimized to 0 properly.
183  * Typically x and y are constants, and then this doesn't result
184  * in any actual multiplication in the assembly code.. */
185  return (y * MapSizeX()) + x;
186 }
187 
194 static inline TileIndex TileVirtXY(uint x, uint y)
195 {
196  return (y >> 4 << MapLogX()) + (x >> 4);
197 }
198 
199 
205 static inline uint TileX(TileIndex tile)
206 {
207  return tile & MapMaxX();
208 }
209 
215 static inline uint TileY(TileIndex tile)
216 {
217  return tile >> MapLogX();
218 }
219 
231 {
232  return (tidc.y << MapLogX()) + tidc.x;
233 }
234 
235 
236 #ifndef _DEBUG
237 
244 # define TILE_ADD(x, y) ((x) + (y))
245 #else
246  extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
247  const char *exp, const char *file, int line);
248 # define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
249 #endif
250 
258 #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
259 
260 TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
261 
269 {
271 
272  assert(IsValidDiagDirection(dir));
273  return _tileoffs_by_diagdir[dir];
274 }
275 
283 {
285 
286  assert(IsValidDirection(dir));
287  return _tileoffs_by_dir[dir];
288 }
289 
301 {
302  int x = TileX(tile) + diff.x;
303  int y = TileY(tile) + diff.y;
304  /* Negative value will become big positive value after cast */
305  if ((uint)x >= MapSizeX() || (uint)y >= MapSizeY()) return INVALID_TILE;
306  return TileXY(x, y);
307 }
308 
317 {
318  TileIndexDiffC difference;
319 
320  difference.x = TileX(tile_a) - TileX(tile_b);
321  difference.y = TileY(tile_a) - TileY(tile_b);
322 
323  return difference;
324 }
325 
326 /* Functions to calculate distances */
333 
342 {
344 
345  assert(IsValidDiagDirection(dir));
347 }
348 
356 {
358 
359  assert(IsValidDirection(dir));
360  return ToTileIndexDiff(_tileoffs_by_dir[dir]);
361 }
362 
370 static inline TileIndex TileAddByDir(TileIndex tile, Direction dir)
371 {
372  return TILE_ADD(tile, TileOffsByDir(dir));
373 }
374 
383 {
384  return TILE_ADD(tile, TileOffsByDiagDir(dir));
385 }
386 
394 static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
395 {
396  int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
397  int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
398  if (dx == 0) {
399  if (dy == 0) return INVALID_DIAGDIR;
400  return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
401  } else {
402  if (dy != 0) return INVALID_DIAGDIR;
403  return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
404  }
405 }
406 
414 typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
415 
416 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
417 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
418 
424 static inline TileIndex RandomTileSeed(uint32 r)
425 {
426  return TILE_MASK(r);
427 }
428 
435 #define RandomTile() RandomTileSeed(Random())
436 
437 uint GetClosestWaterDistance(TileIndex tile, bool water);
438 
439 #endif /* MAP_FUNC_H */
MapLogX
static uint MapLogX()
Logarithm of the map size along the X side.
Definition: map_func.h:51
_map_log_y
uint _map_log_y
2^_map_log_y == _map_size_y
Definition: map.cpp:24
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:80
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
AddTileIndexDiffCWrap
static TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
Definition: map_func.h:300
TILE_ADD
#define TILE_ADD(x, y)
Adds to tiles together.
Definition: map_func.h:244
_tileoffs_by_dir
const TileIndexDiffC _tileoffs_by_dir[]
'Lookup table' for tile offsets given a Direction
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
TestTileOnSearchProc
bool TestTileOnSearchProc(TileIndex tile, void *user_data)
A callback function type for searching tiles.
Definition: map_func.h:414
_map_size
uint _map_size
The number of tiles on the map.
Definition: map.cpp:27
math_func.hpp
TileAddByDir
static TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition: map_func.h:370
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:83
IsValidDiagDirection
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:21
CircularTileSearch
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:258
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:82
TILE_MASK
#define TILE_MASK(x)
'Wraps' the given tile to it is within the map.
Definition: map_func.h:26
_tileoffs_by_diagdir
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
TileIndexToTileIndexDiffC
static TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
Returns the diff between two tiles.
Definition: map_func.h:316
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
_map_size_x
uint _map_size_x
Size of the map along the X.
Definition: map.cpp:25
_map_tile_mask
uint _map_tile_mask
_map_size - 1 (to mask the mapsize)
Definition: map.cpp:28
ToTileIndexDiff
static TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between to tiles from a TileIndexDiffC struct.
Definition: map_func.h:230
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:81
TileIndexDiffC::y
int16 y
The y value of the coordinate.
Definition: map_type.h:59
DistanceSquare
uint DistanceSquare(TileIndex, TileIndex)
euclidian- or L2-Norm squared
Definition: map.cpp:174
DistanceFromEdgeDir
uint DistanceFromEdgeDir(TileIndex, DiagDirection)
distance from the map edge in given direction
Definition: map.cpp:234
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
DistanceManhattan
uint DistanceManhattan(TileIndex, TileIndex)
also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
Definition: map.cpp:157
TileIndexDiff
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:154
map_type.h
TileOffsByDir
static TileIndexDiff TileOffsByDir(Direction dir)
Convert a Direction to a TileIndexDiff.
Definition: map_func.h:355
TileAddByDiagDir
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:382
IsValidDirection
static bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Definition: direction_func.h:32
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:84
DistanceFromEdge
uint DistanceFromEdge(TileIndex)
shortest distance from any edge of the map
Definition: map.cpp:217
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
_m
Tile * _m
Pointer to the tile-array.
Definition: map.cpp:30
_map_log_x
uint _map_log_x
2^_map_log_x == _map_size_x
Definition: map.cpp:23
Tile
Data that is stored per tile.
Definition: map_type.h:17
TileIndexDiffC
A pair-construct of a TileIndexDiff.
Definition: map_type.h:57
AllocateMap
void AllocateMap(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition: map.cpp:39
ScaleByMapSize
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:122
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:111
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
DiagdirBetweenTiles
static DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
Definition: map_func.h:394
DistanceMaxPlusManhattan
uint DistanceMaxPlusManhattan(TileIndex, TileIndex)
Max + Manhattan.
Definition: map.cpp:205
tile_type.h
DIR_END
@ DIR_END
Used to iterate.
Definition: direction_type.h:34
TileExtended
Data that is stored per tile.
Definition: map_type.h:33
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
ScaleByMapSize1D
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:136
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:102
DistanceMax
uint DistanceMax(TileIndex, TileIndex)
also known as L-Infinity-Norm
Definition: map.cpp:189
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
CeilDiv
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:254
_map_size_y
uint _map_size_y
Size of the map along the Y.
Definition: map.cpp:26
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:79
TileIndexDiffCByDiagDir
static TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
Definition: map_func.h:268
GetClosestWaterDistance
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition: map.cpp:340
MapLogY
static uint MapLogY()
Logarithm of the map size along the y side.
Definition: map_func.h:62
TileAddWrap
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:114
_me
TileExtended * _me
Pointer to the extended tile-array.
Definition: map.cpp:31
direction_func.h
RandomTileSeed
static TileIndex RandomTileSeed(uint32 r)
Get a random tile out of a given seed.
Definition: map_func.h:424
TileIndexDiffCByDir
static TileIndexDiffC TileIndexDiffCByDir(Direction dir)
Returns the TileIndexDiffC offset from a Direction.
Definition: map_func.h:282
TileVirtXY
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:194
TileIndexDiffC::x
int16 x
The x value of the coordinate.
Definition: map_type.h:58