Fix 03cc0d6: Mark level crossings dirty when removing road from them, not from bridge...
[openttd-github.git] / src / road_map.h
blob6be02e42f623ecd9c8265f5ec2c564afd7c8ca10
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 */
8 /** @file road_map.h Map accessors for roads. */
10 #ifndef ROAD_MAP_H
11 #define ROAD_MAP_H
13 #include "track_func.h"
14 #include "depot_type.h"
15 #include "rail_type.h"
16 #include "road_func.h"
17 #include "tile_map.h"
18 #include "road_type.h"
21 /** The different types of road tiles. */
22 enum RoadTileType {
23 ROAD_TILE_NORMAL, ///< Normal road
24 ROAD_TILE_CROSSING, ///< Level crossing
25 ROAD_TILE_DEPOT, ///< Depot (one entrance)
28 /**
29 * Test whether a tile can have road/tram types.
30 * @param t Tile to query.
31 * @return true if tile can be queried about road/tram types.
33 static inline bool MayHaveRoad(TileIndex t)
35 switch (GetTileType(t)) {
36 case MP_ROAD:
37 case MP_STATION:
38 case MP_TUNNELBRIDGE:
39 return true;
41 default:
42 return false;
46 /**
47 * Get the type of the road tile.
48 * @param t Tile to query.
49 * @pre IsTileType(t, MP_ROAD)
50 * @return The road tile type.
52 static inline RoadTileType GetRoadTileType(TileIndex t)
54 assert(IsTileType(t, MP_ROAD));
55 return (RoadTileType)GB(_m[t].m5, 6, 2);
58 /**
59 * Return whether a tile is a normal road.
60 * @param t Tile to query.
61 * @pre IsTileType(t, MP_ROAD)
62 * @return True if normal road.
64 static inline bool IsNormalRoad(TileIndex t)
66 return GetRoadTileType(t) == ROAD_TILE_NORMAL;
69 /**
70 * Return whether a tile is a normal road tile.
71 * @param t Tile to query.
72 * @return True if normal road tile.
74 static inline bool IsNormalRoadTile(TileIndex t)
76 return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
79 /**
80 * Return whether a tile is a level crossing.
81 * @param t Tile to query.
82 * @pre IsTileType(t, MP_ROAD)
83 * @return True if level crossing.
85 static inline bool IsLevelCrossing(TileIndex t)
87 return GetRoadTileType(t) == ROAD_TILE_CROSSING;
90 /**
91 * Return whether a tile is a level crossing tile.
92 * @param t Tile to query.
93 * @return True if level crossing tile.
95 static inline bool IsLevelCrossingTile(TileIndex t)
97 return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
101 * Return whether a tile is a road depot.
102 * @param t Tile to query.
103 * @pre IsTileType(t, MP_ROAD)
104 * @return True if road depot.
106 static inline bool IsRoadDepot(TileIndex t)
108 return GetRoadTileType(t) == ROAD_TILE_DEPOT;
112 * Return whether a tile is a road depot tile.
113 * @param t Tile to query.
114 * @return True if road depot tile.
116 static inline bool IsRoadDepotTile(TileIndex t)
118 return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
122 * Get the present road bits for a specific road type.
123 * @param t The tile to query.
124 * @param rt Road type.
125 * @pre IsNormalRoad(t)
126 * @return The present road bits for the road type.
128 static inline RoadBits GetRoadBits(TileIndex t, RoadTramType rtt)
130 assert(IsNormalRoad(t));
131 if (rtt == RTT_TRAM) return (RoadBits)GB(_m[t].m3, 0, 4);
132 return (RoadBits)GB(_m[t].m5, 0, 4);
136 * Get all set RoadBits on the given tile
138 * @param tile The tile from which we want to get the RoadBits
139 * @return all set RoadBits of the tile
141 static inline RoadBits GetAllRoadBits(TileIndex tile)
143 return GetRoadBits(tile, RTT_ROAD) | GetRoadBits(tile, RTT_TRAM);
147 * Set the present road bits for a specific road type.
148 * @param t The tile to change.
149 * @param r The new road bits.
150 * @param rt Road type.
151 * @pre IsNormalRoad(t)
153 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadTramType rtt)
155 assert(IsNormalRoad(t)); // XXX incomplete
156 if (rtt == RTT_TRAM) {
157 SB(_m[t].m3, 0, 4, r);
158 } else {
159 SB(_m[t].m5, 0, 4, r);
163 static inline RoadType GetRoadTypeRoad(TileIndex t)
165 assert(MayHaveRoad(t));
166 return (RoadType)GB(_m[t].m4, 0, 6);
169 static inline RoadType GetRoadTypeTram(TileIndex t)
171 assert(MayHaveRoad(t));
172 return (RoadType)GB(_me[t].m8, 6, 6);
175 static inline RoadType GetRoadType(TileIndex t, RoadTramType rtt)
177 return (rtt == RTT_TRAM) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
181 * Get the present road types of a tile.
182 * @param t The tile to query.
183 * @return Present road types.
185 static inline RoadTypes GetPresentRoadTypes(TileIndex t)
187 RoadTypes result = ROADTYPES_NONE;
188 if (MayHaveRoad(t)) {
189 if (GetRoadTypeRoad(t) != INVALID_ROADTYPE) SetBit(result, GetRoadTypeRoad(t));
190 if (GetRoadTypeTram(t) != INVALID_ROADTYPE) SetBit(result, GetRoadTypeTram(t));
192 return result;
195 static inline bool HasRoadTypeRoad(TileIndex t)
197 return GetRoadTypeRoad(t) != INVALID_ROADTYPE;
200 static inline bool HasRoadTypeTram(TileIndex t)
202 return GetRoadTypeTram(t) != INVALID_ROADTYPE;
206 * Check if a tile has a road or a tram road type.
207 * @param t The tile to check.
208 * @param tram True to check tram, false to check road.
209 * @return True if the tile has the specified road type.
211 static inline bool HasTileRoadType(TileIndex t, RoadTramType rtt)
213 return GetRoadType(t, rtt) != INVALID_ROADTYPE;
217 * Check if a tile has one of the specified road types.
218 * @param t The tile to check.
219 * @param rts Allowed road types.
220 * @return True if the tile has one of the specified road types.
222 static inline bool HasTileAnyRoadType(TileIndex t, RoadTypes rts)
224 if (!MayHaveRoad(t)) return false;
225 return (GetPresentRoadTypes(t) & rts);
229 * Get the owner of a specific road type.
230 * @param t The tile to query.
231 * @param rtt RoadTramType.
232 * @return Owner of the given road type.
234 static inline Owner GetRoadOwner(TileIndex t, RoadTramType rtt)
236 assert(MayHaveRoad(t));
237 if (rtt == RTT_ROAD) return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
239 /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
240 * to OWNER_TOWN makes it use one bit less */
241 Owner o = (Owner)GB(_m[t].m3, 4, 4);
242 return o == OWNER_TOWN ? OWNER_NONE : o;
246 * Set the owner of a specific road type.
247 * @param t The tile to change.
248 * @param rtt RoadTramType.
249 * @param o New owner of the given road type.
251 static inline void SetRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
253 if (rtt == RTT_ROAD) {
254 SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o);
255 } else {
256 SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o);
261 * Check if a specific road type is owned by an owner.
262 * @param t The tile to query.
263 * @param tram True to check tram, false to check road.
264 * @param o Owner to compare with.
265 * @pre HasTileRoadType(t, rt)
266 * @return True if the road type is owned by the given owner.
268 static inline bool IsRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
270 assert(HasTileRoadType(t, rtt));
271 return (GetRoadOwner(t, rtt) == o);
275 * Checks if given tile has town owned road
276 * @param t tile to check
277 * @pre IsTileType(t, MP_ROAD)
278 * @return true iff tile has road and the road is owned by a town
280 static inline bool HasTownOwnedRoad(TileIndex t)
282 return HasTileRoadType(t, RTT_ROAD) && IsRoadOwner(t, RTT_ROAD, OWNER_TOWN);
286 * Checks if a DisallowedRoadDirections is valid.
288 * @param wc The value to check
289 * @return true if the given value is a valid DisallowedRoadDirections.
291 static inline bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt)
293 return drt < DRD_END;
297 * Gets the disallowed directions
298 * @param t the tile to get the directions from
299 * @return the disallowed directions
301 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
303 assert(IsNormalRoad(t));
304 return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
308 * Sets the disallowed directions
309 * @param t the tile to set the directions for
310 * @param drd the disallowed directions
312 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
314 assert(IsNormalRoad(t));
315 assert(drd < DRD_END);
316 SB(_m[t].m5, 4, 2, drd);
320 * Get the road axis of a level crossing.
321 * @param t The tile to query.
322 * @pre IsLevelCrossing(t)
323 * @return The axis of the road.
325 static inline Axis GetCrossingRoadAxis(TileIndex t)
327 assert(IsLevelCrossing(t));
328 return (Axis)GB(_m[t].m5, 0, 1);
332 * Get the rail axis of a level crossing.
333 * @param t The tile to query.
334 * @pre IsLevelCrossing(t)
335 * @return The axis of the rail.
337 static inline Axis GetCrossingRailAxis(TileIndex t)
339 assert(IsLevelCrossing(t));
340 return OtherAxis((Axis)GetCrossingRoadAxis(t));
344 * Get the road bits of a level crossing.
345 * @param tile The tile to query.
346 * @return The present road bits.
348 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
350 return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
354 * Get the rail track of a level crossing.
355 * @param tile The tile to query.
356 * @return The rail track.
358 static inline Track GetCrossingRailTrack(TileIndex tile)
360 return AxisToTrack(GetCrossingRailAxis(tile));
364 * Get the rail track bits of a level crossing.
365 * @param tile The tile to query.
366 * @return The rail track bits.
368 static inline TrackBits GetCrossingRailBits(TileIndex tile)
370 return AxisToTrackBits(GetCrossingRailAxis(tile));
375 * Get the reservation state of the rail crossing
376 * @param t the crossing tile
377 * @return reservation state
378 * @pre IsLevelCrossingTile(t)
380 static inline bool HasCrossingReservation(TileIndex t)
382 assert(IsLevelCrossingTile(t));
383 return HasBit(_m[t].m5, 4);
387 * Set the reservation state of the rail crossing
388 * @note Works for both waypoints and rail depots
389 * @param t the crossing tile
390 * @param b the reservation state
391 * @pre IsLevelCrossingTile(t)
393 static inline void SetCrossingReservation(TileIndex t, bool b)
395 assert(IsLevelCrossingTile(t));
396 SB(_m[t].m5, 4, 1, b ? 1 : 0);
400 * Get the reserved track bits for a rail crossing
401 * @param t the tile
402 * @pre IsLevelCrossingTile(t)
403 * @return reserved track bits
405 static inline TrackBits GetCrossingReservationTrackBits(TileIndex t)
407 return HasCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
411 * Check if the level crossing is barred.
412 * @param t The tile to query.
413 * @pre IsLevelCrossing(t)
414 * @return True if the level crossing is barred.
416 static inline bool IsCrossingBarred(TileIndex t)
418 assert(IsLevelCrossing(t));
419 return HasBit(_m[t].m5, 5);
423 * Set the bar state of a level crossing.
424 * @param t The tile to modify.
425 * @param barred True if the crossing should be barred, false otherwise.
426 * @pre IsLevelCrossing(t)
428 static inline void SetCrossingBarred(TileIndex t, bool barred)
430 assert(IsLevelCrossing(t));
431 SB(_m[t].m5, 5, 1, barred ? 1 : 0);
435 * Unbar a level crossing.
436 * @param t The tile to change.
438 static inline void UnbarCrossing(TileIndex t)
440 SetCrossingBarred(t, false);
444 * Bar a level crossing.
445 * @param t The tile to change.
447 static inline void BarCrossing(TileIndex t)
449 SetCrossingBarred(t, true);
452 /** Check if a road tile has snow/desert. */
453 #define IsOnDesert IsOnSnow
455 * Check if a road tile has snow/desert.
456 * @param t The tile to query.
457 * @return True if the tile has snow/desert.
459 static inline bool IsOnSnow(TileIndex t)
461 return HasBit(_me[t].m7, 5);
464 /** Toggle the snow/desert state of a road tile. */
465 #define ToggleDesert ToggleSnow
467 * Toggle the snow/desert state of a road tile.
468 * @param t The tile to change.
470 static inline void ToggleSnow(TileIndex t)
472 ToggleBit(_me[t].m7, 5);
476 /** The possible road side decorations. */
477 enum Roadside {
478 ROADSIDE_BARREN = 0, ///< Road on barren land
479 ROADSIDE_GRASS = 1, ///< Road on grass
480 ROADSIDE_PAVED = 2, ///< Road with paved sidewalks
481 ROADSIDE_STREET_LIGHTS = 3, ///< Road with street lights on paved sidewalks
482 // 4 is unused for historical reasons
483 ROADSIDE_TREES = 5, ///< Road with trees on paved sidewalks
484 ROADSIDE_GRASS_ROAD_WORKS = 6, ///< Road on grass with road works
485 ROADSIDE_PAVED_ROAD_WORKS = 7, ///< Road with sidewalks and road works
489 * Get the decorations of a road.
490 * @param tile The tile to query.
491 * @return The road decoration of the tile.
493 static inline Roadside GetRoadside(TileIndex tile)
495 return (Roadside)GB(_me[tile].m6, 3, 3);
499 * Set the decorations of a road.
500 * @param tile The tile to change.
501 * @param s The new road decoration of the tile.
503 static inline void SetRoadside(TileIndex tile, Roadside s)
505 SB(_me[tile].m6, 3, 3, s);
509 * Check if a tile has road works.
510 * @param t The tile to check.
511 * @return True if the tile has road works in progress.
513 static inline bool HasRoadWorks(TileIndex t)
515 return GetRoadside(t) >= ROADSIDE_GRASS_ROAD_WORKS;
519 * Increase the progress counter of road works.
520 * @param t The tile to modify.
521 * @return True if the road works are in the last stage.
523 static inline bool IncreaseRoadWorksCounter(TileIndex t)
525 AB(_me[t].m7, 0, 4, 1);
527 return GB(_me[t].m7, 0, 4) == 15;
531 * Start road works on a tile.
532 * @param t The tile to start the work on.
533 * @pre !HasRoadWorks(t)
535 static inline void StartRoadWorks(TileIndex t)
537 assert(!HasRoadWorks(t));
538 /* Remove any trees or lamps in case or roadwork */
539 switch (GetRoadside(t)) {
540 case ROADSIDE_BARREN:
541 case ROADSIDE_GRASS: SetRoadside(t, ROADSIDE_GRASS_ROAD_WORKS); break;
542 default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
547 * Terminate road works on a tile.
548 * @param t Tile to stop the road works on.
549 * @pre HasRoadWorks(t)
551 static inline void TerminateRoadWorks(TileIndex t)
553 assert(HasRoadWorks(t));
554 SetRoadside(t, (Roadside)(GetRoadside(t) - ROADSIDE_GRASS_ROAD_WORKS + ROADSIDE_GRASS));
555 /* Stop the counter */
556 SB(_me[t].m7, 0, 4, 0);
561 * Get the direction of the exit of a road depot.
562 * @param t The tile to query.
563 * @return Diagonal direction of the depot exit.
565 static inline DiagDirection GetRoadDepotDirection(TileIndex t)
567 assert(IsRoadDepot(t));
568 return (DiagDirection)GB(_m[t].m5, 0, 2);
572 RoadBits GetAnyRoadBits(TileIndex tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
575 * Set the road road type of a tile.
576 * @param t The tile to change.
577 * @param rt The road type to set.
579 static inline void SetRoadTypeRoad(TileIndex t, RoadType rt)
581 assert(MayHaveRoad(t));
582 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
583 SB(_m[t].m4, 0, 6, rt);
587 * Set the tram road type of a tile.
588 * @param t The tile to change.
589 * @param rt The road type to set.
591 static inline void SetRoadTypeTram(TileIndex t, RoadType rt)
593 assert(MayHaveRoad(t));
594 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
595 SB(_me[t].m8, 6, 6, rt);
599 * Set the road type of a tile.
600 * @param t The tile to change.
601 * @param rtt Set road or tram type.
602 * @param rt The road type to set.
604 static inline void SetRoadType(TileIndex t, RoadTramType rtt, RoadType rt)
606 if (rtt == RTT_TRAM) {
607 SetRoadTypeTram(t, rt);
608 } else {
609 SetRoadTypeRoad(t, rt);
614 * Set the present road types of a tile.
615 * @param t The tile to change.
616 * @param road_rt The road roadtype to set for the tile.
617 * @param tram_rt The tram roadtype to set for the tile.
619 static inline void SetRoadTypes(TileIndex t, RoadType road_rt, RoadType tram_rt)
621 SetRoadTypeRoad(t, road_rt);
622 SetRoadTypeTram(t, tram_rt);
626 * Make a normal road tile.
627 * @param t Tile to make a normal road.
628 * @param bits Road bits to set for all present road types.
629 * @param road_rt The road roadtype to set for the tile.
630 * @param tram_rt The tram roadtype to set for the tile.
631 * @param town Town ID if the road is a town-owned road.
632 * @param road New owner of road.
633 * @param tram New owner of tram tracks.
635 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
637 SetTileType(t, MP_ROAD);
638 SetTileOwner(t, road);
639 _m[t].m2 = town;
640 _m[t].m3 = (tram_rt != INVALID_ROADTYPE ? bits : 0);
641 _m[t].m5 = (road_rt != INVALID_ROADTYPE ? bits : 0) | ROAD_TILE_NORMAL << 6;
642 SB(_me[t].m6, 2, 4, 0);
643 _me[t].m7 = 0;
644 SetRoadTypes(t, road_rt, tram_rt);
645 SetRoadOwner(t, RTT_TRAM, tram);
649 * Make a level crossing.
650 * @param t Tile to make a level crossing.
651 * @param road New owner of road.
652 * @param tram New owner of tram tracks.
653 * @param rail New owner of the rail track.
654 * @param roaddir Axis of the road.
655 * @param rat New rail type.
656 * @param road_rt The road roadtype to set for the tile.
657 * @param tram_rt The tram roadtype to set for the tile.
658 * @param town Town ID if the road is a town-owned road.
660 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, uint town)
662 SetTileType(t, MP_ROAD);
663 SetTileOwner(t, rail);
664 _m[t].m2 = town;
665 _m[t].m3 = 0;
666 _m[t].m4 = INVALID_ROADTYPE;
667 _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
668 SB(_me[t].m6, 2, 4, 0);
669 _me[t].m7 = road;
670 _me[t].m8 = INVALID_ROADTYPE << 6 | rat;
671 SetRoadTypes(t, road_rt, tram_rt);
672 SetRoadOwner(t, RTT_TRAM, tram);
676 * Make a road depot.
677 * @param t Tile to make a level crossing.
678 * @param owner New owner of the depot.
679 * @param did New depot ID.
680 * @param dir Direction of the depot exit.*
681 * @param rt Road type of the depot.
683 static inline void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
685 SetTileType(t, MP_ROAD);
686 SetTileOwner(t, owner);
687 _m[t].m2 = did;
688 _m[t].m3 = 0;
689 _m[t].m4 = INVALID_ROADTYPE;
690 _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
691 SB(_me[t].m6, 2, 4, 0);
692 _me[t].m7 = owner;
693 _me[t].m8 = INVALID_ROADTYPE << 6;
694 SetRoadType(t, GetRoadTramType(rt), rt);
695 SetRoadOwner(t, RTT_TRAM, owner);
698 #endif /* ROAD_MAP_H */