OpenTTD
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends
VehicleCargoList Class Reference

CargoList that is used for vehicles. More...

#include <cargopacket.h>

Inheritance diagram for VehicleCargoList:
CargoList< VehicleCargoList, CargoPacketList >

Public Member Functions

StationID Source () const
 Returns source of the first cargo packet in this list. More...
 
Money FeederShare () const
 Returns total sum of the feeder share for all packets. More...
 
uint ActionCount (MoveToAction action) const
 Returns the amount of cargo designated for a given purpose. More...
 
uint StoredCount () const
 Returns sum of cargo on board the vehicle (ie not only reserved). More...
 
uint TotalCount () const
 Returns sum of cargo, including reserved cargo. More...
 
uint ReservedCount () const
 Returns sum of reserved cargo. More...
 
uint UnloadCount () const
 Returns sum of cargo to be moved out of the vehicle at the current station. More...
 
uint RemainingCount () const
 Returns the sum of cargo to be kept in the vehicle at the current station. More...
 
void Append (CargoPacket *cp, MoveToAction action=MTA_KEEP)
 Appends the given cargo packet. More...
 
void AgeCargo ()
 Ages the all cargo in this list.
 
void InvalidateCache ()
 Invalidates the cached data and rebuild it. More...
 
void SetTransferLoadPlace (TileIndex xy)
 Sets loaded_at_xy to the current station for all cargo to be transfered. More...
 
bool Stage (bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment)
 Stages cargo for unloading. More...
 
void KeepAll ()
 Marks all cargo in the vehicle as to be kept. More...
 
template<MoveToAction Tfrom, MoveToAction Tto>
uint Reassign (uint max_move, TileOrStationID update=INVALID_TILE)
 Moves some cargo from one designation to another. More...
 
uint Return (uint max_move, StationCargoList *dest, StationID next_station)
 Returns reserved cargo to the station and removes it from the cache. More...
 
uint Unload (uint max_move, StationCargoList *dest, CargoPayment *payment)
 Unloads cargo at the given station. More...
 
uint Shift (uint max_move, VehicleCargoList *dest)
 Shifts cargo between two vehicles. More...
 
uint Truncate (uint max_move=UINT_MAX)
 Truncates the cargo in this list to the given amount. More...
 
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. More...
 
- Public Member Functions inherited from CargoList< VehicleCargoList, CargoPacketList >
 CargoList ()
 Create the cargo list. More...
 
 ~CargoList ()
 Destroy the cargolist ("frees" all cargo packets).
 
void OnCleanPool ()
 Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket's CleanPool.
 
const CargoPacketList * Packets () const
 Returns a pointer to the cargo packet list (so you can iterate over it etc). More...
 
uint DaysInTransit () const
 Returns average number of days in transit for a cargo entity. More...
 
void InvalidateCache ()
 Invalidates the cached data and rebuilds it. More...
 

Static Public Member Functions

static bool AreMergable (const CargoPacket *cp1, const CargoPacket *cp2)
 Are two the two CargoPackets mergeable in the context of a list of CargoPackets for a Vehicle? More...
 

Protected Types

typedef CargoList< VehicleCargoList, CargoPacketList > Parent
 The (direct) parent of this class. More...
 

Protected Member Functions

template<class Taction >
void ShiftCargo (Taction action)
 Shifts cargo from the front of the packet list and applies some action to it. More...
 
template<class Taction >
void PopCargo (Taction action)
 Pops cargo from the back of the packet list and applies some action to it. More...
 
void AssertCountConsistency () const
 Assert that the designation counts add up.
 
void AddToCache (const CargoPacket *cp)
 Update the cache to reflect adding of this packet. More...
 
void RemoveFromCache (const CargoPacket *cp, uint count)
 Update the cached values to reflect the removal of this packet or part of it. More...
 
void AddToMeta (const CargoPacket *cp, MoveToAction action)
 Adds a packet to the metadata. More...
 
