OpenTTD Source 20241224-master-gee860a5c8e
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 (ClearGround)GB(t.m5(), 2, 3);
51}
52
60{
61 if (IsSnowTile(t)) return CLEAR_SNOW;
62 return GetRawClearGround(t);
63}
64
71inline bool IsClearGround(Tile t, ClearGround ct)
72{
73 return GetClearGround(t) == ct;
74}
75
76
83inline uint GetClearDensity(Tile t)
84{
85 assert(IsTileType(t, MP_CLEAR));
86 return GB(t.m5(), 0, 2);
87}
88
95inline void AddClearDensity(Tile t, int d)
96{
97 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
98 t.m5() += d;
99}
100
107inline void SetClearDensity(Tile t, uint d)
108{
109 assert(IsTileType(t, MP_CLEAR));
110 SB(t.m5(), 0, 2, d);
111}
112
113
120inline uint GetClearCounter(Tile t)
121{
122 assert(IsTileType(t, MP_CLEAR));
123 return GB(t.m5(), 5, 3);
124}
125
132inline void AddClearCounter(Tile t, int c)
133{
134 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
135 t.m5() += c << 5;
136}
137
144inline void SetClearCounter(Tile t, uint c)
145{
146 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
147 SB(t.m5(), 5, 3, c);
148}
149
150
158inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
159{
160 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
161 t.m5() = 0 << 5 | type << 2 | density;
162}
163
164
171inline uint GetFieldType(Tile t)
172{
173 assert(GetClearGround(t) == CLEAR_FIELDS);
174 return GB(t.m3(), 0, 4);
175}
176
183inline void SetFieldType(Tile t, uint f)
184{
185 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
186 SB(t.m3(), 0, 4, f);
187}
188
195inline IndustryID GetIndustryIndexOfField(Tile t)
196{
197 assert(GetClearGround(t) == CLEAR_FIELDS);
198 return(IndustryID) t.m2();
199}
200
207inline void SetIndustryIndexOfField(Tile t, IndustryID i)
208{
209 assert(GetClearGround(t) == CLEAR_FIELDS);
210 t.m2() = i;
211}
212
213
221inline uint GetFence(Tile t, DiagDirection side)
222{
223 assert(IsClearGround(t, CLEAR_FIELDS));
224 switch (side) {
225 default: NOT_REACHED();
226 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
227 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
228 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
229 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
230 }
231}
232
240inline void SetFence(Tile t, DiagDirection side, uint h)
241{
242 assert(IsClearGround(t, CLEAR_FIELDS));
243 switch (side) {
244 default: NOT_REACHED();
245 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
246 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
247 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
248 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
249 }
250}
251
252
259inline void MakeClear(Tile t, ClearGround g, uint density)
260{
262 t.m1() = 0;
264 t.m2() = 0;
265 t.m3() = 0;
266 t.m4() = 0 << 5 | 0 << 2;
267 SetClearGroundDensity(t, g, density); // Sets m5
268 t.m6() = 0;
269 t.m7() = 0;
270 t.m8() = 0;
271}
272
273
280inline void MakeField(Tile t, uint field_type, IndustryID industry)
281{
283 t.m1() = 0;
285 t.m2() = industry;
286 t.m3() = field_type;
287 t.m4() = 0 << 5 | 0 << 2;
289 SB(t.m6(), 2, 4, 0);
290 t.m7() = 0;
291 t.m8() = 0;
292}
293
300inline void MakeSnow(Tile t, uint density = 0)
301{
302 assert(GetClearGround(t) != CLEAR_SNOW);
303 SetBit(t.m3(), 4);
306 } else {
307 SetClearDensity(t, density);
308 }
309}
310
316inline void ClearSnow(Tile t)
317{
318 assert(GetClearGround(t) == CLEAR_SNOW);
319 ClrBit(t.m3(), 4);
320 SetClearDensity(t, 3);
321}
322
323#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:183
ClearGround GetRawClearGround(Tile t)
Get the type of clear tile but never return CLEAR_SNOW.
Definition clear_map.h:47
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:132
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:171
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:95
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:107
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:316
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:71
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
Definition clear_map.h:24
@ CLEAR_ROUGH
3
Definition clear_map.h:21
@ CLEAR_ROCKS
3
Definition clear_map.h:22
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:300
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:240
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:195
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:259
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:59
void SetIndustryIndexOfField(Tile t, IndustryID i)
Set the industry (farm) that made the field.
Definition clear_map.h:207
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:280
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:144
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:158
uint GetFence(Tile t, DiagDirection side)
Is there a fence at the given border?
Definition clear_map.h:221
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:120
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:83
@ 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.
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