|
@@ -125,41 +125,55 @@ static inline TileIndexDiffC TileIndexDi
|
|
|
return _tileoffs_by_dir[dir];
|
|
|
}
|
|
|
|
|
|
/* Returns tile + the diff given in diff. If the result tile would end up
|
|
|
* outside of the map, INVALID_TILE is returned instead.
|
|
|
*/
|
|
|
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) {
|
|
|
int x = TileX(tile) + diff.x;
|
|
|
int y = TileY(tile) + diff.y;
|
|
|
if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY())
|
|
|
return INVALID_TILE;
|
|
|
else
|
|
|
return TileXY(x, y);
|
|
|
}
|
|
|
|
|
|
// Functions to calculate distances
|
|
|
uint DistanceManhattan(TileIndex, TileIndex); // also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
|
|
|
uint DistanceSquare(TileIndex, TileIndex); // euclidian- or L2-Norm squared
|
|
|
uint DistanceMax(TileIndex, TileIndex); // also known as L-Infinity-Norm
|
|
|
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); // Max + Manhattan
|
|
|
uint DistanceTrack(TileIndex, TileIndex); // Returns the shortest distance one could go over tracks
|
|
|
uint DistanceFromEdge(TileIndex); // shortest distance from any edge of the map
|
|
|
|
|
|
|
|
|
#define BEGIN_TILE_LOOP(var,w,h,tile) \
|
|
|
{ \
|
|
|
int h_cur = h; \
|
|
|
uint var = tile; \
|
|
|
do { \
|
|
|
int w_cur = w; \
|
|
|
do {
|
|
|
|
|
|
#define END_TILE_LOOP(var,w,h,tile) \
|
|
|
} while (++var, --w_cur != 0); \
|
|
|
} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \
|
|
|
}
|
|
|
|
|
|
|
|
|
static inline TileIndexDiff TileOffsByDir(uint dir)
|
|
|
{
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[4];
|
|
|
|
|
|
assert(dir < lengthof(_tileoffs_by_dir));
|
|
|
return ToTileIndexDiff(_tileoffs_by_dir[dir]);
|
|
|
}
|
|
|
|
|
|
/* Approximation of the length of a straight track, relative to a diagonal
|
|
|
* track (ie the size of a tile side). #defined instead of const so it can
|
|
|
* stay integer. (no runtime float operations) Is this needed?
|
|
|
* Watch out! There are _no_ brackets around here, to prevent intermediate
|
|
|
* rounding! Be careful when using this!
|
|
|
* This value should be sqrt(2)/2 ~ 0.7071 */
|
|
|
#define STRAIGHT_TRACK_LENGTH 7071/10000
|
|
|
|
|
|
#endif
|