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/>.
8 /** @file cargopacket.h Base class for cargo packets. */
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 "saveload/saveload.h"
23 /** Unique identifier for a single cargo packet. */
24 typedef uint32 CargoPacketID
;
27 /** Type of the pool for cargo packets for a little over 16 million packets. */
28 typedef Pool
<CargoPacket
, CargoPacketID
, 1024, 0xFFF000, PT_NORMAL
, true, false> CargoPacketPool
;
29 /** The actual pool with cargo packets. */
30 extern CargoPacketPool _cargopacket_pool
;
32 struct GoodsEntry
; // forward-declare for Stage() and RerouteStalePackets()
34 template <class Tinst
, class Tcont
> class CargoList
;
35 class StationCargoList
; // forward-declare, so we can use it in VehicleCargoList.
36 extern SaveLoadTable
GetCargoPacketDesc();
38 typedef uint32 TileOrStationID
;
41 * Container for cargo from the same location and time.
43 struct CargoPacket
: CargoPacketPool::PoolItem
<&_cargopacket_pool
> {
45 Money feeder_share
; ///< Value of feeder pickup to be paid for on delivery of cargo.
46 uint16 count
; ///< The amount of cargo in this packet.
47 byte days_in_transit
; ///< Amount of days this packet has been in transit.
48 SourceType source_type
; ///< Type of \c source_id.
49 SourceID source_id
; ///< Index of source, INVALID_SOURCE if unknown/invalid.
50 StationID source
; ///< The station where the cargo came from first.
51 TileIndex source_xy
; ///< The origin of the cargo (first station in feeder chain).
53 TileOrStationID loaded_at_xy
; ///< Location where this cargo has been loaded into the vehicle.
54 TileOrStationID next_station
; ///< Station where the cargo wants to go next.
57 /** The CargoList caches, thus needs to know about it. */
58 template <class Tinst
, class Tcont
> friend class CargoList
;
59 friend class VehicleCargoList
;
60 friend class StationCargoList
;
61 /** We want this to be saved, right? */
62 friend SaveLoadTable
GetCargoPacketDesc();
64 /** Maximum number of items in a single cargo packet. */
65 static const uint16 MAX_COUNT
= UINT16_MAX
;
68 CargoPacket(StationID source
, TileIndex source_xy
, uint16 count
, SourceType source_type
, SourceID source_id
);
69 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
);
71 /** Destroy the packet. */
74 CargoPacket
*Split(uint new_size
);
75 void Merge(CargoPacket
*cp
);
76 void Reduce(uint count
);
79 * Sets the tile where the packet was loaded last.
80 * @param load_place Tile where the packet was loaded last.
82 void SetLoadPlace(TileIndex load_place
) { this->loaded_at_xy
= load_place
; }
85 * Sets the station where the packet is supposed to go next.
86 * @param next_station Next station the packet should go to.
88 void SetNextStation(StationID next_station
) { this->next_station
= next_station
; }
91 * Adds some feeder share to the packet.
92 * @param new_share Feeder share to be added.
94 void AddFeederShare(Money new_share
) { this->feeder_share
+= new_share
; }
97 * Gets the number of 'items' in this packet.
100 inline uint16
Count() const
106 * Gets the amount of money already paid to earlier vehicles in
108 * @return Feeder share.
110 inline Money
FeederShare() const
112 return this->feeder_share
;
116 * Gets part of the amount of money already paid to earlier vehicles in
118 * @param part Amount of cargo to get the share for.
119 * @return Feeder share for the given amount of cargo.
121 inline Money
FeederShare(uint part
) const
123 return this->feeder_share
* part
/ static_cast<uint
>(this->count
);
127 * Gets the number of days this cargo has been in transit.
128 * This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and
129 * it is capped at 255.
130 * @return Length this cargo has been in transit.
132 inline byte
DaysInTransit() const
134 return this->days_in_transit
;
138 * Gets the type of the cargo's source. industry, town or head quarter.
139 * @return Source type.
141 inline SourceType
SourceSubsidyType() const
143 return this->source_type
;
147 * Gets the ID of the cargo's source. An IndustryID, TownID or CompanyID.
150 inline SourceID
SourceSubsidyID() const
152 return this->source_id
;
156 * Gets the ID of the station where the cargo was loaded for the first time.
159 inline StationID
SourceStation() const
165 * Gets the coordinates of the cargo's source station.
166 * @return Source station's coordinates.
168 inline TileIndex
SourceStationXY() const
170 return this->source_xy
;
174 * Gets the coordinates of the cargo's last loading station.
175 * @return Last loading station's coordinates.
177 inline TileIndex
LoadedAtXY() const
179 return this->loaded_at_xy
;
183 * Gets the ID of station the cargo wants to go next.
184 * @return Next station for this packets.
186 inline StationID
NextStation() const
188 return this->next_station
;
191 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
192 static void InvalidateAllFrom(StationID sid
);
193 static void AfterLoad();
197 * Simple collection class for a list of cargo packets.
198 * @tparam Tinst Actual instantiation of this cargo list.
200 template <class Tinst
, class Tcont
>
203 /** The iterator for our container. */
204 typedef typename
Tcont::iterator Iterator
;
205 /** The reverse iterator for our container. */
206 typedef typename
Tcont::reverse_iterator ReverseIterator
;
207 /** The const iterator for our container. */
208 typedef typename
Tcont::const_iterator ConstIterator
;
209 /** The const reverse iterator for our container. */
210 typedef typename
Tcont::const_reverse_iterator ConstReverseIterator
;
212 /** Kind of actions that could be done with packets on move. */
215 MTA_TRANSFER
= 0, ///< Transfer the cargo to the station.
216 MTA_DELIVER
, ///< Deliver the cargo to some town or industry.
217 MTA_KEEP
, ///< Keep the cargo in the vehicle.
218 MTA_LOAD
, ///< Load the cargo from the station.
220 NUM_MOVE_TO_ACTION
= MTA_END
224 uint count
; ///< Cache for the number of cargo entities.
225 uint cargo_days_in_transit
; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours.
227 Tcont packets
; ///< The cargo packets in this list.
229 void AddToCache(const CargoPacket
*cp
);
231 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
233 static bool TryMerge(CargoPacket
*cp
, CargoPacket
*icp
);
236 /** Create the cargo list. */
244 * Returns a pointer to the cargo packet list (so you can iterate over it etc).
245 * @return Pointer to the packet list.
247 inline const Tcont
*Packets() const
249 return &this->packets
;
253 * Returns average number of days in transit for a cargo entity.
254 * @return The before mentioned number.
256 inline uint
DaysInTransit() const
258 return this->count
== 0 ? 0 : this->cargo_days_in_transit
/ this->count
;
261 void InvalidateCache();
264 typedef std::list
<CargoPacket
*> CargoPacketList
;
267 * CargoList that is used for vehicles.
269 class VehicleCargoList
: public CargoList
<VehicleCargoList
, CargoPacketList
> {
271 /** The (direct) parent of this class. */
272 typedef CargoList
<VehicleCargoList
, CargoPacketList
> Parent
;
274 Money feeder_share
; ///< Cache for the feeder share.
275 uint action_counts
[NUM_MOVE_TO_ACTION
]; ///< Counts of cargo to be transferred, delivered, kept and loaded.
277 template<class Taction
>
278 void ShiftCargo(Taction action
);
280 template<class Taction
>
281 void PopCargo(Taction action
);
284 * Assert that the designation counts add up.
286 inline void AssertCountConsistency() const
288 assert(this->action_counts
[MTA_KEEP
] +
289 this->action_counts
[MTA_DELIVER
] +
290 this->action_counts
[MTA_TRANSFER
] +
291 this->action_counts
[MTA_LOAD
] == this->count
);
294 void AddToCache(const CargoPacket
*cp
);
295 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
297 void AddToMeta(const CargoPacket
*cp
, MoveToAction action
);
298 void RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
);
300 static MoveToAction
ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
301 StationID current_station
, bool accepted
, StationIDStack next_station
);
304 /** The station cargo list needs to control the unloading. */
305 friend class StationCargoList
;
306 /** The super class ought to know what it's doing. */
307 friend class CargoList
<VehicleCargoList
, CargoPacketList
>;
308 /* So we can use private/protected variables in the saveload code */
309 friend class SlVehicleCommon
;
311 friend class CargoShift
;
312 friend class CargoTransfer
;
313 friend class CargoDelivery
;
314 template<class Tsource
>
315 friend class CargoRemoval
;
316 friend class CargoReturn
;
317 friend class VehicleCargoReroute
;
320 * Returns source of the first cargo packet in this list.
321 * @return The before mentioned source.
323 inline StationID
Source() const
325 return this->count
== 0 ? INVALID_STATION
: this->packets
.front()->source
;
329 * Returns total sum of the feeder share for all packets.
330 * @return The before mentioned number.
332 inline Money
FeederShare() const
334 return this->feeder_share
;
338 * Returns the amount of cargo designated for a given purpose.
339 * @param action Action the cargo is designated for.
340 * @return Amount of cargo designated for the given action.
342 inline uint
ActionCount(MoveToAction action
) const
344 return this->action_counts
[action
];
348 * Returns sum of cargo on board the vehicle (ie not only
350 * @return Cargo on board the vehicle.
352 inline uint
StoredCount() const
354 return this->count
- this->action_counts
[MTA_LOAD
];
358 * Returns sum of cargo, including reserved cargo.
359 * @return Sum of cargo.
361 inline uint
TotalCount() const
367 * Returns sum of reserved cargo.
368 * @return Sum of reserved cargo.
370 inline uint
ReservedCount() const
372 return this->action_counts
[MTA_LOAD
];
376 * Returns sum of cargo to be moved out of the vehicle at the current station.
377 * @return Cargo to be moved.
379 inline uint
UnloadCount() const
381 return this->action_counts
[MTA_TRANSFER
] + this->action_counts
[MTA_DELIVER
];
385 * Returns the sum of cargo to be kept in the vehicle at the current station.
386 * @return Cargo to be kept or loaded.
388 inline uint
RemainingCount() const
390 return this->action_counts
[MTA_KEEP
] + this->action_counts
[MTA_LOAD
];
393 void Append(CargoPacket
*cp
, MoveToAction action
= MTA_KEEP
);
397 void InvalidateCache();
399 void SetTransferLoadPlace(TileIndex xy
);
401 bool Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8 order_flags
, const GoodsEntry
*ge
, CargoPayment
*payment
);
404 * Marks all cargo in the vehicle as to be kept. This is mostly useful for
405 * loading old savegames. When loading is aborted the reserved cargo has
406 * to be returned first.
408 inline void KeepAll()
410 this->action_counts
[MTA_DELIVER
] = this->action_counts
[MTA_TRANSFER
] = this->action_counts
[MTA_LOAD
] = 0;
411 this->action_counts
[MTA_KEEP
] = this->count
;
414 /* Methods for moving cargo around. First parameter is always maximum
415 * amount of cargo to be moved. Second parameter is destination (if
416 * applicable), return value is amount of cargo actually moved. */
418 template<MoveToAction Tfrom
, MoveToAction Tto
>
419 uint
Reassign(uint max_move
, TileOrStationID update
= INVALID_TILE
);
420 uint
Return(uint max_move
, StationCargoList
*dest
, StationID next_station
);
421 uint
Unload(uint max_move
, StationCargoList
*dest
, CargoPayment
*payment
);
422 uint
Shift(uint max_move
, VehicleCargoList
*dest
);
423 uint
Truncate(uint max_move
= UINT_MAX
);
424 uint
Reroute(uint max_move
, VehicleCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
);
427 * Are the two CargoPackets mergeable in the context of
428 * a list of CargoPackets for a Vehicle?
429 * @param cp1 First CargoPacket.
430 * @param cp2 Second CargoPacket.
431 * @return True if they are mergeable.
433 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
435 return cp1
->source_xy
== cp2
->source_xy
&&
436 cp1
->days_in_transit
== cp2
->days_in_transit
&&
437 cp1
->source_type
== cp2
->source_type
&&
438 cp1
->source_id
== cp2
->source_id
&&
439 cp1
->loaded_at_xy
== cp2
->loaded_at_xy
;
443 typedef MultiMap
<StationID
, CargoPacket
*> StationCargoPacketMap
;
444 typedef std::map
<StationID
, uint
> StationCargoAmountMap
;
447 * CargoList that is used for stations.
449 class StationCargoList
: public CargoList
<StationCargoList
, StationCargoPacketMap
> {
451 /** The (direct) parent of this class. */
452 typedef CargoList
<StationCargoList
, StationCargoPacketMap
> Parent
;
454 uint reserved_count
; ///< Amount of cargo being reserved for loading.
457 /** The super class ought to know what it's doing. */
458 friend class CargoList
<StationCargoList
, StationCargoPacketMap
>;
459 /* So we can use private/protected variables in the saveload code */
460 friend class SlStationGoods
;
462 friend class CargoLoad
;
463 friend class CargoTransfer
;
464 template<class Tsource
>
465 friend class CargoRemoval
;
466 friend class CargoReservation
;
467 friend class CargoReturn
;
468 friend class StationCargoReroute
;
470 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
472 template<class Taction
>
473 bool ShiftCargo(Taction
&action
, StationID next
);
475 template<class Taction
>
476 uint
ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
);
478 void Append(CargoPacket
*cp
, StationID next
);
481 * Check for cargo headed for a specific station.
482 * @param next Station the cargo is headed for.
483 * @return If there is any cargo for that station.
485 inline bool HasCargoFor(StationIDStack next
) const
487 while (!next
.IsEmpty()) {
488 if (this->packets
.find(next
.Pop()) != this->packets
.end()) return true;
490 /* Packets for INVALID_STATION can go anywhere. */
491 return this->packets
.find(INVALID_STATION
) != this->packets
.end();
495 * Returns source of the first cargo packet in this list.
496 * @return The before mentioned source.
498 inline StationID
Source() const
500 return this->count
== 0 ? INVALID_STATION
: this->packets
.begin()->second
.front()->source
;
504 * Returns sum of cargo still available for loading at the sation.
505 * (i.e. not counting cargo which is already reserved for loading)
506 * @return Cargo on board the vehicle.
508 inline uint
AvailableCount() const
514 * Returns sum of cargo reserved for loading onto vehicles.
515 * @return Cargo reserved for loading.
517 inline uint
ReservedCount() const
519 return this->reserved_count
;
523 * Returns total count of cargo at the station, including
524 * cargo which is already reserved for loading.
525 * @return Total cargo count.
527 inline uint
TotalCount() const
529 return this->count
+ this->reserved_count
;
532 /* Methods for moving cargo around. First parameter is always maximum
533 * amount of cargo to be moved. Second parameter is destination (if
534 * applicable), return value is amount of cargo actually moved. */
536 uint
Reserve(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next
);
537 uint
Load(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next
);
538 uint
Truncate(uint max_move
= UINT_MAX
, StationCargoAmountMap
*cargo_per_source
= nullptr);
539 uint
Reroute(uint max_move
, StationCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
);
542 * Are the two CargoPackets mergeable in the context of
543 * a list of CargoPackets for a Station?
544 * @param cp1 First CargoPacket.
545 * @param cp2 Second CargoPacket.
546 * @return True if they are mergeable.
548 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
550 return cp1
->source_xy
== cp2
->source_xy
&&
551 cp1
->days_in_transit
== cp2
->days_in_transit
&&
552 cp1
->source_type
== cp2
->source_type
&&
553 cp1
->source_id
== cp2
->source_id
;
557 #endif /* CARGOPACKET_H */