Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / roadstop_base.h
blobf2e295042a738e4bf79e0bc5558d94cd61e74926
1 /* $Id$ */
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 roadstop_base.h Base class for roadstops. */
12 #ifndef ROADSTOP_BASE_H
13 #define ROADSTOP_BASE_H
15 #include "station_type.h"
16 #include "core/pool_type.hpp"
17 #include "core/bitmath_func.hpp"
18 #include "vehicle_type.h"
20 /** A Stop for a Road Vehicle */
21 struct RoadStop : PooledItem <RoadStop, RoadStopID, 32, 64000> {
22 enum RoadStopStatusFlags {
23 RSSFB_BAY0_FREE = 0, ///< Non-zero when bay 0 is free
24 RSSFB_BAY1_FREE = 1, ///< Non-zero when bay 1 is free
25 RSSFB_BAY_COUNT = 2, ///< Max. number of bays
26 RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
27 RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy
30 /** Container for each entry point of a drive through road stop */
31 struct Entry {
32 private:
33 int length; ///< The length of the stop in tile 'units'
34 int occupied; ///< The amount of occupied stop in tile 'units'
36 public:
37 friend struct RoadStop; ///< Oh yeah, the road stop may play with me.
39 /** Create an entry */
40 Entry() : length(0), occupied(0) {}
42 /**
43 * Get the length of this drive through stop.
44 * @return the length in tile units.
46 inline int GetLength() const
48 return this->length;
51 /**
52 * Get the amount of occupied space in this drive through stop.
53 * @return the occupied space in tile units.
55 inline int GetOccupied() const
57 return this->occupied;
60 void Leave(const RoadVehicle *rv);
61 void Enter(const RoadVehicle *rv);
62 void CheckIntegrity(const RoadStop *rs) const;
63 void Rebuild(const RoadStop *rs, int side = -1);
66 TileIndex xy; ///< Position on the map
67 byte status; ///< Current status of the Stop, @see RoadStopSatusFlag. Access using *Bay and *Busy functions.
68 struct RoadStop *next; ///< Next stop of the given type at this station
70 /** Initializes a RoadStop */
71 inline RoadStop(TileIndex tile = INVALID_TILE) :
72 xy(tile),
73 status((1 << RSSFB_BAY_COUNT) - 1)
74 { }
76 ~RoadStop();
78 /**
79 * Checks whether there is a free bay in this road stop
80 * @return is at least one bay free?
82 inline bool HasFreeBay() const
84 return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
87 /**
88 * Checks whether the given bay is free in this road stop
89 * @param nr bay to check
90 * @return is given bay free?
92 inline bool IsFreeBay(uint nr) const
94 assert(nr < RSSFB_BAY_COUNT);
95 return HasBit(this->status, nr);
98 /**
99 * Checks whether the entrance of the road stop is occupied by a vehicle
100 * @return is entrance busy?
102 inline bool IsEntranceBusy() const
104 return HasBit(this->status, RSSFB_ENTRY_BUSY);
108 * Makes an entrance occupied or free
109 * @param busy If true, marks busy; free otherwise.
111 inline void SetEntranceBusy(bool busy)
113 SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
117 * Get the drive through road stop entry struct for the given direction.
118 * @param dir The direction to get the entry for.
119 * @return the entry
121 inline const Entry *GetEntry(DiagDirection dir) const
123 return HasBit((int)dir, 1) ? this->west : this->east;
127 * Get the drive through road stop entry struct for the given direction.
128 * @param dir The direction to get the entry for.
129 * @return the entry
131 inline Entry *GetEntry(DiagDirection dir)
133 return HasBit((int)dir, 1) ? this->west : this->east;
136 void MakeDriveThrough();
137 void ClearDriveThrough();
139 void Leave(RoadVehicle *rv);
140 bool Enter(RoadVehicle *rv);
142 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
144 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
146 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
148 private:
149 Entry *east; ///< The vehicles that entered from the east
150 Entry *west; ///< The vehicles that entered from the west
153 * Allocates a bay
154 * @return the allocated bay number
155 * @pre this->HasFreeBay()
157 inline uint AllocateBay()
159 assert(this->HasFreeBay());
161 /* Find the first free bay. If the bit is set, the bay is free. */
162 uint bay_nr = 0;
163 while (!HasBit(this->status, bay_nr)) bay_nr++;
165 ClrBit(this->status, bay_nr);
166 return bay_nr;
170 * Allocates a bay in a drive-through road stop
171 * @param nr the number of the bay to allocate
173 inline void AllocateDriveThroughBay(uint nr)
175 assert(nr < RSSFB_BAY_COUNT);
176 ClrBit(this->status, nr);
180 * Frees the given bay
181 * @param nr the number of the bay to free
183 inline void FreeBay(uint nr)
185 assert(nr < RSSFB_BAY_COUNT);
186 SetBit(this->status, nr);
190 #define FOR_ALL_ROADSTOPS_FROM(var, start) FOR_ALL_ITEMS_FROM(RoadStop, roadstop_index, var, start)
191 #define FOR_ALL_ROADSTOPS(var) FOR_ALL_ROADSTOPS_FROM(var, 0)
193 #endif /* ROADSTOP_BASE_H */