OpenTTD
roadstop_base.h
Go to the documentation of this file.
1 /* $Id: roadstop_base.h 23704 2012-01-01 17:22:32Z alberth $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef ROADSTOP_BASE_H
13 #define ROADSTOP_BASE_H
14 
15 #include "station_type.h"
16 #include "core/pool_type.hpp"
17 #include "core/bitmath_func.hpp"
18 #include "vehicle_type.h"
19 
22 
24 struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
31  };
32 
34  struct Entry {
35  private:
36  int length;
37  int occupied;
38 
39  public:
40  friend struct RoadStop;
41 
43  Entry() : length(0), occupied(0) {}
44 
49  inline int GetLength() const
50  {
51  return this->length;
52  }
53 
58  inline int GetOccupied() const
59  {
60  return this->occupied;
61  }
62 
63  void Leave(const RoadVehicle *rv);
64  void Enter(const RoadVehicle *rv);
65  void CheckIntegrity(const RoadStop *rs) const;
66  void Rebuild(const RoadStop *rs, int side = -1);
67  };
68 
70  byte status;
71  struct RoadStop *next;
72 
74  inline RoadStop(TileIndex tile = INVALID_TILE) :
75  xy(tile),
76  status((1 << RSSFB_BAY_COUNT) - 1)
77  { }
78 
79  ~RoadStop();
80 
85  inline bool HasFreeBay() const
86  {
87  return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
88  }
89 
95  inline bool IsFreeBay(uint nr) const
96  {
97  assert(nr < RSSFB_BAY_COUNT);
98  return HasBit(this->status, nr);
99  }
100 
105  inline bool IsEntranceBusy() const
106  {
107  return HasBit(this->status, RSSFB_ENTRY_BUSY);
108  }
109 
114  inline void SetEntranceBusy(bool busy)
115  {
116  SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
117  }
118 
124  inline const Entry *GetEntry(DiagDirection dir) const
125  {
126  return HasBit((int)dir, 1) ? this->west : this->east;
127  }
128 
135  {
136  return HasBit((int)dir, 1) ? this->west : this->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 
151 private:
154 
160  inline uint AllocateBay()
161  {
162  assert(this->HasFreeBay());
163 
164  /* Find the first free bay. If the bit is set, the bay is free. */
165  uint bay_nr = 0;
166  while (!HasBit(this->status, bay_nr)) bay_nr++;
167 
168  ClrBit(this->status, bay_nr);
169  return bay_nr;
170  }
171 
176  inline void AllocateDriveThroughBay(uint nr)
177  {
178  assert(nr < RSSFB_BAY_COUNT);
179  ClrBit(this->status, nr);
180  }
181 
186  inline void FreeBay(uint nr)
187  {
188  assert(nr < RSSFB_BAY_COUNT);
189  SetBit(this->status, nr);
190  }
191 };
192 
193 #define FOR_ALL_ROADSTOPS_FROM(var, start) FOR_ALL_ITEMS_FROM(RoadStop, roadstop_index, var, start)
194 #define FOR_ALL_ROADSTOPS(var) FOR_ALL_ROADSTOPS_FROM(var, 0)
195 
196 #endif /* ROADSTOP_BASE_H */