author | celestar |
Sun, 31 Dec 2006 16:08:00 +0000 | |
branch | custombridgeheads |
changeset 5614 | 623bae81fbd2 |
parent 5568 | 75f13d7bfaed |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1677
diff
changeset
|
4 |
#include "openttd.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1247
diff
changeset
|
5 |
#include "debug.h" |
1218 | 6 |
#include "functions.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2051
diff
changeset
|
7 |
#include "macros.h" |
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 |
#include "map.h" |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
9 |
#include "direction.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
10 |
|
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
11 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 /* VStudio 2005 is stupid! */ |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
12 |
/* Why the hell is that not in all MSVC headers?? */ |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
13 |
_CRTIMP void __cdecl _assert(void *, void *, unsigned); |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
14 |
#endif |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
15 |
|
1218 | 16 |
uint _map_log_x; |
2051 | 17 |
uint _map_size_x; |
18 |
uint _map_size_y; |
|
19 |
uint _map_tile_mask; |
|
20 |
uint _map_size; |
|
689 | 21 |
|
2049
ad0d49c916d4
(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:
2043
diff
changeset
|
22 |
Tile* _m = NULL; |
863
8d09f9331a80
(svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents:
817
diff
changeset
|
23 |
|
1218 | 24 |
|
2051 | 25 |
void AllocateMap(uint size_x, uint size_y) |
1218 | 26 |
{ |
2051 | 27 |
// Make sure that the map size is within the limits and that |
28 |
// the x axis size is a power of 2. |
|
29 |
if (size_x < 64 || size_x > 2048 || |
|
30 |
size_y < 64 || size_y > 2048 || |
|
31 |
(size_x&(size_x-1)) != 0 || |
|
32 |
(size_y&(size_y-1)) != 0) |
|
1244 | 33 |
error("Invalid map size"); |
1218 | 34 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5170
diff
changeset
|
35 |
DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); |
1218 | 36 |
|
2051 | 37 |
_map_log_x = FindFirstBit(size_x); |
38 |
_map_size_x = size_x; |
|
39 |
_map_size_y = size_y; |
|
40 |
_map_size = size_x * size_y; |
|
41 |
_map_tile_mask = _map_size - 1; |
|
1218 | 42 |
|
2049
ad0d49c916d4
(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:
2043
diff
changeset
|
43 |
free(_m); |
3717
360ddbd9fe85
(svn r4688) - Ensure the map memory is cleared after it is allocated. This fixes random deserts that sometimes occurred.
peter1138
parents:
2548
diff
changeset
|
44 |
_m = calloc(_map_size, sizeof(*_m)); |
1218 | 45 |
|
46 |
// XXX TODO handle memory shortage more gracefully |
|
2049
ad0d49c916d4
(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:
2043
diff
changeset
|
47 |
if (_m == NULL) error("Failed to allocate memory for the map"); |
1218 | 48 |
} |
900 | 49 |
|
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
|
50 |
|
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
|
51 |
#ifdef _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
|
52 |
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
|
53 |
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
|
54 |
{ |
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
|
55 |
int dx; |
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
|
56 |
int dy; |
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
|
57 |
uint x; |
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
|
58 |
uint 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
|
59 |
|
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
|
60 |
dx = add & MapMaxX(); |
957
d1dd9429e417
(svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents:
955
diff
changeset
|
61 |
if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX(); |
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
|
62 |
dy = (add - dx) / (int)MapSizeX(); |
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
|
63 |
|
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
|
64 |
x = TileX(tile) + dx; |
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 |
y = TileY(tile) + dy; |
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 |
|
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 |
if (x >= MapSizeX() || y >= MapSizeY()) { |
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 |
char buf[512]; |
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 |
|
5170
56c12a62af49
(svn r7280) -Codechange: Replace some sprintf() functions with the safer snprintf() functions
Darkvater
parents:
5118
diff
changeset
|
70 |
snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed", |
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
|
71 |
exp, tile, 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
|
72 |
#if !defined(_MSC_VER) |
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 |
fprintf(stderr, "%s:%d %s\n", file, line, buf); |
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 |
#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
|
75 |
_assert(buf, (char*)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
|
76 |
#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
|
77 |
} |
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
|
78 |
|
1981 | 79 |
assert(TileXY(x,y) == TILE_MASK(tile + add)); |
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
|
80 |
|
1981 | 81 |
return TileXY(x,y); |
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
|
82 |
} |
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
|
83 |
#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
|
84 |
|
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
|
85 |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
86 |
uint ScaleByMapSize(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
87 |
{ |
2051 | 88 |
// First shift by 12 to prevent integer overflow for large values of n. |
89 |
// >>12 is safe since the min mapsize is 64x64 |
|
90 |
// Add (1<<4)-1 to round upwards. |
|
91 |
return (n * (MapSize() >> 12) + (1<<4) - 1) >> 4; |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
92 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
93 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
94 |
|
2051 | 95 |
// Scale relative to the circumference of the map |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
96 |
uint ScaleByMapSize1D(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
97 |
{ |
2051 | 98 |
// Normal circumference for the X+Y is 256+256 = 1<<9 |
99 |
// Note, not actually taking the full circumference into account, |
|
100 |
// just half of it. |
|
101 |
// (1<<9) - 1 is there to scale upwards. |
|
102 |
return (n * (MapSizeX() + MapSizeY()) + (1<<9) - 1) >> 9; |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
103 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
104 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
105 |
|
1247 | 106 |
// This function checks if we add addx/addy to tile, if we |
107 |
// do wrap around the edges. For example, tile = (10,2) and |
|
108 |
// addx = +3 and addy = -4. This function will now return |
|
109 |
// INVALID_TILE, because the y is wrapped. This is needed in |
|
110 |
// for example, farmland. When the tile is not wrapped, |
|
1981 | 111 |
// the result will be tile + TileDiffXY(addx, addy) |
1247 | 112 |
uint TileAddWrap(TileIndex tile, int addx, int addy) |
113 |
{ |
|
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2482
diff
changeset
|
114 |
uint x = TileX(tile) + addx; |
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2482
diff
changeset
|
115 |
uint y = TileY(tile) + addy; |
1247 | 116 |
|
117 |
// Are we about to wrap? |
|
1981 | 118 |
if (x < MapMaxX() && y < MapMaxY()) |
119 |
return tile + TileDiffXY(addx, addy); |
|
1247 | 120 |
|
121 |
return INVALID_TILE; |
|
122 |
} |
|
123 |
||
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
124 |
const TileIndexDiffC _tileoffs_by_diagdir[] = { |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
125 |
{-1, 0}, // DIAGDIR_NE |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
126 |
{ 0, 1}, // DIAGDIR_SE |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
127 |
{ 1, 0}, // DIAGDIR_SW |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
128 |
{ 0, -1} // DIAGDIR_NW |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
129 |
}; |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
130 |
|
1247 | 131 |
const TileIndexDiffC _tileoffs_by_dir[] = { |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
132 |
{-1, -1}, // DIR_N |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
133 |
{-1, 0}, // DIR_NE |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
134 |
{-1, 1}, // DIR_E |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
135 |
{ 0, 1}, // DIR_SE |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
136 |
{ 1, 1}, // DIR_S |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
137 |
{ 1, 0}, // DIR_SW |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
138 |
{ 1, -1}, // DIR_W |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
139 |
{ 0, -1} // DIR_NW |
1247 | 140 |
}; |
141 |
||
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
142 |
uint DistanceManhattan(TileIndex t0, TileIndex t1) |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
143 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
144 |
const uint dx = abs(TileX(t0) - TileX(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
145 |
const uint dy = abs(TileY(t0) - TileY(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
146 |
return dx + dy; |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
147 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
148 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
149 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
150 |
uint DistanceSquare(TileIndex t0, TileIndex t1) |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
151 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
152 |
const int dx = TileX(t0) - TileX(t1); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
153 |
const int dy = TileY(t0) - TileY(t1); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
154 |
return dx * dx + dy * dy; |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
155 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
156 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
157 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
158 |
uint DistanceMax(TileIndex t0, TileIndex t1) |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
159 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
160 |
const uint dx = abs(TileX(t0) - TileX(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
161 |
const uint dy = abs(TileY(t0) - TileY(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
162 |
return dx > dy ? dx : dy; |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
163 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
164 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
165 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
166 |
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1) |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
167 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
168 |
const uint dx = abs(TileX(t0) - TileX(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
169 |
const uint dy = abs(TileY(t0) - TileY(t1)); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
170 |
return dx > dy ? 2 * dx + dy : 2 * dy + dx; |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
171 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
172 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
173 |
uint DistanceFromEdge(TileIndex tile) |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
174 |
{ |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
175 |
const uint xl = TileX(tile); |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
176 |
const uint yl = TileY(tile); |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
177 |
const uint xh = MapSizeX() - 1 - xl; |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
178 |
const uint yh = MapSizeY() - 1 - yl; |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
179 |
const uint minl = xl < yl ? xl : yl; |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
180 |
const uint minh = xh < yh ? xh : yh; |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
181 |
return minl < minh ? minl : minh; |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
182 |
} |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
183 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
184 |
/** |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
185 |
* Function performing a search around a center tile and going outward, thus in circle. |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
186 |
* Although it really is a square search... |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
187 |
* Every tile will be tested by means of the callback function proc, |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
188 |
* which will determine if yes or no the given tile meets criteria of search. |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
189 |
* @param tile to start the search from |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
190 |
* @param size: number of tiles per side of the desired search area |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
191 |
* @param proc: callback testing function pointer. |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
192 |
* @param data to be passed to the callback function. Depends on the implementation |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
193 |
* @result of the search |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
194 |
* @pre proc != NULL |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
195 |
* @pre size > 0 |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
196 |
*/ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
197 |
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data) |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
198 |
{ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
199 |
uint n, x, y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
200 |
DiagDirection dir; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
201 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
202 |
assert(proc != NULL); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
203 |
assert(size > 0); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
204 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
205 |
x = TileX(tile); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
206 |
y = TileY(tile); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
207 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
208 |
if (size % 2 == 1) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
209 |
/* If the length of the side is uneven, the center has to be checked |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
210 |
* separately, as the pattern of uneven sides requires to go around the center */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
211 |
n = 2; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
212 |
if (proc(TileXY(x, y), data)) return true; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
213 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
214 |
/* If tile test is not successfull, get one tile down and left, |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
215 |
* ready for a test in first circle around center tile */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
216 |
x += _tileoffs_by_dir[DIR_W].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
217 |
y += _tileoffs_by_dir[DIR_W].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
218 |
} else { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
219 |
n = 1; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
220 |
/* To use _tileoffs_by_diagdir's order, we must relocate to |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
221 |
* another tile, as we now first go 'up', 'right', 'down', 'left' |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
222 |
* instead of 'right', 'down', 'left', 'up', which the calling |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
223 |
* function assume. */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
224 |
x++; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
225 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
226 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
227 |
for (; n < size; n += 2) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
228 |
for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
229 |
uint j; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
230 |
for (j = n; j != 0; j--) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
231 |
if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map? |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
232 |
proc(TileXY(x, y), data)) { ///< Is the callback successfulll? |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
233 |
return true; ///< then stop the search |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
234 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
235 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
236 |
/* Step to the next 'neighbour' in the circular line */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
237 |
x += _tileoffs_by_diagdir[dir].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
238 |
y += _tileoffs_by_diagdir[dir].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
239 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
240 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
241 |
/* Jump to next circle to test */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
242 |
x += _tileoffs_by_dir[DIR_W].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
243 |
y += _tileoffs_by_dir[DIR_W].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
244 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
245 |
return false; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
246 |
} |