OpenTTD Source: src/saveload/linkgraph_sl.cpp Source File

OpenTTD Source 13.1

linkgraph_sl.cpp
Go to the documentation of this file.
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 */
7
10 #include "../stdafx.h"
11
12 #include "saveload.h"
13 #include "compat/linkgraph_sl_compat.h"
14
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"
21
22 #include "../safeguards.h"
23
24 typedef LinkGraph::BaseNode Node;
25 typedef LinkGraph::BaseEdge Edge;
26
27 static uint16 _num_nodes;
28 static LinkGraph *_linkgraph;
29 static NodeID _linkgraph_from;
30
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, next_edge, SLE_UINT16),
40 };
41 inline const static SaveLoadCompatTable compat_description = _linkgraph_edge_sl_compat;
42
43 void Save(Node *bn) const override
44 {
45 uint16 size = 0;
46 for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
47 size++;
48 }
49
50 SlSetStructListLength(size);
51 for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
52 SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetDescription());
53 }
54 }
55
56 void Load(Node *bn) const override
57 {
58 uint16 max_size = _linkgraph->Size();
59
60 if (IsSavegameVersionBefore(SLV_191)) {
61 /* We used to save the full matrix ... */
62 for (NodeID to = 0; to < max_size; ++to) {
63 SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetLoadDescription());
64 }
65 return;
66 }
67
68 size_t used_size = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? max_size : SlGetStructListLength(UINT16_MAX);
69
70 /* ... but as that wasted a lot of space we save a sparse matrix now. */
71 for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
72 if (used_size == 0) SlErrorCorrupt("Link graph structure overflow");
73 used_size--;
74
75 if (to >= max_size) SlErrorCorrupt("Link graph structure overflow");
76 SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetLoadDescription());
77 }
78
79 if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) && used_size > 0) SlErrorCorrupt("Corrupted link graph");
80 }
81 };
82
83 class SlLinkgraphNode : public DefaultSaveLoadHandler<SlLinkgraphNode, LinkGraph> {
84 public:
85 inline static const SaveLoad description[] = {
86 SLE_CONDVAR(Node, xy, SLE_UINT32, SLV_191, SL_MAX_VERSION),
87 SLE_VAR(Node, supply, SLE_UINT32),
88 SLE_VAR(Node, demand, SLE_UINT32),
89 SLE_VAR(Node, station, SLE_UINT16),
90 SLE_VAR(Node, last_update, SLE_INT32),
91 SLEG_STRUCTLIST("edges", SlLinkgraphEdge),
92 };
93 inline const static SaveLoadCompatTable compat_description = _linkgraph_node_sl_compat;
94
95 void Save(LinkGraph *lg) const override
96 {
97 _linkgraph = lg;
98
99 SlSetStructListLength(lg->Size());
100 for (NodeID from = 0; from < lg->Size(); ++from) {
101 _linkgraph_from = from;
102 SlObject(&lg->nodes[from], this->GetDescription());
103 }
104 }
105
106 void Load(LinkGraph *lg) const override
107 {
108 _linkgraph = lg;
109
110 uint16 length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _num_nodes : (uint16)SlGetStructListLength(UINT16_MAX);
111 lg->Init(length);
112 for (NodeID from = 0; from < length; ++from) {
113 _linkgraph_from = from;
114 SlObject(&lg->nodes[from], this->GetLoadDescription());
115 }
116 }
117 };
118
123 SaveLoadTable GetLinkGraphDesc()
124 {
125 static const SaveLoad link_graph_desc[] = {
126 SLE_VAR(LinkGraph, last_compression, SLE_INT32),
127 SLEG_CONDVAR("num_nodes", _num_nodes, SLE_UINT16, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
128 SLE_VAR(LinkGraph, cargo, SLE_UINT8),
129 SLEG_STRUCTLIST("nodes", SlLinkgraphNode),
130 };
131 return link_graph_desc;
132 }
133
141 class SlLinkgraphJobProxy : public DefaultSaveLoadHandler<SlLinkgraphJobProxy, LinkGraphJob> {
142 public:
143 inline static const SaveLoad description[] = {{}}; // Needed to keep DefaultSaveLoadHandler happy.
144 SaveLoadTable GetDescription() const override { return GetLinkGraphDesc(); }
145 inline const static SaveLoadCompatTable compat_description = _linkgraph_sl_compat;
146
147 void Save(LinkGraphJob *lgj) const override
148 {
149 SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetDescription());
150 }
151
152 void Load(LinkGraphJob *lgj) const override
153 {
154 SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetLoadDescription());
155 }
156 };
157
167 SaveLoadTable GetLinkGraphJobDesc()
168 {
169 static std::vector<SaveLoad> saveloads;
170
171 static const SaveLoad job_desc[] = {
172 SLE_VAR(LinkGraphJob, join_date, SLE_INT32),
173 SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
174 SLEG_STRUCT("linkgraph", SlLinkgraphJobProxy),
175 };
176
177 /* The member offset arithmetic below is only valid if the types in question
178 * are standard layout types. Otherwise, it would be undefined behaviour. */
179 static_assert(std::is_standard_layout<LinkGraphSettings>::value, "LinkGraphSettings needs to be a standard layout type");
180
181 /* We store the offset of each member of the #LinkGraphSettings in the
182 * extra data of the saveload struct. Use it together with the address
183 * of the settings struct inside the job to find the final memory address. */
184 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)); };
185
186 /* Build the SaveLoad array on first call and don't touch it later on */
187 if (saveloads.size() == 0) {
188 GetSaveLoadFromSettingTable(_linkgraph_settings, saveloads);
189
190 for (auto &sl : saveloads) {
191 sl.address_proc = proc;
192 }
193
194 for (auto &sld : job_desc) {
195 saveloads.push_back(sld);
196 }
197 }
198
199 return saveloads;
200 }
201
206 SaveLoadTable GetLinkGraphScheduleDesc()
207 {
208 static const SaveLoad schedule_desc[] = {
209 SLE_REFLIST(LinkGraphSchedule, schedule, REF_LINK_GRAPH),
210 SLE_REFLIST(LinkGraphSchedule, running, REF_LINK_GRAPH_JOB),
211 };
212 return schedule_desc;
213 }
214
219 void AfterLoadLinkGraphs()
220 {
221 if (IsSavegameVersionBefore(SLV_191)) {
222 for (LinkGraph *lg : LinkGraph::Iterate()) {
223 for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
224 const Station *st = Station::GetIfValid((*lg)[node_id].Station());
225 if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
226 }
227 }
228
229 for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
230 LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
231 for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
232 const Station *st = Station::GetIfValid((*lg)[node_id].Station());
233 if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
234 }
235 }
236 }
237
238 LinkGraphSchedule::instance.SpawnAll();
239
240 if (!_networking || _network_server) {
241 AfterLoad_LinkGraphPauseControl();
242 }
243 }
244
248 struct LGRPChunkHandler : ChunkHandler {
249 LGRPChunkHandler() : ChunkHandler('LGRP', CH_TABLE) {}
250
251 void Save() const override
252 {
253 SlTableHeader(GetLinkGraphDesc());
254
255 for (LinkGraph *lg : LinkGraph::Iterate()) {
256 SlSetArrayIndex(lg->index);
257 SlObject(lg, GetLinkGraphDesc());
258 }
259 }
260
261 void Load() const override
262 {
263 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphDesc(), _linkgraph_sl_compat);
264
265 int index;
266 while ((index = SlIterateArray()) != -1) {
267 LinkGraph *lg = new (index) LinkGraph();
268 SlObject(lg, slt);
269 }
270 }
271 };
272
276 struct LGRJChunkHandler : ChunkHandler {
277 LGRJChunkHandler() : ChunkHandler('LGRJ', CH_TABLE) {}
278
279 void Save() const override
280 {
281 SlTableHeader(GetLinkGraphJobDesc());
282
283 for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
284 SlSetArrayIndex(lgj->index);
285 SlObject(lgj, GetLinkGraphJobDesc());
286 }
287 }
288
289 void Load() const override
290 {
291 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphJobDesc(), _linkgraph_job_sl_compat);
292
293 int index;
294 while ((index = SlIterateArray()) != -1) {
295 LinkGraphJob *lgj = new (index) LinkGraphJob();
296 SlObject(lgj, slt);
297 }
298 }
299 };
300
304 struct LGRSChunkHandler : ChunkHandler {
305 LGRSChunkHandler() : ChunkHandler('LGRS', CH_TABLE) {}
306
307 void Save() const override
308 {
309 SlTableHeader(GetLinkGraphScheduleDesc());
310
311 SlSetArrayIndex(0);
312 SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
313 }
314
315 void Load() const override
316 {
317 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphScheduleDesc(), _linkgraph_schedule_sl_compat);
318
319 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
320 SlObject(&LinkGraphSchedule::instance, slt);
321 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many LGRS entries");
322 }
323
324 void FixPointers() const override
325 {
326 SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc());
327 }
328 };
329
330 static const LGRPChunkHandler LGRP;
331 static const LGRJChunkHandler LGRJ;
332 static const LGRSChunkHandler LGRS;
333 static const ChunkHandlerRef linkgraph_chunk_handlers[] = {
334 LGRP,
335 LGRJ,
336 LGRS,
337 };
338
339 extern const ChunkHandlerTable _linkgraph_chunk_handlers(linkgraph_chunk_handlers);
SLV_187
@ SLV_187
187 25899 Linkgraph - restricted flows
Definition: saveload.h:271
SlLinkgraphNode
Definition: linkgraph_sl.cpp:83
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition: saveload.h:521
SLE_REFLIST
#define SLE_REFLIST(base, variable, type)
Storage of a list of SL_REF elements in every savegame version.
Definition: saveload.h:820
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
SLV_LINKGRAPH_TRAVEL_TIME
@ SLV_LINKGRAPH_TRAVEL_TIME
297 PR#9457 v12.0-RC1 Store travel time in the linkgraph.
Definition: saveload.h:340
LinkGraph::edges
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:546
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
LGRPChunkHandler
All link graphs.
Definition: linkgraph_sl.cpp:248
LinkGraph::Node
Updatable node class.
Definition: linkgraph.h:380
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
LinkGraph::nodes
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:545
Station
Station data structure.
Definition: station_base.h:454
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:449
_linkgraph_node_sl_compat
const SaveLoadCompat _linkgraph_node_sl_compat[]
Original field order for SlLinkgraphNode.
Definition: linkgraph_sl_compat.h:26
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:30
_network_server
bool _network_server
network-server is active
Definition: network.cpp:59
LinkGraphSchedule
Definition: linkgraphschedule.h:36
LinkGraphSchedule::instance
static LinkGraphSchedule instance
Static instance of LinkGraphSchedule.
Definition: linkgraphschedule.h:52
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:708
saveload.h
LGRJChunkHandler
All link graph jobs.
Definition: linkgraph_sl.cpp:276
_linkgraph_schedule_sl_compat
const SaveLoadCompat _linkgraph_schedule_sl_compat[]
Original field order for schedule_desc.
Definition: linkgraph_sl_compat.h:61
AfterLoad_LinkGraphPauseControl
void AfterLoad_LinkGraphPauseControl()
Pause the game on load if we would do a join with the next link graph job, but it is still running,...
Definition: linkgraphschedule.cpp:194
LinkGraph::Size
NodeID Size() const
Get the current size of the component.
Definition: linkgraph.h:508
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition: linkgraphschedule.cpp:111
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:412
LinkGraph::Init
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:305
LGRSChunkHandler
Link graph schedule.
Definition: linkgraph_sl.cpp:304
LinkGraph::Edge
An updatable edge class.
Definition: linkgraph.h:299
LinkGraphJob::Graph
const LinkGraph & Graph() const
Get a reference to the underlying link graph.
Definition: linkgraphjob.h:359
GetLinkGraphJobDesc
SaveLoadTable GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
Definition: linkgraph_sl.cpp:167
SlLinkgraphEdge
Definition: linkgraph_sl.cpp:31
SlLinkgraphJobProxy::GetDescription
SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
Definition: linkgraph_sl.cpp:144
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
LGRJChunkHandler::Save
void Save() const override
Save the chunk.
Definition: linkgraph_sl.cpp:279
LGRSChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: linkgraph_sl.cpp:324
LinkGraph::BaseEdge
An edge in the link graph.
Definition: linkgraph.h:62
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
LGRPChunkHandler::Load
void Load() const override
Load the chunk.
Definition: linkgraph_sl.cpp:261
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1030
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1826
_linkgraph_job_sl_compat
const SaveLoadCompat _linkgraph_job_sl_compat[]
Original field order for job_desc.
Definition: linkgraph_sl_compat.h:44
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:348
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:552
LGRSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: linkgraph_sl.cpp:307
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:778
Pool::PoolItem<&_link_graph_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:855
linkgraph_sl_compat.h
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:365
AfterLoadLinkGraphs
void AfterLoadLinkGraphs()
Spawn the threads for running link graph calculations.
Definition: linkgraph_sl.cpp:219
LGRPChunkHandler::Save
void Save() const override
Save the chunk.
Definition: linkgraph_sl.cpp:251
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
SlLinkgraphJobProxy
Proxy to reuse LinkGraph to save/load a LinkGraphJob.
Definition: linkgraph_sl.cpp:141
_linkgraph_edge_sl_compat
const SaveLoadCompat _linkgraph_edge_sl_compat[]
Original field order for SlLinkgraphEdge.
Definition: linkgraph_sl_compat.h:16
REF_LINK_GRAPH
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition: saveload.h:551
SpecializedStation< Station, false >::GetIfValid
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
Definition: base_station_base.h:227
GetLinkGraphScheduleDesc
SaveLoadTable GetLinkGraphScheduleDesc()
Get a SaveLoad array for the link graph schedule.
Definition: linkgraph_sl.cpp:206
SLV_SAVELOAD_LIST_LENGTH
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition: saveload.h:335
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2030
SLV_191
@ SLV_191
191 26636 FS#6026 Fix disaster vehicle storage (No bump) 191 26646 FS#6041 Linkgraph - store location...
Definition: saveload.h:276
LGRJChunkHandler::Load
void Load() const override
Load the chunk.
Definition: linkgraph_sl.cpp:289
GetLinkGraphDesc
SaveLoadTable GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
Definition: linkgraph_sl.cpp:123
LinkGraph::BaseNode
Node of the link graph.
Definition: linkgraph.h:47
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1839
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1892
LGRSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: linkgraph_sl.cpp:315
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1810
SaveLoad
SaveLoad type struct.
Definition: saveload.h:659
_linkgraph
static LinkGraph * _linkgraph
Contains the current linkgraph being saved/loaded.
Definition: linkgraph_sl.cpp:28
GetSaveLoadFromSettingTable
void GetSaveLoadFromSettingTable(SettingTable settings, std::vector< SaveLoad > &saveloads)
Get the SaveLoad for all settings in the settings table.
Definition: settings.cpp:1445
_linkgraph_sl_compat
const SaveLoadCompat _linkgraph_sl_compat[]
Original field order for link_graph_desc.
Definition: linkgraph_sl_compat.h:36
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:671
_linkgraph_from
static NodeID _linkgraph_from
Contains the current "from" node being saved/loaded.
Definition: linkgraph_sl.cpp:29
SLEG_STRUCTLIST
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition: saveload.h:1005
SLEG_STRUCT
#define SLEG_STRUCT(name, handler)
Storage of a structs in every savegame version.
Definition: saveload.h:982