OpenTTD Source 20261112-master-gbcbec5e63f
cargopacket.cpp
Go to the documentation of this file.
1/*
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "station_base.h"
12#include "core/pool_func.hpp"
13#include "core/random_func.hpp"
14#include "economy_base.h"
15#include "cargoaction.h"
16#include "order_type.h"
17
18#include "safeguards.h"
19
20/* Initialize the cargopacket-pool */
23
24
31
49
70
81 count(count),
84 source_xy(original.source_xy),
85 travelled(original.travelled),
86 source(original.source),
87#ifdef WITH_ASSERT
88 in_vehicle(original.in_vehicle),
89#endif /* WITH_ASSERT */
91 next_hop(original.next_hop)
92{
93 assert(count != 0);
94}
95
102{
103 if (!CargoPacket::CanAllocateItem()) return nullptr;
104
105 Money fs = this->GetFeederShare(new_size);
106 CargoPacket *cp_new = CargoPacket::Create(new_size, fs, *this);
107 this->feeder_share -= fs;
108 this->count -= new_size;
109 return cp_new;
110}
111
117{
118 this->count += cp->count;
119 this->feeder_share += cp->feeder_share;
120 delete cp;
121}
122
128{
129 assert(count < this->count);
130 this->feeder_share -= this->GetFeederShare(count);
131 this->count -= count;
132}
133
139{
140 for (CargoPacket *cp : CargoPacket::Iterate()) {
141 if (cp->source == src) cp->source.MakeInvalid();
142 }
143}
144
149/* static */ void CargoPacket::InvalidateAllFrom(StationID sid)
150{
151 for (CargoPacket *cp : CargoPacket::Iterate()) {
152 if (cp->first_station == sid) cp->first_station = StationID::Invalid();
153 }
154}
155
156/*
157 *
158 * Cargo list implementation
159 *
160 */
161
165template <class Tinst, class Tcont>
167{
168 for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
169 delete *it;
170 }
171}
172
177template <class Tinst, class Tcont>
179{
180 this->packets.clear();
181}
182
189template <class Tinst, class Tcont>
191{
192 assert(count <= cp->count);
193 this->count -= count;
194 this->cargo_periods_in_transit -= static_cast<uint64_t>(cp->periods_in_transit) * count;
195}
196
202template <class Tinst, class Tcont>
204{
205 this->count += cp->count;
206 this->cargo_periods_in_transit += static_cast<uint64_t>(cp->periods_in_transit) * cp->count;
207}
208
210template <class Tinst, class Tcont>
212{
213 this->count = 0;
214 this->cargo_periods_in_transit = 0;
215
216 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
217 static_cast<Tinst *>(this)->AddToCache(*it);
218 }
219}
220
228template <class Tinst, class Tcont>
230{
231 if (CargoPacket::AreMergable(icp, cp) &&
232 icp->count + cp->count <= CargoPacket::MAX_COUNT) {
233 icp->Merge(cp);
234 return true;
235 } else {
236 return false;
237 }
238}
239
240/*
241 *
242 * Vehicle cargo list implementation.
243 *
244 */
245
262{
263 assert(cp != nullptr);
264 assert(action == MoveToAction::Load ||
265 (action == MoveToAction::Keep && this->action_counts[MoveToAction::Load] == 0));
266 this->AddToMeta(cp, action);
267
268 if (this->count == cp->count) {
269 this->packets.push_back(cp);
270 return;
271 }
272
273 uint sum = cp->count;
274 for (ReverseIterator it(this->packets.rbegin()); it != this->packets.rend(); it++) {
275 CargoPacket *icp = *it;
276 if (VehicleCargoList::TryMerge(icp, cp)) return;
277 sum += icp->count;
278 if (sum >= this->action_counts[action]) {
279 this->packets.push_back(cp);
280 return;
281 }
282 }
283
284 NOT_REACHED();
285}
286
295template <class Taction>
297{
298 Iterator it(this->packets.begin());
299 while (it != this->packets.end() && action.MaxMove() > 0) {
300 CargoPacket *cp = *it;
301 if (action(cp)) {
302 it = this->packets.erase(it);
303 } else {
304 break;
305 }
306 }
307}
308
317template <class Taction>
318void VehicleCargoList::PopCargo(Taction action)
319{
320 if (this->packets.empty()) return;
321 Iterator it(--(this->packets.end()));
322 Iterator begin(this->packets.begin());
323 while (action.MaxMove() > 0) {
324 CargoPacket *cp = *it;
325 if (action(cp)) {
326 if (it != begin) {
327 this->packets.erase(it--);
328 } else {
329 this->packets.erase(it);
330 break;
331 }
332 } else {
333 break;
334 }
335 }
336}
337
345{
346 this->feeder_share -= cp->GetFeederShare(count);
348}
349
356{
357 this->feeder_share += cp->feeder_share;
358 this->Parent::AddToCache(cp);
359}
360
368{
369 assert(count <= this->action_counts[action]);
371 this->RemoveFromCache(cp, count);
372 this->action_counts[action] -= count;
374}
375
384{
385 uint remove = std::min(this->action_counts[action], count);
386 if (remove > 0) this->RemoveFromMeta(cp, action, remove);
387 return remove;
388}
389
396{
398 this->AddToCache(cp);
399 this->action_counts[action] += cp->count;
401}
402
407{
408 for (const auto &cp : this->packets) {
409 /* If we're at the maximum, then we can't increase no more. */
410 if (cp->periods_in_transit == UINT16_MAX) continue;
411
412 cp->periods_in_transit++;
413 this->cargo_periods_in_transit += cp->count;
414 }
415}
416
427 StationID current_station, bool accepted, std::span<const StationID> next_station)
428{
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;
435 } else {
436 return MoveToAction::Transfer;
437 }
438}
439
455bool VehicleCargoList::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)
456{
458 assert(this->action_counts[MoveToAction::Load] == 0);
459 this->action_counts[MoveToAction::Transfer] = this->action_counts[MoveToAction::Deliver] = this->action_counts[MoveToAction::Keep] = 0;
460 Iterator deliver = this->packets.end();
461 Iterator it = this->packets.begin();
462 uint sum = 0;
463
464 static const FlowStatMap EMPTY_FLOW_STAT_MAP = {};
465 const FlowStatMap &flows = ge->HasData() ? ge->GetData().flows : EMPTY_FLOW_STAT_MAP;
466
467 bool force_keep = unload_type == OrderUnloadType::NoUnload;
468 bool force_unload = unload_type == OrderUnloadType::Unload;
469 bool force_transfer = unload_type == OrderUnloadType::Transfer || unload_type == OrderUnloadType::Unload;
470 assert(this->count > 0 || it == this->packets.end());
471 while (sum < this->count) {
472 CargoPacket *cp = *it;
473
474 this->packets.erase(it++);
475 StationID cargo_next = StationID::Invalid();
476 MoveToAction action = MoveToAction::Load;
477 if (force_keep) {
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;
483 /* We cannot send the cargo to any of the possible next hops and
484 * also not to the current station. */
485 FlowStatMap::const_iterator flow_it(flows.find(cp->first_station));
486 if (flow_it == flows.end()) {
487 cargo_next = StationID::Invalid();
488 } else {
489 FlowStat new_shares = flow_it->second;
490 new_shares.ChangeShare(current_station, INT_MIN);
491 for (auto station_it = next_station.rbegin(); station_it != next_station.rend(); ++station_it) {
492 if (new_shares.GetShares()->empty()) break;
493 new_shares.ChangeShare(*station_it, INT_MIN);
494 }
495 if (new_shares.GetShares()->empty()) {
496 cargo_next = StationID::Invalid();
497 } else {
498 cargo_next = new_shares.GetVia();
499 }
500 }
501 } else {
502 /* Rewrite an invalid source station to some random other one to
503 * avoid keeping the cargo in the vehicle forever. */
504 if (cp->first_station == StationID::Invalid() && !flows.empty()) {
505 cp->first_station = flows.begin()->first;
506 }
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();
511 } else {
512 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
513 }
514 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
515 if (restricted && action == MoveToAction::Transfer) {
516 /* If the flow is restricted we can't transfer to it. Choose an
517 * unrestricted one instead. */
518 cargo_next = flow_it->second.GetVia();
519 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
520 }
521 }
522 Money share;
523 switch (action) {
524 case MoveToAction::Keep:
525 this->packets.push_back(cp);
526 if (deliver == this->packets.end()) --deliver;
527 break;
528 case MoveToAction::Deliver:
529 this->packets.insert(deliver, cp);
530 break;
531 case MoveToAction::Transfer:
532 this->packets.push_front(cp);
533 /* Add feeder share here to allow reusing field for next station. */
534 share = payment->PayTransfer(cargo, cp, cp->count, current_tile);
535 cp->AddFeederShare(share);
536 this->feeder_share += share;
537 cp->next_hop = cargo_next;
538 break;
539 default:
540 NOT_REACHED();
541 }
542 this->action_counts[action] += cp->count;
543 sum += cp->count;
544 }
546 return this->action_counts[MoveToAction::Deliver] > 0 || this->action_counts[MoveToAction::Transfer] > 0;
547}
548
551{
552 this->feeder_share = 0;
554}
555
568template <VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
569uint VehicleCargoList::Reassign(uint max_move)
570{
571 static_assert(Tfrom != MoveToAction::Transfer && Tto != MoveToAction::Transfer);
572 static_assert(to_underlying(Tfrom) - to_underlying(Tto) == 1 || to_underlying(Tto) - to_underlying(Tfrom) == 1);
573 max_move = std::min(this->action_counts[Tfrom], max_move);
574 this->action_counts[Tfrom] -= max_move;
575 this->action_counts[Tto] += max_move;
576 return max_move;
577}
578
585template <>
587{
588 max_move = std::min(this->action_counts[MoveToAction::Deliver], max_move);
589
590 uint sum = 0;
591 for (Iterator it(this->packets.begin()); sum < this->action_counts[MoveToAction::Transfer] + max_move;) {
592 CargoPacket *cp = *it++;
593 sum += cp->Count();
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);
599 }
600 cp->next_hop = StationID::Invalid();
601 }
602
603 this->action_counts[MoveToAction::Deliver] -= max_move;
604 this->action_counts[MoveToAction::Transfer] += max_move;
605 return max_move;
606}
607
616uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next, TileIndex current_tile)
617{
618 max_move = std::min(this->action_counts[MoveToAction::Load], max_move);
619 this->PopCargo(CargoReturn(this, dest, max_move, next, current_tile));
620 return max_move;
621}
622
630{
631 max_move = std::min(this->count, max_move);
632 this->PopCargo(CargoShift(this, dest, max_move));
633 return max_move;
634}
635
646uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoType cargo, CargoPayment *payment, TileIndex current_tile)
647{
648 uint moved = 0;
649 if (this->action_counts[MoveToAction::Transfer] > 0) {
650 uint move = std::min(this->action_counts[MoveToAction::Transfer], max_move);
651 this->ShiftCargo(CargoTransfer(this, dest, move, current_tile));
652 moved += move;
653 }
654 if (this->action_counts[MoveToAction::Transfer] == 0 && this->action_counts[MoveToAction::Deliver] > 0 && moved < max_move) {
655 uint move = std::min(this->action_counts[MoveToAction::Deliver], max_move - moved);
656 this->ShiftCargo(CargoDelivery(this, move, cargo, payment, current_tile));
657 moved += move;
658 }
659 return moved;
660}
661
668uint VehicleCargoList::Truncate(uint max_move)
669{
670 max_move = std::min(this->count, max_move);
671 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
672 return max_move;
673}
674
684uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
685{
686 max_move = std::min(this->action_counts[MoveToAction::Transfer], max_move);
687 this->ShiftCargo(VehicleCargoReroute(this, dest, max_move, avoid, avoid2, ge));
688 return max_move;
689}
690
691/*
692 *
693 * Station cargo list implementation.
694 *
695 */
696
705void StationCargoList::Append(CargoPacket *cp, StationID next)
706{
707 assert(cp != nullptr);
708 this->AddToCache(cp);
709
710 StationCargoPacketMap::List &list = this->packets[next];
711 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
712 it != list.rend(); it++) {
713 if (StationCargoList::TryMerge(*it, cp)) return;
714 }
715
716 /* The packet could not be merged with another one */
717 list.push_back(cp);
718}
719
732template <class Taction>
733bool StationCargoList::ShiftCargo(Taction &action, StationID next)
734{
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;
738 CargoPacket *cp = *it;
739 if (action(cp)) {
740 it = this->packets.erase(it);
741 } else {
742 return false;
743 }
744 }
745 return true;
746}
747
762template <class Taction>
763uint StationCargoList::ShiftCargo(Taction action, std::span<const StationID> next, bool include_invalid)
764{
765 uint max_move = action.MaxMove();
766 for (auto it = next.rbegin(); it != next.rend(); ++it) {
767 this->ShiftCargo(action, *it);
768 if (action.MaxMove() == 0) break;
769 }
770 if (include_invalid && action.MaxMove() > 0) {
771 this->ShiftCargo(action, StationID::Invalid());
772 }
773 return max_move - action.MaxMove();
774}
775
784uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
785{
786 max_move = std::min(max_move, this->count);
787 uint prev_count = this->count;
788 uint moved = 0;
789 uint loop = 0;
790 bool do_count = cargo_per_source != nullptr;
791 while (max_move > moved) {
792 for (Iterator it(this->packets.begin()); it != this->packets.end();) {
793 CargoPacket *cp = *it;
794 if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
795 if (do_count && loop == 0) {
796 (*cargo_per_source)[cp->first_station] += cp->count;
797 }
798 ++it;
799 continue;
800 }
801 uint diff = max_move - moved;
802 if (cp->count > diff) {
803 if (diff > 0) {
804 this->RemoveFromCache(cp, diff);
805 cp->Reduce(diff);
806 moved += diff;
807 }
808 if (loop > 0) {
809 if (do_count) (*cargo_per_source)[cp->first_station] -= diff;
810 return moved;
811 } else {
812 if (do_count) (*cargo_per_source)[cp->first_station] += cp->count;
813 ++it;
814 }
815 } else {
816 it = this->packets.erase(it);
817 if (do_count && loop > 0) {
818 (*cargo_per_source)[cp->first_station] -= cp->count;
819 }
820 moved += cp->count;
821 this->RemoveFromCache(cp, cp->count);
822 delete cp;
823 }
824 }
825 loop++;
826 }
827 return moved;
828}
829
838uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, std::span<const StationID> next_station, TileIndex current_tile)
839{
840 return this->ShiftCargo(CargoReservation(this, dest, max_move, current_tile), next_station, true);
841}
842
855uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, std::span<const StationID> next_station, TileIndex current_tile)
856{
857 uint move = std::min(dest->ActionCount(VehicleCargoList::MoveToAction::Load), max_move);
858 if (move > 0) {
859 this->reserved_count -= move;
860 dest->Reassign<VehicleCargoList::MoveToAction::Load, VehicleCargoList::MoveToAction::Keep>(move);
861 return move;
862 } else {
863 return this->ShiftCargo(CargoLoad{this, dest, max_move, current_tile}, next_station, true);
864 }
865}
866
876uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
877{
878 return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), {&avoid, 1}, false);
879}
880
881/*
882 * We have to instantiate everything we want to be usable.
883 */
886
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
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.
Definition cargopacket.h:23
Pool< CargoPacket, CargoPacketID, 1024, PoolType::Normal, true > CargoPacketPool
Type of the pool for cargo packets for a little over 16 million packets.
Definition cargopacket.h:27
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).
Definition enum_type.hpp:21
Types related to orders.
OrderUnloadType
Unloading order types.
Definition order_type.h:67
@ Transfer
Transfer all cargo onto the platform.
Definition order_type.h:70
@ NoUnload
Totally no unloading will be done.
Definition order_type.h:71
@ Unload
Force unloading all cargo onto the platform, possibly not getting paid.
Definition order_type.h:69
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.
Definition cargopacket.h:40
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.
Definition cargopacket.h:45
static const uint16_t MAX_COUNT
Maximum number of items in a single cargo packet.
Definition cargopacket.h:67
TileIndex source_xy
The origin of the cargo.
Definition cargopacket.h:47
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.
Definition cargopacket.h:57
Source source
Source of the cargo.
Definition cargopacket.h:50
Coord2D< int16_t > travelled
If cargo is in station: the vector from the unload tile to the source tile. If in vehicle: an interme...
Definition cargopacket.h:48
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.
Definition cargopacket.h:42
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.
Definition cargopacket.h:56
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.
Definition cargopacket.h:43
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.
Definition economy.cpp:1236
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.
static Pool::IterateWrapper< CargoPacket > Iterate(size_t from=0)
A location from where cargo can come from (or go to).
Definition source_type.h:32
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.
Definition tile_type.h:92