Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / road_map.h
blob22d0fa54d372afe6cad1ddce52ef4398a85ae6ee
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"
20 /** The different types of road tiles. */
21 enum RoadTileType {
22 ROAD_TILE_NORMAL, ///< Normal road
23 ROAD_TILE_CROSSING, ///< Level crossing
24 ROAD_TILE_DEPOT, ///< Depot (one entrance)
27 /**
28 * Test whether a tile can have road/tram types.
29 * @param t Tile to query.
30 * @return true if tile can be queried about road/tram types.
32 static inline bool MayHaveRoad(TileIndex t)
34 switch (GetTileType(t)) {
35 case MP_ROAD:
36 case MP_STATION:
37 case MP_TUNNELBRIDGE:
38 return true;
40 default:
41 return false;
45 /**
46 * Get the type of the road tile.
47 * @param t Tile to query.
48 * @pre IsTileType(t, MP_ROAD)
49 * @return The road tile type.
51 static inline RoadTileType GetRoadTileType(TileIndex t)
53 assert(IsTileType(t, MP_ROAD));
54 return (RoadTileType)GB(_m[t].m5, 6, 2);
57 /**
58 * Return whether a tile is a normal road.
59 * @param t Tile to query.
60 * @pre IsTileType(t, MP_ROAD)
61 * @return True if normal road.
63 static inline bool IsNormalRoad(TileIndex t)
65 return GetRoadTileType(t) == ROAD_TILE_NORMAL;
68 /**
69 * Return whether a tile is a normal road tile.
70 * @param t Tile to query.
71 * @return True if normal road tile.
73 static inline bool IsNormalRoadTile(TileIndex t)
75 return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
78 /**
79 * Return whether a tile is a level crossing.
80 * @param t Tile to query.
81 * @pre IsTileType(t, MP_ROAD)
82 * @return True if level crossing.
84 static inline bool IsLevelCrossing(TileIndex t)
86 return GetRoadTileType(t) == ROAD_TILE_CROSSING;
89 /**
90 * Return whether a tile is a level crossing tile.
91 * @param t Tile to query.
92 * @return True if level crossing tile.
94 static inline bool IsLevelCrossingTile(TileIndex t)
96 return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
99 /**
100 * Return whether a tile is a road depot.
101 * @param t Tile to query.
102 * @pre IsTileType(t, MP_ROAD)
103 * @return True if road depot.
105 static inline bool IsRoadDepot(TileIndex t)
107 return GetRoadTileType(t) == ROAD_TILE_DEPOT;
111 * Return whether a tile is a road depot tile.
112 * @param t Tile to query.
113 * @return True if road depot tile.
115 static inline bool IsRoadDepotTile(TileIndex t)
117 return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
121 * Get the present road bits for a specific road type.
122 * @param t The tile to query.
123 * @param rt Road type.
124 * @pre IsNormalRoad(t)
125 * @return The present road bits for the road type.
127 static inline RoadBits GetRoadBits(TileIndex t, RoadTramType rtt)
129 assert(IsNormalRoad(t));
130 if (rtt == RTT_TRAM) return (RoadBits)GB(_m[t].m3, 0, 4);
131 return (RoadBits)GB(_m[t].m5, 0, 4);
135 * Get all set RoadBits on the given tile
137 * @param tile The tile from which we want to get the RoadBits
138 * @return all set RoadBits of the tile
140 static inline RoadBits GetAllRoadBits(TileIndex tile)
142 return GetRoadBits(tile, RTT_ROAD) | GetRoadBits(tile, RTT_TRAM);
146 * Set the present road bits for a specific road type.
147 * @param t The tile to change.
148 * @param r The new road bits.
149 * @param rt Road type.
150 * @pre IsNormalRoad(t)
152 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadTramType rtt)
154 assert(IsNormalRoad(t)); // XXX incomplete
155 if (rtt == RTT_TRAM) {
156 SB(_m[t].m3, 0, 4, r);
157 } else {
158 SB(_m[t].m5, 0, 4, r);
162 static inline RoadType GetRoadTypeRoad(TileIndex t)
164 assert(MayHaveRoad(t));
165 return (RoadType)GB(_m[t].m4, 0, 6);
168 static inline RoadType GetRoadTypeTram(TileIndex t)
170 assert(MayHaveRoad(t));
171 return (RoadType)GB(_me[t].m8, 6, 6);
174 static inline RoadType GetRoadType(TileIndex t, RoadTramType rtt)
176 return (rtt == RTT_TRAM) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
180 * Get the present road types of a tile.
181 * @param t The tile to query.
182 * @return Present road types.
184 static inline RoadTypes GetPresentRoadTypes(TileIndex t)
186 RoadTypes result = ROADTYPES_NONE;
187 if (MayHaveRoad(t)) {
188 if (GetRoadTypeRoad(t) != INVALID_ROADTYPE) SetBit(result, GetRoadTypeRoad(t));
189 if (GetRoadTypeTram(t) != INVALID_ROADTYPE) SetBit(result, GetRoadTypeTram(t));
191 return result;
194 static inline bool HasRoadTypeRoad(TileIndex t)
196 return GetRoadTypeRoad(t) != INVALID_ROADTYPE;
199 static inline bool HasRoadTypeTram(TileIndex t)
201 return GetRoadTypeTram(t) != INVALID_ROADTYPE;
205 * Check if a tile has a road or a tram road type.
206 * @param t The tile to check.
207 * @param tram True to check tram, false to check road.
208 * @return True if the tile has the specified road type.
210 static inline bool HasTileRoadType(TileIndex t, RoadTramType rtt)
212 return GetRoadType(t, rtt) != INVALID_ROADTYPE;
216 * Check if a tile has one of the specified road types.
217 * @param t The tile to check.
218 * @param rts Allowed road types.
219 * @return True if the tile has one of the specified road types.
221 static inline bool HasTileAnyRoadType(TileIndex t, RoadTypes rts)
223 if (!MayHaveRoad(t)) return false;
224 return (GetPresentRoadTypes(t) & rts);
228 * Get the owner of a specific road type.
229 * @param t The tile to query.
230 * @param rtt RoadTramType.
231 * @return Owner of the given road type.
233 static inline Owner GetRoadOwner(TileIndex t, RoadTramType rtt)
235 assert(MayHaveRoad(t));
236 if (rtt == RTT_ROAD) return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
238 /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
239 * to OWNER_TOWN makes it use one bit less */
240 Owner o = (Owner)GB(_m[t].m3, 4, 4);
241 return o == OWNER_TOWN ? OWNER_NONE : o;
245 * Set the owner of a specific road type.
246 * @param t The tile to change.
247 * @param rtt RoadTramType.
248 * @param o New owner of the given road type.
250 static inline void SetRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
252 if (rtt == RTT_ROAD) {
253 SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o);
254 } else {
255 SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o);
260 * Check if a specific road type is owned by an owner.
261 * @param t The tile to query.
262 * @param tram True to check tram, false to check road.
263 * @param o Owner to compare with.
264 * @pre HasTileRoadType(t, rt)
265 * @return True if the road type is owned by the given owner.
267 static inline bool IsRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
269 assert(HasTileRoadType(t, rtt));
270 return (GetRoadOwner(t, rtt) == o);
274 * Checks if given tile has town owned road
275 * @param t tile to check
276 * @pre IsTileType(t, MP_ROAD)
277 * @return true iff tile has road and the road is owned by a town
279 static inline bool HasTownOwnedRoad(TileIndex t)
281 return HasTileRoadType(t, RTT_ROAD) && IsRoadOwner(t, RTT_ROAD, OWNER_TOWN);
284 /** Which directions are disallowed ? */
285 enum DisallowedRoadDirections {
286 DRD_NONE, ///< None of the directions are disallowed
287 DRD_SOUTHBOUND, ///< All southbound traffic is disallowed
288 DRD_NORTHBOUND, ///< All northbound traffic is disallowed
289 DRD_BOTH, ///< All directions are disallowed
290 DRD_END, ///< Sentinel
292 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections)
293 /** Helper information for extract tool. */
294 template <> struct EnumPropsT<DisallowedRoadDirections> : MakeEnumPropsT<DisallowedRoadDirections, byte, DRD_NONE, DRD_END, DRD_END, 2> {};
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 */