2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-06-23 21:44:48 +00:00
|
|
|
/** @file roadstop_base.h Base class for roadstops. */
|
|
|
|
|
|
|
|
#ifndef ROADSTOP_BASE_H
|
|
|
|
#define ROADSTOP_BASE_H
|
|
|
|
|
|
|
|
#include "station_type.h"
|
|
|
|
#include "core/pool_type.hpp"
|
|
|
|
#include "core/bitmath_func.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "vehicle_type.h"
|
2020-10-24 19:39:15 +00:00
|
|
|
#include "roadveh.h"
|
|
|
|
#include "road_map.h"
|
2009-06-23 21:44:48 +00:00
|
|
|
|
|
|
|
typedef Pool<RoadStop, RoadStopID, 32, 64000> RoadStopPool;
|
|
|
|
extern RoadStopPool _roadstop_pool;
|
|
|
|
|
|
|
|
/** A Stop for a Road Vehicle */
|
|
|
|
struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
|
|
|
|
enum RoadStopStatusFlags {
|
|
|
|
RSSFB_BAY0_FREE = 0, ///< Non-zero when bay 0 is free
|
|
|
|
RSSFB_BAY1_FREE = 1, ///< Non-zero when bay 1 is free
|
|
|
|
RSSFB_BAY_COUNT = 2, ///< Max. number of bays
|
2009-12-04 20:52:19 +00:00
|
|
|
RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
|
2009-06-23 21:44:48 +00:00
|
|
|
RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy
|
|
|
|
};
|
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
/** Container for each entry point of a drive through road stop */
|
|
|
|
struct Entry {
|
|
|
|
private:
|
|
|
|
int length; ///< The length of the stop in tile 'units'
|
|
|
|
int occupied; ///< The amount of occupied stop in tile 'units'
|
|
|
|
|
|
|
|
public:
|
2009-12-07 10:44:58 +00:00
|
|
|
friend struct RoadStop; ///< Oh yeah, the road stop may play with me.
|
2009-12-04 20:52:19 +00:00
|
|
|
|
|
|
|
/** Create an entry */
|
|
|
|
Entry() : length(0), occupied(0) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the length of this drive through stop.
|
|
|
|
* @return the length in tile units.
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline int GetLength() const
|
2009-12-04 20:52:19 +00:00
|
|
|
{
|
|
|
|
return this->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the amount of occupied space in this drive through stop.
|
|
|
|
* @return the occupied space in tile units.
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline int GetOccupied() const
|
2009-12-04 20:52:19 +00:00
|
|
|
{
|
|
|
|
return this->occupied;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Leave(const RoadVehicle *rv);
|
|
|
|
void Enter(const RoadVehicle *rv);
|
|
|
|
void CheckIntegrity(const RoadStop *rs) const;
|
|
|
|
void Rebuild(const RoadStop *rs, int side = -1);
|
|
|
|
};
|
|
|
|
|
2009-12-04 21:13:31 +00:00
|
|
|
TileIndex xy; ///< Position on the map
|
|
|
|
byte status; ///< Current status of the Stop, @see RoadStopSatusFlag. Access using *Bay and *Busy functions.
|
|
|
|
struct RoadStop *next; ///< Next stop of the given type at this station
|
2009-06-23 21:44:48 +00:00
|
|
|
|
|
|
|
/** Initializes a RoadStop */
|
2011-12-20 17:57:56 +00:00
|
|
|
inline RoadStop(TileIndex tile = INVALID_TILE) :
|
2009-06-23 21:44:48 +00:00
|
|
|
xy(tile),
|
|
|
|
status((1 << RSSFB_BAY_COUNT) - 1)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~RoadStop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether there is a free bay in this road stop
|
|
|
|
* @return is at least one bay free?
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline bool HasFreeBay() const
|
2009-06-23 21:44:48 +00:00
|
|
|
{
|
|
|
|
return GB(this->status, 0, RSSFB_BAY_COUNT) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given bay is free in this road stop
|
|
|
|
* @param nr bay to check
|
|
|
|
* @return is given bay free?
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline bool IsFreeBay(uint nr) const
|
2009-06-23 21:44:48 +00:00
|
|
|
{
|
|
|
|
assert(nr < RSSFB_BAY_COUNT);
|
|
|
|
return HasBit(this->status, nr);
|
|
|
|
}
|
|
|
|
|
2009-12-02 23:53:15 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the entrance of the road stop is occupied by a vehicle
|
|
|
|
* @return is entrance busy?
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline bool IsEntranceBusy() const
|
2009-12-02 23:53:15 +00:00
|
|
|
{
|
|
|
|
return HasBit(this->status, RSSFB_ENTRY_BUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes an entrance occupied or free
|
2012-01-01 17:22:32 +00:00
|
|
|
* @param busy If true, marks busy; free otherwise.
|
2009-12-02 23:53:15 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void SetEntranceBusy(bool busy)
|
2009-12-02 23:53:15 +00:00
|
|
|
{
|
|
|
|
SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
|
|
|
|
}
|
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
/**
|
|
|
|
* Get the drive through road stop entry struct for the given direction.
|
2012-01-01 17:22:32 +00:00
|
|
|
* @param dir The direction to get the entry for.
|
2009-12-04 20:52:19 +00:00
|
|
|
* @return the entry
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline const Entry *GetEntry(DiagDirection dir) const
|
2009-12-04 20:52:19 +00:00
|
|
|
{
|
|
|
|
return HasBit((int)dir, 1) ? this->west : this->east;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the drive through road stop entry struct for the given direction.
|
2012-01-01 17:22:32 +00:00
|
|
|
* @param dir The direction to get the entry for.
|
2009-12-04 20:52:19 +00:00
|
|
|
* @return the entry
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline Entry *GetEntry(DiagDirection dir)
|
2009-12-04 20:52:19 +00:00
|
|
|
{
|
|
|
|
return HasBit((int)dir, 1) ? this->west : this->east;
|
|
|
|
}
|
|
|
|
|
2020-10-24 19:39:15 +00:00
|
|
|
inline const Entry *GetEntry(const RoadVehicle *rv) const {
|
|
|
|
DiagDirection diag_dir = DirToDiagDir(rv->direction);
|
|
|
|
return this->GetEntry(rv->overtaking != 0 ? ReverseDiagDir(diag_dir) : diag_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Entry *GetEntry(const RoadVehicle *rv) {
|
|
|
|
DiagDirection diag_dir = DirToDiagDir(rv->direction);
|
|
|
|
return this->GetEntry(rv->overtaking != 0 ? ReverseDiagDir(diag_dir) : diag_dir);
|
|
|
|
}
|
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
void MakeDriveThrough();
|
|
|
|
void ClearDriveThrough();
|
2020-10-24 19:39:15 +00:00
|
|
|
void ChangeDriveThroughDisallowedRoadDirections(DisallowedRoadDirections drd);
|
2009-12-04 20:52:19 +00:00
|
|
|
|
2009-12-02 23:53:15 +00:00
|
|
|
void Leave(RoadVehicle *rv);
|
|
|
|
bool Enter(RoadVehicle *rv);
|
|
|
|
|
|
|
|
RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;
|
|
|
|
|
|
|
|
static RoadStop *GetByTile(TileIndex tile, RoadStopType type);
|
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);
|
|
|
|
|
2009-12-02 23:53:15 +00:00
|
|
|
private:
|
2009-12-04 20:52:19 +00:00
|
|
|
Entry *east; ///< The vehicles that entered from the east
|
|
|
|
Entry *west; ///< The vehicles that entered from the west
|
|
|
|
|
2009-06-23 21:44:48 +00:00
|
|
|
/**
|
|
|
|
* Allocates a bay
|
|
|
|
* @return the allocated bay number
|
|
|
|
* @pre this->HasFreeBay()
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint AllocateBay()
|
2009-06-23 21:44:48 +00:00
|
|
|
{
|
|
|
|
assert(this->HasFreeBay());
|
|
|
|
|
|
|
|
/* Find the first free bay. If the bit is set, the bay is free. */
|
|
|
|
uint bay_nr = 0;
|
|
|
|
while (!HasBit(this->status, bay_nr)) bay_nr++;
|
|
|
|
|
|
|
|
ClrBit(this->status, bay_nr);
|
|
|
|
return bay_nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a bay in a drive-through road stop
|
|
|
|
* @param nr the number of the bay to allocate
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void AllocateDriveThroughBay(uint nr)
|
2009-06-23 21:44:48 +00:00
|
|
|
{
|
|
|
|
assert(nr < RSSFB_BAY_COUNT);
|
|
|
|
ClrBit(this->status, nr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the given bay
|
|
|
|
* @param nr the number of the bay to free
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void FreeBay(uint nr)
|
2009-06-23 21:44:48 +00:00
|
|
|
{
|
|
|
|
assert(nr < RSSFB_BAY_COUNT);
|
|
|
|
SetBit(this->status, nr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ROADSTOP_BASE_H */
|