Rework return statement in FlowsDown
[openttd/fttd.git] / src / roadstop_base.h
blobee470db8d1758d9a1369bfbb6094aa15f8f6fc0c
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 typedef Pool<RoadStop, RoadStopID, 32, 64000> RoadStopPool;
21 extern RoadStopPool _roadstop_pool;
23 /** A Stop for a Road Vehicle */
24 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
25 enum RoadStopStatusFlags {
26 RSSFB_BAY0_FREE = 0, ///< Non-zero when bay 0 is free
27 RSSFB_BAY1_FREE = 1, ///< Non-zero when bay 1 is free
28 RSSFB_BAY_COUNT = 2, ///< Max. number of bays
29 RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
30 RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy
33 /** Container for each entry point of a drive through road stop */
34 struct Entry {
35 private:
36 int length; ///< The length of the stop in tile 'units'
37 int occupied; ///< The amount of occupied stop in tile 'units'
39 public:
40 friend struct RoadStop; ///< Oh yeah, the road stop may play with me.
42 /** Create an entry */
43 Entry() : length(0), occupied(0) {}
45 /**
46 * Get the length of this drive through stop.
47 * @return the length in tile units.
49 inline int GetLength() const
51 return this->length;
54 /**
55 * Get the amount of occupied space in this drive through stop.
56 * @return the occupied space in tile units.
58 inline int GetOccupied() const
60 return this->occupied;
63 void Leave(const RoadVehicle *rv);
64 void Enter(const RoadVehicle *rv);
65 void CheckIntegrity(const RoadStop *rs) const;
66 void Rebuild(const RoadStop *rs, int side = -1);
69 TileIndex xy; ///< Position on the map
70 byte status; ///< Current status of the Stop, @see RoadStopSatusFlag. Access using *Bay and *Busy functions.
71 struct RoadStop *next; ///< Next stop of the given type at this station
73 /** Initializes a RoadStop */
74 inline RoadStop(TileIndex tile = INVALID_TILE) :
75 xy(tile),
76 status((1 << RSSFB_BAY_COUNT) - 1)
77 { }
79 ~RoadStop();
81 /**
82 * Checks whether there is a free bay in this road stop
83 * @return is at least one bay free?
85 inline bool HasFreeBay() const
87 return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
90 /**
91 * Checks whether the given bay is free in this road stop
92 * @param nr bay to check
93 * @return is given bay free?
95 inline bool IsFreeBay(uint nr) const
97 assert(nr < RSSFB_BAY_COUNT);
98 return HasBit(this->status, nr);
102 * Checks whether the entrance of the road stop is occupied by a vehicle
103 * @return is entrance busy?
105 inline bool IsEntranceBusy() const
107 return HasBit(this->status, RSSFB_ENTRY_BUSY);
111 * Makes an entrance occupied or free
112 * @param busy If true, marks busy; free otherwise.
114 inline void SetEntranceBusy(bool busy)
116 SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
120 * Get the drive through road stop entry struct for the given direction.
121 * @param dir The direction to get the entry for.
122 * @return the entry
124 inline const Entry *GetEntry(DiagDirection dir) const
126 return HasBit((int)dir, 1) ? this->west : this->east;
130 * Get the drive through road stop entry struct for the given direction.
131 * @param dir The direction to get the entry for.
132 * @return the entry
134 inline Entry *GetEntry(DiagDirection dir)
136 return HasBit((int)dir, 1) ? this->west : this->east;
139 void MakeDriveThrough();
140 void ClearDriveThrough();
142 void Leave(RoadVehicle *rv);
143 bool Enter(RoadVehicle *rv);
145 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
147 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
149 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
151 private:
152 Entry *east; ///< The vehicles that entered from the east
153 Entry *west; ///< The vehicles that entered from the west
156 * Allocates a bay
157 * @return the allocated bay number
158 * @pre this->HasFreeBay()
160 inline uint AllocateBay()
162 assert(this->HasFreeBay());
164 /* Find the first free bay. If the bit is set, the bay is free. */
165 uint bay_nr = 0;
166 while (!HasBit(this->status, bay_nr)) bay_nr++;
168 ClrBit(this->status, bay_nr);
169 return bay_nr;
173 * Allocates a bay in a drive-through road stop
174 * @param nr the number of the bay to allocate
176 inline void AllocateDriveThroughBay(uint nr)
178 assert(nr < RSSFB_BAY_COUNT);
179 ClrBit(this->status, nr);
183 * Frees the given bay
184 * @param nr the number of the bay to free
186 inline void FreeBay(uint nr)
188 assert(nr < RSSFB_BAY_COUNT);
189 SetBit(this->status, nr);
193 #define FOR_ALL_ROADSTOPS_FROM(var, start) FOR_ALL_ITEMS_FROM(RoadStop, roadstop_index, var, start)
194 #define FOR_ALL_ROADSTOPS(var) FOR_ALL_ROADSTOPS_FROM(var, 0)
196 #endif /* ROADSTOP_BASE_H */