OpenTTD Source 20260206-master-g4d4e37dbf1
CargoPacket Struct Reference

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

#include <cargopacket.h>

Inheritance diagram for CargoPacket:

Data Structures

struct  Vector

Public Member Functions

 CargoPacket (CargoPacketID index)
 Create a new packet for savegame loading.
 CargoPacket (CargoPacketID index, StationID first_station, uint16_t count, Source source)
 Creates a new cargo packet.
 CargoPacket (CargoPacketID index, uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share)
 Create a new cargo packet.
 CargoPacket (CargoPacketID index, 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.
Source GetSource () const
 Gets the source of the packet for subsidy purposes.
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.

Static Public Member Functions

static void InvalidateAllFrom (Source src)
 Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
static void InvalidateAllFrom (StationID sid)
 Invalidates (sets source to StationID::Invalid()) all cargo packets from given station.
static void AfterLoad ()
 Savegame conversion for cargopackets.

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.
Source source {Source::Invalid, SourceType::Industry}
 Source of the cargo.
StationID first_station = StationID::Invalid()
 The station where the cargo came from first.
StationID next_hop = StationID::Invalid()
 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?

Detailed Description

Container for cargo from the same location and time.

Definition at line 41 of file cargopacket.h.

Constructor & Destructor Documentation

◆ CargoPacket() [1/4]

CargoPacket::CargoPacket ( CargoPacketID index)

Create a new packet for savegame loading.

Parameters
indexIndex into the cargo packet pool.

Definition at line 28 of file cargopacket.cpp.

References _cargopacket_pool, and CargoPacket().

Referenced by AfterLoad(), CargoPacket(), CargoPacket(), GetCargoPacketDesc, InvalidateAllFrom(), InvalidateAllFrom(), Merge(), and Split().

◆ CargoPacket() [2/4]

CargoPacket::CargoPacket ( CargoPacketID index,
StationID first_station,
uint16_t count,
Source source )

Creates a new cargo packet.

Parameters
indexIndex into the cargo packet pool.
first_stationSource station of the packet.
countNumber of cargo entities to put in this packet.
sourceSource of the packet (for subsidies).
Precondition
count != 0

Definition at line 41 of file cargopacket.cpp.

References _cargopacket_pool, count, first_station, and source.

◆ CargoPacket() [3/4]

CargoPacket::CargoPacket ( CargoPacketID index,
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
indexIndex into the cargo packet pool.
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 _cargopacket_pool, count, feeder_share, first_station, periods_in_transit, and source_xy.

◆ CargoPacket() [4/4]

CargoPacket::CargoPacket ( CargoPacketID index,
uint16_t count,
Money feeder_share,
CargoPacket & original )

Creates a new cargo packet.

Used when loading or splitting packets.

Parameters
indexIndex into the cargo packet pool.
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 79 of file cargopacket.cpp.

References _cargopacket_pool, CargoPacket(), count, feeder_share, first_station, next_hop, periods_in_transit, source, source_xy, and travelled.

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

Referenced by CargoPayment::PayFinalDelivery(), CargoPayment::PayTransfer(), Reduce(), VehicleCargoList::RemoveFromCache(), 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.

◆ 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 209 of file cargopacket.h.

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 259 of file cargopacket.h.

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

◆ GetSource()

Source CargoPacket::GetSource ( ) const
inline

Gets the source of the packet for subsidy purposes.

Returns
The source.

Definition at line 200 of file cargopacket.h.

Referenced by CargoPayment::PayFinalDelivery().

◆ InvalidateAllFrom() [1/2]

void CargoPacket::InvalidateAllFrom ( Source src)
static

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

Parameters
srcIndex of source.

Definition at line 138 of file cargopacket.cpp.

References CargoPacket().

Referenced by Station::~Station(), and Town::~Town().

◆ InvalidateAllFrom() [2/2]

void CargoPacket::InvalidateAllFrom ( StationID sid)
static

Invalidates (sets source to StationID::Invalid()) all cargo packets from given station.

Parameters
sidStation that gets removed.

Definition at line 149 of file cargopacket.cpp.

References CargoPacket().

◆ Merge()

void CargoPacket::Merge ( CargoPacket * cp)

Merge another packet into this one.

Parameters
cpPacket to be merged in.

Definition at line 116 of file cargopacket.cpp.

References CargoPacket(), 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 127 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 101 of file cargopacket.cpp.

References 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(), and TileY().

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(), and TileY().

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

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

References CargoList.

Referenced by CargoList.

◆ GetCargoPacketDesc

SaveLoadTable GetCargoPacketDesc ( )
friend

◆ 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 52 of file cargopacket.h.

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

◆ first_station

StationID CargoPacket::first_station = StationID::Invalid()
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 = StationID::Invalid()
private

◆ periods_in_transit

uint16_t CargoPacket::periods_in_transit = 0
private

◆ source

◆ source_xy

TileIndex CargoPacket::source_xy = INVALID_TILE
private

◆ 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 55 of file cargopacket.h.

Referenced by AfterLoad(), CargoPacket(), GetCargoPacketDesc, and GetDistance().


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