diff options
author | belugas <belugas@openttd.org> | 2006-11-17 23:01:58 +0000 |
---|---|---|
committer | belugas <belugas@openttd.org> | 2006-11-17 23:01:58 +0000 |
commit | c26dc76a7d40884c5f57014cecff5e4aff3261ba (patch) | |
tree | cb862a03cf34020ff230c133372a4be330d3a917 /industry_cmd.c | |
parent | bd129cf6bf59d62896fec327a3b5677f74bbb606 (diff) | |
download | openttd-c26dc76a7d40884c5f57014cecff5e4aff3261ba.tar.xz |
(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.
Diffstat (limited to 'industry_cmd.c')
-rw-r--r-- | industry_cmd.c | 69 |
1 files changed, 31 insertions, 38 deletions
diff --git a/industry_cmd.c b/industry_cmd.c index 14f8660b6..5d6c78d67 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -845,51 +845,44 @@ static void MaybePlantFarmField(const Industry *i) if (CHANCE16(1, 8)) PlantRandomFarmField(i); } -static void ChopLumberMillTrees(Industry *i) +/** + * Search callback function for ChopLumberMillTrees + * @param tile to test + * @param data that is passed by the caller. In this case, nothing + * @result of the test + */ +static bool SearchLumberMillTrees(TileIndex tile, uint32 data) { - static const TileIndexDiffC _chop_dir[] = { - { 0, 1}, - { 1, 0}, - { 0, -1}, - {-1, 0} - }; - - TileIndex tile = i->xy; - int a; - - if (!IsIndustryCompleted(tile)) return; - - /* search outwards as a rectangular spiral */ - for (a = 1; a != 41; a += 2) { - uint dir; + if (IsTileType(tile, MP_TREES)) { + PlayerID old_player = _current_player; + /* found a tree */ - for (dir = 0; dir != 4; dir++) { - int j = a; + _current_player = OWNER_NONE; + _industry_sound_ctr = 1; + _industry_sound_tile = tile; + SndPlayTileFx(SND_38_CHAINSAW, tile); - do { - tile = TILE_MASK(tile); - if (IsTileType(tile, MP_TREES)) { - PlayerID old_player = _current_player; - /* found a tree */ + DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); + SetTropicZone(tile, TROPICZONE_INVALID); - _current_player = OWNER_NONE; - _industry_sound_ctr = 1; - _industry_sound_tile = tile; - SndPlayTileFx(SND_38_CHAINSAW, tile); + _current_player = old_player; + return true; + } + return false; +} - DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); - SetTropicZone(tile, TROPICZONE_INVALID); +/** + * Perform a circular search around the Lumber Mill in order to find trees to cut + * @param i industry + */ +static void ChopLumberMillTrees(Industry *i) +{ + TileIndex tile = i->xy; - i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + 45); + if (!IsIndustryCompleted(tile)) return; ///< Can't proceed if not completed - _current_player = old_player; - return; - } - tile += ToTileIndexDiff(_chop_dir[dir]); - } while (--j); - } - tile -= TileDiffXY(1, 1); - } + if (CircularTileSearch(tile, 40, SearchLumberMillTrees, 0)) ///< 40x40 tiles to search + i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + 45); ///< Found a tree, add according value to waiting cargo } static const byte _industry_sounds[37][2] = { |