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

Compare commits

...

10 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
Susan
573b55cf9f Codechange: remove magic number 2024-05-28 01:59:17 +01:00
Su
23f8519624 Codefix: Remove typos and use correct formatting
Co-authored-by: Tyler Trahan <tyler@tylertrahan.com>
2024-05-28 01:59:17 +01:00
Sylvain Devidal
ac06b4a7a6 Fix: "Alone" trees are now replaced after death. They also can spread on their own tile. 2024-05-28 01:59:17 +01:00
Sylvain Devidal
3a3b1c5f04 Codechange: Merged "Improved" and "Forest" options. 2024-05-28 01:59:17 +01:00
Sylvain Devidal
a2dd8ed419 Codechange: Use of proper FALLTHROUGH macro 2024-05-28 01:59:17 +01:00
Sylvain Devidal
53d2153844 Feature: New Trees Placement Algorithm "Forest" 2024-05-28 01:59:17 +01:00

View File

@@ -57,6 +57,8 @@ uint8_t _trees_tick_ctr;
static const uint16_t DEFAULT_TREE_STEPS = 1000; ///< Default number of attempts for placing trees.
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 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
@@ -80,6 +82,30 @@ static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
}
}
/**
* Tests if a tile is near an already existing forest.
* Trees won't spread if not placed near some other trees.
*
* @param tile the tile of interest
* @return true if the tile is near a forest
*/
static bool IsNearbyForest(TileIndex tile)
{
uint planted_tile_count = 0;
/* An already planted tile can always be planted again. */
if (IsTileType(tile, MP_TREES)) return true;
/* Count the trees around the clear tile to determine if it's near a forest */
for (TileIndex t : TileArea(tile).Expand(FOREST_SEARCH_RADIUS)) {
if (!IsTileType(t, MP_TREES)) continue;
++planted_tile_count;
if (planted_tile_count >= FOREST_THRESHOLD) return true;
}
return false;
}
/**
* Creates a tree tile
* Ground type and density is preserved.
@@ -758,6 +784,9 @@ static void TileLoop_Trees(TileIndex tile)
/* Don't plant trees, if ground was freshly cleared */
if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
/* Plants trees only near existing forests */
if (_settings_game.game_creation.tree_placer == TP_IMPROVED && !IsNearbyForest(tile)) return;
PlantTreesOnTile(tile, treetype, 0, 0);
break;
@@ -770,7 +799,7 @@ static void TileLoop_Trees(TileIndex tile)
break;
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 */
SetTreeGrowth(tile, 0);
} else if (GetTreeCount(tile) > 1) {
@@ -859,6 +888,9 @@ void OnTick_Trees()
r = Random();
tile = RandomTileSeed(r);
if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
/* Plants trees only near existing forests */
if (_settings_game.game_creation.tree_placer == TP_IMPROVED && !IsNearbyForest(tile)) return;
PlantTreesOnTile(tile, tree, 0, 0);
}
}