OpenTTD Source 20250612-master-gb012d9e3dc
order_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 "saveload_internal.h"
16#include "../order_backup.h"
17#include "../settings_type.h"
18#include "../network/network.h"
19
20#include "../safeguards.h"
21
27{
28 uint8_t old_flags = this->flags;
29 this->flags = 0;
30
31 /* First handle non-stop - use value from savegame if possible, else use value from config file */
33 /* OFB_NON_STOP */
35 } else {
37 }
38
39 switch (this->GetType()) {
40 /* Only a few types need the other savegame conversions. */
41 case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
42 default: return;
43 }
44
45 if (this->GetType() != OT_GOTO_DEPOT) {
46 /* Then the load flags */
47 if ((old_flags & 2) != 0) { // OFB_UNLOAD
49 } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
51 } else {
52 /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
54 }
55
56 if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OSL_PLATFORM_FAR_END);
57
58 /* Finally fix the unload flags */
59 if ((old_flags & 1) != 0) { // OFB_TRANSFER
61 } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
63 } else {
65 }
66 } else {
67 /* Then the depot action flags */
68 this->SetDepotActionType(((old_flags & 6) == 4) ? ODATFB_HALT : ODATF_SERVICE_ONLY);
69
70 /* Finally fix the depot type flags */
71 uint t = ((old_flags & 6) == 6) ? ODTFB_SERVICE : ODTF_MANUAL;
72 if ((old_flags & 2) != 0) t |= ODTFB_PART_OF_ORDERS;
74 }
75}
76
82static Order UnpackVersion4Order(uint16_t packed)
83{
84 return Order(GB(packed, 0, 4), GB(packed, 4, 4), GB(packed, 8, 8));
85}
86
92Order UnpackOldOrder(uint16_t packed)
93{
94 Order order = UnpackVersion4Order(packed);
95
96 /*
97 * Sanity check
98 * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
99 */
100 if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
101
102 return order;
103}
104
106static std::vector<OldOrderSaveLoadItem> _old_order_saveload_pool;
107
112{
114 _old_order_saveload_pool.shrink_to_fit();
115}
116
123{
124 if (ref_index == 0) return nullptr;
125 assert(ref_index <= _old_order_saveload_pool.size());
126 return &_old_order_saveload_pool[ref_index - 1];
127}
128
135{
136 assert(pool_index < UINT32_MAX);
137 if (pool_index >= _old_order_saveload_pool.size()) _old_order_saveload_pool.resize(pool_index + 1);
138 return _old_order_saveload_pool[pool_index];
139}
140
141SaveLoadTable GetOrderDescription()
142{
143 static const SaveLoad _order_desc[] = {
144 SLE_VARNAME(OldOrderSaveLoadItem, order.type, "type", SLE_UINT8),
145 SLE_VARNAME(OldOrderSaveLoadItem, order.flags, "flags", SLE_UINT8),
146 SLE_VARNAME(OldOrderSaveLoadItem, order.dest, "dest", SLE_UINT16),
147 SLE_CONDVARNAME(OldOrderSaveLoadItem, next, "next", SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_69),
148 SLE_CONDVARNAME(OldOrderSaveLoadItem, next, "next", SLE_UINT32, SLV_69, SL_MAX_VERSION),
149 SLE_CONDVARNAME(OldOrderSaveLoadItem, order.refit_cargo, "refit_cargo", SLE_UINT8, SLV_36, SL_MAX_VERSION),
150 SLE_CONDVARNAME(OldOrderSaveLoadItem, order.wait_time, "wait_time", SLE_UINT16, SLV_67, SL_MAX_VERSION),
151 SLE_CONDVARNAME(OldOrderSaveLoadItem, order.travel_time, "travel_time", SLE_UINT16, SLV_67, SL_MAX_VERSION),
152 SLE_CONDVARNAME(OldOrderSaveLoadItem, order.max_speed, "max_speed", SLE_UINT16, SLV_172, SL_MAX_VERSION),
153 };
154
155 return _order_desc;
156}
157
160
161 void Load() const override
162 {
164 /* Version older than 5.2 did not have a ->next pointer. Convert them
165 * (in the old days, the orderlist was 5000 items big) */
166 size_t len = SlGetFieldLength();
167
169 /* Pre-version 5 had another layout for orders
170 * (uint16_t instead of uint32_t) */
171 len /= sizeof(uint16_t);
172 std::vector<uint16_t> orders(len);
173
174 SlCopy(&orders[0], len, SLE_UINT16);
175
176 for (size_t i = 0; i < len; ++i) {
177 auto &item = AllocateOldOrder(i);
178 item.order.AssignOrder(UnpackVersion4Order(orders[i]));
179 }
180 } else if (IsSavegameVersionBefore(SLV_5, 2)) {
181 len /= sizeof(uint32_t);
182 std::vector<uint32_t> orders(len);
183
184 SlCopy(&orders[0], len, SLE_UINT32);
185
186 for (size_t i = 0; i < len; ++i) {
187 auto &item = AllocateOldOrder(i);
188 item.order = Order(GB(orders[i], 0, 8), GB(orders[i], 8, 8), GB(orders[i], 16, 16));
189 }
190 }
191
192 /* Update all the next pointer. The orders were built like this:
193 * While the order is valid, the previous order will get its next pointer set */
194 for (uint32_t num = 1; OldOrderSaveLoadItem item : _old_order_saveload_pool) {
195 if (!item.order.IsType(OT_NOTHING) && num > 1) {
196 OldOrderSaveLoadItem *prev = GetOldOrder(num - 1);
197 if (prev != nullptr) prev->next = num;
198 }
199 ++num;
200 }
201 } else {
202 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat);
203
204 int index;
205
206 while ((index = SlIterateArray()) != -1) {
207 auto &item = AllocateOldOrder(index);
208 SlObject(&item, slt);
209 }
210 }
211 }
212};
213
214template <typename T>
215class SlOrders : public VectorSaveLoadHandler<SlOrders<T>, T, Order> {
216public:
217 static inline const SaveLoad description[] = {
218 SLE_VAR(Order, type, SLE_UINT8),
219 SLE_VAR(Order, flags, SLE_UINT8),
220 SLE_VAR(Order, dest, SLE_UINT16),
221 SLE_VAR(Order, refit_cargo, SLE_UINT8),
222 SLE_VAR(Order, wait_time, SLE_UINT16),
223 SLE_VAR(Order, travel_time, SLE_UINT16),
224 SLE_VAR(Order, max_speed, SLE_UINT16),
225 };
226 static inline const SaveLoadCompatTable compat_description = {};
227
228 std::vector<Order> &GetVector(T *container) const override { return container->orders; }
229
230 void LoadCheck(T *container) const override { this->Load(container); }
231};
232
233/* Instantiate SlOrders classes. */
234template class SlOrders<OrderList>;
235template class SlOrders<OrderBackup>;
236
237SaveLoadTable GetOrderListDescription()
238{
239 static const SaveLoad _orderlist_desc[] = {
240 SLE_CONDVARNAME(OrderList, old_order_index, "first", SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_69),
241 SLE_CONDVARNAME(OrderList, old_order_index, "first", SLE_UINT32, SLV_69, SLV_ORDERS_OWNED_BY_ORDERLIST),
243 };
244
245 return _orderlist_desc;
246}
247
249 ORDLChunkHandler() : ChunkHandler('ORDL', CH_TABLE) {}
250
251 void Save() const override
252 {
253 const SaveLoadTable slt = GetOrderListDescription();
254 SlTableHeader(slt);
255
256 for (OrderList *list : OrderList::Iterate()) {
257 SlSetArrayIndex(list->index);
258 SlObject(list, slt);
259 }
260 }
261
262 void Load() const override
263 {
264 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat);
265
266 int index;
267
268 while ((index = SlIterateArray()) != -1) {
269 OrderList *list = new (OrderListID(index)) OrderList();
270 SlObject(list, slt);
271 }
272
273 }
274
275 void FixPointers() const override
276 {
278
279 for (OrderList *list : OrderList::Iterate()) {
280 SlObject(list, GetOrderListDescription());
281
282 if (migrate_orders) {
283 std::vector<Order> orders;
284 for (OldOrderSaveLoadItem *old_order = GetOldOrder(list->old_order_index); old_order != nullptr; old_order = GetOldOrder(old_order->next)) {
285 orders.push_back(std::move(old_order->order));
286 }
287 list->orders = std::move(orders);
288 }
289 }
290 }
291};
292
293SaveLoadTable GetOrderBackupDescription()
294{
295 static const SaveLoad _order_backup_desc[] = {
296 SLE_VAR(OrderBackup, user, SLE_UINT32),
297 SLE_VAR(OrderBackup, tile, SLE_UINT32),
298 SLE_VAR(OrderBackup, group, SLE_UINT16),
299 SLE_CONDVAR(OrderBackup, service_interval, SLE_FILE_U32 | SLE_VAR_U16, SL_MIN_VERSION, SLV_192),
300 SLE_CONDVAR(OrderBackup, service_interval, SLE_UINT16, SLV_192, SL_MAX_VERSION),
301 SLE_SSTR(OrderBackup, name, SLE_STR),
303 SLE_VAR(OrderBackup, cur_real_order_index, SLE_UINT8),
304 SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8, SLV_176, SL_MAX_VERSION),
305 SLE_CONDVAR(OrderBackup, current_order_time, SLE_UINT32, SLV_176, SL_MAX_VERSION),
306 SLE_CONDVAR(OrderBackup, lateness_counter, SLE_INT32, SLV_176, SL_MAX_VERSION),
307 SLE_CONDVAR(OrderBackup, timetable_start, SLE_FILE_I32 | SLE_VAR_U64, SLV_176, SLV_TIMETABLE_START_TICKS_FIX),
309 SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_176, SLV_180),
310 SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
311 SLE_CONDVARNAME(OrderBackup, old_order_index, "orders", SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_69),
312 SLE_CONDVARNAME(OrderBackup, old_order_index, "orders", SLE_UINT32, SLV_69, SLV_ORDERS_OWNED_BY_ORDERLIST),
314 };
315
316 return _order_backup_desc;
317}
318
320 BKORChunkHandler() : ChunkHandler('BKOR', CH_TABLE) {}
321
322 void Save() const override
323 {
324 const SaveLoadTable slt = GetOrderBackupDescription();
325 SlTableHeader(slt);
326
327 /* We only save this when we're a network server
328 * as we want this information on our clients. For
329 * normal games this information isn't needed. */
330 if (!_networking || !_network_server) return;
331
332 for (OrderBackup *ob : OrderBackup::Iterate()) {
333 SlSetArrayIndex(ob->index);
334 SlObject(ob, slt);
335 }
336 }
337
338 void Load() const override
339 {
340 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat);
341
342 int index;
343
344 while ((index = SlIterateArray()) != -1) {
345 /* set num_orders to 0 so it's a valid OrderList */
346 OrderBackup *ob = new (OrderBackupID(index)) OrderBackup();
347 SlObject(ob, slt);
348 }
349 }
350
351 void FixPointers() const override
352 {
354
355 for (OrderBackup *ob : OrderBackup::Iterate()) {
356 SlObject(ob, GetOrderBackupDescription());
357
358 if (migrate_orders) {
359 std::vector<Order> orders;
360 for (OldOrderSaveLoadItem *old_order = GetOldOrder(ob->old_order_index); old_order != nullptr; old_order = GetOldOrder(old_order->next)) {
361 orders.push_back(std::move(old_order->order));
362 }
363 ob->orders = std::move(orders);
364 }
365 }
366 }
367};
368
369static const BKORChunkHandler BKOR;
370static const ORDRChunkHandler ORDR;
371static const ORDLChunkHandler ORDL;
372static const ChunkHandlerRef order_chunk_handlers[] = {
373 BKOR,
374 ORDR,
375 ORDL,
376};
377
378extern const ChunkHandlerTable _order_chunk_handlers(order_chunk_handlers);
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
std::vector< Order > & GetVector(T *container) const override
Get instance of vector to load/save.
Definition order_sl.cpp:228
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1372
SavegameType _savegame_type
type of savegame we are loading
Definition saveload.cpp:65
bool _networking
are we in networking mode?
Definition network.cpp:67
bool _network_server
network-server is active
Definition network.cpp:68
PoolID< uint8_t, struct OrderBackupIDTag, 255, 0xFF > OrderBackupID
Unique identifier for an order backup.
static std::vector< OldOrderSaveLoadItem > _old_order_saveload_pool
Temporary storage for conversion from old order pool.
Definition order_sl.cpp:106
Order UnpackOldOrder(uint16_t packed)
Unpacks a order from savegames made with TTD(Patch)
Definition order_sl.cpp:92
static Order UnpackVersion4Order(uint16_t packed)
Unpacks a order from savegames with version 4 and lower.
Definition order_sl.cpp:82
void ClearOldOrders()
Clear all old orders.
Definition order_sl.cpp:111
OldOrderSaveLoadItem & AllocateOldOrder(size_t pool_index)
Allocate an old order with the given pool index.
Definition order_sl.cpp:134
OldOrderSaveLoadItem * GetOldOrder(size_t ref_index)
Get a pointer to an old order with the given reference index.
Definition order_sl.cpp:122
Loading of order chunks before table headers were added.
const SaveLoadCompat _order_sl_compat[]
Original field order for _order_desc.
const SaveLoadCompat _orderlist_sl_compat[]
Original field order for _orderlist_desc.
const SaveLoadCompat _order_backup_sl_compat[]
Original field order for _order_backup_desc.
@ OSL_PLATFORM_FAR_END
Stop at the far end of the platform.
Definition order_type.h:101
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition order_type.h:70
@ OUF_UNLOAD_IF_POSSIBLE
Unload all cargo that the station accepts.
Definition order_type.h:68
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition order_type.h:69
OrderDepotTypeFlags
Reasons that could cause us to go to the depot.
Definition order_type.h:108
@ ODTFB_PART_OF_ORDERS
This depot order is because of a regular order.
Definition order_type.h:111
@ ODTFB_SERVICE
This depot order is because of the servicing limit.
Definition order_type.h:110
@ ODTF_MANUAL
Manually initiated order.
Definition order_type.h:109
@ ODATFB_HALT
Service the vehicle and then halt it.
Definition order_type.h:119
@ ODATF_SERVICE_ONLY
Only service the vehicle.
Definition order_type.h:118
@ OLFB_FULL_LOAD
Full load all cargoes of the consist.
Definition order_type.h:79
@ OLFB_NO_LOAD
Do not load anything.
Definition order_type.h:81
@ OLF_LOAD_IF_POSSIBLE
Load as long as there is cargo that fits in the train.
Definition order_type.h:78
@ OLF_FULL_LOAD_ANY
Full load a single cargo of the consist.
Definition order_type.h:80
@ ONSF_NO_STOP_AT_ANY_STATION
The vehicle will not stop at any stations it passes including the destination.
Definition order_type.h:91
@ ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition order_type.h:88
@ ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
The vehicle will not stop at any stations it passes except the destination.
Definition order_type.h:89
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:665
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:788
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Functions/types related to saving and loading games.
@ SGT_TTD
TTD savegame (can be detected incorrectly)
Definition saveload.h:429
@ SGT_TTO
TTO savegame.
Definition saveload.h:433
#define SLE_VARNAME(base, variable, name, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1016
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:514
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition saveload.h:606
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:517
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:523
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:873
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1268
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1051
@ SLV_69
69 10319
Definition saveload.h:125
@ SLV_172
172 23947
Definition saveload.h:249
@ SLV_67
67 10236
Definition saveload.h:123
@ SLV_36
36 6624
Definition saveload.h:86
@ SLV_180
180 24998 1.3.x
Definition saveload.h:259
@ SLV_176
176 24446
Definition saveload.h:254
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:406
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
@ SLV_22
22 3726
Definition saveload.h:69
@ SLV_192
192 26700 FS#6066 Fix saving of order backups
Definition saveload.h:274
@ SLV_TIMETABLE_START_TICKS_FIX
322 PR#11557 Fix for missing convert timetable start from a date to ticks.
Definition saveload.h:366
@ SLV_ORDERS_OWNED_BY_ORDERLIST
354 PR#13948 Orders stored in OrderList, pool removed.
Definition saveload.h:404
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition saveload.h:43
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:894
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition saveload.h:520
#define SLEG_CONDSTRUCTLIST(name, handler, from, to)
Storage of a list of structs in some savegame versions.
Definition saveload.h:1183
@ CH_READONLY
Chunk is never saved.
Definition saveload.h:464
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1007
#define SLE_CONDVARNAME(base, variable, name, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:884
Declaration of functions used in more save/load files.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
void Save() const override
Save the chunk.
Definition order_sl.cpp:322
void Load() const override
Load the chunk.
Definition order_sl.cpp:338
void FixPointers() const override
Fix the pointers.
Definition order_sl.cpp:351
Handlers and description of chunk.
Definition saveload.h:468
GUISettings gui
settings related to the GUI
bool new_nonstop
ttdpatch compatible nonstop handling
bool sg_new_nonstop
ttdpatch compatible nonstop handling read from pre v93 savegames
bool sg_full_load_any
new full load calculation, any cargo must be full read from pre v93 savegames
void FixPointers() const override
Fix the pointers.
Definition order_sl.cpp:275
void Save() const override
Save the chunk.
Definition order_sl.cpp:251
void Load() const override
Load the chunk.
Definition order_sl.cpp:262
void Load() const override
Load the chunk.
Definition order_sl.cpp:161
Compatibility struct to allow saveload of pool-based orders.
Definition order_base.h:248
uint32_t next
The next order index (1-based).
Definition order_base.h:250
Data for backing up an order of a vehicle so it can be restored after a vehicle is rebuilt in the sam...
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:264
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition order_base.h:155
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition order_base.h:153
void ConvertFromOldSavegame()
Converts this order from an old savegame's version; it moves all bits to the new location.
Definition order_sl.cpp:26
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition order_base.h:157
OrderType GetType() const
Get the type of order of this order.
Definition order_base.h:72
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition order_base.h:161
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition order_base.h:159
void MakeDummy()
Makes this order a Dummy order.
uint8_t flags
Load/unload types, depot order/action types.
Definition order_base.h:48
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition order_base.h:163
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
SaveLoad type struct.
Definition saveload.h:722