OpenTTD Source 20260311-master-g511d3794ce
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/geometry_type.hpp"
#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  BlobHarmonic
struct  TreeListEnt

Enumerations

enum  ExtraTreePlacement : uint8_t { 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 TileType::Trees This is true for clear ground without farms or rocks.
static TreeGround TreeGroundFromClearGround (ClearGround clearground)
 Get equivalent TreeGround for a ClearGround.
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.
void PlaceTree (TileIndex tile, uint32_t r, bool keep_density)
 Make a random tree tile of the given tile.
static void CreateStarShapedPolygon (int radius, std::span< const BlobHarmonic > harmonics, std::span< Point > shape)
 Creates a star-shaped polygon originating from (0, 0) as defined by the given harmonics.
static void CreateRandomStarShapedPolygon (int radius, std::span< Point > shape)
 Creates a random star-shaped polygon originating from (0, 0).
static bool IsPointInTriangle (int x, int y, const Point &v1, const Point &v2, const Point &v3)
 Returns true if the given coordinates lie within a triangle.
static bool IsPointInStarShapedPolygon (int x, int y, std::span< Point > shape)
 Returns true if the given coordinates lie within a star shaped polygon.
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 (DoCommandFlags flags, TileIndex tile, TileIndex start_tile, uint8_t tree_to_plant, bool diagonal)
 Plant a tree.
static void DrawTile_Trees (TileInfo *ti)
 Tile callback function signature for drawing a tile and its contents to the screen.
static int GetSlopePixelZ_Trees (TileIndex tile, uint x, uint y, bool ground_vehicle)
 Tile callback function signature for obtaining the world Z coordinate of a given point of a tile.
static CommandCost ClearTile_Trees (TileIndex tile, DoCommandFlags flags)
 Tile callback function signature for clearing a tile.
static void GetTileDesc_Trees (TileIndex tile, TileDesc &td)
 Tile callback function signature for obtaining a tile description.
static void TileLoopTreesDesert (TileIndex tile)
static void TileLoopTreesAlps (TileIndex tile)
static bool TreesOnTileCanSpread (TileIndex tile)
 Check if trees on this tile are allowed to spread.
static void TileLoop_Trees (TileIndex tile)
 Tile callback function signature for running periodic tile updates.
bool DecrementTreeCounter ()
 Decrement the tree tick counter.
static void PlantRandomTree (bool rainforest)
 Place a random tree on a random tile.
void OnTick_Trees ()
void InitializeTrees ()

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
 TileTypeProcs definitions for TileType::Trees tiles.

Detailed Description

Handling of tree tiles.

Definition in file tree_cmd.cpp.

Enumeration Type Documentation

◆ ExtraTreePlacement

enum ExtraTreePlacement : uint8_t

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 37 of file tree_cmd.cpp.

Function Documentation

◆ CanPlantTreesOnTile()

bool CanPlantTreesOnTile ( TileIndex tile,
bool allow_desert )
static

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

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

Definition at line 59 of file tree_cmd.cpp.

References Clear, Desert, Fields, GetTileSlope(), GetTileType(), IsBridgeAbove(), IsClearGround(), IsCoast(), IsSlopeWithOneCornerRaised(), Rocks, and Water.

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

◆ ClearTile_Trees()

CommandCost ClearTile_Trees ( TileIndex tile,
DoCommandFlags flags )
static

Tile callback function signature for clearing a tile.

Parameters
tileThe tile to clear.
flagsThe command flags.
Returns
The cost or error.
See also
ClearTile

Definition at line 726 of file tree_cmd.cpp.

References _current_company, _price, _settings_game, ChangeTownRating(), ClearTrees, ClosestTownFromTile(), Execute, EXPENSES_CONSTRUCTION, GetTreeCount(), GetTreeType(), IsInsideMM(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), TREE_CACTUS, and TREE_RAINFOREST.

◆ CmdPlantTree()

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

◆ CreateRandomStarShapedPolygon()

void CreateRandomStarShapedPolygon ( int radius,
std::span< Point > shape )
static

Creates a random star-shaped polygon originating from (0, 0).

The shape is placed into a pre-allocated span so the caller controls allocation.

Parameters
radiusThe maximum radius of the blob. May be smaller, but will not be larger.
[out]shapeShape to fill with polygon points.

Definition at line 234 of file tree_cmd.cpp.

References CreateStarShapedPolygon().

Referenced by PlaceTreeGroups().

◆ CreateStarShapedPolygon()

void CreateStarShapedPolygon ( int radius,
std::span< const BlobHarmonic > harmonics,
std::span< Point > shape )
static

Creates a star-shaped polygon originating from (0, 0) as defined by the given harmonics.

The shape is placed into a pre-allocated span so the caller controls allocation.

Parameters
radiusThe maximum radius of the polygon. May be smaller, but will not be larger.
harmonicsHarmonics data for the polygon.
[out]shapeShape to fill with points.

Definition at line 203 of file tree_cmd.cpp.

References Point.

Referenced by CreateRandomStarShapedPolygon().

◆ 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 962 of file tree_cmd.cpp.

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

◆ DrawTile_Trees()

◆ 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 479 of file tree_cmd.cpp.

References _settings_game, Arctic, DEFAULT_RAINFOREST_TREE_STEPS, DEFAULT_TREE_STEPS, GB(), GWP_TREE, Improved, None, Original, PlaceTreeGroups(), PlaceTreesRandomly(), Map::ScaleBySize(), SetGeneratingWorldProgress(), Toyland, and Tropic.

Referenced by _GenerateWorld().

◆ GetRandomTreeType()

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 140 of file tree_cmd.cpp.

References _settings_game, Arctic, GetTropicZone(), Temperate, 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, Tropic, TROPICZONE_DESERT, and TROPICZONE_NORMAL.

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

◆ GetSlopePixelZ_Trees()

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

Tile callback function signature for obtaining the world Z coordinate of a given point of a tile.

Parameters
tileThe queries tile for the Z coordinate.
xWorld X coordinate in tile "units".
yWorld Y coordinate in tile "units".
ground_vehicleWhether to get the Z coordinate of the ground vehicle, or the ground.
Returns
World Z coordinate at tile ground (vehicle) level, including slopes and foundations.
See also
GetSlopePixelZ

Definition at line 718 of file tree_cmd.cpp.

References GetPartialPixelZ(), and GetTilePixelSlope().

◆ GetTileDesc_Trees()

void GetTileDesc_Trees ( TileIndex tile,
TileDesc & td )
static

Tile callback function signature for obtaining a tile description.

Parameters
tileTile being queried
tdStorage pointer for returned tile description
See also
GetTileDesc

Definition at line 742 of file tree_cmd.cpp.

References GetTileOwner(), GetTreeType(), IsInsideMM(), TileDesc::owner, TileDesc::str, TREE_CACTUS, and TREE_RAINFOREST.

◆ InitializeTrees()

void InitializeTrees ( )

Definition at line 1015 of file tree_cmd.cpp.

◆ IsPointInStarShapedPolygon()

bool IsPointInStarShapedPolygon ( int x,
int y,
std::span< Point > shape )
static

Returns true if the given coordinates lie within a star shaped polygon.

Breaks the polygon into a series of triangles around the centre point (0, 0) and then tests the coordinates against each triangle until a match is found (or not).

Parameters
xX coordinate relative to centre of shape.
yY coordinate relative to centre of shape.
shapeThe shape to check against.
Returns
true if the given coordinates lie within the star shaped polygon.

Definition at line 281 of file tree_cmd.cpp.

References IsPointInTriangle(), and Point.

Referenced by PlaceTreeGroups().

◆ IsPointInTriangle()

bool IsPointInTriangle ( int x,
int y,
const Point & v1,
const Point & v2,
const Point & v3 )
static

Returns true if the given coordinates lie within a triangle.

Parameters
xX coordinate relative to centre of shape.
yY coordinate relative to centre of shape.
v1First vertex of triangle.
v2Second vertex of triangle.
v3Third vertex of triangle.
Returns
true if the given coordinates lie within a triangle.

Definition at line 262 of file tree_cmd.cpp.

References Point, Coord2D< T >::x, and Coord2D< T >::y.

Referenced by IsPointInStarShapedPolygon().

◆ OnTick_Trees()

void OnTick_Trees ( )

Definition at line 991 of file tree_cmd.cpp.

◆ PlaceTree()

void PlaceTree ( TileIndex tile,
uint32_t r,
bool keep_density )

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
keep_densityWhether to keep the existing ground density of the tile.

Definition at line 171 of file tree_cmd.cpp.

References GB(), GetRandomTreeType(), GetTreeGround(), MarkTileDirtyByTile(), PlantTreesOnTile(), RoughSnow, SetTreeGroundDensity(), Shore, SnowOrDesert, and TREE_INVALID.

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

◆ PlaceTreeAtSameHeight()

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 340 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 432 of file tree_cmd.cpp.

References AddTreeCount(), CanPlantTreesOnTile(), DistanceSquare(), GB(), GetTileType(), GetTreeCount(), Growing1, Grown, INVALID_TILE, IsInsideMM(), IsTileType(), MarkTileDirtyByTile(), PlantTreesOnTile(), SetTreeGrowth(), SetTropicZone(), TileAddWrap(), TREE_CACTUS, TREE_COUNT_TOYLAND, TREE_RAINFOREST, TREE_TOYLAND, Trees, TROPICZONE_RAINFOREST, and Void.

◆ PlaceTreeGroups()

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.

< How many segments make up the tree group.

< Maximum radius of tree groups.

Definition at line 300 of file tree_cmd.cpp.

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

Referenced by GenerateTrees().

◆ PlaceTreesRandomly()

◆ PlantRandomTree()

void PlantRandomTree ( bool rainforest)
static

Place a random tree on a random tile.

Parameters
rainforestIf set the random tile must be in a rainforest zone.

Definition at line 977 of file tree_cmd.cpp.

References CanPlantTreesOnTile(), GB(), GetRandomTreeType(), GetTropicZone(), Growing1, PlantTreesOnTile(), RandomTileSeed(), TREE_INVALID, and TROPICZONE_RAINFOREST.

◆ PlantTreesOnTile()

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 98 of file tree_cmd.cpp.

References CanPlantTreesOnTile(), Clear, ClearNeighbourNonFloodingStates(), GetClearDensity(), GetClearGround(), GetTileType(), IsSnowTile(), MakeTree(), Rough, RoughSnow, Shore, SnowOrDesert, TREE_INVALID, TreeGroundFromClearGround(), and Water.

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

◆ TileLoop_Trees()

◆ TileLoopTreesAlps()

void TileLoopTreesAlps ( TileIndex tile)
static

Definition at line 782 of file tree_cmd.cpp.

◆ TileLoopTreesDesert()

void TileLoopTreesDesert ( TileIndex tile)
static

Definition at line 755 of file tree_cmd.cpp.

◆ TreeGroundFromClearGround()

TreeGround TreeGroundFromClearGround ( ClearGround clearground)
static

Get equivalent TreeGround for a ClearGround.

Parameters
cleargroundThe ClearGround.
Returns
Equivalent TreeGround.

Definition at line 78 of file tree_cmd.cpp.

References Grass, Rough, and SnowOrDesert.

Referenced by PlantTreesOnTile().

◆ TreesOnTileCanSpread()

bool TreesOnTileCanSpread ( TileIndex tile)
static

Check if trees on this tile are allowed to spread.

If they are allowed to spread, they are also allowed to die.

Parameters
tileThe tile to check.
Returns
Whether trees on this tile can spread.

Definition at line 819 of file tree_cmd.cpp.

References _settings_game, ETP_SPREAD_ALL, ETP_SPREAD_RAINFOREST, GetTropicZone(), Tropic, TROPICZONE_DESERT, and TROPICZONE_RAINFOREST.

Referenced by TileLoop_Trees().

Variable Documentation

◆ _tile_type_trees_procs

const TileTypeProcs _tile_type_trees_procs
extern
Initial value:
= {
.draw_tile_proc = DrawTile_Trees,
.get_slope_pixel_z_proc = GetSlopePixelZ_Trees,
.clear_tile_proc = ClearTile_Trees,
.get_tile_desc_proc = GetTileDesc_Trees,
.tile_loop_proc = TileLoop_Trees,
.terraform_tile_proc = [](TileIndex tile, DoCommandFlags flags, int, Slope) { return Command<Commands::LandscapeClear>::Do(flags, tile); }
}
Slope
Enumeration for the slope-type.
Definition slope_type.h:47
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
static void DrawTile_Trees(TileInfo *ti)
Tile callback function signature for drawing a tile and its contents to the screen.
Definition tree_cmd.cpp:645
static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlags flags)
Tile callback function signature for clearing a tile.
Definition tree_cmd.cpp:726
static int GetSlopePixelZ_Trees(TileIndex tile, uint x, uint y, bool ground_vehicle)
Tile callback function signature for obtaining the world Z coordinate of a given point of a tile.
Definition tree_cmd.cpp:718
static void GetTileDesc_Trees(TileIndex tile, TileDesc &td)
Tile callback function signature for obtaining a tile description.
Definition tree_cmd.cpp:742
static void TileLoop_Trees(TileIndex tile)
Tile callback function signature for running periodic tile updates.
Definition tree_cmd.cpp:838

TileTypeProcs definitions for TileType::Trees tiles.

Definition at line 56 of file landscape.cpp.

◆ _trees_tick_ctr

uint8_t _trees_tick_ctr

Determines when to consider building more trees.

Definition at line 45 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 48 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 47 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 49 of file tree_cmd.cpp.

Referenced by PlaceTreesRandomly().