OpenTTD
linkgraph_sl.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../linkgraph/linkgraph.h"
14 #include "../linkgraph/linkgraphjob.h"
15 #include "../linkgraph/linkgraphschedule.h"
16 #include "../settings_internal.h"
17 #include "saveload.h"
18 
19 #include "../safeguards.h"
20 
23 
24 const SettingDesc *GetSettingDescription(uint index);
25 
26 static uint16 _num_nodes;
27 
33 {
34  static const SaveLoad link_graph_desc[] = {
35  SLE_VAR(LinkGraph, last_compression, SLE_INT32),
36  SLEG_VAR(_num_nodes, SLE_UINT16),
37  SLE_VAR(LinkGraph, cargo, SLE_UINT8),
38  SLE_END()
39  };
40  return link_graph_desc;
41 }
42 
53 {
54  static SmallVector<SaveLoad, 16> saveloads;
55  static const char *prefix = "linkgraph.";
56 
57  /* Build the SaveLoad array on first call and don't touch it later on */
58  if (saveloads.Length() == 0) {
59  size_t offset_gamesettings = cpp_offsetof(GameSettings, linkgraph);
60  size_t offset_component = cpp_offsetof(LinkGraphJob, settings);
61 
62  size_t prefixlen = strlen(prefix);
63 
64  int setting = 0;
65  const SettingDesc *desc = GetSettingDescription(setting);
66  while (desc->save.cmd != SL_END) {
67  if (desc->desc.name != NULL && strncmp(desc->desc.name, prefix, prefixlen) == 0) {
68  SaveLoad sl = desc->save;
69  char *&address = reinterpret_cast<char *&>(sl.address);
70  address -= offset_gamesettings;
71  address += offset_component;
72  *(saveloads.Append()) = sl;
73  }
74  desc = GetSettingDescription(++setting);
75  }
76 
77  const SaveLoad job_desc[] = {
78  SLE_VAR(LinkGraphJob, join_date, SLE_INT32),
79  SLE_VAR(LinkGraphJob, link_graph.index, SLE_UINT16),
80  SLE_END()
81  };
82 
83  int i = 0;
84  do {
85  *(saveloads.Append()) = job_desc[i++];
86  } while (saveloads[saveloads.Length() - 1].cmd != SL_END);
87  }
88 
89  return &saveloads[0];
90 }
91 
97 {
98  static const SaveLoad schedule_desc[] = {
101  SLE_END()
102  };
103  return schedule_desc;
104 }
105 
106 /* Edges and nodes are saved in the correct order, so we don't need to save their IDs. */
107 
111 static const SaveLoad _node_desc[] = {
112  SLE_CONDVAR(Node, xy, SLE_UINT32, SLV_191, SL_MAX_VERSION),
113  SLE_VAR(Node, supply, SLE_UINT32),
114  SLE_VAR(Node, demand, SLE_UINT32),
115  SLE_VAR(Node, station, SLE_UINT16),
116  SLE_VAR(Node, last_update, SLE_INT32),
117  SLE_END()
118 };
119 
123 static const SaveLoad _edge_desc[] = {
124  SLE_CONDNULL(4, SL_MIN_VERSION, SLV_191), // distance
125  SLE_VAR(Edge, capacity, SLE_UINT32),
126  SLE_VAR(Edge, usage, SLE_UINT32),
127  SLE_VAR(Edge, last_unrestricted_update, SLE_INT32),
128  SLE_CONDVAR(Edge, last_restricted_update, SLE_INT32, SLV_187, SL_MAX_VERSION),
129  SLE_VAR(Edge, next_edge, SLE_UINT16),
130  SLE_END()
131 };
132 
138 {
139  uint size = lg.Size();
140  for (NodeID from = 0; from < size; ++from) {
141  Node *node = &lg.nodes[from];
142  SlObject(node, _node_desc);
144  /* We used to save the full matrix ... */
145  for (NodeID to = 0; to < size; ++to) {
146  SlObject(&lg.edges[from][to], _edge_desc);
147  }
148  } else {
149  /* ... but as that wasted a lot of space we save a sparse matrix now. */
150  for (NodeID to = from; to != INVALID_NODE; to = lg.edges[from][to].next_edge) {
151  SlObject(&lg.edges[from][to], _edge_desc);
152  }
153  }
154  }
155 }
156 
161 static void DoSave_LGRJ(LinkGraphJob *lgj)
162 {
164  _num_nodes = lgj->Size();
165  SlObject(const_cast<LinkGraph *>(&lgj->Graph()), GetLinkGraphDesc());
166  SaveLoad_LinkGraph(const_cast<LinkGraph &>(lgj->Graph()));
167 }
168 
173 static void DoSave_LGRP(LinkGraph *lg)
174 {
175  _num_nodes = lg->Size();
176  SlObject(lg, GetLinkGraphDesc());
177  SaveLoad_LinkGraph(*lg);
178 }
179 
183 static void Load_LGRP()
184 {
185  int index;
186  while ((index = SlIterateArray()) != -1) {
188  /* Impossible as they have been present in previous game. */
189  NOT_REACHED();
190  }
191  LinkGraph *lg = new (index) LinkGraph();
192  SlObject(lg, GetLinkGraphDesc());
193  lg->Init(_num_nodes);
194  SaveLoad_LinkGraph(*lg);
195  }
196 }
197 
201 static void Load_LGRJ()
202 {
203  int index;
204  while ((index = SlIterateArray()) != -1) {
206  /* Impossible as they have been present in previous game. */
207  NOT_REACHED();
208  }
209  LinkGraphJob *lgj = new (index) LinkGraphJob();
211  LinkGraph &lg = const_cast<LinkGraph &>(lgj->Graph());
212  SlObject(&lg, GetLinkGraphDesc());
213  lg.Init(_num_nodes);
214  SaveLoad_LinkGraph(lg);
215  }
216 }
217 
221 static void Load_LGRS()
222 {
224 }
225 
231 {
233  LinkGraph *lg;
234  FOR_ALL_LINK_GRAPHS(lg) {
235  for (NodeID node_id = 0; node_id < lg->Size(); ++node_id) {
236  const Station *st = Station::GetIfValid((*lg)[node_id].Station());
237  if (st != nullptr) (*lg)[node_id].UpdateLocation(st->xy);
238  }
239  }
240 
241  LinkGraphJob *lgj;
242  FOR_ALL_LINK_GRAPH_JOBS(lgj) {
243  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);
247  }
248  }
249  }
250 
252 }
253 
257 static void Save_LGRP()
258 {
259  LinkGraph *lg;
260  FOR_ALL_LINK_GRAPHS(lg) {
261  SlSetArrayIndex(lg->index);
262  SlAutolength((AutolengthProc*)DoSave_LGRP, lg);
263  }
264 }
265 
269 static void Save_LGRJ()
270 {
271  LinkGraphJob *lgj;
272  FOR_ALL_LINK_GRAPH_JOBS(lgj) {
273  SlSetArrayIndex(lgj->index);
274  SlAutolength((AutolengthProc*)DoSave_LGRJ, lgj);
275  }
276 }
277 
281 static void Save_LGRS()
282 {
284 }
285 
289 static void Ptrs_LGRS()
290 {
292 }
293 
294 extern const ChunkHandler _linkgraph_chunk_handlers[] = {
295  { 'LGRP', Save_LGRP, Load_LGRP, NULL, NULL, CH_ARRAY },
296  { 'LGRJ', Save_LGRJ, Load_LGRJ, NULL, NULL, CH_ARRAY },
297  { 'LGRS', Save_LGRS, Load_LGRS, Ptrs_LGRS, NULL, CH_LAST }
298 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:632
static void Load_LGRJ()
Load all link graph jobs.
const SaveLoad * GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
static void DoSave_LGRJ(LinkGraphJob *lgj)
Save a link graph job.
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:753
An edge in the link graph.
Definition: linkgraph.h:63
static void Ptrs_LGRS()
Substitute pointers in link graph schedule.
All settings together for the game.
void SpawnAll()
Start all threads in the running list.
#define SLE_LST(base, variable, type)
Storage of a list in every savegame version.
Definition: saveload.h:618
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:22
Load/save a reference to a link graph job.
Definition: saveload.h:372
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
191 26636 FS#6026 Fix disaster vehicle storage (No bump) 191 26646 FS#6041 Linkgraph - store location...
Definition: saveload.h:273
void * address
address of variable OR offset of variable in the struct (max offset is 65536)
Definition: saveload.h:497
Simple vector template class.
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:706
const char * name
name of the setting. Used in configuration file and for console
T * Append(uint to_add=1)
Append an item and return it.
static void Load_LGRS()
Load the link graph schedule.
A connected component of a link graph.
Definition: linkgraph.h:40
const SaveLoad * GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
SaveLoad save
Internal structure (going to savegame, parts to config)
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:499
Functions/types related to saving and loading games.
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:524
static const SaveLoad _node_desc[]
SaveLoad desc for a link graph node.
Updatable node class.
Definition: linkgraph.h:374
uint Length() const
Get the number of items in the list.
Highest possible saveload version.
Definition: saveload.h:295
static void Save_LGRP()
Save all link graphs.
static LinkGraphSchedule instance
Static instance of LinkGraphSchedule.
static const SaveLoad _edge_desc[]
SaveLoad desc for a link graph edge.
First savegame version.
Definition: saveload.h:32
static void Save_LGRJ()
Save all link graph jobs.
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:536
const SaveLoad * GetLinkGraphScheduleDesc()
Get a SaveLoad array for the link graph schedule.
void AfterLoadLinkGraphs()
Spawn the threads for running link graph calculations.
An updatable edge class.
Definition: linkgraph.h:293
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:634
Handlers and description of chunk.
Definition: saveload.h:346
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:641
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:280
static void Save_LGRS()
Save the link graph schedule.
Node of the link graph.
Definition: linkgraph.h:48
TileIndex xy
Base tile of the station.
SaveLoadType cmd
the action to take with the saved/loaded type, All types need different action
Definition: saveload.h:488
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1543
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:312
const LinkGraph & Graph() const
Get a reference to the underlying link graph.
Definition: linkgraphjob.h:336
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1571
SaveLoad type struct.
Definition: saveload.h:486
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:584
static void Load_LGRP()
Load all link graphs.
Load/save a reference to a link graph.
Definition: saveload.h:371
static void DoSave_LGRP(LinkGraph *lg)
Save a link graph.
187 25899 Linkgraph - restricted flows
Definition: saveload.h:268
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:535
SettingDescBase desc
Settings structure (going to configuration file)
Station data structure.
Definition: station_base.h:446
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:31
Last chunk in this array.
Definition: saveload.h:381
void SaveLoad_LinkGraph(LinkGraph &lg)
Save/load a link graph.
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.