OpenTTD Source: CargoPacket Struct Reference

OpenTTD Source 13.1

Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | Friends
CargoPacket Struct Reference

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

`#include <cargopacket.h>`

Inheritance diagram for CargoPacket:

## Public Member Functions
| CargoPacket ()
| Create a new packet for savegame loading.
| CargoPacket (StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id)
| Creates a new cargo packet. More...
| CargoPacket (uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, Money feeder_share=0, SourceType source_type=ST_INDUSTRY, SourceID source_id=INVALID_SOURCE)
| Creates a new cargo packet. More...
| ~CargoPacket ()
| Destroy the packet.
CargoPacket * | Split (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 | SetLoadPlace (TileIndex load_place)
| Sets the tile where the packet was loaded last. More...
void | SetNextStation (StationID next_station)
| Sets the station where the packet is supposed to go next. More...
void | AddFeederShare (Money new_share)
| Adds some feeder share to the packet. More...
uint16 | Count () const
| Gets the number of 'items' in this packet. More...
Money | FeederShare () const
| Gets the amount of money already paid to earlier vehicles in the feeder chain. More...
Money | FeederShare (uint part) const
| Gets part of the amount of money already paid to earlier vehicles in the feeder chain. More...
byte | DaysInTransit () const
| Gets the number of days this cargo has been in transit. More...
SourceType | SourceSubsidyType () const
| Gets the type of the cargo's source. More...
SourceID | SourceSubsidyID () const
| Gets the ID of the cargo's source. More...
StationID | SourceStation () const
| Gets the ID of the station where the cargo was loaded for the first time. More...
TileIndex | SourceStationXY () const
| Gets the coordinates of the cargo's source station. More...
TileIndex | LoadedAtXY () const
| Gets the coordinates of the cargo's last loading station. More...
StationID | NextStation () 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 size, 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 (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 | MAX_COUNT = UINT16_MAX
| Maximum number of items in a single cargo packet.

## Private Attributes
Money | feeder_share
| Value of feeder pickup to be paid for on delivery of cargo.
uint16 | count
| The amount of cargo in this packet.
byte | days_in_transit
| Amount of days this packet has been in transit.
SourceType | source_type
| Type of `source_id`.
SourceID | source_id
| Index of source, INVALID_SOURCE if unknown/invalid.
StationID | source
| The station where the cargo came from first.
TileIndex | source_xy
| The origin of the cargo (first station in feeder chain).
union {
TileOrStationID loaded_at_xy
| Location where this cargo has been loaded into the vehicle.
TileOrStationID next_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 43 of file cargopacket.h.

## Constructor & Destructor Documentation

## ◆ CargoPacket() [1/2]

CargoPacket::CargoPacket | ( | StationID | source,
|  | TileIndex | source_xy,
|  | uint16 | count,
|  | SourceType | source_type,
|  | SourceID | source_id
| ) |  |

Creates a new cargo packet.

Parameters

  source | Source station of the packet.
  source_xy | Source location of the packet.
  count | Number of cargo entities to put in this packet.
  source_type | 'Type' of source the packet comes from (for subsidies).
  source_id | Actual source of the packet (for subsidies).

Precondition
  count != 0

Note
  We have to zero memory ourselves here because we are using a 'new' that, in contrary to all other pools, does not memset to 0.

Definition at line 44 of file cargopacket.cpp.

References count, and source_type.

## ◆ CargoPacket() [2/2]

CargoPacket::CargoPacket | ( | uint16 | count,
|  | byte | days_in_transit,
|  | StationID | source,
|  | TileIndex | source_xy,
|  | TileIndex | loaded_at_xy,
|  | Money | feeder_share = `0`,
|  | SourceType | source_type = `ST_INDUSTRY`,
|  | SourceID | source_id = `INVALID_SOURCE`
| ) |  |

Creates a new cargo packet.

Initializes the fields that cannot be changed later. Used when loading or splitting packets.

Parameters

  count | Number of cargo entities to put in this packet.
  days_in_transit | Number of days the cargo has been in transit.
  source | Station the cargo was initially loaded.
  source_xy | Station location the cargo was initially loaded.
  loaded_at_xy | Location the cargo was loaded last.
  feeder_share | Feeder share the packet has already accumulated.
  source_type | 'Type' of source the packet comes from (for subsidies).
  source_id | Actual source of the packet (for subsidies).

Note
  We have to zero memory ourselves here because we are using a 'new' that, in contrary to all other pools, does not memset to 0.

Definition at line 71 of file cargopacket.cpp.

References count, and source_type.

## Member Function Documentation

## ◆ AddFeederShare()

void CargoPacket::AddFeederShare | ( | Money | new_share | ) |
| inline

Adds some feeder share to the packet.

Parameters

  new_share | Feeder share to be added.

Definition at line 94 of file cargopacket.h.

## ◆ Count()

uint16 CargoPacket::Count | ( |  | ) | const
| inline

Gets the number of 'items' in this packet.

Returns
  Item count.

Definition at line 100 of file cargopacket.h.

References count.

Referenced by CargoTransfer::operator()(), CargoLoad::operator()(), CargoReservation::operator()(), CargoReturn::operator()(), CargoShift::operator()(), StationCargoReroute::operator()(), VehicleCargoReroute::operator()(), CargoRemoval< VehicleCargoList >::Postprocess(), CargoRemoval< VehicleCargoList >::Preprocess(), CargoMovement< VehicleCargoList, VehicleCargoList >::Preprocess(), and VehicleCargoList::Reassign< VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER >().

## ◆ DaysInTransit()

byte CargoPacket::DaysInTransit | ( |  | ) | const
| inline

Gets the number of days this cargo has been in transit.

This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and it is capped at 255.

Returns
  Length this cargo has been in transit.

Definition at line 132 of file cargopacket.h.

References days_in_transit.

Referenced by CargoPayment::PayFinalDelivery().

## ◆ FeederShare() [1/2]

Money CargoPacket::FeederShare | ( |  | ) | const
| inline

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

Returns
  Feeder share.

Definition at line 110 of file cargopacket.h.

References feeder_share.

Referenced by CargoPayment::PayFinalDelivery(), CargoPayment::PayTransfer(), Reduce(), VehicleCargoList::RemoveFromCache(), and Split().

## ◆ FeederShare() [2/2]

Money CargoPacket::FeederShare | ( | uint | part | ) | const
| inline

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

Parameters

  part | Amount of cargo to get the share for.

Returns
  Feeder share for the given amount of cargo.

Definition at line 121 of file cargopacket.h.

References count.

## ◆ 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_type | Type of source.
  src | Index of source.

Definition at line 127 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

  sid | Station that gets removed.

Definition at line 138 of file cargopacket.cpp.

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

## ◆ LoadedAtXY()

TileIndex CargoPacket::LoadedAtXY | ( |  | ) | const
| inline

Gets the coordinates of the cargo's last loading station.

Returns
  Last loading station's coordinates.

Definition at line 177 of file cargopacket.h.

References loaded_at_xy.

## ◆ Merge()

void CargoPacket::Merge | ( | CargoPacket * | cp | ) |

Merge another packet into this one.

Parameters

  cp | Packet to be merged in.

Definition at line 104 of file cargopacket.cpp.

References count, and feeder_share.

Referenced by CargoList< VehicleCargoList, CargoPacketList >::TryMerge().

## ◆ NextStation()

StationID CargoPacket::NextStation | ( |  | ) | const
| inline

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

Returns
  Next station for this packets.

Definition at line 186 of file cargopacket.h.

References next_station.

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

## ◆ Reduce()

void CargoPacket::Reduce | ( | uint | count | ) |

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

Parameters

  count | Amount to be removed.

Definition at line 115 of file cargopacket.cpp.

References count, feeder_share, and FeederShare().

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

## ◆ SetLoadPlace()

void CargoPacket::SetLoadPlace | ( | TileIndex | load_place | ) |
| inline

Sets the tile where the packet was loaded last.

Parameters

  load_place | Tile where the packet was loaded last.

Definition at line 82 of file cargopacket.h.

References loaded_at_xy.

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

## ◆ SetNextStation()

void CargoPacket::SetNextStation | ( | StationID | next_station | ) |
| inline

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

Parameters

  next_station | Next station the packet should go to.

Definition at line 88 of file cargopacket.h.

References next_station.

Referenced by VehicleCargoReroute::operator()().

## ◆ SourceStation()

StationID CargoPacket::SourceStation | ( |  | ) | const
| inline

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

Returns
  StationID.

Definition at line 159 of file cargopacket.h.

References source.

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

## ◆ SourceStationXY()

TileIndex CargoPacket::SourceStationXY | ( |  | ) | const
| inline

Gets the coordinates of the cargo's source station.

Returns
  Source station's coordinates.

Definition at line 168 of file cargopacket.h.

References source_xy.

Referenced by CargoPayment::PayFinalDelivery().

## ◆ SourceSubsidyID()

SourceID CargoPacket::SourceSubsidyID | ( |  | ) | const
| inline

Gets the ID of the cargo's source.

An IndustryID, TownID or CompanyID.

Returns
  Source ID.

Definition at line 150 of file cargopacket.h.

References source_id.

Referenced by CargoPayment::PayFinalDelivery().

## ◆ SourceSubsidyType()

SourceType CargoPacket::SourceSubsidyType | ( |  | ) | const
| inline

Gets the type of the cargo's source.

industry, town or head quarter.

Returns
  Source type.

Definition at line 141 of file cargopacket.h.

References source_type.

Referenced by CargoPayment::PayFinalDelivery().

## ◆ Split()

CargoPacket * CargoPacket::Split | ( | uint | new_size | ) |

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

Parameters

  new_size | Size of the split part.

Returns
  Split off part, or nullptr if no packet could be allocated!

Definition at line 89 of file cargopacket.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >::CanAllocateItem(), CargoPacket(), count, days_in_transit, feeder_share, FeederShare(), loaded_at_xy, source, source_id, source_type, and source_xy.

Referenced by CargoMovement< VehicleCargoList, VehicleCargoList >::Preprocess(), and VehicleCargoList::Reassign< VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER >().

## 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 88 of file cargopacket_sl.cpp.

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

- src/cargopacket.h
- src/cargopacket.cpp
- src/saveload/cargopacket_sl.cpp