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/>.
10 /** @file station_base.h Base classes/functions for stations. */
12 #ifndef STATION_BASE_H
13 #define STATION_BASE_H
15 #include "core/random_func.hpp"
16 #include "base_station_base.h"
17 #include "newgrf_airport.h"
18 #include "cargopacket.h"
19 #include "industry_type.h"
20 #include "linkgraph/linkgraph_type.h"
21 #include "newgrf_storage.h"
24 static const byte INITIAL_STATION_RATING
= 175;
27 * Flow statistics telling how much flow should be sent along a link. This is
28 * done by creating "flow shares" and using std::map's upper_bound() method to
29 * look them up with a random number. A flow share is the difference between a
30 * key in a map and the previous key. So one key in the map doesn't actually
31 * mean anything by itself.
35 typedef std::map
<uint32
, StationID
> SharesMap
;
38 * Invalid constructor. This can't be called as a FlowStat must not be
39 * empty. However, the constructor must be defined and reachable for
40 * FlwoStat to be used in a std::map.
42 inline FlowStat() {NOT_REACHED();}
45 * Create a FlowStat with an initial entry.
46 * @param st Station the initial entry refers to.
47 * @param flow Amount of flow for the initial entry.
49 inline FlowStat(StationID st
, uint flow
)
52 this->shares
[flow
] = st
;
53 this->unrestricted
= flow
;
57 * Add some flow to the end of the shares map. Only do that if you know
58 * that the station isn't in the map yet. Anything else may lead to
60 * @param st Remote station.
61 * @param flow Amount of flow to be added.
62 * @param restricted If the flow to be added is restricted.
64 inline void AppendShare(StationID st
, uint flow
, bool restricted
= false)
67 this->shares
[(--this->shares
.end())->first
+ flow
] = st
;
68 if (!restricted
) this->unrestricted
+= flow
;
71 uint
GetShare(StationID st
) const;
73 void ChangeShare(StationID st
, int flow
);
75 void RestrictShare(StationID st
);
77 void ReleaseShare(StationID st
);
79 void ScaleToMonthly(uint runtime
);
82 * Get the actual shares as a const pointer so that they can be iterated
84 * @return Actual shares.
86 inline const SharesMap
*GetShares() const { return &this->shares
; }
89 * Return total amount of unrestricted shares.
90 * @return Amount of unrestricted shares.
92 inline uint
GetUnrestricted() const { return this->unrestricted
; }
95 * Swap the shares maps, and thus the content of this FlowStat with the
97 * @param other FlowStat to swap with.
99 inline void SwapShares(FlowStat
&other
)
101 this->shares
.swap(other
.shares
);
102 Swap(this->unrestricted
, other
.unrestricted
);
106 * Get a station a package can be routed to. This done by drawing a
107 * random number between 0 and sum_shares and then looking that up in
108 * the map with lower_bound. So each share gets selected with a
109 * probability dependent on its flow. Do include restricted flows here.
110 * @param is_restricted Output if a restricted flow was chosen.
111 * @return A station ID from the shares map.
113 inline StationID
GetViaWithRestricted(bool &is_restricted
) const
115 assert(!this->shares
.empty());
116 uint rand
= RandomRange((--this->shares
.end())->first
);
117 is_restricted
= rand
>= this->unrestricted
;
118 return this->shares
.upper_bound(rand
)->second
;
122 * Get a station a package can be routed to. This done by drawing a
123 * random number between 0 and sum_shares and then looking that up in
124 * the map with lower_bound. So each share gets selected with a
125 * probability dependent on its flow. Don't include restricted flows.
126 * @return A station ID from the shares map.
128 inline StationID
GetVia() const
130 assert(!this->shares
.empty());
131 return this->unrestricted
> 0 ?
132 this->shares
.upper_bound(RandomRange(this->unrestricted
))->second
:
136 StationID
GetVia(StationID excluded
, StationID excluded2
= INVALID_STATION
) const;
141 SharesMap shares
; ///< Shares of flow to be sent via specified station (or consumed locally).
142 uint unrestricted
; ///< Limit for unrestricted shares.
145 /** Flow descriptions by origin stations. */
146 class FlowStatMap
: public std::map
<StationID
, FlowStat
> {
148 void AddFlow(StationID origin
, StationID via
, uint amount
);
149 void PassOnFlow(StationID origin
, StationID via
, uint amount
);
150 StationIDStack
DeleteFlows(StationID via
);
151 void RestrictFlows(StationID via
);
152 void ReleaseFlows(StationID via
);
153 void FinalizeLocalConsumption(StationID self
);
157 * Stores station stats for a single cargo.
160 /** Status of this cargo for the station. */
161 enum GoodsEntryStatus
{
163 * Set when the station accepts the cargo currently for final deliveries.
164 * It is updated every STATION_ACCEPTANCE_TICKS ticks by checking surrounding tiles for acceptance >= 8/8.
169 * Set when the cargo was ever waiting at the station.
170 * It is set when cargo supplied by surrounding tiles is moved to the station, or when
171 * arriving vehicles unload/transfer cargo without it being a final delivery.
172 * This also indicates, whether a cargo has a rating at the station.
173 * This flag is never cleared.
178 * Set when a vehicle ever delivered cargo to the station for final delivery.
179 * This flag is never cleared.
184 * Set when cargo was delivered for final delivery last month.
185 * This flag is set to the value of GES_CURRENT_MONTH at the start of each month.
190 * Set when cargo was delivered for final delivery this month.
191 * This flag is reset on the beginning of every month.
196 * Set when cargo was delivered for final delivery during the current STATION_ACCEPTANCE_TICKS interval.
197 * This flag is reset every STATION_ACCEPTANCE_TICKS ticks.
199 GES_ACCEPTED_BIGTICK
,
203 acceptance_pickup(0),
204 time_since_pickup(255),
205 rating(INITIAL_STATION_RATING
),
209 link_graph(INVALID_LINK_GRAPH
),
214 byte acceptance_pickup
; ///< Status of this cargo, see #GoodsEntryStatus.
217 * Number of rating-intervals (up to 255) since the last vehicle tried to load this cargo.
218 * The unit used is STATION_RATING_TICKS.
219 * This does not imply there was any cargo to load.
221 byte time_since_pickup
;
223 byte rating
; ///< %Station rating for this cargo.
226 * Maximum speed (up to 255) of the last vehicle that tried to load this cargo.
227 * This does not imply there was any cargo to load.
228 * The unit used is a special vehicle-specific speed unit for station ratings.
231 * - Ships: 0.5 * km-ish/h
232 * - Aircraft: 8 * mph
237 * Age in years (up to 255) of the last vehicle that tried to load this cargo.
238 * This does not imply there was any cargo to load.
242 byte amount_fract
; ///< Fractional part of the amount in the cargo list
243 StationCargoList cargo
; ///< The cargo packets of cargo waiting in this station
245 LinkGraphID link_graph
; ///< Link graph this station belongs to.
246 NodeID node
; ///< ID of node in link graph referring to this goods entry.
247 FlowStatMap flows
; ///< Planned flows through this station.
248 uint max_waiting_cargo
; ///< Max cargo from this station waiting at any station.
251 * Reports whether a vehicle has ever tried to load the cargo at this station.
252 * This does not imply that there was cargo available for loading. Refer to GES_PICKUP for that.
253 * @return true if vehicle tried to load.
255 bool HasVehicleEverTriedLoading() const { return this->last_speed
!= 0; }
258 * Does this cargo have a rating at this station?
259 * @return true if the cargo has a rating, i.e. pickup has been attempted.
261 inline bool HasRating() const
263 return HasBit(this->acceptance_pickup
, GES_PICKUP
);
266 uint
GetSumFlowVia(StationID via
) const;
269 * Get the best next hop for a cargo packet from station source.
270 * @param source Source of the packet.
271 * @return The chosen next hop or INVALID_STATION if none was found.
273 inline StationID
GetVia(StationID source
) const
275 FlowStatMap::const_iterator
flow_it(this->flows
.find(source
));
276 return flow_it
!= this->flows
.end() ? flow_it
->second
.GetVia() : INVALID_STATION
;
280 * Get the best next hop for a cargo packet from station source, optionally
281 * excluding one or two stations.
282 * @param source Source of the packet.
283 * @param excluded If this station would be chosen choose the second best one instead.
284 * @param excluded2 Second station to be excluded, if != INVALID_STATION.
285 * @return The chosen next hop or INVALID_STATION if none was found.
287 inline StationID
GetVia(StationID source
, StationID excluded
, StationID excluded2
= INVALID_STATION
) const
289 FlowStatMap::const_iterator
flow_it(this->flows
.find(source
));
290 return flow_it
!= this->flows
.end() ? flow_it
->second
.GetVia(excluded
, excluded2
) : INVALID_STATION
;
295 struct Dock
: PooledItem
<Dock
, DockID
, 32, 64000> {
296 TileIndex xy
; ///< Position on the map
297 struct Dock
*next
; ///< Next dock at this station
299 /** Initialises a Dock */
300 inline Dock (TileIndex tile
= INVALID_TILE
) : xy(tile
) { }
303 #define FOR_ALL_DOCKS_FROM(var, start) FOR_ALL_ITEMS_FROM(Dock, dock_index, var, start)
304 #define FOR_ALL_DOCKS(var) FOR_ALL_DOCKS_FROM(var, 0)
306 /** All airport-related information. Only valid if tile != INVALID_TILE. */
307 struct Airport
: public TileArea
{
308 Airport() : TileArea(INVALID_TILE
, 0, 0) {}
310 uint64 flags
; ///< stores which blocks on the airport are taken. was 16 bit earlier on, then 32
311 byte type
; ///< Type of this airport, @see AirportTypes
312 byte layout
; ///< Airport layout number.
313 Direction rotation
; ///< How this airport is rotated.
315 PersistentStorage
*psa
; ///< Persistent storage for NewGRF airports.
318 * Get the AirportSpec that from the airport type of this airport. If there
319 * is no airport (\c tile == INVALID_TILE) then return the dummy AirportSpec.
320 * @return The AirportSpec for this airport.
322 const AirportSpec
*GetSpec() const
324 if (this->tile
== INVALID_TILE
) return &AirportSpec::dummy
;
325 return AirportSpec::Get(this->type
);
329 * Get the finite-state machine for this airport or the finite-state machine
330 * for the dummy airport in case this isn't an airport.
331 * @pre this->type < NEW_AIRPORT_OFFSET.
332 * @return The state machine for this airport.
334 const AirportFTAClass
*GetFTA() const
336 return this->GetSpec()->fsm
;
339 /** Check if this airport has at least one hangar. */
340 inline bool HasHangar() const
342 return this->GetSpec()->nof_depots
> 0;
346 * Add the tileoffset to the base tile of this airport but rotate it first.
347 * The base tile is the northernmost tile of this airport. This function
348 * helps to make sure that getting the tile of a hangar works even for
349 * rotated airport layouts without requiring a rotated array of hangar tiles.
350 * @param tidc The tilediff to add to the airport tile.
351 * @return The tile of this airport plus the rotated offset.
353 inline TileIndex
GetRotatedTileFromOffset(CoordDiff diff
) const
355 const AirportSpec
*as
= this->GetSpec();
356 switch (this->rotation
) {
357 case DIR_N
: return this->tile
+ ToTileIndexDiff(diff
);
359 case DIR_E
: return this->tile
+ TileDiffXY(diff
.y
, as
->size_x
- 1 - diff
.x
);
361 case DIR_S
: return this->tile
+ TileDiffXY(as
->size_x
- 1 - diff
.x
, as
->size_y
- 1 - diff
.y
);
363 case DIR_W
: return this->tile
+ TileDiffXY(as
->size_y
- 1 - diff
.y
, diff
.x
);
365 default: NOT_REACHED();
370 * Get the first tile of the given hangar.
371 * @param hangar_num The hangar to get the location of.
372 * @pre hangar_num < GetNumHangars().
373 * @return A tile with the given hangar.
375 inline TileIndex
GetHangarTile(uint hangar_num
) const
377 const AirportSpec
*as
= this->GetSpec();
378 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
379 if (as
->depot_table
[i
].hangar_num
== hangar_num
) {
380 return this->GetRotatedTileFromOffset(as
->depot_table
[i
].ti
);
387 * Get the exit direction of the hangar at a specific tile.
388 * @param tile The tile to query.
389 * @pre IsHangarTile(tile).
390 * @return The exit direction of the hangar, taking airport rotation into account.
392 inline Direction
GetHangarExitDirection(TileIndex tile
) const
394 const AirportSpec
*as
= this->GetSpec();
395 const HangarTileTable
*htt
= GetHangarDataByTile(tile
);
396 return ChangeDir(htt
->dir
, DirDifference(this->rotation
, as
->rotation
[0]));
400 * Get the hangar number of the hangar at a specific tile.
401 * @param tile The tile to query.
402 * @pre IsHangarTile(tile).
403 * @return The hangar number of the hangar at the given tile.
405 inline uint
GetHangarNum(TileIndex tile
) const
407 const HangarTileTable
*htt
= GetHangarDataByTile(tile
);
408 return htt
->hangar_num
;
411 /** Get the number of hangars on this airport. */
412 inline uint
GetNumHangars() const
416 const AirportSpec
*as
= this->GetSpec();
417 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
418 if (!HasBit(counted
, as
->depot_table
[i
].hangar_num
)) {
420 SetBit(counted
, as
->depot_table
[i
].hangar_num
);
428 * Retrieve hangar information of a hangar at a given tile.
429 * @param tile %Tile containing the hangar.
430 * @return The requested hangar information.
431 * @pre The \a tile must be at a hangar tile at an airport.
433 inline const HangarTileTable
*GetHangarDataByTile(TileIndex tile
) const
435 const AirportSpec
*as
= this->GetSpec();
436 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
437 if (this->GetRotatedTileFromOffset(as
->depot_table
[i
].ti
) == tile
) {
438 return as
->depot_table
+ i
;
445 typedef SmallVector
<Industry
*, 2> IndustryVector
;
447 /** Station data structure */
448 struct Station FINAL
: SpecializedStation
<Station
, false> {
450 RoadStop
*GetPrimaryRoadStop(RoadStopType type
) const
452 return type
== ROADSTOP_BUS
? bus_stops
: truck_stops
;
455 RoadStop
*GetPrimaryRoadStop(const struct RoadVehicle
*v
) const;
457 RoadStop
*bus_stops
; ///< All the road stops
458 TileArea bus_station
; ///< Tile area the bus 'station' part covers
459 RoadStop
*truck_stops
; ///< All the truck stops
460 TileArea truck_station
; ///< Tile area the truck 'station' part covers
462 Dock
*docks
; ///< All the docks
463 TileArea dock_area
; ///< Tile area the docks cover
465 Airport airport
; ///< Tile area the airport covers
467 IndustryType indtype
; ///< Industry type to get the name from
469 StationHadVehicleOfTypeByte had_vehicle_of_type
;
471 byte time_since_load
;
472 byte time_since_unload
;
474 byte last_vehicle_type
;
475 std::list
<Vehicle
*> loading_vehicles
;
476 GoodsEntry goods
[NUM_CARGO
]; ///< Goods at this station
477 uint32 always_accepted
; ///< Bitmask of always accepted cargo types (by houses, HQs, industry tiles when industry doesn't accept cargo)
479 IndustryVector industries_near
; ///< Cached list of industries near the station that can accept cargo, @see DeliverGoodsToIndustry()
481 Station(TileIndex tile
= INVALID_TILE
);
484 void AddFacility(StationFacility new_facility_bit
, TileIndex facil_xy
);
486 void MarkTilesDirty(bool cargo_change
) const;
488 void UpdateVirtCoord();
490 /* virtual */ uint
GetPlatformLength(TileIndex tile
, DiagDirection dir
) const;
491 /* virtual */ uint
GetPlatformLength(TileIndex tile
) const;
492 void RecomputeIndustriesNear();
493 static void RecomputeIndustriesNearForAll();
495 uint
GetCatchmentRadius() const;
496 Rect
GetCatchmentRect() const;
498 /* virtual */ inline bool TileBelongsToRailStation(TileIndex tile
) const
500 return IsRailStationTile(tile
) && GetStationIndex(tile
) == this->index
;
503 inline bool IsDockingTile(TileIndex tile
) const
506 for (d
= this->docks
; d
!= NULL
; d
= d
->next
) {
507 if (tile
== GetDockingTile(d
->xy
)) return true;
512 inline bool TileBelongsToAirport(TileIndex tile
) const
514 return IsAirportTile(tile
) && GetStationIndex(tile
) == this->index
;
517 /* virtual */ uint32
GetNewGRFVariable(const ResolverObject
&object
, byte variable
, byte parameter
, bool *available
) const;
519 /* virtual */ void GetTileArea(TileArea
*ta
, StationType type
) const;
522 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
524 /** Iterator to iterate over all tiles belonging to an airport. */
525 class AirportTileIterator
: public OrthogonalTileIterator
{
527 const Station
*st
; ///< The station the airport is a part of.
530 inline void Next() OVERRIDE
532 do this->OrthogonalTileIterator::Next();
533 while (this->tile
!= INVALID_TILE
&& !st
->TileBelongsToAirport(this->tile
));
538 * Construct the iterator.
539 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
541 AirportTileIterator(const Station
*st
) : OrthogonalTileIterator(st
->airport
), st(st
)
543 if (!st
->TileBelongsToAirport(this->tile
)) this->Next();
546 AirportTileIterator
*Clone() const OVERRIDE
548 return new AirportTileIterator(*this);
552 #endif /* STATION_BASE_H */