Add a new method Station::CanHandleCargo
[openttd/fttd.git] / src / station_base.h
blob12e337d41b97d9fb944011f289388a93e114f452
1 /* $Id$ */
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 */
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>
24 static const byte INITIAL_STATION_RATING = 175;
26 /**
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.
33 class FlowStat {
34 public:
35 typedef std::map<uint32, StationID> SharesMap;
37 /**
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();}
44 /**
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)
51 assert(flow > 0);
52 this->shares[flow] = st;
53 this->unrestricted = flow;
56 /**
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
59 * inconsistencies.
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)
66 assert(flow > 0);
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);
81 /**
82 * Get the actual shares as a const pointer so that they can be iterated
83 * over.
84 * @return Actual shares.
86 inline const SharesMap *GetShares() const { return &this->shares; }
88 /**
89 * Return total amount of unrestricted shares.
90 * @return Amount of unrestricted shares.
92 inline uint GetUnrestricted() const { return this->unrestricted; }
94 /**
95 * Swap the shares maps, and thus the content of this FlowStat with the
96 * other one.
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 :
133 INVALID_STATION;
136 StationID GetVia(StationID excluded, StationID excluded2 = INVALID_STATION) const;
138 void Invalidate();
140 private:
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> {
147 public:
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.
159 struct GoodsEntry {
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.
166 GES_ACCEPTANCE,
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.
175 GES_PICKUP,
178 * Set when a vehicle ever delivered cargo to the station for final delivery.
179 * This flag is never cleared.
181 GES_EVER_ACCEPTED,
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.
187 GES_LAST_MONTH,
190 * Set when cargo was delivered for final delivery this month.
191 * This flag is reset on the beginning of every month.
193 GES_CURRENT_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,
202 GoodsEntry() :
203 acceptance_pickup(0),
204 time_since_pickup(255),
205 rating(INITIAL_STATION_RATING),
206 last_speed(0),
207 last_age(255),
208 amount_fract(0),
209 link_graph(INVALID_LINK_GRAPH),
210 node(INVALID_NODE),
211 max_waiting_cargo(0)
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.
229 * - Trains: km-ish/h
230 * - RV: km-ish/h
231 * - Ships: 0.5 * km-ish/h
232 * - Aircraft: 8 * mph
234 byte last_speed;
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.
240 byte last_age;
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;
294 /** A Dock */
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);
383 NOT_REACHED();
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
414 uint num = 0;
415 uint counted = 0;
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)) {
419 num++;
420 SetBit(counted, as->depot_table[i].hangar_num);
423 return num;
426 private:
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;
441 NOT_REACHED();
445 typedef SmallVector<Industry *, 2> IndustryVector;
447 /** Station data structure */
448 struct Station FINAL : SpecializedStation<Station, false> {
449 public:
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);
482 ~Station();
484 void AddFacility(StationFacility new_facility_bit, TileIndex facil_xy);
486 void MarkTilesDirty(bool cargo_change) const;
488 void UpdateVirtCoord();
490 bool CanHandleCargo (CargoID cargo) const
492 StationFacility f = IsCargoInClass (cargo, CC_PASSENGERS) ? ~FACIL_TRUCK_STOP : ~FACIL_BUS_STOP;
493 return (this->facilities & f) != 0;
496 /* virtual */ uint GetPlatformLength(TileIndex tile, DiagDirection dir) const;
497 /* virtual */ uint GetPlatformLength(TileIndex tile) const;
498 void RecomputeIndustriesNear();
499 static void RecomputeIndustriesNearForAll();
501 uint GetCatchmentRadius() const;
502 Rect GetCatchmentRect() const;
504 /* virtual */ inline bool TileBelongsToRailStation(TileIndex tile) const
506 return IsRailStationTile(tile) && GetStationIndex(tile) == this->index;
509 inline bool IsDockingTile(TileIndex tile) const
511 Dock *d;
512 for (d = this->docks; d != NULL; d = d->next) {
513 if (tile == GetDockingTile(d->xy)) return true;
515 return false;
518 inline bool TileBelongsToAirport(TileIndex tile) const
520 return IsAirportTile(tile) && GetStationIndex(tile) == this->index;
523 /* virtual */ uint32 GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const;
525 /* virtual */ void GetTileArea(TileArea *ta, StationType type) const;
528 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
530 /** Iterator to iterate over all tiles belonging to an airport. */
531 class AirportTileIterator : public OrthogonalTileIterator {
532 private:
533 const Station *st; ///< The station the airport is a part of.
535 protected:
536 inline void Next() OVERRIDE
538 do this->OrthogonalTileIterator::Next();
539 while (this->tile != INVALID_TILE && !st->TileBelongsToAirport(this->tile));
542 public:
544 * Construct the iterator.
545 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
547 AirportTileIterator(const Station *st) : OrthogonalTileIterator(st->airport), st(st)
549 if (!st->TileBelongsToAirport(this->tile)) this->Next();
552 AirportTileIterator *Clone() const OVERRIDE
554 return new AirportTileIterator(*this);
558 #endif /* STATION_BASE_H */