OpenTTD Source  20241120-master-g6d3adc6169
water_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 WATER_MAP_H
11 #define WATER_MAP_H
12 
13 #include "depot_type.h"
14 #include "tile_map.h"
15 
19 static constexpr uint8_t WBL_TYPE_BEGIN = 4;
20 static constexpr uint8_t WBL_TYPE_COUNT = 4;
21 
22 static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN = 0;
23 static constexpr uint8_t WBL_LOCK_ORIENT_COUNT = 2;
24 static constexpr uint8_t WBL_LOCK_PART_BEGIN = 2;
25 static constexpr uint8_t WBL_LOCK_PART_COUNT = 2;
26 
27 static constexpr uint8_t WBL_DEPOT_PART = 0;
28 static constexpr uint8_t WBL_DEPOT_AXIS = 1;
29 
36 };
37 
39 enum WaterClass : uint8_t {
44 };
45 
53 {
54  return wc < WATER_CLASS_INVALID;
55 }
56 
58 enum DepotPart {
61  DEPOT_PART_END
62 };
63 
65 enum LockPart {
69 };
70 
71 bool IsPossibleDockingTile(Tile t);
72 
79 {
80  assert(IsTileType(t, MP_WATER));
81  return static_cast<WaterTileType>(GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT));
82 }
83 
89 inline void SetWaterTileType(Tile t, WaterTileType type)
90 {
91  assert(IsTileType(t, MP_WATER));
93 }
94 
101 inline bool HasTileWaterClass(Tile t)
102 {
104 }
105 
113 {
114  assert(HasTileWaterClass(t));
115  return (WaterClass)GB(t.m1(), 5, 2);
116 }
117 
124 inline void SetWaterClass(Tile t, WaterClass wc)
125 {
126  assert(HasTileWaterClass(t));
127  SB(t.m1(), 5, 2, wc);
128 }
129 
136 inline bool IsTileOnWater(Tile t)
137 {
138  return (GetWaterClass(t) != WATER_CLASS_INVALID);
139 }
140 
147 inline bool IsWater(Tile t)
148 {
149  return GetWaterTileType(t) == WATER_TILE_CLEAR;
150 }
151 
158 inline bool IsSea(Tile t)
159 {
160  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
161 }
162 
169 inline bool IsCanal(Tile t)
170 {
171  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
172 }
173 
180 inline bool IsRiver(Tile t)
181 {
182  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
183 }
184 
190 inline bool IsWaterTile(Tile t)
191 {
192  return IsTileType(t, MP_WATER) && IsWater(t);
193 }
194 
201 inline bool IsCoast(Tile t)
202 {
203  return GetWaterTileType(t) == WATER_TILE_COAST;
204 }
205 
211 inline bool IsCoastTile(Tile t)
212 {
213  return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
214 }
215 
222 inline bool IsShipDepot(Tile t)
223 {
224  return GetWaterTileType(t) == WATER_TILE_DEPOT;
225 }
226 
232 inline bool IsShipDepotTile(Tile t)
233 {
234  return IsTileType(t, MP_WATER) && IsShipDepot(t);
235 }
236 
244 {
245  assert(IsShipDepotTile(t));
246  return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
247 }
248 
256 {
257  assert(IsShipDepotTile(t));
258  return (DepotPart)GB(t.m5(), WBL_DEPOT_PART, 1);
259 }
260 
268 {
270 }
271 
279 {
281 }
282 
290 {
291  assert(IsShipDepot(t));
292  TileIndex tile2 = GetOtherShipDepotTile(t);
293 
294  return t < tile2 ? TileIndex(t) : tile2;
295 }
296 
303 inline bool IsLock(Tile t)
304 {
305  return GetWaterTileType(t) == WATER_TILE_LOCK;
306 }
307 
315 {
316  assert(IsLock(t));
318 }
319 
326 inline uint8_t GetLockPart(Tile t)
327 {
328  assert(IsLock(t));
330 }
331 
338 inline uint8_t GetWaterTileRandomBits(Tile t)
339 {
340  assert(IsTileType(t, MP_WATER));
341  return t.m4();
342 }
343 
350 inline bool HasTileWaterGround(Tile t)
351 {
352  return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
353 }
354 
361 inline void SetDockingTile(Tile t, bool b)
362 {
364  AssignBit(t.m1(), 7, b);
365 }
366 
371 inline bool IsDockingTile(Tile t)
372 {
373  return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(t.m1(), 7);
374 }
375 
376 
381 inline void MakeShore(Tile t)
382 {
383  SetTileType(t, MP_WATER);
386  SetDockingTile(t, false);
387  t.m2() = 0;
388  t.m3() = 0;
389  t.m4() = 0;
390  t.m5() = 0;
392  SB(t.m6(), 2, 4, 0);
393  t.m7() = 0;
394 }
395 
403 inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
404 {
405  SetTileType(t, MP_WATER);
406  SetTileOwner(t, o);
407  SetWaterClass(t, wc);
408  SetDockingTile(t, false);
409  t.m2() = 0;
410  t.m3() = 0;
411  t.m4() = random_bits;
412  t.m5() = 0;
414  SB(t.m6(), 2, 4, 0);
415  t.m7() = 0;
416 }
417 
422 inline void MakeSea(Tile t)
423 {
425 }
426 
432 inline void MakeRiver(Tile t, uint8_t random_bits)
433 {
434  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
435 }
436 
443 inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
444 {
445  assert(o != OWNER_WATER);
446  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
447 }
448 
458 inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
459 {
460  SetTileType(t, MP_WATER);
461  SetTileOwner(t, o);
462  SetWaterClass(t, original_water_class);
463  SetDockingTile(t, false);
464  t.m2() = did;
465  t.m3() = 0;
466  t.m4() = 0;
467  t.m5() = part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
469  SB(t.m6(), 2, 4, 0);
470  t.m7() = 0;
471 }
472 
482 inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
483 {
484  SetTileType(t, MP_WATER);
485  SetTileOwner(t, o);
486  SetWaterClass(t, original_water_class);
487  SetDockingTile(t, false);
488  t.m2() = 0;
489  t.m3() = 0;
490  t.m4() = 0;
491  t.m5() = part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
493  SB(t.m6(), 2, 4, 0);
494  t.m7() = 0;
495 }
496 
506 inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
507 {
508  TileIndexDiff delta = TileOffsByDiagDir(d);
509  Tile lower_tile = TileIndex(t) - delta;
510  Tile upper_tile = TileIndex(t) + delta;
511 
512  /* Keep the current waterclass and owner for the tiles.
513  * It allows to restore them after the lock is deleted */
514  MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
515  MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LOCK_PART_LOWER, d, wc_lower);
516  MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LOCK_PART_UPPER, d, wc_upper);
517 }
518 
524 inline void SetNonFloodingWaterTile(Tile t, bool b)
525 {
526  assert(IsTileType(t, MP_WATER));
527  AssignBit(t.m3(), 0, b);
528 }
534 {
535  assert(IsTileType(t, MP_WATER));
536  return HasBit(t.m3(), 0);
537 }
538 
539 #endif /* WATER_MAP_H */
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
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 static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Wrapper class to abstract away the way the tiles are stored.
Definition: map_func.h:25
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition: map_func.h:185
debug_inline uint8_t & m5()
General purpose.
Definition: map_func.h:161
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition: map_func.h:113
debug_inline uint8_t & m4()
General purpose.
Definition: map_func.h:149
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition: map_func.h:125
debug_inline uint8_t & m6()
General purpose.
Definition: map_func.h:173
debug_inline uint8_t & m3()
General purpose.
Definition: map_func.h:137
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
Header files for depots (not hangars)
uint16_t DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:13
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition: enum_type.hpp:15
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition: map_func.h:552
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:567
int32_t TileIndexDiff
An offset value between two tiles.
Definition: map_type.h:23
Map writing/reading functions for tiles.
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition: tile_map.h:178
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
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition: tile_type.h:87
@ MP_TREES
Tile got trees.
Definition: tile_type.h:52
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
@ MP_WATER
Water tile.
Definition: tile_type.h:54
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
void MakeShore(Tile t)
Helper function to make a coast tile.
Definition: water_map.h:381
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:124
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition: water_map.h:350
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition: water_map.h:289
void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
Make a water lock.
Definition: water_map.h:506
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition: water_map.h:403
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition: water_map.h:255
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition: water_map.h:136
DepotPart
Sections of the water depot.
Definition: water_map.h:58
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition: water_map.h:59
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition: water_map.h:60
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:222
bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition: water_map.h:52
static constexpr uint8_t WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition: water_map.h:24
bool IsRiver(Tile t)
Is it a river water tile?
Definition: water_map.h:180
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition: water_map.h:314
static constexpr uint8_t WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition: water_map.h:20
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:39
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:40
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:41
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:43
@ WATER_CLASS_RIVER
River.
Definition: water_map.h:42
static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition: water_map.h:22
void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:482
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:101
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition: water_map.h:232
bool IsCanal(Tile t)
Is it a canal tile?
Definition: water_map.h:169
static constexpr uint8_t WBL_TYPE_BEGIN
Bit field layout of m5 for water tiles.
Definition: water_map.h:19
bool IsCoast(Tile t)
Is it a coast tile?
Definition: water_map.h:201
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition: water_map.h:432
WaterTileType GetWaterTileType(Tile t)
Get the water tile type of a tile.
Definition: water_map.h:78
void SetNonFloodingWaterTile(Tile t, bool b)
Set the non-flooding water tile state of a tile.
Definition: water_map.h:524
void SetWaterTileType(Tile t, WaterTileType type)
Set the water tile type of a tile.
Definition: water_map.h:89
WaterTileType
Available water tile types.
Definition: water_map.h:31
@ WATER_TILE_COAST
Coast.
Definition: water_map.h:33
@ WATER_TILE_LOCK
Water lock.
Definition: water_map.h:34
@ WATER_TILE_DEPOT
Water Depot.
Definition: water_map.h:35
@ WATER_TILE_CLEAR
Plain water.
Definition: water_map.h:32
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition: water_map.h:112
static constexpr uint8_t WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition: water_map.h:23
void MakeCanal(Tile t, Owner o, uint8_t random_bits)
Make a canal tile.
Definition: water_map.h:443
static constexpr uint8_t WBL_DEPOT_AXIS
Depot axis flag.
Definition: water_map.h:28
bool IsCoastTile(Tile t)
Is it a coast tile.
Definition: water_map.h:211
uint8_t GetLockPart(Tile t)
Get the part of a lock.
Definition: water_map.h:326
static constexpr uint8_t WBL_DEPOT_PART
Depot part flag.
Definition: water_map.h:27
TileIndex GetOtherShipDepotTile(Tile t)
Get the other tile of the ship depot.
Definition: water_map.h:278
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:371
bool IsNonFloodingWaterTile(Tile t)
Checks whether the tile is marked as a non-flooding water tile.
Definition: water_map.h:533
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:361
bool IsWater(Tile t)
Is it a plain water tile?
Definition: water_map.h:147
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition: water_map.h:190
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition: water_map.h:303
void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
Make a ship depot section.
Definition: water_map.h:458
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition: water_map.h:338
void MakeSea(Tile t)
Make a sea tile.
Definition: water_map.h:422
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition: water_map.h:267
bool IsSea(Tile t)
Is it a sea water tile?
Definition: water_map.h:158
LockPart
Sections of the water lock.
Definition: water_map.h:65
@ LOCK_PART_UPPER
Upper part of a lock.
Definition: water_map.h:68
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:66
@ LOCK_PART_LOWER
Lower part of a lock.
Definition: water_map.h:67
static constexpr uint8_t WBL_LOCK_PART_COUNT
Length of lock part bitfield.
Definition: water_map.h:25
Axis GetShipDepotAxis(Tile t)
Get the axis of the ship depot.
Definition: water_map.h:243