OpenTTD Source  12.0-beta2
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 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 packed)
83 {
84  return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
85 }
86 
92 Order UnpackOldOrder(uint16 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 instead of uint32) */
145  len /= sizeof(uint16);
146  uint16 *orders = MallocT<uint16>(len + 1);
147 
148  SlCopy(orders, 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 
155  free(orders);
156  } else if (IsSavegameVersionBefore(SLV_5, 2)) {
157  len /= sizeof(uint32);
158  uint32 *orders = MallocT<uint32>(len + 1);
159 
160  SlCopy(orders, len, SLE_UINT32);
161 
162  for (size_t i = 0; i < len; ++i) {
163  new (i) Order(orders[i]);
164  }
165 
166  free(orders);
167  }
168 
169  /* Update all the next pointer */
170  for (Order *o : Order::Iterate()) {
171  size_t order_index = o->index;
172  /* Delete invalid orders */
173  if (o->IsType(OT_NOTHING)) {
174  delete o;
175  continue;
176  }
177  /* The orders were built like this:
178  * While the order is valid, set the previous will get its next pointer set */
179  Order *prev = Order::GetIfValid(order_index - 1);
180  if (prev != nullptr) prev->next = o;
181  }
182  } else {
183  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat);
184 
185  int index;
186 
187  while ((index = SlIterateArray()) != -1) {
188  Order *order = new (index) Order();
189  SlObject(order, slt);
190  }
191  }
192  }
193 
194  void FixPointers() const override
195  {
196  /* Orders from old savegames have pointers corrected in Load_ORDR */
197  if (IsSavegameVersionBefore(SLV_5, 2)) return;
198 
199  for (Order *o : Order::Iterate()) {
200  SlObject(o, GetOrderDescription());
201  }
202  }
203 };
204 
205 SaveLoadTable GetOrderListDescription()
206 {
207  static const SaveLoad _orderlist_desc[] = {
208  SLE_REF(OrderList, first, REF_ORDER),
209  };
210 
211  return _orderlist_desc;
212 }
213 
215  ORDLChunkHandler() : ChunkHandler('ORDL', CH_TABLE) {}
216 
217  void Save() const override
218  {
219  const SaveLoadTable slt = GetOrderListDescription();
220  SlTableHeader(slt);
221 
222  for (OrderList *list : OrderList::Iterate()) {
223  SlSetArrayIndex(list->index);
224  SlObject(list, slt);
225  }
226  }
227 
228  void Load() const override
229  {
230  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat);
231 
232  int index;
233 
234  while ((index = SlIterateArray()) != -1) {
235  /* set num_orders to 0 so it's a valid OrderList */
236  OrderList *list = new (index) OrderList(0);
237  SlObject(list, slt);
238  }
239 
240  }
241 
242  void FixPointers() const override
243  {
244  for (OrderList *list : OrderList::Iterate()) {
245  SlObject(list, GetOrderListDescription());
246  }
247  }
248 };
249 
250 SaveLoadTable 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, SL_MIN_VERSION, SLV_192),
257  SLE_CONDVAR(OrderBackup, service_interval, SLE_UINT16, SLV_192, SL_MAX_VERSION),
258  SLE_SSTR(OrderBackup, name, SLE_STR),
260  SLE_VAR(OrderBackup, cur_real_order_index, SLE_UINT8),
261  SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8, SLV_176, SL_MAX_VERSION),
262  SLE_CONDVAR(OrderBackup, current_order_time, SLE_UINT32, SLV_176, SL_MAX_VERSION),
263  SLE_CONDVAR(OrderBackup, lateness_counter, SLE_INT32, SLV_176, SL_MAX_VERSION),
264  SLE_CONDVAR(OrderBackup, timetable_start, SLE_INT32, SLV_176, SL_MAX_VERSION),
265  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_176, SLV_180),
266  SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
267  SLE_REF(OrderBackup, orders, REF_ORDER),
268  };
269 
270  return _order_backup_desc;
271 }
272 
274  BKORChunkHandler() : ChunkHandler('BKOR', CH_TABLE) {}
275 
276  void Save() const override
277  {
278  const SaveLoadTable slt = GetOrderBackupDescription();
279  SlTableHeader(slt);
280 
281  /* We only save this when we're a network server
282  * as we want this information on our clients. For
283  * normal games this information isn't needed. */
284  if (!_networking || !_network_server) return;
285 
286  for (OrderBackup *ob : OrderBackup::Iterate()) {
287  SlSetArrayIndex(ob->index);
288  SlObject(ob, slt);
289  }
290  }
291 
292  void Load() const override
293  {
294  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat);
295 
296  int index;
297 
298  while ((index = SlIterateArray()) != -1) {
299  /* set num_orders to 0 so it's a valid OrderList */
300  OrderBackup *ob = new (index) OrderBackup();
301  SlObject(ob, slt);
302  }
303  }
304 
305  void FixPointers() const override
306  {
307  for (OrderBackup *ob : OrderBackup::Iterate()) {
308  SlObject(ob, GetOrderBackupDescription());
309  }
310  }
311 };
312 
313 static const BKORChunkHandler BKOR;
314 static const ORDRChunkHandler ORDR;
315 static const ORDLChunkHandler ORDL;
316 static const ChunkHandlerRef order_chunk_handlers[] = {
317  BKOR,
318  ORDR,
319  ORDL,
320 };
321 
322 extern const ChunkHandlerTable _order_chunk_handlers(order_chunk_handlers);
Order::MakeDummy
void MakeDummy()
Makes this order a Dummy order.
Definition: order_cmd.cpp:132
REF_ORDER
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:535
UnpackOldOrder
Order UnpackOldOrder(uint16 packed)
Unpacks a order from savegames made with TTD(Patch)
Definition: order_sl.cpp:92
OUFB_UNLOAD
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:54
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:64
_order_sl_compat
const SaveLoadCompat _order_sl_compat[]
Original field order for _order_desc.
Definition: order_sl_compat.h:16
Order::SetLoadType
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition: order_base.h:151
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Pool::PoolItem<&_order_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
ODTFB_SERVICE
@ ODTFB_SERVICE
This depot order is because of the servicing limit.
Definition: order_type.h:95
OUFB_TRANSFER
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition: order_type.h:55
OLFB_FULL_LOAD
@ OLFB_FULL_LOAD
Full load all cargoes of the consist.
Definition: order_type.h:64
ORDRChunkHandler
Definition: order_sl.cpp:121
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
GUISettings::sg_new_nonstop
bool sg_new_nonstop
ttdpatch compatible nonstop handling read from pre v93 savegames
Definition: settings_type.h:109
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1151
GUISettings::new_nonstop
bool new_nonstop
ttdpatch compatible nonstop handling
Definition: settings_type.h:110
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:702
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:798
ODTF_MANUAL
@ ODTF_MANUAL
Manually initiated order.
Definition: order_type.h:94
saveload.h
OrderDepotTypeFlags
OrderDepotTypeFlags
Reasons that could cause us to go to the depot.
Definition: order_type.h:93
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
Order::AssignOrder
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
SLV_22
@ SLV_22
22 3726
Definition: saveload.h:73
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
_orderlist_sl_compat
const SaveLoadCompat _orderlist_sl_compat[]
Original field order for _orderlist_desc.
Definition: order_sl_compat.h:30
SLV_172
@ SLV_172
172 23947
Definition: saveload.h:253
ORDLChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: order_sl.cpp:242
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:62
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:780
Order::GetType
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:70
_order_backup_sl_compat
const SaveLoadCompat _order_backup_sl_compat[]
Original field order for _order_backup_desc.
Definition: order_sl_compat.h:35
ONSF_NO_STOP_AT_ANY_STATION
@ 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_NO_STOP_AT_INTERMEDIATE_STATIONS
@ ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
The vehicle will not stop at any stations it passes except the destination.
Definition: order_type.h:74
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:47
OLF_FULL_LOAD_ANY
@ OLF_FULL_LOAD_ANY
Full load a single cargo of the consist.
Definition: order_type.h:65
ONSF_STOP_EVERYWHERE
@ ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition: order_type.h:73
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
SGT_TTO
@ SGT_TTO
TTO savegame.
Definition: saveload.h:372
OUF_UNLOAD_IF_POSSIBLE
@ OUF_UNLOAD_IF_POSSIBLE
Unload all cargo that the station accepts.
Definition: order_type.h:53
SLV_67
@ SLV_67
67 10236
Definition: saveload.h:127
SLV_176
@ SLV_176
176 24446
Definition: saveload.h:258
ORDLChunkHandler
Definition: order_sl.cpp:214
BKORChunkHandler::Save
void Save() const override
Save the chunk.
Definition: order_sl.cpp:276
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:712
ODTFB_PART_OF_ORDERS
@ ODTFB_PART_OF_ORDERS
This depot order is because of a regular order.
Definition: order_type.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
Order::ConvertFromOldSavegame
void ConvertFromOldSavegame()
Converts this order from an old savegame's version; it moves all bits to the new location.
Definition: order_sl.cpp:26
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:536
ORDRChunkHandler::Save
void Save() const override
Save the chunk.
Definition: order_sl.cpp:124
ORDLChunkHandler::Save
void Save() const override
Save the chunk.
Definition: order_sl.cpp:217
OLF_LOAD_IF_POSSIBLE
@ OLF_LOAD_IF_POSSIBLE
Load as long as there is cargo that fits in the train.
Definition: order_type.h:63
Order::SetStopLocation
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition: order_base.h:157
ODATFB_HALT
@ ODATFB_HALT
Service the vehicle and then halt it.
Definition: order_type.h:104
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:342
SGT_TTD
@ SGT_TTD
TTD savegame (can be detected incorrectly)
Definition: saveload.h:368
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:772
Pool::PoolItem<&_order_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
ORDRChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: order_sl.cpp:194
OSL_PLATFORM_FAR_END
@ OSL_PLATFORM_FAR_END
Stop at the far end of the platform.
Definition: order_type.h:86
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:792
SLV_36
@ SLV_36
36 6624
Definition: saveload.h:90
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:253
ORDLChunkHandler::Load
void Load() const override
Load the chunk.
Definition: order_sl.cpp:228
GUISettings::sg_full_load_any
bool sg_full_load_any
new full load calculation, any cargo must be full read from pre v93 savegames
Definition: settings_type.h:104
Order::SetDepotOrderType
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition: order_base.h:159
Order::SetDepotActionType
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition: order_base.h:161
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:806
BKORChunkHandler
Definition: order_sl.cpp:273
BKORChunkHandler::Load
void Load() const override
Load the chunk.
Definition: order_sl.cpp:292
saveload_internal.h
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SLV_180
@ SLV_180
180 24998 1.3.x
Definition: saveload.h:263
ODATF_SERVICE_ONLY
@ ODATF_SERVICE_ONLY
Only service the vehicle.
Definition: order_type.h:103
SLV_192
@ SLV_192
192 26700 FS#6066 Fix saving of order backups
Definition: saveload.h:278
Order::SetUnloadType
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition: order_base.h:153
OrderBackup
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:36
BKORChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: order_sl.cpp:305
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
UnpackVersion4Order
static Order UnpackVersion4Order(uint16 packed)
Unpacks a order from savegames with version 4 and lower.
Definition: order_sl.cpp:82
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
order_sl_compat.h
Order::next
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:52
Order::flags
uint8 flags
Load/unload types, depot order/action types.
Definition: order_base.h:42
Order
Definition: order_base.h:33
Order::SetNonStopType
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition: order_base.h:155
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
ORDRChunkHandler::Load
void Load() const override
Load the chunk.
Definition: order_sl.cpp:135
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
OLFB_NO_LOAD
@ OLFB_NO_LOAD
Do not load anything.
Definition: order_type.h:66