Make it possible to stop road vehicles from slowing down in curves so "diagonal"...
[openttd-joker.git] / src / station_base.h
blob53d3c03b16b201fe8785ca92e874fc819ea7e6b6
1 /* $Id: station_base.h 26166 2013-12-20 14:57:44Z fonsinchen $ */
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 "core/smallvec_type.hpp"
23 #include "3rdparty/cpp-btree/btree_map.h"
24 #include <map>
25 #include <set>
26 #include <vector>
28 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
29 extern StationPool _station_pool;
31 static const byte INITIAL_STATION_RATING = 175;
33 /**
34 * Flow statistics telling how much flow should be sent along a link. This is
35 * done by creating "flow shares" and using std::map's upper_bound() method to
36 * look them up with a random number. A flow share is the difference between a
37 * key in a map and the previous key. So one key in the map doesn't actually
38 * mean anything by itself.
40 class FlowStat {
41 public:
42 typedef btree::btree_map<uint32, StationID> SharesMap;
44 static const SharesMap empty_sharesmap;
46 /**
47 * Invalid constructor. This can't be called as a FlowStat must not be
48 * empty. However, the constructor must be defined and reachable for
49 * FlwoStat to be used in a std::map.
51 inline FlowStat() {NOT_REACHED();}
53 /**
54 * Create a FlowStat with an initial entry.
55 * @param st Station the initial entry refers to.
56 * @param flow Amount of flow for the initial entry.
57 * @param restricted If the flow to be added is restricted.
59 inline FlowStat(StationID st, uint flow, bool restricted = false)
61 assert(flow > 0);
62 this->shares[flow] = st;
63 this->unrestricted = restricted ? 0 : flow;
66 /**
67 * Add some flow to the end of the shares map. Only do that if you know
68 * that the station isn't in the map yet. Anything else may lead to
69 * inconsistencies.
70 * @param st Remote station.
71 * @param flow Amount of flow to be added.
72 * @param restricted If the flow to be added is restricted.
74 inline void AppendShare(StationID st, uint flow, bool restricted = false)
76 assert(flow > 0);
77 this->shares[(--this->shares.end())->first + flow] = st;
78 if (!restricted) this->unrestricted += flow;
81 uint GetShare(StationID st) const;
83 void ChangeShare(StationID st, int flow);
85 void RestrictShare(StationID st);
87 void ReleaseShare(StationID st);
89 void ScaleToMonthly(uint runtime);
91 /**
92 * Get the actual shares as a const pointer so that they can be iterated
93 * over.
94 * @return Actual shares.
96 inline const SharesMap *GetShares() const { return &this->shares; }
98 /**
99 * Return total amount of unrestricted shares.
100 * @return Amount of unrestricted shares.
102 inline uint GetUnrestricted() const { return this->unrestricted; }
105 * Swap the shares maps, and thus the content of this FlowStat with the
106 * other one.
107 * @param other FlowStat to swap with.
109 inline void SwapShares(FlowStat &other)
111 this->shares.swap(other.shares);
112 Swap(this->unrestricted, other.unrestricted);
116 * Get a station a package can be routed to. This done by drawing a
117 * random number between 0 and sum_shares and then looking that up in
118 * the map with lower_bound. So each share gets selected with a
119 * probability dependent on its flow. Do include restricted flows here.
120 * @param is_restricted Output if a restricted flow was chosen.
121 * @return A station ID from the shares map.
123 inline StationID GetViaWithRestricted(bool &is_restricted) const
125 assert(!this->shares.empty());
126 uint rand = RandomRange((--this->shares.end())->first);
127 is_restricted = rand >= this->unrestricted;
128 return this->shares.upper_bound(rand)->second;
132 * Get a station a package can be routed to. This done by drawing a
133 * random number between 0 and sum_shares and then looking that up in
134 * the map with lower_bound. So each share gets selected with a
135 * probability dependent on its flow. Don't include restricted flows.
136 * @return A station ID from the shares map.
138 inline StationID GetVia() const
140 assert(!this->shares.empty());
141 return this->unrestricted > 0 ?
142 this->shares.upper_bound(RandomRange(this->unrestricted))->second :
143 INVALID_STATION;
146 StationID GetVia(StationID excluded, StationID excluded2 = INVALID_STATION) const;
148 void Invalidate();
150 private:
151 SharesMap shares; ///< Shares of flow to be sent via specified station (or consumed locally).
152 uint unrestricted; ///< Limit for unrestricted shares.
155 /** Flow descriptions by origin stations. */
156 class FlowStatMap : public std::map<StationID, FlowStat> {
157 public:
158 uint GetFlow() const;
159 uint GetFlowVia(StationID via) const;
160 uint GetFlowFrom(StationID from) const;
161 uint GetFlowFromVia(StationID from, StationID via) const;
163 void AddFlow(StationID origin, StationID via, uint amount);
164 void PassOnFlow(StationID origin, StationID via, uint amount);
165 StationIDStack DeleteFlows(StationID via);
166 void RestrictFlows(StationID via);
167 void ReleaseFlows(StationID via);
168 void FinalizeLocalConsumption(StationID self);
172 * Stores station stats for a single cargo.
174 struct GoodsEntry {
175 /** Status of this cargo for the station. */
176 enum GoodsEntryStatus {
178 * Set when the station accepts the cargo currently for final deliveries.
179 * It is updated every STATION_ACCEPTANCE_TICKS ticks by checking surrounding tiles for acceptance >= 8/8.
181 GES_ACCEPTANCE,
184 * This indicates whether a cargo has a rating at the station.
185 * Set when cargo was ever waiting at the station.
186 * It is set when cargo supplied by surrounding tiles is moved to the station, or when
187 * arriving vehicles unload/transfer cargo without it being a final delivery.
189 * This flag is cleared after 255 * STATION_RATING_TICKS of not having seen a pickup.
191 GES_RATING,
194 * Set when a vehicle ever delivered cargo to the station for final delivery.
195 * This flag is never cleared.
197 GES_EVER_ACCEPTED,
200 * Set when cargo was delivered for final delivery last month.
201 * This flag is set to the value of GES_CURRENT_MONTH at the start of each month.
203 GES_LAST_MONTH,
206 * Set when cargo was delivered for final delivery this month.
207 * This flag is reset on the beginning of every month.
209 GES_CURRENT_MONTH,
212 * Set when cargo was delivered for final delivery during the current STATION_ACCEPTANCE_TICKS interval.
213 * This flag is reset every STATION_ACCEPTANCE_TICKS ticks.
215 GES_ACCEPTED_BIGTICK,
218 GoodsEntry() :
219 status(0),
220 time_since_pickup(255),
221 rating(INITIAL_STATION_RATING),
222 last_speed(0),
223 last_unprocessed_speed(0),
224 last_vehicle_type(VEH_INVALID),
225 last_age(255),
226 amount_fract(0),
227 link_graph(INVALID_LINK_GRAPH),
228 node(INVALID_NODE),
229 max_waiting_cargo(0),
230 punishment_triggered(false),
231 punishment_in_effect(false)
234 byte status; ///< Status of this cargo, see #GoodsEntryStatus.
237 * Number of rating-intervals (up to 255) since the last vehicle tried to load this cargo.
238 * The unit used is STATION_RATING_TICKS.
239 * This does not imply there was any cargo to load.
241 byte time_since_pickup;
243 byte rating; ///< %Station rating for this cargo.
246 * Maximum speed (up to 255) of the last vehicle that tried to load this cargo.
247 * This does not imply there was any cargo to load.
248 * The unit used is a special vehicle-specific speed unit for station ratings.
249 * - Trains: km-ish/h
250 * - RV: km-ish/h
251 * - Ships: 0.5 * km-ish/h
252 * - Aircraft: 8 * mph
254 byte last_speed;
257 * Speed of the last vehicle without the vehicle type postprocessing. Used to display in the detailed station rating tooltip.
259 uint16 last_unprocessed_speed;
262 * Type of the last vehicle.
264 byte last_vehicle_type;
267 * Age in years (up to 255) of the last vehicle that tried to load this cargo.
268 * This does not imply there was any cargo to load.
270 byte last_age;
272 byte amount_fract; ///< Fractional part of the amount in the cargo list
273 StationCargoList cargo; ///< The cargo packets of cargo waiting in this station
275 LinkGraphID link_graph; ///< Link graph this station belongs to.
276 NodeID node; ///< ID of node in link graph referring to this goods entry.
277 FlowStatMap flows; ///< Planned flows through this station.
278 uint max_waiting_cargo; ///< Max cargo from this station waiting at any station.
279 bool punishment_triggered; ///< A punishment was triggered. See below.
280 bool punishment_in_effect; ///< The station is being punished because the cargo is not being transported further at a destination.
283 * Reports whether a vehicle has ever tried to load the cargo at this station.
284 * This does not imply that there was cargo available for loading. Refer to GES_RATING for that.
285 * @return true if vehicle tried to load.
287 bool HasVehicleEverTriedLoading() const { return this->last_speed != 0; }
290 * Does this cargo have a rating at this station?
291 * @return true if the cargo has a rating, i.e. cargo has been moved to the station.
293 inline bool HasRating() const
295 return HasBit(this->status, GES_RATING);
299 * Get the best next hop for a cargo packet from station source.
300 * @param source Source of the packet.
301 * @return The chosen next hop or INVALID_STATION if none was found.
303 inline StationID GetVia(StationID source) const
305 FlowStatMap::const_iterator flow_it(this->flows.find(source));
306 return flow_it != this->flows.end() ? flow_it->second.GetVia() : INVALID_STATION;
310 * Get the best next hop for a cargo packet from station source, optionally
311 * excluding one or two stations.
312 * @param source Source of the packet.
313 * @param excluded If this station would be chosen choose the second best one instead.
314 * @param excluded2 Second station to be excluded, if != INVALID_STATION.
315 * @return The chosen next hop or INVALID_STATION if none was found.
317 inline StationID GetVia(StationID source, StationID excluded, StationID excluded2 = INVALID_STATION) const
319 FlowStatMap::const_iterator flow_it(this->flows.find(source));
320 return flow_it != this->flows.end() ? flow_it->second.GetVia(excluded, excluded2) : INVALID_STATION;
324 enum CatchmentType {
325 ACCEPTANCE = 0,
326 PRODUCTION = 1,
327 INDUSTRY = 2
330 struct StationCatchment {
331 std::map<TileIndex, std::set<TileIndex> > catchmentTiles;
332 public:
333 StationCatchment();
334 void MakeEmpty();
335 bool IsTileInCatchment(TileIndex tile) const;
336 bool IsEmpty() const;
337 void BeforeAddTile(TileIndex tile, uint catchmentRadius);
338 void BeforeAddRect(TileIndex tile, int w, int h, uint catchmentRadius);
339 void AfterRemoveTile(TileIndex tile, uint catchmentRadius);
340 void AfterRemoveRect(TileIndex tile, int w, int h, uint catchmentRadius);
343 /** All airport-related information. Only valid if tile != INVALID_TILE. */
344 struct Airport : public TileArea {
345 Airport() : TileArea(INVALID_TILE, 0, 0) {}
347 uint64 flags; ///< stores which blocks on the airport are taken. was 16 bit earlier on, then 32
348 byte type; ///< Type of this airport, @see AirportTypes
349 byte layout; ///< Airport layout number.
350 DirectionByte rotation; ///< How this airport is rotated.
352 PersistentStorage *psa; ///< Persistent storage for NewGRF airports.
355 * Get the AirportSpec that from the airport type of this airport. If there
356 * is no airport (\c tile == INVALID_TILE) then return the dummy AirportSpec.
357 * @return The AirportSpec for this airport.
359 const AirportSpec *GetSpec() const
361 if (this->tile == INVALID_TILE) return &AirportSpec::dummy;
362 return AirportSpec::Get(this->type);
366 * Get the finite-state machine for this airport or the finite-state machine
367 * for the dummy airport in case this isn't an airport.
368 * @pre this->type < NEW_AIRPORT_OFFSET.
369 * @return The state machine for this airport.
371 const AirportFTAClass *GetFTA() const
373 return this->GetSpec()->fsm;
376 /** Check if this airport has at least one hangar. */
377 inline bool HasHangar() const
379 return this->GetSpec()->nof_depots > 0;
383 * Add the tileoffset to the base tile of this airport but rotate it first.
384 * The base tile is the northernmost tile of this airport. This function
385 * helps to make sure that getting the tile of a hangar works even for
386 * rotated airport layouts without requiring a rotated array of hangar tiles.
387 * @param tidc The tilediff to add to the airport tile.
388 * @return The tile of this airport plus the rotated offset.
390 inline TileIndex GetRotatedTileFromOffset(TileIndexDiffC tidc) const
392 const AirportSpec *as = this->GetSpec();
393 switch (this->rotation) {
394 case DIR_N: return this->tile + ToTileIndexDiff(tidc);
396 case DIR_E: return this->tile + TileDiffXY(tidc.y, as->size_x - 1 - tidc.x);
398 case DIR_S: return this->tile + TileDiffXY(as->size_x - 1 - tidc.x, as->size_y - 1 - tidc.y);
400 case DIR_W: return this->tile + TileDiffXY(as->size_y - 1 - tidc.y, tidc.x);
402 default: NOT_REACHED();
407 * Get the first tile of the given hangar.
408 * @param hangar_num The hangar to get the location of.
409 * @pre hangar_num < GetNumHangars().
410 * @return A tile with the given hangar.
412 inline TileIndex GetHangarTile(uint hangar_num) const
414 const AirportSpec *as = this->GetSpec();
415 for (uint i = 0; i < as->nof_depots; i++) {
416 if (as->depot_table[i].hangar_num == hangar_num) {
417 return this->GetRotatedTileFromOffset(as->depot_table[i].ti);
420 NOT_REACHED();
424 * Get the exit direction of the hangar at a specific tile.
425 * @param tile The tile to query.
426 * @pre IsHangarTile(tile).
427 * @return The exit direction of the hangar, taking airport rotation into account.
429 inline Direction GetHangarExitDirection(TileIndex tile) const
431 const AirportSpec *as = this->GetSpec();
432 const HangarTileTable *htt = GetHangarDataByTile(tile);
433 return ChangeDir(htt->dir, DirDifference(this->rotation, as->rotation[0]));
437 * Get the hangar number of the hangar at a specific tile.
438 * @param tile The tile to query.
439 * @pre IsHangarTile(tile).
440 * @return The hangar number of the hangar at the given tile.
442 inline uint GetHangarNum(TileIndex tile) const
444 const HangarTileTable *htt = GetHangarDataByTile(tile);
445 return htt->hangar_num;
448 /** Get the number of hangars on this airport. */
449 inline uint GetNumHangars() const
451 uint num = 0;
452 uint counted = 0;
453 const AirportSpec *as = this->GetSpec();
454 for (uint i = 0; i < as->nof_depots; i++) {
455 if (!HasBit(counted, as->depot_table[i].hangar_num)) {
456 num++;
457 SetBit(counted, as->depot_table[i].hangar_num);
460 return num;
463 private:
465 * Retrieve hangar information of a hangar at a given tile.
466 * @param tile %Tile containing the hangar.
467 * @return The requested hangar information.
468 * @pre The \a tile must be at a hangar tile at an airport.
470 inline const HangarTileTable *GetHangarDataByTile(TileIndex tile) const
472 const AirportSpec *as = this->GetSpec();
473 for (uint i = 0; i < as->nof_depots; i++) {
474 if (this->GetRotatedTileFromOffset(as->depot_table[i].ti) == tile) {
475 return as->depot_table + i;
478 NOT_REACHED();
482 typedef SmallVector<Industry *, 2> IndustryVector;
484 /** Station data structure */
485 struct Station FINAL : SpecializedStation<Station, false> {
486 public:
487 RoadStop *GetPrimaryRoadStop(RoadStopType type) const
489 return type == ROADSTOP_BUS ? bus_stops : truck_stops;
492 RoadStop *GetPrimaryRoadStop(const struct RoadVehicle *v) const;
494 RoadStop *bus_stops; ///< All the road stops
495 TileArea bus_station; ///< Tile area the bus 'station' part covers
496 RoadStop *truck_stops; ///< All the truck stops
497 TileArea truck_station; ///< Tile area the truck 'station' part covers
498 Dock *docks; ///< All the docks
499 TileArea dock_station; ///< Tile area dock 'station' part covers
501 Airport airport; ///< Tile area the airport covers
503 IndustryType indtype; ///< Industry type to get the name from
505 StationHadVehicleOfTypeByte had_vehicle_of_type;
507 byte time_since_load;
508 byte time_since_unload;
510 byte last_vehicle_type;
511 std::vector<Vehicle *> loading_vehicles;
512 GoodsEntry goods[NUM_CARGO]; ///< Goods at this station
513 uint32 always_accepted; ///< Bitmask of always accepted cargo types (by houses, HQs, industry tiles when industry doesn't accept cargo)
515 IndustryVector industries_near; ///< Cached list of industries near the station that can accept cargo, @see DeliverGoodsToIndustry()
517 StationCatchment catchment;
519 uint8 station_cargo_history[NUM_CARGO * MAX_STATION_CARGO_HISTORY_DAYS]; ///< Station history of waiting cargo.
521 Station(TileIndex tile = INVALID_TILE);
522 ~Station();
524 void AddFacility(StationFacility new_facility_bit, TileIndex facil_xy);
526 void MarkTilesDirty(bool cargo_change) const;
528 void UpdateVirtCoord();
530 void UpdateCargoHistory();
532 /* virtual */ uint GetPlatformLength(TileIndex tile, DiagDirection dir) const;
533 /* virtual */ uint GetPlatformLength(TileIndex tile) const;
534 void RecomputeIndustriesNear();
535 static void RecomputeIndustriesNearForAll();
537 Dock *GetPrimaryDock() const { return docks; }
539 uint GetCatchmentRadius() const;
541 bool IsTileInCatchmentArea(const TileInfo* ti, CatchmentType type) const;
543 void MarkAcceptanceTilesDirty() const;
545 Rect GetCatchmentRectUsingRadius(uint radius) const;
547 inline Rect GetCatchmentRect() const
549 return GetCatchmentRectUsingRadius(this->GetCatchmentRadius());
552 /* virtual */ inline bool TileBelongsToRailStation(TileIndex tile) const
554 return IsRailStationTile(tile) && GetStationIndex(tile) == this->index;
557 inline bool TileBelongsToAirport(TileIndex tile) const
559 return IsAirportTile(tile) && GetStationIndex(tile) == this->index;
562 bool IsDockingTile(TileIndex tile) const;
564 /* virtual */ uint32 GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const;
566 /* virtual */ void GetTileArea(TileArea *ta, StationType type) const;
569 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
571 /** Iterator to iterate over all tiles belonging to an airport. */
572 class AirportTileIterator : public OrthogonalTileIterator {
573 private:
574 const Station *st; ///< The station the airport is a part of.
576 public:
578 * Construct the iterator.
579 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
581 AirportTileIterator(const Station *st) : OrthogonalTileIterator(st->airport), st(st)
583 if (!st->TileBelongsToAirport(this->tile)) ++(*this);
586 inline TileIterator& operator ++()
588 (*this).OrthogonalTileIterator::operator++();
589 while (this->tile != INVALID_TILE && !st->TileBelongsToAirport(this->tile)) {
590 (*this).OrthogonalTileIterator::operator++();
592 return *this;
595 virtual TileIterator *Clone() const
597 return new AirportTileIterator(*this);
601 #endif /* STATION_BASE_H */