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

Compare commits

...

6 Commits

Author SHA1 Message Date
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,7 @@ 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.
/**
* Tests if a tile can be converted to MP_TREES
@@ -80,6 +81,34 @@ 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 (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
TileIndex vincity = TileAddWrap(tile, x, y);
if (vincity != INVALID_TILE &&
IsTileType(vincity, MP_TREES)) {
planted_tile_count++;
}
}
}
return (planted_tile_count >= FOREST_THRESHOLD);
}
/**
* Creates a tree tile
* Ground type and density is preserved.
@@ -758,6 +787,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;
@@ -778,6 +810,12 @@ static void TileLoop_Trees(TileIndex tile)
AddTreeCount(tile, -1);
SetTreeGrowth(tile, 3);
} 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 */
switch (GetTreeGround(tile)) {
case TREE_GROUND_SHORE: MakeShore(tile); break;
@@ -799,6 +837,11 @@ static void TileLoop_Trees(TileIndex tile)
}
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;
@@ -859,6 +902,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);
}
}