void RemoveFromMeta (const CargoPacket *cp, MoveToAction action, uint count)
 Removes a packet or part of it from the metadata. More...
 
- Protected Member Functions inherited from CargoList< VehicleCargoList, CargoPacketList >
void AddToCache (const CargoPacket *cp)
 Update the cache to reflect adding of this packet. More...
 
void RemoveFromCache (const CargoPacket *cp, uint count)
 Update the cached values to reflect the removal of this packet or part of it. More...
 

Static Protected Member Functions

static MoveToAction ChooseAction (const CargoPacket *cp, StationID cargo_next, StationID current_station, bool accepted, StationIDStack next_station)
 Choose action to be performed with the given cargo packet. More...
 
- Static Protected Member Functions inherited from CargoList< VehicleCargoList, CargoPacketList >
static bool TryMerge (CargoPacket *cp, CargoPacket *icp)
 Tries to merge the second packet into the first and return if that was successful. More...
 

Protected Attributes

Money feeder_share
 Cache for the feeder share.
 
uint action_counts [NUM_MOVE_TO_ACTION]
 Counts of cargo to be transfered, delivered, kept and loaded.
 
- Protected Attributes inherited from CargoList< VehicleCargoList, CargoPacketList >
uint count
 Cache for the number of cargo entities.
 
uint cargo_days_in_transit
 Cache for the sum of number of days in transit of each entity; comparable to man-hours.
 
CargoPacketList packets
 The cargo packets in this list.
 

Friends

class StationCargoList
 The station cargo list needs to control the unloading. More...
 
class CargoList< VehicleCargoList, CargoPacketList >
 The super class ought to know what it's doing. More...
 
class CargoShift
 
class CargoTransfer
 
class CargoDelivery
 
template<class Tsource >
class CargoRemoval
 
class CargoReturn
 
class VehicleCargoReroute
 
const struct SaveLoadGetVehicleDescription (VehicleType vt)
 The vehicles have a cargo list (and we want that saved). More...
 

Additional Inherited Members

- Public Types inherited from CargoList< VehicleCargoList, CargoPacketList >
enum  MoveToAction
 Kind of actions that could be done with packets on move. More...
 
typedef CargoPacketList ::iterator Iterator
 The iterator for our container. More...
 
typedef CargoPacketList ::reverse_iterator ReverseIterator
 The reverse iterator for our container. More...
 
typedef CargoPacketList ::const_iterator ConstIterator
 The const iterator for our container. More...
 
typedef CargoPacketList ::const_reverse_iterator ConstReverseIterator
 The const reverse iterator for our container. More...
 

Detailed Description

CargoList that is used for vehicles.

Definition at line 283 of file cargopacket.h.

Member Typedef Documentation

◆ Parent

typedef CargoList<VehicleCargoList, CargoPacketList> VehicleCargoList::Parent
protected

The (direct) parent of this class.

Definition at line 286 of file cargopacket.h.

Member Function Documentation

◆ ActionCount()

uint VehicleCargoList::ActionCount ( MoveToAction  action) const
inline

Returns the amount of cargo designated for a given purpose.

Parameters
actionAction the cargo is designated for.
Returns
Amount of cargo designated for the given action.

Definition at line 356 of file cargopacket.h.

Referenced by Vehicle::CancelReservation(), and StationCargoList::Load().

◆ AddToCache()

void VehicleCargoList::AddToCache ( const CargoPacket cp)
protected

Update the cache to reflect adding of this packet.

Increases count, feeder share and days_in_transit.

Parameters
cpNew packet to be inserted.

Definition at line 348 of file cargopacket.cpp.

References CargoPacket::feeder_share.

◆ AddToMeta()

void VehicleCargoList::AddToMeta ( const CargoPacket cp,
MoveToAction  action 
)
protected

Adds a packet to the metadata.

