1
0
Fork 0

Codechange: Use separate function for planting an individual tree. (#13296)

This allows early return and removes use of assignment inside if-conditions.
pull/13297/head
Peter Nelson 2025-01-08 20:36:38 +00:00 committed by GitHub
parent f404f3154e
commit 4db0af99a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 14 deletions

View File

@ -829,15 +829,29 @@ bool DecrementTreeCounter()
return old_trees_tick_ctr <= _trees_tick_ctr;
}
/**
* Place a random tree on a random tile.
* @param rainforest If set the random tile must be in a rainforest zone.
*/
static void PlantRandomTree(bool rainforest)
{
uint32_t r = Random();
TileIndex tile = RandomTileSeed(r);
if (rainforest && GetTropicZone(tile) != TROPICZONE_RAINFOREST) return;
if (!CanPlantTreesOnTile(tile, false)) return;
TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
if (tree == TREE_INVALID) return;
PlantTreesOnTile(tile, tree, 0, TreeGrowthStage::Growing1);
}
void OnTick_Trees()
{
/* Don't spread trees if that's not allowed */
if (_settings_game.construction.extra_tree_placement == ETP_NO_SPREAD || _settings_game.construction.extra_tree_placement == ETP_NO_GROWTH_NO_SPREAD) return;
uint32_t r;
TileIndex tile;
TreeType tree;
/* Skip some tree ticks for map sizes below 256 * 256. 64 * 64 is 16 times smaller, so
* this is the maximum number of ticks that are skipped. Number of ticks to skip is
* inversely proportional to map size, so that is handled to create a mask. */
@ -847,22 +861,14 @@ void OnTick_Trees()
/* place a tree at a random rainforest spot */
if (_settings_game.game_creation.landscape == LT_TROPIC) {
for (uint c = Map::ScaleBySize(1); c > 0; c--) {
if ((r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
CanPlantTreesOnTile(tile, false) &&
(tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
PlantTreesOnTile(tile, tree, 0, TreeGrowthStage::Growing1);
}
PlantRandomTree(true);
}
}
if (!DecrementTreeCounter() || _settings_game.construction.extra_tree_placement == ETP_SPREAD_RAINFOREST) return;
/* place a tree at a random spot */
r = Random();
tile = RandomTileSeed(r);
if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
PlantTreesOnTile(tile, tree, 0, TreeGrowthStage::Growing1);
}
PlantRandomTree(false);
}
static TrackStatus GetTileTrackStatus_Trees(TileIndex, TransportType, uint, DiagDirection)