OpenTTD
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 #include "../order_backup.h"
12 #include "../settings_type.h"
13 #include "../network/network.h"
14 
15 #include "saveload_internal.h"
16 
17 #include "../safeguards.h"
18 
24 {
25  uint8 old_flags = this->flags;
26  this->flags = 0;
27 
28  /* First handle non-stop - use value from savegame if possible, else use value from config file */
30  /* OFB_NON_STOP */
32  } else {
34  }
35 
36  switch (this->GetType()) {
37  /* Only a few types need the other savegame conversions. */
38  case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
39  default: return;
40  }
41 
42  if (this->GetType() != OT_GOTO_DEPOT) {
43  /* Then the load flags */
44  if ((old_flags & 2) != 0) { // OFB_UNLOAD
46  } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
48  } else {
49  /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
51  }
52 
53  if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OSL_PLATFORM_FAR_END);
54 
55  /* Finally fix the unload flags */
56  if ((old_flags & 1) != 0) { // OFB_TRANSFER
58  } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
60  } else {
62  }
63  } else {
64  /* Then the depot action flags */
65  this->SetDepotActionType(((old_flags & 6) == 4) ? ODATFB_HALT : ODATF_SERVICE_ONLY);
66 
67  /* Finally fix the depot type flags */
68  uint t = ((old_flags & 6) == 6) ? ODTFB_SERVICE : ODTF_MANUAL;
69  if ((old_flags & 2) != 0) t |= ODTFB_PART_OF_ORDERS;
71  }
72 }
73 
79 static Order UnpackVersion4Order(uint16 packed)
80 {
81  return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
82 }
83 
89 Order UnpackOldOrder(uint16 packed)
90 {
91  Order order = UnpackVersion4Order(packed);
92 
93  /*
94  * Sanity check
95  * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
96  */
97  if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
98 
99  return order;
100 }
101 
103 {
104  static const SaveLoad _order_desc[] = {
105  SLE_VAR(Order, type, SLE_UINT8),
106  SLE_VAR(Order, flags, SLE_UINT8),
107  SLE_VAR(Order, dest, SLE_UINT16),
110  SLE_CONDNULL(1, SLV_36, SLV_182), // refit_subtype
114 
115  /* Leftover from the minor savegame version stuff
116  * We will never use those free bytes, but we have to keep this line to allow loading of old savegames */
117  SLE_CONDNULL(10, SLV_5, SLV_36),
118  SLE_END()
119  };
120 
121  return _order_desc;
122 }
123 
124 static void Save_ORDR()
125 {
126  for (Order *order : Order::Iterate()) {
127  SlSetArrayIndex(order->index);
128  SlObject(order, GetOrderDescription());
129  }
130 }
131 
132 static void Load_ORDR()
133 {
134  if (IsSavegameVersionBefore(SLV_5, 2)) {
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 instead of uint32) */
142  len /= sizeof(uint16);
143  uint16 *orders = MallocT<uint16>(len + 1);
144 
145  SlArray(orders, len, SLE_UINT16);
146 
147  for (size_t i = 0; i < len; ++i) {
148  Order *o = new (i) Order();
149  o->AssignOrder(UnpackVersion4Order(orders[i]));
150  }
151 
152  free(orders);
153  } else if (IsSavegameVersionBefore(SLV_5, 2)) {
154  len /= sizeof(uint32);
155  uint32 *orders = MallocT<uint32>(len + 1);
156 
157  SlArray(orders, len, SLE_UINT32);
158 
159  for (size_t i = 0; i < len; ++i) {
160  new (i) Order(orders[i]);
161  }
162 
163  free(orders);
164  }
165 
166  /* Update all the next pointer */
167  for (Order *o : Order::Iterate()) {
168  size_t order_index = o->index;
169  /* Delete invalid orders */
170  if (o->IsType(OT_NOTHING)) {
171  delete o;
172  continue;
173  }
174  /* The orders were built like this:
175  * While the order is valid, set the previous will get its next pointer set */
176  Order *prev = Order::GetIfValid(order_index - 1);
177  if (prev != nullptr) prev->next = o;
178  }
179  } else {
180  int index;
181 
182  while ((index = SlIterateArray()) != -1) {
183  Order *order = new (index) Order();
184  SlObject(order, GetOrderDescription());
186  order->SetTravelTimetabled(order->GetTravelTime() > 0);
187  order->SetWaitTimetabled(order->GetWaitTime() > 0);
188  }
189  }
190  }
191 }
192 
193 static void Ptrs_ORDR()
194 {
195  /* Orders from old savegames have pointers corrected in Load_ORDR */
196  if (IsSavegameVersionBefore(SLV_5, 2)) return;
197 
198  for (Order *o : Order::Iterate()) {
200  }
201 }
202 
203 const SaveLoad *GetOrderListDescription()
204 {
205  static const SaveLoad _orderlist_desc[] = {
206  SLE_REF(OrderList, first, REF_ORDER),
207  SLE_END()
208  };
209 
210  return _orderlist_desc;
211 }
212 
213 static void Save_ORDL()
214 {
215  for (OrderList *list : OrderList::Iterate()) {
216  SlSetArrayIndex(list->index);
217  SlObject(list, GetOrderListDescription());
218  }
219 }
220 
221 static void Load_ORDL()
222 {
223  int index;
224 
225  while ((index = SlIterateArray()) != -1) {
226  /* set num_orders to 0 so it's a valid OrderList */
227  OrderList *list = new (index) OrderList(0);
228  SlObject(list, GetOrderListDescription());
229  }
230 
231 }
232 
233 static void Ptrs_ORDL()
234 {
235  for (OrderList *list : OrderList::Iterate()) {
236  SlObject(list, GetOrderListDescription());
237  }
238 }
239 
240 const SaveLoad *GetOrderBackupDescription()
241 {
242  static const SaveLoad _order_backup_desc[] = {
243  SLE_VAR(OrderBackup, user, SLE_UINT32),
244  SLE_VAR(OrderBackup, tile, SLE_UINT32),
245  SLE_VAR(OrderBackup, group, SLE_UINT16),
246  SLE_CONDVAR(OrderBackup, service_interval, SLE_FILE_U32 | SLE_VAR_U16, SL_MIN_VERSION, SLV_192),
247  SLE_CONDVAR(OrderBackup, service_interval, SLE_UINT16, SLV_192, SL_MAX_VERSION),
248  SLE_STR(OrderBackup, name, SLE_STR, 0),
249  SLE_CONDNULL(2, SL_MIN_VERSION, SLV_192), // clone (2 bytes of pointer, i.e. garbage)
251  SLE_VAR(OrderBackup, cur_real_order_index, SLE_UINT8),
252  SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8, SLV_176, SL_MAX_VERSION),
253  SLE_CONDVAR(OrderBackup, current_order_time, SLE_UINT32, SLV_176, SL_MAX_VERSION),
254  SLE_CONDVAR(OrderBackup, lateness_counter, SLE_INT32, SLV_176, SL_MAX_VERSION),
255  SLE_CONDVAR(OrderBackup, timetable_start, SLE_INT32, SLV_176, SL_MAX_VERSION),
256  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_176, SLV_180),
257  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
258  SLE_REF(OrderBackup, orders, REF_ORDER),
259  SLE_END()
260  };
261 
262  return _order_backup_desc;
263 }
264 
265 static void Save_BKOR()
266 {
267  /* We only save this when we're a network server
268  * as we want this information on our clients. For
269  * normal games this information isn't needed. */
270  if (!_networking || !_network_server) return;
271 
272  for (OrderBackup *ob : OrderBackup::Iterate()) {
273  SlSetArrayIndex(ob->index);
274  SlObject(ob, GetOrderBackupDescription());
275  }
276 }
277 
278 void Load_BKOR()
279 {
280  int index;
281 
282  while ((index = SlIterateArray()) != -1) {
283  /* set num_orders to 0 so it's a valid OrderList */
284  OrderBackup *ob = new (index) OrderBackup();
285  SlObject(ob, GetOrderBackupDescription());
286  }
287 }
288 
289 static void Ptrs_BKOR()
290 {
291  for (OrderBackup *ob : OrderBackup::Iterate()) {
292  SlObject(ob, GetOrderBackupDescription());
293  }
294 }
295 
296 extern const ChunkHandler _order_chunk_handlers[] = {
297  { 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, nullptr, CH_ARRAY},
298  { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, nullptr, CH_ARRAY},
299  { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, nullptr, CH_ARRAY | CH_LAST},
300 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:642
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:302
bool _networking
are we in networking mode?
Definition: network.cpp:52
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
uint16 GetTravelTime() const
Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not)...
Definition: order_base.h:185
Load as long as there is cargo that fits in the train.
Definition: order_type.h:63
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:544
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition: order_base.h:156
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:602
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition: order_base.h:158
Unload all cargo that the station accepts.
Definition: order_type.h:53
Only service the vehicle.
Definition: order_type.h:103
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
TTD savegame (can be detected incorrectly)
Definition: saveload.h:331
CargoID refit_cargo
Refit CargoID.
Definition: order_base.h:42
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition: order_base.h:150
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:743
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:49
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:995
Stop at the far end of the platform.
Definition: order_type.h:86
DestinationID dest
The destination of the order.
Definition: order_base.h:40
TTO savegame.
Definition: saveload.h:335
uint16 travel_time
How long in ticks the journey to this destination should take.
Definition: order_base.h:45
The vehicle will stop at any station it passes and the destination.
Definition: order_type.h:73
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
180 24998 1.3.x
Definition: saveload.h:258
22 3726
Definition: saveload.h:68
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:534
uint8 type
The type of order + non-stop flags.
Definition: order_base.h:38
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition: order_base.h:152
Highest possible saveload version.
Definition: saveload.h:305
Order UnpackOldOrder(uint16 packed)
Unpacks a order from savegames made with TTD(Patch)
Definition: order_sl.cpp:89
The vehicle will not stop at any stations it passes including the destination.
Definition: order_type.h:76
void MakeDummy()
Makes this order a Dummy order.
Definition: order_cmd.cpp:132
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:42
First savegame version.
Definition: saveload.h:30
Do not load anything.
Definition: order_type.h:66
Manually initiated order.
Definition: order_type.h:94
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:61
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
friend const struct SaveLoad * GetOrderDescription()
Saving and loading of orders.
Definition: order_sl.cpp:102
uint8 flags
Load/unload types, depot order/action types.
Definition: order_base.h:39
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:57
Load/save a reference to an order.
Definition: saveload.h:371
Service the vehicle and then halt it.
Definition: order_type.h:104
void SetWaitTimetabled(bool timetabled)
Set if the wait time is explicitly timetabled (unless the order is conditional).
Definition: order_base.h:195
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Load/save a reference to a vehicle.
Definition: saveload.h:372
Handlers and description of chunk.
Definition: saveload.h:356
Transfer all cargo onto the platform.
Definition: order_type.h:55
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition: order_base.h:148
uint16 GetWaitTime() const
Get the time in ticks a vehicle will probably wait at the destination (timetabled or not)...
Definition: order_base.h:183
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:651
GUISettings gui
settings related to the GUI
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
bool sg_new_nonstop
ttdpatch compatible nonstop handling read from pre v93 savegames
Definition: settings_type.h:88
190 26547 Separate order travel and wait times
Definition: saveload.h:270
uint16 wait_time
How long in ticks to wait at the destination.
Definition: order_base.h:44
void SetTravelTimetabled(bool timetabled)
Set if the travel time is explicitly timetabled (unless the order is conditional).
Definition: order_base.h:197
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Full load all cargoes of the consist.
Definition: order_type.h:64
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
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:272
The vehicle will not stop at any stations it passes except the destination.
Definition: order_type.h:74
bool _network_server
network-server is active
Definition: network.cpp:53
bool sg_full_load_any
new full load calculation, any cargo must be full read from pre v93 savegames
Definition: settings_type.h:83
192 26700 FS#6066 Fix saving of order backups
Definition: saveload.h:273
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
SaveLoad type struct.
Definition: saveload.h:496
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:594
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:620
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:67
bool new_nonstop
ttdpatch compatible nonstop handling
Definition: settings_type.h:89
182 25115 FS#5492, r25259, r25296 Goal status
Definition: saveload.h:260
176 24446
Definition: saveload.h:253
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:54
void ConvertFromOldSavegame()
Converts this order from an old savegame&#39;s version; it moves all bits to the new location.
Definition: order_sl.cpp:23
36 6624
Definition: saveload.h:85
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:35
This depot order is because of a regular order.
Definition: order_type.h:96
172 23947
Definition: saveload.h:248
static Order UnpackVersion4Order(uint16 packed)
Unpacks a order from savegames with version 4 and lower.
Definition: order_sl.cpp:79
uint16 max_speed
How fast the vehicle may go on the way to the destination.
Definition: order_base.h:46
Declaration of functions used in more save/load files.
Full load a single cargo of the consist.
Definition: order_type.h:65
This depot order is because of the servicing limit.
Definition: order_type.h:95
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition: order_base.h:154
67 10236
Definition: saveload.h:122
OrderDepotTypeFlags
Reasons that could cause us to go to the depot.
Definition: order_type.h:93
Last chunk in this array.
Definition: saveload.h:391