author | KUDr |
Fri, 20 Apr 2007 19:43:06 +0000 | |
changeset 6513 | 454347ca3dfb |
parent 6492 | 286a52449b54 |
child 6540 | d30795308feb |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
3 |
/** @file map.cpp */ |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
4 |
|
679
04ca2cd69420
(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
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1677
diff
changeset
|
6 |
#include "openttd.h" |
1299
39c06aba09aa
(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
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2051
diff
changeset
|
9 |
#include "macros.h" |
679
04ca2cd69420
(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
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
11 |
#include "direction.h" |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
12 |
#include "helpers.hpp" |
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff
changeset
|
13 |
|
2482
374f6395847d
(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! */ |
374f6395847d
(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?? */ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
16 |
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned); |
2482
374f6395847d
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
17 |
#endif |
374f6395847d
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2403
diff
changeset
|
18 |
|
1218 | 19 |
uint _map_log_x; |
2051 | 20 |
uint _map_size_x; |
21 |
uint _map_size_y; |
|
22 |
uint _map_tile_mask; |
|
23 |
uint _map_size; |
|
689 | 24 |
|
6332
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
25 |
Tile *_m = NULL; |
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
26 |
TileExtended *_me = NULL; |
863
6a1444534f62
(svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents:
817
diff
changeset
|
27 |
|
1218 | 28 |
|
2051 | 29 |
void AllocateMap(uint size_x, uint size_y) |
1218 | 30 |
{ |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
31 |
/* Make sure that the map size is within the limits and that |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
32 |
* the x axis size is a power of 2. */ |
2051 | 33 |
if (size_x < 64 || size_x > 2048 || |
34 |
size_y < 64 || size_y > 2048 || |
|
35 |
(size_x&(size_x-1)) != 0 || |
|
36 |
(size_y&(size_y-1)) != 0) |
|
1244 | 37 |
error("Invalid map size"); |
1218 | 38 |
|
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5170
diff
changeset
|
39 |
DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); |
1218 | 40 |
|
2051 | 41 |
_map_log_x = FindFirstBit(size_x); |
42 |
_map_size_x = size_x; |
|
43 |
_map_size_y = size_y; |
|
44 |
_map_size = size_x * size_y; |
|
45 |
_map_tile_mask = _map_size - 1; |
|
1218 | 46 |
|
2049
538e73c53f54
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2043
diff
changeset
|
47 |
free(_m); |
6332
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
48 |
free(_me); |
1218 | 49 |
|
6332
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
50 |
_m = CallocT<Tile>(_map_size); |
6492
286a52449b54
(svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents:
6491
diff
changeset
|
51 |
_me = CallocT<TileExtended>(_map_size); |
6332
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
52 |
|
6492
286a52449b54
(svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents:
6491
diff
changeset
|
53 |
/* XXX @todo handle memory shortage more gracefully |
286a52449b54
(svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents:
6491
diff
changeset
|
54 |
* Maybe some attemps could be made to try with smaller maps down to 64x64 |
286a52449b54
(svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents:
6491
diff
changeset
|
55 |
* Maybe check for available memory before doing the calls, after all, we know how big |
286a52449b54
(svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents:
6491
diff
changeset
|
56 |
* the map is */ |
6332
f3f436dcd7d0
(svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
6201
diff
changeset
|
57 |
if ((_m == NULL) || (_me == NULL)) error("Failed to allocate memory for the map"); |
1218 | 58 |
} |
900 | 59 |
|
955
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
60 |
|
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
61 |
#ifdef _DEBUG |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
62 |
TileIndex TileAdd(TileIndex tile, TileIndexDiff add, |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
63 |
const char *exp, const char *file, int line) |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
64 |
{ |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
65 |
int dx; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
66 |
int dy; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
67 |
uint x; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
68 |
uint y; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
69 |
|
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
70 |
dx = add & MapMaxX(); |
957
a1cb5aac6a6f
(svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents:
955
diff
changeset
|
71 |
if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX(); |
955
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
72 |
dy = (add - dx) / (int)MapSizeX(); |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
73 |
|
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
74 |
x = TileX(tile) + dx; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
75 |
y = TileY(tile) + dy; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
76 |
|
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
77 |
if (x >= MapSizeX() || y >= MapSizeY()) { |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
78 |
char buf[512]; |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
79 |
|
5170
817550aa5a5f
(svn r7280) -Codechange: Replace some sprintf() functions with the safer snprintf() functions
Darkvater
parents:
5118
diff
changeset
|
80 |
snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed", |
955
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
81 |
exp, tile, add); |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
82 |
#if !defined(_MSC_VER) |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
83 |
fprintf(stderr, "%s:%d %s\n", file, line, buf); |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
84 |
#else |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
85 |
_assert(buf, (char*)file, line); |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
86 |
#endif |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
87 |
} |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
88 |
|
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6332
diff
changeset
|
89 |
assert(TileXY(x, y) == TILE_MASK(tile + add)); |
955
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
90 |
|
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6332
diff
changeset
|
91 |
return TileXY(x, y); |
955
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
92 |
} |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
93 |
#endif |
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
94 |
|
62b8588f50c8
(svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents:
927
diff
changeset
|
95 |
|
1202
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
96 |
uint ScaleByMapSize(uint n) |
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
97 |
{ |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
98 |
/* First shift by 12 to prevent integer overflow for large values of n. |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
99 |
* >>12 is safe since the min mapsize is 64x64 |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
100 |
* Add (1<<4)-1 to round upwards. */ |
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6332
diff
changeset
|
101 |
return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; |
1202
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
102 |
} |
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
103 |
|
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
104 |
|
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
105 |
/* Scale relative to the circumference of the map */ |
1202
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
106 |
uint ScaleByMapSize1D(uint n) |
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
107 |
{ |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
108 |
/* Normal circumference for the X+Y is 256+256 = 1<<9 |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
109 |
* Note, not actually taking the full circumference into account, |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
110 |
* just half of it. |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
111 |
* (1<<9) - 1 is there to scale upwards. */ |
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6332
diff
changeset
|
112 |
return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; |
1202
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
113 |
} |
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
114 |
|
4d2a20c50760
(svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents:
957
diff
changeset
|
115 |
|
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
116 |
/* This function checks if we add addx/addy to tile, if we |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
117 |
* do wrap around the edges. For example, tile = (10,2) and |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
118 |
* addx = +3 and addy = -4. This function will now return |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
119 |
* INVALID_TILE, because the y is wrapped. This is needed in |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
120 |
* for example, farmland. When the tile is not wrapped, |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
121 |
* the result will be tile + TileDiffXY(addx, addy) */ |
1247 | 122 |
uint TileAddWrap(TileIndex tile, int addx, int addy) |
123 |
{ |
|
2548
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2482
diff
changeset
|
124 |
uint x = TileX(tile) + addx; |
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2482
diff
changeset
|
125 |
uint y = TileY(tile) + addy; |
1247 | 126 |
|
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
127 |
/* Are we about to wrap? */ |
1981 | 128 |
if (x < MapMaxX() && y < MapMaxY()) |
129 |
return tile + TileDiffXY(addx, addy); |
|
1247 | 130 |
|
131 |
return INVALID_TILE; |
|
132 |
} |
|
133 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
134 |
extern const TileIndexDiffC _tileoffs_by_diagdir[] = { |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
135 |
{-1, 0}, ///< DIAGDIR_NE |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
136 |
{ 0, 1}, ///< DIAGDIR_SE |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
137 |
{ 1, 0}, ///< DIAGDIR_SW |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
138 |
{ 0, -1} ///< DIAGDIR_NW |
4559
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
139 |
}; |
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
3717
diff
changeset
|
140 |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
141 |
extern const TileIndexDiffC _tileoffs_by_dir[] = { |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
142 |
{-1, -1}, ///< DIR_N |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
143 |
{-1, 0}, ///< DIR_NE |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
144 |
{-1, 1}, ///< DIR_E |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
145 |
{ 0, 1}, ///< DIR_SE |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
146 |
{ 1, 1}, ///< DIR_S |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
147 |
{ 1, 0}, ///< DIR_SW |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
148 |
{ 1, -1}, ///< DIR_W |
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
149 |
{ 0, -1} ///< DIR_NW |
1247 | 150 |
}; |
151 |
||
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
152 |
uint DistanceManhattan(TileIndex t0, TileIndex t1) |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
153 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
154 |
const uint dx = delta(TileX(t0), TileX(t1)); |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
155 |
const uint dy = delta(TileY(t0), TileY(t1)); |
1677
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
156 |
return dx + dy; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
157 |
} |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
158 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
159 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
160 |
uint DistanceSquare(TileIndex t0, TileIndex t1) |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
161 |
{ |
1677
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
162 |
const int dx = TileX(t0) - TileX(t1); |
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
163 |
const int dy = TileY(t0) - TileY(t1); |
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
164 |
return dx * dx + dy * dy; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
165 |
} |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
166 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
167 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
168 |
uint DistanceMax(TileIndex t0, TileIndex t1) |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
169 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
170 |
const uint dx = delta(TileX(t0), TileX(t1)); |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
171 |
const uint dy = delta(TileY(t0), TileY(t1)); |
1677
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
172 |
return dx > dy ? dx : dy; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
173 |
} |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
174 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
175 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
176 |
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1) |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
177 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
178 |
const uint dx = delta(TileX(t0), TileX(t1)); |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
179 |
const uint dy = delta(TileY(t0), TileY(t1)); |
1677
d534f0c8c845
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1410
diff
changeset
|
180 |
return dx > dy ? 2 * dx + dy : 2 * dy + dx; |
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
181 |
} |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
182 |
|
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
183 |
uint DistanceFromEdge(TileIndex tile) |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
184 |
{ |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
185 |
const uint xl = TileX(tile); |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
186 |
const uint yl = TileY(tile); |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
187 |
const uint xh = MapSizeX() - 1 - xl; |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
188 |
const uint yh = MapSizeY() - 1 - yl; |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
189 |
const uint minl = xl < yl ? xl : yl; |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
190 |
const uint minh = xh < yh ? xh : yh; |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
191 |
return minl < minh ? minl : minh; |
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1244
diff
changeset
|
192 |
} |
5118
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
193 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
194 |
/** |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
195 |
* Function performing a search around a center tile and going outward, thus in circle. |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
196 |
* Although it really is a square search... |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
197 |
* Every tile will be tested by means of the callback function proc, |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
198 |
* which will determine if yes or no the given tile meets criteria of search. |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
199 |
* @param tile to start the search from |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
200 |
* @param size: number of tiles per side of the desired search area |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
201 |
* @param proc: callback testing function pointer. |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
202 |
* @param data to be passed to the callback function. Depends on the implementation |
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5609
diff
changeset
|
203 |
* @return result of the search |
5118
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
204 |
* @pre proc != NULL |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
205 |
* @pre size > 0 |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
206 |
*/ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
207 |
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data) |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
208 |
{ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
209 |
uint n, x, y; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
210 |
DiagDirection dir; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
211 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
212 |
assert(proc != NULL); |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
213 |
assert(size > 0); |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
214 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
215 |
x = TileX(tile); |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
216 |
y = TileY(tile); |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
217 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
218 |
if (size % 2 == 1) { |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
219 |
/* If the length of the side is uneven, the center has to be checked |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
220 |
* separately, as the pattern of uneven sides requires to go around the center */ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
221 |
n = 2; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
222 |
if (proc(TileXY(x, y), data)) return true; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
223 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
224 |
/* If tile test is not successfull, get one tile down and left, |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
225 |
* ready for a test in first circle around center tile */ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
226 |
x += _tileoffs_by_dir[DIR_W].x; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
227 |
y += _tileoffs_by_dir[DIR_W].y; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
228 |
} else { |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
229 |
n = 1; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
230 |
/* To use _tileoffs_by_diagdir's order, we must relocate to |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
231 |
* another tile, as we now first go 'up', 'right', 'down', 'left' |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
232 |
* instead of 'right', 'down', 'left', 'up', which the calling |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
233 |
* function assume. */ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
234 |
x++; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
235 |
} |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
236 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
237 |
for (; n < size; n += 2) { |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
238 |
for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) { |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
239 |
uint j; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
240 |
for (j = n; j != 0; j--) { |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
241 |
if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map? |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
242 |
proc(TileXY(x, y), data)) { ///< Is the callback successfulll? |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
243 |
return true; ///< then stop the search |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
244 |
} |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
245 |
|
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
246 |
/* Step to the next 'neighbour' in the circular line */ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
247 |
x += _tileoffs_by_diagdir[dir].x; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
248 |
y += _tileoffs_by_diagdir[dir].y; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
249 |
} |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
250 |
} |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
251 |
/* Jump to next circle to test */ |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
252 |
x += _tileoffs_by_dir[DIR_W].x; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
253 |
y += _tileoffs_by_dir[DIR_W].y; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
254 |
} |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
255 |
return false; |
9640617e1abb
(svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents:
4559
diff
changeset
|
256 |
} |