OpenTTD Source 20241224-master-gee860a5c8e
tree_cmd.cpp File Reference

Handling of tree tiles. More...

#include "stdafx.h"
#include "clear_map.h"
#include "landscape.h"
#include "tree_map.h"
#include "viewport_func.h"
#include "command_func.h"
#include "town.h"
#include "genworld.h"
#include "clear_func.h"
#include "company_func.h"
#include "sound_func.h"
#include "water.h"
#include "company_base.h"
#include "core/random_func.hpp"
#include "newgrf_generic.h"
#include "timer/timer_game_tick.h"
#include "tree_cmd.h"
#include "landscape_cmd.h"
#include "table/strings.h"
#include "table/tree_land.h"
#include "table/clear_land.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  TreeListEnt
 

Enumerations

enum  TreePlacer { TP_NONE , TP_ORIGINAL , TP_IMPROVED }
 List of tree placer algorithm. More...
 
enum  ExtraTreePlacement { ETP_NO_SPREAD , ETP_SPREAD_RAINFOREST , ETP_SPREAD_ALL , ETP_NO_GROWTH_NO_SPREAD }
 Where to place trees while in-game? More...
 

Functions

static bool CanPlantTreesOnTile (TileIndex tile, bool allow_desert)
 Tests if a tile can be converted to MP_TREES This is true for clear ground without farms or rocks.
 
static void PlantTreesOnTile (TileIndex tile, TreeType treetype, uint count, TreeGrowthStage growth)
 Creates a tree tile Ground type and density is preserved.
 
static TreeType GetRandomTreeType (TileIndex tile, uint seed)
 Get a random TreeType for the given tile based on a given seed.
 
static void PlaceTree (TileIndex tile, uint32_t r)
 Make a random tree tile of the given tile.
 
static void PlaceTreeGroups (uint num_groups)
 Creates a number of tree groups.
 
static void PlaceTreeAtSameHeight (TileIndex tile, int height)
 Place a tree at the same height as an existing tree.
 
void PlaceTreesRandomly ()
 Place some trees randomly.
 
uint PlaceTreeGroupAroundTile (TileIndex tile, TreeType treetype, uint radius, uint count, bool set_zone)
 Place some trees in a radius around a tile.
 
void GenerateTrees ()
 Place new trees.
 
CommandCost CmdPlantTree (DoCommandFlag flags, TileIndex tile, TileIndex start_tile, uint8_t tree_to_plant, bool diagonal)
 Plant a tree.
 
static void DrawTile_Trees (TileInfo *ti)
 
static int GetSlopePixelZ_Trees (TileIndex tile, uint x, uint y, bool)
 
static Foundation GetFoundation_Trees (TileIndex, Slope)
 
static CommandCost ClearTile_Trees (TileIndex tile, DoCommandFlag flags)
 
static void GetTileDesc_Trees (TileIndex tile, TileDesc *td)
 
static void TileLoopTreesDesert (TileIndex tile)
 
static void TileLoopTreesAlps (TileIndex tile)
 
static bool CanPlantExtraTrees (TileIndex tile)
 
static void TileLoop_Trees (TileIndex tile)
 
bool DecrementTreeCounter ()
 Decrement the tree tick counter.
 
void OnTick_Trees ()
 
static TrackStatus GetTileTrackStatus_Trees (TileIndex, TransportType, uint, DiagDirection)
 
static void ChangeTileOwner_Trees (TileIndex, Owner, Owner)
 
void InitializeTrees ()
 
static CommandCost TerraformTile_Trees (TileIndex tile, DoCommandFlag flags, int, Slope)
 

Variables

uint8_t _trees_tick_ctr
 Determines when to consider building more trees.
 
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.
 
const TileTypeProcs _tile_type_trees_procs
 

Detailed Description

Handling of tree tiles.

Definition in file tree_cmd.cpp.

Enumeration Type Documentation

◆ ExtraTreePlacement

Where to place trees while in-game?

Enumerator
ETP_NO_SPREAD 

Grow trees on tiles that have them but don't spread to new ones.

ETP_SPREAD_RAINFOREST 

Grow trees on tiles that have them, only spread to new ones in rainforests.

ETP_SPREAD_ALL 

Grow trees and spread them without restrictions.

