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>
231 if (Tinst::AreMergable(icp, cp) &&
263 assert(cp !=
nullptr);
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) {
394 for (
const auto &cp : this->
packets) {
396 if (cp->periods_in_transit == UINT16_MAX)
continue;
398 cp->periods_in_transit++;
413 StationID current_station,
bool accepted, std::span<const StationID> next_station)
415 if (cargo_next == StationID::Invalid()) {
417 }
else if (cargo_next == current_station) {
419 }
else if (std::ranges::find(next_station, cargo_next) != std::end(next_station)) {
457 while (sum < this->
count) {
461 StationID cargo_next = StationID::Invalid();
465 }
else if (force_unload && accepted && cp->
first_station != current_station) {
467 }
else if (force_transfer) {
471 FlowStatMap::const_iterator flow_it(flows.find(cp->
first_station));
472 if (flow_it == flows.end()) {
473 cargo_next = StationID::Invalid();
475 FlowStat new_shares = flow_it->second;
477 for (
auto station_it = next_station.rbegin(); station_it != next_station.rend(); ++station_it) {
478 if (new_shares.
GetShares()->empty())
break;
482 cargo_next = StationID::Invalid();
484 cargo_next = new_shares.
GetVia();
490 if (cp->
first_station == StationID::Invalid() && !flows.empty()) {
493 bool restricted =
false;
494 FlowStatMap::const_iterator flow_it(flows.find(cp->
first_station));
495 if (flow_it == flows.end()) {
496 cargo_next = StationID::Invalid();
498 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
504 cargo_next = flow_it->second.GetVia();
512 if (deliver == this->
packets.end()) --deliver;
515 this->
packets.insert(deliver, cp);
554template <VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
558 static_assert(Tfrom - Tto == 1 || Tto - Tfrom == 1);
574 max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
577 for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
580 if (sum <= this->action_counts[MTA_TRANSFER])
continue;
581 if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
582 CargoPacket *cp_split = cp->
Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
583 sum -= cp_split->
Count();
584 this->packets.insert(it, cp_split);
586 cp->
next_hop = StationID::Invalid();
589 this->action_counts[MTA_DELIVER] -= max_move;
590 this->action_counts[MTA_TRANSFER] += max_move;
605 this->
PopCargo(CargoReturn(
this, dest, max_move, next, current_tile));
617 max_move = std::min(this->
count, max_move);
618 this->
PopCargo(CargoShift(
this, dest, max_move));
637 this->
ShiftCargo(CargoTransfer(
this, dest, move, current_tile));
642 this->
ShiftCargo(CargoDelivery(
this, move, cargo, payment, current_tile));
656 max_move = std::min(this->
count, max_move);
657 this->
PopCargo(CargoRemoval<VehicleCargoList>(
this, max_move));
672 this->
ShiftCargo(VehicleCargoReroute(
this, dest, max_move, avoid, avoid2, ge));
692 assert(cp !=
nullptr);
695 StationCargoPacketMap::List &list = this->
packets[next];
696 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
697 it != list.rend(); it++) {
717template <
class Taction>
720 std::pair<Iterator, Iterator> range(this->
packets.equal_range(next));
721 for (
Iterator it(range.first); it != range.second && it.GetKey() == next;) {
722 if (action.MaxMove() == 0)
return false;
747template <
class Taction>
750 uint max_move = action.MaxMove();
751 for (
auto it = next.rbegin(); it != next.rend(); ++it) {
753 if (action.MaxMove() == 0)
break;
755 if (include_invalid && action.MaxMove() > 0) {
756 this->
ShiftCargo(action, StationID::Invalid());
758 return max_move - action.MaxMove();
771 max_move = std::min(max_move, this->
count);
772 uint prev_count = this->
count;
775 bool do_count = cargo_per_source !=
nullptr;
776 while (max_move > moved) {
779 if (prev_count > max_move &&
RandomRange(prev_count) < prev_count - max_move) {
780 if (do_count && loop == 0) {
786 uint diff = max_move - moved;
787 if (cp->
count > diff) {
794 if (do_count) (*cargo_per_source)[cp->
first_station] -= diff;
802 if (do_count && loop > 0) {
825 return this->
ShiftCargo(CargoReservation(
this, dest, max_move, current_tile), next_station,
true);
848 return this->
ShiftCargo(CargoLoad{
this, dest, max_move, current_tile}, next_station,
true);
862 return this->
ShiftCargo(StationCargoReroute(
this, dest, max_move, avoid, avoid2, ge), {&avoid, 1},
false);
uint8_t 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.
uint action_counts[NUM_MOVE_TO_ACTION]
Counts of cargo to be transferred, delivered, kept and loaded.
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 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.
Base classes related to the economy.
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.
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
CargoPacket(CargoPacketID index)
Create a new packet for savegame loading.
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.
Vector travelled
If cargo is in station: the vector from the unload tile to the source tile. If in vehicle: an interme...
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 bool CanAllocateItem(size_t n=1)
static T * Create(Targs &&... args)
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.