OpenTTD Source 20251213-master-g1091fa6071
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
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
19
21struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
22 enum class RoadStopStatusFlag : uint8_t {
23 Bay0Free = 0,
24 Bay1Free = 1,
25 BaseEntry = 6,
26 EntryBusy = 7,
27 };
28 using RoadStopStatusFlags = EnumBitSet<RoadStopStatusFlag, uint8_t>;
29
31 struct Entry {
32 private:
33 uint16_t length = 0;
34 uint16_t occupied = 0;
35
36 public:
37 friend struct RoadStop;
38
43 inline int GetLength() const
44 {
45 return this->length;
46 }
47
52 inline int GetOccupied() const
53 {
54 return this->occupied;
55 }
56
57 void Leave(const RoadVehicle *rv);
58 void Enter(const RoadVehicle *rv);
59 void CheckIntegrity(const RoadStop *rs) const;
60 void Rebuild(const RoadStop *rs, int side = -1);
61 };
62
64 struct Entries {
67 };
68
71 RoadStop *next = nullptr;
72
74 inline RoadStop(TileIndex tile = INVALID_TILE) : xy(tile) { }
75
76 ~RoadStop();
77
82 inline bool HasFreeBay() const
83 {
85 }
86
92 inline bool IsFreeBay(uint nr) const
93 {
94 switch (nr) {
95 case 0: return this->status.Test(RoadStopStatusFlag::Bay0Free);
96 case 1: return this->status.Test(RoadStopStatusFlag::Bay1Free);
97 default: NOT_REACHED();
98 }
99 }
100
105 inline bool IsEntranceBusy() const
106 {
108 }
109
114 inline void SetEntranceBusy(bool busy)
115 {
117 }
118
124 inline const Entry &GetEntry(DiagDirection dir) const
125 {
126 return dir >= DIAGDIR_SW ? this->entries->west : this->entries->east;
127 }
128
135 {
136 return dir >= DIAGDIR_SW ? this->entries->west : this->entries->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
150
151private:
152 Entries *entries = nullptr;
153
159 inline uint AllocateBay()
160 {
161 assert(this->HasFreeBay());
162
163 /* Find the first free bay. */
164 uint bay_nr = 0;
165 while (!this->IsFreeBay(bay_nr)) ++bay_nr;
166
167 this->AllocateDriveThroughBay(bay_nr);
168 return bay_nr;
169 }
170
175 inline void AllocateDriveThroughBay(uint nr)
176 {
177 switch (nr) {
178 case 0: this->status.Reset(RoadStopStatusFlag::Bay0Free); break;
179 case 1: this->status.Reset(RoadStopStatusFlag::Bay1Free); break;
180 default: NOT_REACHED();
181 }
182 }
183
188 inline void FreeBay(uint nr)
189 {
190 switch (nr) {
191 case 0: this->status.Set(RoadStopStatusFlag::Bay0Free); break;
192 case 1: this->status.Set(RoadStopStatusFlag::Bay1Free); break;
193 default: NOT_REACHED();
194 }
195 }
196};
197
198#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.
@ 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:267
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.
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: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.
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:98
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Types related to vehicles.