OpenTTD Source 20260129-master-g2bb01bd0e4
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
10#ifndef CLEAR_MAP_H
11#define CLEAR_MAP_H
12
13#include "bridge_map.h"
14#include "industry_type.h"
15
26
27
34inline bool IsSnowTile(Tile t)
35{
36 assert(IsTileType(t, TileType::Clear));
37 return HasBit(t.m3(), 4);
38}
39
47{
48 assert(IsTileType(t, TileType::Clear));
49 return static_cast<ClearGround>(GB(t.m5(), 2, 3));
50}
51
58inline bool IsClearGround(Tile t, ClearGround ct)
59{
60 return GetClearGround(t) == ct;
61}
62
63
70inline uint GetClearDensity(Tile t)
71{
72 assert(IsTileType(t, TileType::Clear));
73 return GB(t.m5(), 0, 2);
74}
75
82inline void AddClearDensity(Tile t, int d)
83{
84 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
85 t.m5() += d;
86}
87
94inline void SetClearDensity(Tile t, uint d)
95{
96 assert(IsTileType(t, TileType::Clear));
97 SB(t.m5(), 0, 2, d);
98}
99
100
107inline uint GetClearCounter(Tile t)
108{
109 assert(IsTileType(t, TileType::Clear));
110 return GB(t.m5(), 5, 3);
111}
112
119inline void AddClearCounter(Tile t, int c)
120{
121 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
122 t.m5() += c << 5;
123}
124
131inline void SetClearCounter(Tile t, uint c)
132{
133 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
134 SB(t.m5(), 5, 3, c);
135}
136
137
145inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
146{
147 assert(IsTileType(t, TileType::Clear)); // XXX incomplete
148 t.m5() = 0 << 5 | type << 2 | density;
149}
150
151
158inline uint GetFieldType(Tile t)
159{
160 assert(GetClearGround(t) == CLEAR_FIELDS);
161 return GB(t.m3(), 0, 4);
162}
163
170inline void SetFieldType(Tile t, uint f)
171{
172 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
173 SB(t.m3(), 0, 4, f);
174}
175
183{
184 assert(GetClearGround(t) == CLEAR_FIELDS);
185 return(IndustryID) t.m2();
186}
187
195{
196 assert(GetClearGround(t) == CLEAR_FIELDS);
197 t.m2() = i.base();
198}
199
200
208inline uint GetFence(Tile t, DiagDirection side)
209{
210 assert(IsClearGround(t, CLEAR_FIELDS));
211 switch (side) {
212 default: NOT_REACHED();
213 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
214 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
215 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
216 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
217 }
218}
219
227inline void SetFence(Tile t, DiagDirection side, uint h)
228{
229 assert(IsClearGround(t, CLEAR_FIELDS));
230 switch (side) {
231 default: NOT_REACHED();
232 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
233 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
234 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
235 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
236 }
237}
238
239
246inline void MakeClear(Tile t, ClearGround g, uint density)
247{
249 t.m1() = 0;
251 t.m2() = 0;
252 t.m3() = 0;
253 t.m4() = 0 << 5 | 0 << 2;
254 SetClearGroundDensity(t, g, density); // Sets m5
255 t.m6() = 0;
256 t.m7() = 0;
257 t.m8() = 0;
258}
259
260
267inline void MakeField(Tile t, uint field_type, IndustryID industry)
268{
270 t.m1() = 0;
272 t.m2() = industry.base();
273 t.m3() = field_type;
274 t.m4() = 0 << 5 | 0 << 2;
276 SB(t.m6(), 2, 6, 0);
277 t.m7() = 0;
278 t.m8() = 0;
279}
280
287inline void MakeSnow(Tile t, uint density = 0)
288{
289 assert(!IsSnowTile(t));
290 SetBit(t.m3(), 4);
291 if (GetClearGround(t) == CLEAR_FIELDS) {
293 } else {
294 SetClearDensity(t, density);
295 }
296}
297
303inline void ClearSnow(Tile t)
304{
305 assert(IsSnowTile(t));
306 ClrBit(t.m3(), 4);
307 SetClearDensity(t, 3);
308}
309
310#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:161
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
uint8_t & m4()
General purpose.
Definition map_func.h:149
uint8_t & m6()
General purpose.
Definition map_func.h:173
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
uint8_t & m3()
General purpose.
Definition map_func.h:137
uint16_t & m8()
General purpose.
Definition map_func.h:197
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
void SetFieldType(Tile t, uint f)
Set the field type (production stage) of the field.
Definition clear_map.h:170
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:119
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:158
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:82
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:94
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:303
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:58
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:287
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:227
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:182
ClearGround
Ground types.
Definition clear_map.h:19
@ CLEAR_GRASS
0-3
Definition clear_map.h:20
@ CLEAR_FIELDS
3
Definition clear_map.h:23
@ CLEAR_DESERT
1,3
Definition clear_map.h:24
@ CLEAR_ROUGH
3
Definition clear_map.h:21
@ CLEAR_ROCKS
3
Definition clear_map.h:22
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:246
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:46
void SetIndustryIndexOfField(Tile t, IndustryID i)
Set the industry (farm) that made the field.
Definition clear_map.h:194
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:267
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:131
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:145
uint GetFence(Tile t, DiagDirection side)
Is there a fence at the given border?
Definition clear_map.h:208
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:107
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:34
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:70
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.
Types related to the industry.
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:47
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.