OpenTTD Source 20260208-master-g43af8e94d0
tree_map.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef TREE_MAP_H
11#define TREE_MAP_H
12
13#include "tile_map.h"
14#include "water_map.h"
15
25enum TreeType : uint8_t {
29 TREE_CACTUS = 0x1B,
31 TREE_TOYLAND = 0x20,
32 TREE_INVALID = 0xFF,
33};
34
35/* Counts the number of tree types for each landscape.
36 *
37 * This list contains the counts of different tree types for each landscape. This list contains
38 * 5 entries instead of 4 (as there are only 4 landscape types) as the sub tropic landscape
39 * has two types of area, one for normal trees and one only for cacti.
40 */
45static const uint TREE_COUNT_TOYLAND = 9;
46
52enum class TreeGround : uint8_t {
53 Grass = 0,
54 Rough = 1,
56 Shore = 3,
58};
59
65enum class TreeGrowthStage : uint8_t {
69 Grown = 3,
70 Dying1 = 4,
71 Dying2 = 5,
72 Dead = 6,
73};
74
88{
89 assert(IsTileType(t, TileType::Trees));
90 return (TreeType)t.m3();
91}
92
103{
104 assert(IsTileType(t, TileType::Trees));
105 return (TreeGround)GB(t.m2(), 6, 3);
106}
107
127inline uint GetTreeDensity(Tile t)
128{
129 assert(IsTileType(t, TileType::Trees));
130 return GB(t.m2(), 4, 2);
131}
132
144inline void SetTreeGroundDensity(Tile t, TreeGround g, uint d)
145{
146 assert(IsTileType(t, TileType::Trees)); // XXX incomplete
147 SB(t.m2(), 4, 2, d);
148 SB(t.m2(), 6, 3, to_underlying(g));
150}
151
163inline uint GetTreeCount(Tile t)
164{
165 assert(IsTileType(t, TileType::Trees));
166 return GB(t.m5(), 6, 2) + 1;
167}
168
180inline void AddTreeCount(Tile t, int c)
181{
182 assert(IsTileType(t, TileType::Trees)); // XXX incomplete
183 t.m5() += c << 6;
184}
185
196{
197 assert(IsTileType(t, TileType::Trees));
198 return static_cast<TreeGrowthStage>(GB(t.m5(), 0, 3));
199}
200
210inline void AddTreeGrowth(Tile t, int a)
211{
212 assert(IsTileType(t, TileType::Trees)); // XXX incomplete
213 t.m5() += a;
214}
215
227{
228 assert(IsTileType(t, TileType::Trees)); // XXX incomplete
229 SB(t.m5(), 0, 3, to_underlying(g));
230}
231
244inline void MakeTree(Tile t, TreeType type, uint count, TreeGrowthStage growth, TreeGround ground, uint density)
245{
249 t.m2() = to_underlying(ground) << 6 | density << 4 | 0;
250 t.m3() = type;
251 t.m4() = 0 << 5 | 0 << 2;
252 t.m5() = count << 6 | to_underlying(growth);
253 SB(t.m6(), 2, 6, 0);
254 t.m7() = 0;
255 t.m8() = 0;
256}
257
258#endif /* TREE_MAP_H */
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m5()
General purpose.
Definition map_func.h:154
uint8_t & m4()
General purpose.
Definition map_func.h:143
uint8_t & m6()
General purpose.
Definition map_func.h:165
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:176
uint8_t & m3()
General purpose.
Definition map_func.h:132
uint16_t & m8()
General purpose.
Definition map_func.h:187
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:121
static constexpr Owner OWNER_NONE
The tile has no ownership.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:17
Map writing/reading functions for tiles.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
@ Trees
Tile with one or more trees.
Definition tile_type.h:53
static const uint TREE_COUNT_RAINFOREST
number of tree types for the 'rainforest part' of a sub-tropic map.
Definition tree_map.h:43
uint GetTreeCount(Tile t)
Returns the number of trees on a tile.
Definition tree_map.h:163
TreeGrowthStage GetTreeGrowth(Tile t)
Returns the tree growth stage.
Definition tree_map.h:195
static const uint TREE_COUNT_SUB_TROPICAL
number of tree types for the 'sub-tropic part' of a sub-tropic map.
Definition tree_map.h:44
static const uint TREE_COUNT_TOYLAND
number of tree types on a toyland map.
Definition tree_map.h:45
void MakeTree(Tile t, TreeType type, uint count, TreeGrowthStage growth, TreeGround ground, uint density)
Make a tree-tile.
Definition tree_map.h:244
void SetTreeGrowth(Tile t, TreeGrowthStage g)
Sets the tree growth stage of a tile.
Definition tree_map.h:226
TreeGround GetTreeGround(Tile t)
Returns the groundtype for tree tiles.
Definition tree_map.h:102
TreeType
List of tree types along all landscape types.
Definition tree_map.h:25
@ TREE_RAINFOREST
tree on the 'green part' on a sub-tropical map
Definition tree_map.h:28
@ TREE_TOYLAND
tree on a toyland map
Definition tree_map.h:31
@ TREE_SUB_ARCTIC
tree on a sub_arctic landscape
Definition tree_map.h:27
@ TREE_SUB_TROPICAL
tree on a sub-tropical map, non-rainforest, non-desert
Definition tree_map.h:30
@ TREE_TEMPERATE
temperate tree
Definition tree_map.h:26
@ TREE_CACTUS
a cactus for the 'desert part' on a sub-tropical map
Definition tree_map.h:29
@ TREE_INVALID
An invalid tree.
Definition tree_map.h:32
void AddTreeCount(Tile t, int c)
Add a amount to the tree-count value of a tile with trees.
Definition tree_map.h:180
TreeGround
Enumeration for ground types of tiles with trees.
Definition tree_map.h:52
@ SnowOrDesert
Snow or desert, depending on landscape.
Definition tree_map.h:55
@ Shore
Shore.
Definition tree_map.h:56
@ RoughSnow
A snow tile that is rough underneath.
Definition tree_map.h:57
@ Grass
Normal grass.
Definition tree_map.h:53
@ Rough
Rough land.
Definition tree_map.h:54
static const uint TREE_COUNT_SUB_ARCTIC
number of tree types on a sub arctic map.
Definition tree_map.h:42
TreeType GetTreeType(Tile t)
Returns the treetype of a tile.
Definition tree_map.h:87
void SetTreeGroundDensity(Tile t, TreeGround g, uint d)
Set the density and ground type of a tile with trees.
Definition tree_map.h:144
void AddTreeGrowth(Tile t, int a)
Add a value to the tree growth stage.
Definition tree_map.h:210
uint GetTreeDensity(Tile t)
Returns the 'density' of a tile with trees.
Definition tree_map.h:127
TreeGrowthStage
Enumeration for tree growth stages.
Definition tree_map.h:65
@ Grown
Fully grown tree.
Definition tree_map.h:69
@ Dead
Dead tree.
Definition tree_map.h:72
@ Dying1
First stage of dying.
Definition tree_map.h:70
@ Growing1
First stage of growth.
Definition tree_map.h:66
@ Growing2
Second stage of growth.
Definition tree_map.h:67
@ Dying2
Second stage of dying.
Definition tree_map.h:71
@ Growing3
Third stage of growth.
Definition tree_map.h:68
static const uint TREE_COUNT_TEMPERATE
number of tree types on a temperate map.
Definition tree_map.h:41
Map accessors for water tiles.
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:126
@ Invalid
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:43
@ Sea
Sea.
Definition water_map.h:40