OpenTTD Source 20241224-master-gee860a5c8e
LinkGraph Class Reference

A connected component of a link graph. More...

#include <linkgraph.h>

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

Data Structures

struct  BaseEdge
 An edge in the link graph. More...
 
struct  BaseNode
 Node of the link graph. More...
 

Public Types

typedef std::vector< BaseNodeNodeVector
 
- Public Types inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_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.
 

Public Member Functions

 LinkGraph ()
 Bare constructor, only for save/load.
 
 LinkGraph (CargoID cargo)
 Real constructor.
 
void Init (uint size)
 Resize the component and fill it with empty nodes and edges.
 
void ShiftDates (TimerGameEconomy::Date interval)
 Shift all dates by given interval.
 
void Compress ()
 
void Merge (LinkGraph *other)
 Merge a link graph with another one.
 
BaseNodeoperator[] (NodeID num)
 Get a node with the specified id.
 
const BaseNodeoperator[] (NodeID num) const
 Get a const reference to a node with the specified id.
 
NodeID Size () const
 Get the current size of the component.
 
TimerGameEconomy::Date LastCompression () const
 Get date of last compression.
 
CargoID Cargo () const
 Get the cargo ID this component's link graph refers to.
 
uint Monthly (uint base) const
 Scale a value to its monthly equivalent, based on last compression.
 
NodeID AddNode (const Station *st)
 Add a node to the component and create empty edges associated with it.
 
void RemoveNode (NodeID id)
 Remove a node from the link graph by overwriting it with the last node.
 
- Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_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 uint Scale (uint val, TimerGameEconomy::Date target_age, TimerGameEconomy::Date orig_age)
 Scale a value from a link graph of age orig_age for usage in one of age target_age.
 
- Static Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_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 uint MIN_TIMEOUT_DISTANCE = 32
 Minimum effective distance for timeout calculation.
 
static constexpr TimerGameEconomy::Date STALE_LINK_DEPOT_TIMEOUT = 1024
 Number of days before deleting links served only by vehicles stopped in depot.
 
static constexpr TimerGameEconomy::Date COMPRESSION_INTERVAL = 256
 Minimum number of days between subsequent compressions of a LG.
 

Protected Attributes

CargoID cargo
 Cargo of this component's link graph.
 
TimerGameEconomy::Date last_compression
 Last time the capacities and supplies were compressed.
 
NodeVector nodes
 Nodes in the component.
 

Friends

class SlLinkgraphNode
 
class SlLinkgraphEdge
 
class LinkGraphJob
 
SaveLoadTable GetLinkGraphDesc ()
 Get a SaveLoad array for a link graph.
 
SaveLoadTable GetLinkGraphJobDesc ()
 Get a SaveLoad array for a link graph job.
 

Additional Inherited Members

- Data Fields inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_pool >
Tindex index
 Index of this pool item.
 

Detailed Description

A connected component of a link graph.

Contains a complete set of stations connected by links as nodes and edges. Each component also holds a copy of the link graph settings at the time of its creation. The global settings might change between the creation and join time so we can't rely on them.

Definition at line 37 of file linkgraph.h.

Member Typedef Documentation

◆ NodeVector

typedef std::vector<BaseNode> LinkGraph::NodeVector

Definition at line 167 of file linkgraph.h.

Constructor & Destructor Documentation

◆ LinkGraph() [1/2]

LinkGraph::LinkGraph ( )
inline

Bare constructor, only for save/load.

Definition at line 192 of file linkgraph.h.

◆ LinkGraph() [2/2]

LinkGraph::LinkGraph ( CargoID  cargo)
inline

Real constructor.

Parameters
cargoCargo the link graph is about.

Definition at line 197 of file linkgraph.h.

Member Function Documentation

◆ AddNode()

NodeID LinkGraph::AddNode ( const Station st)

Add a node to the component and create empty edges associated with it.

Set the station's last_component to this component. Calculate the distances to all other nodes. The distances to all nodes are important as the demand calculator relies on their availability.

Parameters
stNew node's station.
Returns
New node's ID.

Definition at line 149 of file linkgraph.cpp.

References cargo, GoodsEntry::GES_ACCEPTANCE, Station::goods, HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, nodes, Size(), GoodsEntry::status, and BaseStation::xy.

Referenced by IncreaseStats(), and Merge().

◆ Cargo()

CargoID LinkGraph::Cargo ( ) const
inline

Get the cargo ID this component's link graph refers to.

Returns
Cargo ID.

Definition at line 242 of file linkgraph.h.

References cargo.

Referenced by LinkGraphJob::Cargo().

◆ Compress()

void LinkGraph::Compress ( )

Definition at line 66 of file linkgraph.cpp.

◆ Init()

void LinkGraph::Init ( uint  size)

Resize the component and fill it with empty nodes and edges.

Used when loading from save games. The component is expected to be empty before.

Parameters
sizeNew size of the component.

Definition at line 251 of file linkgraph.cpp.

References nodes, and Size().

◆ LastCompression()

TimerGameEconomy::Date LinkGraph::LastCompression ( ) const
inline

Get date of last compression.

Returns
Date of last compression.

Definition at line 236 of file linkgraph.h.

References last_compression.

Referenced by DeleteStaleLinks(), and LinkGraphJob::LastCompression().

◆ Merge()

◆ Monthly()

uint LinkGraph::Monthly ( uint  base) const
inline

Scale a value to its monthly equivalent, based on last compression.

Parameters
baseValue to be scaled.
Returns
Scaled value.

