OpenTTD Source  12.0-beta1
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"
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 
26 
27 static uint16 _num_nodes;
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_VAR(Edge, last_unrestricted_update, SLE_INT32),
37  SLE_CONDVAR(Edge, last_restricted_update, SLE_INT32, SLV_187, SL_MAX_VERSION),
38  SLE_VAR(Edge, next_edge, SLE_UINT16),
39  };
40  inline const static SaveLoadCompatTable compat_description = _linkgraph_edge_sl_compat;
41 
42  void Save(Node *bn) const override
43  {
44  uint16 size = 0;
45  for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
46  size++;
47  }
48 
50  for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
51  SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetDescription());
52  }
53  }
54 
55  void Load(Node *bn) const override
56  {
57  uint16 max_size = _linkgraph->Size();
58 
60  /* We used to save the full matrix ... */
61  for (NodeID to = 0; to < max_size; ++to) {
62  SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetLoadDescription());
63  }
64  return;
65  }
66 
67  size_t used_size = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? max_size : SlGetStructListLength(UINT16_MAX);
68 
69  /* ... but as that wasted a lot of space we save a sparse matrix now. */
70  for (NodeID to = _linkgraph_from; to != INVALID_NODE; to = _linkgraph->edges[_linkgraph_from][to].next_edge) {
71  if (used_size == 0) SlErrorCorrupt("Link graph structure overflow");
72  used_size--;
73 
74  if (to >= max_size) SlErrorCorrupt("Link graph structure overflow");
75  SlObject(&_linkgraph->edges[_linkgraph_from][to], this->GetLoadDescription());
76  }
77 
78  if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) && used_size > 0) SlErrorCorrupt("Corrupted link graph");
79  }
80 };
81 
82 class SlLinkgraphNode : public DefaultSaveLoadHandler<SlLinkgraphNode, LinkGraph> {
83 public:
84  inline static const SaveLoad description[] = {
85  SLE_CONDVAR(Node, xy, SLE_UINT32, SLV_191, SL_MAX_VERSION),
86  SLE_VAR(Node, supply, SLE_UINT32),
87  SLE_VAR(Node, demand, SLE_UINT32),
88  SLE_VAR(Node, station, SLE_UINT16),
89  SLE_VAR(Node, last_update, SLE_INT32),
91  };
92  inline const static SaveLoadCompatTable compat_description = _linkgraph_node_sl_compat;
93 
94  void Save(LinkGraph *lg) const override
95  {
96  _linkgraph = lg;
97 
99  for (NodeID from = 0; from < lg->Size(); ++from) {
100  _linkgraph_from = from;
101  SlObject(&lg->nodes[from], this->GetDescription());
102  }
103  }
104 
105  void Load(LinkGraph *lg) const override
106  {
107  _linkgraph = lg;
108 
109  uint16 length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _num_nodes : (uint16)SlGetStructListLength(UINT16_MAX);
110  lg->Init(length);
111  for (NodeID from = 0; from < length; ++from) {
112  _linkgraph_from = from;
113  SlObject(&lg->nodes[from], this->GetLoadDescription());
114  }
115  }
116 };
117 
123 {
124  static const SaveLoad link_graph_desc[] = {
125  SLE_VAR(LinkGraph, last_compression, SLE_INT32),
126  SLEG_CONDVAR("num_nodes", _num_nodes, SLE_UINT16, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
127  SLE_VAR(LinkGraph, cargo, SLE_UINT8),
129  };
130  return link_graph_desc;
131 }
132 
140 class SlLinkgraphJobProxy : public DefaultSaveLoadHandler<SlLinkgraphJobProxy, LinkGraphJob> {
141 public:
142  inline static const SaveLoad description[] = {{}}; // Needed to keep DefaultSaveLoadHandler happy.
143  SaveLoadTable GetDescription() const override { return GetLinkGraphDesc(); }
144  inline const static SaveLoadCompatTable compat_description = _linkgraph_sl_compat;
145 
146  void Save(LinkGraphJob *lgj) const override
147  {
148  SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetDescription());
149  }
150 
151  void Load(LinkGraphJob *lgj) const override
152  {
153  SlObject(const_cast<LinkGraph *>(&lgj->Graph()), this->GetLoadDescription());
154  }
155 };
156 
167 {
168  static std::vector<SaveLoad> saveloads;
169 
170  static const SaveLoad job_desc[] = {
171  SLE_VAR(LinkGraphJob, join_date, SLE_INT32),
172  SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
173  SLEG_STRUCT("linkgraph", SlLinkgraphJobProxy),
174  };
175 
176  /* The member offset arithmetic below is only valid if the types in question
177  * are standard layout types. Otherwise, it would be undefined behaviour. */
178  static_assert(std::is_standard_layout<LinkGraphSettings>::value, "LinkGraphSettings needs to be a standard layout type");
179 
180  /* We store the offset of each member of the #LinkGraphSettings in the
181  * extra data of the saveload struct. Use it together with the address
182  * of the settings struct inside the job to find the final memory address. */
183  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)); };
184 
185  /* Build the SaveLoad array on first call and don't touch it later on */
186  if (saveloads.size() == 0) {
187  GetSaveLoadFromSettingTable(_linkgraph_settings, saveloads);
188 
189  for (auto &sl : saveloads) {
190  sl.address_proc = proc;
191  }
192 
193  for (auto &sld : job_desc) {
194  saveloads.push_back(sld);
195  }
196  }
197 
198  return saveloads;
199 }
200 
206 {
207  static const SaveLoad schedule_desc[] = {
210  };
211  return schedule_desc;
212 }
213 
219 {
221  for (LinkGraph *lg : LinkGraph::Iterate()) {
222  for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
223  const Station *st = Station::GetIfValid((*lg)[node_id].Station());
224  if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
225  }
226  }
227 
228  for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
229  LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
230  for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
231  const Station *st = Station::GetIfValid((*lg)[node_id].Station());
232  if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
233  }
234  }
235  }
236 
238 
239  if (!_networking || _network_server) {
241  }
242 }
243 
248  LGRPChunkHandler() : ChunkHandler('LGRP', CH_TABLE) {}
249 
250  void Save() const override
251  {
253 
254  for (LinkGraph *lg : LinkGraph::Iterate()) {
255  SlSetArrayIndex(lg->index);
256  SlObject(lg, GetLinkGraphDesc());
257  }
258  }
259 
260  void Load() const override
261  {
262  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphDesc(), _linkgraph_sl_compat);
263 
264  int index;
265  while ((index = SlIterateArray()) != -1) {
266  LinkGraph *lg = new (index) LinkGraph();
267  SlObject(lg, slt);
268  }
269  }
270 };
271 
276  LGRJChunkHandler() : ChunkHandler('LGRJ', CH_TABLE) {}
277 
278  void Save() const override
279  {
281 
282  for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
283  SlSetArrayIndex(lgj->index);
285  }
286  }
287 
288  void Load() const override
289  {
290  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphJobDesc(), _linkgraph_job_sl_compat);
291 
292  int index;
293  while ((index = SlIterateArray()) != -1) {
294  LinkGraphJob *lgj = new (index) LinkGraphJob();
295  SlObject(lgj, slt);
296  }
297  }
298 };
299 
304  LGRSChunkHandler() : ChunkHandler('LGRS', CH_TABLE) {}
305 
306  void Save() const override
307  {
309 
310  SlSetArrayIndex(0);
312  }
313 
314  void Load() const override
315  {
316  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetLinkGraphScheduleDesc(), _linkgraph_schedule_sl_compat);
317 
320  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many LGRS entries");
321  }
322 
323  void FixPointers() const override
324  {
326  }
327 };
328 
329 static const LGRPChunkHandler LGRP;
330 static const LGRJChunkHandler LGRJ;
331 static const LGRSChunkHandler LGRS;
332 static const ChunkHandlerRef linkgraph_chunk_handlers[] = {
333  LGRP,
334  LGRJ,
335  LGRS,
336 };
337 
338 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:82
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition: saveload.h:514
SLE_REFLIST
#define SLE_REFLIST(base, variable, type)
Storage of a list of SL_REF elements in every savegame version.
Definition: saveload.h:813
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
LinkGraph::edges
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:536
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
LGRPChunkHandler
All link graphs.
Definition: linkgraph_sl.cpp:247
LinkGraph::Node
Updatable node class.
Definition: linkgraph.h:373
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
LinkGraph::nodes
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:535
Station
Station data structure.
Definition: station_base.h:447
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:442
_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:57
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:701
saveload.h
LGRJChunkHandler
All link graph jobs.
Definition: linkgraph_sl.cpp:275
_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:193
LinkGraph::Size
NodeID Size() const
Get the current size of the component.
Definition: linkgraph.h:498
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition: linkgraphschedule.cpp:110
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:405
LinkGraph::Init
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:280
LGRSChunkHandler
Link graph schedule.
Definition: linkgraph_sl.cpp:303
LinkGraph::Edge
An updatable edge class.
Definition: linkgraph.h:292
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:166
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:143
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:278
LGRSChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: linkgraph_sl.cpp:323
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:260
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1023
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1825
_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:341
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:545
LGRSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: linkgraph_sl.cpp:306
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:771
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:848
linkgraph_sl_compat.h
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
AfterLoadLinkGraphs
void AfterLoadLinkGraphs()
Spawn the threads for running link graph calculations.
Definition: linkgraph_sl.cpp:218
LGRPChunkHandler::Save
void Save() const override
Save the chunk.
Definition: linkgraph_sl.cpp:250
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:140
_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:544
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:228
GetLinkGraphScheduleDesc
SaveLoadTable GetLinkGraphScheduleDesc()
Get a SaveLoad array for the link graph schedule.
Definition: linkgraph_sl.cpp:205
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:2029
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:288
GetLinkGraphDesc
SaveLoadTable GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
Definition: linkgraph_sl.cpp:122
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:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
LGRSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: linkgraph_sl.cpp:314
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1809
SaveLoad
SaveLoad type struct.
Definition: saveload.h:652
_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:1417
_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:670
_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:998
SLEG_STRUCT
#define SLEG_STRUCT(name, handler)
Storage of a structs in every savegame version.
Definition: saveload.h:975