summaryrefslogtreecommitdiff
path: root/src/roadstop_base.h
blob: 7bfdba16ddfbfea0130bc10086d76cee4a7fb904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* $Id$ */

/*
 * 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/>.
 */

/** @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"
#include "direction_type.h"
#include "vehicle_type.h"

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
		RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
		RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy
	};

	/** 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:
		friend struct RoadStop; ///< Oh yeah, the road stop may play with me.

		/** Create an entry */
		Entry() : length(0), occupied(0) {}

		/**
		 * Get the length of this drive through stop.
		 * @return the length in tile units.
		 */
		FORCEINLINE int GetLength() const
		{
			return this->length;
		}

		/**
		 * Get the amount of occupied space in this drive through stop.
		 * @return the occupied space in tile units.
		 */
		FORCEINLINE int GetOccupied() const
		{
			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);
	};

	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

	/** Initializes a RoadStop */
	FORCEINLINE RoadStop(TileIndex tile = INVALID_TILE) :
		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?
	 */
	FORCEINLINE bool HasFreeBay() const
	{
		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?
	 */
	FORCEINLINE bool IsFreeBay(uint nr) const
	{
		assert(nr < RSSFB_BAY_COUNT);
		return HasBit(this->status, nr);
	}

	/**
	 * Checks whether the entrance of the road stop is occupied by a vehicle
	 * @return is entrance busy?
	 */
	FORCEINLINE bool IsEntranceBusy() const
	{
		return HasBit(this->status, RSSFB_ENTRY_BUSY);
	}

	/**
	 * Makes an entrance occupied or free
	 * @param busy if true, marks busy; free otherwise
	 */
	FORCEINLINE void SetEntranceBusy(bool busy)
	{
		SB(this->status, RSSFB_ENTRY_BUSY, 1, busy);
	}

	/**
	 * Get the drive through road stop entry struct for the given direction.
	 * @param direction the direciton to get the entry for
	 * @return the entry
	 */
	FORCEINLINE const Entry *GetEntry(DiagDirection dir) const
	{
		return HasBit((int)dir, 1) ? this->west : this->east;
	}

	/**
	 * Get the drive through road stop entry struct for the given direction.
	 * @param direction the direciton to get the entry for
	 * @return the entry
	 */
	FORCEINLINE Entry *GetEntry(DiagDirection dir)
	{
		return HasBit((int)dir, 1) ? this->west : this->east;
	}

	void MakeDriveThrough();
	void ClearDriveThrough();

	void Leave(RoadVehicle *rv);
	bool Enter(RoadVehicle *rv);

	RoadStop *GetNextRoadStop(const struct RoadVehicle *v) const;

	static RoadStop *GetByTile(TileIndex tile, RoadStopType type);

	static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next);

private:
	Entry *east; ///< The vehicles that entered from the east
	Entry *west; ///< The vehicles that entered from the west

	/**
	 * Allocates a bay
	 * @return the allocated bay number
	 * @pre this->HasFreeBay()
	 */
	FORCEINLINE uint AllocateBay()
	{
		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
	 */
	FORCEINLINE void AllocateDriveThroughBay(uint nr)
	{
		assert(nr < RSSFB_BAY_COUNT);
		ClrBit(this->status, nr);
	}

	/**
	 * Frees the given bay
	 * @param nr the number of the bay to free
	 */
	FORCEINLINE void FreeBay(uint nr)
	{
		assert(nr < RSSFB_BAY_COUNT);
		SetBit(this->status, nr);
	}
};

#define FOR_ALL_ROADSTOPS_FROM(var, start) FOR_ALL_ITEMS_FROM(RoadStop, roadstop_index, var, start)
#define FOR_ALL_ROADSTOPS(var) FOR_ALL_ROADSTOPS_FROM(var, 0)

#endif /* ROADSTOP_BASE_H */