OpenTTD Source  20241121-master-g67a0fccfad
CargoPacket Struct Reference

Container for cargo from the same location and time. More...

#include <cargopacket.h>

Inheritance diagram for CargoPacket:
Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >

Data Structures

struct  Vector
 

Public Member Functions

 CargoPacket ()
 Create a new packet for savegame loading.
 
 CargoPacket (StationID first_station, uint16_t count, SourceType source_type, SourceID source_id)
 Creates a new cargo packet. More...
 
 CargoPacket (uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share)
 Create a new cargo packet. More...
 
 CargoPacket (uint16_t count, Money feeder_share, CargoPacket &original)
 Creates a new cargo packet. More...
 
 ~CargoPacket ()
 Destroy the packet.
 
CargoPacketSplit (uint new_size)
 Split this packet in two and return the split off part. More...
 
void Merge (CargoPacket *cp)
 Merge another packet into this one. More...
 
void Reduce (uint count)
 Reduce the packet by the given amount and remove the feeder share. More...
 
void SetNextHop (StationID next_hop)
 Sets the station where the packet is supposed to go next. More...
 
void UpdateLoadingTile (TileIndex tile)
 Update for the cargo being loaded on this tile. More...
 
void UpdateUnloadingTile (TileIndex tile)
 Update for the cargo being unloaded on this tile. More...
 
void AddFeederShare (Money new_share)
 Adds some feeder share to the packet. More...
 
uint16_t Count () const
 Gets the number of 'items' in this packet. More...
 
Money GetFeederShare () const
 Gets the amount of money already paid to earlier vehicles in the feeder chain. More...
 
Money GetFeederShare (uint part) const
 Gets part of the amount of money already paid to earlier vehicles in the feeder chain. More...
 
uint16_t GetPeriodsInTransit () const
 Gets the number of cargo aging periods this cargo has been in transit. More...
 
SourceType GetSourceType () const
 Gets the type of the cargo's source. More...
 
SourceID GetSourceID () const
 Gets the ID of the cargo's source. More...
 
StationID GetFirstStation () const
 Gets the ID of the station where the cargo was loaded for the first time. More...
 
uint GetDistance (TileIndex current_tile) const
 Get the current distance the cargo has traveled. More...
 
StationID GetNextHop () const
 Gets the ID of station the cargo wants to go next. More...
 
- Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >
void * operator new (size_t size)
 Allocates space for new Titem. More...
 
void * operator new (size_t size, size_t index)
 Allocates space for new Titem with given index. More...
 
void * operator new (size_t, void *ptr)
 Allocates space for new Titem at given memory address. More...
 
void operator delete (void *p)
 Marks Titem as free. More...
 

Static Public Member Functions

static void InvalidateAllFrom (SourceType src_type, SourceID src)
 Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source. More...
 
static void InvalidateAllFrom (StationID sid)
 Invalidates (sets source to INVALID_STATION) all cargo packets from given station. More...
 
static void AfterLoad ()
 Savegame conversion for cargopackets.
 
- Static Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >
static bool CanAllocateItem (size_t n=1)
 Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() More...
 
static bool CleaningPool ()
 Returns current state of pool cleaning - yes or no. More...
 
static bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-nullptr) Titem. More...
 
static Titem * Get (size_t index)
 Returns Titem with given index. More...
 
static Titem * GetIfValid (size_t index)
 Returns Titem with given index. More...
 
static size_t GetPoolSize ()
 Returns first unused index. More...
 
static size_t GetNumItems ()
 Returns number of valid items in the pool. More...
 
static void PostDestructor ([[maybe_unused]] size_t index)
 Dummy function called after destructor of each member. More...
 
static Pool::IterateWrapper< Titem > Iterate (size_t from=0)
 Returns an iterable ensemble of all valid Titem. More...
 

Static Public Attributes

static const uint16_t MAX_COUNT = UINT16_MAX
 Maximum number of items in a single cargo packet.
 

Private Attributes

uint16_t count = 0
 The amount of cargo in this packet.
 
uint16_t periods_in_transit = 0
 Amount of cargo aging periods this packet has been in transit.
 
Money feeder_share = 0
 Value of feeder pickup to be paid for on delivery of cargo.
 
TileIndex source_xy = INVALID_TILE
 The origin of the cargo.
 
Vector travelled {0, 0}
 If cargo is in station: the vector from the unload tile to the source tile. If in vehicle: an intermediate value.
 
SourceID source_id = INVALID_SOURCE
 Index of industry/town/HQ, INVALID_SOURCE if unknown/invalid.
 
SourceType source_type = SourceType::Industry
 Type of source_id.
 
