Update: Translations from eints
[openttd-github.git] / src / saveload / linkgraph_sl.cpp
blob9295302abd3fb9e8015f46f621942d318c8470ba
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file linkgraph_sl.cpp Code handling saving and loading of link graphs */
10 #include "../stdafx.h"
12 #include "saveload.h"
13 #include "compat/linkgraph_sl_compat.h"
15 #include "../linkgraph/linkgraph.h"
16 #include "../linkgraph/linkgraphjob.h"
17 #include "../linkgraph/linkgraphschedule.h"
18 #include "../network/network.h"
19 #include "../settings_internal.h"
20 #include "../settings_table.h"
22 #include "../safeguards.h"
24 typedef LinkGraph::BaseNode Node;
25 typedef LinkGraph::BaseEdge Edge;
27 static uint16_t _num_nodes;
28 static LinkGraph *_linkgraph; ///< Contains the current linkgraph being saved/loaded.
29 static NodeID _linkgraph_from; ///< Contains the current "from" node being saved/loaded.
31 class SlLinkgraphEdge : public DefaultSaveLoadHandler<SlLinkgraphEdge, Node> {
32 public:
33 inline static const SaveLoad description[] = {
34 SLE_VAR(Edge, capacity, SLE_UINT32),
35 SLE_VAR(Edge, usage, SLE_UINT32),
36 SLE_CONDVAR(Edge, travel_time_sum, SLE_UINT64, SLV_LINKGRAPH_TRAVEL_TIME, SL_MAX_VERSION),
37 SLE_VAR(Edge, last_unrestricted_update, SLE_INT32),
38 SLE_CONDVAR(Edge, last_restricted_update, SLE_INT32, SLV_187, SL_MAX_VERSION),
39 SLE_VAR(Edge, dest_node, SLE_UINT16),
40 SLE_CONDVARNAME(Edge, dest_node, "next_edge", SLE_UINT16, SL_MIN_VERSION, SLV_LINKGRAPH_EDGES),
42 inline const static SaveLoadCompatTable compat_description = _linkgraph_edge_sl_compat;
44 void Save(Node *bn) const override
46 SlSetStructListLength(bn->edges.size());
47 for (Edge &e : bn->edges) {
48 SlObject(&e, this->GetDescription());
52 void Load(Node *bn) const override
54 if (IsSavegameVersionBefore(SLV_LINKGRAPH_EDGES)) {
55 uint16_t max_size = _linkgraph->Size();
56 std::vector<Edge> edges(max_size);
58 if (IsSavegameVersionBefore(SLV_191)) {
59 /* We used to save the full matrix ... */
60 for (NodeID to = 0; to < max_size; ++to) {
61 SlObject(&edges[to], this->GetLoadDescription());
63 } else {
64 size_t used_size = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? max_size : SlGetStructListLength(UINT16_MAX);
66 /* ... but as that wasted a lot of space we save a sparse matrix now. */
67 for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = edges[to].dest_node) {
68 if (used_size == 0) SlErrorCorrupt("Link graph structure overflow");
69 used_size--;
71 if (to >= max_size) SlErrorCorrupt("Link graph structure overflow");
72 SlObject(&edges[to], this->GetLoadDescription());
75 if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) && used_size > 0) SlErrorCorrupt("Corrupted link graph");
78 /* Build edge list from edge matrix. */
79 for (NodeID to = edges[_linkgraph_from].dest_node; to != INVALID_NODE; to = edges[to].dest_node) {
80 bn->edges.push_back(edges[to]);
81 bn->edges.back().dest_node = to;
83 /* Sort by destination. */
84 std::sort(bn->edges.begin(), bn->edges.end());
85 } else {
86 /* Edge data is now a simple vector and not any kind of matrix. */
87 size_t size = SlGetStructListLength(UINT16_MAX);
88 for (size_t i = 0; i < size; i++) {
89 bn->edges.emplace_back();
90 SlObject(&bn->edges.back(), this->GetLoadDescription());
96 class SlLinkgraphNode : public DefaultSaveLoadHandler<SlLinkgraphNode, LinkGraph> {
97 public:
98 inline static const SaveLoad description[] = {
99 SLE_CONDVAR(Node, xy, SLE_UINT32, SLV_191, SL_MAX_VERSION),
100 SLE_VAR(Node, supply, SLE_UINT32),
101 SLE_VAR(Node, demand, SLE_UINT32),
102 SLE_VAR(Node, station, SLE_UINT16),
103 SLE_VAR(Node, last_update, SLE_INT32),
104 SLEG_STRUCTLIST("edges", SlLinkgraphEdge),
106 inline const static SaveLoadCompatTable compat_description = _linkgraph_node_sl_compat;
108 void Save(LinkGraph *lg) const override
110 _linkgraph = lg;
112 SlSetStructListLength(lg->Size());
113 for (NodeID from = 0; from < lg->Size(); ++from) {
114 _linkgraph_from = from;
115 SlObject(&lg->nodes[from], this->GetDescription());
119 void Load(LinkGraph *lg) const override
121 _linkgraph = lg;
123 uint16_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _num_nodes : (uint16_t)SlGetStructListLength(UINT16_MAX);
124 lg->Init(length);
125 for (NodeID from = 0; from < length; ++from) {
126 _linkgraph_from = from;
127 SlObject(&lg->nodes[from], this->GetLoadDescription());
133 * Get a SaveLoad array for a link graph.
134 * @return SaveLoad array for link graph.
136 SaveLoadTable GetLinkGraphDesc()
138 static const SaveLoad link_graph_desc[] = {
139 SLE_VAR(LinkGraph, last_compression, SLE_INT32),
140 SLEG_CONDVAR("num_nodes", _num_nodes, SLE_UINT16, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
141 SLE_VAR(LinkGraph, cargo, SLE_UINT8),
142 SLEG_STRUCTLIST("nodes", SlLinkgraphNode),
144 return link_graph_desc;
148 * Proxy to reuse LinkGraph to save/load a LinkGraphJob.
149 * One of the members of a LinkGraphJob is a LinkGraph, but SLEG_STRUCT()
150 * doesn't allow us to select a member. So instead, we add a bit of glue to
151 * accept a LinkGraphJob, get the LinkGraph, and use that to call the
152 * save/load routines for a regular LinkGraph.
154 class SlLinkgraphJobProxy : public DefaultSaveLoadHandler<SlLinkgraphJobProxy, LinkGraphJob> {
155 public:
156 inline static const SaveLoad description[] = {{}}; // Needed to keep DefaultSaveLoadHandler happy.
157 SaveLoadTable GetDescription() const override { return GetLinkGraphDesc(); }
158 inline const static SaveLoadCompatTable compat_description = _linkgraph_sl_compat;
160 void Save(LinkGraphJob *lgj) const override
162 SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetDescription());
165 void Load(LinkGraphJob *lgj) const override
167 SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetLoadDescription());
172 * Get a SaveLoad array for a link graph job. The settings struct is derived from
173 * the global settings saveload array. The exact entries are calculated when the function
174 * is called the first time.
175 * It's necessary to keep a copy of the settings for each link graph job so that you can
176 * change the settings while in-game and still not mess with current link graph runs.
177 * Of course the settings have to be saved and loaded, too, to avoid desyncs.
178 * @return Array of SaveLoad structs.
180 SaveLoadTable GetLinkGraphJobDesc()
182 static std::vector<SaveLoad> saveloads;
184 static const SaveLoad job_desc[] = {
185 SLE_VAR(LinkGraphJob, join_date, SLE_INT32),
186 SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
187 SLEG_STRUCT("linkgraph", SlLinkgraphJobProxy),
190 /* The member offset arithmetic below is only valid if the types in question
191 * are standard layout types. Otherwise, it would be undefined behaviour. */
192 static_assert(std::is_standard_layout<LinkGraphSettings>::value, "LinkGraphSettings needs to be a standard layout type");
194 /* We store the offset of each member of the #LinkGraphSettings in the
195 * extra data of the saveload struct. Use it together with the address
196 * of the settings struct inside the job to find the final memory address. */
197 static SaveLoadAddrProc * const proc = [](void *b, size_t extra) -> void * { return const_cast<void *>(static_cast<const void *>(reinterpret_cast<const char *>(std::addressof(static_cast<LinkGraphJob *>(b)->settings)) + extra)); };
199 /* Build the SaveLoad array on first call and don't touch it later on */
200 if (saveloads.empty()) {
201 GetSaveLoadFromSettingTable(_linkgraph_settings, saveloads);
203 for (auto &sl : saveloads) {
204 sl.address_proc = proc;
207 for (auto &sld : job_desc) {
208 saveloads.push_back(sld);
212 return saveloads;
216 * Get a SaveLoad array for the link graph schedule.
217 * @return SaveLoad array for the link graph schedule.
219 SaveLoadTable GetLinkGraphScheduleDesc()
221 static const SaveLoad schedule_desc[] = {
222 SLE_REFLIST(LinkGraphSchedule, schedule, REF_LINK_GRAPH),
223 SLE_REFLIST(LinkGraphSchedule, running, REF_LINK_GRAPH_JOB),
225 return schedule_desc;
229 * Spawn the threads for running link graph calculations.
230 * Has to be done after loading as the cargo classes might have changed.
232 void AfterLoadLinkGraphs()
234 if (IsSavegameVersionBefore(SLV_191)) {
235 for (LinkGraph *lg : LinkGraph::Iterate()) {
236 for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
237 const Station *st = Station::GetIfValid((*lg)[node_id].station);
238 if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
242 for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
243 LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
244 for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
245 const Station *st = Station::GetIfValid((*lg)[node_id].station);
246 if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
251 LinkGraphSchedule::instance.SpawnAll();
253 if (!_networking || _network_server) {
254 AfterLoad_LinkGraphPauseControl();
259 * All link graphs.
261 struct LGRPChunkHandler : ChunkHandler {
262 LGRPChunkHandler() : ChunkHandler('LGRP', CH_TABLE) {}
264 void Save() const override
266 SlTableHeader(GetLinkGraphDesc());
268 for (LinkGraph *lg : LinkGraph::Iterate()) {
269 SlSetArrayIndex(lg->index);
270 SlObject(lg, GetLinkGraphDesc());
274 void Load() const override
276 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphDesc(), _linkgraph_sl_compat);
278 int index;
279 while ((index = SlIterateArray()) != -1) {
280 LinkGraph *lg = new (index) LinkGraph();
281 SlObject(lg, slt);
287 * All link graph jobs.
289 struct LGRJChunkHandler : ChunkHandler {
290 LGRJChunkHandler() : ChunkHandler('LGRJ', CH_TABLE) {}
292 void Save() const override
294 SlTableHeader(GetLinkGraphJobDesc());
296 for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
297 SlSetArrayIndex(lgj->index);
298 SlObject(lgj, GetLinkGraphJobDesc());
302 void Load() const override
304 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphJobDesc(), _linkgraph_job_sl_compat);
306 int index;
307 while ((index = SlIterateArray()) != -1) {
308 LinkGraphJob *lgj = new (index) LinkGraphJob();
309 SlObject(lgj, slt);
315 * Link graph schedule.
317 struct LGRSChunkHandler : ChunkHandler {
318 LGRSChunkHandler() : ChunkHandler('LGRS', CH_TABLE) {}
320 void Save() const override
322 SlTableHeader(GetLinkGraphScheduleDesc());
324 SlSetArrayIndex(0);
325 SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
328 void Load() const override
330 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphScheduleDesc(), _linkgraph_schedule_sl_compat);
332 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
333 SlObject(&LinkGraphSchedule::instance, slt);
334 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many LGRS entries");
337 void FixPointers() const override
339 SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
343 static const LGRPChunkHandler LGRP;
344 static const LGRJChunkHandler LGRJ;
345 static const LGRSChunkHandler LGRS;
346 static const ChunkHandlerRef linkgraph_chunk_handlers[] = {
347 LGRP,
348 LGRJ,
349 LGRS,
352 extern const ChunkHandlerTable _linkgraph_chunk_handlers(linkgraph_chunk_handlers);