Definition at line 249 of file linkgraph.h.

References TimerGameEconomy::date, and last_compression.

Referenced by LinkGraphOverlay::AddLinks(), and LinkGraphOverlay::RebuildCache().

◆ operator[]() [1/2]

BaseNode & LinkGraph::operator[] ( NodeID  num)
inline

Get a node with the specified id.

Parameters
numID of the node.
Returns
the Requested node.

Definition at line 217 of file linkgraph.h.

References nodes.

◆ operator[]() [2/2]

const BaseNode & LinkGraph::operator[] ( NodeID  num) const
inline

Get a const reference to a node with the specified id.

Parameters
numID of the node.
Returns
the Requested node.

Definition at line 224 of file linkgraph.h.

References nodes.

◆ RemoveNode()

void LinkGraph::RemoveNode ( NodeID  id)

Remove a node from the link graph by overwriting it with the last node.

Parameters
idID of the node to be removed.

Definition at line 116 of file linkgraph.cpp.

References cargo, SpecializedStation< Station, false >::Get(), Station::goods, GoodsEntry::node, nodes, and Size().

Referenced by Station::~Station().

◆ Scale()

static uint LinkGraph::Scale ( uint  val,
TimerGameEconomy::Date  target_age,
TimerGameEconomy::Date  orig_age 
)
inlinestatic

Scale a value from a link graph of age orig_age for usage in one of age target_age.

Make sure that the value stays > 0 if it was > 0 before.

Parameters
valValue to be scaled.
target_ageAge of the target link graph.
orig_ageAge of the original link graph.
Returns
scaled value.

Definition at line 186 of file linkgraph.h.

Referenced by Merge().

◆ ShiftDates()

void LinkGraph::ShiftDates ( TimerGameEconomy::Date  interval)

Shift all dates by given interval.

This is useful if the date has been modified with the cheat menu.

Parameters
intervalNumber of days to be added or subtracted.

Definition at line 53 of file linkgraph.cpp.

References TimerGameConst< struct Economy >::INVALID_DATE, last_compression, LinkGraph::BaseNode::last_update, nodes, and Size().

◆ Size()

NodeID LinkGraph::Size ( ) const
inline

Get the current size of the component.

Returns
Size.

Definition at line 230 of file linkgraph.h.

References nodes.

Referenced by AddNode(), AfterLoadLinkGraphs(), IncreaseStats(), Init(), Merge(), RemoveNode(), ShiftDates(), LinkGraphJob::Size(), LinkGraphSchedule::SpawnNext(), and Station::~Station().

Friends And Related Symbol Documentation

◆ GetLinkGraphDesc

SaveLoadTable GetLinkGraphDesc ( )
friend

Get a SaveLoad array for a link graph.

Returns
SaveLoad array for link graph.

Definition at line 136 of file linkgraph_sl.cpp.

◆ GetLinkGraphJobDesc

SaveLoadTable GetLinkGraphJobDesc ( )
friend

Get a SaveLoad array for a link graph job.

The settings struct is derived from the global settings saveload array. The exact entries are calculated when the function is called the first time. It's necessary to keep a copy of the settings for each link graph job so that you can change the settings while in-game and still not mess with current link graph runs. Of course the settings have to be saved and loaded, too, to avoid desyncs.

Returns
Array of SaveLoad structs.

Definition at line 180 of file linkgraph_sl.cpp.

◆ LinkGraphJob

friend class LinkGraphJob
friend

Definition at line 262 of file linkgraph.h.

◆ SlLinkgraphEdge

friend class SlLinkgraphEdge
friend

Definition at line 261 of file linkgraph.h.

◆ SlLinkgraphNode

friend class SlLinkgraphNode
friend

Definition at line 260 of file linkgraph.h.

Field Documentation

◆ cargo

CargoID LinkGraph::cargo
protected

Cargo of this component's link graph.

Definition at line 264 of file linkgraph.h.

Referenced by AddNode(), Cargo(), Merge(), and RemoveNode().

◆ COMPRESSION_INTERVAL

constexpr TimerGameEconomy::Date LinkGraph::COMPRESSION_INTERVAL = 256
staticconstexpr

Minimum number of days between subsequent compressions of a LG.

Definition at line 176 of file linkgraph.h.

Referenced by DeleteStaleLinks().

◆ last_compression

TimerGameEconomy::Date LinkGraph::last_compression
protected

Last time the capacities and supplies were compressed.

Definition at line 265 of file linkgraph.h.

Referenced by LastCompression(), Merge(), Monthly(), and ShiftDates().

◆ MIN_TIMEOUT_DISTANCE

const uint LinkGraph::MIN_TIMEOUT_DISTANCE = 32
static

Minimum effective distance for timeout calculation.

Definition at line 170 of file linkgraph.h.

Referenced by DeleteStaleLinks().

◆ nodes

NodeVector LinkGraph::nodes
protected

Nodes in the component.

Definition at line 266 of file linkgraph.h.

Referenced by AddNode(), LinkGraphJob::Init(), Init(), Merge(), operator[](), operator[](), RemoveNode(), ShiftDates(), and Size().

◆ STALE_LINK_DEPOT_TIMEOUT

constexpr TimerGameEconomy::Date LinkGraph::STALE_LINK_DEPOT_TIMEOUT = 1024
staticconstexpr

Number of days before deleting links served only by vehicles stopped in depot.

Definition at line 173 of file linkgraph.h.

Referenced by DeleteStaleLinks().


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