OpenTTD Source  20241108-master-g80f628063a
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"
13 #include "compat/order_sl_compat.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 
82 static Order UnpackVersion4Order(uint16_t packed)
83 {
84  return Order(GB(packed, 0, 4), GB(packed, 4, 4), GB(packed, 8, 8));
85 }
86 
92 Order 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 
105 SaveLoadTable GetOrderDescription()
106 {
107  static const SaveLoad _order_desc[] = {
108  SLE_VAR(Order, type, SLE_UINT8),
109  SLE_VAR(Order, flags, SLE_UINT8),
110  SLE_VAR(Order, dest, SLE_UINT16),
111  SLE_REF(Order, next, REF_ORDER),
112  SLE_CONDVAR(Order, refit_cargo, SLE_UINT8, SLV_36, SL_MAX_VERSION),
113  SLE_CONDVAR(Order, wait_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
114  SLE_CONDVAR(Order, travel_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
115  SLE_CONDVAR(Order, max_speed, SLE_UINT16, SLV_172, SL_MAX_VERSION),
116  };
117 
118  return _order_desc;
119 }
120 
122  ORDRChunkHandler() : ChunkHandler('ORDR', CH_TABLE) {}
123 
124  void Save() const override
125  {
126  const SaveLoadTable slt = GetOrderDescription();
127  SlTableHeader(slt);
128 
129  for (Order *order : Order::Iterate()) {
130  SlSetArrayIndex(order->index);
131  SlObject(order, slt);
132  }
133  }
134 
135  void Load() const override
136  {
137  if (IsSavegameVersionBefore(SLV_5, 2)) {
138  /* Version older than 5.2 did not have a ->next pointer. Convert them
139  * (in the old days, the orderlist was 5000 items big) */
140  size_t len = SlGetFieldLength();
141 
143  /* Pre-version 5 had another layout for orders
144  * (uint16_t instead of uint32_t) */
145  len /= sizeof(uint16_t);
146  std::vector<uint16_t> orders(len);
147 
148  SlCopy(&orders[0], len, SLE_UINT16);
149 
150  for (size_t i = 0; i < len; ++i) {
151  Order *o = new (i) Order();
152  o->AssignOrder(UnpackVersion4Order(orders[i]));
153  }
154  } else if (IsSavegameVersionBefore(SLV_5, 2)) {
155  len /= sizeof(uint32_t);
156  std::vector<uint32_t> orders(len);
157 
158  SlCopy(&orders[0], len, SLE_UINT32);
159 
160  for (size_t i = 0; i < len; ++i) {
161  new (i) Order(GB(orders[i], 0, 8), GB(orders[i], 8, 8), GB(orders[i], 16, 16));
162  }
163  }
164 
165  /* Update all the next pointer */
166  for (Order *o : Order::Iterate()) {
167  size_t order_index = o->index;
168  /* Delete invalid orders */
169  if (o->IsType(OT_NOTHING)) {
170  delete o;
171  continue;
172  }
173  /* The orders were built like this:
174  * While the order is valid, set the previous will get its next pointer set */
175  Order *prev = Order::GetIfValid(order_index - 1);
176  if (prev != nullptr) prev->next = o;
177  }
178  } else {
179  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat);
180 
181  int index;
182 
183  while ((index = SlIterateArray()) != -1) {
184  Order *order = new (index) Order();
185  SlObject(order, slt);
186  }
187  }
188  }
189 
190  void FixPointers() const override
191  {
192  /* Orders from old savegames have pointers corrected in Load_ORDR */
193  if (IsSavegameVersionBefore(SLV_5, 2)) return;
194 
195  for (Order *o : Order::Iterate()) {
196  SlObject(o, GetOrderDescription());
197  }
198  }
199 };
200 
201 SaveLoadTable GetOrderListDescription()
202 {
203  static const SaveLoad _orderlist_desc[] = {
204  SLE_REF(OrderList, first, REF_ORDER),
205  };
206 
207  return _orderlist_desc;
208 }
209 
211  ORDLChunkHandler() : ChunkHandler('ORDL', CH_TABLE) {}
212 
213  void Save() const override
214  {
215  const SaveLoadTable slt = GetOrderListDescription();
216  SlTableHeader(slt);
217 
218  for (OrderList *list : OrderList::Iterate()) {
219  SlSetArrayIndex(list->index);
220  SlObject(list, slt);
221  }
222  }
223 
224  void Load() const override
225  {
226  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat);
227 
228  int index;
229 
230  while ((index = SlIterateArray()) != -1) {
231  /* set num_orders to 0 so it's a valid OrderList */
232  OrderList *list = new (index) OrderList(0);
233  SlObject(list, slt);
234  }
235 
236  }
237 
238  void FixPointers() const override
239  {
240  for (OrderList *list : OrderList::Iterate()) {
241  SlObject(list, GetOrderListDescription());
242  }
243  }
244 };
245 
246 SaveLoadTable GetOrderBackupDescription()
247 {
248  static const SaveLoad _order_backup_desc[] = {
249  SLE_VAR(OrderBackup, user, SLE_UINT32),
250  SLE_VAR(OrderBackup, tile, SLE_UINT32),
251  SLE_VAR(OrderBackup, group, SLE_UINT16),
252  SLE_CONDVAR(OrderBackup, service_interval, SLE_FILE_U32 | SLE_VAR_U16, SL_MIN_VERSION, SLV_192),
253  SLE_CONDVAR(OrderBackup, service_interval, SLE_UINT16, SLV_192, SL_MAX_VERSION),
254  SLE_SSTR(OrderBackup, name, SLE_STR),
256  SLE_VAR(OrderBackup, cur_real_order_index, SLE_UINT8),
257  SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8, SLV_176, SL_MAX_VERSION),
258  SLE_CONDVAR(OrderBackup, current_order_time, SLE_UINT32, SLV_176, SL_MAX_VERSION),
259  SLE_CONDVAR(OrderBackup, lateness_counter, SLE_INT32, SLV_176, SL_MAX_VERSION),
260  SLE_CONDVAR(OrderBackup, timetable_start, SLE_FILE_I32 | SLE_VAR_U64, SLV_176, SLV_TIMETABLE_START_TICKS_FIX),
262  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_176, SLV_180),
263  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
264  SLE_REF(OrderBackup, orders, REF_ORDER),
265  };
266 
267  return _order_backup_desc;
268 }
269 
271  BKORChunkHandler() : ChunkHandler('BKOR', CH_TABLE) {}
272 
273  void Save() const override
274  {
275  const SaveLoadTable slt = GetOrderBackupDescription();
276  SlTableHeader(slt);
277 
278  /* We only save this when we're a network server
279  * as we want this information on our clients. For
280  * normal games this information isn't needed. */
281  if (!_networking || !_network_server) return;
282 
283  for (OrderBackup *ob : OrderBackup::Iterate()) {
284  SlSetArrayIndex(ob->index);
285  SlObject(ob, slt);
286  }
287  }
288 
289  void Load() const override
290  {
291  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat);
292 
293  int index;
294 
295  while ((index = SlIterateArray()) != -1) {
296  /* set num_orders to 0 so it's a valid OrderList */
297  OrderBackup *ob = new (index) OrderBackup();
298  SlObject(ob, slt);
299  }
300  }
301 
302  void FixPointers() const override
303  {
304  for (OrderBackup *ob : OrderBackup::Iterate()) {
305  SlObject(ob, GetOrderBackupDescription());
306  }
307  }
308 };
309 
310 static const BKORChunkHandler BKOR;
311 static const ORDRChunkHandler ORDR;
312 static const ORDLChunkHandler ORDL;
313 static const ChunkHandlerRef order_chunk_handlers[] = {
314  BKOR,
315  ORDR,
316  ORDL,
317 };
318 
319 extern const ChunkHandlerTable _order_chunk_handlers(order_chunk_handlers);
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:59
bool _networking
are we in networking mode?
Definition: network.cpp:65
bool _network_server
network-server is active
Definition: network.cpp:66
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
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.
@ ODATFB_HALT
Service the vehicle and then halt it.
Definition: order_type.h:104
@ ODATF_SERVICE_ONLY
Only service the vehicle.
Definition: order_type.h:103
@ OLFB_FULL_LOAD
Full load all cargoes of the consist.
Definition: order_type.h:64
@ OLFB_NO_LOAD
Do not load anything.
Definition: order_type.h:66
@ OLF_LOAD_IF_POSSIBLE
Load as long as there is cargo that fits in the train.
Definition: order_type.h:63
@ OLF_FULL_LOAD_ANY
Full load a single cargo of the consist.
Definition: order_type.h:65
@ OSL_PLATFORM_FAR_END
Stop at the far end of the platform.
Definition: order_type.h:86
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition: order_type.h:55
@ OUF_UNLOAD_IF_POSSIBLE
Unload all cargo that the station accepts.
Definition: order_type.h:53
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:54
@ ONSF_NO_STOP_AT_ANY_STATION
The vehicle will not stop at any stations it passes including the destination.
Definition: order_type.h:76
@ ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition: order_type.h:73
@ ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
The vehicle will not stop at any stations it passes except the destination.
Definition: order_type.h:74
OrderDepotTypeFlags
Reasons that could cause us to go to the depot.
Definition: order_type.h:93
@ ODTFB_PART_OF_ORDERS
This depot order is because of a regular order.
Definition: order_type.h:96
@ ODTFB_SERVICE
This depot order is because of the servicing limit.
Definition: order_type.h:95
@ ODTF_MANUAL
Manually initiated order.
Definition: order_type.h:94
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1029
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:781
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
Functions/types related to saving and loading games.
@ SGT_TTD
TTD savegame (can be detected incorrectly)
Definition: saveload.h:420
@ SGT_TTO
TTO savegame.
Definition: saveload.h:424
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:505
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:508
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:992
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:861
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1227
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:1019
@ 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:395
@ 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_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:882
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:597
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:598
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition: saveload.h:511
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:975
Declaration of functions used in more save/load files.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
void Save() const override
Save the chunk.
Definition: order_sl.cpp:273
void Load() const override
Load the chunk.
Definition: order_sl.cpp:289
void FixPointers() const override
Fix the pointers.
Definition: order_sl.cpp:302
Handlers and description of chunk.
Definition: saveload.h:459
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:238
void Save() const override
Save the chunk.
Definition: order_sl.cpp:213
void Load() const override
Load the chunk.
Definition: order_sl.cpp:224
void Save() const override
Save the chunk.
Definition: order_sl.cpp:124
void Load() const override
Load the chunk.
Definition: order_sl.cpp:135
void FixPointers() const override
Fix the pointers.
Definition: order_sl.cpp:190
Data for backing up an order of a vehicle so it can be restored after a vehicle is rebuilt in the sam...
Definition: order_backup.h:33
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:259
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition: order_base.h:159
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition: order_base.h:157
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:70
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition: order_base.h:161
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:76
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition: order_base.h:165
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition: order_base.h:163
void MakeDummy()
Makes this order a Dummy order.
Definition: order_cmd.cpp:133
uint8_t flags
Load/unload types, depot order/action types.
Definition: order_base.h:49
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:59
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition: order_base.h:167
void AssignOrder(const Order &other)
Assign data to an order (from another order) This function makes sure that the index is maintained co...
Definition: order_cmd.cpp:257
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
SaveLoad type struct.
Definition: saveload.h:711