Parameters
cpPacket to be added.
actionMoveToAction of the packet.

Definition at line 374 of file cargopacket.cpp.

References CargoPacket::count.

◆ Append()

void VehicleCargoList::Append ( CargoPacket cp,
MoveToAction  action = MTA_KEEP 
)

Appends the given cargo packet.

Tries to merge it with another one in the packets list. If no fitting packet is found, appends it. You can only append packets to the ranges of packets designated for keeping or loading. Furthermore if there are already packets reserved for loading you cannot directly add packets to the "keep" list. You first have to load the reserved ones.

Warning
After appending this packet may not exist anymore!
Note
Do not use the cargo packet anymore after it has been appended to this CargoList!
Parameters
cpCargo packet to add.
actionEither MTA_KEEP if you want to add the packet directly or MTA_LOAD if you want to reserve it first.
Precondition
cp != NULL
action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)

Definition at line 254 of file cargopacket.cpp.

◆ AreMergable()

static bool VehicleCargoList::AreMergable ( const CargoPacket cp1,
const CargoPacket cp2 
)
inlinestatic

Are two the two CargoPackets mergeable in the context of a list of CargoPackets for a Vehicle?

Parameters
cp1First CargoPacket.
cp2Second CargoPacket.
Returns
True if they are mergeable.

Definition at line 447 of file cargopacket.h.

References CargoPacket::days_in_transit, CargoPacket::loaded_at_xy, CargoPacket::source_id, CargoPacket::source_type, and CargoPacket::source_xy.

◆ ChooseAction()

VehicleCargoList::MoveToAction VehicleCargoList::ChooseAction ( const CargoPacket cp,
StationID  cargo_next,
StationID  current_station,
bool  accepted,
StationIDStack  next_station 
)
staticprotected

Choose action to be performed with the given cargo packet.

Parameters
cpThe packet.
cargo_nextNext hop the cargo wants to pass.
current_stationCurrent station of the vehicle carrying the cargo.
acceptedIf the cargo is accepted at the current station.
next_stationNext station(s) the vehicle may stop at.
Returns
MoveToAction to be performed.

Definition at line 423 of file cargopacket.cpp.

◆ FeederShare()

Money VehicleCargoList::FeederShare ( ) const
inline

Returns total sum of the feeder share for all packets.

Returns
The before mentioned number.

Definition at line 346 of file cargopacket.h.

References CargoPacket::feeder_share.

◆ InvalidateCache()

void VehicleCargoList::InvalidateCache ( )

Invalidates the cached data and rebuild it.

Definition at line 542 of file cargopacket.cpp.

References CargoPacket::feeder_share.

◆ KeepAll()

void VehicleCargoList::KeepAll ( )
inline

Marks all cargo in the vehicle as to be kept.

This is mostly useful for loading old savegames. When loading is aborted the reserved cargo has to be returned first.

Definition at line 422 of file cargopacket.h.

References CargoPacket::count, INVALID_TILE, CargoMovement< VehicleCargoList, VehicleCargoList >::max_move, and SQConvert::Return().

Referenced by Vehicle::CancelReservation().

◆ PopCargo()

template<class Taction >
void VehicleCargoList::PopCargo ( Taction  action)
protected

Pops cargo from the back of the packet list and applies some action to it.

Template Parameters
TactionAction class or function to be used. It should define "bool operator()(CargoPacket *)". If true is returned the cargo packet will be removed from the list. Otherwise it will be kept and the loop will be aborted.
Parameters
actionAction instance to be applied.

Definition at line 311 of file cargopacket.cpp.

◆ Reassign()

template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
uint VehicleCargoList::Reassign ( uint  max_move,
TileOrStationID  update = INVALID_TILE 
)

Moves some cargo from one designation to another.

You can only move between adjacent designations. E.g. you can keep cargo that was previously reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be delivered. Furthermore, as this method doesn't change the actual packets, you cannot move cargo from or to MTA_TRANSFER. You need a specialized template method for that.

