~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/map.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: map.cpp 11961 2008-01-23 17:08:35Z belugas $ */
 
1
/* $Id: map.cpp 15568 2009-02-24 20:59:17Z smatz $ */
2
2
 
3
 
/** @file map.cpp */
 
3
/** @file map.cpp Base functions related to the map and distances on them. */
4
4
 
5
5
#include "stdafx.h"
6
6
#include "debug.h"
7
 
#include "direction_func.h"
8
7
#include "core/bitmath_func.hpp"
9
8
#include "core/alloc_func.hpp"
10
9
#include "core/math_func.hpp"
11
10
#include "map_func.h"
12
11
 
13
 
#if defined(_MSC_VER) && _MSC_VER >= 1400 /* VStudio 2005 is stupid! */
 
12
#if defined(_MSC_VER)
14
13
/* Why the hell is that not in all MSVC headers?? */
15
14
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
16
15
#endif
53
52
        free(_m);
54
53
        free(_me);
55
54
 
56
 
        /* XXX @todo handle memory shortage more gracefully
57
 
         * CallocT does the out-of-memory check
58
 
         * Maybe some attemps could be made to try with smaller maps down to 64x64
59
 
         * Maybe check for available memory before doing the calls, after all, we know how big
60
 
         * the map is */
61
55
        _m = CallocT<Tile>(_map_size);
62
56
        _me = CallocT<TileExtended>(_map_size);
63
57
}
258
252
 * Although it really is a square search...
259
253
 * Every tile will be tested by means of the callback function proc,
260
254
 * which will determine if yes or no the given tile meets criteria of search.
261
 
 * @param tile to start the search from
 
255
 * @param tile to start the search from. Upon completion, it will return the tile matching the search
262
256
 * @param size: number of tiles per side of the desired search area
263
257
 * @param proc: callback testing function pointer.
264
 
 * @param data to be passed to the callback function. Depends on the implementation
 
258
 * @param user_data to be passed to the callback function. Depends on the implementation
265
259
 * @return result of the search
266
260
 * @pre proc != NULL
267
261
 * @pre size > 0
268
262
 */
269
 
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data)
 
263
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
270
264
{
271
 
        uint n, x, y;
272
 
        DiagDirection dir;
273
 
 
274
265
        assert(proc != NULL);
275
266
        assert(size > 0);
276
267
 
277
 
        x = TileX(tile);
278
 
        y = TileY(tile);
279
 
 
280
268
        if (size % 2 == 1) {
281
269
                /* If the length of the side is uneven, the center has to be checked
282
270
                 * separately, as the pattern of uneven sides requires to go around the center */
283
 
                n = 2;
284
 
                if (proc(TileXY(x, y), data)) return true;
 
271
                if (proc(*tile, user_data)) return true;
285
272
 
286
273
                /* If tile test is not successful, get one tile down and left,
287
274
                 * ready for a test in first circle around center tile */
288
 
                x += _tileoffs_by_dir[DIR_W].x;
289
 
                y += _tileoffs_by_dir[DIR_W].y;
 
275
                *tile = TILE_ADD(*tile, TileOffsByDir(DIR_W));
 
276
                return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
290
277
        } else {
291
 
                n = 1;
292
 
                /* To use _tileoffs_by_diagdir's order, we must relocate to
293
 
                 * another tile, as we now first go 'up', 'right', 'down', 'left'
294
 
                 * instead of 'right', 'down', 'left', 'up', which the calling
295
 
                 * function assume. */
296
 
                x++;
 
278
                return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
297
279
        }
298
 
 
299
 
        for (; n < size; n += 2) {
300
 
                for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
301
 
                        uint j;
302
 
                        for (j = n; j != 0; j--) {
 
280
}
 
281
 
 
282
/*!
 
283
 * Generalized circular search allowing for rectangles and a hole.
 
284
 * Function performing a search around a center rectangle and going outward.
 
285
 * The center rectangle is left out from the search. To do a rectangular search
 
286
 * without a hole, set either h or w to zero.
 
287
 * Every tile will be tested by means of the callback function proc,
 
288
 * which will determine if yes or no the given tile meets criteria of search.
 
289
 * @param tile to start the search from. Upon completion, it will return the tile matching the search
 
290
 * @param radius: How many tiles to search outwards. Note: This is a radius and thus different
 
291
 *                from the size parameter of the other CircularTileSearch function, which is a diameter.
 
292
 * @param proc: callback testing function pointer.
 
293
 * @param user_data to be passed to the callback function. Depends on the implementation
 
294
 * @return result of the search
 
295
 * @pre proc != NULL
 
296
 * @pre radius > 0
 
297
 */
 
298
bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
 
299
{
 
300
        assert(proc != NULL);
 
301
        assert(radius > 0);
 
302
 
 
303
        uint x = TileX(*tile) + w + 1;
 
304
        uint y = TileY(*tile);
 
305
 
 
306
        uint extent[DIAGDIR_END] = { w, h, w, h };
 
307
 
 
308
        for (uint n = 0; n < radius; n++) {
 
309
                for (DiagDirection dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
 
310
                        for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
303
311
                                if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map?
304
 
                                                proc(TileXY(x, y), data)) {     ///< Is the callback successful?
 
312
                                                proc(TileXY(x, y), user_data)) {     ///< Is the callback successful?
 
313
                                        *tile = TileXY(x, y);
305
314
                                        return true;                        ///< then stop the search
306
315
                                }
307
316
 
314
323
                x += _tileoffs_by_dir[DIR_W].x;
315
324
                y += _tileoffs_by_dir[DIR_W].y;
316
325
        }
 
326
 
 
327
        *tile = INVALID_TILE;
317
328
        return false;
318
329
}