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
269
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data)
263
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
274
265
assert(proc != NULL);
275
266
assert(size > 0);
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 */
284
if (proc(TileXY(x, y), data)) return true;
271
if (proc(*tile, user_data)) return true;
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);
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. */
278
return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
299
for (; n < size; n += 2) {
300
for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
302
for (j = n; j != 0; j--) {
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
298
bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
300
assert(proc != NULL);
303
uint x = TileX(*tile) + w + 1;
304
uint y = TileY(*tile);
306
uint extent[DIAGDIR_END] = { w, h, w, h };
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