Template Parameters
fromPrevious designation of cargo.
toNew designation of cargo.
Parameters
max_moveMaximum amount of cargo to reassign.
Returns
Amount of cargo actually reassigned.

Definition at line 561 of file cargopacket.cpp.

Referenced by StationCargoList::Load().

◆ RemainingCount()

uint VehicleCargoList::RemainingCount ( ) const
inline

Returns the sum of cargo to be kept in the vehicle at the current station.

Returns
Cargo to be kept or loaded.

Definition at line 402 of file cargopacket.h.

Referenced by FinalizeRefitAction::operator()().

◆ RemoveFromCache()

void VehicleCargoList::RemoveFromCache ( const CargoPacket cp,
uint  count 
)
protected

Update the cached values to reflect the removal of this packet or part of it.

Decreases count, feeder share and days_in_transit.

Parameters
cpPacket to be removed from cache.
countAmount of cargo from the given packet to be removed.

Definition at line 337 of file cargopacket.cpp.

References CargoPacket::feeder_share, and CargoPacket::FeederShare().

◆ RemoveFromMeta()

void VehicleCargoList::RemoveFromMeta ( const CargoPacket cp,
MoveToAction  action,
uint  count 
)
protected

Removes a packet or part of it from the metadata.

Parameters
cpPacket to be removed.
actionMoveToAction of the packet (for updating the counts).
countAmount of cargo to be removed.

Definition at line 360 of file cargopacket.cpp.

◆ Reroute()

uint VehicleCargoList::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.

Parameters
max_moveMaximum amount of cargo to move.
destList to prepend the cargo to.
avoidStation to exclude from routing and current next hop of packets to reroute.
avoid2Additional station to exclude from routing. ge GoodsEntry to get the routing info from.

Definition at line 673 of file cargopacket.cpp.

References min().

Referenced by RerouteCargo().

◆ ReservedCount()

uint VehicleCargoList::ReservedCount ( ) const
inline

Returns sum of reserved cargo.

Returns
Sum of reserved cargo.

Definition at line 384 of file cargopacket.h.

Referenced by PrepareRefitAction::operator()().

◆ Return()

uint VehicleCargoList::Return ( uint  max_move,
StationCargoList dest,
StationID  next 
)

Returns reserved cargo to the station and removes it from the cache.

Parameters
max_moveMaximum amount of cargo to move.
destStation the cargo is returned to.
IDof next the station the cargo wants to go next.
Returns
Amount of cargo actually returned.

Definition at line 608 of file cargopacket.cpp.

References min().

Referenced by Vehicle::CancelReservation(), and ReturnCargoAction::operator()().

◆ SetTransferLoadPlace()

void VehicleCargoList::SetTransferLoadPlace ( TileIndex  xy)

Sets loaded_at_xy to the current station for all cargo to be transfered.

This is done when stopping or skipping while the vehicle is unloading. In that case the vehicle will get part of its transfer credits early and it may get more transfer credits than it's entitled to.

Parameters
xyNew loaded_at_xy for the cargo.

Definition at line 404 of file cargopacket.cpp.

References CargoPacket::count, CargoPacket::loaded_at_xy, and BaseStation::xy.

Referenced by Vehicle::CancelReservation().

◆ Shift()

uint VehicleCargoList::Shift ( uint  max_move,
VehicleCargoList dest 
)

Shifts cargo between two vehicles.

Parameters
destOther vehicle's cargo list.
max_moveMaximum amount of cargo to be moved.
Returns
Amount of cargo actually moved.

Definition at line 621 of file cargopacket.cpp.

References CargoPacket::count, and min().

◆ ShiftCargo()

template<class Taction >
void VehicleCargoList::ShiftCargo ( Taction  action)
protected

Shifts cargo from the front of the packet list and applies some action to it.

