OpenTTD Source  13.2.1
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 : byte {
52 };
54 template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
55 
62 static inline bool IsValidWaterClass(WaterClass wc)
63 {
64  return wc < WATER_CLASS_INVALID;
65 }
66 
68 enum DepotPart {
71  DEPOT_PART_END
72 };
73 
75 enum LockPart {
79 };
80 
81 bool IsPossibleDockingTile(TileIndex t);
82 
89 {
90  assert(IsTileType(t, MP_WATER));
91 
92  switch (GB(_m[t].m5, WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
94  case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
95  case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
96  default: NOT_REACHED();
97  }
98 }
99 
106 static inline bool HasTileWaterClass(TileIndex t)
107 {
109 }
110 
118 {
119  assert(HasTileWaterClass(t));
120  return (WaterClass)GB(_m[t].m1, 5, 2);
121 }
122 
129 static inline void SetWaterClass(TileIndex t, WaterClass wc)
130 {
131  assert(HasTileWaterClass(t));
132  SB(_m[t].m1, 5, 2, wc);
133 }
134 
141 static inline bool IsTileOnWater(TileIndex t)
142 {
143  return (GetWaterClass(t) != WATER_CLASS_INVALID);
144 }
145 
152 static inline bool IsWater(TileIndex t)
153 {
154  return GetWaterTileType(t) == WATER_TILE_CLEAR;
155 }
156 
163 static inline bool IsSea(TileIndex t)
164 {
165  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
166 }
167 
174 static inline bool IsCanal(TileIndex t)
175 {
176  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
177 }
178 
185 static inline bool IsRiver(TileIndex t)
186 {
187  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
188 }
189 
195 static inline bool IsWaterTile(TileIndex t)
196 {
197  return IsTileType(t, MP_WATER) && IsWater(t);
198 }
199 
206 static inline bool IsCoast(TileIndex t)
207 {
208  return GetWaterTileType(t) == WATER_TILE_COAST;
209 }
210 
216 static inline bool IsCoastTile(TileIndex t)
217 {
218  return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
219 }
220 
227 static inline bool IsShipDepot(TileIndex t)
228 {
229  return GetWaterTileType(t) == WATER_TILE_DEPOT;
230 }
231 
237 static inline bool IsShipDepotTile(TileIndex t)
238 {
239  return IsTileType(t, MP_WATER) && IsShipDepot(t);
240 }
241 
249 {
250  assert(IsShipDepotTile(t));
251  return (Axis)GB(_m[t].m5, WBL_DEPOT_AXIS, 1);
252 }
253 
261 {
262  assert(IsShipDepotTile(t));
263  return (DepotPart)GB(_m[t].m5, WBL_DEPOT_PART, 1);
264 }
265 
273 {
275 }
276 
284 {
285  return t + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
286 }
287 
295 {
296  assert(IsShipDepot(t));
297  TileIndex tile2 = GetOtherShipDepotTile(t);
298 
299  return t < tile2 ? t : tile2;
300 }
301 
308 static inline bool IsLock(TileIndex t)
309 {
310  return GetWaterTileType(t) == WATER_TILE_LOCK;
311 }
312 
320 {
321  assert(IsLock(t));
323 }
324 
331 static inline byte GetLockPart(TileIndex t)
332 {
333  assert(IsLock(t));
335 }
336 
343 static inline byte GetWaterTileRandomBits(TileIndex t)
344 {
345  assert(IsTileType(t, MP_WATER));
346  return _m[t].m4;
347 }
348 
355 static inline bool HasTileWaterGround(TileIndex t)
356 {
357  return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
358 }
359 
366 static inline void SetDockingTile(TileIndex t, bool b)
367 {
369  SB(_m[t].m1, 7, 1, b ? 1 : 0);
370 }
371 
376 static inline bool IsDockingTile(TileIndex t)
377 {
378  return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(_m[t].m1, 7);
379 }
380 
381 
386 static inline void MakeShore(TileIndex t)
387 {
388  SetTileType(t, MP_WATER);
391  SetDockingTile(t, false);
392  _m[t].m2 = 0;
393  _m[t].m3 = 0;
394  _m[t].m4 = 0;
396  SB(_me[t].m6, 2, 4, 0);
397  _me[t].m7 = 0;
398 }
399 
407 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
408 {
409  SetTileType(t, MP_WATER);
410  SetTileOwner(t, o);
411  SetWaterClass(t, wc);
412  SetDockingTile(t, false);
413  _m[t].m2 = 0;
414  _m[t].m3 = 0;
415  _m[t].m4 = random_bits;
417  SB(_me[t].m6, 2, 4, 0);
418  _me[t].m7 = 0;
419 }
420 
425 static inline void MakeSea(TileIndex t)
426 {
428 }
429 
435 static inline void MakeRiver(TileIndex t, uint8 random_bits)
436 {
437  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
438 }
439 
446 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
447 {
448  assert(o != OWNER_WATER);
449  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
450 }
451 
461 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
462 {
463  SetTileType(t, MP_WATER);
464  SetTileOwner(t, o);
465  SetWaterClass(t, original_water_class);
466  SetDockingTile(t, false);
467  _m[t].m2 = did;
468  _m[t].m3 = 0;
469  _m[t].m4 = 0;
471  SB(_me[t].m6, 2, 4, 0);
472  _me[t].m7 = 0;
473 }
474 
484 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
485 {
486  SetTileType(t, MP_WATER);
487  SetTileOwner(t, o);
488  SetWaterClass(t, original_water_class);
489  SetDockingTile(t, false);
490  _m[t].m2 = 0;
491  _m[t].m3 = 0;
492  _m[t].m4 = 0;
494  SB(_me[t].m6, 2, 4, 0);
495  _me[t].m7 = 0;
496 }
497 
507 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
508 {
509  TileIndexDiff delta = TileOffsByDiagDir(d);
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(t - delta, IsWaterTile(t - delta) ? GetTileOwner(t - delta) : o, LOCK_PART_LOWER, d, wc_lower);
515  MakeLockTile(t + delta, IsWaterTile(t + delta) ? GetTileOwner(t + delta) : 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:78
IsSea
static bool IsSea(TileIndex t)
Is it a sea water tile?
Definition: water_map.h:163
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
IsCanal
static bool IsCanal(TileIndex t)
Is it a canal tile?
Definition: water_map.h:174
GetOtherShipDepotTile
static TileIndex GetOtherShipDepotTile(TileIndex t)
Get the other tile of the ship depot.
Definition: water_map.h:283
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
HasTileWaterClass
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:106
WBL_TYPE_LOCK
@ WBL_TYPE_LOCK
Lock ('type' bitfield).
Definition: water_map.h:24
SetTileType
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
GetLockDirection
static DiagDirection GetLockDirection(TileIndex t)
Get the direction of the water lock.
Definition: water_map.h:319
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:125
WBL_LOCK_ORIENT_COUNT
@ WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition: water_map.h:30
MakeSea
static void MakeSea(TileIndex t)
Make a sea tile.
Definition: water_map.h:425
DepotID
uint16 DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:15
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_me
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
LockPart
LockPart
Sections of the water lock.
Definition: water_map.h:75
WBL_TYPE_NORMAL
@ WBL_TYPE_NORMAL
Clear water or coast ('type' bitfield).
Definition: water_map.h:23
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
Tile::m2
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:20
IsCoastTile
static bool IsCoastTile(TileIndex t)
Is it a coast tile.
Definition: water_map.h:216
WBL_LOCK_ORIENT_BEGIN
@ WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition: water_map.h:29
IsCoast
static bool IsCoast(TileIndex t)
Is it a coast tile?
Definition: water_map.h:206
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
WATER_TILE_DEPOT
@ WATER_TILE_DEPOT
Water Depot.
Definition: water_map.h:43
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
WATER_CLASS_INVALID
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:51
GetShipDepotDirection
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:272
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:331
DEPOT_PART_NORTH
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition: water_map.h:69
IsTileOnWater
static bool IsTileOnWater(TileIndex t)
Tests if the tile was built on water.
Definition: water_map.h:141
GetShipDepotNorthTile
static TileIndex GetShipDepotNorthTile(TileIndex t)
Get the most northern tile of a ship depot.
Definition: water_map.h:294
WATER_TILE_COAST
@ WATER_TILE_COAST
Coast.
Definition: water_map.h:41
EnumPropsT
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:48
tile_map.h
HasTileWaterGround
static bool HasTileWaterGround(TileIndex t)
Checks whether the tile has water at the ground.
Definition: water_map.h:355
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
WATER_TILE_CLEAR
@ WATER_TILE_CLEAR
Plain water.
Definition: water_map.h:40
MakeLock
static void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
Make a water lock.
Definition: water_map.h:507
XYNSToDiagDir
static DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition: direction_func.h:261
TileIndexDiff
int32 TileIndexDiff
An offset value between two tiles.
Definition: map_func.h:154
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
IsShipDepot
static bool IsShipDepot(TileIndex t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:227
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:76
IsValidWaterClass
static bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition: water_map.h:62
WBL_TYPE_DEPOT
@ WBL_TYPE_DEPOT
Depot ('type' bitfield).
Definition: water_map.h:25
SetDockingTile
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:366
LOCK_PART_LOWER
@ LOCK_PART_LOWER
Lower part of a lock.
Definition: water_map.h:77
WaterTileTypeBitLayout
WaterTileTypeBitLayout
Bit field layout of m5 for water tiles.
Definition: water_map.h:19
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
WATER_CLASS_RIVER
@ WATER_CLASS_RIVER
River.
Definition: water_map.h:50
MakeLockTile
static void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:484
GetShipDepotPart
static DepotPart GetShipDepotPart(TileIndex t)
Get the part of a ship depot.
Definition: water_map.h:260
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
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
DEPOT_PART_SOUTH
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition: water_map.h:70
Tile::m5
byte m5
General purpose.
Definition: map_type.h:24
WaterTileType
WaterTileType
Available water tile types.
Definition: water_map.h:39
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:117
GetWaterTileType
static WaterTileType GetWaterTileType(TileIndex t)
Get the water tile type at a tile.
Definition: water_map.h:88
GetWaterTileRandomBits
static byte GetWaterTileRandomBits(TileIndex t)
Get the random bits of the water tile.
Definition: water_map.h:343
MakeShipDepot
static void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
Make a ship depot section.
Definition: water_map.h:461
IsWaterTile
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:195
depot_type.h
MakeShore
static void MakeShore(TileIndex t)
Helper function to make a coast tile.
Definition: water_map.h:386
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
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
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:308
GetShipDepotAxis
static Axis GetShipDepotAxis(TileIndex t)
Get the axis of the ship depot.
Definition: water_map.h:248
IsRiver
static bool IsRiver(TileIndex t)
Is it a river water tile?
Definition: water_map.h:185
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:126
MakeEnumPropsT
Helper template class that makes basic properties of given enumeration type visible from outsize.
Definition: enum_type.hpp:62
IsDockingTile
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:376
MakeCanal
static void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
Make a canal tile.
Definition: water_map.h:446
TileExtended::m7
byte m7
Primarily used for newgrf support.
Definition: map_type.h:35
SetTileOwner
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
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
MakeWater
static void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
Helper function for making a watery tile.
Definition: water_map.h:407
SetWaterClass
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:129
WBL_LOCK_PART_BEGIN
@ WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition: water_map.h:31
IsWater
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:152
WATER_TILE_LOCK
@ WATER_TILE_LOCK
Water lock.
Definition: water_map.h:42
Tile::m3
byte m3
General purpose.
Definition: map_type.h:22
IsShipDepotTile
static bool IsShipDepotTile(TileIndex t)
Is it a ship depot tile?
Definition: water_map.h:237
DepotPart
DepotPart
Sections of the water depot.
Definition: water_map.h:68
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
Tile::m4
byte m4
General purpose.
Definition: map_type.h:23
_m
Tile * _m
Tiles of the map.
Definition: map.cpp:30
WBL_DEPOT_PART
@ WBL_DEPOT_PART
Depot part flag.
Definition: water_map.h:34
MakeRiver
static void MakeRiver(TileIndex t, uint8 random_bits)
Make a river tile.
Definition: water_map.h:435