Fix incorrect tile and trackdir in reserve through program execution
[openttd-joker.git] / src / water_map.h
blob242a6dd2d1ed14e06838adefc98851e861aaf2a0
1 /* $Id: water_map.h 23512 2011-12-13 23:01:36Z planetmaker $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file water_map.h Map accessors for water tiles. */
12 #ifndef WATER_MAP_H
13 #define WATER_MAP_H
15 #include "depot_type.h"
16 #include "tile_map.h"
17 #include "tree_map.h"
19 /**
20 * Bit field layout of m5 for water tiles.
22 enum WaterTileTypeBitLayout {
23 WBL_TYPE_BEGIN = 4, ///< Start of the 'type' bitfield.
24 WBL_TYPE_COUNT = 4, ///< Length of the 'type' bitfield.
26 WBL_TYPE_NORMAL = 0x0, ///< Clear water or coast ('type' bitfield).
27 WBL_TYPE_LOCK = 0x1, ///< Lock ('type' bitfield).
28 WBL_TYPE_DEPOT = 0x8, ///< Depot ('type' bitfield).
30 WBL_COAST_FLAG = 0, ///< Flag for coast.
32 WBL_LOCK_ORIENT_BEGIN = 0, ///< Start of lock orientiation bitfield.
33 WBL_LOCK_ORIENT_COUNT = 2, ///< Length of lock orientiation bitfield.
34 WBL_LOCK_PART_BEGIN = 2, ///< Start of lock part bitfield.
35 WBL_LOCK_PART_COUNT = 2, ///< Length of lock part bitfield.
37 WBL_DEPOT_PART = 0, ///< Depot part flag.
38 WBL_DEPOT_AXIS = 1, ///< Depot axis flag.
41 /** Available water tile types. */
42 enum WaterTileType {
43 WATER_TILE_CLEAR, ///< Plain water.
44 WATER_TILE_COAST, ///< Coast.
45 WATER_TILE_LOCK, ///< Water lock.
46 WATER_TILE_DEPOT, ///< Water Depot.
49 /** classes of water (for #WATER_TILE_CLEAR water tile type). */
50 enum WaterClass {
51 WATER_CLASS_SEA, ///< Sea.
52 WATER_CLASS_CANAL, ///< Canal.
53 WATER_CLASS_RIVER, ///< River.
54 WATER_CLASS_INVALID, ///< Used for industry tiles on land (also for oilrig if newgrf says so).
56 /** Helper information for extract tool. */
57 template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
59 /** Sections of the water depot. */
60 enum DepotPart {
61 DEPOT_PART_NORTH = 0, ///< Northern part of a depot.
62 DEPOT_PART_SOUTH = 1, ///< Southern part of a depot.
63 DEPOT_PART_END
66 /** Sections of the water lock. */
67 enum LockPart {
68 LOCK_PART_MIDDLE = 0, ///< Middle part of a lock.
69 LOCK_PART_LOWER = 1, ///< Lower part of a lock.
70 LOCK_PART_UPPER = 2, ///< Upper part of a lock.
73 /**
74 * Get the water tile type at a tile.
75 * @param t Water tile to query.
76 * @return Water tile type at the tile.
78 static inline WaterTileType GetWaterTileType(TileIndex t)
80 if (IsTileType(t, MP_TUNNELBRIDGE))
81 return WATER_TILE_CLEAR;
83 assert(IsTileType(t, MP_WATER));
85 switch (GB(_m[t].m5, WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
86 case WBL_TYPE_NORMAL: return HasBit(_m[t].m5, WBL_COAST_FLAG) ? WATER_TILE_COAST : WATER_TILE_CLEAR;
87 case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
88 case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
89 default: NOT_REACHED();
93 /**
94 * Checks whether the tile has an waterclass associated.
95 * You can then subsequently call GetWaterClass().
96 * @param t Tile to query.
97 * @return True if the tiletype has a waterclass.
99 static inline bool HasTileWaterClass(TileIndex t)
101 return IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT);
105 * Get the water class at a tile.
106 * @param t Water tile to query.
107 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
108 * @return Water class at the tile.
110 static inline WaterClass GetWaterClass(TileIndex t)
112 assert(HasTileWaterClass(t));
113 return (WaterClass)GB(_m[t].m1, 5, 2);
117 * Set the water class at a tile.
118 * @param t Water tile to change.
119 * @param wc New water class.
120 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
122 static inline void SetWaterClass(TileIndex t, WaterClass wc)
124 assert(HasTileWaterClass(t));
125 SB(_m[t].m1, 5, 2, wc);
129 * Tests if the tile was built on water.
130 * @param t the tile to check
131 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
132 * @return true iff on water
134 static inline bool IsTileOnWater(TileIndex t)
136 return (GetWaterClass(t) != WATER_CLASS_INVALID);
140 * Is it a plain water tile?
141 * @param t Water tile to query.
142 * @return \c true if any type of clear water like ocean, river, or canal.
143 * @pre IsTileType(t, MP_WATER)
145 static inline bool IsWater(TileIndex t)
147 return GetWaterTileType(t) == WATER_TILE_CLEAR;
151 * Is it a sea water tile?
152 * @param t Water tile to query.
153 * @return \c true if it is a sea water tile.
154 * @pre IsTileType(t, MP_WATER)
156 static inline bool IsSea(TileIndex t)
158 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
162 * Is it a canal tile?
163 * @param t Water tile to query.
164 * @return \c true if it is a canal tile.
165 * @pre IsTileType(t, MP_WATER)
167 static inline bool IsCanal(TileIndex t)
169 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
173 * Is it a river water tile?
174 * @param t Water tile to query.
175 * @return \c true if it is a river water tile.
176 * @pre IsTileType(t, MP_WATER)
178 static inline bool IsRiver(TileIndex t)
180 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
184 * Is it a water tile with plain water?
185 * @param t Tile to query.
186 * @return \c true if it is a plain water tile.
188 static inline bool IsWaterTile(TileIndex t)
190 return IsTileType(t, MP_WATER) && IsWater(t);
194 * Is it a coast tile?
195 * @param t Water tile to query.
196 * @return \c true if it is a sea water tile.
197 * @pre IsTileType(t, MP_WATER)
199 static inline bool IsCoast(TileIndex t)
201 return GetWaterTileType(t) == WATER_TILE_COAST;
205 * Is it a coast tile
206 * @param t Tile to query.
207 * @return \c true if it is a coast.
209 static inline bool IsCoastTile(TileIndex t)
211 return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetTreeGround(t) == TREE_GROUND_SHORE);
215 * Is it a water tile with a ship depot on it?
216 * @param t Water tile to query.
217 * @return \c true if it is a ship depot tile.
218 * @pre IsTileType(t, MP_WATER)
220 static inline bool IsShipDepot(TileIndex t)
222 return GetWaterTileType(t) == WATER_TILE_DEPOT;
226 * Is it a ship depot tile?
227 * @param t Tile to query.
228 * @return \c true if it is a ship depot tile.
230 static inline bool IsShipDepotTile(TileIndex t)
232 return IsTileType(t, MP_WATER) && IsShipDepot(t);
236 * Get the axis of the ship depot.
237 * @param t Water tile to query.
238 * @return Axis of the depot.
239 * @pre IsShipDepotTile(t)
241 static inline Axis GetShipDepotAxis(TileIndex t)
243 assert(IsShipDepotTile(t));
244 return (Axis)GB(_m[t].m5, WBL_DEPOT_AXIS, 1);
248 * Get the part of a ship depot.
249 * @param t Water tile to query.
250 * @return Part of the depot.
251 * @pre IsShipDepotTile(t)
253 static inline DepotPart GetShipDepotPart(TileIndex t)
255 assert(IsShipDepotTile(t));
256 return (DepotPart)GB(_m[t].m5, WBL_DEPOT_PART, 1);
260 * Get the direction of the ship depot.
261 * @param t Water tile to query.
262 * @return Direction of the depot.
263 * @pre IsShipDepotTile(t)
265 static inline DiagDirection GetShipDepotDirection(TileIndex t)
267 return XYNSToDiagDir(GetShipDepotAxis(t), GetShipDepotPart(t));
271 * Get the other tile of the ship depot.
272 * @param t Tile to query, containing one section of a ship depot.
273 * @return Tile containing the other section of the depot.
274 * @pre IsShipDepotTile(t)
276 static inline TileIndex GetOtherShipDepotTile(TileIndex t)
278 return t + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
282 * Get the most northern tile of a ship depot.
283 * @param t One of the tiles of the ship depot.
284 * @return The northern tile of the depot.
285 * @pre IsShipDepotTile(t)
287 static inline TileIndex GetShipDepotNorthTile(TileIndex t)
289 assert(IsShipDepot(t));
290 TileIndex tile2 = GetOtherShipDepotTile(t);
292 return t < tile2 ? t : tile2;
296 * Is there a lock on a given water tile?
297 * @param t Water tile to query.
298 * @return \c true if it is a water lock tile.
299 * @pre IsTileType(t, MP_WATER)
301 static inline bool IsLock(TileIndex t)
303 return GetWaterTileType(t) == WATER_TILE_LOCK;
307 * Get the direction of the water lock.
308 * @param t Water tile to query.
309 * @return Direction of the lock.
310 * @pre IsTileType(t, MP_WATER) && IsLock(t)
312 static inline DiagDirection GetLockDirection(TileIndex t)
314 assert(IsLock(t));
315 return (DiagDirection)GB(_m[t].m5, WBL_LOCK_ORIENT_BEGIN, WBL_LOCK_ORIENT_COUNT);
319 * Get the part of a lock.
320 * @param t Water tile to query.
321 * @return The part.
322 * @pre IsTileType(t, MP_WATER) && IsLock(t)
324 static inline byte GetLockPart(TileIndex t)
326 assert(IsLock(t));
327 return GB(_m[t].m5, WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT);
331 * Get the random bits of the water tile.
332 * @param t Water tile to query.
333 * @return Random bits of the tile.
334 * @pre IsTileType(t, MP_WATER)
336 static inline byte GetWaterTileRandomBits(TileIndex t)
338 assert(IsTileType(t, MP_WATER));
339 return _m[t].m4;
343 * Checks whether the tile has water at the ground.
344 * That is, it is either some plain water tile, or a object/industry/station/... with water under it.
345 * @return true iff the tile has water at the ground.
346 * @note Coast tiles are not considered waterish, even if there is water on a halftile.
348 static inline bool HasTileWaterGround(TileIndex t)
350 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
355 * Helper function to make a coast tile.
356 * @param t The tile to change into water
358 static inline void MakeShore(TileIndex t)
360 SetTileType(t, MP_WATER);
361 SetTileOwner(t, OWNER_WATER);
362 SetWaterClass(t, WATER_CLASS_SEA);
363 _m[t].m2 = 0;
364 _m[t].m3 = 0;
365 _m[t].m4 = 0;
366 _m[t].m5 = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN | 1 << WBL_COAST_FLAG;
367 SB(_me[t].m6, 2, 4, 0);
368 _me[t].m7 = 0;
372 * Helper function for making a watery tile.
373 * @param t The tile to change into water
374 * @param o The owner of the water
375 * @param wc The class of water the tile has to be
376 * @param random_bits Eventual random bits to be set for this tile
378 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
380 SetTileType(t, MP_WATER);
381 SetTileOwner(t, o);
382 SetWaterClass(t, wc);
383 _m[t].m2 = 0;
384 _m[t].m3 = 0;
385 _m[t].m4 = random_bits;
386 _m[t].m5 = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN;
387 SB(_me[t].m6, 2, 4, 0);
388 _me[t].m7 = 0;
392 * Make a sea tile.
393 * @param t The tile to change into sea
395 static inline void MakeSea(TileIndex t)
397 MakeWater(t, OWNER_WATER, WATER_CLASS_SEA, 0);
401 * Make a river tile
402 * @param t The tile to change into river
403 * @param random_bits Random bits to be set for this tile
405 static inline void MakeRiver(TileIndex t, uint8 random_bits)
407 MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
411 * Make a canal tile
412 * @param t The tile to change into canal
413 * @param o The owner of the canal
414 * @param random_bits Random bits to be set for this tile
416 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
418 assert(o != OWNER_WATER);
419 MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
423 * Make a ship depot section.
424 * @param t Tile to place the ship depot section.
425 * @param o Owner of the depot.
426 * @param did Depot ID.
427 * @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
428 * @param a Axis of the depot.
429 * @param original_water_class Original water class.
431 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
433 SetTileType(t, MP_WATER);
434 SetTileOwner(t, o);
435 SetWaterClass(t, original_water_class);
436 _m[t].m2 = did;
437 _m[t].m3 = 0;
438 _m[t].m4 = 0;
439 _m[t].m5 = WBL_TYPE_DEPOT << WBL_TYPE_BEGIN | part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
440 SB(_me[t].m6, 2, 4, 0);
441 _me[t].m7 = 0;
445 * Make a lock section.
446 * @param t Tile to place the water lock section.
447 * @param o Owner of the lock.
448 * @param part Part to place.
449 * @param dir Lock orientation
450 * @param original_water_class Original water class.
451 * @see MakeLock
453 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
455 SetTileType(t, MP_WATER);
456 SetTileOwner(t, o);
457 SetWaterClass(t, original_water_class);
458 _m[t].m2 = 0;
459 _m[t].m3 = 0;
460 _m[t].m4 = 0;
461 _m[t].m5 = WBL_TYPE_LOCK << WBL_TYPE_BEGIN | part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
462 SB(_me[t].m6, 2, 4, 0);
463 _me[t].m7 = 0;
467 * Make a water lock.
468 * @param t Tile to place the water lock section.
469 * @param o Owner of the lock.
470 * @param d Direction of the water lock.
471 * @param wc_lower Original water class of the lower part.
472 * @param wc_upper Original water class of the upper part.
473 * @param wc_middle Original water class of the middle part.
475 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
477 TileIndexDiff delta = TileOffsByDiagDir(d);
479 /* Keep the current waterclass and owner for the tiles.
480 * It allows to restore them after the lock is deleted */
481 MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
482 MakeLockTile(t - delta, IsWaterTile(t - delta) ? GetTileOwner(t - delta) : o, LOCK_PART_LOWER, d, wc_lower);
483 MakeLockTile(t + delta, IsWaterTile(t + delta) ? GetTileOwner(t + delta) : o, LOCK_PART_UPPER, d, wc_upper);
486 #endif /* WATER_MAP_H */