OpenTTD Source 20260621-master-g720d10536d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef ROADSTOP_BASE_H
11#define ROADSTOP_BASE_H
12
13#include "station_type.h"
14#include "core/pool_type.hpp"
15#include "vehicle_type.h"
16
17using RoadStopPool = Pool<RoadStop, RoadStopID, 32>;
18extern RoadStopPool _roadstop_pool;
19
21struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
23 enum class RoadStopStatusFlag : uint8_t {
28 };
29
32
34 struct Entry {
35 private:
36 uint16_t length = 0;
37 uint16_t occupied = 0;
38
39 public:
40 friend struct RoadStop;
41
46 inline int GetLength() const
47 {
48 return this->length;
49 }
50
55 inline int GetOccupied() const
56 {
57 return this->occupied;
58 }
59
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);
64 };
65
67 struct Entries {
70 };
71
74 RoadStop *next = nullptr;
75
81 inline RoadStop(RoadStopID index, TileIndex tile = INVALID_TILE) : RoadStopPool::PoolItem<&_roadstop_pool>(index), xy(tile) { }
82
83 ~RoadStop();
84
89 inline bool HasFreeBay() const
90 {
92 }
93
99 inline bool IsFreeBay(uint nr) const
100 {
101 switch (nr) {
102 case 0: return this->status.Test(RoadStopStatusFlag::Bay0Free);
103 case 1: return this->status.Test(RoadStopStatusFlag::Bay1Free);
104 default: NOT_REACHED();
105 }
106 }
107
112 inline bool IsEntranceBusy() const
113 {
114 return this->status.Test(RoadStopStatusFlag::EntryBusy);
115 }
116
121 inline void SetEntranceBusy(bool busy)
122 {
123 this->status.Set(RoadStopStatusFlag::EntryBusy, busy);
124 }
125
131 inline const Entry &GetEntry(DiagDirection dir) const
132 {
133 return dir >= DiagDirection::SW ? this->entries->west : this->entries->east;
134 }
135
142 {
143 return dir >= DiagDirection::SW ? this->entries->west : this->entries->east;
144 }
145
146 void MakeDriveThrough();
147 void ClearDriveThrough();
148
149 void Leave(RoadVehicle *rv);
150 bool Enter(RoadVehicle *rv);
151
152 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
153
154 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
155
157
158private:
159 Entries *entries = nullptr;
160
166 inline uint AllocateBay()
167 {
168 assert(this->HasFreeBay());
169
170 /* Find the first free bay. */
171 uint bay_nr = 0;
172 while (!this->IsFreeBay(bay_nr)) ++bay_nr;
173
174 this->AllocateDriveThroughBay(bay_nr);
175 return bay_nr;
176 }
177
182 inline void AllocateDriveThroughBay(uint nr)
183 {
184 switch (nr) {
185 case 0: this->status.Reset(RoadStopStatusFlag::Bay0Free); break;
186 case 1: this->status.Reset(RoadStopStatusFlag::Bay1Free); break;
187 default: NOT_REACHED();
188 }
189 }
190
195 inline void FreeBay(uint nr)
196 {
197 switch (nr) {
198 case 0: this->status.Set(RoadStopStatusFlag::Bay0Free); break;
199 case 1: this->status.Set(RoadStopStatusFlag::Bay1Free); break;
200 default: NOT_REACHED();
201 }
202 }
203};
204
205#endif /* ROADSTOP_BASE_H */
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Enum-as-bit-set wrapper.
DiagDirection
Enumeration for diagonal directions.
@ SW
Southwest.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
RoadStopPool _roadstop_pool("RoadStop")
The pool of roadstops.
Types related to stations.
RoadStopType
Types of RoadStops.
Base class for all pools.
Container for both east and west entry points.
Entry west
Information for vehicles that entered from the west.
Entry east
Information for vehicles that entered from the east.
Container for each entry point of a drive through road stop.
uint16_t occupied
The amount of occupied stop in tile 'units'.
friend struct RoadStop
Oh yeah, the road stop may play with me.
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:267
void Enter(const RoadVehicle *rv)
Enter the road stop.
Definition roadstop.cpp:277
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition roadstop.cpp:358
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
void Rebuild(const RoadStop *rs, int side=-1)
Rebuild, from scratch, the vehicles and other metadata on this stop.
Definition roadstop.cpp:321
int GetLength() const
Get the length of this drive through stop.
uint16_t length
The length of the stop in tile 'units'.
A Stop for a Road Vehicle.
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
RoadStopStatusFlag
Flags describing the status of a single road stop.
@ BaseEntry
Non-zero when the entries on this road stop are the primary, i.e. the ones to delete.
@ EntryBusy
Non-zero when roadstop entry is busy.
@ Bay0Free
Non-zero when bay 0 is free.
@ Bay1Free
Non-zero when bay 1 is free.
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
EnumBitSet< RoadStopStatusFlag, uint8_t > RoadStopStatusFlags
Bitset of RoadStopStatusFlag elements.
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:203
RoadStop * next
Next stop of the given type at this station.
Entries * entries
Information about available and allocated bays.
uint AllocateBay()
Allocates a bay.
RoadStop * GetNextRoadStop(const struct RoadVehicle *v) const
Get the next road stop accessible by this vehicle.
Definition roadstop.cpp:40
~RoadStop()
De-Initializes RoadStops.
Definition roadstop.cpp:27
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
RoadStop(RoadStopID index, TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
bool Enter(RoadVehicle *rv)
Enter the road stop.
Definition roadstop.cpp:220
void FreeBay(uint nr)
Frees the given bay.
Entry & GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
TileIndex xy
Position on the map.
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
RoadStopStatusFlags status
Current status of the Stop. Access using *Bay and *Busy functions.
const Entry & GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
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:292
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition roadstop.cpp:253
void MakeDriveThrough()
Join this road stop to another 'base' road stop if possible; fill all necessary data to become an act...
Definition roadstop.cpp:60
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition roadstop.cpp:122
Buses, trucks and trams belong to this class.
Definition roadveh.h:105
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Types related to vehicles.