map.h
author matthijs
Mon, 31 Jan 2005 11:23:10 +0000
changeset 1247 01711347f9ac
parent 1245 768d9bc95aaa
child 1330 8a67d04016ce
permissions -rw-r--r--
(svn r1751) - Feature: New PathFinder (NPF).
- Supports trains, road vehicles and ships.
- Uses A* pathfinding (same codebase as the new ai).
- Currently unlimited search depth, so might perform badly on large maps/networks (especially ships).
- Will always find a route if there is one.
- Allows custom penalties for obstacles to be set in openttd.cfg (npf_ values).
- With NPF enabled, ships can have orders that are very far apart. Be careful, this will break (ships get lost) when the old pathfinder is used again.
- Feature: Disabling 90 degree turns for trains and ships.
- Requires NPF to be enabled.
- Ships and trains can no longer make weird 90 degree turns on tile borders.
- Codechange: Removed table/directions.h.
- table/directions.h contained ugly static tables but was included more than once. The tables, along with a few new ones are in npf.[ch] now. Better suggestions for a location?
- Fix: Binary heap in queue.c did not allocate enough space, resulting in a segfault.
- Codechange: Rewritten FindFirstBit2x64, added KillFirstBit2x64.
- Codechange: Introduced constant INVALID_TILE, to replace the usage of 0 as an invalid tile. Also replaces TILE_WRAPPED.
- Codechange: Moved TileAddWrap() to map.[ch]
- Add TileIndexDiffCByDir(), TileIndexDiffCByDir().
- Codechange: Moved IsTrainStationTile() to station.h
- Add: IsRoadStationTile() and GetRoadStationDir().
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     1
#ifndef MAP_H
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     2
#define MAP_H
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     3
1210
05975e9167c4 (svn r1714) Add missing include
tron
parents: 1209
diff changeset
     4
#include "stdafx.h"
05975e9167c4 (svn r1714) Add missing include
tron
parents: 1209
diff changeset
     5
927
28f45a22a564 (svn r1415) Move TILE_FROM_XY and TILE_XY to map.h and push TILE_[XY] bits from map.h into map.c.
tron
parents: 926
diff changeset
     6
#define TILE_FROM_XY(x,y) (int)((((y) >> 4) << MapLogX()) + ((x) >> 4))
1174
27e386195965 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
     7
#define TILE_XY(x,y) (((y) << MapLogX()) + (x))
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     8
979
f12f96116cdd (svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents: 955
diff changeset
     9
#define TILE_MASK(x) ((x) & ((1 << (MapLogX() + MapLogY())) - 1))
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
    10
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    11
extern byte   *_map_type_and_height;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    12
extern byte   *_map_owner;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    13
extern uint16 *_map2;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    14
extern byte   *_map3_lo;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    15
extern byte   *_map3_hi;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    16
extern byte   *_map5;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    17
extern byte   *_map_extra_bits;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    18
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    19
void InitMap(uint log_x, uint log_y);
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
    20
689
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    21
// binary logarithm of the map size, try to avoid using this one
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    22
static inline uint MapLogX(void)  { extern uint _map_log_x; return _map_log_x; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    23
static inline uint MapLogY(void)  { extern uint _map_log_y; return _map_log_y; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    24
/* The size of the map */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    25
static inline uint MapSizeX(void) { return 1 << MapLogX(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    26
static inline uint MapSizeY(void) { return 1 << MapLogY(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    27
/* The maximum coordinates */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    28
static inline uint MapMaxX(void) { return MapSizeX() - 1; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    29
static inline uint MapMaxY(void) { return MapSizeY() - 1; }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    30
/* The number of tiles in the map */
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    31
static inline uint MapSize(void) { return MapSizeX() * MapSizeY(); }
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    32
1202
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    33
// Scale a number relative to the map size
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    34
uint ScaleByMapSize(uint); // Scale relative to the number of tiles
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    35
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    36
1174
27e386195965 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
    37
typedef uint32 TileIndex;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    38
enum {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    39
	INVALID_TILE = (uint32) -1
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    40
};
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
    41
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
    42
static inline uint TileX(TileIndex tile)
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
    43
{
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
    44
	return tile & MapMaxX();
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
    45
}
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
    46
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
    47
static inline uint TileY(TileIndex tile)
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
    48
{
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
    49
	return tile >> MapLogX();
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
    50
}
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
    51
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
    52
1174
27e386195965 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1059
diff changeset
    53
typedef int32 TileIndexDiff;
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    54
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    55
typedef struct TileIndexDiffC {
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    56
	int16 x;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    57
	int16 y;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    58
} TileIndexDiffC;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    59
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    60
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    61
{
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    62
	return (tidc.y << MapLogX()) + tidc.x;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    63
}
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    64
955
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    65
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    66
#ifndef _DEBUG
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    67
	#define TILE_ADD(x,y) ((x) + (y))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    68
#else
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    69
	extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    70
		const char *exp, const char *file, int line);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    71
	#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    72
#endif
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    73
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    74
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x, y))
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    75
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    76
uint TileAddWrap(TileIndex tile, int addx, int addy);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    77
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    78
static inline TileIndexDiffC TileIndexDiffCByDir(uint dir) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    79
	extern const TileIndexDiffC _tileoffs_by_dir[4];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    80
	return _tileoffs_by_dir[dir];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    81
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    82
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    83
/* Returns tile + the diff given in diff. If the result tile would end up
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    84
 * outside of the map, INVALID_TILE is returned instead.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    85
 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    86
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    87
	int x = TileX(tile) + diff.x;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    88
	int y = TileY(tile) + diff.y;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    89
	if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY())
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    90
		return INVALID_TILE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    91
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    92
		return TILE_XY(x, y);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    93
}
955
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    94
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
    95
// Functions to calculate distances
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
    96
uint DistanceManhattan(TileIndex, TileIndex); // also known as L1-Norm
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
    97
uint DistanceSquare(TileIndex, TileIndex); // euclidian- or L2-Norm squared
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
    98
uint DistanceMax(TileIndex, TileIndex); // also known as L-Infinity-Norm
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
    99
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); // Max + Manhattan
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   100
uint DistanceFromEdge(TileIndex); // shortest distance from any edge of the map
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   101
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   102
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   103
static inline TileIndexDiff TileOffsByDir(uint dir)
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   104
{
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   105
	extern const TileIndexDiffC _tileoffs_by_dir[4];
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   106
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   107
	assert(dir < lengthof(_tileoffs_by_dir));
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   108
	return ToTileIndexDiff(_tileoffs_by_dir[dir]);
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   109
}
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   110
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
   111
#endif