Template Parameters
TactionAction class or function to be used. It should define "bool operator()(CargoPacket *)". If true is returned the cargo packet will be removed from the list. Otherwise it will be kept and the loop will be aborted.
Parameters
actionAction instance to be applied.

Definition at line 289 of file cargopacket.cpp.

◆ Source()

StationID VehicleCargoList::Source ( ) const
inline

Returns source of the first cargo packet in this list.

Returns
The before mentioned source.

Definition at line 337 of file cargopacket.h.

◆ Stage()

bool VehicleCargoList::Stage ( bool  accepted,
StationID  current_station,
StationIDStack  next_station,
uint8  order_flags,
const GoodsEntry ge,
CargoPayment payment 
)

Stages cargo for unloading.

The cargo is sorted so that packets to be transferred, delivered or kept are in consecutive chunks in the list. At the same time the designation_counts are updated to reflect the size of those chunks.

Parameters
acceptedIf the cargo will be accepted at the station.
current_stationID of the station.
next_stationID of the station the vehicle will go to next.
order_flagsOrderUnloadFlags that will apply to the unload operation.
geGoodsEntry for getting the flows.
paymentPayment object for registering transfers. return If any cargo will be unloaded.

Definition at line 450 of file cargopacket.cpp.

◆ StoredCount()

uint VehicleCargoList::StoredCount ( ) const
inline

Returns sum of cargo on board the vehicle (ie not only reserved).

Returns
Cargo on board the vehicle.

Definition at line 366 of file cargopacket.h.

Referenced by Vehicle::GetConsistFreeCapacities(), RoadVehicle::GetWeight(), Train::GetWeight(), IncreaseStats(), and IsEmptyAction::operator()().

◆ TotalCount()

uint VehicleCargoList::TotalCount ( ) const
inline

Returns sum of cargo, including reserved cargo.

Returns
Sum of cargo.

Definition at line 375 of file cargopacket.h.

References CargoPacket::count.

◆ Truncate()

uint VehicleCargoList::Truncate ( uint  max_move = UINT_MAX)

Truncates the cargo in this list to the given amount.

It leaves the first cargo entities and removes max_move from the back of the list.

Parameters
max_moveMaximum amount of entities to be removed from the list.
Returns
Amount of entities actually moved.

Definition at line 658 of file cargopacket.cpp.

References CargoPacket::count, and min().

Referenced by CrashAirplane().

◆ Unload()

uint VehicleCargoList::Unload ( uint  max_move,
StationCargoList dest,
CargoPayment payment 
)

Unloads cargo at the given station.

Deliver or transfer, depending on the ranges defined by designation_counts.

Parameters
destStationCargoList to add transferred cargo to.
max_moveMaximum amount of cargo to move.
paymentPayment object to register payments in.
Returns
Amount of cargo actually unloaded.

Definition at line 636 of file cargopacket.cpp.

References min().

◆ UnloadCount()

uint VehicleCargoList::UnloadCount ( ) const
inline

Returns sum of cargo to be moved out of the vehicle at the current station.

Returns
Cargo to be moved.

Definition at line 393 of file cargopacket.h.

Friends And Related Function Documentation

◆ CargoList< VehicleCargoList, CargoPacketList >

friend class CargoList< VehicleCargoList, CargoPacketList >
friend

The super class ought to know what it's doing.

Definition at line 321 of file cargopacket.h.

◆ GetVehicleDescription

const struct SaveLoad* GetVehicleDescription ( VehicleType  vt)
friend

The vehicles have a cargo list (and we want that saved).

Parameters
vtthe vehicle type. Can be VEH_END for the common vehicle description data
Returns
the saveload description

Save and load of vehicles

Definition at line 574 of file vehicle_sl.cpp.

◆ StationCargoList

friend class StationCargoList
friend

The station cargo list needs to control the unloading.

Definition at line 319 of file cargopacket.h.


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