OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef CLEAR_MAP_H
11#define CLEAR_MAP_H
12
13#include "bridge_map.h"
14#include "industry_type.h"
15
27
28
35inline bool IsSnowTile(Tile t)
36{
37 assert(IsTileType(t, MP_CLEAR));
38 return HasBit(t.m3(), 4);
39}
40
48{
49 assert(IsTileType(t, MP_CLEAR));
50 return static_cast<ClearGround>(GB(t.m5(), 2, 3));
51}
52
59inline bool IsClearGround(Tile t, ClearGround ct)
60{
61 return GetClearGround(t) == ct;
62}
63
64
71inline uint GetClearDensity(Tile t)
72{
73 assert(IsTileType(t, MP_CLEAR));
74 return GB(t.m5(), 0, 2);
75}
76
83inline void AddClearDensity(Tile t, int d)
84{
85 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
86 t.m5() += d;
87}
88
95inline void SetClearDensity(Tile t, uint d)
96{
97 assert(IsTileType(t, MP_CLEAR));
98 SB(t.m5(), 0, 2, d);
99}
100
101
108inline uint GetClearCounter(Tile t)
109{
110 assert(IsTileType(t, MP_CLEAR));
111 return GB(t.m5(), 5, 3);
112}
113
120inline void AddClearCounter(Tile t, int c)
121{
122 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
123 t.m5() += c << 5;
124}
125
132inline void SetClearCounter(Tile t, uint c)
133{
134 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
135 SB(t.m5(), 5, 3, c);
136}
137
138
146inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
147{
148 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
149 t.m5() = 0 << 5 | type << 2 | density;
150}
151
152
159inline uint GetFieldType(Tile t)
160{
161 assert(GetClearGround(t) == CLEAR_FIELDS);
162 return GB(t.m3(), 0, 4);
163}
164
171inline void SetFieldType(Tile t, uint f)
172{
173 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
174 SB(t.m3(), 0, 4, f);
175}
176
184{
185 assert(GetClearGround(t) == CLEAR_FIELDS);
186 return(IndustryID) t.m2();
187}
188
196{
197 assert(GetClearGround(t) == CLEAR_FIELDS);
198 t.m2() = i.base();
199}
200
201
209inline uint GetFence(Tile t, DiagDirection side)
210{
211 assert(IsClearGround(t, CLEAR_FIELDS));
212 switch (side) {
213 default: NOT_REACHED();
214 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
215 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
216 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
217 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
218 }
219}
220
228inline void SetFence(Tile t, DiagDirection side, uint h)
229{
230 assert(IsClearGround(t, CLEAR_FIELDS));
231 switch (side) {
232 default: NOT_REACHED();
233 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
234 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
235 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
236 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
237 }
238}
239
240
247inline void MakeClear(Tile t, ClearGround g, uint density)
248{
250 t.m1() = 0;
252 t.m2() = 0;
253 t.m3() = 0;
254 t.m4() = 0 << 5 | 0 << 2;
255 SetClearGroundDensity(t, g, density); // Sets m5
256 t.m6() = 0;
257 t.m7() = 0;
258 t.m8() = 0;
259}
260
261
268inline void MakeField(Tile t, uint field_type, IndustryID industry)
269{
271 t.m1() = 0;
273 t.m2() = industry.base();
274 t.m3() = field_type;
275 t.m4() = 0 << 5 | 0 << 2;
277 SB(t.m6(), 2, 4, 0);
278 t.m7() = 0;
279 t.m8() = 0;
280}
281
288inline void MakeSnow(Tile t, uint density = 0)
289{
290 assert(!IsSnowTile(t));
291 SetBit(t.m3(), 4);
292 if (GetClearGround(t) == CLEAR_FIELDS) {
294 } else {
295 SetClearDensity(t, density);
296 }
297}
298
304inline void ClearSnow(Tile t)
305{
306 assert(IsSnowTile(t));
307 ClrBit(t.m3(), 4);
308 SetClearDensity(t, 3);
309}
310
311#endif /* CLEAR_MAP_H */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline 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 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
debug_inline uint16_t & m8()
General purpose.
Definition map_func.h:197
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
debug_inline uint8_t & m4()
General purpose.
Definition map_func.h:149
debug_inline uint8_t & m6()
General purpose.
Definition map_func.h:173
debug_inline uint8_t & m3()
General purpose.
Definition map_func.h:137
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
debug_inline uint8_t & m5()
General purpose.
Definition map_func.h:161
void SetFieldType(Tile t, uint f)
Set the field type (production stage) of the field.
Definition clear_map.h:171
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:120
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:159
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:83
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:95
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:304
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:59
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:288
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:228
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:183
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:25
@ CLEAR_SNOW
0-3 (Not stored in map.)
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:247
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:47
void SetIndustryIndexOfField(Tile t, IndustryID i)
Set the industry (farm) that made the field.
Definition clear_map.h:195
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:268
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:132
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:146
uint GetFence(Tile t, DiagDirection side)
Is there a fence at the given border?
Definition clear_map.h:209
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:108
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:35
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:71
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:43
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
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48