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). */
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).
53 /** Helper information for extract tool. */
54 template <> struct EnumPropsT
<WaterClass
> : MakeEnumPropsT
<WaterClass
, byte
, WATER_CLASS_SEA
, WATER_CLASS_INVALID
, WATER_CLASS_INVALID
, 2> {};
56 /** Sections of the water depot. */
58 DEPOT_PART_NORTH
= 0, ///< Northern part of a depot.
59 DEPOT_PART_SOUTH
= 1, ///< Southern part of a depot.
63 /** Sections of the water lock. */
65 LOCK_PART_MIDDLE
= 0, ///< Middle part of a lock.
66 LOCK_PART_LOWER
= 1, ///< Lower part of a lock.
67 LOCK_PART_UPPER
= 2, ///< Upper part of a lock.
70 bool IsPossibleDockingTile(TileIndex t
);
73 * Get the water tile type at a tile.
74 * @param t Water tile to query.
75 * @return Water tile type at the tile.
77 static inline WaterTileType
GetWaterTileType(TileIndex t
)
79 assert(IsTileType(t
, MP_WATER
));
81 switch (GB(_m
[t
].m5
, WBL_TYPE_BEGIN
, WBL_TYPE_COUNT
)) {
82 case WBL_TYPE_NORMAL
: return HasBit(_m
[t
].m5
, WBL_COAST_FLAG
) ? WATER_TILE_COAST
: WATER_TILE_CLEAR
;
83 case WBL_TYPE_LOCK
: return WATER_TILE_LOCK
;
84 case WBL_TYPE_DEPOT
: return WATER_TILE_DEPOT
;
85 default: NOT_REACHED();
90 * Checks whether the tile has an waterclass associated.
91 * You can then subsequently call GetWaterClass().
92 * @param t Tile to query.
93 * @return True if the tiletype has a waterclass.
95 static inline bool HasTileWaterClass(TileIndex t
)
97 return IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_INDUSTRY
) || IsTileType(t
, MP_OBJECT
) || IsTileType(t
, MP_TREES
);
101 * Get the water class at a tile.
102 * @param t Water tile to query.
103 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
104 * @return Water class at the tile.
106 static inline WaterClass
GetWaterClass(TileIndex t
)
108 assert(HasTileWaterClass(t
));
109 return (WaterClass
)GB(_m
[t
].m1
, 5, 2);
113 * Set the water class at a tile.
114 * @param t Water tile to change.
115 * @param wc New water class.
116 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
118 static inline void SetWaterClass(TileIndex t
, WaterClass wc
)
120 assert(HasTileWaterClass(t
));
121 SB(_m
[t
].m1
, 5, 2, wc
);
125 * Tests if the tile was built on water.
126 * @param t the tile to check
127 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
128 * @return true iff on water
130 static inline bool IsTileOnWater(TileIndex t
)
132 return (GetWaterClass(t
) != WATER_CLASS_INVALID
);
136 * Is it a plain water tile?
137 * @param t Water tile to query.
138 * @return \c true if any type of clear water like ocean, river, or canal.
139 * @pre IsTileType(t, MP_WATER)
141 static inline bool IsWater(TileIndex t
)
143 return GetWaterTileType(t
) == WATER_TILE_CLEAR
;
147 * Is it a sea water tile?
148 * @param t Water tile to query.
149 * @return \c true if it is a sea water tile.
150 * @pre IsTileType(t, MP_WATER)
152 static inline bool IsSea(TileIndex t
)
154 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_SEA
;
158 * Is it a canal tile?
159 * @param t Water tile to query.
160 * @return \c true if it is a canal tile.
161 * @pre IsTileType(t, MP_WATER)
163 static inline bool IsCanal(TileIndex t
)
165 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_CANAL
;
169 * Is it a river water tile?
170 * @param t Water tile to query.
171 * @return \c true if it is a river water tile.
172 * @pre IsTileType(t, MP_WATER)
174 static inline bool IsRiver(TileIndex t
)
176 return IsWater(t
) && GetWaterClass(t
) == WATER_CLASS_RIVER
;
180 * Is it a water tile with plain water?
181 * @param t Tile to query.
182 * @return \c true if it is a plain water tile.
184 static inline bool IsWaterTile(TileIndex t
)
186 return IsTileType(t
, MP_WATER
) && IsWater(t
);
190 * Is it a coast tile?
191 * @param t Water tile to query.
192 * @return \c true if it is a sea water tile.
193 * @pre IsTileType(t, MP_WATER)
195 static inline bool IsCoast(TileIndex t
)
197 return GetWaterTileType(t
) == WATER_TILE_COAST
;
202 * @param t Tile to query.
203 * @return \c true if it is a coast.
205 static inline bool IsCoastTile(TileIndex t
)
207 return (IsTileType(t
, MP_WATER
) && IsCoast(t
)) || (IsTileType(t
, MP_TREES
) && GetWaterClass(t
) != WATER_CLASS_INVALID
);
211 * Is it a water tile with a ship depot on it?
212 * @param t Water tile to query.
213 * @return \c true if it is a ship depot tile.
214 * @pre IsTileType(t, MP_WATER)
216 static inline bool IsShipDepot(TileIndex t
)
218 return GetWaterTileType(t
) == WATER_TILE_DEPOT
;
222 * Is it a ship depot tile?
223 * @param t Tile to query.
224 * @return \c true if it is a ship depot tile.
226 static inline bool IsShipDepotTile(TileIndex t
)
228 return IsTileType(t
, MP_WATER
) && IsShipDepot(t
);
232 * Get the axis of the ship depot.
233 * @param t Water tile to query.
234 * @return Axis of the depot.
235 * @pre IsShipDepotTile(t)
237 static inline Axis
GetShipDepotAxis(TileIndex t
)
239 assert(IsShipDepotTile(t
));
240 return (Axis
)GB(_m
[t
].m5
, WBL_DEPOT_AXIS
, 1);
244 * Get the part of a ship depot.
245 * @param t Water tile to query.
246 * @return Part of the depot.
247 * @pre IsShipDepotTile(t)
249 static inline DepotPart
GetShipDepotPart(TileIndex t
)
251 assert(IsShipDepotTile(t
));
252 return (DepotPart
)GB(_m
[t
].m5
, WBL_DEPOT_PART
, 1);
256 * Get the direction of the ship depot.
257 * @param t Water tile to query.
258 * @return Direction of the depot.
259 * @pre IsShipDepotTile(t)
261 static inline DiagDirection
GetShipDepotDirection(TileIndex t
)
263 return XYNSToDiagDir(GetShipDepotAxis(t
), GetShipDepotPart(t
));
267 * Get the other tile of the ship depot.
268 * @param t Tile to query, containing one section of a ship depot.
269 * @return Tile containing the other section of the depot.
270 * @pre IsShipDepotTile(t)
272 static inline TileIndex
GetOtherShipDepotTile(TileIndex t
)
274 return t
+ (GetShipDepotPart(t
) != DEPOT_PART_NORTH
? -1 : 1) * (GetShipDepotAxis(t
) != AXIS_X
? TileDiffXY(0, 1) : TileDiffXY(1, 0));
278 * Get the most northern tile of a ship depot.
279 * @param t One of the tiles of the ship depot.
280 * @return The northern tile of the depot.
281 * @pre IsShipDepotTile(t)
283 static inline TileIndex
GetShipDepotNorthTile(TileIndex t
)
285 assert(IsShipDepot(t
));
286 TileIndex tile2
= GetOtherShipDepotTile(t
);
288 return t
< tile2
? t
: tile2
;
292 * Is there a lock on a given water tile?
293 * @param t Water tile to query.
294 * @return \c true if it is a water lock tile.
295 * @pre IsTileType(t, MP_WATER)
297 static inline bool IsLock(TileIndex t
)
299 return GetWaterTileType(t
) == WATER_TILE_LOCK
;
303 * Get the direction of the water lock.
304 * @param t Water tile to query.
305 * @return Direction of the lock.
306 * @pre IsTileType(t, MP_WATER) && IsLock(t)
308 static inline DiagDirection
GetLockDirection(TileIndex t
)
311 return (DiagDirection
)GB(_m
[t
].m5
, WBL_LOCK_ORIENT_BEGIN
, WBL_LOCK_ORIENT_COUNT
);
315 * Get the part of a lock.
316 * @param t Water tile to query.
318 * @pre IsTileType(t, MP_WATER) && IsLock(t)
320 static inline byte
GetLockPart(TileIndex t
)
323 return GB(_m
[t
].m5
, WBL_LOCK_PART_BEGIN
, WBL_LOCK_PART_COUNT
);
327 * Get the random bits of the water tile.
328 * @param t Water tile to query.
329 * @return Random bits of the tile.
330 * @pre IsTileType(t, MP_WATER)
332 static inline byte
GetWaterTileRandomBits(TileIndex t
)
334 assert(IsTileType(t
, MP_WATER
));
339 * Checks whether the tile has water at the ground.
340 * That is, it is either some plain water tile, or a object/industry/station/... with water under it.
341 * @return true iff the tile has water at the ground.
342 * @note Coast tiles are not considered waterish, even if there is water on a halftile.
344 static inline bool HasTileWaterGround(TileIndex t
)
346 return HasTileWaterClass(t
) && IsTileOnWater(t
) && !IsCoastTile(t
);
350 * Set the docking tile state of a tile. This is used by pathfinders to reach their destination.
351 * As well as water tiles, half-rail tiles, buoys and aqueduct ends can also be docking tiles.
353 * @param b the docking tile state
355 static inline void SetDockingTile(TileIndex t
, bool b
)
357 assert(IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
));
358 SB(_m
[t
].m1
, 7, 1, b
? 1 : 0);
362 * Checks whether the tile is marked as a dockling tile.
363 * @return true iff the tile is marked as a docking tile.
365 static inline bool IsDockingTile(TileIndex t
)
367 return (IsTileType(t
, MP_WATER
) || IsTileType(t
, MP_RAILWAY
) || IsTileType(t
, MP_STATION
) || IsTileType(t
, MP_TUNNELBRIDGE
)) && HasBit(_m
[t
].m1
, 7);
372 * Helper function to make a coast tile.
373 * @param t The tile to change into water
375 static inline void MakeShore(TileIndex t
)
377 SetTileType(t
, MP_WATER
);
378 SetTileOwner(t
, OWNER_WATER
);
379 SetWaterClass(t
, WATER_CLASS_SEA
);
380 SetDockingTile(t
, false);
384 _m
[t
].m5
= WBL_TYPE_NORMAL
<< WBL_TYPE_BEGIN
| 1 << WBL_COAST_FLAG
;
385 SB(_me
[t
].m6
, 2, 4, 0);
390 * Helper function for making a watery tile.
391 * @param t The tile to change into water
392 * @param o The owner of the water
393 * @param wc The class of water the tile has to be
394 * @param random_bits Eventual random bits to be set for this tile
396 static inline void MakeWater(TileIndex t
, Owner o
, WaterClass wc
, uint8 random_bits
)
398 SetTileType(t
, MP_WATER
);
400 SetWaterClass(t
, wc
);
401 SetDockingTile(t
, false);
404 _m
[t
].m4
= random_bits
;
405 _m
[t
].m5
= WBL_TYPE_NORMAL
<< WBL_TYPE_BEGIN
;
406 SB(_me
[t
].m6
, 2, 4, 0);
412 * @param t The tile to change into sea
414 static inline void MakeSea(TileIndex t
)
416 MakeWater(t
, OWNER_WATER
, WATER_CLASS_SEA
, 0);
421 * @param t The tile to change into river
422 * @param random_bits Random bits to be set for this tile
424 static inline void MakeRiver(TileIndex t
, uint8 random_bits
)
426 MakeWater(t
, OWNER_WATER
, WATER_CLASS_RIVER
, random_bits
);
431 * @param t The tile to change into canal
432 * @param o The owner of the canal
433 * @param random_bits Random bits to be set for this tile
435 static inline void MakeCanal(TileIndex t
, Owner o
, uint8 random_bits
)
437 assert(o
!= OWNER_WATER
);
438 MakeWater(t
, o
, WATER_CLASS_CANAL
, random_bits
);
442 * Make a ship depot section.
443 * @param t Tile to place the ship depot section.
444 * @param o Owner of the depot.
445 * @param did Depot ID.
446 * @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
447 * @param a Axis of the depot.
448 * @param original_water_class Original water class.
450 static inline void MakeShipDepot(TileIndex t
, Owner o
, DepotID did
, DepotPart part
, Axis a
, WaterClass original_water_class
)
452 SetTileType(t
, MP_WATER
);
454 SetWaterClass(t
, original_water_class
);
455 SetDockingTile(t
, false);
459 _m
[t
].m5
= WBL_TYPE_DEPOT
<< WBL_TYPE_BEGIN
| part
<< WBL_DEPOT_PART
| a
<< WBL_DEPOT_AXIS
;
460 SB(_me
[t
].m6
, 2, 4, 0);
465 * Make a lock section.
466 * @param t Tile to place the water lock section.
467 * @param o Owner of the lock.
468 * @param part Part to place.
469 * @param dir Lock orientation
470 * @param original_water_class Original water class.
473 static inline void MakeLockTile(TileIndex t
, Owner o
, LockPart part
, DiagDirection dir
, WaterClass original_water_class
)
475 SetTileType(t
, MP_WATER
);
477 SetWaterClass(t
, original_water_class
);
478 SetDockingTile(t
, false);
482 _m
[t
].m5
= WBL_TYPE_LOCK
<< WBL_TYPE_BEGIN
| part
<< WBL_LOCK_PART_BEGIN
| dir
<< WBL_LOCK_ORIENT_BEGIN
;
483 SB(_me
[t
].m6
, 2, 4, 0);
489 * @param t Tile to place the water lock section.
490 * @param o Owner of the lock.
491 * @param d Direction of the water lock.
492 * @param wc_lower Original water class of the lower part.
493 * @param wc_upper Original water class of the upper part.
494 * @param wc_middle Original water class of the middle part.
496 static inline void MakeLock(TileIndex t
, Owner o
, DiagDirection d
, WaterClass wc_lower
, WaterClass wc_upper
, WaterClass wc_middle
)
498 TileIndexDiff delta
= TileOffsByDiagDir(d
);
500 /* Keep the current waterclass and owner for the tiles.
501 * It allows to restore them after the lock is deleted */
502 MakeLockTile(t
, o
, LOCK_PART_MIDDLE
, d
, wc_middle
);
503 MakeLockTile(t
- delta
, IsWaterTile(t
- delta
) ? GetTileOwner(t
- delta
) : o
, LOCK_PART_LOWER
, d
, wc_lower
);
504 MakeLockTile(t
+ delta
, IsWaterTile(t
+ delta
) ? GetTileOwner(t
+ delta
) : o
, LOCK_PART_UPPER
, d
, wc_upper
);
507 #endif /* WATER_MAP_H */