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/>.
8 /** @file water_map.h Map accessors for water tiles. */
13 #include "depot_type.h"
17 * Bit field layout of m5 for water tiles.
19 static constexpr uint8_t WBL_TYPE_BEGIN
= 4; ///< Start of the 'type' bitfield.
20 static constexpr uint8_t WBL_TYPE_COUNT
= 4; ///< Length of the 'type' bitfield.
22 static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN
= 0; ///< Start of lock orientation bitfield.
23 static constexpr uint8_t WBL_LOCK_ORIENT_COUNT
= 2; ///< Length of lock orientation bitfield.
24 static constexpr uint8_t WBL_LOCK_PART_BEGIN
= 2; ///< Start of lock part bitfield.
25 static constexpr uint8_t WBL_LOCK_PART_COUNT
= 2; ///< Length of lock part bitfield.
27 static constexpr uint8_t WBL_DEPOT_PART
= 0; ///< Depot part flag.
28 static constexpr uint8_t WBL_DEPOT_AXIS
= 1; ///< Depot axis flag.
30 /** Available water tile types. */
32 WATER_TILE_CLEAR
, ///< Plain water.
33 WATER_TILE_COAST
, ///< Coast.
34 WATER_TILE_LOCK
, ///< Water lock.
35 WATER_TILE_DEPOT
, ///< Water Depot.
38 /** classes of water (for #WATER_TILE_CLEAR water tile type). */
39 enum WaterClass
: uint8_t {
40 WATER_CLASS_SEA
, ///< Sea.
41 WATER_CLASS_CANAL
, ///< Canal.
42 WATER_CLASS_RIVER
, ///< River.
43 WATER_CLASS_INVALID
, ///< Used for industry tiles on land (also for oilrig if newgrf says so).
47 * Checks if a water class is valid.
49 * @param wc The value to check
50 * @return true if the given value is a valid water class.
52 inline bool IsValidWaterClass(WaterClass wc
)
54 return wc
< WATER_CLASS_INVALID
;
57 /** Sections of the water depot. */
59 DEPOT_PART_NORTH
= 0, ///< Northern part of a depot.
60 DEPOT_PART_SOUTH
= 1, ///< Southern part of a depot.
64 /** Sections of the water lock. */
66 LOCK_PART_MIDDLE
= 0, ///< Middle part of a lock.
67 LOCK_PART_LOWER
= 1, ///< Lower part of a lock.
68 LOCK_PART_UPPER
= 2, ///< Upper part of a lock.
71 bool IsPossibleDockingTile(Tile t
);
74 * Get the water tile type of a tile.
75 * @param t Water tile to query.
76 * @return Water tile type at the tile.
78 inline WaterTileType
GetWaterTileType(Tile t
)
80 assert(IsTileType(t
, MP_WATER
));
81 return static_cast<WaterTileType
>(GB(t
.m5(), WBL_TYPE_BEGIN
, WBL_TYPE_COUNT
));
85 * Set the water tile type of a tile.
86 * @param t Water tile to set.
87 * @param type Water tile type of the tile.
89 inline void SetWaterTileType(Tile t
, WaterTileType type
)
91 assert(IsTileType(t
, MP_WATER
));
92 SB(t
.m5(), WBL_TYPE_BEGIN
, WBL_TYPE_COUNT
, to_underlying(type
));
96 * Checks whether the tile has an waterclass associated.
97 * You can then subsequently call GetWaterClass().
98 * @param t Tile to query.
99 * @return True if the tiletype has a waterclass.
101 inline bool HasTileWaterClass(Tile t
)
103 return IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_INDUSTRY
) || IsTileType(t
, MP_OBJECT
) || IsTileType(t
, MP_TREES
);
107 * Get the water class at a tile.
108 * @param t Water tile to query.
109 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
110 * @return Water class at the tile.
112 inline WaterClass
GetWaterClass(Tile t
)
114 assert(HasTileWaterClass(t
));
115 return (WaterClass
)GB(t
.m1(), 5, 2);
119 * Set the water class at a tile.
120 * @param t Water tile to change.
121 * @param wc New water class.
122 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
124 inline void SetWaterClass(Tile t
, WaterClass wc
)
126 assert(HasTileWaterClass(t
));
127 SB(t
.m1(), 5, 2, wc
);
131 * Tests if the tile was built on water.
132 * @param t the tile to check
133 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
134 * @return true iff on water
136 inline bool IsTileOnWater(Tile t
)
138 return (GetWaterClass(t
) != WATER_CLASS_INVALID
);
142 * Is it a plain water tile?
143 * @param t Water tile to query.
144 * @return \c true if any type of clear water like ocean, river, or canal.
145 * @pre IsTileType(t, MP_WATER)
147 inline bool IsWater(Tile t
)
149 return GetWaterTileType(t
) == WATER_TILE_CLEAR
;
153 * Is it a sea water tile?
154 * @param t Water tile to query.
155 * @return \c true if it is a sea water tile.
156 * @pre IsTileType(t, MP_WATER)
158 inline bool IsSea(Tile t
)
160 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_SEA
;
164 * Is it a canal tile?
165 * @param t Water tile to query.
166 * @return \c true if it is a canal tile.
167 * @pre IsTileType(t, MP_WATER)
169 inline bool IsCanal(Tile t
)
171 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_CANAL
;
175 * Is it a river water tile?
176 * @param t Water tile to query.
177 * @return \c true if it is a river water tile.
178 * @pre IsTileType(t, MP_WATER)
180 inline bool IsRiver(Tile t
)
182 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_RIVER
;
186 * Is it a water tile with plain water?
187 * @param t Tile to query.
188 * @return \c true if it is a plain water tile.
190 inline bool IsWaterTile(Tile t
)
192 return IsTileType(t
, MP_WATER
) && IsWater(t
);
196 * Is it a coast tile?
197 * @param t Water tile to query.
198 * @return \c true if it is a sea water tile.
199 * @pre IsTileType(t, MP_WATER)
201 inline bool IsCoast(Tile t
)
203 return GetWaterTileType(t
) == WATER_TILE_COAST
;
208 * @param t Tile to query.
209 * @return \c true if it is a coast.
211 inline bool IsCoastTile(Tile t
)
213 return (IsTileType(t
, MP_WATER
) && IsCoast(t
)) || (IsTileType(t
, MP_TREES
) && GetWaterClass(t
) != WATER_CLASS_INVALID
);
217 * Is it a water tile with a ship depot on it?
218 * @param t Water tile to query.
219 * @return \c true if it is a ship depot tile.
220 * @pre IsTileType(t, MP_WATER)
222 inline bool IsShipDepot(Tile t
)
224 return GetWaterTileType(t
) == WATER_TILE_DEPOT
;
228 * Is it a ship depot tile?
229 * @param t Tile to query.
230 * @return \c true if it is a ship depot tile.
232 inline bool IsShipDepotTile(Tile t
)
234 return IsTileType(t
, MP_WATER
) && IsShipDepot(t
);
238 * Get the axis of the ship depot.
239 * @param t Water tile to query.
240 * @return Axis of the depot.
241 * @pre IsShipDepotTile(t)
243 inline Axis
GetShipDepotAxis(Tile t
)
245 assert(IsShipDepotTile(t
));
246 return (Axis
)GB(t
.m5(), WBL_DEPOT_AXIS
, 1);
250 * Get the part of a ship depot.
251 * @param t Water tile to query.
252 * @return Part of the depot.
253 * @pre IsShipDepotTile(t)
255 inline DepotPart
GetShipDepotPart(Tile t
)
257 assert(IsShipDepotTile(t
));
258 return (DepotPart
)GB(t
.m5(), WBL_DEPOT_PART
, 1);
262 * Get the direction of the ship depot.
263 * @param t Water tile to query.
264 * @return Direction of the depot.
265 * @pre IsShipDepotTile(t)
267 inline DiagDirection
GetShipDepotDirection(Tile t
)
269 return XYNSToDiagDir(GetShipDepotAxis(t
), GetShipDepotPart(t
));
273 * Get the other tile of the ship depot.
274 * @param t Tile to query, containing one section of a ship depot.
275 * @return Tile containing the other section of the depot.
276 * @pre IsShipDepotTile(t)
278 inline TileIndex
GetOtherShipDepotTile(Tile t
)
280 return TileIndex(t
) + (GetShipDepotPart(t
) != DEPOT_PART_NORTH
? -1 : 1) * TileOffsByAxis(GetShipDepotAxis(t
));
284 * Get the most northern tile of a ship depot.
285 * @param t One of the tiles of the ship depot.
286 * @return The northern tile of the depot.
287 * @pre IsShipDepotTile(t)
289 inline TileIndex
GetShipDepotNorthTile(Tile t
)
291 assert(IsShipDepot(t
));
292 TileIndex tile2
= GetOtherShipDepotTile(t
);
294 return t
< tile2
? TileIndex(t
) : tile2
;
298 * Is there a lock on a given water tile?
299 * @param t Water tile to query.
300 * @return \c true if it is a water lock tile.
301 * @pre IsTileType(t, MP_WATER)
303 inline bool IsLock(Tile t
)
305 return GetWaterTileType(t
) == WATER_TILE_LOCK
;
309 * Get the direction of the water lock.
310 * @param t Water tile to query.
311 * @return Direction of the lock.
312 * @pre IsTileType(t, MP_WATER) && IsLock(t)
314 inline DiagDirection
GetLockDirection(Tile t
)
317 return (DiagDirection
)GB(t
.m5(), WBL_LOCK_ORIENT_BEGIN
, WBL_LOCK_ORIENT_COUNT
);
321 * Get the part of a lock.
322 * @param t Water tile to query.
324 * @pre IsTileType(t, MP_WATER) && IsLock(t)
326 inline uint8_t GetLockPart(Tile t
)
329 return GB(t
.m5(), WBL_LOCK_PART_BEGIN
, WBL_LOCK_PART_COUNT
);
333 * Get the random bits of the water tile.
334 * @param t Water tile to query.
335 * @return Random bits of the tile.
336 * @pre IsTileType(t, MP_WATER)
338 inline uint8_t GetWaterTileRandomBits(Tile t
)
340 assert(IsTileType(t
, MP_WATER
));
345 * Checks whether the tile has water at the ground.
346 * That is, it is either some plain water tile, or a object/industry/station/... with water under it.
347 * @return true iff the tile has water at the ground.
348 * @note Coast tiles are not considered waterish, even if there is water on a halftile.
350 inline bool HasTileWaterGround(Tile t
)
352 return HasTileWaterClass(t
) && IsTileOnWater(t
) && !IsCoastTile(t
);
356 * Set the docking tile state of a tile. This is used by pathfinders to reach their destination.
357 * As well as water tiles, half-rail tiles, buoys and aqueduct ends can also be docking tiles.
359 * @param b the docking tile state
361 inline void SetDockingTile(Tile t
, bool b
)
363 assert(IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
));
364 AssignBit(t
.m1(), 7, b
);
368 * Checks whether the tile is marked as a dockling tile.
369 * @return true iff the tile is marked as a docking tile.
371 inline bool IsDockingTile(Tile t
)
373 return (IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
)) && HasBit(t
.m1(), 7);
378 * Helper function to make a coast tile.
379 * @param t The tile to change into water
381 inline void MakeShore(Tile t
)
383 SetTileType(t
, MP_WATER
);
384 SetTileOwner(t
, OWNER_WATER
);
385 SetWaterClass(t
, WATER_CLASS_SEA
);
386 SetDockingTile(t
, false);
391 SetWaterTileType(t
, WATER_TILE_COAST
);
397 * Helper function for making a watery tile.
398 * @param t The tile to change into water
399 * @param o The owner of the water
400 * @param wc The class of water the tile has to be
401 * @param random_bits Eventual random bits to be set for this tile
403 inline void MakeWater(Tile t
, Owner o
, WaterClass wc
, uint8_t random_bits
)
405 SetTileType(t
, MP_WATER
);
407 SetWaterClass(t
, wc
);
408 SetDockingTile(t
, false);
411 t
.m4() = random_bits
;
413 SetWaterTileType(t
, WATER_TILE_CLEAR
);
420 * @param t The tile to change into sea
422 inline void MakeSea(Tile t
)
424 MakeWater(t
, OWNER_WATER
, WATER_CLASS_SEA
, 0);
429 * @param t The tile to change into river
430 * @param random_bits Random bits to be set for this tile
432 inline void MakeRiver(Tile t
, uint8_t random_bits
)
434 MakeWater(t
, OWNER_WATER
, WATER_CLASS_RIVER
, random_bits
);
439 * @param t The tile to change into canal
440 * @param o The owner of the canal
441 * @param random_bits Random bits to be set for this tile
443 inline void MakeCanal(Tile t
, Owner o
, uint8_t random_bits
)
445 assert(o
!= OWNER_WATER
);
446 MakeWater(t
, o
, WATER_CLASS_CANAL
, random_bits
);
450 * Make a ship depot section.
451 * @param t Tile to place the ship depot section.
452 * @param o Owner of the depot.
453 * @param did Depot ID.
454 * @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
455 * @param a Axis of the depot.
456 * @param original_water_class Original water class.
458 inline void MakeShipDepot(Tile t
, Owner o
, DepotID did
, DepotPart part
, Axis a
, WaterClass original_water_class
)
460 SetTileType(t
, MP_WATER
);
462 SetWaterClass(t
, original_water_class
);
463 SetDockingTile(t
, false);
467 t
.m5() = part
<< WBL_DEPOT_PART
| a
<< WBL_DEPOT_AXIS
;
468 SetWaterTileType(t
, WATER_TILE_DEPOT
);
474 * Make a lock section.
475 * @param t Tile to place the water lock section.
476 * @param o Owner of the lock.
477 * @param part Part to place.
478 * @param dir Lock orientation
479 * @param original_water_class Original water class.
482 inline void MakeLockTile(Tile t
, Owner o
, LockPart part
, DiagDirection dir
, WaterClass original_water_class
)
484 SetTileType(t
, MP_WATER
);
486 SetWaterClass(t
, original_water_class
);
487 SetDockingTile(t
, false);
491 t
.m5() = part
<< WBL_LOCK_PART_BEGIN
| dir
<< WBL_LOCK_ORIENT_BEGIN
;
492 SetWaterTileType(t
, WATER_TILE_LOCK
);
499 * @param t Tile to place the water lock section.
500 * @param o Owner of the lock.
501 * @param d Direction of the water lock.
502 * @param wc_lower Original water class of the lower part.
503 * @param wc_upper Original water class of the upper part.
504 * @param wc_middle Original water class of the middle part.
506 inline void MakeLock(Tile t
, Owner o
, DiagDirection d
, WaterClass wc_lower
, WaterClass wc_upper
, WaterClass wc_middle
)
508 TileIndexDiff delta
= TileOffsByDiagDir(d
);
509 Tile lower_tile
= TileIndex(t
) - delta
;
510 Tile upper_tile
= TileIndex(t
) + delta
;
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
);
519 #endif /* WATER_MAP_H */