author | tron |
Sun, 24 Jul 2005 14:12:37 +0000 | |
changeset 2186 | 461a2aff3486 |
parent 2159 | 3b634157c3b2 |
child 2403 | f339737b38bc |
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" |
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
9 |
|
1218 | 10 |
uint _map_log_x; |
2051 | 11 |
uint _map_size_x; |
12 |
uint _map_size_y; |
|
13 |
uint _map_tile_mask; |
|
14 |
uint _map_size; |
|
689 | 15 |
|
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
|
16 |
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
|
17 |
|
1218 | 18 |
|
2051 | 19 |
void AllocateMap(uint size_x, uint size_y) |
1218 | 20 |
{ |
2051 | 21 |
// Make sure that the map size is within the limits and that |
22 |
// the x axis size is a power of 2. |
|
23 |
if (size_x < 64 || size_x > 2048 || |
|
24 |
size_y < 64 || size_y > 2048 || |
|
25 |
(size_x&(size_x-1)) != 0 || |
|
26 |
(size_y&(size_y-1)) != 0) |
|
1244 | 27 |
error("Invalid map size"); |
1218 | 28 |
|
2051 | 29 |
DEBUG(map, 1)("Allocating map of size %dx%d", size_x, size_y); |
1218 | 30 |
|
2051 | 31 |
_map_log_x = FindFirstBit(size_x); |
32 |
_map_size_x = size_x; |
|
33 |
_map_size_y = size_y; |
|
34 |
_map_size = size_x * size_y; |
|
35 |
_map_tile_mask = _map_size - 1; |
|
1218 | 36 |
|
2051 | 37 |
// free/malloc uses less memory than realloc. |
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
|
38 |
free(_m); |
2051 | 39 |
_m = malloc(_map_size * sizeof(*_m)); |
1218 | 40 |
|
41 |
// 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
|
42 |
if (_m == NULL) error("Failed to allocate memory for the map"); |
1218 | 43 |
} |
900 | 44 |
|
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
|
45 |
|
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
|
46 |
#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
|
47 |
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
|
48 |
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
|
49 |
{ |
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 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
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 |
dx = add & MapMaxX(); |
957
d1dd9429e417
(svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents:
955
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
|
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 |
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
|
60 |
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
|
61 |
|
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 |
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
|
63 |
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
|
64 |
|
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 |
sprintf(buf, "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed", |
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 |
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
|
67 |
#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
|
68 |
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
|
69 |
#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
|
70 |
_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
|
71 |
#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
|
72 |
} |
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 |
|
1981 | 74 |
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
|
75 |
|
1981 | 76 |
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
|
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 |
#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
|
79 |
|
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 |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
81 |
uint ScaleByMapSize(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
82 |
{ |
2051 | 83 |
// First shift by 12 to prevent integer overflow for large values of n. |
84 |
// >>12 is safe since the min mapsize is 64x64 |
|
85 |
// Add (1<<4)-1 to round upwards. |
|
86 |
return (n * (MapSize() >> 12) + (1<<4) - 1) >> 4; |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
87 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
88 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
89 |
|
2051 | 90 |
// Scale relative to the circumference of the map |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
91 |
uint ScaleByMapSize1D(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
92 |
{ |
2051 | 93 |
// Normal circumference for the X+Y is 256+256 = 1<<9 |
94 |
// Note, not actually taking the full circumference into account, |
|
95 |
// just half of it. |
|
96 |
// (1<<9) - 1 is there to scale upwards. |
|
97 |
return (n * (MapSizeX() + MapSizeY()) + (1<<9) - 1) >> 9; |
|
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
98 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
99 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
100 |
|
1247 | 101 |
// This function checks if we add addx/addy to tile, if we |
102 |
// do wrap around the edges. For example, tile = (10,2) and |
|
103 |
// addx = +3 and addy = -4. This function will now return |
|
104 |
// INVALID_TILE, because the y is wrapped. This is needed in |
|
105 |
// for example, farmland. When the tile is not wrapped, |
|
1981 | 106 |
// the result will be tile + TileDiffXY(addx, addy) |
1247 | 107 |
uint TileAddWrap(TileIndex tile, int addx, int addy) |
108 |
{ |
|
109 |
uint x, y; |
|
110 |
x = TileX(tile) + addx; |
|
111 |
y = TileY(tile) + addy; |
|
112 |
||
113 |
// Are we about to wrap? |
|
1981 | 114 |
if (x < MapMaxX() && y < MapMaxY()) |
115 |
return tile + TileDiffXY(addx, addy); |
|
1247 | 116 |
|
117 |
return INVALID_TILE; |
|
118 |
} |
|
119 |
||
120 |
const TileIndexDiffC _tileoffs_by_dir[] = { |
|
121 |
{-1, 0}, |
|
122 |
{ 0, 1}, |
|
123 |
{ 1, 0}, |
|
124 |
{ 0, -1} |
|
125 |
}; |
|
126 |
||
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
127 |
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
|
128 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
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
|
132 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
133 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
134 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
135 |
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
|
136 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
141 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
142 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
143 |
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
|
144 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
145 |
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
|
146 |
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
|
147 |
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
|
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 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
151 |
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
|
152 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
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
|
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 |
|
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
158 |
uint DistanceTrack(TileIndex t0, TileIndex t1) |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
159 |
{ |
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 |
|
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
163 |
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */ |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
164 |
/* OPTIMISATION: |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
165 |
* Original: diagTracks = max(dx, dy) - min(dx,dy); |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
166 |
* Proof: |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
167 |
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */ |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
168 |
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */ |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
169 |
|
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
170 |
return diagTracks + straightTracks * STRAIGHT_TRACK_LENGTH; |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
171 |
} |
1245
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 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
183 |