34template <
class Tinst,
class Tcont>
class CargoList;
41struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
54 bool in_vehicle =
false;
61 template <
class Tinst,
class Tcont>
friend class CargoList;
62 friend class VehicleCargoList;
63 friend class StationCargoList;
108 this->source_xy = tile;
112 assert(!this->in_vehicle);
113 this->in_vehicle =
true;
119 this->travelled.
x +=
TileX(tile);
120 this->travelled.
y +=
TileY(tile);
131 assert(this->in_vehicle);
132 this->in_vehicle =
false;
135 this->travelled.
x -=
TileX(tile);
136 this->travelled.
y -=
TileY(tile);
145 this->feeder_share += new_share;
164 return this->feeder_share;
175 return this->feeder_share * part /
static_cast<uint
>(this->count);
187 return this->periods_in_transit;
205 return this->first_station;
218 assert(this->in_vehicle);
225 local_travelled.x -=
TileX(current_tile);
226 local_travelled.y -=
TileY(current_tile);
244 uint distance_travelled =
abs(local_travelled.x) +
abs(local_travelled.y);
246 return std::min(distance_travelled, distance_source_dest);
255 return this->next_hop;
267template <
class Tinst,
class Tcont>
287 NUM_MOVE_TO_ACTION = MTA_END
316 return &this->packets;
325 return this->count == 0 ? 0 : this->cargo_periods_in_transit / this->count;
331typedef std::list<CargoPacket *> CargoPacketList;
344 template <
class Taction>
347 template <
class Taction>
355 assert(this->action_counts[
MTA_KEEP] +
368 StationID current_station,
bool accepted, std::span<const StationID> next_station);
376 friend class SlVehicleCommon;
378 friend class CargoShift;
379 friend class CargoTransfer;
380 friend class CargoDelivery;
381 template <
class Tsource>
382 friend class CargoRemoval;
383 friend class CargoReturn;
384 friend class VehicleCargoReroute;
392 return this->
count == 0 ? StationID::Invalid() : this->
packets.front()->first_station;
401 return this->feeder_share;
411 return this->action_counts[action];
439 return this->action_counts[
MTA_LOAD];
483 template <MoveToAction Tfrom, MoveToAction Tto>
488 uint
Truncate(uint max_move = UINT_MAX);
508typedef std::map<StationID, uint> StationCargoAmountMap;
524 friend class SlStationGoods;
526 friend class CargoLoad;
527 friend class CargoTransfer;
528 template <
class Tsource>
529 friend class CargoRemoval;
530 friend class CargoReservation;
531 friend class CargoReturn;
532 friend class StationCargoReroute;
534 static void InvalidateAllFrom(
Source src);
536 template <
class Taction>
537 bool ShiftCargo(Taction &action, StationID next);
539 template <
class Taction>
540 uint
ShiftCargo(Taction action, std::span<const StationID> next,
bool include_invalid);
551 for (
const StationID &station : next) {
552 if (this->
packets.find(station) != this->packets.end())
return true;
555 return this->
packets.find(StationID::Invalid()) != this->
packets.end();
564 return this->
count == 0 ? StationID::Invalid() : this->
packets.begin()->second.front()->first_station;
583 return this->reserved_count;
593 return this->
count + this->reserved_count;
602 uint
Truncate(uint max_move = UINT_MAX, StationCargoAmountMap *cargo_per_source =
nullptr);
Types related to cargoes...
CargoType
Cargo slots to indicate a cargo type within a game.
PoolID< uint32_t, struct CargoPacketIDTag, 0xFFF000, 0xFFFFFF > CargoPacketID
Unique identifier for a single cargo packet.
SaveLoadTable GetCargoPacketDesc()
Wrapper function to get the CargoPacket's internal structure while some of the variables itself are p...
Pool< CargoPacket, CargoPacketID, 1024, PoolType::Normal, true > CargoPacketPool
Type of the pool for cargo packets for a little over 16 million packets.
Simple collection class for a list of cargo packets.
Tcont::reverse_iterator ReverseIterator
The reverse iterator for our container.
void OnCleanPool()
Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket'...
~CargoList()
Destroy the cargolist ("frees" all cargo packets).
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Tcont::const_iterator ConstIterator
The const iterator for our container.
CargoList()
Create the cargo list.
uint64_t cargo_periods_in_transit
static bool TryMerge(CargoPacket *cp, CargoPacket *icp)
Tries to merge the second packet into the first and return if that was successful.
Tcont::iterator Iterator
The iterator for our container.
MoveToAction
Kind of actions that could be done with packets on move.
Tcont::const_reverse_iterator ConstReverseIterator
The const reverse iterator for our container.
uint PeriodsInTransit() const
Returns average number of cargo aging periods in transit for a cargo entity.
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
void InvalidateCache()
Invalidates the cached data and rebuilds it.
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
Hand-rolled multimap as map of lists.
CargoList that is used for stations.
bool HasCargoFor(std::span< const StationID > next) const
Check for cargo headed for a specific station.
CargoList< StationCargoList, StationCargoPacketMap > Parent
The (direct) parent of this class.
uint TotalCount() const
Returns total count of cargo at the station, including cargo which is already reserved for loading.
uint Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
Routes packets with station "avoid" as next hop to a different place.
uint ReservedCount() const
Returns sum of cargo reserved for loading onto vehicles.
uint Load(uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
Loads cargo onto a vehicle.
uint AvailableCount() const
Returns sum of cargo still available for loading at the station.
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
uint Truncate(uint max_move=UINT_MAX, StationCargoAmountMap *cargo_per_source=nullptr)
Truncates where each destination loses roughly the same percentage of its cargo.
bool ShiftCargo(Taction &action, StationID next)
Shifts cargo from the front of the packet list for a specific station and applies some action to it.
uint reserved_count
Amount of cargo being reserved for loading.
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
Are the two CargoPackets mergeable in the context of a list of CargoPackets for a Station?
uint Reserve(uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
Reserves cargo for loading onto the vehicle.
StationID GetFirstStation() const
Returns first station of the first cargo packet in this list.
CargoList that is used for vehicles.
uint Shift(uint max_move, VehicleCargoList *dest)
Shifts cargo between two vehicles.
void AddToMeta(const CargoPacket *cp, MoveToAction action)
Adds a packet to the metadata.
uint UnloadCount() const
Returns sum of cargo to be moved out of the vehicle at the current station.
void PopCargo(Taction action)
Pops cargo from the back of the packet list and applies some action to it.
friend class StationCargoList
The station cargo list needs to control the unloading.
Money GetFeederShare() const
Returns total sum of the feeder share for all packets.
uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
Routes packets with station "avoid" as next hop to a different place.
uint ActionCount(MoveToAction action) const
Returns the amount of cargo designated for a given purpose.
static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next, StationID current_station, bool accepted, std::span< const StationID > next_station)
Choose action to be performed with the given cargo packet.
uint Truncate(uint max_move=UINT_MAX)
Truncates the cargo in this list to the given amount.
void AssertCountConsistency() const
Assert that the designation counts add up.
void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
Removes a packet or part of it from the metadata.
uint Return(uint max_move, StationCargoList *dest, StationID next_station, TileIndex current_tile)
Returns reserved cargo to the station and removes it from the cache.
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
Are the two CargoPackets mergeable in the context of a list of CargoPackets for a Vehicle?
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
uint action_counts[NUM_MOVE_TO_ACTION]
Counts of cargo to be transferred, delivered, kept and loaded.
uint ReservedCount() const
Returns sum of reserved cargo.
uint RemainingCount() const
Returns the sum of cargo to be kept in the vehicle at the current station.
CargoList< VehicleCargoList, CargoPacketList > Parent
The (direct) parent of this class.
Money feeder_share
Cache for the feeder share.
void Append(CargoPacket *cp, MoveToAction action=MTA_KEEP)
Appends the given cargo packet.
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
uint Reassign(uint max_move)
Moves some cargo from one designation to another.
void InvalidateCache()
Invalidates the cached data and rebuild it.
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
void KeepAll()
Marks all cargo in the vehicle as to be kept.
StationID GetFirstStation() const
Returns the first station of the first cargo packet in this list.
void AgeCargo()
Ages the all cargo in this list.
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
uint Unload(uint max_move, StationCargoList *dest, CargoType cargo, CargoPayment *payment, TileIndex current_tile)
Unloads cargo at the given station.
bool Stage(bool accepted, StationID current_station, std::span< const StationID > next_station, OrderUnloadType unload_type, const GoodsEntry *ge, CargoType cargo, CargoPayment *payment, TileIndex current_tile)
Stages cargo for unloading.
void ShiftCargo(Taction action)
Shifts cargo from the front of the packet list and applies some action to it.
Types related to the economy.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
static uint TileX(TileIndex tile)
Get the X component of a tile.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Multimap with deterministic ordering of items with equal keys.
OrderUnloadType
Unloading order types.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
Functions/types related to saving and loading games.
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Type for the source of cargo.
@ Industry
Source/destination is an industry.
Types related to stations.
Container for cargo from the same location and time.
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
uint16_t Count() const
Gets the number of 'items' in this packet.
Money GetFeederShare(uint part) const
Gets part of the amount of money already paid to earlier vehicles in the feeder chain.
~CargoPacket()
Destroy the packet.
friend class CargoList
The CargoList caches, thus needs to know about it.
void Merge(CargoPacket *cp)
Merge another packet into this one.
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
static const uint16_t MAX_COUNT
Maximum number of items in a single cargo packet.
TileIndex source_xy
The origin of the cargo.
void SetNextHop(StationID next_hop)
Sets the station where the packet is supposed to go next.
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
StationID next_hop
Station where the cargo wants to go next.
Source source
Source of the cargo.
Coord2D< int16_t > travelled
If cargo is in station: the vector from the unload tile to the source tile. If in vehicle: an interme...
void UpdateLoadingTile(TileIndex tile)
Update for the cargo being loaded on this tile.
friend SaveLoadTable GetCargoPacketDesc()
We want this to be saved, right?
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
CargoPacket(CargoPacketID index)
Create a new packet for savegame loading.
static void AfterLoad()
Savegame conversion for cargopackets.
StationID GetNextHop() const
Gets the ID of station the cargo wants to go next.
uint16_t count
The amount of cargo in this packet.
Money GetFeederShare() const
Gets the amount of money already paid to earlier vehicles in the feeder chain.
StationID GetFirstStation() const
Gets the ID of the station where the cargo was loaded for the first time.
uint GetDistance(TileIndex current_tile) const
Get the current distance the cargo has traveled.
StationID first_station
The station where the cargo came from first.
uint16_t GetPeriodsInTransit() const
Gets the number of cargo aging periods this cargo has been in transit.
Source GetSource() const
Gets the source of the packet for subsidy purposes.
static void InvalidateAllFrom(Source src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
void UpdateUnloadingTile(TileIndex tile)
Update for the cargo being unloaded on this tile.
uint16_t periods_in_transit
Amount of cargo aging periods this packet has been in transit.
Helper class to perform the cargo payment.
A coordinate with two dimensions.
Stores station stats for a single cargo.
Templated helper to make a PoolID a single POD value.
const CargoPacketID index
Base class for all pools.
A location from where cargo can come from (or go to).
static constexpr SourceID Invalid
Invalid/unknown index of source.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Types related to vehicles.