11#include "../core/pool_func.hpp"
14#include "../safeguards.h"
30 this->demand = demand;
56 for (NodeID node1 = 0; node1 < this->
Size(); ++node1) {
66void LinkGraph::Compress()
69 for (NodeID node1 = 0; node1 < this->
Size(); ++node1) {
70 this->
nodes[node1].supply /= 2;
71 for (BaseEdge &edge : this->
nodes[node1].edges) {
72 if (edge.capacity > 0) {
73 uint new_capacity = std::max(1U, edge.capacity / 2);
74 if (edge.capacity < (1 << 16)) {
75 edge.travel_time_sum = edge.travel_time_sum * new_capacity / edge.capacity;
76 }
else if (edge.travel_time_sum != 0) {
77 edge.travel_time_sum = std::max<uint64_t>(1, edge.travel_time_sum / 2);
79 edge.capacity = new_capacity;
94 NodeID first = this->
Size();
95 for (NodeID node1 = 0; node1 < other->
Size(); ++node1) {
97 NodeID new_node = this->
AddNode(st);
103 BaseEdge &new_edge = this->
nodes[new_node].edges.emplace_back(first + e.dest_node);
118 assert(id < this->
Size());
120 NodeID last_node = this->
Size() - 1;
125 this->
nodes.pop_back();
126 for (
auto &n : this->
nodes) {
128 auto [first, last] = std::equal_range(n.edges.begin(), n.edges.end(),
id);
130 auto insert = n.edges.erase(first, last);
132 if (!n.edges.empty() && n.edges.back().dest_node == last_node) {
134 n.edges.back().dest_node = id;
135 n.edges.insert(insert, n.edges.back());
153 NodeID new_node = this->
Size();
169 assert(!this->HasEdgeTo(to));
171 BaseEdge &edge = *this->edges.emplace(std::upper_bound(this->edges.begin(), this->edges.end(), to), to);
188 assert(capacity > 0);
189 assert(usage <= capacity);
190 if (!this->HasEdgeTo(to)) {
191 this->AddEdge(to, capacity, usage, travel_time, mode);
193 this->GetEdge(to)->Update(capacity, usage, travel_time, mode);
203 auto [first, last] = std::equal_range(this->edges.begin(), this->edges.end(), to);
204 this->edges.erase(first, last);
219 assert(this->capacity > 0);
220 assert(capacity >= usage);
223 if (this->travel_time_sum == 0) {
224 this->travel_time_sum =
static_cast<uint64_t
>(this->capacity + capacity) * travel_time;
225 }
else if (travel_time == 0) {
226 this->travel_time_sum += this->travel_time_sum / this->capacity * capacity;
228 this->travel_time_sum +=
static_cast<uint64_t
>(travel_time) * capacity;
230 this->capacity += capacity;
231 this->usage += usage;
233 if (this->travel_time_sum == 0) {
234 this->capacity = std::max(this->capacity, capacity);
235 this->travel_time_sum =
static_cast<uint64_t
>(travel_time) * this->capacity;
236 }
else if (capacity > this->capacity) {
237 this->travel_time_sum = this->travel_time_sum / this->capacity * capacity;
238 this->capacity = capacity;
240 this->usage = std::max(this->usage, usage);
253 assert(this->
Size() == 0);
254 this->
nodes.resize(size);
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
A connected component of a link graph.
void Merge(LinkGraph *other)
Merge a link graph with another one.
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
CargoID cargo
Cargo of this component's link graph.
NodeVector nodes
Nodes in the component.
void ShiftDates(TimerGameEconomy::Date interval)
Shift all dates by given interval.
NodeID AddNode(const Station *st)
Add a node to the component and create empty edges associated with it.
NodeID Size() const
Get the current size of the component.
void RemoveNode(NodeID id)
Remove a node from the link graph by overwriting it with the last node.
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.
TimerGameEconomy::Date last_compression
Last time the capacities and supplies were compressed.
static constexpr TimerGame< struct Economy >::Date INVALID_DATE
Representation of an invalid date.
static Date date
Current date in days (day counter).
Declaration of link graph classes used for cargo distribution.
EdgeUpdateMode
Special modes for updating links.
@ EUM_REFRESH
Refresh capacity.
@ EUM_INCREASE
Increase capacity.
@ EUM_RESTRICTED
Use restricted link.
@ EUM_UNRESTRICTED
Use unrestricted link.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
TileIndex xy
Base tile of the station.
Stores station stats for a single cargo.
@ GES_ACCEPTANCE
Set when the station accepts the cargo currently for final deliveries.
NodeID node
ID of node in link graph referring to this goods entry.
LinkGraphID link_graph
Link graph this station belongs to.
uint8_t status
Status of this cargo, see GoodsEntryStatus.
An edge in the link graph.
TimerGameEconomy::Date last_restricted_update
When the restricted part of the link was last updated.
uint64_t travel_time_sum
Sum of the travel times of the link, in ticks.
NodeID dest_node
Destination of the edge.
uint usage
Usage of the link.
BaseEdge(NodeID dest_node=INVALID_NODE)
Create an edge.
TimerGameEconomy::Date last_unrestricted_update
When the unrestricted part of the link was last updated.
void Update(uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode)
Update an edge.
uint capacity
Capacity of the link.
TimerGameEconomy::Date last_update
When the supply was last updated.
void AddEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode)
Fill an edge with values from a link.
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode)
Creates an edge if none exists yet or updates an existing edge.
void RemoveEdge(NodeID to)
Remove an outgoing edge from this node.
Tindex index
Index of this pool item.
Base class for all pools.
static Station * Get(size_t index)
Gets station with given index.
GoodsEntry goods[NUM_CARGO]
Goods at this station.