# 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 "economy_func.h"`
`#include "town.h"`
`#include "variables.h"`
`#include "genworld.h"`
`#include "transparency.h"`
`#include "functions.h"`
`#include "company_func.h"`
`#include "sound_func.h"`
`#include "water_map.h"`
`#include "water.h"`
`#include "landscape_type.h"`
`#include "company_base.h"`
`#include "core/random_func.hpp"`
`#include "table/strings.h"`
`#include "table/sprites.h"`
`#include "table/tree_land.h"`
`#include "table/clear_land.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_NONE, ETP_RAINFOREST, ETP_ALL }
| 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, uint 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 r)
| Make a random tree tile of the given tile.
static void | DoPlaceMoreTrees (TileIndex tile)
| Place some amount of trees around a given tile.
static void | PlaceMoreTrees ()
| Place more trees on the map.
static void | PlaceTreeAtSameHeight (TileIndex tile, uint height)
| Place a tree at the same height as an existing tree.
void | PlaceTreesRandomly ()
| Place some trees randomly.
void | GenerateTrees ()
| Place new trees.
CommandCost | CmdPlantTree (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Plant a tree.
static void | DrawTile_Trees (TileInfo *ti)
static uint | GetSlopeZ_Trees (TileIndex tile, uint x, uint y)
static Foundation | GetFoundation_Trees (TileIndex tile, Slope tileh)
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 void | TileLoop_Trees (TileIndex tile)
void | OnTick_Trees ()
static TrackStatus | GetTileTrackStatus_Trees (TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
static void | ChangeTileOwner_Trees (TileIndex tile, Owner old_owner, Owner new_owner)
void | InitializeTrees ()
static CommandCost | TerraformTile_Trees (TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
## Variables
const TileTypeProcs | _tile_type_trees_procs

## Detailed Description

Handling of tree tiles.

Definition in file tree_cmd.cpp.

## Enumeration Type Documentation

enum ExtraTreePlacement

Where to place trees while in-game?

Enumerator:

  ETP_NONE | Place trees on no tiles.
  ETP_RAINFOREST | Place trees only on rainforest tiles.
  ETP_ALL | Place trees on all tiles.

Definition at line 49 of file tree_cmd.cpp.

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

## Function Documentation

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:

  | tile | the tile of interest
  | allow_desert | Allow planting trees on CLEAR_DESERT?

Returns:
  true if trees can be built.

Definition at line 64 of file tree_cmd.cpp.

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

Referenced by DoPlaceMoreTrees(), PlaceTreeAtSameHeight(), PlaceTreesRandomly(), and PlantTreesOnTile().

CommandCost CmdPlantTree | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Plant a tree.

Parameters:

  | tile | start tile of area-drag of tree plantation
  | flags | type of operation
  | p1 | tree type, -1 means random.
  | p2 | end tile of area-drag
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 344 of file tree_cmd.cpp.

References _settings_game, CommandCost::AddCost(), AddTreeCount(), ChangeTownRating(), CLEAR_FIELDS, CLEAR_ROCKS, CMD_ERROR, CMD_LANDSCAPE_CLEAR, DC_EXEC, EconomySettings::dist_local_authority, DoCommand(), GameSettings::economy, EXPENSES_OTHER, CommandCost::Failed(), GameSettings::game_creation, GB(), CommandCost::GetCost(), GetRandomTreeType(), GetRawClearGround(), GetTileSlope(), GetTileType(), GetTreeCount(), INVALID_STRING_ID, IsBridgeAbove(), IsInsideMM(), IsSlopeWithOneCornerRaised(), IsTileType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), GameCreationSettings::landscape, MapSize(), MarkTileDirtyByTile(), MP_CLEAR, MP_TREES, MP_WATER, PlantTreesOnTile(), return_cmd_error, SetTropicZone(), TILE_AREA_LOOP, TREE_CACTUS, TREE_INVALID, TREE_RAINFOREST, and TROPICZONE_RAINFOREST.

static void DoPlaceMoreTrees | ( | TileIndex | tile | ) | `[static]`

Place some amount of trees around a given tile.

This function adds some trees around a given tile. As this function use the Random() call it depends on the random how many trees are actually placed around the given tile.

Parameters:

  | tile | The center of the trees to add

Definition at line 186 of file tree_cmd.cpp.

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

Referenced by PlaceMoreTrees().

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

References _settings_game, GameSettings::game_creation, GWP_TREE, GameCreationSettings::landscape, PlaceMoreTrees(), PlaceTreesRandomly(), ScaleByMapSize(), SetGeneratingWorldProgress(), TP_IMPROVED, TP_NONE, TP_ORIGINAL, and GameCreationSettings::tree_placer.

Referenced by _GenerateWorld().

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:

  | tile | The tile to get a random TreeType from
  | seed | The seed for randomness, must be less or equal 256

Returns:
  The random tree type

Definition at line 129 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().

static void PlaceMoreTrees | ( |  | ) | `[static]`

Place more trees on the map.

This function add more trees to the map.

Definition at line 208 of file tree_cmd.cpp.

References DoPlaceMoreTrees(), GB(), RandomTile, and ScaleByMapSize().

Referenced by GenerateTrees().

static void PlaceTree | ( | TileIndex | tile,
|  | uint32 | 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:

  | tile | The tile to make a tree-tile from
  | r | The randomness value from a Random() value

Definition at line 159 of file tree_cmd.cpp.

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

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

static void PlaceTreeAtSameHeight | ( | TileIndex | tile,
|  | uint | 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:

  | tile | The base tile to add a new tree somewhere around
  | height | The height (like the one from the tile)

Definition at line 225 of file tree_cmd.cpp.

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

Referenced by PlaceTreesRandomly().

void PlaceTreesRandomly | ( |  | ) |

Place some trees randomly.

This function just place some trees randomly on the map.

Definition at line 256 of file tree_cmd.cpp.

References _settings_game, CanPlantTreesOnTile(), GameSettings::game_creation, GetSnowLine(), GetTileZ(), GetTropicZone(), GWP_TREE, IncreaseGeneratingWorldProgress(), GameCreationSettings::landscape, PlaceTree(), PlaceTreeAtSameHeight(), RandomTileSeed(), ScaleByMapSize(), TILE_HEIGHT, TP_IMPROVED, GameCreationSettings::tree_placer, and TROPICZONE_RAINFOREST.

Referenced by GenerateTrees(), and BuildTreesWindow::OnClick().

static void PlantTreesOnTile | ( | TileIndex | tile,
|  | TreeType | treetype,
|  | uint | count,
|  | uint | growth |
| ) |  |  | `[static]`

Creates a tree tile Ground type and density is preserved.

Precondition:
  the tile must be suitable for trees.

Parameters:

  | tile | where to plant the trees.
  | treetype | The type of the tree
  | count | the number of trees (minus 1)
  | growth | the growth status

Definition at line 89 of file tree_cmd.cpp.

References CanPlantTreesOnTile(), CLEAR_GRASS, CLEAR_ROUGH, CLEAR_SNOW, 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(), and PlaceTree().

## Variable Documentation

const TileTypeProcs _tile_type_trees_procs

Initial value:

     {
      DrawTile_Trees,
      GetSlopeZ_Trees,
      ClearTile_Trees,
      NULL,
      GetTileDesc_Trees,
      GetTileTrackStatus_Trees,
      NULL,
      NULL,
      TileLoop_Trees,
      ChangeTileOwner_Trees,
      NULL,
      NULL,
      GetFoundation_Trees,
      TerraformTile_Trees,
    }