Go to the documentation of this file.
1 /* $Id: cargopacket.h 26660 2014-06-21 19:52:52Z 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 <>.
8  */
12 #ifndef CARGOPACKET_H
13 #define CARGOPACKET_H
15 #include "core/pool_type.hpp"
16 #include "economy_type.h"
17 #include "station_type.h"
18 #include "order_type.h"
19 #include "cargo_type.h"
20 #include "vehicle_type.h"
21 #include "core/multimap.hpp"
22 #include <list>
25 typedef uint32 CargoPacketID;
26 struct CargoPacket;
33 struct GoodsEntry; // forward-declare for Stage() and RerouteStalePackets()
35 template <class Tinst, class Tcont> class CargoList;
36 class StationCargoList; // forward-declare, so we can use it in VehicleCargoList.
37 extern const struct SaveLoad *GetCargoPacketDesc();
39 typedef uint32 TileOrStationID;
44 struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
45 private:
47  uint16 count;
51  StationID source;
53  union {
54  TileOrStationID loaded_at_xy;
55  TileOrStationID next_station;
56  };
59  template <class Tinst, class Tcont> friend class CargoList;
60  friend class VehicleCargoList;
61  friend class StationCargoList;
63  friend const struct SaveLoad *GetCargoPacketDesc();
64 public:
66  static const uint16 MAX_COUNT = UINT16_MAX;
68  CargoPacket();
69  CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
70  CargoPacket(uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, Money feeder_share = 0, SourceType source_type = ST_INDUSTRY, SourceID source_id = INVALID_SOURCE);
75  CargoPacket *Split(uint new_size);
76  void Merge(CargoPacket *cp);
77  void Reduce(uint count);
83  void SetLoadPlace(TileIndex load_place) { this->loaded_at_xy = load_place; }
89  void SetNextStation(StationID next_station) { this->next_station = next_station; }
95  void AddFeederShare(Money new_share) { this->feeder_share += new_share; }
101  inline uint16 Count() const
102  {
103  return this->count;
104  }
111  inline Money FeederShare() const
112  {
113  return this->feeder_share;
114  }
122  inline Money FeederShare(uint part) const
123  {
124  return this->feeder_share * part / static_cast<uint>(this->count);
125  }
133  inline byte DaysInTransit() const
134  {
135  return this->days_in_transit;
136  }
143  {
144  return this->source_type;
145  }
151  inline SourceID SourceSubsidyID() const
152  {
153  return this->source_id;
154  }
160  inline StationID SourceStation() const
161  {
162  return this->source;
163  }
169  inline TileIndex SourceStationXY() const
170  {
171  return this->source_xy;
172  }
178  inline TileIndex LoadedAtXY() const
179  {
180  return this->loaded_at_xy;
181  }
187  inline StationID NextStation() const
188  {
189  return this->next_station;
190  }
192  static void InvalidateAllFrom(SourceType src_type, SourceID src);
193  static void InvalidateAllFrom(StationID sid);
194  static void AfterLoad();
195 };
202 #define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
214 template <class Tinst, class Tcont>
215 class CargoList {
216 public:
218  typedef typename Tcont::iterator Iterator;
220  typedef typename Tcont::reverse_iterator ReverseIterator;
222  typedef typename Tcont::const_iterator ConstIterator;
224  typedef typename Tcont::const_reverse_iterator ConstReverseIterator;
228  MTA_BEGIN = 0,
229  MTA_TRANSFER = 0,
233  MTA_END,
235  };
237 protected:
238  uint count;
241  Tcont packets;
243  void AddToCache(const CargoPacket *cp);
245  void RemoveFromCache(const CargoPacket *cp, uint count);
247  static bool TryMerge(CargoPacket *cp, CargoPacket *icp);
249 public:
253  ~CargoList();
255  void OnCleanPool();
261  inline const Tcont *Packets() const
262  {
263  return &this->packets;
264  }
270  inline uint DaysInTransit() const
271  {
272  return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
273  }
275  void InvalidateCache();
276 };
278 typedef std::list<CargoPacket *> CargoPacketList;
283 class VehicleCargoList : public CargoList<VehicleCargoList, CargoPacketList> {
284 protected:
289  uint action_counts[NUM_MOVE_TO_ACTION];
291  template<class Taction>
292  void ShiftCargo(Taction action);
294  template<class Taction>
295  void PopCargo(Taction action);
300  inline void AssertCountConsistency() const
301  {
302  assert(this->action_counts[MTA_KEEP] +
303  this->action_counts[MTA_DELIVER] +
304  this->action_counts[MTA_TRANSFER] +
305  this->action_counts[MTA_LOAD] == this->count);
306  }
308  void AddToCache(const CargoPacket *cp);
309  void RemoveFromCache(const CargoPacket *cp, uint count);
311  void AddToMeta(const CargoPacket *cp, MoveToAction action);
312  void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count);
314  static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next,
315  StationID current_station, bool accepted, StationIDStack next_station);
317 public:
319  friend class StationCargoList;
321  friend class CargoList<VehicleCargoList, CargoPacketList>;
323  friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
325  friend class CargoShift;
326  friend class CargoTransfer;
327  friend class CargoDelivery;
328  template<class Tsource>
329  friend class CargoRemoval;
330  friend class CargoReturn;
331  friend class VehicleCargoReroute;
337  inline StationID Source() const
338  {
339  return this->count == 0 ? INVALID_STATION : this->packets.front()->source;
340  }
346  inline Money FeederShare() const
347  {
348  return this->feeder_share;
349  }
356  inline uint ActionCount(MoveToAction action) const
357  {
358  return this->action_counts[action];
359  }
366  inline uint StoredCount() const
367  {
368  return this->count - this->action_counts[MTA_LOAD];
369  }
375  inline uint TotalCount() const
376  {
377  return this->count;
378  }
384  inline uint ReservedCount() const
385  {
386  return this->action_counts[MTA_LOAD];
387  }
393  inline uint UnloadCount() const
394  {
395  return this->action_counts[MTA_TRANSFER] + this->action_counts[MTA_DELIVER];
396  }
402  inline uint RemainingCount() const
403  {
404  return this->action_counts[MTA_KEEP] + this->action_counts[MTA_LOAD];
405  }
407  void Append(CargoPacket *cp, MoveToAction action = MTA_KEEP);
409  void AgeCargo();
411  void InvalidateCache();
413  void SetTransferLoadPlace(TileIndex xy);
415  bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment);
422  inline void KeepAll()
423  {
424  this->action_counts[MTA_DELIVER] = this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_LOAD] = 0;
425  this->action_counts[MTA_KEEP] = this->count;
426  }
428  /* Methods for moving cargo around. First parameter is always maximum
429  * amount of cargo to be moved. Second parameter is destination (if
430  * applicable), return value is amount of cargo actually moved. */
432  template<MoveToAction Tfrom, MoveToAction Tto>
433  uint Reassign(uint max_move, TileOrStationID update = INVALID_TILE);
434  uint Return(uint max_move, StationCargoList *dest, StationID next_station);
435  uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment);
436  uint Shift(uint max_move, VehicleCargoList *dest);
437  uint Truncate(uint max_move = UINT_MAX);
438  uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
447  static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
448  {
449  return cp1->source_xy == cp2->source_xy &&
450  cp1->days_in_transit == cp2->days_in_transit &&
451  cp1->source_type == cp2->source_type &&
452  cp1->source_id == cp2->source_id &&
453  cp1->loaded_at_xy == cp2->loaded_at_xy;
454  }
455 };
458 typedef std::map<StationID, uint> StationCargoAmountMap;
463 class StationCargoList : public CargoList<StationCargoList, StationCargoPacketMap> {
464 protected:
470 public:
474  friend const struct SaveLoad *GetGoodsDesc();
476  friend class CargoLoad;
477  friend class CargoTransfer;
478  template<class Tsource>
479  friend class CargoRemoval;
480  friend class CargoReservation;
481  friend class CargoReturn;
482  friend class StationCargoReroute;
484  static void InvalidateAllFrom(SourceType src_type, SourceID src);
486  template<class Taction>
487  bool ShiftCargo(Taction &action, StationID next);
489  template<class Taction>
490  uint ShiftCargo(Taction action, StationIDStack next, bool include_invalid);
492  void Append(CargoPacket *cp, StationID next);
499  inline bool HasCargoFor(StationIDStack next) const
500  {
501  while (!next.IsEmpty()) {
502  if (this->packets.find(next.Pop()) != this->packets.end()) return true;
503  }
504  /* Packets for INVALID_STTION can go anywhere. */
505  return this->packets.find(INVALID_STATION) != this->packets.end();
506  }
512  inline StationID Source() const
513  {
514  return this->count == 0 ? INVALID_STATION : this->packets.begin()->second.front()->source;
515  }
522  inline uint AvailableCount() const
523  {
524  return this->count;
525  }
531  inline uint ReservedCount() const
532  {
533  return this->reserved_count;
534  }
541  inline uint TotalCount() const
542  {
543  return this->count + this->reserved_count;
544  }
546  /* Methods for moving cargo around. First parameter is always maximum
547  * amount of cargo to be moved. Second parameter is destination (if
548  * applicable), return value is amount of cargo actually moved. */
550  uint Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
551  uint Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
552  uint Truncate(uint max_move = UINT_MAX, StationCargoAmountMap *cargo_per_source = NULL);
553  uint Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
562  static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
563  {
564  return cp1->source_xy == cp2->source_xy &&
565  cp1->days_in_transit == cp2->days_in_transit &&
566  cp1->source_type == cp2->source_type &&
567  cp1->source_id == cp2->source_id;
568  }
569 };
571 #endif /* CARGOPACKET_H */
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:337
uint RemainingCount() const
Returns the sum of cargo to be kept in the vehicle at the current station.
Definition: cargopacket.h:402
Action of rerouting cargo in a station.
Definition: cargoaction.h:128
uint cargo_days_in_transit
Cache for the sum of number of days in transit of each entity; comparable to man-hours.
Definition: cargopacket.h:239
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
Definition: cargopacket.h:95
const SaveLoad * GetVehicleDescription(VehicleType vt)
Make it possible to make the saveload tables "friends" of other classes.
Definition: vehicle_sl.cpp:574
Tcont::reverse_iterator ReverseIterator
The reverse iterator for our container.
Definition: cargopacket.h:220
Minimal stack that uses a pool to avoid pointers.
SourceTypeByte source_type
Type of source_id.
Definition: cargopacket.h:49
Types related to orders.
CargoList that is used for stations.
Definition: cargopacket.h:463
Pool< CargoPacket, CargoPacketID, 1024, 0xFFF000, PT_NORMAL, true, false > CargoPacketPool
Type of the pool for cargo packets for a little over 16 million packets.
Definition: cargopacket.h:26
StationID NextStation() const
Gets the ID of station the cargo wants to go next.
Definition: cargopacket.h:187
Create a new packet for savegame loading.
Definition: cargopacket.cpp:29
Create the cargo list.
Definition: cargopacket.h:251
Money FeederShare() const
Gets the amount of money already paid to earlier vehicles in the feeder chain.
Definition: cargopacket.h:111
Action of shifting cargo from one vehicle to another.
Definition: cargoaction.h:108
void SetLoadPlace(TileIndex load_place)
Sets the tile where the packet was loaded last.
Definition: cargopacket.h:83
uint reserved_count
Amount of cargo being reserved for loading.
Definition: cargopacket.h:468
uint TotalCount() const
Returns total count of cargo at the station, including cargo which is already reserved for loading...
Definition: cargopacket.h:541
static void AfterLoad()
Savegame conversion for cargopackets.
uint DaysInTransit() const
Returns average number of days in transit for a cargo entity.
Definition: cargopacket.h:270
CargoList< VehicleCargoList, CargoPacketList > Parent
The (direct) parent of this class.
Definition: cargopacket.h:286
Stores station stats for a single cargo.
Definition: station_base.h:170
Money FeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:346
TileIndex SourceStationXY() const
Gets the coordinates of the cargo&#39;s source station.
Definition: cargopacket.h:169
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
Definition: cargopacket.h:375
Action of rerouting cargo staged for transfer in a vehicle.
Definition: cargoaction.h:136
static void InvalidateAllFrom(SourceType src_type, SourceID src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
Types related to cargoes...
Defintion of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:366
uint32 CargoPacketID
Unique identifier for a single cargo packet.
Definition: cargopacket.h:25
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:147
uint16 count
The amount of cargo in this packet.
Definition: cargopacket.h:47
SourceID SourceSubsidyID() const
Gets the ID of the cargo&#39;s source.
Definition: cargopacket.h:151
Tcont packets
The cargo packets in this list.
Definition: cargopacket.h:241
bool IsEmpty() const
Check if the stack is empty.
uint count
Cache for the number of cargo entities.
Definition: cargopacket.h:238
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:19
Types related to stations.
SourceID source_id
Index of source, INVALID_SOURCE if unknown/invalid.
Definition: cargopacket.h:50
const SaveLoad * GetGoodsDesc()
Wrapper function to get the GoodsEntry&#39;s internal structure while some of the variables itself are pr...
Definition: station_sl.cpp:266
byte DaysInTransit() const
Gets the number of days this cargo has been in transit.
Definition: cargopacket.h:133
Destroy the packet.
Definition: cargopacket.h:73
Types related to the economy.
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
Are two the two CargoPackets mergeable in the context of a list of CargoPackets for a Vehicle...
Definition: cargopacket.h:562
Available vehicle types.
Definition: vehicle_type.h:21
const struct SaveLoad * GetCargoPacketDesc()
Wrapper function to get the CargoPacket&#39;s internal structure while some of the variables itself are p...
Container for cargo from the same location and time.
Definition: cargopacket.h:44
StationID SourceStation() const
Gets the ID of the station where the cargo was loaded for the first time.
Definition: cargopacket.h:160
TileOrStationID next_station
Station where the cargo wants to go next.
Definition: cargopacket.h:55
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
Are two the two CargoPackets mergeable in the context of a list of CargoPackets for a Vehicle...
Definition: cargopacket.h:447
Titem Pop()
Pop an item from the stack.
uint ActionCount(MoveToAction action) const
Returns the amount of cargo designated for a given purpose.
Definition: cargopacket.h:356
void AssertCountConsistency() const
Assert that the designation counts add up.
Definition: cargopacket.h:300
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
Definition: cargopacket.cpp:91
static int Return(HSQUIRRELVM vm, T t)
To return a value to squirrel, we call this function.
SourceType SourceSubsidyType() const
Gets the type of the cargo&#39;s source.
Definition: cargopacket.h:142
Simple collection class for a list of cargo packets.
Definition: cargopacket.h:35
Helper class to perform the cargo payment.
Definition: economy_base.h:26
Deliver the cargo to some town or industry.
Definition: cargopacket.h:230
Action of transferring cargo from a vehicle to a station.
Definition: cargoaction.h:73
Kind of actions that could be done with packets on move.
Definition: cargopacket.h:227
uint ReservedCount() const
Returns sum of cargo reserved for loading onto vehicles.
Definition: cargopacket.h:531
Base class for all PoolItems.
Definition: pool_type.hpp:146
Action of reserving cargo from a station to be loaded onto a vehicle.
Definition: cargoaction.h:91
Base class for all pools.
Definition: pool_type.hpp:83
Types of cargo source and destination.
Definition: cargo_type.h:139
Keep the cargo in the vehicle.
Definition: cargopacket.h:231
TileIndex LoadedAtXY() const
Gets the coordinates of the cargo&#39;s last loading station.
Definition: cargopacket.h:178
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:512
CargoList that is used for vehicles.
Definition: cargopacket.h:283
uint max_move
Maximum amount of cargo to be moved with this action.
Definition: cargoaction.h:60
Tcont::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:222
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Action of returning previously reserved cargo from the vehicle to the station.
Definition: cargoaction.h:99
Source/destination is an industry.
Definition: cargo_type.h:140
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:146
Types related to vehicles.
Abstract action of removing cargo from a vehicle or a station.
Definition: cargoaction.h:22
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:51
CargoPacketPool _cargopacket_pool
The actual pool with cargo packets.
Tcont::const_reverse_iterator ConstReverseIterator
The const reverse iterator for our container.
Definition: cargopacket.h:224
Money feeder_share
Cache for the feeder share.
Definition: cargopacket.h:288
bool HasCargoFor(StationIDStack next) const
Check for cargo headed for a specific station.
Definition: cargopacket.h:499
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
uint ReservedCount() const
Returns sum of reserved cargo.
Definition: cargopacket.h:384
Load the cargo from the station.
Definition: cargopacket.h:232
void KeepAll()
Marks all cargo in the vehicle as to be kept.
Definition: cargopacket.h:422
void Merge(CargoPacket *cp)
Merge another packet into this one.
SaveLoad type struct.
Definition: saveload.h:208
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:261
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
friend const struct SaveLoad * GetCargoPacketDesc()
We want this to be saved, right?
uint16 Count() const
Gets the number of &#39;items&#39; in this packet.
Definition: cargopacket.h:101
static const uint16 MAX_COUNT
Maximum number of items in a single cargo packet.
Definition: cargopacket.h:66
uint AvailableCount() const
Returns sum of cargo still available for loading at the sation.
Definition: cargopacket.h:522
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
Definition: cargopacket.h:46
Multimap with deterministic ordering of items with equal keys.
Action of final delivery of cargo.
Definition: cargoaction.h:41
CargoList< StationCargoList, StationCargoPacketMap > Parent
The (direct) parent of this class.
Definition: cargopacket.h:466
void SetNextStation(StationID next_station)
Sets the station where the packet is supposed to go next.
Definition: cargopacket.h:89
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:54
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:52
Action of loading cargo from a station onto a vehicle.
Definition: cargoaction.h:81
Tcont::iterator Iterator
The iterator for our container.
Definition: cargopacket.h:218
Money FeederShare(uint part) const
Gets part of the amount of money already paid to earlier vehicles in the feeder chain.
Definition: cargopacket.h:122
byte days_in_transit
Amount of days this packet has been in transit.
Definition: cargopacket.h:48
uint UnloadCount() const
Returns sum of cargo to be moved out of the vehicle at the current station.
Definition: cargopacket.h:393