StationID first_station = INVALID_STATION
 The station where the cargo came from first.
 
StationID next_hop = INVALID_STATION
 Station where the cargo wants to go next.
 

Friends

template<class Tinst , class Tcont >
class CargoList
 The CargoList caches, thus needs to know about it.
 
class VehicleCargoList
 
class StationCargoList
 
SaveLoadTable GetCargoPacketDesc ()
 We want this to be saved, right? More...
 

Additional Inherited Members

- Public Types inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >
typedef struct Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero > Pool
 Type of the pool this item is going to be part of.
 
- Data Fields inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >
Tindex index
 Index of this pool item.
 

Detailed Description

Container for cargo from the same location and time.

Definition at line 40 of file cargopacket.h.

Constructor & Destructor Documentation

◆ CargoPacket() [1/3]

CargoPacket::CargoPacket ( StationID  first_station,
uint16_t  count,
SourceType  source_type,
SourceID  source_id 
)

Creates a new cargo packet.

Parameters
first_stationSource station of the packet.
countNumber of cargo entities to put in this packet.
source_type'Type' of source the packet comes from (for subsidies).
source_idActual source of the packet (for subsidies).
Precondition
count != 0

Definition at line 42 of file cargopacket.cpp.

◆ CargoPacket() [2/3]

CargoPacket::CargoPacket ( uint16_t  count,
uint16_t  periods_in_transit,
StationID  first_station,
TileIndex  source_xy,
Money  feeder_share 
)

Create a new cargo packet.

Used for older savegames to load in their partial data.

Parameters
countNumber of cargo entities to put in this packet.
periods_in_transitNumber of cargo aging periods the cargo has been in transit.
first_stationStation the cargo was initially loaded.
source_xyStation location the cargo was initially loaded.
feeder_shareFeeder share the packet has already accumulated.

Definition at line 60 of file cargopacket.cpp.

◆ CargoPacket() [3/3]

CargoPacket::CargoPacket ( uint16_t  count,
Money  feeder_share,
CargoPacket original 
)

Creates a new cargo packet.

Used when loading or splitting packets.

Parameters
countNumber of cargo entities to put in this packet.
feeder_shareFeeder share the packet has already accumulated.
originalThe original packet we are splitting.

Definition at line 77 of file cargopacket.cpp.

Member Function Documentation

◆ AddFeederShare()

void CargoPacket::AddFeederShare ( Money  new_share)
inline

Adds some feeder share to the packet.

Parameters
new_shareFeeder share to be added.

Definition at line 149 of file cargopacket.h.

◆ Count()

◆ GetDistance()

uint CargoPacket::GetDistance ( TileIndex  current_tile) const
inline

Get the current distance the cargo has traveled.

Parameters
current_tileCurrent tile of the cargo.
Returns
uint The distance (in tiles) traveled.

Definition at line 229 of file cargopacket.h.

References abs(), DistanceManhattan(), INVALID_TILE, TileX(), TileY(), and travelled.

Referenced by CargoPayment::PayFinalDelivery().

◆ GetFeederShare() [1/2]

Money CargoPacket::GetFeederShare ( ) const
inline

Gets the amount of money already paid to earlier vehicles in the feeder chain.

Returns
Feeder share.

Definition at line 168 of file cargopacket.h.

References feeder_share.

Referenced by CargoList< VehicleCargoList, CargoPacketList >::InvalidateCache(), CargoPayment::PayFinalDelivery(), CargoPayment::PayTransfer(), Reduce(), and Split().

◆ GetFeederShare() [2/2]

Money CargoPacket::GetFeederShare ( uint  part) const
inline

Gets part of the amount of money already paid to earlier vehicles in the feeder chain.

Parameters
partAmount of cargo to get the share for.
Returns
Feeder share for the given amount of cargo.

Definition at line 179 of file cargopacket.h.

References count.

◆ GetFirstStation()

StationID CargoPacket::GetFirstStation ( ) const
inline

Gets the ID of the station where the cargo was loaded for the first time.

Returns
StationID.

Definition at line 218 of file cargopacket.h.

References first_station.

Referenced by StationViewWindow::BuildCargoList(), StationCargoReroute::operator()(), and VehicleCargoReroute::operator()().

◆ GetNextHop()

StationID CargoPacket::GetNextHop ( ) const
inline

Gets the ID of station the cargo wants to go next.

Returns
Next station for this packets.

Definition at line 268 of file cargopacket.h.

References next_hop.

Referenced by CargoTransfer::operator()(), and VehicleCargoReroute::operator()().

◆ GetPeriodsInTransit()

uint16_t CargoPacket::GetPeriodsInTransit ( ) const
inline

