1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-19 20:49:11 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Su
89182e4b15 Codefix: early continue, avoid sideeffects
Co-authored-by: Peter Nelson <peter@fuzzle.org>
2024-06-04 14:26:06 +01:00
Susan
a75532dd47 Codechange: exit early if past forest threshold 2024-06-04 13:11:30 +01:00
Susan
6d40780434 Codechange: use TileArea to search for trees 2024-06-04 11:30:14 +01:00
Susan
6335312b3c Codechange: don't remove trees only to immediately replant them 2024-06-04 10:59:57 +01:00

View File

@@ -58,6 +58,7 @@ static const uint16_t DEFAULT_TREE_STEPS = 1000; ///< Default number
static const uint16_t DEFAULT_RAINFOREST_TREE_STEPS = 15000; ///< Default number of attempts for placing extra trees at rainforest in tropic. static const uint16_t DEFAULT_RAINFOREST_TREE_STEPS = 15000; ///< Default number of attempts for placing extra trees at rainforest in tropic.
static const uint16_t EDITOR_TREE_DIV = 5; ///< Game editor tree generation divisor factor. static const uint16_t EDITOR_TREE_DIV = 5; ///< Game editor tree generation divisor factor.
static const uint16_t FOREST_THRESHOLD = 6; ///< Minimum amount of trees required to be considered a forest. static const uint16_t FOREST_THRESHOLD = 6; ///< Minimum amount of trees required to be considered a forest.
static const uint16_t FOREST_SEARCH_RADIUS = 2; ///< Radius of area to examine when determining forest status.
/** /**
* Tests if a tile can be converted to MP_TREES * Tests if a tile can be converted to MP_TREES
@@ -96,17 +97,13 @@ static bool IsNearbyForest(TileIndex tile)
if (IsTileType(tile, MP_TREES)) return true; if (IsTileType(tile, MP_TREES)) return true;
/* Count the trees around the clear tile to determine if it's near a forest */ /* Count the trees around the clear tile to determine if it's near a forest */
for (int x = -2; x <= 2; x++) { for (TileIndex t : TileArea(tile).Expand(FOREST_SEARCH_RADIUS)) {
for (int y = -2; y <= 2; y++) { if (!IsTileType(t, MP_TREES)) continue;
TileIndex vincity = TileAddWrap(tile, x, y); ++planted_tile_count;
if (vincity != INVALID_TILE && if (planted_tile_count >= FOREST_THRESHOLD) return true;
IsTileType(vincity, MP_TREES)) {
planted_tile_count++;
}
}
} }
return (planted_tile_count >= FOREST_THRESHOLD); return false;
} }
/** /**
@@ -802,7 +799,7 @@ static void TileLoop_Trees(TileIndex tile)
break; break;
case 6: // final stage of tree destruction case 6: // final stage of tree destruction
if (!CanPlantExtraTrees(tile)) { if (!CanPlantExtraTrees(tile) || (_settings_game.game_creation.tree_placer == TP_IMPROVED && !IsNearbyForest(tile))) {
/* if trees can't spread just plant a new one to prevent deforestation */ /* if trees can't spread just plant a new one to prevent deforestation */
SetTreeGrowth(tile, 0); SetTreeGrowth(tile, 0);
} else if (GetTreeCount(tile) > 1) { } else if (GetTreeCount(tile) > 1) {
@@ -810,12 +807,6 @@ static void TileLoop_Trees(TileIndex tile)
AddTreeCount(tile, -1); AddTreeCount(tile, -1);
SetTreeGrowth(tile, 3); SetTreeGrowth(tile, 3);
} else { } else {
/* Backups the type of tree if using improved trees */
TreeType treetype;
if (_settings_game.game_creation.tree_placer == TP_IMPROVED && IsTileType(tile, MP_TREES)) {
treetype = GetTreeType(tile);
}
/* just one tree, change type into MP_CLEAR */ /* just one tree, change type into MP_CLEAR */
switch (GetTreeGround(tile)) { switch (GetTreeGround(tile)) {
case TREE_GROUND_SHORE: MakeShore(tile); break; case TREE_GROUND_SHORE: MakeShore(tile); break;
@@ -837,11 +828,6 @@ static void TileLoop_Trees(TileIndex tile)
} }
break; break;
} }
/* When using improved trees, when a "alone" tree is dead, a new one is planted immediately. */
if (_settings_game.game_creation.tree_placer == TP_IMPROVED && !IsNearbyForest(tile)) {
PlantTreesOnTile(tile, treetype, 0, 0);
}
} }
break; break;