Codechange: [Linkgraph] Only store present link graph edges and not all possible edges.

This commit is contained in:
Michael Lutz
2023-01-02 21:46:49 +01:00
parent 178249e7cc
commit 7352f812e6
9 changed files with 153 additions and 146 deletions

View File

@@ -36,48 +36,60 @@ public:
SLE_CONDVAR(Edge, travel_time_sum, SLE_UINT64, SLV_LINKGRAPH_TRAVEL_TIME, SL_MAX_VERSION),
SLE_VAR(Edge, last_unrestricted_update, SLE_INT32),
SLE_CONDVAR(Edge, last_restricted_update, SLE_INT32, SLV_187, SL_MAX_VERSION),
SLE_VAR(Edge, next_edge, SLE_UINT16),
SLE_VAR(Edge, dest_node, SLE_UINT16),
SLE_CONDVARNAME(Edge, dest_node, "next_edge", SLE_UINT16, SL_MIN_VERSION, SLV_LINKGRAPH_EDGES),
};
inline const static SaveLoadCompatTable compat_description = _linkgraph_edge_sl_compat;
void Save(Node *bn) const override
{
uint16 size = 0;
for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->nodes[_linkgraph_from].edges[to].next_edge) {
size++;
}
SlSetStructListLength(size);
for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->nodes[_linkgraph_from].edges[to].next_edge) {
SlObject(&_linkgraph->nodes[_linkgraph_from].edges[to], this->GetDescription());
SlSetStructListLength(bn->edges.size());
for (Edge &e : bn->edges) {
SlObject(&e, this->GetDescription());
}
}
void Load(Node *bn) const override
{
uint16 max_size = _linkgraph->Size();
_linkgraph->nodes[_linkgraph_from].edges.resize(max_size);
if (IsSavegameVersionBefore(SLV_LINKGRAPH_EDGES)) {
uint16 max_size = _linkgraph->Size();
std::vector<Edge> edges(max_size);
if (IsSavegameVersionBefore(SLV_191)) {
/* We used to save the full matrix ... */
for (NodeID to = 0; to < max_size; ++to) {
SlObject(&_linkgraph->nodes[_linkgraph_from].edges[to], this->GetLoadDescription());
if (IsSavegameVersionBefore(SLV_191)) {
/* We used to save the full matrix ... */
for (NodeID to = 0; to < max_size; ++to) {
SlObject(&_linkgraph->nodes[_linkgraph_from].edges[to], this->GetLoadDescription());
}
} else {
size_t used_size = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? max_size : SlGetStructListLength(UINT16_MAX);
/* ... but as that wasted a lot of space we save a sparse matrix now. */
for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = edges[to].dest_node) {
if (used_size == 0) SlErrorCorrupt("Link graph structure overflow");
used_size--;
if (to >= max_size) SlErrorCorrupt("Link graph structure overflow");
SlObject(&edges[to], this->GetLoadDescription());
}
if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) && used_size > 0) SlErrorCorrupt("Corrupted link graph");
}
/* Build edge list from edge matrix. */
for (NodeID to = edges[_linkgraph_from].dest_node; to != INVALID_NODE; to = edges[to].dest_node) {
bn->edges.push_back(edges[to]);
bn->edges.back().dest_node = to;
}
/* Sort by destination. */
std::sort(bn->edges.begin(), bn->edges.end());
} else {
/* Edge data is now a simple vector and not any kind of matrix. */
size_t size = SlGetStructListLength(UINT16_MAX);
for (size_t i = 0; i < size; i++) {
bn->edges.emplace_back();
SlObject(&bn->edges.back(), this->GetLoadDescription());
}
return;
}
size_t used_size = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? max_size : SlGetStructListLength(UINT16_MAX);
/* ... but as that wasted a lot of space we save a sparse matrix now. */
for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->nodes[_linkgraph_from].edges[to].next_edge) {
if (used_size == 0) SlErrorCorrupt("Link graph structure overflow");
used_size--;
if (to >= max_size) SlErrorCorrupt("Link graph structure overflow");
SlObject(&_linkgraph->nodes[_linkgraph_from].edges[to], this->GetLoadDescription());
}
if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) && used_size > 0) SlErrorCorrupt("Corrupted link graph");
}
};

View File

@@ -345,6 +345,8 @@ enum SaveLoadVersion : uint16 {
SLV_MULTITRACK_LEVEL_CROSSINGS, ///< 302 PR#9931 v13.0 Multi-track level crossings.
SLV_NEWGRF_ROAD_STOPS, ///< 303 PR#10144 NewGRF road stops.
SLV_LINKGRAPH_EDGES, ///< 303 PR#10314 Explicitly store link graph edges destination.
SL_MAX_VERSION, ///< Highest possible saveload version
};