OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
45 source_type(source_type),
46 first_station(first_station)
62 periods_in_transit(periods_in_transit),
63 feeder_share(feeder_share),
65 first_station(first_station)
79 periods_in_transit(original.periods_in_transit),
80 feeder_share(feeder_share),
81 source_xy(original.source_xy),
82 travelled(original.travelled),
83 source_id(original.source_id),
84 source_type(original.source_type),
86 in_vehicle(original.in_vehicle),
88 first_station(original.first_station),
89 next_hop(original.next_hop)
106 this->
count -= new_size;
127 assert(count < this->
count);
129 this->count -=
count;
140 if (cp->source_type == src_type && cp->source_id == src) cp->source_id =
INVALID_SOURCE;
151 if (cp->first_station == sid) cp->first_station = INVALID_STATION;
164 template <
class Tinst,
class Tcont>
167 for (
Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
176 template <
class Tinst,
class Tcont>
179 this->packets.clear();
188 template <
class Tinst,
class Tcont>
191 assert(count <= cp->count);
192 this->count -= count;
193 this->cargo_periods_in_transit -=
static_cast<uint64_t
>(cp->
periods_in_transit) * count;
201 template <
class Tinst,
class Tcont>
204 this->count += cp->
count;
209 template <
class Tinst,
class Tcont>
213 this->cargo_periods_in_transit = 0;
215 for (
ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
216 static_cast<Tinst *
>(
this)->AddToCache(*it);
227 template <
class Tinst,
class Tcont>
230 if (Tinst::AreMergable(icp, cp) &&
262 assert(cp !=
nullptr);
272 uint sum = cp->
count;
294 template<
class Taction>
298 while (it != this->
packets.end() && action.MaxMove() > 0) {
316 template<
class Taction>
319 if (this->
packets.empty())
return;
322 while (action.MaxMove() > 0) {
393 for (
const auto &cp : this->
packets) {
395 if (cp->periods_in_transit == UINT16_MAX)
continue;
397 cp->periods_in_transit++;
412 StationID current_station,
bool accepted,
StationIDStack next_station)
414 if (cargo_next == INVALID_STATION) {
416 }
else if (cargo_next == current_station) {
418 }
else if (next_station.
Contains(cargo_next)) {
449 bool force_unload = (order_flags &
OUFB_UNLOAD) != 0;
452 while (sum < this->
count) {
456 StationID cargo_next = INVALID_STATION;
460 }
else if (force_unload && accepted && cp->
first_station != current_station) {
462 }
else if (force_transfer) {
467 if (flow_it == ge->
flows.end()) {
468 cargo_next = INVALID_STATION;
470 FlowStat new_shares = flow_it->second;
477 cargo_next = INVALID_STATION;
479 cargo_next = new_shares.
GetVia();
488 bool restricted =
false;
490 if (flow_it == ge->
flows.end()) {
491 cargo_next = INVALID_STATION;
493 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
499 cargo_next = flow_it->second.GetVia();
507 if (deliver == this->
packets.end()) --deliver;
510 this->
packets.insert(deliver, cp);
549 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
553 static_assert(Tfrom - Tto == 1 || Tto - Tfrom == 1);
567 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move)
569 max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
572 for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
575 if (sum <= this->action_counts[MTA_TRANSFER])
continue;
576 if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
577 CargoPacket *cp_split = cp->
Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
578 sum -= cp_split->
Count();
579 this->packets.insert(it, cp_split);
584 this->action_counts[MTA_DELIVER] -= max_move;
585 this->action_counts[MTA_TRANSFER] += max_move;
612 max_move = std::min(this->
count, max_move);
650 max_move = std::min(this->
count, max_move);
686 assert(cp !=
nullptr);
689 StationCargoPacketMap::List &list = this->
packets[next];
690 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
691 it != list.rend(); it++) {
711 template <
class Taction>
715 for (
Iterator it(range.first); it != range.second && it.GetKey() == next;) {
716 if (action.MaxMove() == 0)
return false;
741 template <
class Taction>
744 uint max_move = action.MaxMove();
747 if (action.MaxMove() == 0)
break;
749 if (include_invalid && action.MaxMove() > 0) {
752 return max_move - action.MaxMove();
765 max_move = std::min(max_move, this->
count);
766 uint prev_count = this->
count;
769 bool do_count = cargo_per_source !=
nullptr;
770 while (max_move > moved) {
773 if (prev_count > max_move &&
RandomRange(prev_count) < prev_count - max_move) {
774 if (do_count && loop == 0) {
780 uint diff = max_move - moved;
781 if (cp->
count > diff) {
788 if (do_count) (*cargo_per_source)[cp->
first_station] -= diff;
796 if (do_count && loop > 0) {
842 return this->
ShiftCargo(
CargoLoad(
this, dest, max_move, current_tile), next_station,
true);
864 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint);
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Minimal stack that uses a pool to avoid pointers.
void ShiftCargo(Taction action)
Shifts cargo from the front of the packet list and applies some action to it.
Titem Pop()
Pop an item from the stack.
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
MoveToAction
Kind of actions that could be done with packets on move.
Action of reserving cargo from a station to be loaded onto a vehicle.
uint Shift(uint max_move, VehicleCargoList *dest)
Shifts cargo between two vehicles.
void OnCleanPool()
Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket'...
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.
bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile)
Stages cargo for unloading.
static bool TryMerge(CargoPacket *cp, CargoPacket *icp)
Tries to merge the second packet into the first and return if that was successful.
uint Load(uint max_move, VehicleCargoList *dest, StationIDStack next, TileIndex current_tile)
Loads cargo onto a vehicle.
uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile)
Unloads cargo at the given station.
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
static void InvalidateAllFrom(SourceType src_type, SourceID src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
uint reserved_count
Amount of cargo being reserved for loading.
void InvalidateCache()
Invalidates the cached data and rebuild it.
void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
Removes a packet or part of it from the metadata.
void AssertCountConsistency() const
Assert that the designation counts add up.
CargoList that is used for stations.
~CargoList()
Destroy the cargolist ("frees" all cargo packets).
Action of returning previously reserved cargo from the vehicle to the station.
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.
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
SourceType
Types of cargo source and destination.
CargoList that is used for vehicles.
StationID first_station
The station where the cargo came from first.
void Merge(CargoPacket *cp)
Merge another packet into this one.
uint16_t count
The amount of cargo in this packet.
Flow statistics telling how much flow should be sent along a link.
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.
Action of rerouting cargo staged for transfer in a vehicle.
uint Truncate(uint max_move=UINT_MAX)
Truncates the cargo in this list to the given amount.
Money GetFeederShare() const
Gets the amount of money already paid to earlier vehicles in the feeder chain.
uint Reassign(uint max_move)
Moves some cargo from one designation to another.
void AgeCargo()
Ages the all cargo in this list.
Money feeder_share
Cache for the feeder share.
Money PayTransfer(const CargoPacket *cp, uint count, TileIndex current_tile)
Handle payment for transfer of the given cargo packet.
void AddToMeta(const CargoPacket *cp, MoveToAction action)
Adds a packet to the metadata.
const SharesMap * GetShares() const
Get the actual shares as a const pointer so that they can be iterated over.
iterator erase(iterator it)
Erase the value pointed to by an iterator.
Helper class to perform the cargo payment.
void Append(CargoPacket *cp, MoveToAction action=MTA_KEEP)
Appends the given cargo packet.
Action of final delivery of cargo.
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
void InvalidateCache()
Invalidates the cached data and rebuilds it.
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next, StationID current_station, bool accepted, StationIDStack next_station)
Choose action to be performed with the given cargo packet.
uint count
Cache for the number of cargo entities.
static const uint16_t MAX_COUNT
Maximum number of items in a single cargo packet.
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
@ MTA_TRANSFER
Transfer the cargo to the station.
uint action_counts[NUM_MOVE_TO_ACTION]
Counts of cargo to be transferred, delivered, kept and loaded.
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...
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
std::pair< iterator, iterator > equal_range(const Tkey &key)
Get a pair of iterators specifying a range of items with equal keys.
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
Action of shifting cargo from one vehicle to another.
CargoPacketList ::reverse_iterator ReverseIterator
The reverse iterator for our container.
uint16_t periods_in_transit
Amount of cargo aging periods this packet has been in transit.
CargoPacketList packets
The cargo packets in this list.
Stores station stats for a single cargo.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
StationID next_hop
Station where the cargo wants to go next.
Base class for all pools.
Action of transferring cargo from a vehicle to a station.
FlowStatMap flows
Planned flows through this station.
@ OUFB_NO_UNLOAD
Totally no unloading will be done.
@ MTA_LOAD
Load the cargo from the station.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
StationID GetVia() const
Get a station a package can be routed to.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
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.
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.
CargoPacketList ::iterator Iterator
The iterator for our container.
Container for cargo from the same location and time.
bool IsEmpty() const
Check if the stack is empty.
@ MTA_KEEP
Keep the cargo in the vehicle.
uint ActionCount(MoveToAction action) const
Returns the amount of cargo designated for a given purpose.
@ MTA_DELIVER
Deliver the cargo to some town or industry.
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
uint16_t Count() const
Gets the number of 'items' in this packet.
uint Reserve(uint max_move, VehicleCargoList *dest, StationIDStack next, TileIndex current_tile)
Reserves cargo for loading onto the vehicle.
void PopCargo(Taction action)
Pops cargo from the back of the packet list and applies some action to it.
Action of rerouting cargo in a station.
CargoPacketPool _cargopacket_pool("CargoPacket")
The actual pool with cargo packets.
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 Contains(const Titem &item) const
Check if the given item is contained in the stack.
@ Industry
Source/destination is an industry.
void ChangeShare(StationID st, int flow)
Change share for specified station.
CargoPacket()
Create a new packet for savegame loading.
Action of loading cargo from a station onto a vehicle.
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.