OpenTTD Source  20241108-master-g80f628063a
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> {
28  };
29 
30  static constexpr uint8_t BAY_COUNT = 2;
31 
33  struct Entry {
34  private:
35  int length;
36  int occupied;
37 
38  public:
39  friend struct RoadStop;
40 
42  Entry() : length(0), occupied(0) {}
43 
48  inline int GetLength() const
49  {
50  return this->length;
51  }
52 
57  inline int GetOccupied() const
58  {
59  return this->occupied;
60  }
61 
62  void Leave(const RoadVehicle *rv);
63  void Enter(const RoadVehicle *rv);
64  void CheckIntegrity(const RoadStop *rs) const;
65  void Rebuild(const RoadStop *rs);
66  };
67 
68  uint8_t status;
71 
73  inline RoadStop(TileIndex tile = INVALID_TILE) :
74  status((1 << BAY_COUNT) - 1),
75  xy(tile)
76  { }
77 
78  ~RoadStop();
79 
84  inline bool HasFreeBay() const
85  {
86  return GB(this->status, 0, BAY_COUNT) != 0;
87  }
88 
94  inline bool IsFreeBay(uint nr) const
95  {
96  assert(nr < BAY_COUNT);
97  return HasBit(this->status, nr);
98  }
99 
104  inline bool IsEntranceBusy() const
105  {
106  return HasBit(this->status, RSSFB_ENTRY_BUSY);
107  }
108 
113  inline void SetEntranceBusy(bool busy)
114  {
115  SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
116  }
117 
123  inline const Entry *GetEntry(DiagDirection dir) const
124  {
125  return HasBit((int)dir, 1) ? this->west : this->east;
126  }
127 
134  {
135  return HasBit((int)dir, 1) ? this->west : this->east;
136  }
137 
138  void MakeDriveThrough();
139  void ClearDriveThrough();
140 
141  void Leave(RoadVehicle *rv);
142  bool Enter(RoadVehicle *rv);
143 
144  RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
145 
146  static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
147 
149 
150 private:
153 
159  inline uint AllocateBay()
160  {
161  assert(this->HasFreeBay());
162 
163  /* Find the first free bay. If the bit is set, the bay is free. */
164  uint bay_nr = 0;
165  while (!HasBit(this->status, bay_nr)) bay_nr++;
166 
167  ClrBit(this->status, bay_nr);
168  return bay_nr;
169  }
170 
175  inline void AllocateDriveThroughBay(uint nr)
176  {
177  assert(nr < BAY_COUNT);
178  ClrBit(this->status, nr);
179  }
180 
185  inline void FreeBay(uint nr)
186  {
187  assert(nr < BAY_COUNT);
188  SetBit(this->status, nr);
189  }
190 };
191 
192 #endif /* ROADSTOP_BASE_H */
Functions related to bit mathematics.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
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.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
DiagDirection
Enumeration for diagonal directions.
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.
Definition: station_type.h:45
Base class for all PoolItems.
Definition: pool_type.hpp:237
Base class for all pools.
Definition: pool_type.hpp:80
Container for each entry point of a drive through road stop.
Definition: roadstop_base.h:33
Entry()
Create an entry.
Definition: roadstop_base.h:42
int length
The length of the stop in tile 'units'.
Definition: roadstop_base.h:35
void Leave(const RoadVehicle *rv)
Leave the road stop.
Definition: roadstop.cpp:278
void Enter(const RoadVehicle *rv)
Enter the road stop.
Definition: roadstop.cpp:288
void Rebuild(const RoadStop *rs)
Rebuild, from scratch, the vehicles and other metadata on this stop.
Definition: roadstop.cpp:364
void CheckIntegrity(const RoadStop *rs) const
Check the integrity of the data in this struct.
Definition: roadstop.cpp:391
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition: roadstop_base.h:57
int GetLength() const
Get the length of this drive through stop.
Definition: roadstop_base.h:48
int occupied
The amount of occupied stop in tile 'units'.
Definition: roadstop_base.h:36
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
Entry * east
The vehicles that entered from the east.
const Entry * GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
RoadStop(TileIndex tile=INVALID_TILE)
Initializes a RoadStop.
Definition: roadstop_base.h:73
@ RSSFB_ENTRY_BUSY
Non-zero when roadstop entry is busy.
Definition: roadstop_base.h:27
@ 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:26
@ RSSFB_BAY1_FREE
Non-zero when bay 1 is free.
Definition: roadstop_base.h:25
@ RSSFB_BAY0_FREE
Non-zero when bay 0 is free.
Definition: roadstop_base.h:24
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition: roadstop_base.h:94
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition: roadstop.cpp:214
RoadStop * next
Next stop of the given type at this station.
Definition: roadstop_base.h:70
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:26
bool HasFreeBay() const
Checks whether there is a free bay in this road stop.
Definition: roadstop_base.h:84
void AllocateDriveThroughBay(uint nr)
Allocates a bay in a drive-through road stop.
bool Enter(RoadVehicle *rv)
Enter the road stop.
Definition: roadstop.cpp:231
void FreeBay(uint nr)
Frees the given bay.
TileIndex xy
Position on the map.
Definition: roadstop_base.h:69
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
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:303
uint8_t status
Current status of the Stop,.
Definition: roadstop_base.h:68
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition: roadstop.cpp:264
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
Entry * west
The vehicles that entered from the west.
static constexpr uint8_t BAY_COUNT
Max. number of bays.
Definition: roadstop_base.h:30
void ClearDriveThrough()
Prepare for removal of this stop; update other neighbouring stops if needed.
Definition: roadstop.cpp:129
Entry * GetEntry(DiagDirection dir)
Get the drive through road stop entry struct for the given direction.
Buses, trucks and trams belong to this class.
Definition: roadveh.h:106
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
Types related to vehicles.