OpenTTD Source: src/clear_map.h Source File

OpenTTD Source 15.3

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
19 enum ClearGround : uint8_t {
20 CLEAR_GRASS = 0,
21 CLEAR_ROUGH = 1,
22 CLEAR_ROCKS = 2,
23 CLEAR_FIELDS = 3,
24 CLEAR_SNOW = 4,
25 CLEAR_DESERT = 5,
26};
27
28
35 inline bool IsSnowTile(Tile t)
36{
37 assert(IsTileType(t, MP_CLEAR));
38 return HasBit(t.m3(), 4);
39}
40
47 inline ClearGround GetClearGround(Tile t)
48{
49 assert(IsTileType(t, MP_CLEAR));
50 return static_cast<ClearGround>(GB(t.m5(), 2, 3));
51}
52
59 inline bool IsClearGround(Tile t, ClearGround ct)
60{
61 return GetClearGround(t) == ct;
62}
63
64
71 inline uint GetClearDensity(Tile t)
72{
73 assert(IsTileType(t, MP_CLEAR));
74 return GB(t.m5(), 0, 2);
75}
76
83 inline void AddClearDensity(Tile t, int d)
84{
85 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
86 t.m5() += d;
87}
88
95 inline void SetClearDensity(Tile t, uint d)
96{
97 assert(IsTileType(t, MP_CLEAR));
98 SB(t.m5(), 0, 2, d);
99}
100
101
108 inline uint GetClearCounter(Tile t)
109{
110 assert(IsTileType(t, MP_CLEAR));
111 return GB(t.m5(), 5, 3);
112}
113
120 inline void AddClearCounter(Tile t, int c)
121{
122 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
123 t.m5() += c << 5;
124}
125
132 inline 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
146 inline 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
159 inline uint GetFieldType(Tile t)
160{
161 assert(GetClearGround(t) == CLEAR_FIELDS);
162 return GB(t.m3(), 0, 4);
163}
164
171 inline void SetFieldType(Tile t, uint f)
172{
173 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
174 SB(t.m3(), 0, 4, f);
175}
176
183 inline IndustryID GetIndustryIndexOfField(Tile t)
184{
185 assert(GetClearGround(t) == CLEAR_FIELDS);
186 return(IndustryID) t.m2();
187}
188
195 inline void SetIndustryIndexOfField(Tile t, IndustryID i)
196{
197 assert(GetClearGround(t) == CLEAR_FIELDS);
198 t.m2() = i.base();
199}
200
201
209 inline 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
228 inline 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
247 inline void MakeClear(Tile t, ClearGround g, uint density)
248{
249 SetTileType(t, MP_CLEAR);
250 t.m1() = 0;
251 SetTileOwner(t, OWNER_NONE);
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
268 inline void MakeField(Tile t, uint field_type, IndustryID industry)
269{
270 SetTileType(t, MP_CLEAR);
271 t.m1() = 0;
272 SetTileOwner(t, OWNER_NONE);
273 t.m2() = industry.base();
274 t.m3() = field_type;
275 t.m4() = 0 << 5 | 0 << 2;
276 SetClearGroundDensity(t, CLEAR_FIELDS, 3);
277 SB(t.m6(), 2, 6, 0);
278 t.m7() = 0;
279 t.m8() = 0;
280}
281
288 inline void MakeSnow(Tile t, uint density = 0)
289{
290 assert(!IsSnowTile(t));
291 SetBit(t.m3(), 4);
292 if (GetClearGround(t) == CLEAR_FIELDS) {
293 SetClearGroundDensity(t, CLEAR_GRASS, density);
294 } else {
295 SetClearDensity(t, density);
296 }
297}
298
304 inline 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 */
SB
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.
Definition bitmath_func.hpp:58
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition bitmath_func.hpp:139
bridge_map.h
Map accessor functions for bridges.
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
Tile::m5
uint8_t & m5()
General purpose.
Definition map_func.h:161
Tile::m1
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
Tile::m4
uint8_t & m4()
General purpose.
Definition map_func.h:149
Tile::m6
uint8_t & m6()
General purpose.
Definition map_func.h:173
Tile::m7
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
Tile::m3
uint8_t & m3()
General purpose.
Definition map_func.h:137
Tile::m8
uint16_t & m8()
General purpose.
Definition map_func.h:197
Tile::m2
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
SetFieldType
void SetFieldType(Tile t, uint f)
Set the field type (production stage) of the field.
Definition clear_map.h:171
AddClearCounter
void AddClearCounter(Tile t, int c)
Increments the counter used to advance to the next clear density/field type.
Definition clear_map.h:120
GetFieldType
uint GetFieldType(Tile t)
Get the field type (production stage) of the field.
Definition clear_map.h:159
AddClearDensity
void AddClearDensity(Tile t, int d)
Increment the density of a non-field clear tile.
Definition clear_map.h:83
SetClearDensity
void SetClearDensity(Tile t, uint d)
Set the density of a non-field clear tile.
Definition clear_map.h:95
ClearSnow
void ClearSnow(Tile t)
Clear the snow from a tile and return it to its previous type.
Definition clear_map.h:304
IsClearGround
bool IsClearGround(Tile t, ClearGround ct)
Set the type of clear tile.
Definition clear_map.h:59
MakeSnow
void MakeSnow(Tile t, uint density=0)
Make a snow tile.
Definition clear_map.h:288
SetFence
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
GetIndustryIndexOfField
IndustryID GetIndustryIndexOfField(Tile t)
Get the industry (farm) that made the field.
Definition clear_map.h:183
ClearGround
ClearGround
Ground types.
Definition clear_map.h:19
CLEAR_GRASS
@ CLEAR_GRASS
0-3
Definition clear_map.h:20
CLEAR_FIELDS
@ CLEAR_FIELDS
3
Definition clear_map.h:23
CLEAR_DESERT
@ CLEAR_DESERT
1,3
Definition clear_map.h:25
CLEAR_SNOW
@ CLEAR_SNOW
0-3 (Not stored in map.)
Definition clear_map.h:24
CLEAR_ROUGH
@ CLEAR_ROUGH
3
Definition clear_map.h:21
CLEAR_ROCKS
@ CLEAR_ROCKS
3
Definition clear_map.h:22
MakeClear
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:247
GetClearGround
ClearGround GetClearGround(Tile t)
Get the type of clear tile.
Definition clear_map.h:47
SetIndustryIndexOfField
void SetIndustryIndexOfField(Tile t, IndustryID i)
Set the industry (farm) that made the field.
Definition clear_map.h:195
MakeField
void MakeField(Tile t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition clear_map.h:268
SetClearCounter
void SetClearCounter(Tile t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition clear_map.h:132
SetClearGroundDensity
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
GetFence
uint GetFence(Tile t, DiagDirection side)
Is there a fence at the given border?
Definition clear_map.h:209
GetClearCounter
uint GetClearCounter(Tile t)
Get the counter used to advance to the next clear density/field type.
Definition clear_map.h:108
IsSnowTile
bool IsSnowTile(Tile t)
Test if a tile is covered with snow.
Definition clear_map.h:35
GetClearDensity
uint GetClearDensity(Tile t)
Get the density of a non-field clear tile.
Definition clear_map.h:71
OWNER_NONE
static constexpr Owner OWNER_NONE
The tile has no ownership.
Definition company_type.h:26
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition direction_type.h:80
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition direction_type.h:83
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition direction_type.h:81
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition direction_type.h:82
industry_type.h
Types related to the industry.
PoolID
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48