ETP_NO_GROWTH_NO_SPREAD 

Don't grow trees and don't spread them at all.

Definition at line 47 of file tree_cmd.cpp.

◆ TreePlacer

enum TreePlacer

List of tree placer algorithm.

This enumeration defines all possible tree placer algorithm in the game.

Enumerator
TP_NONE 

No tree placer algorithm.

TP_ORIGINAL 

The original algorithm.

TP_IMPROVED 

A 'improved' algorithm.

Definition at line 40 of file tree_cmd.cpp.

Function Documentation

◆ CanPlantExtraTrees()

static bool CanPlantExtraTrees ( TileIndex  tile)
static

Definition at line 690 of file tree_cmd.cpp.

◆ CanPlantTreesOnTile()

static bool CanPlantTreesOnTile ( TileIndex  tile,
bool  allow_desert 
)
static

Tests if a tile can be converted to MP_TREES This is true for clear ground without farms or rocks.

Parameters
tilethe tile of interest
allow_desertAllow planting trees on CLEAR_DESERT?
Returns
true if trees can be built.

Definition at line 69 of file tree_cmd.cpp.

References CLEAR_DESERT, CLEAR_FIELDS, CLEAR_ROCKS, GetRawClearGround(), GetTileSlope(), GetTileType(), IsBridgeAbove(), IsClearGround(), IsCoast(), IsSlopeWithOneCornerRaised(), MP_CLEAR, and MP_WATER.

Referenced by PlaceTreeAtSameHeight(), PlaceTreeGroupAroundTile(), PlaceTreeGroups(), PlaceTreesRandomly(), and PlantTreesOnTile().

◆ ChangeTileOwner_Trees()

static void ChangeTileOwner_Trees ( TileIndex  ,
Owner  ,
Owner   
)
static

Definition at line 873 of file tree_cmd.cpp.

◆ ClearTile_Trees()

static CommandCost ClearTile_Trees ( TileIndex  tile,
DoCommandFlag  flags 
)
static

Definition at line 602 of file tree_cmd.cpp.

◆ CmdPlantTree()

CommandCost CmdPlantTree ( DoCommandFlag  flags,
TileIndex  tile,
TileIndex  start_tile,
uint8_t  tree_to_plant,
bool  diagonal 
)

◆ DecrementTreeCounter()

bool DecrementTreeCounter ( )

Decrement the tree tick counter.

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.

Returns
true if the counter was decremented past zero

Definition at line 821 of file tree_cmd.cpp.

References _trees_tick_ctr, MAX_MAP_SIZE_BITS, MIN_MAP_SIZE_BITS, and Map::ScaleBySize().

◆ DrawTile_Trees()

static void DrawTile_Trees ( TileInfo ti)
static

Definition at line 519 of file tree_cmd.cpp.

◆ GenerateTrees()

void GenerateTrees ( )

Place new trees.

This function takes care of the selected tree placer algorithm and place randomly the trees for a new game.

Definition at line 354 of file tree_cmd.cpp.

References _settings_game, DEFAULT_RAINFOREST_TREE_STEPS, DEFAULT_TREE_STEPS, GameSettings::game_creation, GB(), GWP_TREE, GameCreationSettings::landscape, PlaceTreeGroups(), PlaceTreesRandomly(), Map::ScaleBySize(), SetGeneratingWorldProgress(), TP_IMPROVED, TP_NONE, TP_ORIGINAL, and GameCreationSettings::tree_placer.

Referenced by _GenerateWorld().

◆ GetFoundation_Trees()

static Foundation GetFoundation_Trees ( TileIndex  ,
Slope   
)
static

Definition at line 597 of file tree_cmd.cpp.

◆ GetRandomTreeType()

static TreeType GetRandomTreeType ( TileIndex  tile,
uint  seed 
)
static

Get a random TreeType for the given tile based on a given seed.

This function returns a random TreeType which can be placed on the given tile. The seed for randomness must be less or equal 256, use GB on the value of Random() to get such a value.

Parameters
tileThe tile to get a random TreeType from
seedThe seed for randomness, must be less or equal 256
Returns
The random tree type

Definition at line 135 of file tree_cmd.cpp.

