OpenTTD Source  20240917-master-g9ab0a47812
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 
22 
24  WBL_TYPE_LOCK = 0x1,
26 
28 
33 
36 };
37 
44 };
45 
47 enum WaterClass : uint8_t {
52 };
53 
61 {
62  return wc < WATER_CLASS_INVALID;
63 }
64 
66 enum DepotPart {
69  DEPOT_PART_END
70 };
71 
73 enum LockPart {
77 };
78 
79 bool IsPossibleDockingTile(Tile t);
80 
87 {
88  assert(IsTileType(t, MP_WATER));
89 
90  switch (GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
92  case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
93  case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
94  default: NOT_REACHED();
95  }
96 }
97 
104 inline bool HasTileWaterClass(Tile t)
105 {
107 }
108 
116 {
117  assert(HasTileWaterClass(t));
118  return (WaterClass)GB(t.m1(), 5, 2);
119 }
120 
127 inline void SetWaterClass(Tile t, WaterClass wc)
128 {
129  assert(HasTileWaterClass(t));
130  SB(t.m1(), 5, 2, wc);
131 }
132 
139 inline bool IsTileOnWater(Tile t)
140 {
141  return (GetWaterClass(t) != WATER_CLASS_INVALID);
142 }
143 
150 inline bool IsWater(Tile t)
151 {
152  return GetWaterTileType(t) == WATER_TILE_CLEAR;
153 }
154 
161 inline bool IsSea(Tile t)
162 {
163  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
164 }
165 
172 inline bool IsCanal(Tile t)
173 {
174  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
175 }
176 
183 inline bool IsRiver(Tile t)
184 {
185  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
186 }
187 
193 inline bool IsWaterTile(Tile t)
194 {
195  return IsTileType(t, MP_WATER) && IsWater(t);
196 }
197 
204 inline bool IsCoast(Tile t)
205 {
206  return GetWaterTileType(t) == WATER_TILE_COAST;
207 }
208 
214 inline bool IsCoastTile(Tile t)
215 {
216  return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
217 }
218 
225 inline bool IsShipDepot(Tile t)
226 {
227  return GetWaterTileType(t) == WATER_TILE_DEPOT;
228 }
229 
235 inline bool IsShipDepotTile(Tile t)
236 {
237  return IsTileType(t, MP_WATER) && IsShipDepot(t);
238 }
239 
247 {
248  assert(IsShipDepotTile(t));
249  return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
250 }
251 
259 {
260  assert(IsShipDepotTile(t));
261  return (DepotPart)GB(t.m5(), WBL_DEPOT_PART, 1);
262 }
263 
271 {
273 }
274 
282 {
283  return TileIndex(t) + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
284 }
285 
293 {
294  assert(IsShipDepot(t));
295  TileIndex tile2 = GetOtherShipDepotTile(t);
296 
297  return t < tile2 ? TileIndex(t) : tile2;
298 }
299 
306 inline bool IsLock(Tile t)
307 {
308  return GetWaterTileType(t) == WATER_TILE_LOCK;
309 }
310 
318 {
319  assert(IsLock(t));
321 }
322 
329 inline uint8_t GetLockPart(Tile t)
330 {
331  assert(IsLock(t));
333 }
334 
341 inline uint8_t GetWaterTileRandomBits(Tile t)
342 {
343  assert(IsTileType(t, MP_WATER));
344  return t.m4();
345 }
346 
353 inline bool HasTileWaterGround(Tile t)
354 {
355  return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
356 }
357 
364 inline void SetDockingTile(Tile t, bool b)
365 {
367  AssignBit(t.m1(), 7, b);
368 }
369 
374 inline bool IsDockingTile(Tile t)
375 {
376  return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(t.m1(), 7);
377 }
378 
379 
384 inline void MakeShore(Tile t)
385 {
386  SetTileType(t, MP_WATER);
389  SetDockingTile(t, false);
390  t.m2() = 0;
391  t.m3() = 0;
392  t.m4() = 0;
394  SB(t.m6(), 2, 4, 0);
395  t.m7() = 0;
396 }
397 
405 inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
406 {
407  SetTileType(t, MP_WATER);
408  SetTileOwner(t, o);
409  SetWaterClass(t, wc);
410  SetDockingTile(t, false);
411  t.m2() = 0;
412  t.m3() = 0;
413  t.m4() = random_bits;
415  SB(t.m6(), 2, 4, 0);
416  t.m7() = 0;
417 }
418 
423 inline void MakeSea(Tile t)
424 {
426 }
427 
433 inline void MakeRiver(Tile t, uint8_t random_bits)
434 {
435  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
436 }
437 
444 inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
445 {
446  assert(o != OWNER_WATER);
447  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
448 }
449 
459 inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
460 {
461  SetTileType(t, MP_WATER);
462  SetTileOwner(t, o);
463  SetWaterClass(t, original_water_class);
464  SetDockingTile(t, false);
465  t.m2() = did;
466  t.m3() = 0;
467  t.m4() = 0;
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;
492  SB(t.m6(), 2, 4, 0);
493  t.m7() = 0;
494 }
495 
505 inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
506 {
507  TileIndexDiff delta = TileOffsByDiagDir(d);
508  Tile lower_tile = TileIndex(t) - delta;
509  Tile upper_tile = TileIndex(t) + delta;
510 
511  /* Keep the current waterclass and owner for the tiles.
512  * It allows to restore them after the lock is deleted */
513  MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
514  MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LOCK_PART_LOWER, d, wc_lower);
515  MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LOCK_PART_UPPER, d, wc_upper);
516 }
517 
518 #endif /* WATER_MAP_H */
WBL_TYPE_COUNT
@ WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition: water_map.h:21
LOCK_PART_UPPER
@ LOCK_PART_UPPER
Upper part of a lock.
Definition: water_map.h:76
IsCoastTile
bool IsCoastTile(Tile t)
Is it a coast tile.
Definition: water_map.h:214
Tile::m5
debug_inline uint8_t & m5()
General purpose.
Definition: map_func.h:161
Tile::m3
debug_inline uint8_t & m3()
General purpose.
Definition: map_func.h:137
GetLockPart
uint8_t GetLockPart(Tile t)
Get the part of a lock.
Definition: water_map.h:329
WBL_TYPE_LOCK
@ WBL_TYPE_LOCK
Lock ('type' bitfield).
Definition: water_map.h:24
GetWaterClass
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition: water_map.h:115
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
WBL_LOCK_ORIENT_COUNT
@ WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition: water_map.h:30
GetWaterTileRandomBits
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition: water_map.h:341
GB
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.
Definition: bitmath_func.hpp:32
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
LockPart
LockPart
Sections of the water lock.
Definition: water_map.h:73
WBL_TYPE_NORMAL
@ WBL_TYPE_NORMAL
Clear water or coast ('type' bitfield).
Definition: water_map.h:23
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition: map_func.h:25
WBL_LOCK_ORIENT_BEGIN
@ WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition: water_map.h:29
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
IsWaterTile
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition: water_map.h:193
Tile::m4
debug_inline uint8_t & m4()
General purpose.
Definition: map_func.h:149
WATER_TILE_DEPOT
@ WATER_TILE_DEPOT
Water Depot.
Definition: water_map.h:43
Tile::m7
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition: map_func.h:185
IsLock
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition: water_map.h:306
MakeSea
void MakeSea(Tile t)
Make a sea tile.
Definition: water_map.h:423
DEPOT_PART_NORTH
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition: water_map.h:67
IsCoast
bool IsCoast(Tile t)
Is it a coast tile?
Definition: water_map.h:204
GetOtherShipDepotTile
TileIndex GetOtherShipDepotTile(Tile t)
Get the other tile of the ship depot.
Definition: water_map.h:281
WATER_TILE_COAST
@ WATER_TILE_COAST
Coast.
Definition: water_map.h:41
tile_map.h
MakeLock
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:505
GetLockDirection
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition: water_map.h:317
Tile::m2
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition: map_func.h:125
IsWater
bool IsWater(Tile t)
Is it a plain water tile?
Definition: water_map.h:150
WATER_TILE_CLEAR
@ WATER_TILE_CLEAR
Plain water.
Definition: water_map.h:40
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
WATER_CLASS_INVALID
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:51
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
MakeWater
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition: water_map.h:405
IsShipDepotTile
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition: water_map.h:235
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:74
TileDiffXY
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:401
WBL_TYPE_DEPOT
@ WBL_TYPE_DEPOT
Depot ('type' bitfield).
Definition: water_map.h:25
IsTileOnWater
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition: water_map.h:139
LOCK_PART_LOWER
@ LOCK_PART_LOWER
Lower part of a lock.
Definition: water_map.h:75
SetWaterClass
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:127
WaterTileTypeBitLayout
WaterTileTypeBitLayout
Bit field layout of m5 for water tiles.
Definition: water_map.h:19
GetShipDepotDirection
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition: water_map.h:270
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition: tile_map.h:178
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
MakeCanal
void MakeCanal(Tile t, Owner o, uint8_t random_bits)
Make a canal tile.
Definition: water_map.h:444
Tile::m6
debug_inline uint8_t & m6()
General purpose.
Definition: map_func.h:173
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition: map_func.h:376
GetShipDepotPart
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition: water_map.h:258
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
HasTileWaterClass
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:104
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:117
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:565
MP_TREES
@ MP_TREES
Tile got trees.
Definition: tile_type.h:52
WBL_COAST_FLAG
@ WBL_COAST_FLAG
Flag for coast.
Definition: water_map.h:27
AssignBit
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
Definition: bitmath_func.hpp:200
DEPOT_PART_SOUTH
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition: water_map.h:68
DepotID
uint16_t DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:13
WaterTileType
WaterTileType
Available water tile types.
Definition: water_map.h:39
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
IsDockingTile
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:374
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
MakeShore
void MakeShore(Tile t)
Helper function to make a coast tile.
Definition: water_map.h:384
depot_type.h
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
SetDockingTile
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:364
WBL_LOCK_PART_COUNT
@ WBL_LOCK_PART_COUNT
Length of lock part bitfield.
Definition: water_map.h:32
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
MakeShipDepot
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:459
TileIndex
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
GetShipDepotNorthTile
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition: water_map.h:292
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
WBL_TYPE_BEGIN
@ WBL_TYPE_BEGIN
Start of the 'type' bitfield.
Definition: water_map.h:20
WBL_DEPOT_AXIS
@ WBL_DEPOT_AXIS
Depot axis flag.
Definition: water_map.h:35
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
GetWaterTileType
WaterTileType GetWaterTileType(Tile t)
Get the water tile type at a tile.
Definition: water_map.h:86
WBL_LOCK_PART_BEGIN
@ WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition: water_map.h:31
WATER_TILE_LOCK
@ WATER_TILE_LOCK
Water lock.
Definition: water_map.h:42
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
GetShipDepotAxis
Axis GetShipDepotAxis(Tile t)
Get the axis of the ship depot.
Definition: water_map.h:246
IsCanal
bool IsCanal(Tile t)
Is it a canal tile?
Definition: water_map.h:172
DepotPart
DepotPart
Sections of the water depot.
Definition: water_map.h:66
WATER_CLASS_RIVER
@ WATER_CLASS_RIVER
River.
Definition: water_map.h:50
IsRiver
bool IsRiver(Tile t)
Is it a river water tile?
Definition: water_map.h:183
HasTileWaterGround
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition: water_map.h:353
IsShipDepot
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:225
WBL_DEPOT_PART
@ WBL_DEPOT_PART
Depot part flag.
Definition: water_map.h:34
Tile::m1
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition: map_func.h:113
IsValidWaterClass
bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition: water_map.h:60
XYNSToDiagDir
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition: direction_func.h:261
MakeLockTile
void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:482
MakeRiver
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition: water_map.h:433
IsSea
bool IsSea(Tile t)
Is it a sea water tile?
Definition: water_map.h:161
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103