OpenTTD Source 20260218-master-g2123fca5ea
clear_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 CLEAR_MAP_H
11#define CLEAR_MAP_H
12
13#include "bridge_map.h"
14#include "industry_type.h"
15
16static constexpr size_t CLEAR_GROUND_BITS = 3;
17
21enum class ClearGround : uint8_t {
22 Grass = 0,
23 Rough = 1,
24 Rocks = 2,
25 Fields = 3,
26 Desert = 5,
27
30};
31
33
40inline bool IsSnowTile(Tile t)
41{
42 assert(IsTileType(t, TileType::Clear));
43 return HasBit(t.m3(), 4);
44}
45
53{
54 assert(IsTileType(t, TileType::Clear));
55 return static_cast<ClearGround>(GB(t.m5(), 2, CLEAR_GROUND_BITS));
56}
57
65inline bool IsClearGround(Tile t, ClearGround ct)
66{
67 return GetClearGround(t) == ct;
68}
69
70
77inline uint GetClearDensity(Tile t)
78{
79 assert(IsTileType(t, TileType::Clear));
80 return GB(t.m5(), 0, 2);
81}
82
89inline void AddClearDensity(Tile t, int d)
90{
91 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
92 t.m5() += d;
93}
94
101inline void SetClearDensity(Tile t, uint d)
102{
103 assert(IsTileType(t, TileType::Clear));
104 SB(t.m5(), 0, 2, d);
105}
106
107
114inline uint GetClearCounter(Tile t)
115{
116 assert(IsTileType(t, TileType::Clear));
117 return GB(t.m5(), 5, 3);
118}
119
126inline void AddClearCounter(Tile t, int c)
127{
128 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
129 t.m5() += c << 5;
130}
131
138inline void SetClearCounter(Tile t, uint c)
139{
140 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
141 SB(t.m5(), 5, 3, c);
142}
143
144
152inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
153{
154 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
155 t.m5() = 0 << 5 | to_underlying(type) << 2 | density;
156}
157
158
165inline uint GetFieldType(Tile t)
166{
168 return GB(t.m3(), 0, 4);
169}
170
177inline void SetFieldType(Tile t, uint f)
178{
179 assert(GetClearGround(t) == ClearGround::Fields); // XXX incomplete
180 SB(t.m3(), 0, 4, f);
181}
182
189inline IndustryID GetIndustryIndexOfField(Tile t)
190{
192 return(IndustryID) t.m2();
193}
194
201inline void SetIndustryIndexOfField(Tile t, IndustryID i)
202{
204 t.m2() = i.base();
205}
206
207
215inline uint GetFence(Tile t, DiagDirection side)
216{
218 switch (side) {
219 default: NOT_REACHED();
220 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
221 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
222 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
223 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
224 }
225}
226
234inline void SetFence(Tile t, DiagDirection side, uint h)
235{
237 switch (side) {
238 default: NOT_REACHED();
239 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
240 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
241 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
242 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
243 }
244}
245
246
253inline void MakeClear(Tile t, ClearGround g, uint density)
254{
256 t.m1() = 0;
258 t.m2() = 0;
259 t.m3() = 0;
260 t.m4() = 0 << 5 | 0 << 2;
261 SetClearGroundDensity(t, g, density); // Sets m5
262 t.m6() = 0;
263 t.m7() = 0;
264 t.m8() = 0;
265}
266
267
274inline void MakeField(Tile t, uint field_type, IndustryID industry)
275{
277 t.m1() = 0;
279 t.m2() = industry.base();
280 t.m3() = field_type;
281 t.m4() = 0 << 5 | 0 << 2;
283 SB(t.m6(), 2, 6, 0);
284 t.m7() = 0;
285 t.m8() = 0;
286}
287
294inline void MakeSnow(Tile t, uint density = 0)
295{
296 assert(!IsSnowTile(t));
297 SetBit(t.m3(), 4);
300 } else {
301 SetClearDensity(t, density);
302 }
303}
304
310inline void ClearSnow(Tile t)
311{
312 assert(IsSnowTile(t));
313 ClrBit(t.m3(), 4);
314 SetClearDensity(t, 3);
315}
316
317#endif /* CLEAR_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.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Map accessor functions for bridges.
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:156
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:112
uint8_t & m4()
General purpose.
Definition map_func.h:145
uint8_t & m6()
General purpose.
Definition map_func.h:167
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:178
uint8_t & m3()
General purpose.
Definition map_func.h:134
uint16_t & m8()
General purpose.
Definition map_func.h:189
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:123
void SetFieldType(Tile t, uint f)
Set the field type (production stage) of the field.
Definition clear_map.h:177
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:126
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:165
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:89
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:101
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:310
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:65
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:294
void SetFence(Tile t, DiagDirection side, uint h)
Sets the type of fence (and whether there is one) for the given border.
Definition clear_map.h:234
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:189
ClearGround
Ground types.
Definition clear_map.h:21
@ Desert
Desert with transition (1,3).
Definition clear_map.h:26
@ MaxSize
The maximum possible number of clear ground types to be stored in map.
Definition clear_map.h:29
@ Rocks
Rocks with snow transition (0-3).
Definition clear_map.h:24
@ End
End marker.
Definition clear_map.h:28
@ Fields
Farm fields (3).
Definition clear_map.h:25
@ Grass
Plain grass with dirt transition (0-3).
Definition clear_map.h:22
@ Rough
Rough mounds (3).
Definition clear_map.h:23
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:253
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:52
void SetIndustryIndexOfField(Tile t, IndustryID i)
Set the industry (farm) that made the field.
Definition clear_map.h:201
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:274
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:138
void SetClearGroundDensity(Tile t, ClearGround type, uint density)
Sets ground type and density in one go, also sets the counter to 0.
Definition clear_map.h:152
static constexpr size_t CLEAR_GROUND_BITS
How many bits in map array are dedicated for clear ground type.
Definition clear_map.h:16
uint GetFence(Tile t, DiagDirection side)
Is there a fence at the given border?
Definition clear_map.h:215
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:114
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:40
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:77
static constexpr Owner OWNER_NONE
The tile has no ownership.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_NW
Northwest.
@ DIAGDIR_SE
Southeast.
@ DIAGDIR_SW
Southwest.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
Types related to the industry.
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
@ Clear
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:49