OpenTTD Source 20250531-master-g621c031307
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
20
22struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
23 enum class RoadStopStatusFlag : uint8_t {
24 Bay0Free = 0,
25 Bay1Free = 1,
26 BaseEntry = 6,
27 EntryBusy = 7,
28 };
29 using RoadStopStatusFlags = EnumBitSet<RoadStopStatusFlag, uint8_t>;
30
32 struct Entry {
33 private:
34 uint16_t length = 0;
35 uint16_t occupied = 0;
36
37 public:
38 friend struct RoadStop;
39
44 inline int GetLength() const
45 {
46 return this->length;
47 }
48
53 inline int GetOccupied() const
54 {
55 return this->occupied;
56 }
57
58 void Leave(const RoadVehicle *rv);
59 void Enter(const RoadVehicle *rv);
60 void CheckIntegrity(const RoadStop *rs) const;
61 void Rebuild(const RoadStop *rs, int side = -1);
62 };
63
65 struct Entries {
68 };
69
72 RoadStop *next = nullptr;
73
75 inline RoadStop(TileIndex tile = INVALID_TILE) : xy(tile) { }
76
77 ~RoadStop();
78
83 inline bool HasFreeBay() const
84 {
86 }
87
93 inline bool IsFreeBay(uint nr) const
94 {
95 switch (nr) {
96 case 0: return this->status.Test(RoadStopStatusFlag::Bay0Free);
97 case 1: return this->status.Test(RoadStopStatusFlag::Bay1Free);
98 default: NOT_REACHED();
99 }
100 }
101
106 inline bool IsEntranceBusy() const
107 {
109 }
110
115 inline void SetEntranceBusy(bool busy)
116 {
118 }
119
125 inline const Entry &GetEntry(DiagDirection dir) const
126 {
127 return dir >= DIAGDIR_SW ? this->entries->west : this->entries->east;
128 }
129
136 {
137 return dir >= DIAGDIR_SW ? this->entries->west : this->entries->east;
138 }
139
140 void MakeDriveThrough();
141 void ClearDriveThrough();
142
143 void Leave(RoadVehicle *rv);
144 bool Enter(RoadVehicle *rv);
145
146 RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
147
148 static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
149
151
152private:
153 Entries *entries = nullptr;
154
160 inline uint AllocateBay()
161 {
162 assert(this->HasFreeBay());
163
164 /* Find the first free bay. */
165 uint bay_nr = 0;
166 while (!this->IsFreeBay(bay_nr)) ++bay_nr;
167
168 this->AllocateDriveThroughBay(bay_nr);
169 return bay_nr;
170 }
171
176 inline void AllocateDriveThroughBay(uint nr)
177 {
178 switch (nr) {
179 case 0: this->status.Reset(RoadStopStatusFlag::Bay0Free); break;
180 case 1: this->status.Reset(RoadStopStatusFlag::Bay1Free); break;
181 default: NOT_REACHED();
182 }
183 }
184
189 inline void FreeBay(uint nr)
190 {
191 switch (nr) {
192 case 0: this->status.Set(RoadStopStatusFlag::Bay0Free); break;
193 case 1: this->status.Set(RoadStopStatusFlag::Bay1Free); break;
194 default: NOT_REACHED();
195 }
196 }
197};
198
199#endif /* ROADSTOP_BASE_H */
Functions related to bit mathematics.
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.
@ DIAGDIR_SW
Southwest.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
RoadStopPool _roadstop_pool("RoadStop")
The pool of roadstops.
@ Enter
signal entering the block found
@ Rebuild
rebuild the sort list
Types related to stations.
RoadStopType
Types of RoadStops.
Base class for all PoolItems.
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'.
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:269
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition roadstop.cpp:360
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
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.
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
@ 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.
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:205
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:42
~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.
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:294
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition roadstop.cpp:255
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
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition roadstop.cpp:124
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Types related to vehicles.