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"
22 /** Unique identifier for a single cargo packet. */
23 typedef uint32 CargoPacketID
;
26 /** Type of the pool for cargo packets for a little over 16 million packets. */
27 typedef Pool
<CargoPacket
, CargoPacketID
, 1024, 0xFFF000, PT_NORMAL
, true, false> CargoPacketPool
;
28 /** The actual pool with cargo packets. */
29 extern CargoPacketPool _cargopacket_pool
;
31 struct GoodsEntry
; // forward-declare for Stage() and RerouteStalePackets()
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();
37 typedef uint32 TileOrStationID
;
40 * Container for cargo from the same location and time.
42 struct CargoPacket
: CargoPacketPool::PoolItem
<&_cargopacket_pool
> {
44 Money feeder_share
; ///< Value of feeder pickup to be paid for on delivery of cargo.
45 uint16 count
; ///< The amount of cargo in this packet.
46 byte days_in_transit
; ///< Amount of days this packet has been in transit.
47 SourceType source_type
; ///< Type of \c source_id.
48 SourceID source_id
; ///< Index of source, INVALID_SOURCE if unknown/invalid.
49 StationID source
; ///< The station where the cargo came from first.
50 TileIndex source_xy
; ///< The origin of the cargo (first station in feeder chain).
52 TileOrStationID loaded_at_xy
; ///< Location where this cargo has been loaded into the vehicle.
53 TileOrStationID next_station
; ///< Station where the cargo wants to go next.
56 /** The CargoList caches, thus needs to know about it. */
57 template <class Tinst
, class Tcont
> friend class CargoList
;
58 friend class VehicleCargoList
;
59 friend class StationCargoList
;
60 /** We want this to be saved, right? */
61 friend const struct SaveLoad
*GetCargoPacketDesc();
63 /** Maximum number of items in a single cargo packet. */
64 static const uint16 MAX_COUNT
= UINT16_MAX
;
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
);
70 /** Destroy the packet. */
73 CargoPacket
*Split(uint new_size
);
74 void Merge(CargoPacket
*cp
);
75 void Reduce(uint count
);
78 * Sets the tile where the packet was loaded last.
79 * @param load_place Tile where the packet was loaded last.
81 void SetLoadPlace(TileIndex load_place
) { this->loaded_at_xy
= load_place
; }
84 * Sets the station where the packet is supposed to go next.
85 * @param next_station Next station the packet should go to.
87 void SetNextStation(StationID next_station
) { this->next_station
= next_station
; }
90 * Adds some feeder share to the packet.
91 * @param new_share Feeder share to be added.
93 void AddFeederShare(Money new_share
) { this->feeder_share
+= new_share
; }
96 * Gets the number of 'items' in this packet.
99 inline uint16
Count() const
105 * Gets the amount of money already paid to earlier vehicles in
107 * @return Feeder share.
109 inline Money
FeederShare() const
111 return this->feeder_share
;
115 * Gets part of the amount of money already paid to earlier vehicles in
117 * @param part Amount of cargo to get the share for.
118 * @return Feeder share for the given amount of cargo.
120 inline Money
FeederShare(uint part
) const
122 return this->feeder_share
* part
/ static_cast<uint
>(this->count
);
126 * Gets the number of days this cargo has been in transit.
127 * This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and
128 * it is capped at 255.
129 * @return Length this cargo has been in transit.
131 inline byte
DaysInTransit() const
133 return this->days_in_transit
;
137 * Gets the type of the cargo's source. industry, town or head quarter.
138 * @return Source type.
140 inline SourceType
SourceSubsidyType() const
142 return this->source_type
;
146 * Gets the ID of the cargo's source. An IndustryID, TownID or CompanyID.
149 inline SourceID
SourceSubsidyID() const
151 return this->source_id
;
155 * Gets the ID of the station where the cargo was loaded for the first time.
158 inline StationID
SourceStation() const
164 * Gets the coordinates of the cargo's source station.
165 * @return Source station's coordinates.
167 inline TileIndex
SourceStationXY() const
169 return this->source_xy
;
173 * Gets the coordinates of the cargo's last loading station.
174 * @return Last loading station's coordinates.
176 inline TileIndex
LoadedAtXY() const
178 return this->loaded_at_xy
;
182 * Gets the ID of station the cargo wants to go next.
183 * @return Next station for this packets.
185 inline StationID
NextStation() const
187 return this->next_station
;
190 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
191 static void InvalidateAllFrom(StationID sid
);
192 static void AfterLoad();
196 * Simple collection class for a list of cargo packets.
197 * @tparam Tinst Actual instantiation of this cargo list.
199 template <class Tinst
, class Tcont
>
202 /** The iterator for our container. */
203 typedef typename
Tcont::iterator Iterator
;
204 /** The reverse iterator for our container. */
205 typedef typename
Tcont::reverse_iterator ReverseIterator
;
206 /** The const iterator for our container. */
207 typedef typename
Tcont::const_iterator ConstIterator
;
208 /** The const reverse iterator for our container. */
209 typedef typename
Tcont::const_reverse_iterator ConstReverseIterator
;
211 /** Kind of actions that could be done with packets on move. */
214 MTA_TRANSFER
= 0, ///< Transfer the cargo to the station.
215 MTA_DELIVER
, ///< Deliver the cargo to some town or industry.
216 MTA_KEEP
, ///< Keep the cargo in the vehicle.
217 MTA_LOAD
, ///< Load the cargo from the station.
219 NUM_MOVE_TO_ACTION
= MTA_END
223 uint count
; ///< Cache for the number of cargo entities.
224 uint cargo_days_in_transit
; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours.
226 Tcont packets
; ///< The cargo packets in this list.
228 void AddToCache(const CargoPacket
*cp
);
230 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
232 static bool TryMerge(CargoPacket
*cp
, CargoPacket
*icp
);
235 /** Create the cargo list. */
243 * Returns a pointer to the cargo packet list (so you can iterate over it etc).
244 * @return Pointer to the packet list.
246 inline const Tcont
*Packets() const
248 return &this->packets
;
252 * Returns average number of days in transit for a cargo entity.
253 * @return The before mentioned number.
255 inline uint
DaysInTransit() const
257 return this->count
== 0 ? 0 : this->cargo_days_in_transit
/ this->count
;
260 void InvalidateCache();
263 typedef std::list
<CargoPacket
*> CargoPacketList
;
266 * CargoList that is used for vehicles.
268 class VehicleCargoList
: public CargoList
<VehicleCargoList
, CargoPacketList
> {
270 /** The (direct) parent of this class. */
271 typedef CargoList
<VehicleCargoList
, CargoPacketList
> Parent
;
273 Money feeder_share
; ///< Cache for the feeder share.
274 uint action_counts
[NUM_MOVE_TO_ACTION
]; ///< Counts of cargo to be transferred, delivered, kept and loaded.
276 template<class Taction
>
277 void ShiftCargo(Taction action
);
279 template<class Taction
>
280 void PopCargo(Taction action
);
283 * Assert that the designation counts add up.
285 inline void AssertCountConsistency() const
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
);
293 void AddToCache(const CargoPacket
*cp
);
294 void RemoveFromCache(const CargoPacket
*cp
, uint count
);
296 void AddToMeta(const CargoPacket
*cp
, MoveToAction action
);
297 void RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
);
299 static MoveToAction
ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
300 StationID current_station
, bool accepted
, StationIDStack next_station
);
303 /** The station cargo list needs to control the unloading. */
304 friend class StationCargoList
;
305 /** The super class ought to know what it's doing. */
306 friend class CargoList
<VehicleCargoList
, CargoPacketList
>;
307 /** The vehicles have a cargo list (and we want that saved). */
308 friend const struct SaveLoad
*GetVehicleDescription(VehicleType vt
);
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
;
319 * Returns source of the first cargo packet in this list.
320 * @return The before mentioned source.
322 inline StationID
Source() const
324 return this->count
== 0 ? INVALID_STATION
: this->packets
.front()->source
;
328 * Returns total sum of the feeder share for all packets.
329 * @return The before mentioned number.
331 inline Money
FeederShare() const
333 return this->feeder_share
;
337 * Returns the amount of cargo designated for a given purpose.
338 * @param action Action the cargo is designated for.
339 * @return Amount of cargo designated for the given action.
341 inline uint
ActionCount(MoveToAction action
) const
343 return this->action_counts
[action
];
347 * Returns sum of cargo on board the vehicle (ie not only
349 * @return Cargo on board the vehicle.
351 inline uint
StoredCount() const
353 return this->count
- this->action_counts
[MTA_LOAD
];
357 * Returns sum of cargo, including reserved cargo.
358 * @return Sum of cargo.
360 inline uint
TotalCount() const
366 * Returns sum of reserved cargo.
367 * @return Sum of reserved cargo.
369 inline uint
ReservedCount() const
371 return this->action_counts
[MTA_LOAD
];
375 * Returns sum of cargo to be moved out of the vehicle at the current station.
376 * @return Cargo to be moved.
378 inline uint
UnloadCount() const
380 return this->action_counts
[MTA_TRANSFER
] + this->action_counts
[MTA_DELIVER
];
384 * Returns the sum of cargo to be kept in the vehicle at the current station.
385 * @return Cargo to be kept or loaded.
387 inline uint
RemainingCount() const
389 return this->action_counts
[MTA_KEEP
] + this->action_counts
[MTA_LOAD
];
392 void Append(CargoPacket
*cp
, MoveToAction action
= MTA_KEEP
);
396 void InvalidateCache();
398 void SetTransferLoadPlace(TileIndex xy
);
400 bool Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8 order_flags
, const GoodsEntry
*ge
, CargoPayment
*payment
);
403 * Marks all cargo in the vehicle as to be kept. This is mostly useful for
404 * loading old savegames. When loading is aborted the reserved cargo has
405 * to be returned first.
407 inline void KeepAll()
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
;
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. */
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
);
426 * Are the two CargoPackets mergeable in the context of
427 * a list of CargoPackets for a Vehicle?
428 * @param cp1 First CargoPacket.
429 * @param cp2 Second CargoPacket.
430 * @return True if they are mergeable.
432 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
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
;
442 typedef MultiMap
<StationID
, CargoPacket
*> StationCargoPacketMap
;
443 typedef std::map
<StationID
, uint
> StationCargoAmountMap
;
446 * CargoList that is used for stations.
448 class StationCargoList
: public CargoList
<StationCargoList
, StationCargoPacketMap
> {
450 /** The (direct) parent of this class. */
451 typedef CargoList
<StationCargoList
, StationCargoPacketMap
> Parent
;
453 uint reserved_count
; ///< Amount of cargo being reserved for loading.
456 /** The super class ought to know what it's doing. */
457 friend class CargoList
<StationCargoList
, StationCargoPacketMap
>;
458 /** The stations, via GoodsEntry, have a CargoList. */
459 friend const struct SaveLoad
*GetGoodsDesc();
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
;
469 static void InvalidateAllFrom(SourceType src_type
, SourceID src
);
471 template<class Taction
>
472 bool ShiftCargo(Taction
&action
, StationID next
);
474 template<class Taction
>
475 uint
ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
);
477 void Append(CargoPacket
*cp
, StationID next
);
480 * Check for cargo headed for a specific station.
481 * @param next Station the cargo is headed for.
482 * @return If there is any cargo for that station.
484 inline bool HasCargoFor(StationIDStack next
) const
486 while (!next
.IsEmpty()) {
487 if (this->packets
.find(next
.Pop()) != this->packets
.end()) return true;
489 /* Packets for INVALID_STATION can go anywhere. */
490 return this->packets
.find(INVALID_STATION
) != this->packets
.end();
494 * Returns source of the first cargo packet in this list.
495 * @return The before mentioned source.
497 inline StationID
Source() const
499 return this->count
== 0 ? INVALID_STATION
: this->packets
.begin()->second
.front()->source
;
503 * Returns sum of cargo still available for loading at the sation.
504 * (i.e. not counting cargo which is already reserved for loading)
505 * @return Cargo on board the vehicle.
507 inline uint
AvailableCount() const
513 * Returns sum of cargo reserved for loading onto vehicles.
514 * @return Cargo reserved for loading.
516 inline uint
ReservedCount() const
518 return this->reserved_count
;
522 * Returns total count of cargo at the station, including
523 * cargo which is already reserved for loading.
524 * @return Total cargo count.
526 inline uint
TotalCount() const
528 return this->count
+ this->reserved_count
;
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. */
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
);
541 * Are the two CargoPackets mergeable in the context of
542 * a list of CargoPackets for a Station?
543 * @param cp1 First CargoPacket.
544 * @param cp2 Second CargoPacket.
545 * @return True if they are mergeable.
547 static bool AreMergable(const CargoPacket
*cp1
, const CargoPacket
*cp2
)
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
;
556 #endif /* CARGOPACKET_H */