OpenTTD Source  20240917-master-g9ab0a47812
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 
22 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
29  };
30 
32  struct Entry {
33  private:
34  int length;
35  int occupied;
36 
37  public:
38  friend struct RoadStop;
39 
41  Entry() : length(0), occupied(0) {}
42 
47  inline int GetLength() const
48  {
49  return this->length;
50  }
51 
56  inline int GetOccupied() const
57  {
58  return this->occupied;
59  }
60 
61  void Leave(const RoadVehicle *rv);
62  void Enter(const RoadVehicle *rv);
63  void CheckIntegrity(const RoadStop *rs) const;
64  void Rebuild(const RoadStop *rs, int side = -1);
65  };
66 
67  uint8_t status;
70 
72  inline RoadStop(TileIndex tile = INVALID_TILE) :
73  status((1 << RSSFB_BAY_COUNT) - 1),
74  xy(tile)
75  { }
76 
77  ~RoadStop();
78 
83  inline bool HasFreeBay() const
84  {
85  return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
86  }
87 
93  inline bool IsFreeBay(uint nr) const
94  {
95  assert(nr < RSSFB_BAY_COUNT);
96  return HasBit(this->status, nr);
97  }
98 
103  inline bool IsEntranceBusy() const
104  {
105  return HasBit(this->status, RSSFB_ENTRY_BUSY);
106  }
107 
112  inline void SetEntranceBusy(bool busy)
113  {
114  SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
115  }
116 
122  inline const Entry *GetEntry(DiagDirection dir) const
123  {
124  return HasBit((int)dir, 1) ? this->west : this->east;
125  }
126 
133  {
134  return HasBit((int)dir, 1) ? this->west : this->east;
135  }
136 
137  void MakeDriveThrough();
138  void ClearDriveThrough();
139 
140  void Leave(RoadVehicle *rv);
141  bool Enter(RoadVehicle *rv);
142 
143  RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
144 
145  static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
146 
148 
149 private:
152 
158  inline uint AllocateBay()
159  {
160  assert(this->HasFreeBay());
161 
162  /* Find the first free bay. If the bit is set, the bay is free. */
163  uint bay_nr = 0;
164  while (!HasBit(this->status, bay_nr)) bay_nr++;
165 
166  ClrBit(this->status, bay_nr);
167  return bay_nr;
168  }
169 
174  inline void AllocateDriveThroughBay(uint nr)
175  {
176  assert(nr < RSSFB_BAY_COUNT);
177  ClrBit(this->status, nr);
178  }
179 
184  inline void FreeBay(uint nr)
185  {
186  assert(nr < RSSFB_BAY_COUNT);
187  SetBit(this->status, nr);
188  }
189 };
190 
191 #endif /* ROADSTOP_BASE_H */
RoadVehicle
Buses, trucks and trams belong to this class.
Definition: roadveh.h:106
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
RoadStop::HasFreeBay
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
Definition: roadstop_base.h:83
RoadStop::Entry::CheckIntegrity
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition: roadstop.cpp:380
RoadStop::IsEntranceBusy
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
Definition: roadstop_base.h:103
RoadStop::Entry::GetOccupied
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition: roadstop_base.h:56
RoadStop::xy
TileIndex xy
Position on the map.
Definition: roadstop_base.h:68
GB
constexpr static debug_inline 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
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
RoadStop::GetByTile
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition: roadstop.cpp:266
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
RoadStop::GetEntry
const Entry * GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
Definition: roadstop_base.h:122
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
RoadStop::Enter
bool Enter(RoadVehicle *rv)
Enter the road stop.
Definition: roadstop.cpp:233
RoadStop::Entry::length
int length
The length of the stop in tile 'units'.
Definition: roadstop_base.h:34
RoadStop::Entry::Enter
void Enter(const RoadVehicle *rv)
Enter the road stop.
Definition: roadstop.cpp:290
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:352
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::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:27
RoadStop::west
Entry * west
The vehicles that entered from the west.
Definition: roadstop_base.h:151
bitmath_func.hpp
RoadStop::AllocateBay
uint AllocateBay()
Allocates a bay.
Definition: roadstop_base.h:158
RoadStop::RoadStopStatusFlags
RoadStopStatusFlags
Definition: roadstop_base.h:23
RoadStop::Entry::occupied
int occupied
The amount of occupied stop in tile 'units'.
Definition: roadstop_base.h:35
RoadStop::RSSFB_ENTRY_BUSY
@ RSSFB_ENTRY_BUSY
Non-zero when roadstop entry is busy.
Definition: roadstop_base.h:28
RoadStop::FreeBay
void FreeBay(uint nr)
Frees the given bay.
Definition: roadstop_base.h:184
RoadStop::~RoadStop
~RoadStop()
De-Initializes RoadStops.
Definition: roadstop.cpp:26
RoadStop::RSSFB_BAY_COUNT
@ RSSFB_BAY_COUNT
Max. number of bays.
Definition: roadstop_base.h:26
RoadStop::AllocateDriveThroughBay
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
Definition: roadstop_base.h:174
RoadStop::east
Entry * east
The vehicles that entered from the east.
Definition: roadstop_base.h:150
RoadStop::SetEntranceBusy
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
Definition: roadstop_base.h:112
RoadStop::Leave
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition: roadstop.cpp:216
RoadStop::status
uint8_t status
Current status of the Stop,.
Definition: roadstop_base.h:67
RoadStop::RSSFB_BAY1_FREE
@ RSSFB_BAY1_FREE
Non-zero when bay 1 is free.
Definition: roadstop_base.h:25
RoadStop::Entry::Entry
Entry()
Create an entry.
Definition: roadstop_base.h:41
RoadStop::Entry::Leave
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition: roadstop.cpp:280
RoadStop::RoadStop
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
Definition: roadstop_base.h:72
vehicle_type.h
Pool
Base class for all pools.
Definition: pool_type.hpp:80
RoadStop::ClearDriveThrough
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition: roadstop.cpp:130
RoadStop::RSSFB_BAY0_FREE
@ RSSFB_BAY0_FREE
Non-zero when bay 0 is free.
Definition: roadstop_base.h:24
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:305
RoadStop::Entry::GetLength
int GetLength() const
Get the length of this drive through stop.
Definition: roadstop_base.h:47
RoadStop::next
RoadStop * next
Next stop of the given type at this station.
Definition: roadstop_base.h:69
_roadstop_pool
RoadStopPool _roadstop_pool
The pool of roadstops.
RoadStop::IsFreeBay
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition: roadstop_base.h:93
RoadStopType
RoadStopType
Types of RoadStops.
Definition: station_type.h:45
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
pool_type.hpp
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
RoadStop::Entry
Container for each entry point of a drive through road stop.
Definition: roadstop_base.h:32
RoadStop::GetEntry
Entry * GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
Definition: roadstop_base.h:132
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:237
RoadStop::GetNextRoadStop
RoadStop * GetNextRoadStop(const struct RoadVehicle *v) const
Get the next road stop accessible by this vehicle.
Definition: roadstop.cpp:42
station_type.h
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103