References _settings_game, GameSettings::game_creation, GetTropicZone(), GameCreationSettings::landscape, TREE_CACTUS, TREE_COUNT_RAINFOREST, TREE_COUNT_SUB_ARCTIC, TREE_COUNT_SUB_TROPICAL, TREE_COUNT_TEMPERATE, TREE_COUNT_TOYLAND, TREE_INVALID, TREE_RAINFOREST, TREE_SUB_ARCTIC, TREE_SUB_TROPICAL, TREE_TEMPERATE, TREE_TOYLAND, TROPICZONE_DESERT, and TROPICZONE_NORMAL.

Referenced by CmdPlantTree(), and PlaceTree().

◆ GetSlopePixelZ_Trees()

static int GetSlopePixelZ_Trees ( TileIndex  tile,
uint  x,
uint  y,
bool   
)
static

Definition at line 590 of file tree_cmd.cpp.

◆ GetTileDesc_Trees()

static void GetTileDesc_Trees ( TileIndex  tile,
TileDesc td 
)
static

Definition at line 619 of file tree_cmd.cpp.

◆ GetTileTrackStatus_Trees()

static TrackStatus GetTileTrackStatus_Trees ( TileIndex  ,
TransportType  ,
uint  ,
DiagDirection   
)
static

Definition at line 868 of file tree_cmd.cpp.

◆ InitializeTrees()

void InitializeTrees ( )

Definition at line 878 of file tree_cmd.cpp.

◆ OnTick_Trees()

void OnTick_Trees ( )

Definition at line 832 of file tree_cmd.cpp.

◆ PlaceTree()

static void PlaceTree ( TileIndex  tile,
uint32_t  r 
)
static

Make a random tree tile of the given tile.

Create a new tree-tile for the given tile. The second parameter is used for randomness like type and number of trees.

Parameters
tileThe tile to make a tree-tile from
rThe randomness value from a Random() value

Definition at line 165 of file tree_cmd.cpp.

References GB(), GetRandomTreeType(), GetTreeGround(), MarkTileDirtyByTile(), PlantTreesOnTile(), SetTreeGroundDensity(), TREE_GROUND_ROUGH_SNOW, TREE_GROUND_SHORE, TREE_GROUND_SNOW_DESERT, and TREE_INVALID.

Referenced by PlaceTreeAtSameHeight(), PlaceTreeGroups(), and PlaceTreesRandomly().

◆ PlaceTreeAtSameHeight()

static void PlaceTreeAtSameHeight ( TileIndex  tile,
int  height 
)
static

Place a tree at the same height as an existing tree.

Add a new tree around the given tile which is at the same height or at some offset (2 units) of it.

Parameters
tileThe base tile to add a new tree somewhere around
heightThe height (like the one from the tile)

Definition at line 218 of file tree_cmd.cpp.

References abs(), CanPlantTreesOnTile(), DEFAULT_TREE_STEPS, Delta(), GB(), GetTileZ(), INVALID_TILE, PlaceTree(), and TileAddWrap().

Referenced by PlaceTreesRandomly().

◆ PlaceTreeGroupAroundTile()

uint PlaceTreeGroupAroundTile ( TileIndex  tile,
TreeType  treetype,
uint  radius,
uint  count,
bool  set_zone 
)

Place some trees in a radius around a tile.

The trees are placed in an quasi-normal distribution around the indicated tile, meaning that while the radius does define a square, the distribution inside the square will be roughly circular.

Note
This function the interactive RNG and must only be used in editor and map generation.
Parameters
tileTile to place trees around.
treetypeType of trees to place. Must be a valid tree type for the climate.
radiusMaximum distance (on each axis) from tile to place trees.
countMaximum number of trees to place.
set_zoneWhether to create a rainforest zone when placing rainforest trees.
Returns
Number of trees actually placed.

Definition at line 307 of file tree_cmd.cpp.

References AddTreeCount(), CanPlantTreesOnTile(), DistanceSquare(), OrthogonalTileArea::Expand(), GetTileType(), GetTreeCount(), Growing1, Grown, INVALID_TILE, IsInsideMM(), IsTileType(), MarkTileDirtyByTile(), MP_TREES, MP_VOID, PlantTreesOnTile(), SetTreeGrowth(), SetTropicZone(), TileAddWrap(), TREE_CACTUS, TREE_COUNT_TOYLAND, TREE_RAINFOREST, TREE_TOYLAND, and TROPICZONE_RAINFOREST.

