Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / roadstop_base.h
blob768b54282d5d7d9537d185dad1d0c9400278ca61
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 roadstop_base.h Base class for roadstops. */
10 #ifndef ROADSTOP_BASE_H
11 #define ROADSTOP_BASE_H
13 #include "station_type.h"
14 #include "core/pool_type.hpp"
15 #include "core/bitmath_func.hpp"
16 #include "vehicle_type.h"
18 typedef Pool<RoadStop, RoadStopID, 32, 64000> RoadStopPool;
19 extern RoadStopPool _roadstop_pool;
21 /** A Stop for a Road Vehicle */
22 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
23 enum RoadStopStatusFlags {
24 RSSFB_BAY0_FREE = 0, ///< Non-zero when bay 0 is free
25 RSSFB_BAY1_FREE = 1, ///< Non-zero when bay 1 is free
26 RSSFB_BAY_COUNT = 2, ///< Max. number of bays
27 RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
28 RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy
31 /** Container for each entry point of a drive through road stop */
32 struct Entry {
33 private:
34 int length; ///< The length of the stop in tile 'units'
35 int occupied; ///< The amount of occupied stop in tile 'units'
37 public:
38 friend struct RoadStop; ///< Oh yeah, the road stop may play with me.
40 /** Create an entry */
41 Entry() : length(0), occupied(0) {}
43 /**
44 * Get the length of this drive through stop.
45 * @return the length in tile units.
47 inline int GetLength() const
49 return this->length;
52 /**
53 * Get the amount of occupied space in this drive through stop.
54 * @return the occupied space in tile units.
56 inline int GetOccupied() const
58 return this->occupied;
61 void Leave(const RoadVehicle *rv);
62 void Enter(const RoadVehicle *rv);
63 void CheckIntegrity(const RoadStop *rs) const;
64 void Rebuild(const RoadStop *rs, int side = -1);
67 TileIndex xy; ///< Position on the map
68 byte status; ///< Current status of the Stop, @see RoadStopSatusFlag. Access using *Bay and *Busy functions.
69 struct RoadStop *next; ///< Next stop of the given type at this station
71 /** Initializes a RoadStop */
72 inline RoadStop(TileIndex tile = INVALID_TILE) :
73 xy(tile),
74 status((1 << RSSFB_BAY_COUNT) - 1)
75 { }
77 ~RoadStop();
79 /**
80 * Checks whether there is a free bay in this road stop
81 * @return is at least one bay free?
83 inline bool HasFreeBay() const
85 return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
88 /**
89 * Checks whether the given bay is free in this road stop
90 * @param nr bay to check
91 * @return is given bay free?
93 inline bool IsFreeBay(uint nr) const
95 assert(nr < RSSFB_BAY_COUNT);
96 return HasBit(this->status, nr);
99 /**
100 * Checks whether the entrance of the road stop is occupied by a vehicle
101 * @return is entrance busy?
103 inline bool IsEntranceBusy() const
105 return HasBit(this->status, RSSFB_ENTRY_BUSY);
109 * Makes an entrance occupied or free
110 * @param busy If true, marks busy; free otherwise.
112 inline void SetEntranceBusy(bool busy)
114 SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
118 * Get the drive through road stop entry struct for the given direction.
119 * @param dir The direction to get the entry for.
120 * @return the entry
122 inline const Entry *GetEntry(DiagDirection dir) const
124 return HasBit((int)dir, 1) ? this->west : this->east;
128 * Get the drive through road stop entry struct for the given direction.
129 * @param dir The direction to get the entry for.
130 * @return the entry
132 inline Entry *GetEntry(DiagDirection dir)
134 return HasBit((int)dir, 1) ? this->west : this->east;
137 void MakeDriveThrough();
138 void ClearDriveThrough();
140 void Leave(RoadVehicle *rv);
141 bool Enter(RoadVehicle *rv);
143 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
145 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
147 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
149 private:
150 Entry *east; ///< The vehicles that entered from the east
151 Entry *west; ///< The vehicles that entered from the west
154 * Allocates a bay
155 * @return the allocated bay number
156 * @pre this->HasFreeBay()
158 inline uint AllocateBay()
160 assert(this->HasFreeBay());
162 /* Find the first free bay. If the bit is set, the bay is free. */
163 uint bay_nr = 0;
164 while (!HasBit(this->status, bay_nr)) bay_nr++;
166 ClrBit(this->status, bay_nr);
167 return bay_nr;
171 * Allocates a bay in a drive-through road stop
172 * @param nr the number of the bay to allocate
174 inline void AllocateDriveThroughBay(uint nr)
176 assert(nr < RSSFB_BAY_COUNT);
177 ClrBit(this->status, nr);
181 * Frees the given bay
182 * @param nr the number of the bay to free
184 inline void FreeBay(uint nr)
186 assert(nr < RSSFB_BAY_COUNT);
187 SetBit(this->status, nr);
191 #endif /* ROADSTOP_BASE_H */