OpenTTD Source 20261129-master-g72a8bdc861
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "saveload_internal.h"
15
16#include "../order_backup.h"
17#include "../settings_type.h"
18#include "../vehicle_base.h"
19#include "../network/network.h"
20
21#include "../safeguards.h"
22
28{
29 uint8_t old_flags = this->flags;
30 this->flags = 0;
31
32 /* First handle non-stop - use value from savegame if possible, else use value from config file */
34 /* OFB_NON_STOP */
36 } else {
38 }
39
40 switch (this->GetType()) {
41 /* Only a few types need the other savegame conversions. */
42 case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
43 default: return;
44 }
45
46 if (this->GetType() != OT_GOTO_DEPOT) {
47 /* Then the load flags */
48 if ((old_flags & 2) != 0) { // OFB_UNLOAD
50 } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
52 } else {
53 /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
55 }
56
57 if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OrderStopLocation::FarEnd);
58
59 /* Finally fix the unload flags */
60 if ((old_flags & 1) != 0) { // OFB_TRANSFER
62 } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
64 } else {
66 }
67 } else {
68 /* Then the depot action flags */
69 OrderDepotActionFlags action_flags{};
70 if ((old_flags & 6) == 4) action_flags.Set(OrderDepotActionFlag::Halt);
71 this->SetDepotActionType(action_flags);
72
73 /* Finally fix the depot type flags */
74 OrderDepotTypeFlags type_flags{};
75 if ((old_flags & 6) == 6) type_flags.Set(OrderDepotTypeFlag::Service);
76 if ((old_flags & 2) != 0) type_flags.Set(OrderDepotTypeFlag::PartOfOrders);
77 this->SetDepotOrderType(type_flags);
78 }
79}
80
86static Order UnpackVersion4Order(uint16_t packed)
87{
88 return Order(GB(packed, 0, 4), GB(packed, 4, 4), GB(packed, 8, 8));
89}
90
96Order UnpackOldOrder(uint16_t packed)
97{
98 Order order = UnpackVersion4Order(packed);
99
100 /*
101 * Sanity check
102 * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
103 */
104 if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
105
106 return order;
107}
108
111
113{
114 static const SaveLoad _order_desc[] = {
115 SaveLoad::Variable<VarFileType::U8>("type", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.type)),
116 SaveLoad::Variable<VarFileType::U8>("flags", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.flags)),
117 SaveLoad::Variable<VarFileType::U16>("dest", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.dest)),
120 SaveLoad::Variable<VarFileType::U8>("refit_cargo", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.refit_cargo), SaveLoadVersion::RefitOrders),
121 SaveLoad::Variable<VarFileType::U16>("wait_time", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.wait_time), SaveLoadVersion::Timetables),
122 SaveLoad::Variable<VarFileType::U16>("travel_time", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.travel_time), SaveLoadVersion::Timetables),
123 SaveLoad::Variable<VarFileType::U16>("max_speed", SLE_OBJECT_ADDRESS(OldOrderPool::Item, value.max_speed), SaveLoadVersion::OrderMaxSpeed),
124 };
125
126 return _order_desc;
127}
128
129struct ORDRChunkHandler : ChunkHandler {
130 ORDRChunkHandler() : ChunkHandler("ORDR", ChunkType::ReadOnly) {}
131
132 void Load() const override
133 {
135 /* Version older than 5.2 did not have a ->next pointer. Convert them
136 * (in the old days, the orderlist was 5000 items big) */
137 size_t len = SlGetFieldLength();
138
140 /* Pre-version 5 had another layout for orders
141 * (uint16_t instead of uint32_t) */
142 len /= sizeof(uint16_t);
143 std::vector<uint16_t> orders(len);
144
146
147 for (size_t i = 0; i < len; ++i) {
148 auto &item = _old_order_saveload_pool.Allocate(i);
149 item.value.AssignOrder(UnpackVersion4Order(orders[i]));
150 }
152 len /= sizeof(uint32_t);
153 std::vector<uint32_t> orders(len);
154
156
157 for (size_t i = 0; i < len; ++i) {
158 auto &item = _old_order_saveload_pool.Allocate(i);
159 item.value = Order(GB(orders[i], 0, 8), GB(orders[i], 8, 8), GB(orders[i], 16, 16));
160 }
161 }
162
163 /* Update all the next pointer. The orders were built like this:
164 * While the order is valid, the previous order will get its next pointer set */
165 for (uint32_t num = 1; const auto &item : _old_order_saveload_pool.items) {
166 if (!item.value.IsType(OT_NOTHING) && num > 1) {
167 auto *prev = _old_order_saveload_pool.Get(num - 1);
168 if (prev != nullptr) prev->next = num;
169 }
170 ++num;
171 }
172 } else {
173 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat);
174
175 int index;
176
177 while ((index = SlIterateArray()) != -1) {
178 auto &item = _old_order_saveload_pool.Allocate(index);
179 SlObject(&item, slt);
180 }
181 }
182 }
183};
184
185template <typename T>
186class SlOrders : public VectorSaveLoadHandler<SlOrders<T>, T, Order> {
187public:
188 static inline const SaveLoad description[] = {
196 };
197 static inline const SaveLoadCompatTable compat_description = {};
198
199 std::vector<Order> &GetVector(T *container) const override { return container->orders; }
200
201 void LoadCheck(T *container) const override { this->Load(container); }
202};
203
204/* Instantiate SlOrders classes. */
205template class SlOrders<OrderList>;
206template class SlOrders<OrderBackup>;
207
218
219struct ORDLChunkHandler : ChunkHandler {
220 ORDLChunkHandler() : ChunkHandler("ORDL", ChunkType::Table) {}
221
222 void Save() const override
223 {
224 const SaveLoadTable slt = GetOrderListDescription();
225 SlTableHeader(slt);
226
227 for (OrderList *list : OrderList::Iterate()) {
228 SlSetArrayIndex(list->index);
229 SlObject(list, slt);
230 }
231 }
232
233 void Load() const override
234 {
235 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat);
236
237 int index;
238
239 while ((index = SlIterateArray()) != -1) {
240 OrderList *list = OrderList::CreateAtIndex(OrderListID(index));
241 SlObject(list, slt);
242 }
243
244 }
245
246 void FixPointers() const override
247 {
249
250 for (OrderList *list : OrderList::Iterate()) {
251 SlObject(list, GetOrderListDescription());
252
253 if (migrate_orders) {
254 list->orders = _old_order_saveload_pool.TransformToVector(list->old_order_index);
255 }
256 }
257 }
258};
259
261{
262 static const SaveLoad _order_backup_desc[] = {
281 };
282
283 return _order_backup_desc;
284}
285
286struct BKORChunkHandler : ChunkHandler {
287 BKORChunkHandler() : ChunkHandler("BKOR", ChunkType::Table) {}
288
289 void Save() const override
290 {
291 const SaveLoadTable slt = GetOrderBackupDescription();
292 SlTableHeader(slt);
293
294 /* We only save this when we're a network server
295 * as we want this information on our clients. For
296 * normal games this information isn't needed. */
297 if (!_networking || !_network_server) return;
298
299 for (OrderBackup *ob : OrderBackup::Iterate()) {
300 SlSetArrayIndex(ob->index);
301 SlObject(ob, slt);
302 }
303 }
304
305 void Load() const override
306 {
307 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat);
308
309 int index;
310
311 while ((index = SlIterateArray()) != -1) {
312 /* set num_orders to 0 so it's a valid OrderList */
314 SlObject(ob, slt);
315 }
316 }
317
318 void FixPointers() const override
319 {
321
322 for (OrderBackup *ob : OrderBackup::Iterate()) {
323 SlObject(ob, GetOrderBackupDescription());
324
325 if (migrate_orders) {
326 ob->orders = _old_order_saveload_pool.TransformToVector(ob->old_order_index);
327 }
328 }
329 }
330};
331
332static const BKORChunkHandler BKOR;
333static const ORDRChunkHandler ORDR;
334static const ORDLChunkHandler ORDL;
335static const ChunkHandlerRef order_chunk_handlers[] = {
336 BKOR,
337 ORDR,
338 ORDL,
339};
340
341extern const ChunkHandlerTable _order_chunk_handlers(order_chunk_handlers);
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr Timpl & Set()
Set all bits.
std::vector< Order > & GetVector(T *container) const override
Get instance of vector to load/save.
Definition order_sl.cpp:199
void LoadCheck(T *container) const override
Similar to load, but used only to validate savegames.
Definition order_sl.cpp:201
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1338
void Load(T *object) const override
Definition saveload.h:1364
#define T
Climate temperate.
Definition engines.h:91
SavegameType _savegame_type
type of savegame we are loading
Definition saveload.cpp:79
bool _networking
are we in networking mode?
Definition network.cpp:63
bool _network_server
network-server is active
Definition network.cpp:64
Basic functions/variables used all over the place.
@ Order
Something wrong with the order, e.g. invalid or duplicate entries, too few entries.
Definition news_type.h:57
Functions related to order backups.
PoolID< uint8_t, struct OrderBackupIDTag, 255, 0xFF > OrderBackupID
Unique identifier for an order backup.
Order UnpackOldOrder(uint16_t packed)
Unpacks a order from savegames made with TTD(Patch).
Definition order_sl.cpp:96
static Order UnpackVersion4Order(uint16_t packed)
Unpacks a order from savegames with version 4 and lower.
Definition order_sl.cpp:86
OldOrderPool _old_order_saveload_pool
Temporary storage for conversion from old order pool.
Definition order_sl.cpp:110
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.
@ Transfer
Transfer all cargo onto the platform.
Definition order_type.h:70
@ UnloadIfPossible
Unload all cargo that the station accepts.
Definition order_type.h:68
@ Unload
Force unloading all cargo onto the platform, possibly not getting paid.
Definition order_type.h:69
@ FarEnd
Stop at the far end of the platform.
Definition order_type.h:101
EnumBitSet< OrderNonStopFlag, uint8_t > OrderNonStopFlags
Bitset of OrderNonStopFlag elements.
Definition order_type.h:93
@ NonStop
The vehicle will not stop at any stations it passes except the destination, aka non-stop.
Definition order_type.h:88
@ GoVia
The vehicle will stop at any station it passes except the destination, aka via.
Definition order_type.h:89
EnumBitSet< OrderDepotActionFlag, uint8_t > OrderDepotActionFlags
Bitset of OrderDepotActionFlag elements.
Definition order_type.h:126
EnumBitSet< OrderDepotTypeFlag, uint8_t > OrderDepotTypeFlags
Bitset of OrderDepotTypeFlag elements.
Definition order_type.h:114
@ FullLoad
Full load all cargoes of the consist.
Definition order_type.h:79
@ NoLoad
Do not load anything.
Definition order_type.h:81
@ FullLoadAny
Full load a single cargo of the consist.
Definition order_type.h:80
@ LoadIfPossible
Load as long as there is cargo that fits in the train.
Definition order_type.h:78
@ Halt
Service the vehicle and then halt it.
Definition order_type.h:120
@ PartOfOrders
This depot order is because of a regular order.
Definition order_type.h:110
@ Service
This depot order is because of the servicing limit.
Definition order_type.h:109
A number of safeguards to prevent using unsafe methods.
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:747
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SaveLoadType::Variables to/from a savegame.
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:873
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.
#define SLE_OBJECT_ADDRESS(base, variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a base c...
Definition saveload.h:752
@ TTO
TTO savegame.
Definition saveload.h:437
@ TTD
TTD savegame (can be detected incorrectly).
Definition saveload.h:433
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:518
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1223
@ BackupOrderState
Saveload version: 176, SVN revision: 24446 Put more of the state of a vehicle's orders (like latenes...
Definition saveload.h:259
@ OrdersOwnedByOrderlist
Saveload version: 354, GitHub pull request: 13948 Orders stored in OrderList, pool removed.
Definition saveload.h:408
@ TimetableStartTicksFix
Saveload version: 322, GitHub pull request: 11557 Fix for missing convert timetable start from a dat...
Definition saveload.h:370
@ SavePatches
Saveload version: 22, SVN revision: 3726 Save state of patches (precursor of settings) in the savega...
Definition saveload.h:74
@ BigMap
Saveload version: 5.0, SVN revision: 1429 Making maps a different size than 256x256.
Definition saveload.h:48
@ MoreCargoPackets
Saveload version: 69, SVN revision: 10319 Allow more than ~65k cargo packets.
Definition saveload.h:130
@ RefitOrders
Saveload version: 36, SVN revision: 6624 Vehicles can be refitted as part of an order.
Definition saveload.h:91
@ ServiceIntervalPercent
Saveload version: 180, SVN revision: 24998, release: 1.3.x Service interval in percent or days store...
Definition saveload.h:264
@ MinVersion
First savegame version.
Definition saveload.h:35
@ OrderMaxSpeed
Saveload version: 172, SVN revision: 23947 Set maximum speed for orders.
Definition saveload.h:254
@ Timetables
Saveload version: 67, SVN revision: 10236 Introduce timetables for vehicles.
Definition saveload.h:128
@ FixOrderBackup
Saveload version: 192, SVN revision: 26700 Fix saving of order backups.
Definition saveload.h:278
@ ReadOnly
Chunk is never saved.
Definition saveload.h:453
@ Table
An Array with a header describing the elements.
Definition saveload.h:449
Declaration of functions used in more save/load files.
OldSaveLoadPool< Order > OldOrderPool
Type for conversion of old order pool.
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
void Save() const override
Save the chunk.
Definition order_sl.cpp:289
void Load() const override
Load the chunk.
Definition order_sl.cpp:305
void FixPointers() const override
Fix the pointers.
Definition order_sl.cpp:318
std::string name
Name of vehicle.
TimerGameTick::Ticks current_order_time
How many ticks have passed since this order started.
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
VehicleFlags vehicle_flags
Used for gradual loading and other miscellaneous things (.
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
TimerGameTick::TickCounter timetable_start
At what tick of TimerGameTick::counter the vehicle should start its timetable.
TimerGameTick::Ticks lateness_counter
How many ticks late (or early if negative) this vehicle is.
uint16_t service_interval
The interval for (automatic) servicing; either in days or %.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
void FixPointers() const override
Fix the pointers.
Definition order_sl.cpp:246
void Save() const override
Save the chunk.
Definition order_sl.cpp:222
void Load() const override
Load the chunk.
Definition order_sl.cpp:233
void Load() const override
Load the chunk.
Definition order_sl.cpp:132
Data for backing up an order of a vehicle so it can be restored after a vehicle is rebuilt in the sam...
OrderBackup(OrderBackupID index)
Create an order backup for savegame loading.
const Vehicle * clone
Vehicle this vehicle was a clone of.
friend SaveLoadTable GetOrderBackupDescription()
Saving and loading of order backups.
Definition order_sl.cpp:260
TileIndex tile
Tile of the depot where the order was changed.
GroupID group
The group the vehicle was part of.
ClientID user
The user that requested the backup.
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:373
friend SaveLoadTable GetOrderListDescription()
Saving and loading of order lists.
Definition order_sl.cpp:208
OrderList(OrderListID index)
Default constructor producing an invalid order list.
Definition order_base.h:395
If you change this, keep in mind that it is also saved in 2 other places:
Definition order_base.h:33
void ConvertFromOldSavegame()
Converts this order from an old savegame's version; it moves all bits to the new location.
Definition order_sl.cpp:27
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:217
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:229
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition order_base.h:223
void MakeDummy()
Makes this order a Dummy order.
uint8_t flags
Load/unload types, depot order/action types.
Definition order_base.h:48
friend SaveLoadTable GetOrderDescription()
Saving and loading of orders.
Definition order_sl.cpp:112
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition order_base.h:235
void SetUnloadType(OrderUnloadType unload_type)
Set how the consist must be unloaded.
Definition order_base.h:211
void SetLoadType(OrderLoadType load_type)
Set how the consist must be loaded.
Definition order_base.h:205
static Pool::IterateWrapper< OrderList > Iterate(size_t from=0)
static T * CreateAtIndex(OrderListID index, Targs &&... args)
SaveLoad type struct.
Definition saveload.h:762
static SaveLoad StructList(std::string name, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
Storage of a list of structs, optionally in some savegame versions.
Definition saveload.h:1100
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:923
static SaveLoad Reference(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
Storage of a reference to a pool element in some savegame versions.
Definition saveload.h:948
static SaveLoad String(std::string name, AddressFunction< T > address_func, StringValidationSettings string_validation_settings={}, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a string in some savegame versions.
Definition saveload.h:974
Base class for all vehicles.