OpenTTD Source: src/roadstop_base.h Source File

OpenTTD Source 15.0-beta1

roadstop_base.h
Go to the documentation of this file.
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 */
7
10#ifndef ROADSTOP_BASE_H
11#define ROADSTOP_BASE_H
12
13#include "station_type.h"
14#include "core/pool_type.hpp"
15#include "core/bitmath_func.hpp"
16#include "vehicle_type.h"
17
18typedef Pool<RoadStop, RoadStopID, 32, 64000> RoadStopPool;
19extern RoadStopPool _roadstop_pool;
20
22 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
23 enum RoadStopStatusFlags {
24 RSSFB_BAY0_FREE = 0,
25 RSSFB_BAY1_FREE = 1,
26 RSSFB_BASE_ENTRY = 6,
27 RSSFB_ENTRY_BUSY = 7,
28 };
29
30 static constexpr uint8_t BAY_COUNT = 2;
31
33 struct Entry {
34 private:
35 int length;
36 int occupied;
37
38 public:
39 friend struct RoadStop;
40
42 Entry() : length(0), occupied(0) {}
43
48 inline int GetLength() const
49 {
50 return this->length;
51 }
52
57 inline int GetOccupied() const
58 {
59 return this->occupied;
60 }
61
62 void Leave(const RoadVehicle *rv);
63 void Enter(const RoadVehicle *rv);
64 void CheckIntegrity(const RoadStop *rs) const;
65 void Rebuild(const RoadStop *rs, int side = -1);
66 };
67
68 uint8_t status;
69 TileIndex xy;
70 RoadStop *next;
71
73 inline RoadStop(TileIndex tile = INVALID_TILE) :
74 status((1 << BAY_COUNT) - 1),
75 xy(tile)
76 { }
77
78 ~RoadStop();
79
84 inline bool HasFreeBay() const
85 {
86 return GB(this->status, 0, BAY_COUNT) != 0;
87 }
88
94 inline bool IsFreeBay(uint nr) const
95 {
96 assert(nr < BAY_COUNT);
97 return HasBit(this->status, nr);
98 }
99
104 inline bool IsEntranceBusy() const
105 {
106 return HasBit(this->status, RSSFB_ENTRY_BUSY);
107 }
108
113 inline void SetEntranceBusy(bool busy)
114 {
115 SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
116 }
117
123 inline const Entry *GetEntry(DiagDirection dir) const
124 {
125 return HasBit((int)dir, 1) ? this->west : this->east;
126 }
127
133 inline Entry *GetEntry(DiagDirection dir)
134 {
135 return HasBit((int)dir, 1) ? this->west : this->east;
136 }
137
138 void MakeDriveThrough();
139 void ClearDriveThrough();
140
141 void Leave(RoadVehicle *rv);
142 bool Enter(RoadVehicle *rv);
143
144 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
145
146 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
147
148 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
149
150private:
151 Entry *east;
152 Entry *west;
153
159 inline uint AllocateBay()
160 {
161 assert(this->HasFreeBay());
162
163 /* Find the first free bay. If the bit is set, the bay is free. */
164 uint bay_nr = 0;
165 while (!HasBit(this->status, bay_nr)) bay_nr++;
166
167 ClrBit(this->status, bay_nr);
168 return bay_nr;
169 }
170
175 inline void AllocateDriveThroughBay(uint nr)
176 {
177 assert(nr < BAY_COUNT);
178 ClrBit(this->status, nr);
179 }
180
185 inline void FreeBay(uint nr)
186 {
187 assert(nr < BAY_COUNT);
188 SetBit(this->status, nr);
189 }
190};
191
192#endif /* ROADSTOP_BASE_H */
bitmath_func.hpp
Functions related to bit mathematics.
HasBit
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition bitmath_func.hpp:58
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
GB
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition bitmath_func.hpp:151
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:73
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
_roadstop_pool
RoadStopPool _roadstop_pool("RoadStop")
The pool of roadstops.
station_type.h
Types related to stations.
RoadStopType
RoadStopType
Types of RoadStops.
Definition station_type.h:45
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:237
Pool
Base class for all pools.
Definition pool_type.hpp:80
RoadStop::Entry
Container for each entry point of a drive through road stop.
Definition roadstop_base.h:33
RoadStop::Entry::Entry
Entry()
Create an entry.
Definition roadstop_base.h:42
RoadStop::Entry::length
int length
The length of the stop in tile 'units'.
Definition roadstop_base.h:35
RoadStop::Entry::Leave
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:278
RoadStop::Entry::Enter
void Enter(const RoadVehicle *rv)
Enter the road stop.
Definition roadstop.cpp:288
RoadStop::Entry::CheckIntegrity
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition roadstop.cpp:393
RoadStop::Entry::GetOccupied
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition roadstop_base.h:57
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:365
RoadStop::Entry::GetLength
int GetLength() const
Get the length of this drive through stop.
Definition roadstop_base.h:48
RoadStop::Entry::occupied
int occupied
The amount of occupied stop in tile 'units'.
Definition roadstop_base.h:36
RoadStop
A Stop for a Road Vehicle.
Definition roadstop_base.h:22
RoadStop::SetEntranceBusy
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
Definition roadstop_base.h:113
RoadStop::east
Entry * east
The vehicles that entered from the east.
Definition roadstop_base.h:151
RoadStop::RoadStop
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
Definition roadstop_base.h:73
RoadStop::RoadStopStatusFlags
RoadStopStatusFlags
Definition roadstop_base.h:23
RoadStop::RSSFB_ENTRY_BUSY
@ RSSFB_ENTRY_BUSY
Non-zero when roadstop entry is busy.
Definition roadstop_base.h:27
RoadStop::RSSFB_BASE_ENTRY
@ 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:26
RoadStop::RSSFB_BAY1_FREE
@ RSSFB_BAY1_FREE
Non-zero when bay 1 is free.
Definition roadstop_base.h:25
RoadStop::RSSFB_BAY0_FREE
@ RSSFB_BAY0_FREE
Non-zero when bay 0 is free.
Definition roadstop_base.h:24
RoadStop::IsFreeBay
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition roadstop_base.h:94
RoadStop::Leave
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:214
RoadStop::next
RoadStop * next
Next stop of the given type at this station.
Definition roadstop_base.h:70
RoadStop::AllocateBay
uint AllocateBay()
Allocates a bay.
Definition roadstop_base.h:159
RoadStop::GetNextRoadStop
RoadStop * GetNextRoadStop(const struct RoadVehicle *v) const
Get the next road stop accessible by this vehicle.
Definition roadstop.cpp:42
RoadStop::~RoadStop
~RoadStop()
De-Initializes RoadStops.
Definition roadstop.cpp:26
RoadStop::HasFreeBay
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
Definition roadstop_base.h:84
RoadStop::AllocateDriveThroughBay
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
Definition roadstop_base.h:175
RoadStop::GetEntry
Entry * GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
Definition roadstop_base.h:133
RoadStop::Enter
bool Enter(RoadVehicle *rv)
Enter the road stop.
Definition roadstop.cpp:231
RoadStop::FreeBay
void FreeBay(uint nr)
Frees the given bay.
Definition roadstop_base.h:185
RoadStop::xy
TileIndex xy
Position on the map.
Definition roadstop_base.h:69
RoadStop::IsEntranceBusy
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
Definition roadstop_base.h:104
RoadStop::IsDriveThroughRoadStopContinuation
static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next)
Checks whether the 'next' tile is still part of the road same drive through stop 'rs' in the same dir...
Definition roadstop.cpp:303
RoadStop::status
uint8_t status
Current status of the Stop,.
Definition roadstop_base.h:68
RoadStop::GetByTile
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition roadstop.cpp:264
RoadStop::MakeDriveThrough
void MakeDriveThrough()
Join this road stop to another 'base' road stop if possible; fill all necessary data to become an act...
Definition roadstop.cpp:62
RoadStop::west
Entry * west
The vehicles that entered from the west.
Definition roadstop_base.h:152
RoadStop::BAY_COUNT
static constexpr uint8_t BAY_COUNT
Max. number of bays.
Definition roadstop_base.h:30
RoadStop::GetEntry
const Entry * GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
Definition roadstop_base.h:123
RoadStop::ClearDriveThrough
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition roadstop.cpp:129
RoadVehicle
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
vehicle_type.h
Types related to vehicles.