OpenTTD Source 20260206-master-g4d4e37dbf1
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
64inline bool IsClearGround(Tile t, ClearGround ct)
65{
66 return GetClearGround(t) == ct;
67}
68
69
76inline uint GetClearDensity(Tile t)
77{
78 assert(IsTileType(t, TileType::Clear));
79 return GB(t.m5(), 0, 2);
80}
81
88inline void AddClearDensity(Tile t, int d)
89{
90 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
91 t.m5() += d;
92}
93
100inline void SetClearDensity(Tile t, uint d)
101{
102 assert(IsTileType(t, TileType::Clear));
103 SB(t.m5(), 0, 2, d);
104}
105
106
113inline uint GetClearCounter(Tile t)
114{
115 assert(IsTileType(t, TileType::Clear));
116 return GB(t.m5(), 5, 3);
117}
118
125inline void AddClearCounter(Tile t, int c)
126{
127 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
128 t.m5() += c << 5;
129}
130
137inline void SetClearCounter(Tile t, uint c)
138{
139 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
140 SB(t.m5(), 5, 3, c);
141}
142
143
151inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
152{
153 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
154 t.m5() = 0 << 5 | to_underlying(type) << 2 | density;
155}
156
157
164inline uint GetFieldType(Tile t)
165{
167 return GB(t.m3(), 0, 4);
168}
169
176inline void SetFieldType(Tile t, uint f)
177{
178 assert(GetClearGround(t) == ClearGround::Fields); // XXX incomplete
179 SB(t.m3(), 0, 4, f);
180}
181
188inline IndustryID GetIndustryIndexOfField(Tile t)
189{
191 return(IndustryID) t.m2();
192}
193
200inline void SetIndustryIndexOfField(Tile t, IndustryID i)
201{
203 t.m2() = i.base();
204}
205
206
214inline uint GetFence(Tile t, DiagDirection side)
215{
217 switch (side) {
218 default: NOT_REACHED();
219 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
220 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
221 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
222 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
223 }
224}
225
233inline void SetFence(Tile t, DiagDirection side, uint h)
234{
236 switch (side) {
237 default: NOT_REACHED();
238 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
239 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
240 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
241 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
242 }
243}
244
245
252inline void MakeClear(Tile t, ClearGround g, uint density)
253{
255 t.m1() = 0;
257 t.m2() = 0;
258 t.m3() = 0;
259 t.m4() = 0 << 5 | 0 << 2;
260 SetClearGroundDensity(t, g, density); // Sets m5
261 t.m6() = 0;
262 t.m7() = 0;
263 t.m8() = 0;
264}
265
266
273inline void MakeField(Tile t, uint field_type, IndustryID industry)
274{
276 t.m1() = 0;
278 t.m2() = industry.base();
279 t.m3() = field_type;
280 t.m4() = 0 << 5 | 0 << 2;
282 SB(t.m6(), 2, 6, 0);
283 t.m7() = 0;
284 t.m8() = 0;
285}
286
293inline void MakeSnow(Tile t, uint density = 0)
294{
295 assert(!IsSnowTile(t));
296 SetBit(t.m3(), 4);
299 } else {
300 SetClearDensity(t, density);
301 }
302}
303
309inline void ClearSnow(Tile t)
310{
311 assert(IsSnowTile(t));
312 ClrBit(t.m3(), 4);
313 SetClearDensity(t, 3);
314}
315
316#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:154
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:110
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
void SetFieldType(Tile t, uint f)
Set the field type (production stage) of the field.
Definition clear_map.h:176
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:125
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:164
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:88
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:100
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:309
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:64
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:293
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:233
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:188
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:252
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:200
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:273
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:137
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:151
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:214
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:113
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:76
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:17
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