map.h
author bjarni
Sat, 07 Oct 2006 13:58:08 +0000
changeset 4765 d109bfb2cc73
parent 4561 98779da22b99
child 4846 8cf926d11bfa
permissions -rw-r--r--
(svn r6679) -Feature: [train build window] added filter for wagons, engines or both in the display
-Codechange: [train build window] to get rid of a really ugly hack, the train build list is now generated in one loop and stored in an array
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     3
#ifndef MAP_H
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     4
#define MAP_H
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     5
1210
981b36779dd7 (svn r1714) Add missing include
tron
parents: 1209
diff changeset
     6
#include "stdafx.h"
981b36779dd7 (svn r1714) Add missing include
tron
parents: 1209
diff changeset
     7
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
     8
// Putting externs inside inline functions seems to confuse the aliasing
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
     9
// checking on MSVC6. Never use those variables directly.
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    10
extern uint _map_log_x;
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    11
extern uint _map_size_x;
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    12
extern uint _map_size_y;
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    13
extern uint _map_tile_mask;
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    14
extern uint _map_size;
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    15
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    16
#define TILE_MASK(x) ((x) & _map_tile_mask)
1394
79cb56d80a3a (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1330
diff changeset
    17
#define TILE_ASSERT(x) assert(TILE_MASK(x) == (x));
926
a6d140a6a4de (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
    18
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    19
typedef struct Tile {
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    20
	byte type_height;
2360
4e4ebe18e448 (svn r2886) Rename the "owner" attribute to "m1", because when it stores an owner it is accessed by [GS]etOwner anyway and when it doesn't store an owner, but arbitrary data, accessing a field called "owner" is confusing.
tron
parents: 2186
diff changeset
    21
	byte m1;
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    22
	uint16 m2;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    23
	byte m3;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    24
	byte m4;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    25
	byte m5;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    26
	byte extra;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    27
} Tile;
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    28
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
    29
extern Tile* _m;
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    30
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    31
void AllocateMap(uint size_x, uint size_y);
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    32
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    33
// binary logarithm of the map size, try to avoid using this one
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    34
static inline uint MapLogX(void)  { return _map_log_x; }
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    35
/* The size of the map */
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    36
static inline uint MapSizeX(void) { return _map_size_x; }
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    37
static inline uint MapSizeY(void) { return _map_size_y; }
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    38
/* The maximum coordinates */
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    39
static inline uint MapMaxX(void) { return _map_size_x - 1; }
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    40
static inline uint MapMaxY(void) { return _map_size_y - 1; }
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    41
/* The number of tiles in the map */
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    42
static inline uint MapSize(void) { return _map_size; }
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
    43
1202
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    44
// Scale a number relative to the map size
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    45
uint ScaleByMapSize(uint); // Scale relative to the number of tiles
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    46
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
    47
1174
6a5e747f3ba6 (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
    48
typedef uint32 TileIndex;
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    49
typedef int32 TileIndexDiff;
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    50
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    51
static inline TileIndex TileXY(uint x, uint y)
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    52
{
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    53
	return (y * MapSizeX()) + x;
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    54
}
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    55
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    56
static inline TileIndexDiff TileDiffXY(int x, int y)
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    57
{
2051
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    58
	// Multiplication gives much better optimization on MSVC than shifting.
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    59
	// 0 << shift isn't optimized to 0 properly.
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    60
	// Typically x and y are constants, and then this doesn't result
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    61
	// in any actual multiplication in the assembly code..
e369160ce2f3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    62
	return (y * MapSizeX()) + x;
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
    63
}
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1247
diff changeset
    64
1980
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
    65
static inline TileIndex TileVirtXY(uint x, uint y)
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
    66
{
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
    67
	return (y >> 4 << MapLogX()) + (x >> 4);
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
    68
}
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
    69
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 2794
diff changeset
    70
typedef enum Owner {
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 2794
diff changeset
    71
	OWNER_TOWN      = 0x0F, // a town owns the tile
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 2794
diff changeset
    72
	OWNER_NONE      = 0x10, // nobody owns the tile
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 2794
diff changeset
    73
	OWNER_WATER     = 0x11, // "water" owns the tile
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 2794
diff changeset
    74
	OWNER_SPECTATOR = 0xFF, // spectator in MP or in scenario editor
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1247
diff changeset
    75
} Owner;
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1247
diff changeset
    76
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    77
enum {
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
    78
	INVALID_TILE = (TileIndex)-1
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    79
};
926
a6d140a6a4de (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
    80
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    81
enum {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    82
	TILE_SIZE   = 16,   /* Tiles are 16x16 "units" in size */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    83
	TILE_PIXELS = 32,   /* a tile is 32x32 pixels */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4000
diff changeset
    84
	TILE_HEIGHT =  8,   /* The standard height-difference between tiles on two levels is 8 (z-diff 8) */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    85
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    86
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1679
diff changeset
    87
926
a6d140a6a4de (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
    88
static inline uint TileX(TileIndex tile)
a6d140a6a4de (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
    89
{
a6d140a6a4de (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
    90
	return tile & MapMaxX();
a6d140a6a4de (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
    91
}
a6d140a6a4de (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
    92
a6d140a6a4de (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
    93
static inline uint TileY(TileIndex tile)
a6d140a6a4de (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
    94
{
a6d140a6a4de (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
    95
	return tile >> MapLogX();
a6d140a6a4de (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
    96
}
a6d140a6a4de (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
    97
a6d140a6a4de (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
    98
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
    99
typedef struct TileIndexDiffC {
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   100
	int16 x;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   101
	int16 y;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   102
} TileIndexDiffC;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   103
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   104
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   105
{
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   106
	return (tidc.y << MapLogX()) + tidc.x;
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   107
}
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   108
955
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   109
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   110
#ifndef _DEBUG
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   111
	#define TILE_ADD(x,y) ((x) + (y))
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   112
#else
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   113
	extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   114
		const char *exp, const char *file, int line);
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   115
	#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   116
#endif
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   117
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   118
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
955
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   119
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   120
uint TileAddWrap(TileIndex tile, int addx, int addy);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   121
4561
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   122
static inline TileIndexDiffC TileIndexDiffCByDiagDir(uint dir) {
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   123
	extern const TileIndexDiffC _tileoffs_by_diagdir[4];
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   124
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   125
	assert(dir < lengthof(_tileoffs_by_diagdir));
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   126
	return _tileoffs_by_diagdir[dir];
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   127
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   128
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   129
/* Returns tile + the diff given in diff. If the result tile would end up
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   130
 * outside of the map, INVALID_TILE is returned instead.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   131
 */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   132
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   133
	int x = TileX(tile) + diff.x;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   134
	int y = TileY(tile) + diff.y;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   135
	if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY())
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   136
		return INVALID_TILE;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   137
	else
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   138
		return TileXY(x, y);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   139
}
955
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
   140
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   141
// Functions to calculate distances
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   142
uint DistanceManhattan(TileIndex, TileIndex); // also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   143
uint DistanceSquare(TileIndex, TileIndex); // euclidian- or L2-Norm squared
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   144
uint DistanceMax(TileIndex, TileIndex); // also known as L-Infinity-Norm
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   145
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); // Max + Manhattan
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   146
uint DistanceFromEdge(TileIndex); // shortest distance from any edge of the map
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   147
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   148
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   149
#define BEGIN_TILE_LOOP(var,w,h,tile)                      \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   150
	{                                                        \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   151
		int h_cur = h;                                         \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   152
		uint var = tile;                                       \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   153
		do {                                                   \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   154
			int w_cur = w;                                       \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   155
			do {
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   156
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   157
#define END_TILE_LOOP(var,w,h,tile)                        \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   158
			} while (++var, --w_cur != 0);                       \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   159
		} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   160
	}
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   161
4561
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   162
static inline TileIndexDiff TileOffsByDiagDir(uint dir)
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   163
{
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   164
	extern const TileIndexDiffC _tileoffs_by_diagdir[4];
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   165
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   166
	assert(dir < lengthof(_tileoffs_by_diagdir));
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   167
	return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   168
}
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   169
4561
98779da22b99 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   170
static inline TileIndexDiff TileOffsByDir(uint dir)
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   171
{
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   172
	extern const TileIndexDiffC _tileoffs_by_dir[8];
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   173
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   174
	assert(dir < lengthof(_tileoffs_by_dir));
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   175
	return ToTileIndexDiff(_tileoffs_by_dir[dir]);
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   176
}
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   177
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   178
/* Approximation of the length of a straight track, relative to a diagonal
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   179
 * track (ie the size of a tile side). #defined instead of const so it can
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   180
 * stay integer. (no runtime float operations) Is this needed?
1679
9a654f6b85b9 (svn r2183) - Fix: Removed brackets from around STRAIGHT_TRACK_LENGTH, they caused preliminary rounding...
matthijs
parents: 1677
diff changeset
   181
 * Watch out! There are _no_ brackets around here, to prevent intermediate
9a654f6b85b9 (svn r2183) - Fix: Removed brackets from around STRAIGHT_TRACK_LENGTH, they caused preliminary rounding...
matthijs
parents: 1677
diff changeset
   182
 * rounding! Be careful when using this!
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   183
 * This value should be sqrt(2)/2 ~ 0.7071 */
1679
9a654f6b85b9 (svn r2183) - Fix: Removed brackets from around STRAIGHT_TRACK_LENGTH, they caused preliminary rounding...
matthijs
parents: 1677
diff changeset
   184
#define STRAIGHT_TRACK_LENGTH 7071/10000
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   185
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2403
diff changeset
   186
#endif /* MAP_H */