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"
22 #include "map/tilearea.h"
25 static const byte INITIAL_STATION_RATING
= 175;
28 * Flow statistics telling how much flow should be sent along a link. This is
29 * done by creating "flow shares" and using std::map's upper_bound() method to
30 * look them up with a random number. A flow share is the difference between a
31 * key in a map and the previous key. So one key in the map doesn't actually
32 * mean anything by itself.
36 typedef std::map
<uint32
, StationID
> SharesMap
;
38 static const SharesMap empty_sharesmap
;
41 * Invalid constructor. This can't be called as a FlowStat must not be
42 * empty. However, the constructor must be defined and reachable for
43 * FlwoStat to be used in a std::map.
45 inline FlowStat() {NOT_REACHED();}
48 * Create a FlowStat with an initial entry.
49 * @param st Station the initial entry refers to.
50 * @param flow Amount of flow for the initial entry.
51 * @param restricted If the flow to be added is restricted.
53 inline FlowStat(StationID st
, uint flow
, bool restricted
= false)
56 this->shares
[flow
] = st
;
57 this->unrestricted
= restricted
? 0 : flow
;
61 * Add some flow to the end of the shares map. Only do that if you know
62 * that the station isn't in the map yet. Anything else may lead to
64 * @param st Remote station.
65 * @param flow Amount of flow to be added.
66 * @param restricted If the flow to be added is restricted.
68 inline void AppendShare(StationID st
, uint flow
, bool restricted
= false)
71 this->shares
[(--this->shares
.end())->first
+ flow
] = st
;
72 if (!restricted
) this->unrestricted
+= flow
;
75 uint
GetShare(StationID st
) const;
77 void ChangeShare(StationID st
, int flow
);
79 void RestrictShare(StationID st
);
81 void ReleaseShare(StationID st
);
83 void ScaleToMonthly(uint runtime
);
86 * Get the actual shares as a const pointer so that they can be iterated
88 * @return Actual shares.
90 inline const SharesMap
*GetShares() const { return &this->shares
; }
93 * Return total amount of unrestricted shares.
94 * @return Amount of unrestricted shares.
96 inline uint
GetUnrestricted() const { return this->unrestricted
; }
99 * Swap the shares maps, and thus the content of this FlowStat with the
101 * @param other FlowStat to swap with.
103 inline void SwapShares(FlowStat
&other
)
105 this->shares
.swap(other
.shares
);
106 Swap(this->unrestricted
, other
.unrestricted
);
110 * Get a station a package can be routed to. This done by drawing a
111 * random number between 0 and sum_shares and then looking that up in
112 * the map with lower_bound. So each share gets selected with a
113 * probability dependent on its flow. Do include restricted flows here.
114 * @param is_restricted Output if a restricted flow was chosen.
115 * @return A station ID from the shares map.
117 inline StationID
GetViaWithRestricted(bool &is_restricted
) const
119 assert(!this->shares
.empty());
120 uint rand
= RandomRange((--this->shares
.end())->first
);
121 is_restricted
= rand
>= this->unrestricted
;
122 return this->shares
.upper_bound(rand
)->second
;
126 * Get a station a package can be routed to. This done by drawing a
127 * random number between 0 and sum_shares and then looking that up in
128 * the map with lower_bound. So each share gets selected with a
129 * probability dependent on its flow. Don't include restricted flows.
130 * @return A station ID from the shares map.
132 inline StationID
GetVia() const
134 assert(!this->shares
.empty());
135 return this->unrestricted
> 0 ?
136 this->shares
.upper_bound(RandomRange(this->unrestricted
))->second
:
140 StationID
GetVia(StationID excluded
, StationID excluded2
= INVALID_STATION
) const;
145 SharesMap shares
; ///< Shares of flow to be sent via specified station (or consumed locally).
146 uint unrestricted
; ///< Limit for unrestricted shares.
149 /** Flow descriptions by origin stations. */
150 class FlowStatMap
: public std::map
<StationID
, FlowStat
> {
152 uint
GetFlow() const;
153 uint
GetFlowVia(StationID via
) const;
154 uint
GetFlowFrom(StationID from
) const;
155 uint
GetFlowFromVia(StationID from
, StationID via
) const;
157 void AddFlow(StationID origin
, StationID via
, uint amount
);
158 void PassOnFlow(StationID origin
, StationID via
, uint amount
);
159 void DeleteFlows (StationID via
, StationIDStack
*erased
= NULL
);
160 void RestrictFlows(StationID via
);
161 void ReleaseFlows(StationID via
);
162 void FinalizeLocalConsumption(StationID self
);
166 * Stores station stats for a single cargo.
169 /** Status of this cargo for the station. */
170 enum GoodsEntryStatus
{
172 * Set when the station accepts the cargo currently for final deliveries.
173 * It is updated every STATION_ACCEPTANCE_TICKS ticks by checking surrounding tiles for acceptance >= 8/8.
178 * This indicates whether a cargo has a rating at the station.
179 * Set when cargo was ever waiting at the station.
180 * It is set when cargo supplied by surrounding tiles is moved to the station, or when
181 * arriving vehicles unload/transfer cargo without it being a final delivery.
183 * This flag is cleared after 255 * STATION_RATING_TICKS of not having seen a pickup.
188 * Set when a vehicle ever delivered cargo to the station for final delivery.
189 * This flag is never cleared.
194 * Set when cargo was delivered for final delivery last month.
195 * This flag is set to the value of GES_CURRENT_MONTH at the start of each month.
200 * Set when cargo was delivered for final delivery this month.
201 * This flag is reset on the beginning of every month.
206 * Set when cargo was delivered for final delivery during the current STATION_ACCEPTANCE_TICKS interval.
207 * This flag is reset every STATION_ACCEPTANCE_TICKS ticks.
209 GES_ACCEPTED_BIGTICK
,
214 time_since_pickup(255),
215 rating(INITIAL_STATION_RATING
),
219 link_graph(INVALID_LINK_GRAPH
),
224 byte status
; ///< Status of this cargo, see #GoodsEntryStatus.
227 * Number of rating-intervals (up to 255) since the last vehicle tried to load this cargo.
228 * The unit used is STATION_RATING_TICKS.
229 * This does not imply there was any cargo to load.
231 byte time_since_pickup
;
233 byte rating
; ///< %Station rating for this cargo.
236 * Maximum speed (up to 255) of the last vehicle that tried to load this cargo.
237 * This does not imply there was any cargo to load.
238 * The unit used is a special vehicle-specific speed unit for station ratings.
241 * - Ships: 0.5 * km-ish/h
242 * - Aircraft: 8 * mph
247 * Age in years (up to 255) of the last vehicle that tried to load this cargo.
248 * This does not imply there was any cargo to load.
252 byte amount_fract
; ///< Fractional part of the amount in the cargo list
253 StationCargoList cargo
; ///< The cargo packets of cargo waiting in this station
255 LinkGraphID link_graph
; ///< Link graph this station belongs to.
256 NodeID node
; ///< ID of node in link graph referring to this goods entry.
257 FlowStatMap flows
; ///< Planned flows through this station.
258 uint max_waiting_cargo
; ///< Max cargo from this station waiting at any station.
261 * Reports whether a vehicle has ever tried to load the cargo at this station.
262 * This does not imply that there was cargo available for loading. Refer to GES_RATING for that.
263 * @return true if vehicle tried to load.
265 bool HasVehicleEverTriedLoading() const { return this->last_speed
!= 0; }
268 * Does this cargo have a rating at this station?
269 * @return true if the cargo has a rating, i.e. cargo has been moved to the station.
271 inline bool HasRating() const
273 return HasBit(this->status
, GES_RATING
);
277 * Get the best next hop for a cargo packet from station source.
278 * @param source Source of the packet.
279 * @return The chosen next hop or INVALID_STATION if none was found.
281 inline StationID
GetVia(StationID source
) const
283 FlowStatMap::const_iterator
flow_it(this->flows
.find(source
));
284 return flow_it
!= this->flows
.end() ? flow_it
->second
.GetVia() : INVALID_STATION
;
288 * Get the best next hop for a cargo packet from station source, optionally
289 * excluding one or two stations.
290 * @param source Source of the packet.
291 * @param excluded If this station would be chosen choose the second best one instead.
292 * @param excluded2 Second station to be excluded, if != INVALID_STATION.
293 * @return The chosen next hop or INVALID_STATION if none was found.
295 inline StationID
GetVia(StationID source
, StationID excluded
, StationID excluded2
= INVALID_STATION
) const
297 FlowStatMap::const_iterator
flow_it(this->flows
.find(source
));
298 return flow_it
!= this->flows
.end() ? flow_it
->second
.GetVia(excluded
, excluded2
) : INVALID_STATION
;
303 struct Dock
: PooledItem
<Dock
, DockID
, 32, 64000> {
304 TileIndex xy
; ///< Position on the map
305 struct Dock
*next
; ///< Next dock at this station
307 /** Initialises a Dock */
308 inline Dock (TileIndex tile
= INVALID_TILE
) : xy(tile
) { }
310 /** Check if a tile is the docking tile for this dock. */
311 bool is_docking_tile (TileIndex tile
)
313 return tile
== GetDockingTile (this->xy
);
317 #define FOR_ALL_DOCKS_FROM(var, start) FOR_ALL_ITEMS_FROM(Dock, dock_index, var, start)
318 #define FOR_ALL_DOCKS(var) FOR_ALL_DOCKS_FROM(var, 0)
320 /** All airport-related information. Only valid if tile != INVALID_TILE. */
321 struct Airport
: public TileArea
{
322 Airport() : TileArea(INVALID_TILE
, 0, 0) {}
324 uint64 flags
; ///< stores which blocks on the airport are taken. was 16 bit earlier on, then 32
325 byte type
; ///< Type of this airport, @see AirportTypes
326 byte layout
; ///< Airport layout number.
327 DirectionByte rotation
; ///< How this airport is rotated.
329 PersistentStorage
*psa
; ///< Persistent storage for NewGRF airports.
332 * Get the AirportSpec that from the airport type of this airport. If there
333 * is no airport (\c tile == INVALID_TILE) then return the dummy AirportSpec.
334 * @return The AirportSpec for this airport.
336 const AirportSpec
*GetSpec() const
338 if (this->tile
== INVALID_TILE
) return &AirportSpec::dummy
;
339 return AirportSpec::Get(this->type
);
343 * Get the finite-state machine for this airport or the finite-state machine
344 * for the dummy airport in case this isn't an airport.
345 * @pre this->type < NEW_AIRPORT_OFFSET.
346 * @return The state machine for this airport.
348 const AirportFTAClass
*GetFTA() const
350 return this->GetSpec()->fsm
;
353 /** Check if this airport has at least one hangar. */
354 inline bool HasHangar() const
356 return this->GetSpec()->nof_depots
> 0;
360 * Add the tileoffset to the base tile of this airport but rotate it first.
361 * The base tile is the northernmost tile of this airport. This function
362 * helps to make sure that getting the tile of a hangar works even for
363 * rotated airport layouts without requiring a rotated array of hangar tiles.
364 * @param tidc The tilediff to add to the airport tile.
365 * @return The tile of this airport plus the rotated offset.
367 inline TileIndex
GetRotatedTileFromOffset(CoordDiff diff
) const
369 const AirportSpec
*as
= this->GetSpec();
370 switch (this->rotation
) {
371 case DIR_N
: return this->tile
+ ToTileIndexDiff(diff
);
373 case DIR_E
: return this->tile
+ TileDiffXY(diff
.y
, as
->size_x
- 1 - diff
.x
);
375 case DIR_S
: return this->tile
+ TileDiffXY(as
->size_x
- 1 - diff
.x
, as
->size_y
- 1 - diff
.y
);
377 case DIR_W
: return this->tile
+ TileDiffXY(as
->size_y
- 1 - diff
.y
, diff
.x
);
379 default: NOT_REACHED();
384 * Get the first tile of the given hangar.
385 * @param hangar_num The hangar to get the location of.
386 * @pre hangar_num < GetNumHangars().
387 * @return A tile with the given hangar.
389 inline TileIndex
GetHangarTile(uint hangar_num
) const
391 const AirportSpec
*as
= this->GetSpec();
392 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
393 if (as
->depot_table
[i
].hangar_num
== hangar_num
) {
394 return this->GetRotatedTileFromOffset(as
->depot_table
[i
].ti
);
401 * Get the exit direction of the hangar at a specific tile.
402 * @param tile The tile to query.
403 * @pre IsHangarTile(tile).
404 * @return The exit direction of the hangar, taking airport rotation into account.
406 inline Direction
GetHangarExitDirection(TileIndex tile
) const
408 const AirportSpec
*as
= this->GetSpec();
409 const HangarTileTable
*htt
= GetHangarDataByTile(tile
);
410 return ChangeDir(htt
->dir
, DirDifference(this->rotation
, as
->rotation
[0]));
414 * Get the hangar number of the hangar at a specific tile.
415 * @param tile The tile to query.
416 * @pre IsHangarTile(tile).
417 * @return The hangar number of the hangar at the given tile.
419 inline uint
GetHangarNum(TileIndex tile
) const
421 const HangarTileTable
*htt
= GetHangarDataByTile(tile
);
422 return htt
->hangar_num
;
425 /** Get the number of hangars on this airport. */
426 inline uint
GetNumHangars() const
430 const AirportSpec
*as
= this->GetSpec();
431 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
432 if (!HasBit(counted
, as
->depot_table
[i
].hangar_num
)) {
434 SetBit(counted
, as
->depot_table
[i
].hangar_num
);
442 * Retrieve hangar information of a hangar at a given tile.
443 * @param tile %Tile containing the hangar.
444 * @return The requested hangar information.
445 * @pre The \a tile must be at a hangar tile at an airport.
447 inline const HangarTileTable
*GetHangarDataByTile(TileIndex tile
) const
449 const AirportSpec
*as
= this->GetSpec();
450 for (uint i
= 0; i
< as
->nof_depots
; i
++) {
451 if (this->GetRotatedTileFromOffset(as
->depot_table
[i
].ti
) == tile
) {
452 return as
->depot_table
+ i
;
459 typedef SmallVector
<Industry
*, 2> IndustryVector
;
461 /** Station data structure */
462 struct Station FINAL
: SpecializedStation
<Station
, false> {
464 RoadStop
*GetPrimaryRoadStop(RoadStopType type
) const
466 return type
== ROADSTOP_BUS
? bus_stops
: truck_stops
;
469 RoadStop
*GetPrimaryRoadStop(const struct RoadVehicle
*v
) const;
471 RoadStop
*bus_stops
; ///< All the road stops
472 TileArea bus_station
; ///< Tile area the bus 'station' part covers
473 RoadStop
*truck_stops
; ///< All the truck stops
474 TileArea truck_station
; ///< Tile area the truck 'station' part covers
476 Dock
*docks
; ///< All the docks
477 TileArea dock_area
; ///< Tile area the docks cover
479 Airport airport
; ///< Tile area the airport covers
481 IndustryType indtype
; ///< Industry type to get the name from
483 StationHadVehicleOfTypeByte had_vehicle_of_type
;
485 byte time_since_load
;
486 byte time_since_unload
;
488 byte last_vehicle_type
;
489 std::list
<Vehicle
*> loading_vehicles
;
490 GoodsEntry goods
[NUM_CARGO
]; ///< Goods at this station
491 uint32 always_accepted
; ///< Bitmask of always accepted cargo types (by houses, HQs, industry tiles when industry doesn't accept cargo)
493 IndustryVector industries_near
; ///< Cached list of industries near the station that can accept cargo, @see DeliverGoodsToIndustry()
495 Station(TileIndex tile
= INVALID_TILE
);
498 void AddFacility(StationFacility new_facility_bit
, TileIndex facil_xy
);
500 void MarkTilesDirty(bool cargo_change
) const;
502 void UpdateVirtCoord();
504 bool CanHandleCargo (CargoID cargo
) const
506 StationFacility f
= IsCargoInClass (cargo
, CC_PASSENGERS
) ? ~FACIL_TRUCK_STOP
: ~FACIL_BUS_STOP
;
507 return (this->facilities
& f
) != 0;
510 /* virtual */ uint
GetPlatformLength(TileIndex tile
, DiagDirection dir
) const;
511 /* virtual */ uint
GetPlatformLength(TileIndex tile
) const;
512 void RecomputeIndustriesNear();
513 static void RecomputeIndustriesNearForAll();
515 uint
GetCatchmentRadius() const;
516 TileArea
GetCatchmentArea() const;
518 inline bool TileBelongsToRailStation(TileIndex tile
) const
520 return IsRailStationTile(tile
) && GetStationIndex(tile
) == this->index
;
523 inline bool IsDockingTile(TileIndex tile
) const
526 for (d
= this->docks
; d
!= NULL
; d
= d
->next
) {
527 if (d
->is_docking_tile (tile
)) return true;
532 inline bool TileBelongsToAirport(TileIndex tile
) const
534 return IsAirportTile(tile
) && GetStationIndex(tile
) == this->index
;
537 uint32
GetNewGRFVariable (const struct GRFFile
*grffile
, byte variable
, byte parameter
, bool *available
) const OVERRIDE
;
539 /* virtual */ void GetTileArea(TileArea
*ta
, StationType type
) const;
542 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
544 #endif /* STATION_BASE_H */