From c26dc76a7d40884c5f57014cecff5e4aff3261ba Mon Sep 17 00:00:00 2001 From: belugas Date: Fri, 17 Nov 2006 23:01:58 +0000 Subject: (svn r7198) -Codechange: Implement a circular tile search function. Just provide the number of tiles per side, a pointer to a test function, the tile to start searching and voila. Fixes [FS#364] by removing a lengthy and suboptimal random search pattern. Thanks Rubidium. --- town_cmd.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'town_cmd.c') diff --git a/town_cmd.c b/town_cmd.c index d9bc799ae..20be3354a 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -1476,39 +1476,27 @@ static bool DoBuildStatueOfCompany(TileIndex tile) return true; } +/** + * Search callback function for TownActionBuildStatue + * @param data that is passed by the caller. In this case, nothing + * @result of the test + */ +static bool SearchTileForStatue(TileIndex tile, uint32 data) +{ + return DoBuildStatueOfCompany(tile); +} + +/** + * Perform a 9x9 tiles circular search from the center of the town + * in order to find a free tile to place a statue + * @param t town to search in + */ static void TownActionBuildStatue(Town* t) { - // Layouted as an outward spiral - static const TileIndexDiffC _statue_tiles[] = { - {-1, 0}, - { 0, 1}, - { 1, 0}, { 1, 0}, - { 0,-1}, { 0,-1}, - {-1, 0}, {-1, 0}, {-1, 0}, - { 0, 1}, { 0, 1}, { 0, 1}, - { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, - { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, - {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, - { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, - { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, - { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, - {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, - { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, - { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, - { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, - {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, - { 0, 0} - }; TileIndex tile = t->xy; - const TileIndexDiffC *p; - for (p = _statue_tiles; p != endof(_statue_tiles); ++p) { - if (DoBuildStatueOfCompany(tile)) { - SETBIT(t->statues, _current_player); - return; - } - tile = TILE_ADD(tile, ToTileIndexDiff(*p)); - } + if (CircularTileSearch(tile, 9, SearchTileForStatue, 0)) + SETBIT(t->statues, _current_player); ///< Once found and built, "inform" the Town } static void TownActionFundBuildings(Town* t) -- cgit v1.2.3-70-g09d2