OpenTTD Source: src/roadstop_base.h Source File

OpenTTD Source 15.0-RC2

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
17using RoadStopPool = Pool<RoadStop, RoadStopID, 32>;
18extern RoadStopPool _roadstop_pool;
19
21 struct 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 {
65 Entry east{};
66 Entry west{};
67 };
68
69 RoadStopStatusFlags status{RoadStopStatusFlag::Bay0Free, RoadStopStatusFlag::Bay1Free};
70 TileIndex xy = INVALID_TILE;
71 RoadStop *next = nullptr;
72
74 inline RoadStop(TileIndex tile = INVALID_TILE) : xy(tile) { }
75
76 ~RoadStop();
77
82 inline bool HasFreeBay() const
83 {
84 return this->status.Any({RoadStopStatusFlag::Bay0Free, RoadStopStatusFlag::Bay1Free});
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 {
107 return this->status.Test(RoadStopStatusFlag::EntryBusy);
108 }
109
114 inline void SetEntranceBusy(bool busy)
115 {
116 this->status.Set(RoadStopStatusFlag::EntryBusy, busy);
117 }
118
124 inline const Entry &GetEntry(DiagDirection dir) const
125 {
126 return dir >= DIAGDIR_SW ? this->entries->west : this->entries->east;
127 }
128
134 inline Entry &GetEntry(DiagDirection dir)
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
149 static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
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 */
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition direction_type.h:82
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.
SigFlag::Enter
@ Enter
signal entering the block found
SortListFlag::Rebuild
@ Rebuild
rebuild the sort list
station_type.h
Types related to stations.
RoadStopType
RoadStopType
Types of RoadStops.
Definition station_type.h:43
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool
Base class for all pools.
Definition pool_type.hpp:133
RoadStop::Entries
Container for both east and west entry points.
Definition roadstop_base.h:64
RoadStop::Entries::west
Entry west
Information for vehicles that entered from the west.
Definition roadstop_base.h:66
RoadStop::Entries::east
Entry east
Information for vehicles that entered from the east.
Definition roadstop_base.h:65
RoadStop::Entry
Container for each entry point of a drive through road stop.
Definition roadstop_base.h:31
RoadStop::Entry::occupied
uint16_t occupied
The amount of occupied stop in tile 'units'.
Definition roadstop_base.h:34
RoadStop::Entry::Leave
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:267
RoadStop::Entry::CheckIntegrity
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition roadstop.cpp:358
RoadStop::Entry::GetOccupied
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition roadstop_base.h:52
RoadStop::Entry::GetLength
int GetLength() const
Get the length of this drive through stop.
Definition roadstop_base.h:43
RoadStop::Entry::length
uint16_t length
The length of the stop in tile 'units'.
Definition roadstop_base.h:33
RoadStop
A Stop for a Road Vehicle.
Definition roadstop_base.h:21
RoadStop::SetEntranceBusy
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
Definition roadstop_base.h:114
RoadStop::RoadStop
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
Definition roadstop_base.h:74
RoadStop::RoadStopStatusFlag
RoadStopStatusFlag
Definition roadstop_base.h:22
RoadStop::RoadStopStatusFlag::BaseEntry
@ BaseEntry
Non-zero when the entries on this road stop are the primary, i.e. the ones to delete.
RoadStop::RoadStopStatusFlag::EntryBusy
@ EntryBusy
Non-zero when roadstop entry is busy.
RoadStop::RoadStopStatusFlag::Bay0Free
@ Bay0Free
Non-zero when bay 0 is free.
RoadStop::RoadStopStatusFlag::Bay1Free
@ Bay1Free
Non-zero when bay 1 is free.
RoadStop::IsFreeBay
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition roadstop_base.h:92
RoadStop::Leave
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:203
RoadStop::next
RoadStop * next
Next stop of the given type at this station.
Definition roadstop_base.h:71
RoadStop::entries
Entries * entries
Information about available and allocated bays.
Definition roadstop_base.h:152
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:40
RoadStop::~RoadStop
~RoadStop()
De-Initializes RoadStops.
Definition roadstop.cpp:27
RoadStop::HasFreeBay
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
Definition roadstop_base.h:82
RoadStop::AllocateDriveThroughBay
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
Definition roadstop_base.h:175
RoadStop::FreeBay
void FreeBay(uint nr)
Frees the given bay.
Definition roadstop_base.h:188
RoadStop::GetEntry
Entry & GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
Definition roadstop_base.h:134
RoadStop::xy
TileIndex xy
Position on the map.
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::status
RoadStopStatusFlags status
Current status of the Stop. Access using *Bay and *Busy functions.
Definition roadstop_base.h:69
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::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:292
RoadStop::GetByTile
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition roadstop.cpp:253
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:60
RoadStop::ClearDriveThrough
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition roadstop.cpp:122
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.