OpenTTD
cargopacket.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef CARGOPACKET_H
11 #define CARGOPACKET_H
12 
13 #include "core/pool_type.hpp"
14 #include "economy_type.h"
15 #include "station_type.h"
16 #include "order_type.h"
17 #include "cargo_type.h"
18 #include "vehicle_type.h"
19 #include "core/multimap.hpp"
20 #include <list>
21 
23 typedef uint32 CargoPacketID;
24 struct CargoPacket;
25 
30 
31 struct GoodsEntry; // forward-declare for Stage() and RerouteStalePackets()
32 
33 template <class Tinst, class Tcont> class CargoList;
34 class StationCargoList; // forward-declare, so we can use it in VehicleCargoList.
35 extern const struct SaveLoad *GetCargoPacketDesc();
36 
37 typedef uint32 TileOrStationID;
38 
42 struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
43 private:
45  uint16 count;
49  StationID source;
51  union {
52  TileOrStationID loaded_at_xy;
53  TileOrStationID next_station;
54  };
55 
57  template <class Tinst, class Tcont> friend class CargoList;
58  friend class VehicleCargoList;
59  friend class StationCargoList;
61  friend const struct SaveLoad *GetCargoPacketDesc();
62 public:
64  static const uint16 MAX_COUNT = UINT16_MAX;
65 
66  CargoPacket();
67  CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
68  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);
69 
72 
73  CargoPacket *Split(uint new_size);
74  void Merge(CargoPacket *cp);
75  void Reduce(uint count);
76 
81  void SetLoadPlace(TileIndex load_place) { this->loaded_at_xy = load_place; }
82 
87  void SetNextStation(StationID next_station) { this->next_station = next_station; }
88 
93  void AddFeederShare(Money new_share) { this->feeder_share += new_share; }
94 
99  inline uint16 Count() const
100  {
101  return this->count;
102  }
103 
109  inline Money FeederShare() const
110  {
111  return this->feeder_share;
112  }
113 
120  inline Money FeederShare(uint part) const
121  {
122  return this->feeder_share * part / static_cast<uint>(this->count);
123  }
124 
131  inline byte DaysInTransit() const
132  {
133  return this->days_in_transit;
134  }
135 
141  {
142  return this->source_type;
143  }
144 
149  inline SourceID SourceSubsidyID() const
150  {
151  return this->source_id;
152  }
153 
158  inline StationID SourceStation() const
159  {
160  return this->source;
161  }
162 
167  inline TileIndex SourceStationXY() const
168  {
169  return this->source_xy;
170  }
171 
176  inline TileIndex LoadedAtXY() const
177  {
178  return this->loaded_at_xy;
179  }
180 
185  inline StationID NextStation() const
186  {
187  return this->next_station;
188  }
189 
190  static void InvalidateAllFrom(SourceType src_type, SourceID src);
191  static void InvalidateAllFrom(StationID sid);
192  static void AfterLoad();
193 };
194 
199 template <class Tinst, class Tcont>
200 class CargoList {
201 public:
203  typedef typename Tcont::iterator Iterator;
205  typedef typename Tcont::reverse_iterator ReverseIterator;
207  typedef typename Tcont::const_iterator ConstIterator;
209  typedef typename Tcont::const_reverse_iterator ConstReverseIterator;
210 
213  MTA_BEGIN = 0,
214  MTA_TRANSFER = 0,
218  MTA_END,
219  NUM_MOVE_TO_ACTION = MTA_END
220  };
221 
222 protected:
223  uint count;
225 
226  Tcont packets;
227 
228  void AddToCache(const CargoPacket *cp);
229 
230  void RemoveFromCache(const CargoPacket *cp, uint count);
231 
232  static bool TryMerge(CargoPacket *cp, CargoPacket *icp);
233 
234 public:
237 
238  ~CargoList();
239 
240  void OnCleanPool();
241 
246  inline const Tcont *Packets() const
247  {
248  return &this->packets;
249  }
250 
255  inline uint DaysInTransit() const
256  {
257  return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
258  }
259 
260  void InvalidateCache();
261 };
262 
263 typedef std::list<CargoPacket *> CargoPacketList;
264 
268 class VehicleCargoList : public CargoList<VehicleCargoList, CargoPacketList> {
269 protected:
272 
274  uint action_counts[NUM_MOVE_TO_ACTION];
275 
276  template<class Taction>
277  void ShiftCargo(Taction action);
278 
279  template<class Taction>
280  void PopCargo(Taction action);
281 
285  inline void AssertCountConsistency() const
286  {
287  assert(this->action_counts[MTA_KEEP] +
288  this->action_counts[MTA_DELIVER] +
289  this->action_counts[MTA_TRANSFER] +
290  this->action_counts[MTA_LOAD] == this->count);
291  }
292 
293  void AddToCache(const CargoPacket *cp);
294  void RemoveFromCache(const CargoPacket *cp, uint count);
295 
296  void AddToMeta(const CargoPacket *cp, MoveToAction action);
297  void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count);
298 
299  static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next,
300  StationID current_station, bool accepted, StationIDStack next_station);
301 
302 public:
304  friend class StationCargoList;
306  friend class CargoList<VehicleCargoList, CargoPacketList>;
308  friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
309 
310  friend class CargoShift;
311  friend class CargoTransfer;
312  friend class CargoDelivery;
313  template<class Tsource>
314  friend class CargoRemoval;
315  friend class CargoReturn;
316  friend class VehicleCargoReroute;
317 
322  inline StationID Source() const
323  {
324  return this->count == 0 ? INVALID_STATION : this->packets.front()->source;
325  }
326 
331  inline Money FeederShare() const
332  {
333  return this->feeder_share;
334  }
335 
341  inline uint ActionCount(MoveToAction action) const
342  {
343  return this->action_counts[action];
344  }
345 
351  inline uint StoredCount() const
352  {
353  return this->count - this->action_counts[MTA_LOAD];
354  }
355 
360  inline uint TotalCount() const
361  {
362  return this->count;
363  }
364 
369  inline uint ReservedCount() const
370  {
371  return this->action_counts[MTA_LOAD];
372  }
373 
378  inline uint UnloadCount() const
379  {
380  return this->action_counts[MTA_TRANSFER] + this->action_counts[MTA_DELIVER];
381  }
382 
387  inline uint RemainingCount() const
388  {
389  return this->action_counts[MTA_KEEP] + this->action_counts[MTA_LOAD];
390  }
391 
392  void Append(CargoPacket *cp, MoveToAction action = MTA_KEEP);
393 
394  void AgeCargo();
395 
396  void InvalidateCache();
397 
398  void SetTransferLoadPlace(TileIndex xy);
399 
400  bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment);
401 
407  inline void KeepAll()
408  {
409  this->action_counts[MTA_DELIVER] = this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_LOAD] = 0;
410  this->action_counts[MTA_KEEP] = this->count;
411  }
412 
413  /* Methods for moving cargo around. First parameter is always maximum
414  * amount of cargo to be moved. Second parameter is destination (if
415  * applicable), return value is amount of cargo actually moved. */
416 
417  template<MoveToAction Tfrom, MoveToAction Tto>
418  uint Reassign(uint max_move, TileOrStationID update = INVALID_TILE);
419  uint Return(uint max_move, StationCargoList *dest, StationID next_station);
420  uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment);
421  uint Shift(uint max_move, VehicleCargoList *dest);
422  uint Truncate(uint max_move = UINT_MAX);
423  uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
424 
432  static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
433  {
434  return cp1->source_xy == cp2->source_xy &&
435  cp1->days_in_transit == cp2->days_in_transit &&
436  cp1->source_type == cp2->source_type &&
437  cp1->source_id == cp2->source_id &&
438  cp1->loaded_at_xy == cp2->loaded_at_xy;
439  }
440 };
441 
443 typedef std::map<StationID, uint> StationCargoAmountMap;
444 
448 class StationCargoList : public CargoList<StationCargoList, StationCargoPacketMap> {
449 protected:
452 
454 
455 public:
459  friend const struct SaveLoad *GetGoodsDesc();
460 
461  friend class CargoLoad;
462  friend class CargoTransfer;
463  template<class Tsource>
464  friend class CargoRemoval;
465  friend class CargoReservation;
466  friend class CargoReturn;
467  friend class StationCargoReroute;
468 
469  static void InvalidateAllFrom(SourceType src_type, SourceID src);
470 
471  template<class Taction>
472  bool ShiftCargo(Taction &action, StationID next);
473 
474  template<class Taction>
475  uint ShiftCargo(Taction action, StationIDStack next, bool include_invalid);
476 
477  void Append(CargoPacket *cp, StationID next);
478 
484  inline bool HasCargoFor(StationIDStack next) const
485  {
486  while (!next.IsEmpty()) {
487  if (this->packets.find(next.Pop()) != this->packets.end()) return true;
488  }
489  /* Packets for INVALID_STTION can go anywhere. */
490  return this->packets.find(INVALID_STATION) != this->packets.end();
491  }
492 
497  inline StationID Source() const
498  {
499  return this->count == 0 ? INVALID_STATION : this->packets.begin()->second.front()->source;
500  }
501 
507  inline uint AvailableCount() const
508  {
509  return this->count;
510  }
511 
516  inline uint ReservedCount() const
517  {
518  return this->reserved_count;
519  }
520 
526  inline uint TotalCount() const
527  {
528  return this->count + this->reserved_count;
529  }
530 
531  /* Methods for moving cargo around. First parameter is always maximum
532  * amount of cargo to be moved. Second parameter is destination (if
533  * applicable), return value is amount of cargo actually moved. */
534 
535  uint Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
536  uint Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
537  uint Truncate(uint max_move = UINT_MAX, StationCargoAmountMap *cargo_per_source = nullptr);
538  uint Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
539 
547  static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
548  {
549  return cp1->source_xy == cp2->source_xy &&
550  cp1->days_in_transit == cp2->days_in_transit &&
551  cp1->source_type == cp2->source_type &&
552  cp1->source_id == cp2->source_id;
553  }
554 };
555 
556 #endif /* CARGOPACKET_H */
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:322
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:146
uint RemainingCount() const
Returns the sum of cargo to be kept in the vehicle at the current station.
Definition: cargopacket.h:387
Action of rerouting cargo in a station.
Definition: cargoaction.h:126
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:224
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
Definition: cargopacket.h:93
const SaveLoad * GetVehicleDescription(VehicleType vt)
Make it possible to make the saveload tables "friends" of other classes.
Definition: vehicle_sl.cpp:580
Tcont::reverse_iterator ReverseIterator
The reverse iterator for our container.
Definition: cargopacket.h:205
Minimal stack that uses a pool to avoid pointers.
Types related to orders.
CargoList that is used for stations.
Definition: cargopacket.h:448
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:24
StationID NextStation() const
Gets the ID of station the cargo wants to go next.
Definition: cargopacket.h:185
CargoPacket()
Create a new packet for savegame loading.
Definition: cargopacket.cpp:27
CargoList()
Create the cargo list.
Definition: cargopacket.h:236
Money FeederShare() const
Gets the amount of money already paid to earlier vehicles in the feeder chain.
Definition: cargopacket.h:109
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Action of shifting cargo from one vehicle to another.
Definition: cargoaction.h:106
void SetLoadPlace(TileIndex load_place)
Sets the tile where the packet was loaded last.
Definition: cargopacket.h:81
uint reserved_count
Amount of cargo being reserved for loading.
Definition: cargopacket.h:453
uint TotalCount() const
Returns total count of cargo at the station, including cargo which is already reserved for loading...
Definition: cargopacket.h:526
static void AfterLoad()
Savegame conversion for cargopackets.
uint DaysInTransit() const
Returns average number of days in transit for a cargo entity.
Definition: cargopacket.h:255
CargoList< VehicleCargoList, CargoPacketList > Parent
The (direct) parent of this class.
Definition: cargopacket.h:271
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:331
TileIndex SourceStationXY() const
Gets the coordinates of the cargo&#39;s source station.
Definition: cargopacket.h:167
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
Definition: cargopacket.h:360
Action of rerouting cargo staged for transfer in a vehicle.
Definition: cargoaction.h:134
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...
Definition 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:351
uint32 CargoPacketID
Unique identifier for a single cargo packet.
Definition: cargopacket.h:23
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:153
uint16 count
The amount of cargo in this packet.
Definition: cargopacket.h:45
SourceID SourceSubsidyID() const
Gets the ID of the cargo&#39;s source.
Definition: cargopacket.h:149
Tcont packets
The cargo packets in this list.
Definition: cargopacket.h:226
bool IsEmpty() const
Check if the stack is empty.
uint count
Cache for the number of cargo entities.
Definition: cargopacket.h:223
SourceType source_type
Type of source_id.
Definition: cargopacket.h:47
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:17
Types related to stations.
SourceID source_id
Index of source, INVALID_SOURCE if unknown/invalid.
Definition: cargopacket.h:48
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:259
byte DaysInTransit() const
Gets the number of days this cargo has been in transit.
Definition: cargopacket.h:131
~CargoPacket()
Destroy the packet.
Definition: cargopacket.h:71
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:547
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:42
StationID SourceStation() const
Gets the ID of the station where the cargo was loaded for the first time.
Definition: cargopacket.h:158
TileOrStationID next_station
Station where the cargo wants to go next.
Definition: cargopacket.h:53
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:432
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:341
void AssertCountConsistency() const
Assert that the designation counts add up.
Definition: cargopacket.h:285
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
Definition: cargopacket.cpp:89
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:140
Simple collection class for a list of cargo packets.
Definition: cargopacket.h:33
Helper class to perform the cargo payment.
Definition: economy_base.h:24
Deliver the cargo to some town or industry.
Definition: cargopacket.h:215
Action of transferring cargo from a vehicle to a station.
Definition: cargoaction.h:71
MoveToAction
Kind of actions that could be done with packets on move.
Definition: cargopacket.h:212
uint ReservedCount() const
Returns sum of cargo reserved for loading onto vehicles.
Definition: cargopacket.h:516
Base class for all PoolItems.
Definition: pool_type.hpp:188
Action of reserving cargo from a station to be loaded onto a vehicle.
Definition: cargoaction.h:89
Base class for all pools.
Definition: pool_type.hpp:82
Keep the cargo in the vehicle.
Definition: cargopacket.h:216
TileIndex LoadedAtXY() const
Gets the coordinates of the cargo&#39;s last loading station.
Definition: cargopacket.h:176
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:497
CargoList that is used for vehicles.
Definition: cargopacket.h:268
uint max_move
Maximum amount of cargo to be moved with this action.
Definition: cargoaction.h:58
Tcont::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:207
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Action of returning previously reserved cargo from the vehicle to the station.
Definition: cargoaction.h:97
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:152
Types related to vehicles.
Abstract action of removing cargo from a vehicle or a station.
Definition: cargoaction.h:20
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:49
CargoPacketPool _cargopacket_pool
The actual pool with cargo packets.
Tcont::const_reverse_iterator ConstReverseIterator
The const reverse iterator for our container.
Definition: cargopacket.h:209
Money feeder_share
Cache for the feeder share.
Definition: cargopacket.h:273
bool HasCargoFor(StationIDStack next) const
Check for cargo headed for a specific station.
Definition: cargopacket.h:484
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:369
Load the cargo from the station.
Definition: cargopacket.h:217
void KeepAll()
Marks all cargo in the vehicle as to be kept.
Definition: cargopacket.h:407
void Merge(CargoPacket *cp)
Merge another packet into this one.
SaveLoad type struct.
Definition: saveload.h:496
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:246
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
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:99
static const uint16 MAX_COUNT
Maximum number of items in a single cargo packet.
Definition: cargopacket.h:64
uint AvailableCount() const
Returns sum of cargo still available for loading at the sation.
Definition: cargopacket.h:507
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
Definition: cargopacket.h:44
Multimap with deterministic ordering of items with equal keys.
Action of final delivery of cargo.
Definition: cargoaction.h:39
CargoList< StationCargoList, StationCargoPacketMap > Parent
The (direct) parent of this class.
Definition: cargopacket.h:451
void SetNextStation(StationID next_station)
Sets the station where the packet is supposed to go next.
Definition: cargopacket.h:87
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:52
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:50
Action of loading cargo from a station onto a vehicle.
Definition: cargoaction.h:79
Tcont::iterator Iterator
The iterator for our container.
Definition: cargopacket.h:203
Money FeederShare(uint part) const
Gets part of the amount of money already paid to earlier vehicles in the feeder chain.
Definition: cargopacket.h:120
byte days_in_transit
Amount of days this packet has been in transit.
Definition: cargopacket.h:46
Source/destination is an industry.
Definition: cargo_type.h:147
uint UnloadCount() const
Returns sum of cargo to be moved out of the vehicle at the current station.
Definition: cargopacket.h:378