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