Update to 14.0-beta1
This commit is contained in:
@@ -36,7 +36,7 @@ public:
|
||||
*/
|
||||
inline void AddNode(const Node &node)
|
||||
{
|
||||
this->supply_sum += node.Supply();
|
||||
this->supply_sum += node.base.supply;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
*/
|
||||
inline uint EffectiveSupply(const Node &from, const Node &to)
|
||||
{
|
||||
return std::max(from.Supply() * std::max(1U, to.Supply()) * this->mod_size / 100 / this->demand_per_node, 1U);
|
||||
return std::max(from.base.supply * std::max(1U, to.base.supply) * this->mod_size / 100 / this->demand_per_node, 1U);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +69,7 @@ public:
|
||||
*/
|
||||
inline bool HasDemandLeft(const Node &to)
|
||||
{
|
||||
return (to.Supply() == 0 || to.UndeliveredSupply() > 0) && to.Demand() > 0;
|
||||
return (to.base.supply == 0 || to.undelivered_supply > 0) && to.base.demand > 0;
|
||||
}
|
||||
|
||||
void SetDemands(LinkGraphJob &job, NodeID from, NodeID to, uint demand_forw);
|
||||
@@ -108,7 +108,7 @@ public:
|
||||
*/
|
||||
inline uint EffectiveSupply(const Node &from, const Node &)
|
||||
{
|
||||
return from.Supply();
|
||||
return from.base.supply;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +116,7 @@ public:
|
||||
* nodes always accept as long as their demand > 0.
|
||||
* @param to The node to be checked.
|
||||
*/
|
||||
inline bool HasDemandLeft(const Node &to) { return to.Demand() > 0; }
|
||||
inline bool HasDemandLeft(const Node &to) { return to.base.demand > 0; }
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -129,9 +129,9 @@ public:
|
||||
*/
|
||||
void SymmetricScaler::SetDemands(LinkGraphJob &job, NodeID from_id, NodeID to_id, uint demand_forw)
|
||||
{
|
||||
if (job[from_id].Demand() > 0) {
|
||||
if (job[from_id].base.demand > 0) {
|
||||
uint demand_back = demand_forw * this->mod_size / 100;
|
||||
uint undelivered = job[to_id].UndeliveredSupply();
|
||||
uint undelivered = job[to_id].undelivered_supply;
|
||||
if (demand_back > undelivered) {
|
||||
demand_back = undelivered;
|
||||
demand_forw = std::max(1U, demand_back * 100 / this->mod_size);
|
||||
@@ -170,11 +170,11 @@ void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
|
||||
|
||||
for (NodeID node = 0; node < job.Size(); node++) {
|
||||
scaler.AddNode(job[node]);
|
||||
if (job[node].Supply() > 0) {
|
||||
if (job[node].base.supply > 0) {
|
||||
supplies.push(node);
|
||||
num_supplies++;
|
||||
}
|
||||
if (job[node].Demand() > 0) {
|
||||
if (job[node].base.demand > 0) {
|
||||
demands.push(node);
|
||||
num_demands++;
|
||||
}
|
||||
@@ -204,16 +204,16 @@ void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
|
||||
continue;
|
||||
}
|
||||
|
||||
int32 supply = scaler.EffectiveSupply(job[from_id], job[to_id]);
|
||||
int32_t supply = scaler.EffectiveSupply(job[from_id], job[to_id]);
|
||||
assert(supply > 0);
|
||||
|
||||
/* Scale the distance by mod_dist around max_distance */
|
||||
int32 distance = this->max_distance - (this->max_distance -
|
||||
(int32)DistanceMaxPlusManhattan(job[from_id].XY(), job[to_id].XY())) *
|
||||
int32_t distance = this->max_distance - (this->max_distance -
|
||||
(int32_t)DistanceMaxPlusManhattan(job[from_id].base.xy, job[to_id].base.xy)) *
|
||||
this->mod_dist / 100;
|
||||
|
||||
/* Scale the accuracy by distance around accuracy / 2 */
|
||||
int32 divisor = this->accuracy * (this->mod_dist - 50) / 100 +
|
||||
int32_t divisor = this->accuracy * (this->mod_dist - 50) / 100 +
|
||||
this->accuracy * distance / this->max_distance + 1;
|
||||
|
||||
assert(divisor > 0);
|
||||
@@ -230,7 +230,7 @@ void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
|
||||
demand_forw = 1;
|
||||
}
|
||||
|
||||
demand_forw = std::min(demand_forw, job[from_id].UndeliveredSupply());
|
||||
demand_forw = std::min(demand_forw, job[from_id].undelivered_supply);
|
||||
|
||||
scaler.SetDemands(job, from_id, to_id, demand_forw);
|
||||
|
||||
@@ -240,10 +240,10 @@ void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
|
||||
num_demands--;
|
||||
}
|
||||
|
||||
if (job[from_id].UndeliveredSupply() == 0) break;
|
||||
if (job[from_id].undelivered_supply == 0) break;
|
||||
}
|
||||
|
||||
if (job[from_id].UndeliveredSupply() != 0) {
|
||||
if (job[from_id].undelivered_supply != 0) {
|
||||
supplies.push(from_id);
|
||||
} else {
|
||||
num_supplies--;
|
||||
@@ -256,7 +256,7 @@ void DemandCalculator::CalcDemand(LinkGraphJob &job, Tscaler scaler)
|
||||
* @param job Job to calculate the demands for.
|
||||
*/
|
||||
DemandCalculator::DemandCalculator(LinkGraphJob &job) :
|
||||
max_distance(DistanceMaxPlusManhattan(TileXY(0,0), TileXY(MapMaxX(), MapMaxY())))
|
||||
max_distance(DistanceMaxPlusManhattan(TileXY(0,0), TileXY(Map::MaxX(), Map::MaxY())))
|
||||
{
|
||||
const LinkGraphSettings &settings = job.Settings();
|
||||
CargoID cargo = job.Cargo();
|
||||
|
||||
@@ -14,9 +14,9 @@ public:
|
||||
DemandCalculator(LinkGraphJob &job);
|
||||
|
||||
private:
|
||||
int32 max_distance; ///< Maximum distance possible on the map.
|
||||
int32 mod_dist; ///< Distance modifier, determines how much demands decrease with distance.
|
||||
int32 accuracy; ///< Accuracy of the calculation.
|
||||
int32_t max_distance; ///< Maximum distance possible on the map.
|
||||
int32_t mod_dist; ///< Distance modifier, determines how much demands decrease with distance.
|
||||
int32_t accuracy; ///< Accuracy of the calculation.
|
||||
|
||||
template<class Tscaler>
|
||||
void CalcDemand(LinkGraphJob &job, Tscaler scaler);
|
||||
@@ -32,12 +32,12 @@ public:
|
||||
* Call the demand calculator on the given component.
|
||||
* @param job Component to calculate the demands for.
|
||||
*/
|
||||
virtual void Run(LinkGraphJob &job) const { DemandCalculator c(job); }
|
||||
void Run(LinkGraphJob &job) const override { DemandCalculator c(job); }
|
||||
|
||||
/**
|
||||
* Virtual destructor has to be defined because of virtual Run().
|
||||
*/
|
||||
virtual ~DemandHandler() {}
|
||||
virtual ~DemandHandler() = default;
|
||||
};
|
||||
|
||||
#endif /* DEMANDS_H */
|
||||
|
||||
@@ -19,49 +19,44 @@
|
||||
void FlowMapper::Run(LinkGraphJob &job) const
|
||||
{
|
||||
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
||||
Node prev_node = job[node_id];
|
||||
StationID prev = prev_node.Station();
|
||||
PathList &paths = prev_node.Paths();
|
||||
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
|
||||
Path *path = *i;
|
||||
Node &prev_node = job[node_id];
|
||||
StationID prev = prev_node.base.station;
|
||||
for (const Path *path : prev_node.paths) {
|
||||
uint flow = path->GetFlow();
|
||||
if (flow == 0) break;
|
||||
Node node = job[path->GetNode()];
|
||||
StationID via = node.Station();
|
||||
StationID origin = job[path->GetOrigin()].Station();
|
||||
Node &node = job[path->GetNode()];
|
||||
StationID via = node.base.station;
|
||||
StationID origin = job[path->GetOrigin()].base.station;
|
||||
assert(prev != via && via != origin);
|
||||
/* Mark all of the flow for local consumption at "first". */
|
||||
node.Flows().AddFlow(origin, via, flow);
|
||||
node.flows.AddFlow(origin, via, flow);
|
||||
if (prev != origin) {
|
||||
/* Pass some of the flow marked for local consumption at "prev" on
|
||||
* to this node. */
|
||||
prev_node.Flows().PassOnFlow(origin, via, flow);
|
||||
prev_node.flows.PassOnFlow(origin, via, flow);
|
||||
} else {
|
||||
/* Prev node is origin. Simply add flow. */
|
||||
prev_node.Flows().AddFlow(origin, via, flow);
|
||||
prev_node.flows.AddFlow(origin, via, flow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
|
||||
/* Remove local consumption shares marked as invalid. */
|
||||
Node node = job[node_id];
|
||||
FlowStatMap &flows = node.Flows();
|
||||
flows.FinalizeLocalConsumption(node.Station());
|
||||
Node &node = job[node_id];
|
||||
FlowStatMap &flows = node.flows;
|
||||
flows.FinalizeLocalConsumption(node.base.station);
|
||||
if (this->scale) {
|
||||
/* Scale by time the graph has been running without being compressed. Add 1 to avoid
|
||||
* division by 0 if spawn date == last compression date. This matches
|
||||
* LinkGraph::Monthly(). */
|
||||
uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1;
|
||||
for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
|
||||
i->second.ScaleToMonthly(runtime);
|
||||
auto runtime = job.JoinDate() - job.Settings().recalc_time / CalendarTime::SECONDS_PER_DAY - job.LastCompression() + 1;
|
||||
for (auto &it : flows) {
|
||||
it.second.ScaleToMonthly(runtime.base());
|
||||
}
|
||||
}
|
||||
/* Clear paths. */
|
||||
PathList &paths = node.Paths();
|
||||
for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
|
||||
delete *i;
|
||||
}
|
||||
paths.clear();
|
||||
for (Path *i : node.paths) delete i;
|
||||
node.paths.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,8 @@ public:
|
||||
* values. Only do that on the very last flow mapping.
|
||||
*/
|
||||
FlowMapper(bool scale) : scale(scale) {}
|
||||
virtual void Run(LinkGraphJob &job) const;
|
||||
void Run(LinkGraphJob &job) const override;
|
||||
|
||||
/**
|
||||
* Virtual destructor has to be defined because of virtual Run().
|
||||
*/
|
||||
virtual ~FlowMapper() {}
|
||||
private:
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,12 +16,7 @@ public:
|
||||
* Initialize the link graph job.
|
||||
* @param job Job to be initialized.
|
||||
*/
|
||||
virtual void Run(LinkGraphJob &job) const { job.Init(); }
|
||||
|
||||
/**
|
||||
* Virtual destructor has to be defined because of virtual Run().
|
||||
*/
|
||||
virtual ~InitHandler() {}
|
||||
void Run(LinkGraphJob &job) const override { job.Init(); }
|
||||
};
|
||||
|
||||
#endif /* INIT_H */
|
||||
|
||||
@@ -23,26 +23,26 @@ INSTANTIATE_POOL_METHODS(LinkGraph)
|
||||
* @param st ID of the associated station.
|
||||
* @param demand Demand for cargo at the station.
|
||||
*/
|
||||
inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
|
||||
LinkGraph::BaseNode::BaseNode(TileIndex xy, StationID st, uint demand)
|
||||
{
|
||||
this->xy = xy;
|
||||
this->supply = 0;
|
||||
this->demand = demand;
|
||||
this->station = st;
|
||||
this->last_update = INVALID_DATE;
|
||||
this->last_update = EconomyTime::INVALID_DATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an edge.
|
||||
*/
|
||||
inline void LinkGraph::BaseEdge::Init()
|
||||
LinkGraph::BaseEdge::BaseEdge(NodeID dest_node)
|
||||
{
|
||||
this->capacity = 0;
|
||||
this->usage = 0;
|
||||
this->travel_time_sum = 0;
|
||||
this->last_unrestricted_update = INVALID_DATE;
|
||||
this->last_restricted_update = INVALID_DATE;
|
||||
this->next_edge = INVALID_NODE;
|
||||
this->last_unrestricted_update = EconomyTime::INVALID_DATE;
|
||||
this->last_restricted_update = EconomyTime::INVALID_DATE;
|
||||
this->dest_node = dest_node;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,33 +50,31 @@ inline void LinkGraph::BaseEdge::Init()
|
||||
* This is useful if the date has been modified with the cheat menu.
|
||||
* @param interval Number of days to be added or subtracted.
|
||||
*/
|
||||
void LinkGraph::ShiftDates(int interval)
|
||||
void LinkGraph::ShiftDates(TimerGameEconomy::Date interval)
|
||||
{
|
||||
this->last_compression += interval;
|
||||
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
|
||||
BaseNode &source = this->nodes[node1];
|
||||
if (source.last_update != INVALID_DATE) source.last_update += interval;
|
||||
for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
|
||||
BaseEdge &edge = this->edges[node1][node2];
|
||||
if (edge.last_unrestricted_update != INVALID_DATE) edge.last_unrestricted_update += interval;
|
||||
if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
|
||||
if (source.last_update != EconomyTime::INVALID_DATE) source.last_update += interval;
|
||||
for (BaseEdge &edge : this->nodes[node1].edges) {
|
||||
if (edge.last_unrestricted_update != EconomyTime::INVALID_DATE) edge.last_unrestricted_update += interval;
|
||||
if (edge.last_restricted_update != EconomyTime::INVALID_DATE) edge.last_restricted_update += interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LinkGraph::Compress()
|
||||
{
|
||||
this->last_compression = (_date + this->last_compression) / 2;
|
||||
this->last_compression = (TimerGameEconomy::date + this->last_compression).base() / 2;
|
||||
for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
|
||||
this->nodes[node1].supply /= 2;
|
||||
for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
|
||||
BaseEdge &edge = this->edges[node1][node2];
|
||||
for (BaseEdge &edge : this->nodes[node1].edges) {
|
||||
if (edge.capacity > 0) {
|
||||
uint new_capacity = std::max(1U, edge.capacity / 2);
|
||||
if (edge.capacity < (1 << 16)) {
|
||||
edge.travel_time_sum = edge.travel_time_sum * new_capacity / edge.capacity;
|
||||
} else if (edge.travel_time_sum != 0) {
|
||||
edge.travel_time_sum = std::max(1ULL, edge.travel_time_sum / 2);
|
||||
edge.travel_time_sum = std::max<uint64_t>(1, edge.travel_time_sum / 2);
|
||||
}
|
||||
edge.capacity = new_capacity;
|
||||
edge.usage /= 2;
|
||||
@@ -91,8 +89,8 @@ void LinkGraph::Compress()
|
||||
*/
|
||||
void LinkGraph::Merge(LinkGraph *other)
|
||||
{
|
||||
Date age = _date - this->last_compression + 1;
|
||||
Date other_age = _date - other->last_compression + 1;
|
||||
TimerGameEconomy::Date age = TimerGameEconomy::date - this->last_compression + 1;
|
||||
TimerGameEconomy::Date other_age = TimerGameEconomy::date - other->last_compression + 1;
|
||||
NodeID first = this->Size();
|
||||
for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
|
||||
Station *st = Station::Get(other->nodes[node1].station);
|
||||
@@ -100,23 +98,13 @@ void LinkGraph::Merge(LinkGraph *other)
|
||||
this->nodes[new_node].supply = LinkGraph::Scale(other->nodes[node1].supply, age, other_age);
|
||||
st->goods[this->cargo].link_graph = this->index;
|
||||
st->goods[this->cargo].node = new_node;
|
||||
for (NodeID node2 = 0; node2 < node1; ++node2) {
|
||||
BaseEdge &forward = this->edges[new_node][first + node2];
|
||||
BaseEdge &backward = this->edges[first + node2][new_node];
|
||||
forward = other->edges[node1][node2];
|
||||
backward = other->edges[node2][node1];
|
||||
forward.capacity = LinkGraph::Scale(forward.capacity, age, other_age);
|
||||
forward.usage = LinkGraph::Scale(forward.usage, age, other_age);
|
||||
forward.travel_time_sum = LinkGraph::Scale(forward.travel_time_sum, age, other_age);
|
||||
if (forward.next_edge != INVALID_NODE) forward.next_edge += first;
|
||||
backward.capacity = LinkGraph::Scale(backward.capacity, age, other_age);
|
||||
backward.usage = LinkGraph::Scale(backward.usage, age, other_age);
|
||||
backward.travel_time_sum = LinkGraph::Scale(backward.travel_time_sum, age, other_age);
|
||||
if (backward.next_edge != INVALID_NODE) backward.next_edge += first;
|
||||
|
||||
for (BaseEdge &e : other->nodes[node1].edges) {
|
||||
BaseEdge &new_edge = this->nodes[new_node].edges.emplace_back(first + e.dest_node);
|
||||
new_edge.capacity = LinkGraph::Scale(e.capacity, age, other_age);
|
||||
new_edge.usage = LinkGraph::Scale(e.usage, age, other_age);
|
||||
new_edge.travel_time_sum = LinkGraph::Scale(e.travel_time_sum, age, other_age);
|
||||
}
|
||||
BaseEdge &new_start = this->edges[new_node][new_node];
|
||||
new_start = other->edges[node1][node1];
|
||||
if (new_start.next_edge != INVALID_NODE) new_start.next_edge += first;
|
||||
}
|
||||
delete other;
|
||||
}
|
||||
@@ -130,30 +118,24 @@ void LinkGraph::RemoveNode(NodeID id)
|
||||
assert(id < this->Size());
|
||||
|
||||
NodeID last_node = this->Size() - 1;
|
||||
for (NodeID i = 0; i <= last_node; ++i) {
|
||||
(*this)[i].RemoveEdge(id);
|
||||
BaseEdge *node_edges = this->edges[i];
|
||||
NodeID prev = i;
|
||||
NodeID next = node_edges[i].next_edge;
|
||||
while (next != INVALID_NODE) {
|
||||
if (next == last_node) {
|
||||
node_edges[prev].next_edge = id;
|
||||
break;
|
||||
}
|
||||
prev = next;
|
||||
next = node_edges[prev].next_edge;
|
||||
}
|
||||
node_edges[id] = node_edges[last_node];
|
||||
}
|
||||
Station::Get(this->nodes[last_node].station)->goods[this->cargo].node = id;
|
||||
/* Erase node by swapping with the last element. Node index is referenced
|
||||
* directly from station goods entries so the order and position must remain. */
|
||||
this->nodes[id] = this->nodes.back();
|
||||
this->nodes.pop_back();
|
||||
this->edges.EraseColumn(id);
|
||||
/* Not doing EraseRow here, as having the extra invalid row doesn't hurt
|
||||
* and removing it would trigger a lot of memmove. The data has already
|
||||
* been copied around in the loop above. */
|
||||
for (auto &n : this->nodes) {
|
||||
/* Find iterator position where an edge to id would be. */
|
||||
auto [first, last] = std::equal_range(n.edges.begin(), n.edges.end(), id);
|
||||
/* Remove potential node (erasing an empty range is safe). */
|
||||
auto insert = n.edges.erase(first, last);
|
||||
/* As the edge list is sorted, a potential edge to last_node will always be the last edge. */
|
||||
if (!n.edges.empty() && n.edges.back().dest_node == last_node) {
|
||||
/* Change dest ID and move into the spot of the deleted edge. */
|
||||
n.edges.back().dest_node = id;
|
||||
n.edges.insert(insert, n.edges.back());
|
||||
n.edges.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,23 +151,8 @@ NodeID LinkGraph::AddNode(const Station *st)
|
||||
const GoodsEntry &good = st->goods[this->cargo];
|
||||
|
||||
NodeID new_node = this->Size();
|
||||
this->nodes.emplace_back();
|
||||
/* Avoid reducing the height of the matrix as that is expensive and we
|
||||
* most likely will increase it again later which is again expensive. */
|
||||
this->edges.Resize(new_node + 1U, std::max(new_node + 1U, this->edges.Height()));
|
||||
this->nodes.emplace_back(st->xy, st->index, HasBit(good.status, GoodsEntry::GES_ACCEPTANCE));
|
||||
|
||||
this->nodes[new_node].Init(st->xy, st->index,
|
||||
HasBit(good.status, GoodsEntry::GES_ACCEPTANCE));
|
||||
|
||||
BaseEdge *new_edges = this->edges[new_node];
|
||||
|
||||
/* Reset the first edge starting at the new node */
|
||||
new_edges[new_node].next_edge = INVALID_NODE;
|
||||
|
||||
for (NodeID i = 0; i <= new_node; ++i) {
|
||||
new_edges[i].Init();
|
||||
this->edges[i][new_node].Init();
|
||||
}
|
||||
return new_node;
|
||||
}
|
||||
|
||||
@@ -197,18 +164,16 @@ NodeID LinkGraph::AddNode(const Station *st)
|
||||
* @param usage Usage to be added.
|
||||
* @param mode Update mode to be used.
|
||||
*/
|
||||
void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage, uint32 travel_time, EdgeUpdateMode mode)
|
||||
void LinkGraph::BaseNode::AddEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
|
||||
{
|
||||
assert(this->index != to);
|
||||
BaseEdge &edge = this->edges[to];
|
||||
BaseEdge &first = this->edges[this->index];
|
||||
assert(!this->HasEdgeTo(to));
|
||||
|
||||
BaseEdge &edge = *this->edges.emplace(std::upper_bound(this->edges.begin(), this->edges.end(), to), to);
|
||||
edge.capacity = capacity;
|
||||
edge.usage = usage;
|
||||
edge.travel_time_sum = travel_time * capacity;
|
||||
edge.next_edge = first.next_edge;
|
||||
first.next_edge = to;
|
||||
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = _date;
|
||||
if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
|
||||
edge.travel_time_sum = static_cast<uint64_t>(travel_time) * capacity;
|
||||
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = TimerGameEconomy::date;
|
||||
if (mode & EUM_RESTRICTED) edge.last_restricted_update = TimerGameEconomy::date;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,14 +183,14 @@ void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage, uint32 trave
|
||||
* @param usage Usage to be added.
|
||||
* @param mode Update mode to be used.
|
||||
*/
|
||||
void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage, uint32 travel_time, EdgeUpdateMode mode)
|
||||
void LinkGraph::BaseNode::UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
|
||||
{
|
||||
assert(capacity > 0);
|
||||
assert(usage <= capacity);
|
||||
if (this->edges[to].capacity == 0) {
|
||||
if (!this->HasEdgeTo(to)) {
|
||||
this->AddEdge(to, capacity, usage, travel_time, mode);
|
||||
} else {
|
||||
(*this)[to].Update(capacity, usage, travel_time, mode);
|
||||
this->GetEdge(to)->Update(capacity, usage, travel_time, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,29 +198,10 @@ void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage, uint32 tr
|
||||
* Remove an outgoing edge from this node.
|
||||
* @param to ID of destination node.
|
||||
*/
|
||||
void LinkGraph::Node::RemoveEdge(NodeID to)
|
||||
void LinkGraph::BaseNode::RemoveEdge(NodeID to)
|
||||
{
|
||||
if (this->index == to) return;
|
||||
BaseEdge &edge = this->edges[to];
|
||||
edge.capacity = 0;
|
||||
edge.last_unrestricted_update = INVALID_DATE;
|
||||
edge.last_restricted_update = INVALID_DATE;
|
||||
edge.usage = 0;
|
||||
edge.travel_time_sum = 0;
|
||||
|
||||
NodeID prev = this->index;
|
||||
NodeID next = this->edges[this->index].next_edge;
|
||||
while (next != INVALID_NODE) {
|
||||
if (next == to) {
|
||||
/* Will be removed, skip it. */
|
||||
this->edges[prev].next_edge = edge.next_edge;
|
||||
edge.next_edge = INVALID_NODE;
|
||||
break;
|
||||
} else {
|
||||
prev = next;
|
||||
next = this->edges[next].next_edge;
|
||||
}
|
||||
}
|
||||
auto [first, last] = std::equal_range(this->edges.begin(), this->edges.end(), to);
|
||||
this->edges.erase(first, last);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,33 +214,33 @@ void LinkGraph::Node::RemoveEdge(NodeID to)
|
||||
* @param travel_time Travel time to be added, in ticks.
|
||||
* @param mode Update mode to be applied.
|
||||
*/
|
||||
void LinkGraph::Edge::Update(uint capacity, uint usage, uint32 travel_time, EdgeUpdateMode mode)
|
||||
void LinkGraph::BaseEdge::Update(uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
|
||||
{
|
||||
assert(this->edge.capacity > 0);
|
||||
assert(this->capacity > 0);
|
||||
assert(capacity >= usage);
|
||||
|
||||
if (mode & EUM_INCREASE) {
|
||||
if (this->edge.travel_time_sum == 0) {
|
||||
this->edge.travel_time_sum = (this->edge.capacity + capacity) * travel_time;
|
||||
if (this->travel_time_sum == 0) {
|
||||
this->travel_time_sum = static_cast<uint64_t>(this->capacity + capacity) * travel_time;
|
||||
} else if (travel_time == 0) {
|
||||
this->edge.travel_time_sum += this->edge.travel_time_sum / this->edge.capacity * capacity;
|
||||
this->travel_time_sum += this->travel_time_sum / this->capacity * capacity;
|
||||
} else {
|
||||
this->edge.travel_time_sum += travel_time * capacity;
|
||||
this->travel_time_sum += static_cast<uint64_t>(travel_time) * capacity;
|
||||
}
|
||||
this->edge.capacity += capacity;
|
||||
this->edge.usage += usage;
|
||||
this->capacity += capacity;
|
||||
this->usage += usage;
|
||||
} else if (mode & EUM_REFRESH) {
|
||||
if (this->edge.travel_time_sum == 0) {
|
||||
this->edge.capacity = std::max(this->edge.capacity, capacity);
|
||||
this->edge.travel_time_sum = travel_time * this->edge.capacity;
|
||||
} else if (capacity > this->edge.capacity) {
|
||||
this->edge.travel_time_sum = this->edge.travel_time_sum / this->edge.capacity * capacity;
|
||||
this->edge.capacity = capacity;
|
||||
if (this->travel_time_sum == 0) {
|
||||
this->capacity = std::max(this->capacity, capacity);
|
||||
this->travel_time_sum = static_cast<uint64_t>(travel_time) * this->capacity;
|
||||
} else if (capacity > this->capacity) {
|
||||
this->travel_time_sum = this->travel_time_sum / this->capacity * capacity;
|
||||
this->capacity = capacity;
|
||||
}
|
||||
this->edge.usage = std::max(this->edge.usage, usage);
|
||||
this->usage = std::max(this->usage, usage);
|
||||
}
|
||||
if (mode & EUM_UNRESTRICTED) this->edge.last_unrestricted_update = _date;
|
||||
if (mode & EUM_RESTRICTED) this->edge.last_restricted_update = _date;
|
||||
if (mode & EUM_UNRESTRICTED) this->last_unrestricted_update = TimerGameEconomy::date;
|
||||
if (mode & EUM_RESTRICTED) this->last_restricted_update = TimerGameEconomy::date;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -305,12 +251,5 @@ void LinkGraph::Edge::Update(uint capacity, uint usage, uint32 travel_time, Edge
|
||||
void LinkGraph::Init(uint size)
|
||||
{
|
||||
assert(this->Size() == 0);
|
||||
this->edges.Resize(size, size);
|
||||
this->nodes.resize(size);
|
||||
|
||||
for (uint i = 0; i < size; ++i) {
|
||||
this->nodes[i].Init();
|
||||
BaseEdge *column = this->edges[i];
|
||||
for (uint j = 0; j < size; ++j) column[j].Init();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,9 @@
|
||||
#define LINKGRAPH_H
|
||||
|
||||
#include "../core/pool_type.hpp"
|
||||
#include "../core/smallmap_type.hpp"
|
||||
#include "../core/smallmatrix_type.hpp"
|
||||
#include "../station_base.h"
|
||||
#include "../cargotype.h"
|
||||
#include "../date_func.h"
|
||||
#include "../timer/timer_game_economy.h"
|
||||
#include "../saveload/saveload.h"
|
||||
#include "linkgraph_type.h"
|
||||
#include <utility>
|
||||
@@ -38,6 +36,51 @@ extern LinkGraphPool _link_graph_pool;
|
||||
*/
|
||||
class LinkGraph : public LinkGraphPool::PoolItem<&_link_graph_pool> {
|
||||
public:
|
||||
/**
|
||||
* An edge in the link graph. Corresponds to a link between two stations.
|
||||
*/
|
||||
struct BaseEdge {
|
||||
uint capacity; ///< Capacity of the link.
|
||||
uint usage; ///< Usage of the link.
|
||||
uint64_t travel_time_sum; ///< Sum of the travel times of the link, in ticks.
|
||||
TimerGameEconomy::Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated.
|
||||
TimerGameEconomy::Date last_restricted_update; ///< When the restricted part of the link was last updated.
|
||||
NodeID dest_node; ///< Destination of the edge.
|
||||
|
||||
BaseEdge(NodeID dest_node = INVALID_NODE);
|
||||
|
||||
/**
|
||||
* Get edge's average travel time.
|
||||
* @return Travel time, in ticks.
|
||||
*/
|
||||
uint32_t TravelTime() const { return this->travel_time_sum / this->capacity; }
|
||||
|
||||
/**
|
||||
* Get the date of the last update to any part of the edge's capacity.
|
||||
* @return Last update.
|
||||
*/
|
||||
TimerGameEconomy::Date LastUpdate() const { return std::max(this->last_unrestricted_update, this->last_restricted_update); }
|
||||
|
||||
void Update(uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
|
||||
void Restrict() { this->last_unrestricted_update = EconomyTime::INVALID_DATE; }
|
||||
void Release() { this->last_restricted_update = EconomyTime::INVALID_DATE; }
|
||||
|
||||
/** Comparison operator based on \c dest_node. */
|
||||
bool operator <(const BaseEdge &rhs) const
|
||||
{
|
||||
return this->dest_node < rhs.dest_node;
|
||||
}
|
||||
|
||||
bool operator <(NodeID rhs) const
|
||||
{
|
||||
return this->dest_node < rhs;
|
||||
}
|
||||
|
||||
friend inline bool operator <(NodeID lhs, const LinkGraph::BaseEdge &rhs)
|
||||
{
|
||||
return lhs < rhs.dest_node;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Node of the link graph. contains all relevant information from the associated
|
||||
@@ -49,364 +92,11 @@ public:
|
||||
uint demand; ///< Acceptance at the station.
|
||||
StationID station; ///< Station ID.
|
||||
TileIndex xy; ///< Location of the station referred to by the node.
|
||||
Date last_update; ///< When the supply was last updated.
|
||||
void Init(TileIndex xy = INVALID_TILE, StationID st = INVALID_STATION, uint demand = 0);
|
||||
};
|
||||
TimerGameEconomy::Date last_update; ///< When the supply was last updated.
|
||||
|
||||
/**
|
||||
* An edge in the link graph. Corresponds to a link between two stations or at
|
||||
* least the distance between them. Edges from one node to itself contain the
|
||||
* ID of the opposite Node of the first active edge (i.e. not just distance) in
|
||||
* the column as next_edge.
|
||||
*/
|
||||
struct BaseEdge {
|
||||
uint capacity; ///< Capacity of the link.
|
||||
uint usage; ///< Usage of the link.
|
||||
uint64 travel_time_sum; ///< Sum of the travel times of the link, in ticks.
|
||||
Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated.
|
||||
Date last_restricted_update; ///< When the restricted part of the link was last updated.
|
||||
NodeID next_edge; ///< Destination of next valid edge starting at the same source node.
|
||||
void Init();
|
||||
};
|
||||
std::vector<BaseEdge> edges; ///< Sorted list of outgoing edges from this node.
|
||||
|
||||
/**
|
||||
* Wrapper for an edge (const or not) allowing retrieval, but no modification.
|
||||
* @tparam Tedge Actual edge class, may be "const BaseEdge" or just "BaseEdge".
|
||||
*/
|
||||
template<typename Tedge>
|
||||
class EdgeWrapper {
|
||||
protected:
|
||||
Tedge &edge; ///< Actual edge to be used.
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Wrap a an edge.
|
||||
* @param edge Edge to be wrapped.
|
||||
*/
|
||||
EdgeWrapper (Tedge &edge) : edge(edge) {}
|
||||
|
||||
/**
|
||||
* Get edge's capacity.
|
||||
* @return Capacity.
|
||||
*/
|
||||
uint Capacity() const { return this->edge.capacity; }
|
||||
|
||||
/**
|
||||
* Get edge's usage.
|
||||
* @return Usage.
|
||||
*/
|
||||
uint Usage() const { return this->edge.usage; }
|
||||
|
||||
/**
|
||||
* Get edge's average travel time.
|
||||
* @return Travel time, in ticks.
|
||||
*/
|
||||
uint32 TravelTime() const { return this->edge.travel_time_sum / this->edge.capacity; }
|
||||
|
||||
/**
|
||||
* Get the date of the last update to the edge's unrestricted capacity.
|
||||
* @return Last update.
|
||||
*/
|
||||
Date LastUnrestrictedUpdate() const { return this->edge.last_unrestricted_update; }
|
||||
|
||||
/**
|
||||
* Get the date of the last update to the edge's restricted capacity.
|
||||
* @return Last update.
|
||||
*/
|
||||
Date LastRestrictedUpdate() const { return this->edge.last_restricted_update; }
|
||||
|
||||
/**
|
||||
* Get the date of the last update to any part of the edge's capacity.
|
||||
* @return Last update.
|
||||
*/
|
||||
Date LastUpdate() const { return std::max(this->edge.last_unrestricted_update, this->edge.last_restricted_update); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper for a node (const or not) allowing retrieval, but no modification.
|
||||
* @tparam Tedge Actual node class, may be "const BaseNode" or just "BaseNode".
|
||||
* @tparam Tedge Actual edge class, may be "const BaseEdge" or just "BaseEdge".
|
||||
*/
|
||||
template<typename Tnode, typename Tedge>
|
||||
class NodeWrapper {
|
||||
protected:
|
||||
Tnode &node; ///< Node being wrapped.
|
||||
Tedge *edges; ///< Outgoing edges for wrapped node.
|
||||
NodeID index; ///< ID of wrapped node.
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Wrap a node.
|
||||
* @param node Node to be wrapped.
|
||||
* @param edges Outgoing edges for node to be wrapped.
|
||||
* @param index ID of node to be wrapped.
|
||||
*/
|
||||
NodeWrapper(Tnode &node, Tedge *edges, NodeID index) : node(node),
|
||||
edges(edges), index(index) {}
|
||||
|
||||
/**
|
||||
* Get supply of wrapped node.
|
||||
* @return Supply.
|
||||
*/
|
||||
uint Supply() const { return this->node.supply; }
|
||||
|
||||
/**
|
||||
* Get demand of wrapped node.
|
||||
* @return Demand.
|
||||
*/
|
||||
uint Demand() const { return this->node.demand; }
|
||||
|
||||
/**
|
||||
* Get ID of station belonging to wrapped node.
|
||||
* @return ID of node's station.
|
||||
*/
|
||||
StationID Station() const { return this->node.station; }
|
||||
|
||||
/**
|
||||
* Get node's last update.
|
||||
* @return Last update.
|
||||
*/
|
||||
Date LastUpdate() const { return this->node.last_update; }
|
||||
|
||||
/**
|
||||
* Get the location of the station associated with the node.
|
||||
* @return Location of the station.
|
||||
*/
|
||||
TileIndex XY() const { return this->node.xy; }
|
||||
};
|
||||
|
||||
/**
|
||||
* Base class for iterating across outgoing edges of a node. Only the real
|
||||
* edges (those with capacity) are iterated. The ones with only distance
|
||||
* information are skipped.
|
||||
* @tparam Tedge Actual edge class. May be "BaseEdge" or "const BaseEdge".
|
||||
* @tparam Titer Actual iterator class.
|
||||
*/
|
||||
template <class Tedge, class Tedge_wrapper, class Titer>
|
||||
class BaseEdgeIterator {
|
||||
protected:
|
||||
Tedge *base; ///< Array of edges being iterated.
|
||||
NodeID current; ///< Current offset in edges array.
|
||||
|
||||
/**
|
||||
* A "fake" pointer to enable operator-> on temporaries. As the objects
|
||||
* returned from operator* aren't references but real objects, we have
|
||||
* to return something that implements operator->, but isn't a pointer
|
||||
* from operator->. A fake pointer.
|
||||
*/
|
||||
class FakePointer : public std::pair<NodeID, Tedge_wrapper> {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Construct a fake pointer from a pair of NodeID and edge.
|
||||
* @param pair Pair to be "pointed" to (in fact shallow-copied).
|
||||
*/
|
||||
FakePointer(const std::pair<NodeID, Tedge_wrapper> &pair) : std::pair<NodeID, Tedge_wrapper>(pair) {}
|
||||
|
||||
/**
|
||||
* Retrieve the pair by operator->.
|
||||
* @return Pair being "pointed" to.
|
||||
*/
|
||||
std::pair<NodeID, Tedge_wrapper> *operator->() { return this; }
|
||||
};
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param base Array of edges to be iterated.
|
||||
* @param current ID of current node (to locate the first edge).
|
||||
*/
|
||||
BaseEdgeIterator (Tedge *base, NodeID current) :
|
||||
base(base),
|
||||
current(current == INVALID_NODE ? current : base[current].next_edge)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Prefix-increment.
|
||||
* @return This.
|
||||
*/
|
||||
Titer &operator++()
|
||||
{
|
||||
this->current = this->base[this->current].next_edge;
|
||||
return static_cast<Titer &>(*this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Postfix-increment.
|
||||
* @return Version of this before increment.
|
||||
*/
|
||||
Titer operator++(int)
|
||||
{
|
||||
Titer ret(static_cast<Titer &>(*this));
|
||||
this->current = this->base[this->current].next_edge;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare with some other edge iterator. The other one may be of a
|
||||
* child class.
|
||||
* @tparam Tother Class of other iterator.
|
||||
* @param other Instance of other iterator.
|
||||
* @return If the iterators have the same edge array and current node.
|
||||
*/
|
||||
template<class Tother>
|
||||
bool operator==(const Tother &other)
|
||||
{
|
||||
return this->base == other.base && this->current == other.current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare for inequality with some other edge iterator. The other one
|
||||
* may be of a child class.
|
||||
* @tparam Tother Class of other iterator.
|
||||
* @param other Instance of other iterator.
|
||||
* @return If either the edge arrays or the current nodes differ.
|
||||
*/
|
||||
template<class Tother>
|
||||
bool operator!=(const Tother &other)
|
||||
{
|
||||
return this->base != other.base || this->current != other.current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereference with operator*.
|
||||
* @return Pair of current target NodeID and edge object.
|
||||
*/
|
||||
std::pair<NodeID, Tedge_wrapper> operator*() const
|
||||
{
|
||||
return std::pair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereference with operator->.
|
||||
* @return Fake pointer to Pair of current target NodeID and edge object.
|
||||
*/
|
||||
FakePointer operator->() const {
|
||||
return FakePointer(this->operator*());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A constant edge class.
|
||||
*/
|
||||
typedef EdgeWrapper<const BaseEdge> ConstEdge;
|
||||
|
||||
/**
|
||||
* An updatable edge class.
|
||||
*/
|
||||
class Edge : public EdgeWrapper<BaseEdge> {
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* @param edge Edge to be wrapped.
|
||||
*/
|
||||
Edge(BaseEdge &edge) : EdgeWrapper<BaseEdge>(edge) {}
|
||||
void Update(uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
|
||||
void Restrict() { this->edge.last_unrestricted_update = INVALID_DATE; }
|
||||
void Release() { this->edge.last_restricted_update = INVALID_DATE; }
|
||||
};
|
||||
|
||||
/**
|
||||
* An iterator for const edges. Cannot be typedef'ed because of
|
||||
* template-reference to ConstEdgeIterator itself.
|
||||
*/
|
||||
class ConstEdgeIterator : public BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator> {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param edges Array of edges to be iterated over.
|
||||
* @param current ID of current edge's end node.
|
||||
*/
|
||||
ConstEdgeIterator(const BaseEdge *edges, NodeID current) :
|
||||
BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator>(edges, current) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* An iterator for non-const edges. Cannot be typedef'ed because of
|
||||
* template-reference to EdgeIterator itself.
|
||||
*/
|
||||
class EdgeIterator : public BaseEdgeIterator<BaseEdge, Edge, EdgeIterator> {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param edges Array of edges to be iterated over.
|
||||
* @param current ID of current edge's end node.
|
||||
*/
|
||||
EdgeIterator(BaseEdge *edges, NodeID current) :
|
||||
BaseEdgeIterator<BaseEdge, Edge, EdgeIterator>(edges, current) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* Constant node class. Only retrieval operations are allowed on both the
|
||||
* node itself and its edges.
|
||||
*/
|
||||
class ConstNode : public NodeWrapper<const BaseNode, const BaseEdge> {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param lg LinkGraph to get the node from.
|
||||
* @param node ID of the node.
|
||||
*/
|
||||
ConstNode(const LinkGraph *lg, NodeID node) :
|
||||
NodeWrapper<const BaseNode, const BaseEdge>(lg->nodes[node], lg->edges[node], node)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Get a ConstEdge. This is not a reference as the wrapper objects are
|
||||
* not actually persistent.
|
||||
* @param to ID of end node of edge.
|
||||
* @return Constant edge wrapper.
|
||||
*/
|
||||
ConstEdge operator[](NodeID to) const { return ConstEdge(this->edges[to]); }
|
||||
|
||||
/**
|
||||
* Get an iterator pointing to the start of the edges array.
|
||||
* @return Constant edge iterator.
|
||||
*/
|
||||
ConstEdgeIterator Begin() const { return ConstEdgeIterator(this->edges, this->index); }
|
||||
|
||||
/**
|
||||
* Get an iterator pointing beyond the end of the edges array.
|
||||
* @return Constant edge iterator.
|
||||
*/
|
||||
ConstEdgeIterator End() const { return ConstEdgeIterator(this->edges, INVALID_NODE); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Updatable node class. The node itself as well as its edges can be modified.
|
||||
*/
|
||||
class Node : public NodeWrapper<BaseNode, BaseEdge> {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param lg LinkGraph to get the node from.
|
||||
* @param node ID of the node.
|
||||
*/
|
||||
Node(LinkGraph *lg, NodeID node) :
|
||||
NodeWrapper<BaseNode, BaseEdge>(lg->nodes[node], lg->edges[node], node)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Get an Edge. This is not a reference as the wrapper objects are not
|
||||
* actually persistent.
|
||||
* @param to ID of end node of edge.
|
||||
* @return Edge wrapper.
|
||||
*/
|
||||
Edge operator[](NodeID to) { return Edge(this->edges[to]); }
|
||||
|
||||
/**
|
||||
* Get an iterator pointing to the start of the edges array.
|
||||
* @return Edge iterator.
|
||||
*/
|
||||
EdgeIterator Begin() { return EdgeIterator(this->edges, this->index); }
|
||||
|
||||
/**
|
||||
* Get an iterator pointing beyond the end of the edges array.
|
||||
* @return Constant edge iterator.
|
||||
*/
|
||||
EdgeIterator End() { return EdgeIterator(this->edges, INVALID_NODE); }
|
||||
BaseNode(TileIndex xy = INVALID_TILE, StationID st = INVALID_STATION, uint demand = 0);
|
||||
|
||||
/**
|
||||
* Update the node's supply and set last_update to the current date.
|
||||
@@ -414,8 +104,8 @@ public:
|
||||
*/
|
||||
void UpdateSupply(uint supply)
|
||||
{
|
||||
this->node.supply += supply;
|
||||
this->node.last_update = _date;
|
||||
this->supply += supply;
|
||||
this->last_update = TimerGameEconomy::date;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -424,7 +114,7 @@ public:
|
||||
*/
|
||||
void UpdateLocation(TileIndex xy)
|
||||
{
|
||||
this->node.xy = xy;
|
||||
this->xy = xy;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -433,25 +123,57 @@ public:
|
||||
*/
|
||||
void SetDemand(uint demand)
|
||||
{
|
||||
this->node.demand = demand;
|
||||
this->demand = demand;
|
||||
}
|
||||
|
||||
void AddEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
|
||||
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
|
||||
void AddEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
|
||||
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
|
||||
void RemoveEdge(NodeID to);
|
||||
|
||||
/**
|
||||
* Check if an edge to a destination is present.
|
||||
* @param dest Wanted edge destination.
|
||||
* @return True if an edge is present.
|
||||
*/
|
||||
bool HasEdgeTo(NodeID dest) const
|
||||
{
|
||||
return std::binary_search(this->edges.begin(), this->edges.end(), dest);
|
||||
}
|
||||
|
||||
BaseEdge &operator[](NodeID to)
|
||||
{
|
||||
assert(this->HasEdgeTo(to));
|
||||
return *GetEdge(to);
|
||||
}
|
||||
|
||||
const BaseEdge &operator[](NodeID to) const
|
||||
{
|
||||
assert(this->HasEdgeTo(to));
|
||||
return *GetEdge(to);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<BaseEdge>::iterator GetEdge(NodeID dest)
|
||||
{
|
||||
return std::lower_bound(this->edges.begin(), this->edges.end(), dest);
|
||||
}
|
||||
|
||||
std::vector<BaseEdge>::const_iterator GetEdge(NodeID dest) const
|
||||
{
|
||||
return std::lower_bound(this->edges.begin(), this->edges.end(), dest);
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::vector<BaseNode> NodeVector;
|
||||
typedef SmallMatrix<BaseEdge> EdgeMatrix;
|
||||
|
||||
/** Minimum effective distance for timeout calculation. */
|
||||
static const uint MIN_TIMEOUT_DISTANCE = 32;
|
||||
|
||||
/** Number of days before deleting links served only by vehicles stopped in depot. */
|
||||
static const uint STALE_LINK_DEPOT_TIMEOUT = 1024;
|
||||
static constexpr TimerGameEconomy::Date STALE_LINK_DEPOT_TIMEOUT = 1024;
|
||||
|
||||
/** Minimum number of days between subsequent compressions of a LG. */
|
||||
static const uint COMPRESSION_INTERVAL = 256;
|
||||
static constexpr TimerGameEconomy::Date COMPRESSION_INTERVAL = 256;
|
||||
|
||||
/**
|
||||
* Scale a value from a link graph of age orig_age for usage in one of age
|
||||
@@ -461,9 +183,9 @@ public:
|
||||
* @param orig_age Age of the original link graph.
|
||||
* @return scaled value.
|
||||
*/
|
||||
inline static uint Scale(uint val, uint target_age, uint orig_age)
|
||||
inline static uint Scale(uint val, TimerGameEconomy::Date target_age, TimerGameEconomy::Date orig_age)
|
||||
{
|
||||
return val > 0 ? std::max(1U, val * target_age / orig_age) : 0;
|
||||
return val > 0 ? std::max(1U, val * target_age.base() / orig_age.base()) : 0;
|
||||
}
|
||||
|
||||
/** Bare constructor, only for save/load. */
|
||||
@@ -472,10 +194,10 @@ public:
|
||||
* Real constructor.
|
||||
* @param cargo Cargo the link graph is about.
|
||||
*/
|
||||
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(_date) {}
|
||||
LinkGraph(CargoID cargo) : cargo(cargo), last_compression(TimerGameEconomy::date) {}
|
||||
|
||||
void Init(uint size);
|
||||
void ShiftDates(int interval);
|
||||
void ShiftDates(TimerGameEconomy::Date interval);
|
||||
void Compress();
|
||||
void Merge(LinkGraph *other);
|
||||
|
||||
@@ -492,14 +214,14 @@ public:
|
||||
* @param num ID of the node.
|
||||
* @return the Requested node.
|
||||
*/
|
||||
inline Node operator[](NodeID num) { return Node(this, num); }
|
||||
inline BaseNode &operator[](NodeID num) { return this->nodes[num]; }
|
||||
|
||||
/**
|
||||
* Get a const reference to a node with the specified id.
|
||||
* @param num ID of the node.
|
||||
* @return the Requested node.
|
||||
*/
|
||||
inline ConstNode operator[](NodeID num) const { return ConstNode(this, num); }
|
||||
inline const BaseNode &operator[](NodeID num) const { return this->nodes[num]; }
|
||||
|
||||
/**
|
||||
* Get the current size of the component.
|
||||
@@ -511,7 +233,7 @@ public:
|
||||
* Get date of last compression.
|
||||
* @return Date of last compression.
|
||||
*/
|
||||
inline Date LastCompression() const { return this->last_compression; }
|
||||
inline TimerGameEconomy::Date LastCompression() const { return this->last_compression; }
|
||||
|
||||
/**
|
||||
* Get the cargo ID this component's link graph refers to.
|
||||
@@ -526,24 +248,22 @@ public:
|
||||
*/
|
||||
inline uint Monthly(uint base) const
|
||||
{
|
||||
return base * 30 / (_date - this->last_compression + 1);
|
||||
return base * 30 / (TimerGameEconomy::date - this->last_compression + 1).base();
|
||||
}
|
||||
|
||||
NodeID AddNode(const Station *st);
|
||||
void RemoveNode(NodeID id);
|
||||
|
||||
protected:
|
||||
friend class LinkGraph::ConstNode;
|
||||
friend class LinkGraph::Node;
|
||||
friend SaveLoadTable GetLinkGraphDesc();
|
||||
friend SaveLoadTable GetLinkGraphJobDesc();
|
||||
friend class SlLinkgraphNode;
|
||||
friend class SlLinkgraphEdge;
|
||||
friend class LinkGraphJob;
|
||||
|
||||
CargoID cargo; ///< Cargo of this component's link graph.
|
||||
Date last_compression; ///< Last time the capacities and supplies were compressed.
|
||||
TimerGameEconomy::Date last_compression; ///< Last time the capacities and supplies were compressed.
|
||||
NodeVector nodes; ///< Nodes in the component.
|
||||
EdgeMatrix edges; ///< Edges in the component.
|
||||
};
|
||||
|
||||
#endif /* LINKGRAPH_H */
|
||||
|
||||
@@ -13,13 +13,11 @@
|
||||
#include "linkgraph.h"
|
||||
#include "linkgraphschedule.h"
|
||||
|
||||
typedef LinkGraph::Node Node;
|
||||
typedef LinkGraph::Edge Edge;
|
||||
typedef LinkGraph::EdgeIterator EdgeIterator;
|
||||
typedef LinkGraph::BaseNode Node;
|
||||
typedef LinkGraph::BaseEdge Edge;
|
||||
|
||||
typedef LinkGraph::ConstNode ConstNode;
|
||||
typedef LinkGraph::ConstEdge ConstEdge;
|
||||
typedef LinkGraph::ConstEdgeIterator ConstEdgeIterator;
|
||||
typedef const LinkGraph::BaseNode ConstNode;
|
||||
typedef const LinkGraph::BaseEdge ConstEdge;
|
||||
|
||||
|
||||
#endif /* LINKGRAPH_BASE_H */
|
||||
|
||||
@@ -12,12 +12,15 @@
|
||||
#include "../window_func.h"
|
||||
#include "../company_base.h"
|
||||
#include "../company_gui.h"
|
||||
#include "../date_func.h"
|
||||
#include "../timer/timer_game_tick.h"
|
||||
#include "../timer/timer_game_calendar.h"
|
||||
#include "../viewport_func.h"
|
||||
#include "../zoom_func.h"
|
||||
#include "../smallmap_gui.h"
|
||||
#include "../core/geometry_func.hpp"
|
||||
#include "../widgets/link_graph_legend_widget.h"
|
||||
#include "../strings_func.h"
|
||||
#include "linkgraph_gui.h"
|
||||
|
||||
#include "table/strings.h"
|
||||
|
||||
@@ -27,7 +30,7 @@
|
||||
* Colours for the various "load" states of links. Ordered from "unused" to
|
||||
* "overloaded".
|
||||
*/
|
||||
const uint8 LinkGraphOverlay::LINK_COLOURS[][12] = {
|
||||
const uint8_t LinkGraphOverlay::LINK_COLOURS[][12] = {
|
||||
{
|
||||
0x0f, 0xd1, 0xd0, 0x57,
|
||||
0x55, 0x53, 0xbf, 0xbd,
|
||||
@@ -88,10 +91,10 @@ void LinkGraphOverlay::RebuildCache()
|
||||
if (!LinkGraph::IsValidID(sta->goods[c].link_graph)) continue;
|
||||
const LinkGraph &lg = *LinkGraph::Get(sta->goods[c].link_graph);
|
||||
|
||||
ConstNode from_node = lg[sta->goods[c].node];
|
||||
supply += lg.Monthly(from_node.Supply());
|
||||
for (ConstEdgeIterator i = from_node.Begin(); i != from_node.End(); ++i) {
|
||||
StationID to = lg[i->first].Station();
|
||||
ConstNode &from_node = lg[sta->goods[c].node];
|
||||
supply += lg.Monthly(from_node.supply);
|
||||
for (const Edge &edge : from_node.edges) {
|
||||
StationID to = lg[edge.dest_node].station;
|
||||
assert(from != to);
|
||||
if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
|
||||
continue;
|
||||
@@ -149,19 +152,19 @@ inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixe
|
||||
* See: https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
|
||||
*/
|
||||
|
||||
const uint8 INSIDE = 0; // 0000
|
||||
const uint8 LEFT = 1; // 0001
|
||||
const uint8 RIGHT = 2; // 0010
|
||||
const uint8 BOTTOM = 4; // 0100
|
||||
const uint8 TOP = 8; // 1000
|
||||
const uint8_t INSIDE = 0; // 0000
|
||||
const uint8_t LEFT = 1; // 0001
|
||||
const uint8_t RIGHT = 2; // 0010
|
||||
const uint8_t BOTTOM = 4; // 0100
|
||||
const uint8_t TOP = 8; // 1000
|
||||
|
||||
int x0 = pta.x;
|
||||
int y0 = pta.y;
|
||||
int x1 = ptb.x;
|
||||
int y1 = ptb.y;
|
||||
|
||||
auto out_code = [&](int x, int y) -> uint8 {
|
||||
uint8 out = INSIDE;
|
||||
auto out_code = [&](int x, int y) -> uint8_t {
|
||||
uint8_t out = INSIDE;
|
||||
if (x < left) {
|
||||
out |= LEFT;
|
||||
} else if (x > right) {
|
||||
@@ -175,24 +178,24 @@ inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixe
|
||||
return out;
|
||||
};
|
||||
|
||||
uint8 c0 = out_code(x0, y0);
|
||||
uint8 c1 = out_code(x1, y1);
|
||||
uint8_t c0 = out_code(x0, y0);
|
||||
uint8_t c1 = out_code(x1, y1);
|
||||
|
||||
while (true) {
|
||||
if (c0 == 0 || c1 == 0) return true;
|
||||
if ((c0 & c1) != 0) return false;
|
||||
|
||||
if (c0 & TOP) { // point 0 is above the clip window
|
||||
x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (top - y0)) / ((int64) (y1 - y0)));
|
||||
x0 = x0 + (int)(((int64_t) (x1 - x0)) * ((int64_t) (top - y0)) / ((int64_t) (y1 - y0)));
|
||||
y0 = top;
|
||||
} else if (c0 & BOTTOM) { // point 0 is below the clip window
|
||||
x0 = x0 + (int)(((int64) (x1 - x0)) * ((int64) (bottom - y0)) / ((int64) (y1 - y0)));
|
||||
x0 = x0 + (int)(((int64_t) (x1 - x0)) * ((int64_t) (bottom - y0)) / ((int64_t) (y1 - y0)));
|
||||
y0 = bottom;
|
||||
} else if (c0 & RIGHT) { // point 0 is to the right of clip window
|
||||
y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (right - x0)) / ((int64) (x1 - x0)));
|
||||
y0 = y0 + (int)(((int64_t) (y1 - y0)) * ((int64_t) (right - x0)) / ((int64_t) (x1 - x0)));
|
||||
x0 = right;
|
||||
} else if (c0 & LEFT) { // point 0 is to the left of clip window
|
||||
y0 = y0 + (int)(((int64) (y1 - y0)) * ((int64) (left - x0)) / ((int64) (x1 - x0)));
|
||||
y0 = y0 + (int)(((int64_t) (y1 - y0)) * ((int64_t) (left - x0)) / ((int64_t) (x1 - x0)));
|
||||
x0 = left;
|
||||
}
|
||||
|
||||
@@ -217,11 +220,11 @@ void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
|
||||
continue;
|
||||
}
|
||||
const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
|
||||
ConstEdge edge = lg[ge.node][to->goods[c].node];
|
||||
if (edge.Capacity() > 0) {
|
||||
this->AddStats(c, lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
|
||||
if (lg[ge.node].HasEdgeTo(to->goods[c].node)) {
|
||||
ConstEdge &edge = lg[ge.node][to->goods[c].node];
|
||||
this->AddStats(c, lg.Monthly(edge.capacity), lg.Monthly(edge.usage),
|
||||
ge.flows.GetFlowVia(to->index),
|
||||
edge.TravelTime() / DAY_TICKS,
|
||||
edge.TravelTime() / Ticks::DAY_TICKS,
|
||||
from->owner == OWNER_NONE || to->owner == OWNER_NONE,
|
||||
this->cached_links[from->index][to->index]);
|
||||
}
|
||||
@@ -238,7 +241,7 @@ void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
|
||||
* @param new_shared If the new link is shared.
|
||||
* @param cargo LinkProperties to write the information to.
|
||||
*/
|
||||
/* static */ void LinkGraphOverlay::AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_plan, uint32 time, bool new_shared, LinkProperties &cargo)
|
||||
/* static */ void LinkGraphOverlay::AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_plan, uint32_t time, bool new_shared, LinkProperties &cargo)
|
||||
{
|
||||
/* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
|
||||
if (cargo.capacity == 0 ||
|
||||
@@ -273,14 +276,14 @@ void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi)
|
||||
void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
|
||||
{
|
||||
int width = ScaleGUITrad(this->scale);
|
||||
for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
|
||||
if (!Station::IsValidID(i->first)) continue;
|
||||
Point pta = this->GetStationMiddle(Station::Get(i->first));
|
||||
for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
|
||||
if (!Station::IsValidID(j->first)) continue;
|
||||
Point ptb = this->GetStationMiddle(Station::Get(j->first));
|
||||
for (const auto &i : this->cached_links) {
|
||||
if (!Station::IsValidID(i.first)) continue;
|
||||
Point pta = this->GetStationMiddle(Station::Get(i.first));
|
||||
for (const auto &j : i.second) {
|
||||
if (!Station::IsValidID(j.first)) continue;
|
||||
Point ptb = this->GetStationMiddle(Station::Get(j.first));
|
||||
if (!this->IsLinkVisible(pta, ptb, dpi, width + 2)) continue;
|
||||
this->DrawContent(pta, ptb, j->second);
|
||||
this->DrawContent(pta, ptb, j.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -319,17 +322,17 @@ void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &c
|
||||
void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
|
||||
{
|
||||
int width = ScaleGUITrad(this->scale);
|
||||
for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
|
||||
const Station *st = Station::GetIfValid(i->first);
|
||||
for (const auto &i : this->cached_stations) {
|
||||
const Station *st = Station::GetIfValid(i.first);
|
||||
if (st == nullptr) continue;
|
||||
Point pt = this->GetStationMiddle(st);
|
||||
if (!this->IsPointVisible(pt, dpi, 3 * width)) continue;
|
||||
|
||||
uint r = width * 2 + width * 2 * std::min(200U, i->second) / 200;
|
||||
uint r = width * 2 + width * 2 * std::min(200U, i.second) / 200;
|
||||
|
||||
LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
|
||||
_colour_gradient[st->owner != OWNER_NONE ?
|
||||
(Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
|
||||
Company::Get(st->owner)->colour : COLOUR_GREY][5],
|
||||
_colour_gradient[COLOUR_GREY][1]);
|
||||
}
|
||||
}
|
||||
@@ -369,17 +372,18 @@ bool LinkGraphOverlay::ShowTooltip(Point pt, TooltipCloseCondition close_cond)
|
||||
|
||||
/* Check the distance from the cursor to the line defined by the two stations. */
|
||||
Point ptb = this->GetStationMiddle(Station::Get(j->first));
|
||||
float dist = std::abs((ptb.x - pta.x) * (pta.y - pt.y) - (pta.x - pt.x) * (ptb.y - pta.y)) /
|
||||
std::sqrt((ptb.x - pta.x) * (ptb.x - pta.x) + (ptb.y - pta.y) * (ptb.y - pta.y));
|
||||
float dist = std::abs((int64_t)(ptb.x - pta.x) * (int64_t)(pta.y - pt.y) - (int64_t)(pta.x - pt.x) * (int64_t)(ptb.y - pta.y)) /
|
||||
std::sqrt((int64_t)(ptb.x - pta.x) * (int64_t)(ptb.x - pta.x) + (int64_t)(ptb.y - pta.y) * (int64_t)(ptb.y - pta.y));
|
||||
const auto &link = j->second;
|
||||
if (dist <= 4 && link.Usage() > 0 &&
|
||||
pt.x >= std::min(pta.x, ptb.x) &&
|
||||
pt.x <= std::max(pta.x, ptb.x)) {
|
||||
static char buf[1024];
|
||||
char *buf_end = buf;
|
||||
buf[0] = 0;
|
||||
pt.x + 2 >= std::min(pta.x, ptb.x) &&
|
||||
pt.x - 2 <= std::max(pta.x, ptb.x) &&
|
||||
pt.y + 2 >= std::min(pta.y, ptb.y) &&
|
||||
pt.y - 2 <= std::max(pta.y, ptb.y)) {
|
||||
static std::string tooltip_extension;
|
||||
tooltip_extension.clear();
|
||||
/* Fill buf with more information if this is a bidirectional link. */
|
||||
uint32 back_time = 0;
|
||||
uint32_t back_time = 0;
|
||||
auto k = this->cached_links[j->first].find(i->first);
|
||||
if (k != this->cached_links[j->first].end()) {
|
||||
const auto &back = k->second;
|
||||
@@ -388,27 +392,29 @@ bool LinkGraphOverlay::ShowTooltip(Point pt, TooltipCloseCondition close_cond)
|
||||
SetDParam(0, back.cargo);
|
||||
SetDParam(1, back.Usage());
|
||||
SetDParam(2, back.Usage() * 100 / (back.capacity + 1));
|
||||
buf_end = GetString(buf, STR_LINKGRAPH_STATS_TOOLTIP_RETURN_EXTENSION, lastof(buf));
|
||||
tooltip_extension = GetString(STR_LINKGRAPH_STATS_TOOLTIP_RETURN_EXTENSION);
|
||||
}
|
||||
}
|
||||
/* Add information about the travel time if known. */
|
||||
const auto time = link.time ? back_time ? ((link.time + back_time) / 2) : link.time : back_time;
|
||||
if (time > 0) {
|
||||
SetDParam(0, time);
|
||||
buf_end = GetString(buf_end, STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION, lastof(buf));
|
||||
tooltip_extension += GetString(STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION);
|
||||
}
|
||||
SetDParam(0, link.cargo);
|
||||
SetDParam(1, link.Usage());
|
||||
SetDParam(2, i->first);
|
||||
SetDParam(3, j->first);
|
||||
SetDParam(4, link.Usage() * 100 / (link.capacity + 1));
|
||||
SetDParamStr(5, buf);
|
||||
GuiShowTooltips(this->window, STR_LINKGRAPH_STATS_TOOLTIP, 7, nullptr, close_cond);
|
||||
SetDParamStr(5, tooltip_extension);
|
||||
GuiShowTooltips(this->window,
|
||||
TimerGameEconomy::UsingWallclockUnits() ? STR_LINKGRAPH_STATS_TOOLTIP_MINUTE : STR_LINKGRAPH_STATS_TOOLTIP_MONTH,
|
||||
close_cond, 7);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
GuiShowTooltips(this->window, STR_NULL, 0, nullptr, close_cond);
|
||||
GuiShowTooltips(this->window, STR_NULL, close_cond);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -423,7 +429,7 @@ Point LinkGraphOverlay::GetStationMiddle(const Station *st) const
|
||||
return GetViewportStationMiddle(this->window->viewport, st);
|
||||
} else {
|
||||
/* assume this is a smallmap */
|
||||
return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
|
||||
return GetSmallMapStationMiddle(this->window, st);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,7 +448,7 @@ void LinkGraphOverlay::SetCargoMask(CargoTypes cargo_mask)
|
||||
* Set a new company mask and rebuild the cache.
|
||||
* @param company_mask New company mask.
|
||||
*/
|
||||
void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
|
||||
void LinkGraphOverlay::SetCompanyMask(CompanyMask company_mask)
|
||||
{
|
||||
this->company_mask = company_mask;
|
||||
this->RebuildCache();
|
||||
@@ -450,58 +456,59 @@ void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
|
||||
}
|
||||
|
||||
/** Make a number of rows with buttons for each company for the linkgraph legend window. */
|
||||
NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
|
||||
std::unique_ptr<NWidgetBase> MakeCompanyButtonRowsLinkGraphGUI()
|
||||
{
|
||||
return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, COLOUR_GREY, 3, STR_NULL);
|
||||
return MakeCompanyButtonRows(WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, COLOUR_GREY, 3, STR_NULL);
|
||||
}
|
||||
|
||||
NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
|
||||
std::unique_ptr<NWidgetBase> MakeSaturationLegendLinkGraphGUI()
|
||||
{
|
||||
NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
|
||||
auto panel = std::make_unique<NWidgetVertical>(NC_EQUALSIZE);
|
||||
for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS[0]); ++i) {
|
||||
NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
|
||||
auto wid = std::make_unique<NWidgetBackground>(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
|
||||
wid->SetMinimalSize(50, 0);
|
||||
wid->SetMinimalTextLines(1, 0, FS_SMALL);
|
||||
wid->SetFill(1, 1);
|
||||
wid->SetResize(0, 0);
|
||||
panel->Add(wid);
|
||||
panel->Add(std::move(wid));
|
||||
}
|
||||
*biggest_index = WID_LGL_SATURATION_LAST;
|
||||
return panel;
|
||||
}
|
||||
|
||||
NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
|
||||
std::unique_ptr<NWidgetBase> MakeCargoesLegendLinkGraphGUI()
|
||||
{
|
||||
static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
|
||||
NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
|
||||
NWidgetHorizontal *row = nullptr;
|
||||
for (uint i = 0; i < NUM_CARGO; ++i) {
|
||||
if (i % ENTRIES_PER_ROW == 0) {
|
||||
if (row) panel->Add(row);
|
||||
row = new NWidgetHorizontal(NC_EQUALSIZE);
|
||||
uint num_cargo = static_cast<uint>(_sorted_cargo_specs.size());
|
||||
static const uint ENTRIES_PER_COL = 5;
|
||||
auto panel = std::make_unique<NWidgetHorizontal>(NC_EQUALSIZE);
|
||||
std::unique_ptr<NWidgetVertical> col = nullptr;
|
||||
|
||||
for (uint i = 0; i < num_cargo; ++i) {
|
||||
if (i % ENTRIES_PER_COL == 0) {
|
||||
if (col != nullptr) panel->Add(std::move(col));
|
||||
col = std::make_unique<NWidgetVertical>(NC_EQUALSIZE);
|
||||
}
|
||||
NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
|
||||
auto wid = std::make_unique<NWidgetBackground>(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
|
||||
wid->SetMinimalSize(25, 0);
|
||||
wid->SetMinimalTextLines(1, 0, FS_SMALL);
|
||||
wid->SetFill(1, 1);
|
||||
wid->SetResize(0, 0);
|
||||
row->Add(wid);
|
||||
col->Add(std::move(wid));
|
||||
}
|
||||
/* Fill up last row */
|
||||
for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
|
||||
NWidgetSpacer *spc = new NWidgetSpacer(25, 0);
|
||||
for (uint i = num_cargo; i < Ceil(num_cargo, ENTRIES_PER_COL); ++i) {
|
||||
auto spc = std::make_unique<NWidgetSpacer>(25, 0);
|
||||
spc->SetMinimalTextLines(1, 0, FS_SMALL);
|
||||
spc->SetFill(1, 1);
|
||||
spc->SetResize(0, 0);
|
||||
row->Add(spc);
|
||||
col->Add(std::move(spc));
|
||||
}
|
||||
panel->Add(row);
|
||||
*biggest_index = WID_LGL_CARGO_LAST;
|
||||
/* If there are no cargo specs defined, then col won't have been created so don't add it. */
|
||||
if (col != nullptr) panel->Add(std::move(col));
|
||||
return panel;
|
||||
}
|
||||
|
||||
|
||||
static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
|
||||
static constexpr NWidgetPart _nested_linkgraph_legend_widgets[] = {
|
||||
NWidget(NWID_HORIZONTAL),
|
||||
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
|
||||
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
||||
@@ -534,11 +541,11 @@ static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
|
||||
static_assert(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
|
||||
lengthof(LinkGraphOverlay::LINK_COLOURS[0]) - 1);
|
||||
|
||||
static WindowDesc _linkgraph_legend_desc(
|
||||
static WindowDesc _linkgraph_legend_desc(__FILE__, __LINE__,
|
||||
WDP_AUTO, "toolbar_linkgraph", 0, 0,
|
||||
WC_LINKGRAPH_LEGEND, WC_NONE,
|
||||
0,
|
||||
_nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
|
||||
std::begin(_nested_linkgraph_legend_widgets), std::end(_nested_linkgraph_legend_widgets)
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -551,32 +558,33 @@ void ShowLinkGraphLegend()
|
||||
|
||||
LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
|
||||
{
|
||||
this->num_cargo = _sorted_cargo_specs.size();
|
||||
|
||||
this->InitNested(window_number);
|
||||
this->InvalidateData(0);
|
||||
this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
|
||||
this->SetOverlay(GetMainWindow()->viewport->overlay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the overlay belonging to this menu and import its company/cargo settings.
|
||||
* @param overlay New overlay for this menu.
|
||||
*/
|
||||
void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
|
||||
void LinkGraphLegendWindow::SetOverlay(std::shared_ptr<LinkGraphOverlay> overlay)
|
||||
{
|
||||
this->overlay = overlay;
|
||||
uint32 companies = this->overlay->GetCompanyMask();
|
||||
CompanyMask companies = this->overlay->GetCompanyMask();
|
||||
for (uint c = 0; c < MAX_COMPANIES; c++) {
|
||||
if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
|
||||
this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
|
||||
}
|
||||
}
|
||||
CargoTypes cargoes = this->overlay->GetCargoMask();
|
||||
for (uint c = 0; c < NUM_CARGO; c++) {
|
||||
if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
|
||||
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
|
||||
}
|
||||
for (uint c = 0; c < this->num_cargo; c++) {
|
||||
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, _sorted_cargo_specs[c]->Index()));
|
||||
}
|
||||
}
|
||||
|
||||
void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
||||
void LinkGraphLegendWindow::UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize)
|
||||
{
|
||||
if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
|
||||
StringID str = STR_NULL;
|
||||
@@ -588,27 +596,24 @@ void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const
|
||||
str = STR_LINKGRAPH_LEGEND_SATURATED;
|
||||
}
|
||||
if (str != STR_NULL) {
|
||||
Dimension dim = GetStringBoundingBox(str);
|
||||
Dimension dim = GetStringBoundingBox(str, FS_SMALL);
|
||||
dim.width += padding.width;
|
||||
dim.height += padding.height;
|
||||
*size = maxdim(*size, dim);
|
||||
}
|
||||
}
|
||||
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
|
||||
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
|
||||
if (cargo->IsValid()) {
|
||||
Dimension dim = GetStringBoundingBox(cargo->abbrev);
|
||||
dim.width += padding.width;
|
||||
dim.height += padding.height;
|
||||
*size = maxdim(*size, dim);
|
||||
}
|
||||
const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
|
||||
Dimension dim = GetStringBoundingBox(cargo->abbrev, FS_SMALL);
|
||||
dim.width += padding.width;
|
||||
dim.height += padding.height;
|
||||
*size = maxdim(*size, dim);
|
||||
}
|
||||
}
|
||||
|
||||
void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
|
||||
void LinkGraphLegendWindow::DrawWidget(const Rect &r, WidgetID widget) const
|
||||
{
|
||||
Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
|
||||
if (this->IsWidgetLowered(widget)) br = br.Translate(WidgetDimensions::scaled.pressed, WidgetDimensions::scaled.pressed);
|
||||
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
|
||||
if (this->IsWidgetDisabled(widget)) return;
|
||||
CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
|
||||
@@ -616,7 +621,7 @@ void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
|
||||
DrawCompanyIcon(cid, CenterBounds(br.left, br.right, sprite_size.width), CenterBounds(br.top, br.bottom, sprite_size.height));
|
||||
}
|
||||
if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
|
||||
uint8 colour = LinkGraphOverlay::LINK_COLOURS[_settings_client.gui.linkgraph_colours][widget - WID_LGL_SATURATION_FIRST];
|
||||
uint8_t colour = LinkGraphOverlay::LINK_COLOURS[_settings_client.gui.linkgraph_colours][widget - WID_LGL_SATURATION_FIRST];
|
||||
GfxFillRect(br, colour);
|
||||
StringID str = STR_NULL;
|
||||
if (widget == WID_LGL_SATURATION_FIRST) {
|
||||
@@ -627,37 +632,31 @@ void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
|
||||
str = STR_LINKGRAPH_LEGEND_SATURATED;
|
||||
}
|
||||
if (str != STR_NULL) {
|
||||
DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, FONT_HEIGHT_SMALL), str, GetContrastColour(colour) | TC_FORCED, SA_HOR_CENTER);
|
||||
DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, GetCharacterHeight(FS_SMALL)), str, GetContrastColour(colour) | TC_FORCED, SA_HOR_CENTER, false, FS_SMALL);
|
||||
}
|
||||
}
|
||||
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
|
||||
if (this->IsWidgetDisabled(widget)) return;
|
||||
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
|
||||
const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
|
||||
GfxFillRect(br, cargo->legend_colour);
|
||||
DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, FONT_HEIGHT_SMALL), cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER);
|
||||
DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, GetCharacterHeight(FS_SMALL)), cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER, false, FS_SMALL);
|
||||
}
|
||||
}
|
||||
|
||||
bool LinkGraphLegendWindow::OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond)
|
||||
bool LinkGraphLegendWindow::OnTooltip([[maybe_unused]] Point, WidgetID widget, TooltipCloseCondition close_cond)
|
||||
{
|
||||
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
|
||||
if (this->IsWidgetDisabled(widget)) {
|
||||
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, 0, nullptr, close_cond);
|
||||
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, close_cond);
|
||||
} else {
|
||||
uint64 params[2];
|
||||
CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
|
||||
params[0] = STR_LINKGRAPH_LEGEND_SELECT_COMPANIES;
|
||||
params[1] = cid;
|
||||
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, 2, params, close_cond);
|
||||
SetDParam(0, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
|
||||
SetDParam(1, (CompanyID)(widget - WID_LGL_COMPANY_FIRST));
|
||||
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, close_cond, 2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
|
||||
if (this->IsWidgetDisabled(widget)) return false;
|
||||
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
|
||||
uint64 params[1];
|
||||
params[0] = cargo->name;
|
||||
GuiShowTooltips(this, STR_BLACK_STRING, 1, params, close_cond);
|
||||
const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
|
||||
GuiShowTooltips(this, cargo->name, close_cond);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -668,10 +667,10 @@ bool LinkGraphLegendWindow::OnTooltip(Point pt, int widget, TooltipCloseConditio
|
||||
*/
|
||||
void LinkGraphLegendWindow::UpdateOverlayCompanies()
|
||||
{
|
||||
uint32 mask = 0;
|
||||
for (uint c = 0; c < MAX_COMPANIES; c++) {
|
||||
if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
|
||||
if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
|
||||
uint32_t mask = 0;
|
||||
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
||||
if (this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) continue;
|
||||
if (!this->IsWidgetLowered(WID_LGL_COMPANY_FIRST + c)) continue;
|
||||
SetBit(mask, c);
|
||||
}
|
||||
this->overlay->SetCompanyMask(mask);
|
||||
@@ -683,15 +682,14 @@ void LinkGraphLegendWindow::UpdateOverlayCompanies()
|
||||
void LinkGraphLegendWindow::UpdateOverlayCargoes()
|
||||
{
|
||||
CargoTypes mask = 0;
|
||||
for (uint c = 0; c < NUM_CARGO; c++) {
|
||||
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
|
||||
if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
|
||||
SetBit(mask, c);
|
||||
for (uint c = 0; c < num_cargo; c++) {
|
||||
if (!this->IsWidgetLowered(WID_LGL_CARGO_FIRST + c)) continue;
|
||||
SetBit(mask, _sorted_cargo_specs[c]->Index());
|
||||
}
|
||||
this->overlay->SetCargoMask(mask);
|
||||
}
|
||||
|
||||
void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
|
||||
void LinkGraphLegendWindow::OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count)
|
||||
{
|
||||
/* Check which button is clicked */
|
||||
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
|
||||
@@ -700,20 +698,17 @@ void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
|
||||
this->UpdateOverlayCompanies();
|
||||
}
|
||||
} else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
|
||||
for (uint c = 0; c < MAX_COMPANIES; c++) {
|
||||
if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
|
||||
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
||||
if (this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) continue;
|
||||
this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
|
||||
}
|
||||
this->UpdateOverlayCompanies();
|
||||
this->SetDirty();
|
||||
} else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
|
||||
if (!this->IsWidgetDisabled(widget)) {
|
||||
this->ToggleWidgetLoweredState(widget);
|
||||
this->UpdateOverlayCargoes();
|
||||
}
|
||||
this->ToggleWidgetLoweredState(widget);
|
||||
this->UpdateOverlayCargoes();
|
||||
} else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
|
||||
for (uint c = 0; c < NUM_CARGO; c++) {
|
||||
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
|
||||
for (uint c = 0; c < this->num_cargo; c++) {
|
||||
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
|
||||
}
|
||||
this->UpdateOverlayCargoes();
|
||||
@@ -726,13 +721,15 @@ void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
|
||||
* @param data ignored
|
||||
* @param gui_scope ignored
|
||||
*/
|
||||
void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
|
||||
void LinkGraphLegendWindow::OnInvalidateData([[maybe_unused]] int data, [[maybe_unused]] bool gui_scope)
|
||||
{
|
||||
if (this->num_cargo != _sorted_cargo_specs.size()) {
|
||||
this->Close();
|
||||
return;
|
||||
}
|
||||
|
||||
/* Disable the companies who are not active */
|
||||
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
|
||||
this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
|
||||
}
|
||||
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
||||
this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
|
||||
this->SetWidgetDisabledState(WID_LGL_COMPANY_FIRST + i, !Company::IsValidID(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,13 @@
|
||||
#include "../widget_type.h"
|
||||
#include "../window_gui.h"
|
||||
#include "linkgraph_base.h"
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Monthly statistics for a link between two stations.
|
||||
* Only the cargo type of the most saturated linkgraph is taken into account.
|
||||
*/
|
||||
struct LinkProperties {
|
||||
LinkProperties() : cargo(CT_INVALID), capacity(0), usage(0), planned(0), shared(false) {}
|
||||
LinkProperties() : cargo(INVALID_CARGO), capacity(0), usage(0), planned(0), shared(false) {}
|
||||
|
||||
/** Return the usage of the link to display. */
|
||||
uint Usage() const { return std::max(this->usage, this->planned); }
|
||||
@@ -32,7 +30,7 @@ struct LinkProperties {
|
||||
uint capacity; ///< Capacity of the link.
|
||||
uint usage; ///< Actual usage of the link.
|
||||
uint planned; ///< Planned usage of the link.
|
||||
uint32 time; ///< Travel time of the link.
|
||||
uint32_t time; ///< Travel time of the link.
|
||||
bool shared; ///< If this is a shared link to be drawn dashed.
|
||||
};
|
||||
|
||||
@@ -46,7 +44,7 @@ public:
|
||||
typedef std::map<StationID, StationLinkMap> LinkMap;
|
||||
typedef std::vector<std::pair<StationID, uint> > StationSupplyList;
|
||||
|
||||
static const uint8 LINK_COLOURS[][12];
|
||||
static const uint8_t LINK_COLOURS[][12];
|
||||
|
||||
/**
|
||||
* Create a link graph overlay for the specified window.
|
||||
@@ -56,13 +54,13 @@ public:
|
||||
* @param company_mask Bitmask of companies to be shown.
|
||||
* @param scale Desired thickness of lines and size of station dots.
|
||||
*/
|
||||
LinkGraphOverlay(Window *w, uint wid, CargoTypes cargo_mask, uint32 company_mask, uint scale) :
|
||||
LinkGraphOverlay(Window *w, WidgetID wid, CargoTypes cargo_mask, CompanyMask company_mask, uint scale) :
|
||||
window(w), widget_id(wid), cargo_mask(cargo_mask), company_mask(company_mask), scale(scale)
|
||||
{}
|
||||
|
||||
void Draw(const DrawPixelInfo *dpi);
|
||||
void SetCargoMask(CargoTypes cargo_mask);
|
||||
void SetCompanyMask(uint32 company_mask);
|
||||
void SetCompanyMask(CompanyMask company_mask);
|
||||
|
||||
bool ShowTooltip(Point pt, TooltipCloseCondition close_cond);
|
||||
|
||||
@@ -73,13 +71,13 @@ public:
|
||||
CargoTypes GetCargoMask() { return this->cargo_mask; }
|
||||
|
||||
/** Get a bitmask of the currently shown companies. */
|
||||
uint32 GetCompanyMask() { return this->company_mask; }
|
||||
CompanyMask GetCompanyMask() { return this->company_mask; }
|
||||
|
||||
protected:
|
||||
Window *window; ///< Window to be drawn into.
|
||||
const uint widget_id; ///< ID of Widget in Window to be drawn to.
|
||||
const WidgetID widget_id; ///< ID of Widget in Window to be drawn to.
|
||||
CargoTypes cargo_mask; ///< Bitmask of cargos to be displayed.
|
||||
uint32 company_mask; ///< Bitmask of companies to be displayed.
|
||||
CompanyMask company_mask; ///< Bitmask of companies to be displayed.
|
||||
LinkMap cached_links; ///< Cache for links to reduce recalculation.
|
||||
StationSupplyList cached_stations; ///< Cache for stations to be drawn.
|
||||
uint scale; ///< Width of link lines.
|
||||
@@ -96,7 +94,7 @@ protected:
|
||||
void GetWidgetDpi(DrawPixelInfo *dpi) const;
|
||||
void RebuildCache();
|
||||
|
||||
static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32 time, bool new_shared, LinkProperties &cargo);
|
||||
static void AddStats(CargoID new_cargo, uint new_cap, uint new_usg, uint new_flow, uint32_t time, bool new_shared, LinkProperties &cargo);
|
||||
static void DrawVertex(int x, int y, int size, int colour, int border_colour);
|
||||
};
|
||||
|
||||
@@ -108,16 +106,17 @@ void ShowLinkGraphLegend();
|
||||
struct LinkGraphLegendWindow : Window {
|
||||
public:
|
||||
LinkGraphLegendWindow(WindowDesc *desc, int window_number);
|
||||
void SetOverlay(LinkGraphOverlay *overlay);
|
||||
void SetOverlay(std::shared_ptr<LinkGraphOverlay> overlay);
|
||||
|
||||
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override;
|
||||
void DrawWidget(const Rect &r, int widget) const override;
|
||||
bool OnTooltip(Point pt, int widget, TooltipCloseCondition close_cond) override;
|
||||
void OnClick(Point pt, int widget, int click_count) override;
|
||||
void UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize) override;
|
||||
void DrawWidget(const Rect &r, WidgetID widget) const override;
|
||||
bool OnTooltip([[maybe_unused]] Point pt, WidgetID widget, TooltipCloseCondition close_cond) override;
|
||||
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override;
|
||||
void OnInvalidateData(int data = 0, bool gui_scope = true) override;
|
||||
|
||||
private:
|
||||
LinkGraphOverlay *overlay;
|
||||
std::shared_ptr<LinkGraphOverlay> overlay;
|
||||
size_t num_cargo;
|
||||
|
||||
void UpdateOverlayCompanies();
|
||||
void UpdateOverlayCargoes();
|
||||
|
||||
@@ -10,13 +10,13 @@
|
||||
#ifndef LINKGRAPH_TYPE_H
|
||||
#define LINKGRAPH_TYPE_H
|
||||
|
||||
typedef uint16 LinkGraphID;
|
||||
typedef uint16_t LinkGraphID;
|
||||
static const LinkGraphID INVALID_LINK_GRAPH = UINT16_MAX;
|
||||
|
||||
typedef uint16 LinkGraphJobID;
|
||||
static const LinkGraphID INVALID_LINK_GRAPH_JOB = UINT16_MAX;
|
||||
typedef uint16_t LinkGraphJobID;
|
||||
static const LinkGraphJobID INVALID_LINK_GRAPH_JOB = UINT16_MAX;
|
||||
|
||||
typedef uint16 NodeID;
|
||||
typedef uint16_t NodeID;
|
||||
static const NodeID INVALID_NODE = UINT16_MAX;
|
||||
|
||||
enum DistributionType : byte {
|
||||
@@ -31,11 +31,6 @@ enum DistributionType : byte {
|
||||
DT_END = 3
|
||||
};
|
||||
|
||||
/* It needs to be 8bits, because we save and load it as such
|
||||
* Define basic enum properties
|
||||
*/
|
||||
template <> struct EnumPropsT<DistributionType> : MakeEnumPropsT<DistributionType, byte, DT_BEGIN, DT_END, DT_NUM> {};
|
||||
|
||||
/**
|
||||
* Special modes for updating links. 'Restricted' means that vehicles with
|
||||
* 'no loading' orders are serving the link. If a link is only served by
|
||||
|
||||
@@ -37,7 +37,7 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
|
||||
* This is on purpose. */
|
||||
link_graph(orig),
|
||||
settings(_settings_game.linkgraph),
|
||||
join_date(_date + _settings_game.linkgraph.recalc_time),
|
||||
join_date(TimerGameEconomy::date + (_settings_game.linkgraph.recalc_time / EconomyTime::SECONDS_PER_DAY)),
|
||||
job_completed(false),
|
||||
job_aborted(false)
|
||||
{
|
||||
@@ -50,7 +50,7 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
|
||||
void LinkGraphJob::EraseFlows(NodeID from)
|
||||
{
|
||||
for (NodeID node_id = 0; node_id < this->Size(); ++node_id) {
|
||||
(*this)[node_id].Flows().erase(from);
|
||||
(*this)[node_id].flows.erase(from);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,12 +101,12 @@ LinkGraphJob::~LinkGraphJob()
|
||||
/* Link graph has been merged into another one. */
|
||||
if (!LinkGraph::IsValidID(this->link_graph.index)) return;
|
||||
|
||||
uint16 size = this->Size();
|
||||
uint16_t size = this->Size();
|
||||
for (NodeID node_id = 0; node_id < size; ++node_id) {
|
||||
Node from = (*this)[node_id];
|
||||
NodeAnnotation &from = this->nodes[node_id];
|
||||
|
||||
/* The station can have been deleted. Remove all flows originating from it then. */
|
||||
Station *st = Station::GetIfValid(from.Station());
|
||||
Station *st = Station::GetIfValid(from.base.station);
|
||||
if (st == nullptr) {
|
||||
this->EraseFlows(node_id);
|
||||
continue;
|
||||
@@ -121,22 +121,24 @@ LinkGraphJob::~LinkGraphJob()
|
||||
}
|
||||
|
||||
LinkGraph *lg = LinkGraph::Get(ge.link_graph);
|
||||
FlowStatMap &flows = from.Flows();
|
||||
FlowStatMap &flows = from.flows;
|
||||
|
||||
for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
|
||||
if (from[it->first].Flow() == 0) continue;
|
||||
StationID to = (*this)[it->first].Station();
|
||||
for (const auto &edge : from.edges) {
|
||||
if (edge.Flow() == 0) continue;
|
||||
NodeID dest_id = edge.base.dest_node;
|
||||
StationID to = this->nodes[dest_id].base.station;
|
||||
Station *st2 = Station::GetIfValid(to);
|
||||
if (st2 == nullptr || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
|
||||
st2->goods[this->Cargo()].node != it->first ||
|
||||
(*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
|
||||
st2->goods[this->Cargo()].node != dest_id ||
|
||||
!(*lg)[node_id].HasEdgeTo(dest_id) ||
|
||||
(*lg)[node_id][dest_id].LastUpdate() == EconomyTime::INVALID_DATE) {
|
||||
/* Edge has been removed. Delete flows. */
|
||||
StationIDStack erased = flows.DeleteFlows(to);
|
||||
/* Delete old flows for source stations which have been deleted
|
||||
* from the new flows. This avoids flow cycles between old and
|
||||
* new flows. */
|
||||
while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
|
||||
} else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
|
||||
} else if ((*lg)[node_id][dest_id].last_unrestricted_update == EconomyTime::INVALID_DATE) {
|
||||
/* Edge is fully restricted. */
|
||||
flows.RestrictFlows(to);
|
||||
}
|
||||
@@ -180,39 +182,12 @@ LinkGraphJob::~LinkGraphJob()
|
||||
void LinkGraphJob::Init()
|
||||
{
|
||||
uint size = this->Size();
|
||||
this->nodes.resize(size);
|
||||
this->edges.Resize(size, size);
|
||||
this->nodes.reserve(size);
|
||||
for (uint i = 0; i < size; ++i) {
|
||||
this->nodes[i].Init(this->link_graph[i].Supply());
|
||||
EdgeAnnotation *node_edges = this->edges[i];
|
||||
for (uint j = 0; j < size; ++j) {
|
||||
node_edges[j].Init();
|
||||
}
|
||||
this->nodes.emplace_back(this->link_graph.nodes[i], this->link_graph.Size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a linkgraph job edge.
|
||||
*/
|
||||
void LinkGraphJob::EdgeAnnotation::Init()
|
||||
{
|
||||
this->demand = 0;
|
||||
this->flow = 0;
|
||||
this->unsatisfied_demand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a Linkgraph job node. The underlying memory is expected to be
|
||||
* freshly allocated, without any constructors having been called.
|
||||
* @param supply Initial undelivered supply.
|
||||
*/
|
||||
void LinkGraphJob::NodeAnnotation::Init(uint supply)
|
||||
{
|
||||
this->undelivered_supply = supply;
|
||||
new (&this->flows) FlowStatMap;
|
||||
new (&this->paths) PathList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add this path as a new child to the given base path, thus making this path
|
||||
* a "fork" of the base path.
|
||||
@@ -246,9 +221,9 @@ void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
|
||||
uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
|
||||
{
|
||||
if (this->parent != nullptr) {
|
||||
LinkGraphJob::Edge edge = job[this->parent->node][this->node];
|
||||
LinkGraphJob::EdgeAnnotation &edge = job[this->parent->node][this->node];
|
||||
if (max_saturation != UINT_MAX) {
|
||||
uint usable_cap = edge.Capacity() * max_saturation / 100;
|
||||
uint usable_cap = edge.base.capacity * max_saturation / 100;
|
||||
if (usable_cap > edge.Flow()) {
|
||||
new_flow = std::min(new_flow, usable_cap - edge.Flow());
|
||||
} else {
|
||||
@@ -257,7 +232,7 @@ uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
|
||||
}
|
||||
new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
|
||||
if (this->flow == 0 && new_flow > 0) {
|
||||
job[this->parent->node].Paths().push_front(this);
|
||||
job[this->parent->node].paths.push_front(this);
|
||||
}
|
||||
edge.AddFlow(new_flow);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
#include "../thread.h"
|
||||
#include "linkgraph.h"
|
||||
#include <list>
|
||||
#include <atomic>
|
||||
|
||||
class LinkGraphJob;
|
||||
@@ -28,88 +27,36 @@ extern LinkGraphJobPool _link_graph_job_pool;
|
||||
* Class for calculation jobs to be run on link graphs.
|
||||
*/
|
||||
class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{
|
||||
private:
|
||||
public:
|
||||
/**
|
||||
* Demand between two nodes.
|
||||
*/
|
||||
struct DemandAnnotation {
|
||||
uint demand; ///< Transport demand between the nodes.
|
||||
uint unsatisfied_demand; ///< Demand over this edge that hasn't been satisfied yet.
|
||||
};
|
||||
|
||||
/**
|
||||
* Annotation for a link graph edge.
|
||||
*/
|
||||
struct EdgeAnnotation {
|
||||
uint demand; ///< Transport demand between the nodes.
|
||||
uint unsatisfied_demand; ///< Demand over this edge that hasn't been satisfied yet.
|
||||
const LinkGraph::BaseEdge &base; ///< Reference to the edge that is annotated.
|
||||
|
||||
uint flow; ///< Planned flow over this edge.
|
||||
void Init();
|
||||
};
|
||||
|
||||
/**
|
||||
* Annotation for a link graph node.
|
||||
*/
|
||||
struct NodeAnnotation {
|
||||
uint undelivered_supply; ///< Amount of supply that hasn't been distributed yet.
|
||||
PathList paths; ///< Paths through this node, sorted so that those with flow == 0 are in the back.
|
||||
FlowStatMap flows; ///< Planned flows to other nodes.
|
||||
void Init(uint supply);
|
||||
};
|
||||
|
||||
typedef std::vector<NodeAnnotation> NodeAnnotationVector;
|
||||
typedef SmallMatrix<EdgeAnnotation> EdgeAnnotationMatrix;
|
||||
|
||||
friend SaveLoadTable GetLinkGraphJobDesc();
|
||||
friend class LinkGraphSchedule;
|
||||
|
||||
protected:
|
||||
const LinkGraph link_graph; ///< Link graph to by analyzed. Is copied when job is started and mustn't be modified later.
|
||||
const LinkGraphSettings settings; ///< Copy of _settings_game.linkgraph at spawn time.
|
||||
std::thread thread; ///< Thread the job is running in or a default-constructed thread if it's running in the main thread.
|
||||
Date join_date; ///< Date when the job is to be joined.
|
||||
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation.
|
||||
EdgeAnnotationMatrix edges; ///< Extra edge data necessary for link graph calculation.
|
||||
std::atomic<bool> job_completed; ///< Is the job still running. This is accessed by multiple threads and reads may be stale.
|
||||
std::atomic<bool> job_aborted; ///< Has the job been aborted. This is accessed by multiple threads and reads may be stale.
|
||||
|
||||
void EraseFlows(NodeID from);
|
||||
void JoinThread();
|
||||
void SpawnThread();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* A job edge. Wraps a link graph edge and an edge annotation. The
|
||||
* annotation can be modified, the edge is constant.
|
||||
*/
|
||||
class Edge : public LinkGraph::ConstEdge {
|
||||
private:
|
||||
EdgeAnnotation &anno; ///< Annotation being wrapped.
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param edge Link graph edge to be wrapped.
|
||||
* @param anno Annotation to be wrapped.
|
||||
*/
|
||||
Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno) :
|
||||
LinkGraph::ConstEdge(edge), anno(anno) {}
|
||||
|
||||
/**
|
||||
* Get the transport demand between end the points of the edge.
|
||||
* @return Demand.
|
||||
*/
|
||||
uint Demand() const { return this->anno.demand; }
|
||||
|
||||
/**
|
||||
* Get the transport demand that hasn't been satisfied by flows, yet.
|
||||
* @return Unsatisfied demand.
|
||||
*/
|
||||
uint UnsatisfiedDemand() const { return this->anno.unsatisfied_demand; }
|
||||
EdgeAnnotation(const LinkGraph::BaseEdge &base) : base(base), flow(0) {}
|
||||
|
||||
/**
|
||||
* Get the total flow on the edge.
|
||||
* @return Flow.
|
||||
*/
|
||||
uint Flow() const { return this->anno.flow; }
|
||||
uint Flow() const { return this->flow; }
|
||||
|
||||
/**
|
||||
* Add some flow.
|
||||
* @param flow Flow to be added.
|
||||
*/
|
||||
void AddFlow(uint flow) { this->anno.flow += flow; }
|
||||
void AddFlow(uint flow) { this->flow += flow; }
|
||||
|
||||
/**
|
||||
* Remove some flow.
|
||||
@@ -117,139 +64,81 @@ public:
|
||||
*/
|
||||
void RemoveFlow(uint flow)
|
||||
{
|
||||
assert(flow <= this->anno.flow);
|
||||
this->anno.flow -= flow;
|
||||
assert(flow <= this->flow);
|
||||
this->flow -= flow;
|
||||
}
|
||||
|
||||
friend inline bool operator <(NodeID dest, const EdgeAnnotation &rhs)
|
||||
{
|
||||
return dest < rhs.base.dest_node;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Annotation for a link graph node.
|
||||
*/
|
||||
struct NodeAnnotation {
|
||||
const LinkGraph::BaseNode &base; ///< Reference to the node that is annotated.
|
||||
|
||||
uint undelivered_supply; ///< Amount of supply that hasn't been distributed yet.
|
||||
PathList paths; ///< Paths through this node, sorted so that those with flow == 0 are in the back.
|
||||
FlowStatMap flows; ///< Planned flows to other nodes.
|
||||
|
||||
std::vector<EdgeAnnotation> edges; ///< Annotations for all edges originating at this node.
|
||||
std::vector<DemandAnnotation> demands; ///< Annotations for the demand to all other nodes.
|
||||
|
||||
NodeAnnotation(const LinkGraph::BaseNode &node, size_t size) : base(node), undelivered_supply(node.supply), paths(), flows()
|
||||
{
|
||||
this->edges.reserve(node.edges.size());
|
||||
for (auto &e : node.edges) this->edges.emplace_back(e);
|
||||
this->demands.resize(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some (not yet satisfied) demand.
|
||||
* @param demand Demand to be added.
|
||||
* Retrieve an edge starting at this node.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
void AddDemand(uint demand)
|
||||
EdgeAnnotation &operator[](NodeID to)
|
||||
{
|
||||
this->anno.demand += demand;
|
||||
this->anno.unsatisfied_demand += demand;
|
||||
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
|
||||
assert(it != this->edges.end());
|
||||
return *it;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an edge starting at this node.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
const EdgeAnnotation &operator[](NodeID to) const
|
||||
{
|
||||
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
|
||||
assert(it != this->edges.end());
|
||||
return *it;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transport demand between end the points of the edge.
|
||||
* @return Demand.
|
||||
*/
|
||||
uint DemandTo(NodeID to) const { return this->demands[to].demand; }
|
||||
|
||||
/**
|
||||
* Get the transport demand that hasn't been satisfied by flows, yet.
|
||||
* @return Unsatisfied demand.
|
||||
*/
|
||||
uint UnsatisfiedDemandTo(NodeID to) const { return this->demands[to].unsatisfied_demand; }
|
||||
|
||||
/**
|
||||
* Satisfy some demand.
|
||||
* @param demand Demand to be satisfied.
|
||||
*/
|
||||
void SatisfyDemand(uint demand)
|
||||
void SatisfyDemandTo(NodeID to, uint demand)
|
||||
{
|
||||
assert(demand <= this->anno.unsatisfied_demand);
|
||||
this->anno.unsatisfied_demand -= demand;
|
||||
assert(demand <= this->demands[to].unsatisfied_demand);
|
||||
this->demands[to].unsatisfied_demand -= demand;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Iterator for job edges.
|
||||
*/
|
||||
class EdgeIterator : public LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator> {
|
||||
EdgeAnnotation *base_anno; ///< Array of annotations to be (indirectly) iterated.
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param base Array of edges to be iterated.
|
||||
* @param base_anno Array of annotations to be iterated.
|
||||
* @param current Start offset of iteration.
|
||||
*/
|
||||
EdgeIterator(const LinkGraph::BaseEdge *base, EdgeAnnotation *base_anno, NodeID current) :
|
||||
LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator>(base, current),
|
||||
base_anno(base_anno) {}
|
||||
|
||||
/**
|
||||
* Dereference.
|
||||
* @return Pair of the edge currently pointed to and the ID of its
|
||||
* other end.
|
||||
*/
|
||||
std::pair<NodeID, Edge> operator*() const
|
||||
{
|
||||
return std::pair<NodeID, Edge>(this->current, Edge(this->base[this->current], this->base_anno[this->current]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereference. Has to be repeated here as operator* is different than
|
||||
* in LinkGraph::EdgeWrapper.
|
||||
* @return Fake pointer to pair of NodeID/Edge.
|
||||
*/
|
||||
FakePointer operator->() const {
|
||||
return FakePointer(this->operator*());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Link graph job node. Wraps a constant link graph node and a modifiable
|
||||
* node annotation.
|
||||
*/
|
||||
class Node : public LinkGraph::ConstNode {
|
||||
private:
|
||||
NodeAnnotation &node_anno; ///< Annotation being wrapped.
|
||||
EdgeAnnotation *edge_annos; ///< Edge annotations belonging to this node.
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param lgj Job to take the node from.
|
||||
* @param node ID of the node.
|
||||
*/
|
||||
Node (LinkGraphJob *lgj, NodeID node) :
|
||||
LinkGraph::ConstNode(&lgj->link_graph, node),
|
||||
node_anno(lgj->nodes[node]), edge_annos(lgj->edges[node])
|
||||
{}
|
||||
|
||||
/**
|
||||
* Retrieve an edge starting at this node. Mind that this returns an
|
||||
* object, not a reference.
|
||||
* @param to Remote end of the edge.
|
||||
* @return Edge between this node and "to".
|
||||
*/
|
||||
Edge operator[](NodeID to) const { return Edge(this->edges[to], this->edge_annos[to]); }
|
||||
|
||||
/**
|
||||
* Iterator for the "begin" of the edge array. Only edges with capacity
|
||||
* are iterated. The others are skipped.
|
||||
* @return Iterator pointing to the first edge.
|
||||
*/
|
||||
EdgeIterator Begin() const { return EdgeIterator(this->edges, this->edge_annos, index); }
|
||||
|
||||
/**
|
||||
* Iterator for the "end" of the edge array. Only edges with capacity
|
||||
* are iterated. The others are skipped.
|
||||
* @return Iterator pointing beyond the last edge.
|
||||
*/
|
||||
EdgeIterator End() const { return EdgeIterator(this->edges, this->edge_annos, INVALID_NODE); }
|
||||
|
||||
/**
|
||||
* Get amount of supply that hasn't been delivered, yet.
|
||||
* @return Undelivered supply.
|
||||
*/
|
||||
uint UndeliveredSupply() const { return this->node_anno.undelivered_supply; }
|
||||
|
||||
/**
|
||||
* Get the flows running through this node.
|
||||
* @return Flows.
|
||||
*/
|
||||
FlowStatMap &Flows() { return this->node_anno.flows; }
|
||||
|
||||
/**
|
||||
* Get a constant version of the flows running through this node.
|
||||
* @return Flows.
|
||||
*/
|
||||
const FlowStatMap &Flows() const { return this->node_anno.flows; }
|
||||
|
||||
/**
|
||||
* Get the paths this node is part of. Paths are always expected to be
|
||||
* sorted so that those with flow == 0 are in the back of the list.
|
||||
* @return Paths.
|
||||
*/
|
||||
PathList &Paths() { return this->node_anno.paths; }
|
||||
|
||||
/**
|
||||
* Get a constant version of the paths this node is part of.
|
||||
* @return Paths.
|
||||
*/
|
||||
const PathList &Paths() const { return this->node_anno.paths; }
|
||||
|
||||
/**
|
||||
* Deliver some supply, adding demand to the respective edge.
|
||||
@@ -258,17 +147,38 @@ public:
|
||||
*/
|
||||
void DeliverSupply(NodeID to, uint amount)
|
||||
{
|
||||
this->node_anno.undelivered_supply -= amount;
|
||||
(*this)[to].AddDemand(amount);
|
||||
this->undelivered_supply -= amount;
|
||||
this->demands[to].demand += amount;
|
||||
this->demands[to].unsatisfied_demand += amount;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
typedef std::vector<NodeAnnotation> NodeAnnotationVector;
|
||||
|
||||
friend SaveLoadTable GetLinkGraphJobDesc();
|
||||
friend class LinkGraphSchedule;
|
||||
|
||||
protected:
|
||||
const LinkGraph link_graph; ///< Link graph to by analyzed. Is copied when job is started and mustn't be modified later.
|
||||
const LinkGraphSettings settings; ///< Copy of _settings_game.linkgraph at spawn time.
|
||||
std::thread thread; ///< Thread the job is running in or a default-constructed thread if it's running in the main thread.
|
||||
TimerGameEconomy::Date join_date; ///< Date when the job is to be joined.
|
||||
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation.
|
||||
std::atomic<bool> job_completed; ///< Is the job still running. This is accessed by multiple threads and reads may be stale.
|
||||
std::atomic<bool> job_aborted; ///< Has the job been aborted. This is accessed by multiple threads and reads may be stale.
|
||||
|
||||
void EraseFlows(NodeID from);
|
||||
void JoinThread();
|
||||
void SpawnThread();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Bare constructor, only for save/load. link_graph, join_date and actually
|
||||
* settings have to be brutally const-casted in order to populate them.
|
||||
*/
|
||||
LinkGraphJob() : settings(_settings_game.linkgraph),
|
||||
join_date(INVALID_DATE), job_completed(false), job_aborted(false) {}
|
||||
join_date(EconomyTime::INVALID_DATE), job_completed(false), job_aborted(false) {}
|
||||
|
||||
LinkGraphJob(const LinkGraph &orig);
|
||||
~LinkGraphJob();
|
||||
@@ -301,19 +211,19 @@ public:
|
||||
* Check if job is supposed to be finished.
|
||||
* @return True if job should be finished by now, false if not.
|
||||
*/
|
||||
inline bool IsScheduledToBeJoined() const { return this->join_date <= _date; }
|
||||
inline bool IsScheduledToBeJoined() const { return this->join_date <= TimerGameEconomy::date; }
|
||||
|
||||
/**
|
||||
* Get the date when the job should be finished.
|
||||
* @return Join date.
|
||||
*/
|
||||
inline Date JoinDate() const { return join_date; }
|
||||
inline TimerGameEconomy::Date JoinDate() const { return join_date; }
|
||||
|
||||
/**
|
||||
* Change the join date on date cheating.
|
||||
* @param interval Number of days to add.
|
||||
*/
|
||||
inline void ShiftJoinDate(int interval) { this->join_date += interval; }
|
||||
inline void ShiftJoinDate(TimerGameEconomy::Date interval) { this->join_date += interval; }
|
||||
|
||||
/**
|
||||
* Get the link graph settings for this component.
|
||||
@@ -326,7 +236,7 @@ public:
|
||||
* @param num ID of the node.
|
||||
* @return the Requested node.
|
||||
*/
|
||||
inline Node operator[](NodeID num) { return Node(this, num); }
|
||||
inline NodeAnnotation &operator[](NodeID num) { return this->nodes[num]; }
|
||||
|
||||
/**
|
||||
* Get the size of the underlying link graph.
|
||||
@@ -344,7 +254,7 @@ public:
|
||||
* Get the date when the underlying link graph was last compressed.
|
||||
* @return Compression date.
|
||||
*/
|
||||
inline Date LastCompression() const { return this->link_graph.LastCompression(); }
|
||||
inline TimerGameEconomy::Date LastCompression() const { return this->link_graph.LastCompression(); }
|
||||
|
||||
/**
|
||||
* Get the ID of the underlying link graph.
|
||||
|
||||
@@ -14,8 +14,7 @@
|
||||
#include "linkgraphjob.h"
|
||||
#include "linkgraphschedule.h"
|
||||
|
||||
typedef LinkGraphJob::Node Node;
|
||||
typedef LinkGraphJob::Edge Edge;
|
||||
typedef LinkGraphJob::EdgeIterator EdgeIterator;
|
||||
typedef LinkGraphJob::NodeAnnotation Node;
|
||||
typedef LinkGraphJob::EdgeAnnotation Edge;
|
||||
|
||||
#endif /* LINKGRAPHJOB_BASE_H */
|
||||
|
||||
@@ -110,8 +110,8 @@ void LinkGraphSchedule::JoinNext()
|
||||
*/
|
||||
void LinkGraphSchedule::SpawnAll()
|
||||
{
|
||||
for (JobList::iterator i = this->running.begin(); i != this->running.end(); ++i) {
|
||||
(*i)->SpawnThread();
|
||||
for (auto &it : this->running) {
|
||||
it->SpawnThread();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +120,8 @@ void LinkGraphSchedule::SpawnAll()
|
||||
*/
|
||||
/* static */ void LinkGraphSchedule::Clear()
|
||||
{
|
||||
for (JobList::iterator i(instance.running.begin()); i != instance.running.end(); ++i) {
|
||||
(*i)->AbortJob();
|
||||
for (auto &it : instance.running) {
|
||||
it->AbortJob();
|
||||
}
|
||||
instance.running.clear();
|
||||
instance.schedule.clear();
|
||||
@@ -132,7 +132,7 @@ void LinkGraphSchedule::SpawnAll()
|
||||
* graph jobs by the number of days given.
|
||||
* @param interval Number of days to be added or subtracted.
|
||||
*/
|
||||
void LinkGraphSchedule::ShiftDates(int interval)
|
||||
void LinkGraphSchedule::ShiftDates(TimerGameEconomy::Date interval)
|
||||
{
|
||||
for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(interval);
|
||||
for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) lgj->ShiftJoinDate(interval);
|
||||
@@ -163,10 +163,10 @@ LinkGraphSchedule::~LinkGraphSchedule()
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the game if in 2 _date_fract ticks, we would do a join with the next
|
||||
* Pause the game if in 2 TimerGameEconomy::date_fract ticks, we would do a join with the next
|
||||
* link graph job, but it is still running.
|
||||
* The check is done 2 _date_fract ticks early instead of 1, as in multiplayer
|
||||
* calls to DoCommandP are executed after a delay of 1 _date_fract tick.
|
||||
* The check is done 2 TimerGameEconomy::date_fract ticks early instead of 1, as in multiplayer
|
||||
* calls to DoCommandP are executed after a delay of 1 TimerGameEconomy::date_fract tick.
|
||||
* If we previously paused, unpause if the job is now ready to be joined with.
|
||||
*/
|
||||
void StateGameLoop_LinkGraphPauseControl()
|
||||
@@ -177,10 +177,10 @@ void StateGameLoop_LinkGraphPauseControl()
|
||||
Command<CMD_PAUSE>::Post(PM_PAUSED_LINK_GRAPH, false);
|
||||
}
|
||||
} else if (_pause_mode == PM_UNPAUSED &&
|
||||
_date_fract == LinkGraphSchedule::SPAWN_JOIN_TICK - 2 &&
|
||||
_date % _settings_game.linkgraph.recalc_interval == _settings_game.linkgraph.recalc_interval / 2 &&
|
||||
TimerGameEconomy::date_fract == LinkGraphSchedule::SPAWN_JOIN_TICK - 2 &&
|
||||
TimerGameEconomy::date.base() % (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) == (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) / 2 &&
|
||||
LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
|
||||
/* Perform check two _date_fract ticks before we would join, to make
|
||||
/* Perform check two TimerGameEconomy::date_fract ticks before we would join, to make
|
||||
* sure it also works in multiplayer. */
|
||||
Command<CMD_PAUSE>::Post(PM_PAUSED_LINK_GRAPH, true);
|
||||
}
|
||||
@@ -204,11 +204,11 @@ void AfterLoad_LinkGraphPauseControl()
|
||||
*/
|
||||
void OnTick_LinkGraph()
|
||||
{
|
||||
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
|
||||
Date offset = _date % _settings_game.linkgraph.recalc_interval;
|
||||
if (TimerGameEconomy::date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
|
||||
TimerGameEconomy::Date offset = TimerGameEconomy::date.base() % (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY);
|
||||
if (offset == 0) {
|
||||
LinkGraphSchedule::instance.SpawnNext();
|
||||
} else if (offset == _settings_game.linkgraph.recalc_interval / 2) {
|
||||
} else if (offset == (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) / 2) {
|
||||
if (!_networking || _network_server) {
|
||||
PerformanceMeasurer::SetInactive(PFE_GL_LINKGRAPH);
|
||||
LinkGraphSchedule::instance.JoinNext();
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
/**
|
||||
* Destroy the handler. Must be given due to virtual Run.
|
||||
*/
|
||||
virtual ~ComponentHandler() {}
|
||||
virtual ~ComponentHandler() = default;
|
||||
|
||||
/**
|
||||
* Run the handler. A link graph handler must not read or write any data
|
||||
@@ -58,7 +58,7 @@ public:
|
||||
bool IsJoinWithUnfinishedJobDue() const;
|
||||
void JoinNext();
|
||||
void SpawnAll();
|
||||
void ShiftDates(int interval);
|
||||
void ShiftDates(TimerGameEconomy::Date interval);
|
||||
|
||||
/**
|
||||
* Queue a link graph for execution.
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../core/math_func.hpp"
|
||||
#include "../timer/timer_game_tick.h"
|
||||
#include "mcf.h"
|
||||
#include <set>
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
@@ -94,8 +94,9 @@ public:
|
||||
class GraphEdgeIterator {
|
||||
private:
|
||||
LinkGraphJob &job; ///< Job being executed
|
||||
EdgeIterator i; ///< Iterator pointing to current edge.
|
||||
EdgeIterator end; ///< Iterator pointing beyond last edge.
|
||||
|
||||
std::vector<LinkGraphJob::EdgeAnnotation>::const_iterator i; ///< Iterator pointing to current edge.
|
||||
std::vector<LinkGraphJob::EdgeAnnotation>::const_iterator end; ///< Iterator pointing beyond last edge.
|
||||
|
||||
public:
|
||||
|
||||
@@ -103,19 +104,16 @@ public:
|
||||
* Construct a GraphEdgeIterator.
|
||||
* @param job Job to iterate on.
|
||||
*/
|
||||
GraphEdgeIterator(LinkGraphJob &job) : job(job),
|
||||
i(nullptr, nullptr, INVALID_NODE), end(nullptr, nullptr, INVALID_NODE)
|
||||
{}
|
||||
GraphEdgeIterator(LinkGraphJob &job) : job(job), i(), end() {}
|
||||
|
||||
/**
|
||||
* Setup the node to start iterating at.
|
||||
* @param source Unused.
|
||||
* @param node Node to start iterating at.
|
||||
*/
|
||||
void SetNode(NodeID source, NodeID node)
|
||||
void SetNode(NodeID, NodeID node)
|
||||
{
|
||||
this->i = this->job[node].Begin();
|
||||
this->end = this->job[node].End();
|
||||
this->i = this->job[node].edges.cbegin();
|
||||
this->end = this->job[node].edges.cend();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,7 +122,7 @@ public:
|
||||
*/
|
||||
NodeID Next()
|
||||
{
|
||||
return this->i != this->end ? (this->i++)->first : INVALID_NODE;
|
||||
return this->i != this->end ? (this->i++)->base.dest_node : INVALID_NODE;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -152,7 +150,7 @@ public:
|
||||
FlowEdgeIterator(LinkGraphJob &job) : job(job)
|
||||
{
|
||||
for (NodeID i = 0; i < job.Size(); ++i) {
|
||||
StationID st = job[i].Station();
|
||||
StationID st = job[i].base.station;
|
||||
if (st >= this->station_to_node.size()) {
|
||||
this->station_to_node.resize(st + 1);
|
||||
}
|
||||
@@ -167,8 +165,8 @@ public:
|
||||
*/
|
||||
void SetNode(NodeID source, NodeID node)
|
||||
{
|
||||
const FlowStatMap &flows = this->job[node].Flows();
|
||||
FlowStatMap::const_iterator it = flows.find(this->job[source].Station());
|
||||
const FlowStatMap &flows = this->job[node].flows;
|
||||
FlowStatMap::const_iterator it = flows.find(this->job[source].base.station);
|
||||
if (it != flows.end()) {
|
||||
this->it = it->second.GetShares()->begin();
|
||||
this->end = it->second.GetShares()->end();
|
||||
@@ -198,7 +196,7 @@ public:
|
||||
* @return True if base + the new edge would be better than the path associated
|
||||
* with this annotation.
|
||||
*/
|
||||
bool DistanceAnnotation::IsBetter(const DistanceAnnotation *base, uint cap,
|
||||
bool DistanceAnnotation::IsBetter(const DistanceAnnotation *base, uint,
|
||||
int free_cap, uint dist) const
|
||||
{
|
||||
/* If any of the paths is disconnected, the other one is better. If both
|
||||
@@ -260,7 +258,7 @@ void MultiCommodityFlow::Dijkstra(NodeID source_node, PathVector &paths)
|
||||
{
|
||||
typedef std::set<Tannotation *, typename Tannotation::Comparator> AnnoSet;
|
||||
Tedge_iterator iter(this->job);
|
||||
uint16 size = this->job.Size();
|
||||
uint16_t size = this->job.Size();
|
||||
AnnoSet annos;
|
||||
paths.resize(size, nullptr);
|
||||
for (NodeID node = 0; node < size; ++node) {
|
||||
@@ -277,8 +275,8 @@ void MultiCommodityFlow::Dijkstra(NodeID source_node, PathVector &paths)
|
||||
iter.SetNode(source_node, from);
|
||||
for (NodeID to = iter.Next(); to != INVALID_NODE; to = iter.Next()) {
|
||||
if (to == from) continue; // Not a real edge but a consumption sign.
|
||||
Edge edge = this->job[from][to];
|
||||
uint capacity = edge.Capacity();
|
||||
const Edge &edge = this->job[from][to];
|
||||
uint capacity = edge.base.capacity;
|
||||
if (this->max_saturation != UINT_MAX) {
|
||||
capacity *= this->max_saturation;
|
||||
capacity /= 100;
|
||||
@@ -290,9 +288,9 @@ void MultiCommodityFlow::Dijkstra(NodeID source_node, PathVector &paths)
|
||||
bool express = IsCargoInClass(this->job.Cargo(), CC_PASSENGERS) ||
|
||||
IsCargoInClass(this->job.Cargo(), CC_MAIL) ||
|
||||
IsCargoInClass(this->job.Cargo(), CC_EXPRESS);
|
||||
uint distance = DistanceMaxPlusManhattan(this->job[from].XY(), this->job[to].XY()) + 1;
|
||||
uint distance = DistanceMaxPlusManhattan(this->job[from].base.xy, this->job[to].base.xy) + 1;
|
||||
/* Compute a default travel time from the distance and an average speed of 1 tile/day. */
|
||||
uint time = (edge.TravelTime() != 0) ? edge.TravelTime() + DAY_TICKS : distance * DAY_TICKS;
|
||||
uint time = (edge.base.TravelTime() != 0) ? edge.base.TravelTime() + Ticks::DAY_TICKS : distance * Ticks::DAY_TICKS;
|
||||
uint distance_anno = express ? time : distance;
|
||||
|
||||
Tannotation *dest = static_cast<Tannotation *>(paths[to]);
|
||||
@@ -336,19 +334,20 @@ void MultiCommodityFlow::CleanupPaths(NodeID source_id, PathVector &paths)
|
||||
/**
|
||||
* Push flow along a path and update the unsatisfied_demand of the associated
|
||||
* edge.
|
||||
* @param edge Edge whose ends the path connects.
|
||||
* @param node Node where the path starts.
|
||||
* @param to Node where the path ends.
|
||||
* @param path End of the path the flow should be pushed on.
|
||||
* @param accuracy Accuracy of the calculation.
|
||||
* @param max_saturation If < UINT_MAX only push flow up to the given
|
||||
* saturation, otherwise the path can be "overloaded".
|
||||
*/
|
||||
uint MultiCommodityFlow::PushFlow(Edge &edge, Path *path, uint accuracy,
|
||||
uint MultiCommodityFlow::PushFlow(Node &node, NodeID to, Path *path, uint accuracy,
|
||||
uint max_saturation)
|
||||
{
|
||||
assert(edge.UnsatisfiedDemand() > 0);
|
||||
uint flow = Clamp(edge.Demand() / accuracy, 1, edge.UnsatisfiedDemand());
|
||||
assert(node.UnsatisfiedDemandTo(to) > 0);
|
||||
uint flow = Clamp(node.DemandTo(to) / accuracy, 1, node.UnsatisfiedDemandTo(to));
|
||||
flow = path->AddFlow(flow, this->job, max_saturation);
|
||||
edge.SatisfyDemand(flow);
|
||||
node.SatisfyDemandTo(to, flow);
|
||||
return flow;
|
||||
}
|
||||
|
||||
@@ -382,7 +381,7 @@ void MCF1stPass::EliminateCycle(PathVector &path, Path *cycle_begin, uint flow)
|
||||
NodeID prev = cycle_begin->GetNode();
|
||||
cycle_begin->ReduceFlow(flow);
|
||||
if (cycle_begin->GetFlow() == 0) {
|
||||
PathList &node_paths = this->job[cycle_begin->GetParent()->GetNode()].Paths();
|
||||
PathList &node_paths = this->job[cycle_begin->GetParent()->GetNode()].paths;
|
||||
for (PathList::iterator i = node_paths.begin(); i != node_paths.end(); ++i) {
|
||||
if (*i == cycle_begin) {
|
||||
node_paths.erase(i);
|
||||
@@ -392,7 +391,7 @@ void MCF1stPass::EliminateCycle(PathVector &path, Path *cycle_begin, uint flow)
|
||||
}
|
||||
}
|
||||
cycle_begin = path[prev];
|
||||
Edge edge = this->job[prev][cycle_begin->GetNode()];
|
||||
Edge &edge = this->job[prev][cycle_begin->GetNode()];
|
||||
edge.RemoveFlow(flow);
|
||||
} while (cycle_begin != cycle_end);
|
||||
}
|
||||
@@ -416,7 +415,7 @@ bool MCF1stPass::EliminateCycles(PathVector &path, NodeID origin_id, NodeID next
|
||||
if (at_next_pos == nullptr) {
|
||||
/* Summarize paths; add up the paths with the same source and next hop
|
||||
* in one path each. */
|
||||
PathList &paths = this->job[next_id].Paths();
|
||||
PathList &paths = this->job[next_id].paths;
|
||||
PathViaMap next_hops;
|
||||
for (PathList::iterator i = paths.begin(); i != paths.end();) {
|
||||
Path *new_child = *i;
|
||||
@@ -482,7 +481,7 @@ bool MCF1stPass::EliminateCycles(PathVector &path, NodeID origin_id, NodeID next
|
||||
bool MCF1stPass::EliminateCycles()
|
||||
{
|
||||
bool cycles_found = false;
|
||||
uint16 size = this->job.Size();
|
||||
uint16_t size = this->job.Size();
|
||||
PathVector path(size, nullptr);
|
||||
for (NodeID node = 0; node < size; ++node) {
|
||||
/* Starting at each node in the graph find all cycles involving this
|
||||
@@ -500,7 +499,7 @@ bool MCF1stPass::EliminateCycles()
|
||||
MCF1stPass::MCF1stPass(LinkGraphJob &job) : MultiCommodityFlow(job)
|
||||
{
|
||||
PathVector paths;
|
||||
uint16 size = job.Size();
|
||||
uint16_t size = job.Size();
|
||||
uint accuracy = job.Settings().accuracy;
|
||||
bool more_loops;
|
||||
std::vector<bool> finished_sources(size);
|
||||
@@ -513,25 +512,25 @@ MCF1stPass::MCF1stPass(LinkGraphJob &job) : MultiCommodityFlow(job)
|
||||
/* First saturate the shortest paths. */
|
||||
this->Dijkstra<DistanceAnnotation, GraphEdgeIterator>(source, paths);
|
||||
|
||||
Node &src_node = job[source];
|
||||
bool source_demand_left = false;
|
||||
for (NodeID dest = 0; dest < size; ++dest) {
|
||||
Edge edge = job[source][dest];
|
||||
if (edge.UnsatisfiedDemand() > 0) {
|
||||
if (src_node.UnsatisfiedDemandTo(dest) > 0) {
|
||||
Path *path = paths[dest];
|
||||
assert(path != nullptr);
|
||||
/* Generally only allow paths that don't exceed the
|
||||
* available capacity. But if no demand has been assigned
|
||||
* yet, make an exception and allow any valid path *once*. */
|
||||
if (path->GetFreeCapacity() > 0 && this->PushFlow(edge, path,
|
||||
if (path->GetFreeCapacity() > 0 && this->PushFlow(src_node, dest, path,
|
||||
accuracy, this->max_saturation) > 0) {
|
||||
/* If a path has been found there is a chance we can
|
||||
* find more. */
|
||||
more_loops = more_loops || (edge.UnsatisfiedDemand() > 0);
|
||||
} else if (edge.UnsatisfiedDemand() == edge.Demand() &&
|
||||
more_loops = more_loops || (src_node.UnsatisfiedDemandTo(dest) > 0);
|
||||
} else if (src_node.UnsatisfiedDemandTo(dest) == src_node.DemandTo(dest) &&
|
||||
path->GetFreeCapacity() > INT_MIN) {
|
||||
this->PushFlow(edge, path, accuracy, UINT_MAX);
|
||||
this->PushFlow(src_node, dest, path, accuracy, UINT_MAX);
|
||||
}
|
||||
if (edge.UnsatisfiedDemand() > 0) source_demand_left = true;
|
||||
if (src_node.UnsatisfiedDemandTo(dest) > 0) source_demand_left = true;
|
||||
}
|
||||
}
|
||||
finished_sources[source] = !source_demand_left;
|
||||
@@ -549,7 +548,7 @@ MCF2ndPass::MCF2ndPass(LinkGraphJob &job) : MultiCommodityFlow(job)
|
||||
{
|
||||
this->max_saturation = UINT_MAX; // disable artificial cap on saturation
|
||||
PathVector paths;
|
||||
uint16 size = job.Size();
|
||||
uint16_t size = job.Size();
|
||||
uint accuracy = job.Settings().accuracy;
|
||||
bool demand_left = true;
|
||||
std::vector<bool> finished_sources(size);
|
||||
@@ -560,13 +559,13 @@ MCF2ndPass::MCF2ndPass(LinkGraphJob &job) : MultiCommodityFlow(job)
|
||||
|
||||
this->Dijkstra<CapacityAnnotation, FlowEdgeIterator>(source, paths);
|
||||
|
||||
Node &src_node = job[source];
|
||||
bool source_demand_left = false;
|
||||
for (NodeID dest = 0; dest < size; ++dest) {
|
||||
Edge edge = this->job[source][dest];
|
||||
Path *path = paths[dest];
|
||||
if (edge.UnsatisfiedDemand() > 0 && path->GetFreeCapacity() > INT_MIN) {
|
||||
this->PushFlow(edge, path, accuracy, UINT_MAX);
|
||||
if (edge.UnsatisfiedDemand() > 0) {
|
||||
if (src_node.UnsatisfiedDemandTo(dest) > 0 && path->GetFreeCapacity() > INT_MIN) {
|
||||
this->PushFlow(src_node, dest, path, accuracy, UINT_MAX);
|
||||
if (src_node.UnsatisfiedDemandTo(dest) > 0) {
|
||||
demand_left = true;
|
||||
source_demand_left = true;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#define MCF_H
|
||||
|
||||
#include "linkgraphjob_base.h"
|
||||
#include <vector>
|
||||
|
||||
typedef std::vector<Path *> PathVector;
|
||||
|
||||
@@ -24,7 +23,7 @@ protected:
|
||||
template<class Tannotation, class Tedge_iterator>
|
||||
void Dijkstra(NodeID from, PathVector &paths);
|
||||
|
||||
uint PushFlow(Edge &edge, Path *path, uint accuracy, uint max_saturation);
|
||||
uint PushFlow(Node &node, NodeID to, Path *path, uint accuracy, uint max_saturation);
|
||||
|
||||
void CleanupPaths(NodeID source, PathVector &paths);
|
||||
|
||||
@@ -81,12 +80,7 @@ public:
|
||||
* Run the calculation.
|
||||
* @param graph Component to be calculated.
|
||||
*/
|
||||
virtual void Run(LinkGraphJob &job) const { Tpass pass(job); }
|
||||
|
||||
/**
|
||||
* Destructor. Has to be given because of virtual Run().
|
||||
*/
|
||||
virtual ~MCFHandler() {}
|
||||
void Run(LinkGraphJob &job) const override { Tpass pass(job); }
|
||||
};
|
||||
|
||||
#endif /* MCF_H */
|
||||
|
||||
@@ -67,7 +67,7 @@ bool LinkRefresher::Hop::operator<(const Hop &other) const
|
||||
* @param is_full_loading If the vehicle is full loading.
|
||||
*/
|
||||
LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading) :
|
||||
vehicle(vehicle), seen_hops(seen_hops), cargo(CT_INVALID), allow_merge(allow_merge),
|
||||
vehicle(vehicle), seen_hops(seen_hops), cargo(INVALID_CARGO), allow_merge(allow_merge),
|
||||
is_full_loading(is_full_loading)
|
||||
{
|
||||
memset(this->capacities, 0, sizeof(this->capacities));
|
||||
@@ -106,7 +106,7 @@ bool LinkRefresher::HandleRefit(CargoID refit_cargo)
|
||||
v->cargo_type = this->cargo;
|
||||
v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
|
||||
|
||||
uint16 mail_capacity = 0;
|
||||
uint16_t mail_capacity = 0;
|
||||
uint amount = e->DetermineCapacity(v, &mail_capacity);
|
||||
|
||||
/* Restore the original cargo type */
|
||||
@@ -144,10 +144,10 @@ bool LinkRefresher::HandleRefit(CargoID refit_cargo)
|
||||
*/
|
||||
void LinkRefresher::ResetRefit()
|
||||
{
|
||||
for (RefitList::iterator it(this->refit_capacities.begin()); it != this->refit_capacities.end(); ++it) {
|
||||
if (it->remaining == it->capacity) continue;
|
||||
this->capacities[it->cargo] += it->capacity - it->remaining;
|
||||
it->remaining = it->capacity;
|
||||
for (auto &it : this->refit_capacities) {
|
||||
if (it.remaining == it.capacity) continue;
|
||||
this->capacities[it.cargo] += it.capacity - it.remaining;
|
||||
it.remaining = it.capacity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ void LinkRefresher::ResetRefit()
|
||||
* @param num_hops Number of hops already taken by recursive calls to this method.
|
||||
* @return new next Order.
|
||||
*/
|
||||
const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops)
|
||||
const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next, uint8_t flags, uint num_hops)
|
||||
{
|
||||
/* next is good if it's either nullptr (then the caller will stop the
|
||||
* evaluation) or if it's not conditional and the caller allows it to be
|
||||
@@ -224,7 +224,7 @@ void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
|
||||
/* This estimates the travel time of the link as the time needed
|
||||
* to travel between the stations at half the max speed of the consist.
|
||||
* The result is in tiles/tick (= 2048 km-ish/h). */
|
||||
uint32 time_estimate = DistanceManhattan(st->xy, st_to->xy) * 4096U / this->vehicle->GetDisplayMaxSpeed();
|
||||
uint32_t time_estimate = DistanceManhattan(st->xy, st_to->xy) * 4096U / this->vehicle->GetDisplayMaxSpeed();
|
||||
|
||||
/* If the vehicle is currently full loading, increase the capacities at the station
|
||||
* where it is loading by an estimate of what it would have transported if it wasn't
|
||||
@@ -233,8 +233,7 @@ void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
|
||||
* probably far off and we'd greatly overestimate the capacity by increasing.*/
|
||||
if (this->is_full_loading && this->vehicle->orders != nullptr &&
|
||||
st->index == vehicle->last_station_visited &&
|
||||
this->vehicle->orders->GetTotalDuration() >
|
||||
(Ticks)this->vehicle->current_order_time) {
|
||||
this->vehicle->orders->GetTotalDuration() > this->vehicle->current_order_time) {
|
||||
uint effective_capacity = cargo_quantity * this->vehicle->load_unload_ticks;
|
||||
if (effective_capacity > (uint)this->vehicle->orders->GetTotalDuration()) {
|
||||
IncreaseStats(st, c, next_station, effective_capacity /
|
||||
@@ -263,7 +262,7 @@ void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
|
||||
* @param flags RefreshFlags to give hints about the previous link and state carried over from that.
|
||||
* @param num_hops Number of hops already taken by recursive calls to this method.
|
||||
*/
|
||||
void LinkRefresher::RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops)
|
||||
void LinkRefresher::RefreshLinks(const Order *cur, const Order *next, uint8_t flags, uint num_hops)
|
||||
{
|
||||
while (next != nullptr) {
|
||||
|
||||
|
||||
@@ -12,9 +12,6 @@
|
||||
|
||||
#include "../cargo_type.h"
|
||||
#include "../vehicle_base.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
/**
|
||||
* Utility to refresh links a consist will visit.
|
||||
@@ -41,9 +38,9 @@ protected:
|
||||
*/
|
||||
struct RefitDesc {
|
||||
CargoID cargo; ///< Cargo type the vehicle will be carrying.
|
||||
uint16 capacity; ///< Capacity the vehicle will have.
|
||||
uint16 remaining; ///< Capacity remaining from before the previous refit.
|
||||
RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
|
||||
uint16_t capacity; ///< Capacity the vehicle will have.
|
||||
uint16_t remaining; ///< Capacity remaining from before the previous refit.
|
||||
RefitDesc(CargoID cargo, uint16_t capacity, uint16_t remaining) :
|
||||
cargo(cargo), capacity(capacity), remaining(remaining) {}
|
||||
};
|
||||
|
||||
@@ -59,7 +56,7 @@ protected:
|
||||
struct Hop {
|
||||
OrderID from; ///< Last order where vehicle could interact with cargo or absolute first order.
|
||||
OrderID to; ///< Next order to be processed.
|
||||
CargoID cargo; ///< Cargo the consist is probably carrying or CT_INVALID if unknown.
|
||||
CargoID cargo; ///< Cargo the consist is probably carrying or INVALID_CARGO if unknown.
|
||||
|
||||
/**
|
||||
* Default constructor should not be called but has to be visible for
|
||||
@@ -93,9 +90,9 @@ protected:
|
||||
bool HandleRefit(CargoID refit_cargo);
|
||||
void ResetRefit();
|
||||
void RefreshStats(const Order *cur, const Order *next);
|
||||
const Order *PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
|
||||
const Order *PredictNextOrder(const Order *cur, const Order *next, uint8_t flags, uint num_hops = 0);
|
||||
|
||||
void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
|
||||
void RefreshLinks(const Order *cur, const Order *next, uint8_t flags, uint num_hops = 0);
|
||||
};
|
||||
|
||||
#endif /* REFRESH_H */
|
||||
|
||||
Reference in New Issue
Block a user