OpenTTD Source  1.11.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 #include "../linkgraph/linkgraph.h"
12 #include "../linkgraph/linkgraphjob.h"
13 #include "../linkgraph/linkgraphschedule.h"
14 #include "../network/network.h"
15 #include "../settings_internal.h"
16 #include "saveload.h"
17 
18 #include "../safeguards.h"
19 
22 
23 const SettingDesc *GetSettingDescription(uint index);
24 
25 static uint16 _num_nodes;
26 
32 {
33  static const SaveLoad link_graph_desc[] = {
34  SLE_VAR(LinkGraph, last_compression, SLE_INT32),
35  SLEG_VAR(_num_nodes, SLE_UINT16),
36  SLE_VAR(LinkGraph, cargo, SLE_UINT8),
37  SLE_END()
38  };
39  return link_graph_desc;
40 }
41 
52 {
53  static std::vector<SaveLoad> saveloads;
54  static const char *prefix = "linkgraph.";
55 
56  /* Build the SaveLoad array on first call and don't touch it later on */
57  if (saveloads.size() == 0) {
58  size_t offset_gamesettings = cpp_offsetof(GameSettings, linkgraph);
59  size_t offset_component = cpp_offsetof(LinkGraphJob, settings);
60 
61  size_t prefixlen = strlen(prefix);
62 
63  int setting = 0;
64  const SettingDesc *desc = GetSettingDescription(setting);
65  while (desc->save.cmd != SL_END) {
66  if (desc->desc.name != nullptr && strncmp(desc->desc.name, prefix, prefixlen) == 0) {
67  SaveLoad sl = desc->save;
68  char *&address = reinterpret_cast<char *&>(sl.address);
69  address -= offset_gamesettings;
70  address += offset_component;
71  saveloads.push_back(sl);
72  }
73  desc = GetSettingDescription(++setting);
74  }
75 
76  const SaveLoad job_desc[] = {
77  SLE_VAR(LinkGraphJob, join_date, SLE_INT32),
78  SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
79  SLE_END()
80  };
81 
82  int i = 0;
83  do {
84  saveloads.push_back(job_desc[i++]);
85  } while (saveloads[saveloads.size() - 1].cmd != SL_END);
86  }
87 
88  return &saveloads[0];
89 }
90 
96 {
97  static const SaveLoad schedule_desc[] = {
100  SLE_END()
101  };
102  return schedule_desc;
103 }
104 
105 /* Edges and nodes are saved in the correct order, so we don't need to save their IDs. */
106 
110 static const SaveLoad _node_desc[] = {
111  SLE_CONDVAR(Node, xy, SLE_UINT32, SLV_191, SL_MAX_VERSION),
112  SLE_VAR(Node, supply, SLE_UINT32),
113  SLE_VAR(Node, demand, SLE_UINT32),
114  SLE_VAR(Node, station, SLE_UINT16),
115  SLE_VAR(Node, last_update, SLE_INT32),
116  SLE_END()
117 };
118 
122 static const SaveLoad _edge_desc[] = {
123  SLE_CONDNULL(4, SL_MIN_VERSION, SLV_191), // distance
124  SLE_VAR(Edge, capacity, SLE_UINT32),
125  SLE_VAR(Edge, usage, SLE_UINT32),
126  SLE_VAR(Edge, last_unrestricted_update, SLE_INT32),
127  SLE_CONDVAR(Edge, last_restricted_update, SLE_INT32, SLV_187, SL_MAX_VERSION),
128  SLE_VAR(Edge, next_edge, SLE_UINT16),
129  SLE_END()
130 };
131 
137 {
138  uint size = lg.Size();
139  for (NodeID from = 0; from < size; ++from) {
140  Node *node = &lg.nodes[from];
141  SlObject(node, _node_desc);
143  /* We used to save the full matrix ... */
144  for (NodeID to = 0; to < size; ++to) {
145  SlObject(&lg.edges[from][to], _edge_desc);
146  }
147  } else {
148  /* ... but as that wasted a lot of space we save a sparse matrix now. */
149  for (NodeID to = from; to != INVALID_NODE; to = lg.edges[from][to].next_edge) {
150  SlObject(&lg.edges[from][to], _edge_desc);
151  }
152  }
153  }
154 }
155 
160 static void DoSave_LGRJ(LinkGraphJob *lgj)
161 {
163  _num_nodes = lgj->Size();
164  SlObject(const_cast<LinkGraph *>(&lgj->Graph()), GetLinkGraphDesc());
165  SaveLoad_LinkGraph(const_cast<LinkGraph &>(lgj->Graph()));
166 }
167 
172 static void DoSave_LGRP(LinkGraph *lg)
173 {
174  _num_nodes = lg->Size();
175  SlObject(lg, GetLinkGraphDesc());
176  SaveLoad_LinkGraph(*lg);
177 }
178 
182 static void Load_LGRP()
183 {
184  int index;
185  while ((index = SlIterateArray()) != -1) {
187  /* Impossible as they have been present in previous game. */
188  NOT_REACHED();
189  }
190  LinkGraph *lg = new (index) LinkGraph();
191  SlObject(lg, GetLinkGraphDesc());
192  lg->Init(_num_nodes);
193  SaveLoad_LinkGraph(*lg);
194  }
195 }
196 
200 static void Load_LGRJ()
201 {
202  int index;
203  while ((index = SlIterateArray()) != -1) {
205  /* Impossible as they have been present in previous game. */
206  NOT_REACHED();
207  }
208  LinkGraphJob *lgj = new (index) LinkGraphJob();
210  LinkGraph &lg = const_cast<LinkGraph &>(lgj->Graph());
211  SlObject(&lg, GetLinkGraphDesc());
212  lg.Init(_num_nodes);
213  SaveLoad_LinkGraph(lg);
214  }
215 }
216 
220 static void Load_LGRS()
221 {
223 }
224 
230 {
232  for (LinkGraph *lg : LinkGraph::Iterate()) {
233  for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
234  const Station *st = Station::GetIfValid((*lg)[node_id].Station());
235  if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
236  }
237  }
238 
239  for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
240  LinkGraph *lg = &(const_cast<LinkGraph &>(lgj->Graph()));
241  for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
242  const Station *st = Station::GetIfValid((*lg)[node_id].Station());
243  if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
244  }
245  }
246  }
247 
249 
250  if (!_networking || _network_server) {
252  }
253 }
254 
258 static void Save_LGRP()
259 {
260  for (LinkGraph *lg : LinkGraph::Iterate()) {
261  SlSetArrayIndex(lg->index);
262  SlAutolength((AutolengthProc*)DoSave_LGRP, lg);
263  }
264 }
265 
269 static void Save_LGRJ()
270 {
271  for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
272  SlSetArrayIndex(lgj->index);
273  SlAutolength((AutolengthProc*)DoSave_LGRJ, lgj);
274  }
275 }
276 
280 static void Save_LGRS()
281 {
283 }
284 
288 static void Ptrs_LGRS()
289 {
291 }
292 
293 extern const ChunkHandler _linkgraph_chunk_handlers[] = {
294  { 'LGRP', Save_LGRP, Load_LGRP, nullptr, nullptr, CH_ARRAY },
295  { 'LGRJ', Save_LGRJ, Load_LGRJ, nullptr, nullptr, CH_ARRAY },
296  { 'LGRS', Save_LGRS, Load_LGRS, Ptrs_LGRS, nullptr, CH_LAST }
297 };
SLV_187
@ SLV_187
187 25899 Linkgraph - restricted flows
Definition: saveload.h:267
running
bool running
Is the main loop running?
Definition: win32_v.cpp:54
Save_LGRS
static void Save_LGRS()
Save the link graph schedule.
Definition: linkgraph_sl.cpp:280
LinkGraph::edges
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:535
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
LinkGraph::Node
Updatable node class.
Definition: linkgraph.h:373
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
Load_LGRS
static void Load_LGRS()
Load the link graph schedule.
Definition: linkgraph_sl.cpp:220
SaveLoad::address
void * address
address of variable OR offset of variable in the struct (max offset is 65536)
Definition: saveload.h:531
LinkGraph::nodes
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:534
Station
Station data structure.
Definition: station_base.h:450
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:53
LinkGraphSchedule
Definition: linkgraphschedule.h:36
SettingDesc::save
SaveLoad save
Internal structure (going to savegame, parts to config)
Definition: settings_internal.h:110
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:227
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:558
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:414
SaveLoad_LinkGraph
void SaveLoad_LinkGraph(LinkGraph &lg)
Save/load a link graph.
Definition: linkgraph_sl.cpp:136
saveload.h
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
Load_LGRJ
static void Load_LGRJ()
Load all link graph jobs.
Definition: linkgraph_sl.cpp:200
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition: linkgraphschedule.cpp:110
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
LinkGraph::Init
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:280
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:684
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
const SaveLoad * GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
Definition: linkgraph_sl.cpp:51
Save_LGRJ
static void Save_LGRJ()
Save all link graph jobs.
Definition: linkgraph_sl.cpp:269
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:693
LinkGraph::BaseEdge
An edge in the link graph.
Definition: linkgraph.h:62
SaveLoad::cmd
SaveLoadType cmd
the action to take with the saved/loaded type, All types need different action
Definition: saveload.h:522
LinkGraph::Size
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:498
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:821
GameSettings
All settings together for the game.
Definition: settings_type.h:546
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
SLE_LST
#define SLE_LST(base, variable, type)
Storage of a list in every savegame version.
Definition: saveload.h:670
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
Save_LGRP
static void Save_LGRP()
Save all link graphs.
Definition: linkgraph_sl.cpp:258
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
GetLinkGraphScheduleDesc
const SaveLoad * GetLinkGraphScheduleDesc()
Get a SaveLoad array for the link graph schedule.
Definition: linkgraph_sl.cpp:95
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:405
SLEG_VAR
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:767
LinkGraphJob::Size
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:335
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:628
_edge_desc
static const SaveLoad _edge_desc[]
SaveLoad desc for a link graph edge.
Definition: linkgraph_sl.cpp:122
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:378
SettingDesc
Definition: settings_internal.h:108
AfterLoadLinkGraphs
void AfterLoadLinkGraphs()
Spawn the threads for running link graph calculations.
Definition: linkgraph_sl.cpp:229
_node_desc
static const SaveLoad _node_desc[]
SaveLoad desc for a link graph node.
Definition: linkgraph_sl.cpp:110
Pool::PoolItem<&_link_graph_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:299
SettingDesc::desc
SettingDescBase desc
Settings structure (going to configuration file)
Definition: settings_internal.h:109
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
Ptrs_LGRS
static void Ptrs_LGRS()
Substitute pointers in link graph schedule.
Definition: linkgraph_sl.cpp:288
REF_LINK_GRAPH
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition: saveload.h:404
DoSave_LGRJ
static void DoSave_LGRJ(LinkGraphJob *lgj)
Save a link graph job.
Definition: linkgraph_sl.cpp:160
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
SettingDescBase::name
const char * name
name of the setting. Used in configuration file and for console
Definition: settings_internal.h:92
SLV_191
@ SLV_191
191 26636 FS#6026 Fix disaster vehicle storage (No bump) 191 26646 FS#6041 Linkgraph - store location...
Definition: saveload.h:272
DoSave_LGRP
static void DoSave_LGRP(LinkGraph *lg)
Save a link graph.
Definition: linkgraph_sl.cpp:172
LinkGraph::BaseNode
Node of the link graph.
Definition: linkgraph.h:47
GetLinkGraphDesc
const SaveLoad * GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
Definition: linkgraph_sl.cpp:31
SaveLoad
SaveLoad type struct.
Definition: saveload.h:520
Load_LGRP
static void Load_LGRP()
Load all link graphs.
Definition: linkgraph_sl.cpp:182
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631