OpenTTD

roadstop_base.h
Go to the documentation of this file.
1 /* $Id: roadstop_base.h 23704 2012-01-01 17:22:32Z alberth $ */
2
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 */
9
12 #ifndef ROADSTOP_BASE_H
13 #define ROADSTOP_BASE_H
14
15 #include "station_type.h"
16 #include "core/pool_type.hpp"
17 #include "core/bitmath_func.hpp"
18 #include "vehicle_type.h"
19
20 typedef Pool<RoadStop, RoadStopID, 32, 64000> RoadStopPool;
21 extern RoadStopPool _roadstop_pool;
22
24 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
25 enum RoadStopStatusFlags {
26 RSSFB_BAY0_FREE = 0,
27 RSSFB_BAY1_FREE = 1,
28 RSSFB_BAY_COUNT = 2,
29 RSSFB_BASE_ENTRY = 6,
30 RSSFB_ENTRY_BUSY = 7,
31 };
32
34 struct Entry {
35 private:
36 int length;
37 int occupied;
38
39 public:
40 friend struct RoadStop;
41
43 Entry() : length(0), occupied(0) {}
44
49 inline int GetLength() const
50 {
51 return this->length;
52 }
53
58 inline int GetOccupied() const
59 {
60 return this->occupied;
61 }
62
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);
67 };
68
69 TileIndex xy;
70 byte status;
71 struct RoadStop *next;
72
74 inline RoadStop(TileIndex tile = INVALID_TILE) :
75 xy(tile),
76 status((1 << RSSFB_BAY_COUNT) - 1)
77 { }
78
79 ~RoadStop();
80
85 inline bool HasFreeBay() const
86 {
87 return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
88 }
89
95 inline bool IsFreeBay(uint nr) const
96 {
97 assert(nr < RSSFB_BAY_COUNT);
98 return HasBit(this->status, nr);
99 }
100
105 inline bool IsEntranceBusy() const
106 {
107 return HasBit(this->status, RSSFB_ENTRY_BUSY);
108 }
109
114 inline void SetEntranceBusy(bool busy)
115 {
116 SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
117 }
118
124 inline const Entry *GetEntry(DiagDirection dir) const
125 {
126 return HasBit((int)dir, 1) ? this->west : this->east;
127 }
128
134 inline Entry *GetEntry(DiagDirection dir)
135 {
136 return HasBit((int)dir, 1) ? this->west : this->east;
137 }
138
139 void MakeDriveThrough();
140 void ClearDriveThrough();
141
142 void Leave(RoadVehicle *rv);
143 bool Enter(RoadVehicle *rv);
144
145 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
146
147 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
148
149 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
150
151 private:
152 Entry *east;
153 Entry *west;
154
160 inline uint AllocateBay()
161 {
162 assert(this->HasFreeBay());
163
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++;
167
168 ClrBit(this->status, bay_nr);
169 return bay_nr;
170 }
171
176 inline void AllocateDriveThroughBay(uint nr)
177 {
178 assert(nr < RSSFB_BAY_COUNT);
179 ClrBit(this->status, nr);
180 }
181
186 inline void FreeBay(uint nr)
187 {
188 assert(nr < RSSFB_BAY_COUNT);
189 SetBit(this->status, nr);
190 }
191 };
192
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)
195
196 #endif /* ROADSTOP_BASE_H */
RoadStop::Entry::Entry
Entry()
Create an entry.
Definition: roadstop_base.h:43
RoadStop::status
byte status
Current status of the Stop,.
Definition: roadstop_base.h:70
RoadStop::IsEntranceBusy
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
Definition: roadstop_base.h:105
RoadStop::AllocateBay
uint AllocateBay()
Allocates a bay.
Definition: roadstop_base.h:160
RoadStop::Entry::GetLength
int GetLength() const
Get the length of this drive through stop.
Definition: roadstop_base.h:49
RoadStop::RoadStopStatusFlags
RoadStopStatusFlags
Definition: roadstop_base.h:25
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:123
RoadStop::RSSFB_ENTRY_BUSY
Non-zero when roadstop entry is busy.
Definition: roadstop_base.h:30
RoadStop::Entry::GetOccupied
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition: roadstop_base.h:58
RoadStop::xy
TileIndex xy
Position on the map.
Definition: roadstop_base.h:69
RoadStop::MakeDriveThrough
void MakeDriveThrough()
Join this road stop to another &#39;base&#39; road stop if possible; fill all necessary data to become an act...
Definition: roadstop.cpp:64
RoadStop::FreeBay
void FreeBay(uint nr)
Frees the given bay.
Definition: roadstop_base.h:186
RoadStop::Entry::Enter
void Enter(const RoadVehicle *rv)
Enter the road stop.
Definition: roadstop.cpp:292
bitmath_func.hpp
Functions related to bit mathematics.
pool_type.hpp
Defintion of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
RoadStop::GetNextRoadStop
RoadStop * GetNextRoadStop(const struct RoadVehicle *v) const
Get the next road stop accessible by this vehicle.
Definition: roadstop.cpp:44
RoadStopType
RoadStopType
Types of RoadStops.
Definition: station_type.h:47
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:60
RoadStop::RSSFB_BAY1_FREE
Non-zero when bay 1 is free.
Definition: roadstop_base.h:27
RoadVehicle
Buses, trucks and trams belong to this class.
Definition: roadveh.h:88
RoadStop::RSSFB_BAY_COUNT
Max. number of bays.
Definition: roadstop_base.h:28
station_type.h
Types related to stations.
RoadStop::ClearDriveThrough
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition: roadstop.cpp:132
RoadStop::Entry::CheckIntegrity
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition: roadstop.cpp:382
RoadStop::RSSFB_BAY0_FREE
Non-zero when bay 0 is free.
Definition: roadstop_base.h:26
RoadStop::east
Entry * east
The vehicles that entered from the east.
Definition: roadstop_base.h:152
RoadStop::SetEntranceBusy
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
Definition: roadstop_base.h:114
RoadStop::next
struct RoadStop * next
Next stop of the given type at this station.
Definition: roadstop_base.h:71
RoadStop::AllocateDriveThroughBay
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
Definition: roadstop_base.h:176
RoadStop::GetEntry
Entry * GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
Definition: roadstop_base.h:134
RoadStop::IsFreeBay
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition: roadstop_base.h:95
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:80
RoadStop::Entry::length
int length
The length of the stop in tile &#39;units&#39;.
Definition: roadstop_base.h:36
RoadStop::west
Entry * west
The vehicles that entered from the west.
Definition: roadstop_base.h:153
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:146
Pool
Base class for all pools.
Definition: pool_type.hpp:83
RoadStop::Entry
Container for each entry point of a drive through road stop.
Definition: roadstop_base.h:34
RoadStop::~RoadStop
~RoadStop()
De-Initializes RoadStops.
Definition: roadstop.cpp:28
RoadStop::HasFreeBay
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
Definition: roadstop_base.h:85
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:153
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
_roadstop_pool
RoadStopPool _roadstop_pool
The pool of roadstops.
vehicle_type.h
Types related to vehicles.
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
RoadStop::Entry::occupied
int occupied
The amount of occupied stop in tile &#39;units&#39;.
Definition: roadstop_base.h:37
RoadStop::GetEntry
const Entry * GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
Definition: roadstop_base.h:124
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:24
RoadStop::IsDriveThroughRoadStopContinuation
static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next)
Checks whether the &#39;next&#39; tile is still part of the road same drive through stop &#39;rs&#39; in the same dir...
Definition: roadstop.cpp:307
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:105
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
RoadStop::Entry::Leave
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition: roadstop.cpp:282
RoadStop::RoadStop
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
Definition: roadstop_base.h:74
RoadStop::RSSFB_BASE_ENTRY
Non-zero when the entries on this road stop are the primary, i.e. the ones to delete.
Definition: roadstop_base.h:29
RoadStop::Entry::Rebuild
void Rebuild(const RoadStop *rs, int side=-1)
Rebuild, from scratch, the vehicles and other metadata on this stop.
Definition: roadstop.cpp:354
RoadStop::GetByTile
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition: roadstop.cpp:268