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 enum WaterTileTypeBitLayout
{
20 WBL_TYPE_BEGIN
= 4, ///< Start of the 'type' bitfield.
21 WBL_TYPE_COUNT
= 4, ///< Length of the 'type' bitfield.
23 WBL_TYPE_NORMAL
= 0x0, ///< Clear water or coast ('type' bitfield).
24 WBL_TYPE_LOCK
= 0x1, ///< Lock ('type' bitfield).
25 WBL_TYPE_DEPOT
= 0x8, ///< Depot ('type' bitfield).
27 WBL_COAST_FLAG
= 0, ///< Flag for coast.
29 WBL_LOCK_ORIENT_BEGIN
= 0, ///< Start of lock orientation bitfield.
30 WBL_LOCK_ORIENT_COUNT
= 2, ///< Length of lock orientation bitfield.
31 WBL_LOCK_PART_BEGIN
= 2, ///< Start of lock part bitfield.
32 WBL_LOCK_PART_COUNT
= 2, ///< Length of lock part bitfield.
34 WBL_DEPOT_PART
= 0, ///< Depot part flag.
35 WBL_DEPOT_AXIS
= 1, ///< Depot axis flag.
38 /** Available water tile types. */
40 WATER_TILE_CLEAR
, ///< Plain water.
41 WATER_TILE_COAST
, ///< Coast.
42 WATER_TILE_LOCK
, ///< Water lock.
43 WATER_TILE_DEPOT
, ///< Water Depot.
46 /** classes of water (for #WATER_TILE_CLEAR water tile type). */
47 enum WaterClass
: uint8_t {
48 WATER_CLASS_SEA
, ///< Sea.
49 WATER_CLASS_CANAL
, ///< Canal.
50 WATER_CLASS_RIVER
, ///< River.
51 WATER_CLASS_INVALID
, ///< Used for industry tiles on land (also for oilrig if newgrf says so).
55 * Checks if a water class is valid.
57 * @param wc The value to check
58 * @return true if the given value is a valid water class.
60 inline bool IsValidWaterClass(WaterClass wc
)
62 return wc
< WATER_CLASS_INVALID
;
65 /** Sections of the water depot. */
67 DEPOT_PART_NORTH
= 0, ///< Northern part of a depot.
68 DEPOT_PART_SOUTH
= 1, ///< Southern part of a depot.
72 /** Sections of the water lock. */
74 LOCK_PART_MIDDLE
= 0, ///< Middle part of a lock.
75 LOCK_PART_LOWER
= 1, ///< Lower part of a lock.
76 LOCK_PART_UPPER
= 2, ///< Upper part of a lock.
79 bool IsPossibleDockingTile(Tile t
);
82 * Get the water tile type at a tile.
83 * @param t Water tile to query.
84 * @return Water tile type at the tile.
86 inline WaterTileType
GetWaterTileType(Tile t
)
88 assert(IsTileType(t
, MP_WATER
));
90 switch (GB(t
.m5(), WBL_TYPE_BEGIN
, WBL_TYPE_COUNT
)) {
91 case WBL_TYPE_NORMAL
: return HasBit(t
.m5(), WBL_COAST_FLAG
) ? WATER_TILE_COAST
: WATER_TILE_CLEAR
;
92 case WBL_TYPE_LOCK
: return WATER_TILE_LOCK
;
93 case WBL_TYPE_DEPOT
: return WATER_TILE_DEPOT
;
94 default: NOT_REACHED();
99 * Checks whether the tile has an waterclass associated.
100 * You can then subsequently call GetWaterClass().
101 * @param t Tile to query.
102 * @return True if the tiletype has a waterclass.
104 inline bool HasTileWaterClass(Tile t
)
106 return IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_INDUSTRY
) || IsTileType(t
, MP_OBJECT
) || IsTileType(t
, MP_TREES
);
110 * Get the water class at a tile.
111 * @param t Water tile to query.
112 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
113 * @return Water class at the tile.
115 inline WaterClass
GetWaterClass(Tile t
)
117 assert(HasTileWaterClass(t
));
118 return (WaterClass
)GB(t
.m1(), 5, 2);
122 * Set the water class at a tile.
123 * @param t Water tile to change.
124 * @param wc New water class.
125 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
127 inline void SetWaterClass(Tile t
, WaterClass wc
)
129 assert(HasTileWaterClass(t
));
130 SB(t
.m1(), 5, 2, wc
);
134 * Tests if the tile was built on water.
135 * @param t the tile to check
136 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
137 * @return true iff on water
139 inline bool IsTileOnWater(Tile t
)
141 return (GetWaterClass(t
) != WATER_CLASS_INVALID
);
145 * Is it a plain water tile?
146 * @param t Water tile to query.
147 * @return \c true if any type of clear water like ocean, river, or canal.
148 * @pre IsTileType(t, MP_WATER)
150 inline bool IsWater(Tile t
)
152 return GetWaterTileType(t
) == WATER_TILE_CLEAR
;
156 * Is it a sea water tile?
157 * @param t Water tile to query.
158 * @return \c true if it is a sea water tile.
159 * @pre IsTileType(t, MP_WATER)
161 inline bool IsSea(Tile t
)
163 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_SEA
;
167 * Is it a canal tile?
168 * @param t Water tile to query.
169 * @return \c true if it is a canal tile.
170 * @pre IsTileType(t, MP_WATER)
172 inline bool IsCanal(Tile t
)
174 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_CANAL
;
178 * Is it a river water tile?
179 * @param t Water tile to query.
180 * @return \c true if it is a river water tile.
181 * @pre IsTileType(t, MP_WATER)
183 inline bool IsRiver(Tile t
)
185 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_RIVER
;
189 * Is it a water tile with plain water?
190 * @param t Tile to query.
191 * @return \c true if it is a plain water tile.
193 inline bool IsWaterTile(Tile t
)
195 return IsTileType(t
, MP_WATER
) && IsWater(t
);
199 * Is it a coast tile?
200 * @param t Water tile to query.
201 * @return \c true if it is a sea water tile.
202 * @pre IsTileType(t, MP_WATER)
204 inline bool IsCoast(Tile t
)
206 return GetWaterTileType(t
) == WATER_TILE_COAST
;
211 * @param t Tile to query.
212 * @return \c true if it is a coast.
214 inline bool IsCoastTile(Tile t
)
216 return (IsTileType(t
, MP_WATER
) && IsCoast(t
)) || (IsTileType(t
, MP_TREES
) && GetWaterClass(t
) != WATER_CLASS_INVALID
);
220 * Is it a water tile with a ship depot on it?
221 * @param t Water tile to query.
222 * @return \c true if it is a ship depot tile.
223 * @pre IsTileType(t, MP_WATER)
225 inline bool IsShipDepot(Tile t
)
227 return GetWaterTileType(t
) == WATER_TILE_DEPOT
;
231 * Is it a ship depot tile?
232 * @param t Tile to query.
233 * @return \c true if it is a ship depot tile.
235 inline bool IsShipDepotTile(Tile t
)
237 return IsTileType(t
, MP_WATER
) && IsShipDepot(t
);
241 * Get the axis of the ship depot.
242 * @param t Water tile to query.
243 * @return Axis of the depot.
244 * @pre IsShipDepotTile(t)
246 inline Axis
GetShipDepotAxis(Tile t
)
248 assert(IsShipDepotTile(t
));
249 return (Axis
)GB(t
.m5(), WBL_DEPOT_AXIS
, 1);
253 * Get the part of a ship depot.
254 * @param t Water tile to query.
255 * @return Part of the depot.
256 * @pre IsShipDepotTile(t)
258 inline DepotPart
GetShipDepotPart(Tile t
)
260 assert(IsShipDepotTile(t
));
261 return (DepotPart
)GB(t
.m5(), WBL_DEPOT_PART
, 1);
265 * Get the direction of the ship depot.
266 * @param t Water tile to query.
267 * @return Direction of the depot.
268 * @pre IsShipDepotTile(t)
270 inline DiagDirection
GetShipDepotDirection(Tile t
)
272 return XYNSToDiagDir(GetShipDepotAxis(t
), GetShipDepotPart(t
));
276 * Get the other tile of the ship depot.
277 * @param t Tile to query, containing one section of a ship depot.
278 * @return Tile containing the other section of the depot.
279 * @pre IsShipDepotTile(t)
281 inline TileIndex
GetOtherShipDepotTile(Tile t
)
283 return TileIndex(t
) + (GetShipDepotPart(t
) != DEPOT_PART_NORTH
? -1 : 1) * (GetShipDepotAxis(t
) != AXIS_X
? TileDiffXY(0, 1) : TileDiffXY(1, 0));
287 * Get the most northern tile of a ship depot.
288 * @param t One of the tiles of the ship depot.
289 * @return The northern tile of the depot.
290 * @pre IsShipDepotTile(t)
292 inline TileIndex
GetShipDepotNorthTile(Tile t
)
294 assert(IsShipDepot(t
));
295 TileIndex tile2
= GetOtherShipDepotTile(t
);
297 return t
< tile2
? TileIndex(t
) : tile2
;
301 * Is there a lock on a given water tile?
302 * @param t Water tile to query.
303 * @return \c true if it is a water lock tile.
304 * @pre IsTileType(t, MP_WATER)
306 inline bool IsLock(Tile t
)
308 return GetWaterTileType(t
) == WATER_TILE_LOCK
;
312 * Get the direction of the water lock.
313 * @param t Water tile to query.
314 * @return Direction of the lock.
315 * @pre IsTileType(t, MP_WATER) && IsLock(t)
317 inline DiagDirection
GetLockDirection(Tile t
)
320 return (DiagDirection
)GB(t
.m5(), WBL_LOCK_ORIENT_BEGIN
, WBL_LOCK_ORIENT_COUNT
);
324 * Get the part of a lock.
325 * @param t Water tile to query.
327 * @pre IsTileType(t, MP_WATER) && IsLock(t)
329 inline uint8_t GetLockPart(Tile t
)
332 return GB(t
.m5(), WBL_LOCK_PART_BEGIN
, WBL_LOCK_PART_COUNT
);
336 * Get the random bits of the water tile.
337 * @param t Water tile to query.
338 * @return Random bits of the tile.
339 * @pre IsTileType(t, MP_WATER)
341 inline uint8_t GetWaterTileRandomBits(Tile t
)
343 assert(IsTileType(t
, MP_WATER
));
348 * Checks whether the tile has water at the ground.
349 * That is, it is either some plain water tile, or a object/industry/station/... with water under it.
350 * @return true iff the tile has water at the ground.
351 * @note Coast tiles are not considered waterish, even if there is water on a halftile.
353 inline bool HasTileWaterGround(Tile t
)
355 return HasTileWaterClass(t
) && IsTileOnWater(t
) && !IsCoastTile(t
);
359 * Set the docking tile state of a tile. This is used by pathfinders to reach their destination.
360 * As well as water tiles, half-rail tiles, buoys and aqueduct ends can also be docking tiles.
362 * @param b the docking tile state
364 inline void SetDockingTile(Tile t
, bool b
)
366 assert(IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
));
367 AssignBit(t
.m1(), 7, b
);
371 * Checks whether the tile is marked as a dockling tile.
372 * @return true iff the tile is marked as a docking tile.
374 inline bool IsDockingTile(Tile t
)
376 return (IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
)) && HasBit(t
.m1(), 7);
381 * Helper function to make a coast tile.
382 * @param t The tile to change into water
384 inline void MakeShore(Tile t
)
386 SetTileType(t
, MP_WATER
);
387 SetTileOwner(t
, OWNER_WATER
);
388 SetWaterClass(t
, WATER_CLASS_SEA
);
389 SetDockingTile(t
, false);
393 t
.m5() = WBL_TYPE_NORMAL
<< WBL_TYPE_BEGIN
| 1 << WBL_COAST_FLAG
;
399 * Helper function for making a watery tile.
400 * @param t The tile to change into water
401 * @param o The owner of the water
402 * @param wc The class of water the tile has to be
403 * @param random_bits Eventual random bits to be set for this tile
405 inline void MakeWater(Tile t
, Owner o
, WaterClass wc
, uint8_t random_bits
)
407 SetTileType(t
, MP_WATER
);
409 SetWaterClass(t
, wc
);
410 SetDockingTile(t
, false);
413 t
.m4() = random_bits
;
414 t
.m5() = WBL_TYPE_NORMAL
<< WBL_TYPE_BEGIN
;
421 * @param t The tile to change into sea
423 inline void MakeSea(Tile t
)
425 MakeWater(t
, OWNER_WATER
, WATER_CLASS_SEA
, 0);
430 * @param t The tile to change into river
431 * @param random_bits Random bits to be set for this tile
433 inline void MakeRiver(Tile t
, uint8_t random_bits
)
435 MakeWater(t
, OWNER_WATER
, WATER_CLASS_RIVER
, random_bits
);
440 * @param t The tile to change into canal
441 * @param o The owner of the canal
442 * @param random_bits Random bits to be set for this tile
444 inline void MakeCanal(Tile t
, Owner o
, uint8_t random_bits
)
446 assert(o
!= OWNER_WATER
);
447 MakeWater(t
, o
, WATER_CLASS_CANAL
, random_bits
);
451 * Make a ship depot section.
452 * @param t Tile to place the ship depot section.
453 * @param o Owner of the depot.
454 * @param did Depot ID.
455 * @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
456 * @param a Axis of the depot.
457 * @param original_water_class Original water class.
459 inline void MakeShipDepot(Tile t
, Owner o
, DepotID did
, DepotPart part
, Axis a
, WaterClass original_water_class
)
461 SetTileType(t
, MP_WATER
);
463 SetWaterClass(t
, original_water_class
);
464 SetDockingTile(t
, false);
468 t
.m5() = WBL_TYPE_DEPOT
<< WBL_TYPE_BEGIN
| part
<< WBL_DEPOT_PART
| a
<< WBL_DEPOT_AXIS
;
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() = WBL_TYPE_LOCK
<< WBL_TYPE_BEGIN
| part
<< WBL_LOCK_PART_BEGIN
| dir
<< WBL_LOCK_ORIENT_BEGIN
;
498 * @param t Tile to place the water lock section.
499 * @param o Owner of the lock.
500 * @param d Direction of the water lock.
501 * @param wc_lower Original water class of the lower part.
502 * @param wc_upper Original water class of the upper part.
503 * @param wc_middle Original water class of the middle part.
505 inline void MakeLock(Tile t
, Owner o
, DiagDirection d
, WaterClass wc_lower
, WaterClass wc_upper
, WaterClass wc_middle
)
507 TileIndexDiff delta
= TileOffsByDiagDir(d
);
508 Tile lower_tile
= TileIndex(t
) - delta
;
509 Tile upper_tile
= TileIndex(t
) + delta
;
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
);
518 #endif /* WATER_MAP_H */