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/>.
10 /** @file cargopacket.h Base class for cargo packets. */
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 "saveload/saveload_data.h"
25 /** Unique identifier for a single cargo packet. */
26 typedef uint32 CargoPacketID
;
29 struct GoodsEntry
; // forward-declare for Stage() and RerouteStalePackets()
31 template <class Tinst
, class Tcont
> class CargoList
;
32 class StationCargoList
; // forward-declare, so we can use it in VehicleCargoList.
33 extern const struct SaveLoad
*GetCargoPacketDesc();
35 typedef uint32 TileOrStationID
;
38 * Container for cargo from the same location and time.
40 struct CargoPacket
: PooledItem
<CargoPacket
, CargoPacketID
, 1024, 0xFFF000, PT_NORMAL
, true, false> {
42 Money feeder_share
; ///< Value of feeder pickup to be paid for on delivery of cargo.
43 uint16 count
; ///< The amount of cargo in this packet.
44 byte days_in_transit
; ///< Amount of days this packet has been in transit.
45 SourceTypeByte source_type
; ///< Type of \c source_id.
46 SourceID source_id
; ///< Index of source, INVALID_SOURCE if unknown/invalid.
47 StationID source
; ///< The station where the cargo came from first.
48 TileIndex source_xy
; ///< The origin of the cargo (first station in feeder chain).
50 TileOrStationID loaded_at_xy
; ///< Location where this cargo has been loaded into the vehicle.
51 TileOrStationID next_station
; ///< Station where the cargo wants to go next.
54 /** The CargoList caches, thus needs to know about it. */
55 template <class Tinst
, class Tcont
> friend class CargoList
;
56 friend class VehicleCargoList
;
57 friend class StationCargoList
;
58 /** We want this to be saved, right? */
59 friend const struct SaveLoad
*GetCargoPacketDesc();
61 /** Maximum number of items in a single cargo packet. */
62 static const uint16 MAX_COUNT
= UINT16_MAX
;
65 CargoPacket(StationID source
, TileIndex source_xy
, uint16 count
, SourceType source_type
, SourceID source_id
);
66 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
);
68 /** Destroy the packet. */
71 CargoPacket
*Split(uint new_size
);
72 void Merge(CargoPacket
*cp
);
73 void Reduce(uint count
);
76 * Sets the tile where the packet was loaded last.
77 * @param load_place Tile where the packet was loaded last.
79 void SetLoadPlace(TileIndex load_place
) { this->loaded_at_xy
= load_place
; }
82 * Sets the station where the packet is supposed to go next.
83 * @param next_station Next station the packet should go to.
85 void SetNextStation(StationID next_station
) { this->next_station
= next_station
; }
88 * Adds some feeder share to the packet.
89 * @param new_share Feeder share to be added.
91 void AddFeederShare(Money new_share
) { this->feeder_share
+= new_share
; }
94 * Gets the number of 'items' in this packet.
97 inline uint16
Count() const
103 * Gets the amount of money already paid to earlier vehicles in
105 * @return Feeder share.
107 inline Money
FeederShare() const
109 return this->feeder_share
;
113 * Gets part of the amount of money already paid to earlier vehicles in
115 * @param part Amount of cargo to get the share for.
116 * @return Feeder share for the given amount of cargo.
118 inline Money
FeederShare(uint part
) const
120 return this->feeder_share
* part
/ static_cast<uint
>(this->count
);
124 * Gets the number of days this cargo has been in transit.
125 * This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and
126 * it is capped at 255.
127 * @return Length this cargo has been in transit.
129 inline byte
DaysInTransit() const
131 return this->days_in_transit
;
135 * Gets the type of the cargo's source. industry, town or head quarter.
136 * @return Source type.
138 inline SourceType
SourceSubsidyType() const
140 return this->source_type
;
144 * Gets the ID of the cargo's source. An IndustryID, TownID or CompanyID.
147 inline SourceID
SourceSubsidyID() const
149 return this->source_id
;
153 * Gets the ID of the station where the cargo was loaded for the first time.
156 inline SourceID
SourceStation() const
162 * Gets the coordinates of the cargo's source station.
163 * @return Source station's coordinates.
165 inline TileIndex
SourceStationXY() const
167 return this->source_xy
;
171 * Gets the coordinates of the cargo's last loading station.
172 * @return Last loading station's coordinates.
174 inline TileIndex
LoadedAtXY() const
176 return this->loaded_at_xy
;
180 * Gets the ID of station the cargo wants to go next.
181 * @return Next station for this packets.
183 inline StationID
NextStation() const
185 return this->next_station
;
188 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
189 static void InvalidateAllFrom(StationID sid
);
190 static void AfterLoad(const SavegameTypeVersion
*stv
);
194 * Iterate over all _valid_ cargo packets from the given start.
195 * @param var Variable used as "iterator".
196 * @param start Cargo packet ID of the first packet to iterate over.
198 #define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
201 * Iterate over all _valid_ cargo packets from the begin of the pool.
202 * @param var Variable used as "iterator".
204 #define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
207 * Simple collection class for a list of cargo packets.
208 * @tparam Tinst Actual instantiation of this cargo list.
210 template <class Tinst
, class Tcont
>
213 /** The iterator for our container. */
214 typedef typename
Tcont::iterator Iterator
;
215 /** The reverse iterator for our container. */
216 typedef typename
Tcont::reverse_iterator ReverseIterator
;
217 /** The const iterator for our container. */
218 typedef typename
Tcont::const_iterator ConstIterator
;
219 /** The const reverse iterator for our container. */
220 typedef typename
Tcont::const_reverse_iterator ConstReverseIterator
;
222 /** Kind of actions that could be done with packets on move. */
225 MTA_TRANSFER
= 0, ///< Transfer the cargo to the station.
226 MTA_DELIVER
, ///< Deliver the cargo to some town or industry.
227 MTA_KEEP
, ///< Keep the cargo in the vehicle.
228 MTA_LOAD
, ///< Load the cargo from the station.
230 NUM_MOVE_TO_ACTION
= MTA_END
234 uint count
; ///< Cache for the number of cargo entities.
235 uint cargo_days_in_transit
; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours.
237 Tcont packets
; ///< The cargo packets in this list.
239 void AddToCache(const CargoPacket
*cp
);
241 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
243 static bool TryMerge(CargoPacket
*cp
, CargoPacket
*icp
);
246 /** Create the cargo list. */
254 * Returns a pointer to the cargo packet list (so you can iterate over it etc).
255 * @return Pointer to the packet list.
257 inline const Tcont
*Packets() const
259 return &this->packets
;
263 * Returns average number of days in transit for a cargo entity.
264 * @return The before mentioned number.
266 inline uint
DaysInTransit() const
268 return this->count
== 0 ? 0 : this->cargo_days_in_transit
/ this->count
;
271 void InvalidateCache();
274 typedef std::list
<CargoPacket
*> CargoPacketList
;
277 * CargoList that is used for vehicles.
279 class VehicleCargoList
: public CargoList
<VehicleCargoList
, CargoPacketList
> {
281 /** The (direct) parent of this class. */
282 typedef CargoList
<VehicleCargoList
, CargoPacketList
> Parent
;
284 Money feeder_share
; ///< Cache for the feeder share.
285 uint action_counts
[NUM_MOVE_TO_ACTION
]; ///< Counts of cargo to be transfered, delivered, kept and loaded.
287 template<class Taction
>
288 void ShiftCargo(Taction action
);
290 template<class Taction
>
291 void PopCargo(Taction action
);
294 * Assert that the designation counts add up.
296 inline void AssertCountConsistency() const
298 assert(this->action_counts
[MTA_KEEP
] +
299 this->action_counts
[MTA_DELIVER
] +
300 this->action_counts
[MTA_TRANSFER
] +
301 this->action_counts
[MTA_LOAD
] == this->count
);
304 void AddToCache(const CargoPacket
*cp
);
305 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
307 void AddToMeta(const CargoPacket
*cp
, MoveToAction action
);
308 void RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
);
310 static MoveToAction
ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
311 StationID current_station
, bool accepted
, StationIDStack next_station
);
314 /** The station cargo list needs to control the unloading. */
315 friend class StationCargoList
;
316 /** The super class ought to know what it's doing. */
317 friend class CargoList
<VehicleCargoList
, CargoPacketList
>;
318 /** The vehicles have a cargo list (and we want that saved). */
319 friend const struct SaveLoad
*GetVehicleDescription(VehicleType vt
);
321 friend class CargoShift
;
322 friend class CargoTransfer
;
323 friend class CargoDelivery
;
324 template<class Tsource
>
325 friend class CargoRemoval
;
326 friend class CargoReturn
;
327 friend class VehicleCargoReroute
;
330 * Returns source of the first cargo packet in this list.
331 * @return The before mentioned source.
333 inline StationID
Source() const
335 return this->count
== 0 ? INVALID_STATION
: this->packets
.front()->source
;
339 * Returns total sum of the feeder share for all packets.
340 * @return The before mentioned number.
342 inline Money
FeederShare() const
344 return this->feeder_share
;
348 * Returns the amount of cargo designated for a given purpose.
349 * @param action Action the cargo is designated for.
350 * @return Amount of cargo designated for the given action.
352 inline uint
ActionCount(MoveToAction action
) const
354 return this->action_counts
[action
];
358 * Returns sum of cargo on board the vehicle (ie not only
360 * @return Cargo on board the vehicle.
362 inline uint
StoredCount() const
364 return this->count
- this->action_counts
[MTA_LOAD
];
368 * Returns sum of cargo, including reserved cargo.
369 * @return Sum of cargo.
371 inline uint
TotalCount() const
377 * Returns sum of reserved cargo.
378 * @return Sum of reserved cargo.
380 inline uint
ReservedCount() const
382 return this->action_counts
[MTA_LOAD
];
386 * Returns sum of cargo to be moved out of the vehicle at the current station.
387 * @return Cargo to be moved.
389 inline uint
UnloadCount() const
391 return this->action_counts
[MTA_TRANSFER
] + this->action_counts
[MTA_DELIVER
];
395 * Returns the sum of cargo to be kept in the vehicle at the current station.
396 * @return Cargo to be kept or loaded.
398 inline uint
RemainingCount() const
400 return this->action_counts
[MTA_KEEP
] + this->action_counts
[MTA_LOAD
];
403 void Append(CargoPacket
*cp
, MoveToAction action
= MTA_KEEP
);
407 void InvalidateCache();
409 void SetTransferLoadPlace(TileIndex xy
);
411 bool Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8 order_flags
, const GoodsEntry
*ge
, CargoPayment
*payment
);
414 * Marks all cargo in the vehicle as to be kept. This is mostly useful for
415 * loading old savegames. When loading is aborted the reserved cargo has
416 * to be returned first.
418 inline void KeepAll()
420 this->action_counts
[MTA_DELIVER
] = this->action_counts
[MTA_TRANSFER
] = this->action_counts
[MTA_LOAD
] = 0;
421 this->action_counts
[MTA_KEEP
] = this->count
;
424 /* Methods for moving cargo around. First parameter is always maximum
425 * amount of cargo to be moved. Second parameter is destination (if
426 * applicable), return value is amount of cargo actually moved. */
428 template<MoveToAction Tfrom
, MoveToAction Tto
>
429 uint
Reassign(uint max_move
, TileOrStationID update
= INVALID_TILE
);
430 uint
Return(uint max_move
, StationCargoList
*dest
, StationID next_station
);
431 uint
Unload(uint max_move
, StationCargoList
*dest
, CargoPayment
*payment
);
432 uint
Shift(uint max_move
, VehicleCargoList
*dest
);
433 uint
Truncate(uint max_move
= UINT_MAX
);
434 uint
Reroute(uint max_move
, VehicleCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
);
437 * Are two the two CargoPackets mergeable in the context of
438 * a list of CargoPackets for a Vehicle?
439 * @param cp1 First CargoPacket.
440 * @param cp2 Second CargoPacket.
441 * @return True if they are mergeable.
443 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
445 return cp1
->source_xy
== cp2
->source_xy
&&
446 cp1
->days_in_transit
== cp2
->days_in_transit
&&
447 cp1
->source_type
== cp2
->source_type
&&
448 cp1
->source_id
== cp2
->source_id
&&
449 cp1
->loaded_at_xy
== cp2
->loaded_at_xy
;
453 typedef MultiMap
<StationID
, CargoPacket
*> StationCargoPacketMap
;
454 typedef std::map
<StationID
, uint
> StationCargoAmountMap
;
457 * CargoList that is used for stations.
459 class StationCargoList
: public CargoList
<StationCargoList
, StationCargoPacketMap
> {
461 /** The (direct) parent of this class. */
462 typedef CargoList
<StationCargoList
, StationCargoPacketMap
> Parent
;
464 uint reserved_count
; ///< Amount of cargo being reserved for loading.
467 /** The super class ought to know what it's doing. */
468 friend class CargoList
<StationCargoList
, StationCargoPacketMap
>;
469 /** The stations, via GoodsEntry, have a CargoList. */
470 friend const struct SaveLoad
*GetGoodsDesc();
472 friend class CargoLoad
;
473 friend class CargoTransfer
;
474 template<class Tsource
>
475 friend class CargoRemoval
;
476 friend class CargoReservation
;
477 friend class CargoReturn
;
478 friend class StationCargoReroute
;
480 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
482 template<class Taction
>
483 bool ShiftCargo(Taction
&action
, StationID next
);
485 template<class Taction
>
486 uint
ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
);
488 void Append(CargoPacket
*cp
, StationID next
);
491 * Check for cargo headed for a specific station.
492 * @param next Station the cargo is headed for.
493 * @return If there is any cargo for that station.
495 inline bool HasCargoFor(StationIDStack next
) const
497 while (!next
.IsEmpty()) {
498 if (this->packets
.find(next
.Pop()) != this->packets
.end()) return true;
500 /* Packets for INVALID_STTION can go anywhere. */
501 return this->packets
.find(INVALID_STATION
) != this->packets
.end();
505 * Returns source of the first cargo packet in this list.
506 * @return The before mentioned source.
508 inline StationID
Source() const
510 return this->count
== 0 ? INVALID_STATION
: this->packets
.begin()->second
.front()->source
;
514 * Returns sum of cargo still available for loading at the sation.
515 * (i.e. not counting cargo which is already reserved for loading)
516 * @return Cargo on board the vehicle.
518 inline uint
AvailableCount() const
524 * Returns sum of cargo reserved for loading onto vehicles.
525 * @return Cargo reserved for loading.
527 inline uint
ReservedCount() const
529 return this->reserved_count
;
533 * Returns total count of cargo at the station, including
534 * cargo which is already reserved for loading.
535 * @return Total cargo count.
537 inline uint
TotalCount() const
539 return this->count
+ this->reserved_count
;
542 /* Methods for moving cargo around. First parameter is always maximum
543 * amount of cargo to be moved. Second parameter is destination (if
544 * applicable), return value is amount of cargo actually moved. */
546 uint
Reserve(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next
);
547 uint
Load(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next
);
548 uint
Truncate(uint max_move
= UINT_MAX
, StationCargoAmountMap
*cargo_per_source
= NULL
);
549 uint
Reroute(uint max_move
, StationCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
);
552 * Are two the two CargoPackets mergeable in the context of
553 * a list of CargoPackets for a Vehicle?
554 * @param cp1 First CargoPacket.
555 * @param cp2 Second CargoPacket.
556 * @return True if they are mergeable.
558 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
560 return cp1
->source_xy
== cp2
->source_xy
&&
561 cp1
->days_in_transit
== cp2
->days_in_transit
&&
562 cp1
->source_type
== cp2
->source_type
&&
563 cp1
->source_id
== cp2
->source_id
;
567 #endif /* CARGOPACKET_H */