author | rubidium |
Mon, 03 Dec 2007 23:39:38 +0000 | |
branch | NewGRF_ports |
changeset 6871 | 5a9dc001e1ad |
parent 6870 | ca3fd1fbe311 |
child 6872 | 1c4a4a609f85 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
3 |
/** @file map.cpp */ |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
4 |
|
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1677
diff
changeset
|
6 |
#include "openttd.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1247
diff
changeset
|
7 |
#include "debug.h" |
1218 | 8 |
#include "functions.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2051
diff
changeset
|
9 |
#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
|
10 |
#include "map.h" |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
11 |
#include "direction.h" |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
12 |
#include "helpers.hpp" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
13 |
|
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
14 |
#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
|
15 |
/* Why the hell is that not in all MSVC headers?? */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
16 |
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned); |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
17 |
#endif |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
18 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
19 |
uint _map_log_x; ///< 2^_map_log_x == _map_size_x |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
20 |
uint _map_size_x; ///< Size of the map along the X |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
21 |
uint _map_size_y; ///< Size of the map along the Y |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
22 |
uint _map_size; ///< The number of tiles on the map |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
23 |
uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize) |
689 | 24 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
25 |
Tile *_m = NULL; ///< Tiles of the map |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
26 |
TileExtended *_me = NULL; ///< Extended Tiles of the map |
863
8d09f9331a80
(svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents:
817
diff
changeset
|
27 |
|
1218 | 28 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
29 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
30 |
* (Re)allocates a map with the given dimension |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
31 |
* @param size_x the width of the map along the NE/SW edge |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
32 |
* @param size_y the 'height' of the map along the SE/NW edge |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
33 |
*/ |
2051 | 34 |
void AllocateMap(uint size_x, uint size_y) |
1218 | 35 |
{ |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
36 |
/* Make sure that the map size is within the limits and that |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
37 |
* the x axis size is a power of 2. */ |
2051 | 38 |
if (size_x < 64 || size_x > 2048 || |
39 |
size_y < 64 || size_y > 2048 || |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
40 |
(size_x & (size_x - 1)) != 0 || |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
41 |
(size_y & (size_y - 1)) != 0) |
1244 | 42 |
error("Invalid map size"); |
1218 | 43 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5170
diff
changeset
|
44 |
DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); |
1218 | 45 |
|
2051 | 46 |
_map_log_x = FindFirstBit(size_x); |
47 |
_map_size_x = size_x; |
|
48 |
_map_size_y = size_y; |
|
49 |
_map_size = size_x * size_y; |
|
50 |
_map_tile_mask = _map_size - 1; |
|
1218 | 51 |
|
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
|
52 |
free(_m); |
6658
59048224be55
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6527
diff
changeset
|
53 |
free(_me); |
1218 | 54 |
|
6658
59048224be55
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6527
diff
changeset
|
55 |
_m = CallocT<Tile>(_map_size); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
56 |
_me = CallocT<TileExtended>(_map_size); |
6658
59048224be55
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6527
diff
changeset
|
57 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
58 |
/* XXX @todo handle memory shortage more gracefully |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
59 |
* Maybe some attemps could be made to try with smaller maps down to 64x64 |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
60 |
* Maybe check for available memory before doing the calls, after all, we know how big |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
61 |
* the map is */ |
6658
59048224be55
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6527
diff
changeset
|
62 |
if ((_m == NULL) || (_me == NULL)) error("Failed to allocate memory for the map"); |
1218 | 63 |
} |
900 | 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 |
#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
|
67 |
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
|
68 |
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
|
69 |
{ |
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 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
|
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 |
dx = add & MapMaxX(); |
957
d1dd9429e417
(svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents:
955
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
|
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 |
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
|
80 |
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
|
81 |
|
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 |
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
|
83 |
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
|
84 |
|
5170
56c12a62af49
(svn r7280) -Codechange: Replace some sprintf() functions with the safer snprintf() functions
Darkvater
parents:
5118
diff
changeset
|
85 |
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
|
86 |
exp, tile, add); |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
87 |
#if !defined(_MSC_VER) || defined(WINCE) |
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
|
88 |
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
|
89 |
#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
|
90 |
_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
|
91 |
#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
|
92 |
} |
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
|
93 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
94 |
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
|
95 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
96 |
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
|
97 |
} |
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
|
98 |
#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
|
99 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
100 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
101 |
* Scales the given value by the map size, where the given value is |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
102 |
* for a 256 by 256 map. |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
103 |
* @param n the value to scale |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
104 |
* @return the scaled size |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
105 |
*/ |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
106 |
uint ScaleByMapSize(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
107 |
{ |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
108 |
/* First shift by 12 to prevent integer overflow for large values of n. |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
109 |
* >>12 is safe since the min mapsize is 64x64 |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
110 |
* Add (1<<4)-1 to round upwards. */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
111 |
return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
112 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
113 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
114 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
115 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
116 |
* Scales the given value by the maps circumference, where the given |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
117 |
* value is for a 256 by 256 map |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
118 |
* @param n the value to scale |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
119 |
* @return the scaled size |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
120 |
*/ |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
121 |
uint ScaleByMapSize1D(uint n) |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
122 |
{ |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
123 |
/* Normal circumference for the X+Y is 256+256 = 1<<9 |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
124 |
* Note, not actually taking the full circumference into account, |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
125 |
* just half of it. |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
126 |
* (1<<9) - 1 is there to scale upwards. */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
127 |
return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; |
1202
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
128 |
} |
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
129 |
|
7d8b86bd8ba2
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
130 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
131 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
132 |
* This function checks if we add addx/addy to tile, if we |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
133 |
* do wrap around the edges. For example, tile = (10,2) and |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
134 |
* addx = +3 and addy = -4. This function will now return |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
135 |
* INVALID_TILE, because the y is wrapped. This is needed in |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
136 |
* for example, farmland. When the tile is not wrapped, |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
137 |
* the result will be tile + TileDiffXY(addx, addy) |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
138 |
* |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
139 |
* @param tile the 'starting' point of the adding |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
140 |
* @param addx the amount of tiles in the X direction to add |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
141 |
* @param addy the amount of tiles in the Y direction to add |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
142 |
* @return translated tile, or INVALID_TILE when it would've wrapped. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
143 |
*/ |
1247 | 144 |
uint TileAddWrap(TileIndex tile, int addx, int addy) |
145 |
{ |
|
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
|
146 |
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
|
147 |
uint y = TileY(tile) + addy; |
1247 | 148 |
|
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
149 |
/* Are we about to wrap? */ |
1981 | 150 |
if (x < MapMaxX() && y < MapMaxY()) |
151 |
return tile + TileDiffXY(addx, addy); |
|
1247 | 152 |
|
153 |
return INVALID_TILE; |
|
154 |
} |
|
155 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
156 |
/** 'Lookup table' for tile offsets given a DiagDirection */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
157 |
extern const TileIndexDiffC _tileoffs_by_diagdir[] = { |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
158 |
{-1, 0}, ///< DIAGDIR_NE |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
159 |
{ 0, 1}, ///< DIAGDIR_SE |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
160 |
{ 1, 0}, ///< DIAGDIR_SW |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
161 |
{ 0, -1} ///< DIAGDIR_NW |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
162 |
}; |
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
163 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
164 |
/** 'Lookup table' for tile offsets given a Direction */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
165 |
extern const TileIndexDiffC _tileoffs_by_dir[] = { |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
166 |
{-1, -1}, ///< DIR_N |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
167 |
{-1, 0}, ///< DIR_NE |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
168 |
{-1, 1}, ///< DIR_E |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
169 |
{ 0, 1}, ///< DIR_SE |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
170 |
{ 1, 1}, ///< DIR_S |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
171 |
{ 1, 0}, ///< DIR_SW |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
172 |
{ 1, -1}, ///< DIR_W |
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
173 |
{ 0, -1} ///< DIR_NW |
1247 | 174 |
}; |
175 |
||
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
176 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
177 |
* Gets the Manhattan distance between the two given tiles. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
178 |
* The Manhattan distance is the sum of the delta of both the |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
179 |
* X and Y component. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
180 |
* Also known as L1-Norm |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
181 |
* @param t0 the start tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
182 |
* @param t1 the end tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
183 |
* @return the distance |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
184 |
*/ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
185 |
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
|
186 |
{ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
187 |
const uint dx = Delta(TileX(t0), TileX(t1)); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
188 |
const uint dy = Delta(TileY(t0), TileY(t1)); |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
189 |
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
|
190 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
191 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
192 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
193 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
194 |
* Gets the 'Square' distance between the two given tiles. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
195 |
* The 'Square' distance is the square of the shortest (straight line) |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
196 |
* distance between the two tiles. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
197 |
* Also known as euclidian- or L2-Norm squared. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
198 |
* @param t0 the start tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
199 |
* @param t1 the end tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
200 |
* @return the distance |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
201 |
*/ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
202 |
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
|
203 |
{ |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
208 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
209 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
210 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
211 |
* Gets the biggest distance component (x or y) between the two given tiles. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
212 |
* Also known as L-Infinity-Norm. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
213 |
* @param t0 the start tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
214 |
* @param t1 the end tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
215 |
* @return the distance |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
216 |
*/ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
217 |
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
|
218 |
{ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
219 |
const uint dx = Delta(TileX(t0), TileX(t1)); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
220 |
const uint dy = Delta(TileY(t0), TileY(t1)); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
221 |
return max(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
|
222 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
223 |
|
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
224 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
225 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
226 |
* Gets the biggest distance component (x or y) between the two given tiles |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
227 |
* plus the Manhattan distance, i.e. two times the biggest distance component |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
228 |
* and once the smallest component. |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
229 |
* @param t0 the start tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
230 |
* @param t1 the end tile |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
231 |
* @return the distance |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
232 |
*/ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
233 |
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
|
234 |
{ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
235 |
const uint dx = Delta(TileX(t0), TileX(t1)); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
236 |
const uint dy = Delta(TileY(t0), TileY(t1)); |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
237 |
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
|
238 |
} |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
239 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
240 |
/*! |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
241 |
* Param the minimum distance to an edge |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
242 |
* @param tile the tile to get the distance from |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
243 |
* @return the distance from the edge in tiles |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6658
diff
changeset
|
244 |
*/ |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
245 |
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
|
246 |
{ |
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
const uint yh = MapSizeY() - 1 - yl; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
251 |
const uint minl = min(xl, yl); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
252 |
const uint minh = min(xh, yh); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
253 |
return min(minl, minh); |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
254 |
} |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
255 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6800
diff
changeset
|
256 |
/*! |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
257 |
* 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
|
258 |
* Although it really is a square search... |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
259 |
* 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
|
260 |
* 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
|
261 |
* @param tile to start the search from |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
262 |
* @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
|
263 |
* @param proc: callback testing function pointer. |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
264 |
* @param data to be passed to the callback function. Depends on the implementation |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5860
diff
changeset
|
265 |
* @return result of the search |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
266 |
* @pre proc != NULL |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
267 |
* @pre size > 0 |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
268 |
*/ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
269 |
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
|
270 |
{ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
271 |
uint n, x, y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
272 |
DiagDirection dir; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
273 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
274 |
assert(proc != NULL); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
275 |
assert(size > 0); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
276 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
277 |
x = TileX(tile); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
278 |
y = TileY(tile); |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
279 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
280 |
if (size % 2 == 1) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
281 |
/* 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
|
282 |
* 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
|
283 |
n = 2; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
284 |
if (proc(TileXY(x, y), data)) return true; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
285 |
|
6743
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6719
diff
changeset
|
286 |
/* If tile test is not successful, get one tile down and left, |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
287 |
* 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
|
288 |
x += _tileoffs_by_dir[DIR_W].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
289 |
y += _tileoffs_by_dir[DIR_W].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
290 |
} else { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
291 |
n = 1; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
292 |
/* 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
|
293 |
* 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
|
294 |
* instead of 'right', 'down', 'left', 'up', which the calling |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
295 |
* function assume. */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
296 |
x++; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
297 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
298 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
299 |
for (; n < size; n += 2) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
300 |
for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
301 |
uint j; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
302 |
for (j = n; j != 0; j--) { |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
303 |
if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map? |
6743
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6719
diff
changeset
|
304 |
proc(TileXY(x, y), data)) { ///< Is the callback successful? |
5118
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
305 |
return true; ///< then stop the search |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
306 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
307 |
|
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
308 |
/* Step to the next 'neighbour' in the circular line */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
309 |
x += _tileoffs_by_diagdir[dir].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
310 |
y += _tileoffs_by_diagdir[dir].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
311 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
312 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
313 |
/* Jump to next circle to test */ |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
314 |
x += _tileoffs_by_dir[DIR_W].x; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
315 |
y += _tileoffs_by_dir[DIR_W].y; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
316 |
} |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
317 |
return false; |
5a56a0f12206
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
318 |
} |