Fix f5183807: Tree planting scaling on extra large maps

This commit is contained in:
Jonathan G Rennison
2021-05-21 18:40:10 +01:00
parent f5183807fe
commit 30409192f0

View File

@@ -930,17 +930,17 @@ static void TileLoop_Trees(TileIndex tile)
* Decrement the tree tick counter. * Decrement the tree tick counter.
* The interval is scaled by map size to allow for the same density regardless of size. * The interval is scaled by map size to allow for the same density regardless of size.
* Adjustment for map sizes below the standard 256 * 256 are handled earlier. * Adjustment for map sizes below the standard 256 * 256 are handled earlier.
* @return true if the counter was decremented past zero * @return number of trees to try to plant
*/ */
bool DecrementTreeCounter() uint DecrementTreeCounter()
{ {
/* Ensure _trees_tick_ctr can be decremented past zero only once for the largest map size. */ uint scaled_map_size = ScaleByMapSize(1);
static_assert(2 * (MAX_MAP_SIZE_BITS - MIN_MAP_SIZE_BITS) - 4 <= std::numeric_limits<byte>::digits); if (scaled_map_size >= 256) return scaled_map_size >> 8;
/* byte underflow */ /* byte underflow */
byte old_trees_tick_ctr = _trees_tick_ctr; byte old_trees_tick_ctr = _trees_tick_ctr;
_trees_tick_ctr -= ScaleByMapSize(1); _trees_tick_ctr -= scaled_map_size;
return old_trees_tick_ctr <= _trees_tick_ctr; return old_trees_tick_ctr <= _trees_tick_ctr ? 1 : 0;
} }
void OnTick_Trees() void OnTick_Trees()
@@ -969,13 +969,15 @@ void OnTick_Trees()
} }
} }
if (!DecrementTreeCounter() || _settings_game.construction.extra_tree_placement == ETP_SPREAD_RAINFOREST) return; if (_settings_game.construction.extra_tree_placement == ETP_SPREAD_RAINFOREST) return;
/* place a tree at a random spot */ for (uint ctr = DecrementTreeCounter(); ctr > 0; ctr--) {
r = Random(); /* place a tree at a random spot */
tile = RandomTileSeed(r); r = Random();
if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) { tile = RandomTileSeed(r);
PlantTreesOnTile(tile, tree, 0, 0); if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
PlantTreesOnTile(tile, tree, 0, 0);
}
} }
} }