88 in_vehicle(original.in_vehicle),
108 this->
count -= new_size;
131 this->count -=
count;
141 if (cp->source == src) cp->source.MakeInvalid();
152 if (cp->first_station == sid) cp->first_station = StationID::Invalid();
165template <
class Tinst,
class Tcont>
168 for (
Iterator it(this->
packets.begin()); it != this->packets.end(); ++it) {
177template <
class Tinst,
class Tcont>
189template <
class Tinst,
class Tcont>
192 assert(count <= cp->
count);
193 this->count -=
count;
202template <
class Tinst,
class Tcont>
210template <
class Tinst,
class Tcont>
228template <
class Tinst,
class Tcont>
263 assert(cp !=
nullptr);
264 assert(action == MoveToAction::Load ||
265 (action == MoveToAction::Keep && this->
action_counts[MoveToAction::Load] == 0));
273 uint sum = cp->
count;
295template <
class Taction>
299 while (it != this->
packets.end() && action.MaxMove() > 0) {
317template <
class Taction>
320 if (this->
packets.empty())
return;
323 while (action.MaxMove() > 0) {
408 for (
const auto &cp : this->
packets) {
410 if (cp->periods_in_transit == UINT16_MAX)
continue;
412 cp->periods_in_transit++;
427 StationID current_station,
bool accepted, std::span<const StationID> next_station)
429 if (cargo_next == StationID::Invalid()) {
430 return (accepted && cp->
first_station != current_station) ? MoveToAction::Deliver : MoveToAction::Keep;
431 }
else if (cargo_next == current_station) {
432 return MoveToAction::Deliver;
433 }
else if (std::ranges::find(next_station, cargo_next) != std::end(next_station)) {
434 return MoveToAction::Keep;
436 return MoveToAction::Transfer;
471 while (sum < this->
count) {
475 StationID cargo_next = StationID::Invalid();
478 action = MoveToAction::Keep;
479 }
else if (force_unload && accepted && cp->
first_station != current_station) {
480 action = MoveToAction::Deliver;
481 }
else if (force_transfer) {
482 action = MoveToAction::Transfer;
485 FlowStatMap::const_iterator flow_it(flows.find(cp->
first_station));
486 if (flow_it == flows.end()) {
487 cargo_next = StationID::Invalid();
489 FlowStat new_shares = flow_it->second;
491 for (
auto station_it = next_station.rbegin(); station_it != next_station.rend(); ++station_it) {
492 if (new_shares.
GetShares()->empty())
break;
496 cargo_next = StationID::Invalid();
498 cargo_next = new_shares.
GetVia();
504 if (cp->
first_station == StationID::Invalid() && !flows.empty()) {
507 bool restricted =
false;
508 FlowStatMap::const_iterator flow_it(flows.find(cp->
first_station));
509 if (flow_it == flows.end()) {
510 cargo_next = StationID::Invalid();
512 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
515 if (restricted && action == MoveToAction::Transfer) {
518 cargo_next = flow_it->second.GetVia();
524 case MoveToAction::Keep:
526 if (deliver == this->
packets.end()) --deliver;
528 case MoveToAction::Deliver:
529 this->
packets.insert(deliver, cp);
531 case MoveToAction::Transfer:
568template <VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
571 static_assert(Tfrom != MoveToAction::Transfer && Tto != MoveToAction::Transfer);
588 max_move = std::min(this->action_counts[MoveToAction::Deliver], max_move);
591 for (Iterator it(this->packets.begin()); sum < this->action_counts[MoveToAction::Transfer] + max_move;) {
594 if (sum <= this->action_counts[MoveToAction::Transfer])
continue;
595 if (sum > this->action_counts[MoveToAction::Transfer] + max_move) {
596 CargoPacket *cp_split = cp->
Split(sum - this->action_counts[MoveToAction::Transfer] + max_move);
597 sum -= cp_split->
Count();
598 this->packets.insert(it, cp_split);
600 cp->
next_hop = StationID::Invalid();
603 this->action_counts[MoveToAction::Deliver] -= max_move;
604 this->action_counts[MoveToAction::Transfer] += max_move;
618 max_move = std::min(this->
action_counts[MoveToAction::Load], max_move);
619 this->
PopCargo(CargoReturn(
this, dest, max_move, next, current_tile));
631 max_move = std::min(this->
count, max_move);
632 this->
PopCargo(CargoShift(
this, dest, max_move));
650 uint move = std::min(this->
action_counts[MoveToAction::Transfer], max_move);
651 this->
ShiftCargo(CargoTransfer(
this, dest, move, current_tile));
655 uint move = std::min(this->
action_counts[MoveToAction::Deliver], max_move - moved);
656 this->
ShiftCargo(CargoDelivery(
this, move, cargo, payment, current_tile));
670 max_move = std::min(this->
count, max_move);
671 this->
PopCargo(CargoRemoval<VehicleCargoList>(
this, max_move));
686 max_move = std::min(this->
action_counts[MoveToAction::Transfer], max_move);
687 this->
ShiftCargo(VehicleCargoReroute(
this, dest, max_move, avoid, avoid2, ge));
707 assert(cp !=
nullptr);
710 StationCargoPacketMap::List &list = this->
packets[next];
711 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
712 it != list.rend(); it++) {
732template <
class Taction>
735 std::pair<Iterator, Iterator> range(this->
packets.equal_range(next));
736 for (
Iterator it(range.first); it != range.second && it.GetKey() == next;) {
737 if (action.MaxMove() == 0)
return false;
762template <
class Taction>
765 uint max_move = action.MaxMove();
766 for (
auto it = next.rbegin(); it != next.rend(); ++it) {
768 if (action.MaxMove() == 0)
break;
770 if (include_invalid && action.MaxMove() > 0) {
771 this->
ShiftCargo(action, StationID::Invalid());
773 return max_move - action.MaxMove();
786 max_move = std::min(max_move, this->
count);
787 uint prev_count = this->
count;
790 bool do_count = cargo_per_source !=
nullptr;
791 while (max_move > moved) {
794 if (prev_count > max_move &&
RandomRange(prev_count) < prev_count - max_move) {
795 if (do_count && loop == 0) {
801 uint diff = max_move - moved;
802 if (cp->
count > diff) {
809 if (do_count) (*cargo_per_source)[cp->
first_station] -= diff;
817 if (do_count && loop > 0) {
840 return this->
ShiftCargo(CargoReservation(
this, dest, max_move, current_tile), next_station,
true);
857 uint move = std::min(dest->
ActionCount(VehicleCargoList::MoveToAction::Load), max_move);
860 dest->
Reassign<VehicleCargoList::MoveToAction::Load, VehicleCargoList::MoveToAction::Keep>(move);
863 return this->
ShiftCargo(CargoLoad{
this, dest, max_move, current_tile}, next_station,
true);
878 return this->
ShiftCargo(StationCargoReroute(
this, dest, max_move, avoid, avoid2, ge), {&avoid, 1},
false);
CargoType
Cargo slots to indicate a cargo type within a game.
Actions to be applied to cargo packets.
CargoPacketPool _cargopacket_pool
The actual pool with cargo packets.
PoolID< uint32_t, struct CargoPacketIDTag, 0xFFF000, 0xFFFFFF > CargoPacketID
Unique identifier for a single cargo packet.
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.
CargoPacketList::reverse_iterator ReverseIterator
void OnCleanPool()
Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket'...
Tcont packets
The cargo packets in this list.
~CargoList()
Destroy the cargolist ("frees" all cargo packets).
Tcont::const_iterator ConstIterator
The const iterator for our container.
uint count
Cache for the number of cargo entities.
uint64_t cargo_periods_in_transit
Cache for the sum of number of cargo aging periods in transit of each entity; comparable to man-hours...
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.
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.
Flow descriptions by origin stations.
Flow statistics telling how much flow should be sent along a link.
StationID GetVia() const
Get a station a package can be routed to.
const SharesMap * GetShares() const
Get the actual shares as a const pointer so that they can be iterated over.
void ChangeShare(StationID st, int flow)
Change share for specified station.
CargoList that is used for stations.
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 Load(uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
Loads cargo onto a vehicle.
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.
uint Reserve(uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
Reserves cargo for loading onto the vehicle.
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.
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.
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.
EnumIndexArray< uint, MoveToAction, MoveToAction::End > action_counts
Counts of cargo to be transferred, delivered, kept and loaded.
Money feeder_share
Cache for the feeder share.
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.
uint TryRemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
Try to remove a packet or part of it from the metadata.
void InvalidateCache()
Invalidates the cached data and rebuild it.
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
void AgeCargo()
Ages the all cargo in this list.
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.
void Append(CargoPacket *cp, MoveToAction action=MoveToAction::Keep)
Appends the given cargo packet.
Base classes related to the economy.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
OrderUnloadType
Unloading order types.
@ Transfer
Transfer all cargo onto the platform.
@ NoUnload
Totally no unloading will be done.
@ Unload
Force unloading all cargo onto the platform, possibly not getting paid.
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Pseudo random number generator.
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
A number of safeguards to prevent using unsafe methods.
Base classes/functions for stations.
Definition of base types and functions in a cross-platform compatible way.
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.
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.
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 AddFeederShare(Money new_share)
Adds some feeder share to the packet.
CargoPacket(CargoPacketID index)
Create a new packet for savegame loading.
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
Are the two CargoPackets mergeable.
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 first_station
The station where the cargo came from first.
static void InvalidateAllFrom(Source src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
uint16_t periods_in_transit
Amount of cargo aging periods this packet has been in transit.
Helper class to perform the cargo payment.
Money PayTransfer(CargoType cargo, const CargoPacket *cp, uint count, TileIndex current_tile)
Handle payment for transfer of the given cargo packet.
FlowStatMap flows
Planned flows through this station.
Stores station stats for a single cargo.
const GoodsEntryData & GetData() const
Get optional cargo packet/flow data.
bool HasData() const
Test if this goods entry has optional cargo packet/flow data.
PoolItem(CargoPacketID index)
static Pool::IterateWrapper< CargoPacket > Iterate(size_t from=0)
static T * Create(Targs &&... args)
static bool CanAllocateItem(size_t n=1)
const CargoPacketID index
A location from where cargo can come from (or go to).
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.