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

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.
 
static void InvalidateAllFrom (StationID sid)
 Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
 
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()
 
static bool CleaningPool ()
 Returns current state of pool cleaning - yes or no.
 
static bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-nullptr) Titem.
 
static Titem * Get (size_t index)
 Returns Titem with given index.
 
static Titem * GetIfValid (size_t index)
 Returns Titem with given index.
 
static size_t GetPoolSize ()
 Returns first unused index.
 
static size_t GetNumItems ()
 Returns number of valid items in the pool.
 
static void PostDestructor (size_t index)
 Dummy function called after destructor of each member.
 
static Pool::IterateWrapper< Titem > Iterate (size_t from=0)
 Returns an iterable ensemble of all valid Titem.
 

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?
 

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/4]

CargoPacket::CargoPacket ( )

Create a new packet for savegame loading.

Definition at line 27 of file cargopacket.cpp.

References Industry, and INVALID_SOURCE.

Referenced by Split().

◆ CargoPacket() [2/4]

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.

References count.

◆ CargoPacket() [3/4]

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.

References count.

◆ CargoPacket() [4/4]

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.

References count.

◆ ~CargoPacket()

CargoPacket::~CargoPacket ( )
inline

Destroy the packet.

Definition at line 82 of file cargopacket.h.

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.

Referenced by VehicleCargoList::Stage().

◆ AfterLoad()

◆ 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(), and CargoPayment::PayTransfer().

◆ 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< StationCargoList, StationCargoPacketMap >::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 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.

References periods_in_transit.

Referenced by CargoPayment::PayFinalDelivery(), and CargoPayment::PayTransfer().

◆ 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 Station::~Station(), and 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 Symbol Documentation

◆ CargoList

template<class Tinst , class Tcont >
friend class CargoList
friend

The CargoList caches, thus needs to know about it.

Definition at line 67 of file cargopacket.h.

◆ 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.

◆ StationCargoList

friend class StationCargoList
friend

Definition at line 69 of file cargopacket.h.

◆ VehicleCargoList

friend class VehicleCargoList
friend

Definition at line 68 of file cargopacket.h.

Field Documentation

◆ count

◆ feeder_share

Money CargoPacket::feeder_share = 0
private

Value of feeder pickup to be paid for on delivery of cargo.

Definition at line 51 of file cargopacket.h.

Referenced by VehicleCargoList::AddToCache(), GetFeederShare(), Merge(), Reduce(), and Split().

◆ first_station

StationID CargoPacket::first_station = INVALID_STATION
private

◆ MAX_COUNT

const uint16_t CargoPacket::MAX_COUNT = UINT16_MAX
static

Maximum number of items in a single cargo packet.

Definition at line 74 of file cargopacket.h.

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

◆ next_hop

StationID CargoPacket::next_hop = INVALID_STATION
private

◆ periods_in_transit

uint16_t CargoPacket::periods_in_transit = 0
private

◆ source_id

SourceID CargoPacket::source_id = INVALID_SOURCE
private

Index of industry/town/HQ, INVALID_SOURCE if unknown/invalid.

Definition at line 56 of file cargopacket.h.

Referenced by VehicleCargoList::AreMergable(), StationCargoList::AreMergable(), and GetSourceID().

◆ source_type

SourceType CargoPacket::source_type = SourceType::Industry
private

Type of source_id.

Definition at line 57 of file cargopacket.h.

Referenced by VehicleCargoList::AreMergable(), StationCargoList::AreMergable(), and GetSourceType().

◆ source_xy

TileIndex CargoPacket::source_xy = INVALID_TILE
private

The origin of the cargo.

Definition at line 53 of file cargopacket.h.

Referenced by AfterLoad(), VehicleCargoList::AreMergable(), and StationCargoList::AreMergable().

◆ travelled

Vector CargoPacket::travelled {0, 0}
private

If cargo is in station: the vector from the unload tile to the source tile. If in vehicle: an intermediate value.

Definition at line 54 of file cargopacket.h.

Referenced by GetDistance(), UpdateLoadingTile(), and UpdateUnloadingTile().


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