◆ PlaceTreeGroups()

static void PlaceTreeGroups ( uint  num_groups)
static

Creates a number of tree groups.

The number of trees in each group depends on how many trees are actually placed around the given tile.

Parameters
num_groupsNumber of tree groups to place.

Definition at line 187 of file tree_cmd.cpp.

References abs(), CanPlantTreesOnTile(), DEFAULT_TREE_STEPS, GB(), GWP_TREE, IncreaseGeneratingWorldProgress(), INVALID_TILE, PlaceTree(), RandomTile, and TileAddWrap().

Referenced by GenerateTrees().

◆ PlaceTreesRandomly()

◆ PlantTreesOnTile()

static void PlantTreesOnTile ( TileIndex  tile,
TreeType  treetype,
uint  count,
TreeGrowthStage  growth 
)
static

Creates a tree tile Ground type and density is preserved.

Precondition
the tile must be suitable for trees.
Parameters
tilewhere to plant the trees.
treetypeThe type of the tree
countthe number of trees (minus 1)
growththe growth status

Definition at line 94 of file tree_cmd.cpp.

References CanPlantTreesOnTile(), CLEAR_GRASS, CLEAR_ROUGH, CLEAR_SNOW, ClearNeighbourNonFloodingStates(), GetClearDensity(), GetClearGround(), GetRawClearGround(), GetTileType(), MakeTree(), MP_CLEAR, MP_WATER, TREE_GROUND_GRASS, TREE_GROUND_ROUGH, TREE_GROUND_ROUGH_SNOW, TREE_GROUND_SHORE, TREE_GROUND_SNOW_DESERT, and TREE_INVALID.

Referenced by CmdPlantTree(), PlaceTree(), and PlaceTreeGroupAroundTile().

◆ TerraformTile_Trees()

static CommandCost TerraformTile_Trees ( TileIndex  tile,
DoCommandFlag  flags,
int  ,
Slope   
)
static

Definition at line 883 of file tree_cmd.cpp.

◆ TileLoop_Trees()

static void TileLoop_Trees ( TileIndex  tile)
static

Definition at line 697 of file tree_cmd.cpp.

◆ TileLoopTreesAlps()

static void TileLoopTreesAlps ( TileIndex  tile)
static

Definition at line 659 of file tree_cmd.cpp.

◆ TileLoopTreesDesert()

static void TileLoopTreesDesert ( TileIndex  tile)
static

Definition at line 632 of file tree_cmd.cpp.

Variable Documentation

◆ _tile_type_trees_procs

const TileTypeProcs _tile_type_trees_procs
extern
Initial value:
= {
DrawTile_Trees,
GetSlopePixelZ_Trees,
ClearTile_Trees,
nullptr,
GetTileDesc_Trees,
GetTileTrackStatus_Trees,
nullptr,
nullptr,
TileLoop_Trees,
ChangeTileOwner_Trees,
nullptr,
nullptr,
GetFoundation_Trees,
TerraformTile_Trees,
}

Definition at line 52 of file landscape.cpp.

◆ _trees_tick_ctr

uint8_t _trees_tick_ctr

Determines when to consider building more trees.

Definition at line 55 of file tree_cmd.cpp.

Referenced by DecrementTreeCounter().

◆ DEFAULT_RAINFOREST_TREE_STEPS

const uint16_t DEFAULT_RAINFOREST_TREE_STEPS = 15000
static

Default number of attempts for placing extra trees at rainforest in tropic.

Definition at line 58 of file tree_cmd.cpp.

Referenced by GenerateTrees(), and PlaceTreesRandomly().

◆ DEFAULT_TREE_STEPS

const uint16_t DEFAULT_TREE_STEPS = 1000
static

Default number of attempts for placing trees.

Definition at line 57 of file tree_cmd.cpp.

Referenced by GenerateTrees(), PlaceTreeAtSameHeight(), PlaceTreeGroups(), and PlaceTreesRandomly().

◆ EDITOR_TREE_DIV

const uint16_t EDITOR_TREE_DIV = 5
static

Game editor tree generation divisor factor.

Definition at line 59 of file tree_cmd.cpp.

Referenced by PlaceTreesRandomly().