Gets the number of cargo aging periods this cargo has been in transit.

By default a period is 2.5 days (CARGO_AGING_TICKS = 185 ticks), however vehicle NewGRFs can overide the length of the cargo aging period. The value is capped at UINT16_MAX.

Returns
Length this cargo has been in transit.

Definition at line 191 of file cargopacket.h.

Referenced by CargoPayment::PayFinalDelivery().

◆ GetSourceID()

SourceID CargoPacket::GetSourceID ( ) const
inline

Gets the ID of the cargo's source.

An IndustryID, TownID or CompanyID.

Returns
Source ID.

Definition at line 209 of file cargopacket.h.

References source_id.

Referenced by CargoPayment::PayFinalDelivery().

◆ GetSourceType()

SourceType CargoPacket::GetSourceType ( ) const
inline

Gets the type of the cargo's source.

industry, town or head quarter.

Returns
Source type.

Definition at line 200 of file cargopacket.h.

References source_type.

Referenced by CargoPayment::PayFinalDelivery().

◆ InvalidateAllFrom() [1/2]

void CargoPacket::InvalidateAllFrom ( SourceType  src_type,
SourceID  src 
)
static

Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.

Parameters
src_typeType of source.
srcIndex of source.

Definition at line 137 of file cargopacket.cpp.

References INVALID_SOURCE, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >::Iterate().

Referenced by Town::~Town().

◆ InvalidateAllFrom() [2/2]

void CargoPacket::InvalidateAllFrom ( StationID  sid)
static

Invalidates (sets source to INVALID_STATION) all cargo packets from given station.

Parameters
sidStation that gets removed.

Definition at line 148 of file cargopacket.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >::Iterate().

◆ Merge()

void CargoPacket::Merge ( CargoPacket cp)

Merge another packet into this one.

Parameters
cpPacket to be merged in.

Definition at line 114 of file cargopacket.cpp.

References count, and feeder_share.

Referenced by CargoList< Tinst, Tcont >::TryMerge().

◆ Reduce()

void CargoPacket::Reduce ( uint  count)

Reduce the packet by the given amount and remove the feeder share.

Parameters
countAmount to be removed.

Definition at line 125 of file cargopacket.cpp.

References count, feeder_share, and GetFeederShare().

Referenced by CargoRemoval< Tsource >::Postprocess(), and StationCargoList::Truncate().

◆ SetNextHop()

void CargoPacket::SetNextHop ( StationID  next_hop)
inline

Sets the station where the packet is supposed to go next.

Parameters
next_hopNext station the packet should go to.

Definition at line 92 of file cargopacket.h.

References next_hop.

Referenced by VehicleCargoReroute::operator()().

◆ Split()

CargoPacket * CargoPacket::Split ( uint  new_size)

Split this packet in two and return the split off part.

Parameters
new_sizeSize of the split part.
Returns
Split off part, or nullptr if no packet could be allocated!

Definition at line 99 of file cargopacket.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >::CanAllocateItem(), CargoPacket(), count, feeder_share, and GetFeederShare().

Referenced by CargoRemoval< VehicleCargoList >::Postprocess(), and VehicleCargoList::Reassign< VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER >().

◆ UpdateLoadingTile()

void CargoPacket::UpdateLoadingTile ( TileIndex  tile)
inline

Update for the cargo being loaded on this tile.

When a CargoPacket is created, it is moved to a station. But at that moment in time it is not known yet at which tile the cargo will be picked up. As this tile is used for payment information, we delay setting the source_xy till first pickup, getting a better idea where a cargo started from.

Further more, we keep track of the amount of tiles the cargo moved inside a vehicle. This is used in GetDistance() below.

Parameters
tileTile the cargo is being picked up from.

Definition at line 111 of file cargopacket.h.

References INVALID_TILE, TileX(), TileY(), and travelled.

Referenced by CargoLoad::operator()(), and CargoReservation::operator()().

◆ UpdateUnloadingTile()

void CargoPacket::UpdateUnloadingTile ( TileIndex  tile)
inline

Update for the cargo being unloaded on this tile.

Parameters
tileTile the cargo is being dropped off at.

Definition at line 134 of file cargopacket.h.

References TileX(), TileY(), and travelled.

Referenced by CargoTransfer::operator()(), and CargoReturn::operator()().

Friends And Related Function Documentation

◆ GetCargoPacketDesc

SaveLoadTable GetCargoPacketDesc ( )
friend

We want this to be saved, right?

Returns
the saveload description for CargoPackets.

Definition at line 119 of file cargopacket_sl.cpp.


The documentation for this struct was generated from the following files: