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/>.
10 /** @file linkgraph_sl.cpp Code handling saving and loading of link graphs */
12 #include "../stdafx.h"
13 #include "../linkgraph/linkgraph.h"
14 #include "../linkgraph/linkgraphjob.h"
15 #include "../linkgraph/linkgraphschedule.h"
16 #include "../settings_internal.h"
19 #include "../safeguards.h"
21 typedef LinkGraph::BaseNode Node
;
22 typedef LinkGraph::BaseEdge Edge
;
24 const SettingDesc
*GetSettingDescription(uint index
);
26 static uint16 _num_nodes
;
29 * Get a SaveLoad array for a link graph.
30 * @return SaveLoad array for link graph.
32 const SaveLoad
*GetLinkGraphDesc()
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
),
40 return link_graph_desc
;
44 * Get a SaveLoad array for a link graph job. The settings struct is derived from
45 * the global settings saveload array. The exact entries are calculated when the function
46 * is called the first time.
47 * It's necessary to keep a copy of the settings for each link graph job so that you can
48 * change the settings while in-game and still not mess with current link graph runs.
49 * Of course the settings have to be saved and loaded, too, to avoid desyncs.
50 * @return Array of SaveLoad structs.
52 const SaveLoad
*GetLinkGraphJobDesc()
54 static SmallVector
<SaveLoad
, 16> saveloads
;
55 static const char *prefix
= "linkgraph.";
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
);
62 size_t prefixlen
= strlen(prefix
);
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
;
74 desc
= GetSettingDescription(++setting
);
77 const SaveLoad job_desc
[] = {
78 SLE_VAR(LinkGraphJob
, join_date
, SLE_INT32
),
79 SLE_VAR(LinkGraphJob
, link_graph
.index
, SLE_UINT16
),
85 *(saveloads
.Append()) = job_desc
[i
++];
86 } while (saveloads
[saveloads
.Length() - 1].cmd
!= SL_END
);
93 * Get a SaveLoad array for the link graph schedule.
94 * @return SaveLoad array for the link graph schedule.
96 const SaveLoad
*GetLinkGraphScheduleDesc()
98 static const SaveLoad schedule_desc
[] = {
99 SLE_LST(LinkGraphSchedule
, schedule
, REF_LINK_GRAPH
),
100 SLE_LST(LinkGraphSchedule
, running
, REF_LINK_GRAPH_JOB
),
103 return schedule_desc
;
106 /* Edges and nodes are saved in the correct order, so we don't need to save their IDs. */
109 * SaveLoad desc for a link graph node.
111 static const SaveLoad _node_desc
[] = {
112 SLE_CONDVAR(Node
, xy
, SLE_UINT32
, 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
),
121 * SaveLoad desc for a link graph edge.
123 static const SaveLoad _edge_desc
[] = {
124 SLE_CONDNULL(4, 0, 190), // 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
, 187, SL_MAX_VERSION
),
129 SLE_VAR(Edge
, next_edge
, SLE_UINT16
),
134 * Save/load a link graph.
135 * @param comp Link graph to be saved or loaded.
137 void SaveLoad_LinkGraph(LinkGraph
&lg
)
139 uint size
= lg
.Size();
140 for (NodeID from
= 0; from
< size
; ++from
) {
141 Node
*node
= &lg
.nodes
[from
];
142 SlObject(node
, _node_desc
);
143 if (IsSavegameVersionBefore(191)) {
144 /* We used to save the full matrix ... */
145 for (NodeID to
= 0; to
< size
; ++to
) {
146 SlObject(&lg
.edges
[from
][to
], _edge_desc
);
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
);
158 * Save a link graph job.
159 * @param lgj LinkGraphJob to be saved.
161 static void DoSave_LGRJ(LinkGraphJob
*lgj
)
163 SlObject(lgj
, GetLinkGraphJobDesc());
164 _num_nodes
= lgj
->Size();
165 SlObject(const_cast<LinkGraph
*>(&lgj
->Graph()), GetLinkGraphDesc());
166 SaveLoad_LinkGraph(const_cast<LinkGraph
&>(lgj
->Graph()));
171 * @param lg LinkGraph to be saved.
173 static void DoSave_LGRP(LinkGraph
*lg
)
175 _num_nodes
= lg
->Size();
176 SlObject(lg
, GetLinkGraphDesc());
177 SaveLoad_LinkGraph(*lg
);
181 * Load all link graphs.
183 static void Load_LGRP()
186 while ((index
= SlIterateArray()) != -1) {
187 if (!LinkGraph::CanAllocateItem()) {
188 /* Impossible as they have been present in previous game. */
191 LinkGraph
*lg
= new (index
) LinkGraph();
192 SlObject(lg
, GetLinkGraphDesc());
193 lg
->Init(_num_nodes
);
194 SaveLoad_LinkGraph(*lg
);
199 * Load all link graph jobs.
201 static void Load_LGRJ()
204 while ((index
= SlIterateArray()) != -1) {
205 if (!LinkGraphJob::CanAllocateItem()) {
206 /* Impossible as they have been present in previous game. */
209 LinkGraphJob
*lgj
= new (index
) LinkGraphJob();
210 SlObject(lgj
, GetLinkGraphJobDesc());
211 LinkGraph
&lg
= const_cast<LinkGraph
&>(lgj
->Graph());
212 SlObject(&lg
, GetLinkGraphDesc());
214 SaveLoad_LinkGraph(lg
);
219 * Load the link graph schedule.
221 static void Load_LGRS()
223 SlObject(&LinkGraphSchedule::instance
, GetLinkGraphScheduleDesc());
227 * Spawn the threads for running link graph calculations.
228 * Has to be done after loading as the cargo classes might have changed.
230 void AfterLoadLinkGraphs()
232 if (IsSavegameVersionBefore(191)) {
234 FOR_ALL_LINK_GRAPHS(lg
) {
235 for (NodeID node_id
= 0; node_id
< lg
->Size(); ++node_id
) {
236 (*lg
)[node_id
].UpdateLocation(Station::Get((*lg
)[node_id
].Station())->xy
);
241 FOR_ALL_LINK_GRAPH_JOBS(lgj
) {
242 lg
= &(const_cast<LinkGraph
&>(lgj
->Graph()));
243 for (NodeID node_id
= 0; node_id
< lg
->Size(); ++node_id
) {
244 (*lg
)[node_id
].UpdateLocation(Station::Get((*lg
)[node_id
].Station())->xy
);
249 LinkGraphSchedule::instance
.SpawnAll();
253 * Save all link graphs.
255 static void Save_LGRP()
258 FOR_ALL_LINK_GRAPHS(lg
) {
259 SlSetArrayIndex(lg
->index
);
260 SlAutolength((AutolengthProc
*)DoSave_LGRP
, lg
);
265 * Save all link graph jobs.
267 static void Save_LGRJ()
270 FOR_ALL_LINK_GRAPH_JOBS(lgj
) {
271 SlSetArrayIndex(lgj
->index
);
272 SlAutolength((AutolengthProc
*)DoSave_LGRJ
, lgj
);
277 * Save the link graph schedule.
279 static void Save_LGRS()
281 SlObject(&LinkGraphSchedule::instance
, GetLinkGraphScheduleDesc());
285 * Substitute pointers in link graph schedule.
287 static void Ptrs_LGRS()
289 SlObject(&LinkGraphSchedule::instance
, GetLinkGraphScheduleDesc());
292 extern const ChunkHandler _linkgraph_chunk_handlers
[] = {
293 { 'LGRP', Save_LGRP
, Load_LGRP
, NULL
, NULL
, CH_ARRAY
},
294 { 'LGRJ', Save_LGRJ
, Load_LGRJ
, NULL
, NULL
, CH_ARRAY
},
295 { 'LGRS', Save_LGRS
, Load_LGRS
, Ptrs_LGRS
, NULL
, CH_LAST
}