OpenTTD Source: src/saveload/order_sl.cpp Source File

OpenTTD Source 1.11.0-RC1

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
23 void Order::ConvertFromOldSavegame()
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 */
29 if (_settings_client.gui.sg_new_nonstop || (IsSavegameVersionBefore(SLV_22) && _savegame_type != SGT_TTO && _savegame_type != SGT_TTD && _settings_client.gui.new_nonstop)) {
30 /* OFB_NON_STOP */
31 this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
32 } else {
33 this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
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
45 this->SetLoadType(OLFB_NO_LOAD);
46 } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
47 this->SetLoadType(OLF_LOAD_IF_POSSIBLE);
48 } else {
49 /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
50 this->SetLoadType(_settings_client.gui.sg_full_load_any || IsSavegameVersionBefore(SLV_22) ? OLF_FULL_LOAD_ANY : OLFB_FULL_LOAD);
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
57 this->SetUnloadType(OUFB_TRANSFER);
58 } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
59 this->SetUnloadType(OUFB_UNLOAD);
60 } else {
61 this->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
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;
70 this->SetDepotOrderType((OrderDepotTypeFlags)t);
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
102 const SaveLoad *GetOrderDescription()
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),
108 SLE_REF(Order, next, REF_ORDER),
109 SLE_CONDVAR(Order, refit_cargo, SLE_UINT8, SLV_36, SL_MAX_VERSION),
110 SLE_CONDNULL(1, SLV_36, SLV_182), // refit_subtype
111 SLE_CONDVAR(Order, wait_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
112 SLE_CONDVAR(Order, travel_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
113 SLE_CONDVAR(Order, max_speed, SLE_UINT16, SLV_172, SL_MAX_VERSION),
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
139 if (IsSavegameVersionBefore(SLV_5)) {
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());
185 }
186 }
187 }
188
189 static void Ptrs_ORDR()
190 {
191 /* Orders from old savegames have pointers corrected in Load_ORDR */
192 if (IsSavegameVersionBefore(SLV_5, 2)) return;
193
194 for (Order *o : Order::Iterate()) {
195 SlObject(o, GetOrderDescription());
196 }
197 }
198
199 const SaveLoad *GetOrderListDescription()
200 {
201 static const SaveLoad _orderlist_desc[] = {
202 SLE_REF(OrderList, first, REF_ORDER),
203 SLE_END()
204 };
205
206 return _orderlist_desc;
207 }
208
209 static void Save_ORDL()
210 {
211 for (OrderList *list : OrderList::Iterate()) {
212 SlSetArrayIndex(list->index);
213 SlObject(list, GetOrderListDescription());
214 }
215 }
216
217 static void Load_ORDL()
218 {
219 int index;
220
221 while ((index = SlIterateArray()) != -1) {
222 /* set num_orders to 0 so it's a valid OrderList */
223 OrderList *list = new (index) OrderList(0);
224 SlObject(list, GetOrderListDescription());
225 }
226
227 }
228
229 static void Ptrs_ORDL()
230 {
231 for (OrderList *list : OrderList::Iterate()) {
232 SlObject(list, GetOrderListDescription());
233 }
234 }
235
236 const SaveLoad *GetOrderBackupDescription()
237 {
238 static const SaveLoad _order_backup_desc[] = {
239 SLE_VAR(OrderBackup, user, SLE_UINT32),
240 SLE_VAR(OrderBackup, tile, SLE_UINT32),
241 SLE_VAR(OrderBackup, group, SLE_UINT16),
242 SLE_CONDVAR(OrderBackup, service_interval, SLE_FILE_U32 | SLE_VAR_U16, SL_MIN_VERSION, SLV_192),
243 SLE_CONDVAR(OrderBackup, service_interval, SLE_UINT16, SLV_192, SL_MAX_VERSION),
244 SLE_SSTR(OrderBackup, name, SLE_STR),
245 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_192), // clone (2 bytes of pointer, i.e. garbage)
246 SLE_CONDREF(OrderBackup, clone, REF_VEHICLE, SLV_192, SL_MAX_VERSION),
247 SLE_VAR(OrderBackup, cur_real_order_index, SLE_UINT8),
248 SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8, SLV_176, SL_MAX_VERSION),
249 SLE_CONDVAR(OrderBackup, current_order_time, SLE_UINT32, SLV_176, SL_MAX_VERSION),
250 SLE_CONDVAR(OrderBackup, lateness_counter, SLE_INT32, SLV_176, SL_MAX_VERSION),
251 SLE_CONDVAR(OrderBackup, timetable_start, SLE_INT32, SLV_176, SL_MAX_VERSION),
252 SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_176, SLV_180),
253 SLE_CONDVAR(OrderBackup, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
254 SLE_REF(OrderBackup, orders, REF_ORDER),
255 SLE_END()
256 };
257
258 return _order_backup_desc;
259 }
260
261 static void Save_BKOR()
262 {
263 /* We only save this when we're a network server
264 * as we want this information on our clients. For
265 * normal games this information isn't needed. */
266 if (!_networking || !_network_server) return;
267
268 for (OrderBackup *ob : OrderBackup::Iterate()) {
269 SlSetArrayIndex(ob->index);
270 SlObject(ob, GetOrderBackupDescription());
271 }
272 }
273
274 void Load_BKOR()
275 {
276 int index;
277
278 while ((index = SlIterateArray()) != -1) {
279 /* set num_orders to 0 so it's a valid OrderList */
280 OrderBackup *ob = new (index) OrderBackup();
281 SlObject(ob, GetOrderBackupDescription());
282 }
283 }
284
285 static void Ptrs_BKOR()
286 {
287 for (OrderBackup *ob : OrderBackup::Iterate()) {
288 SlObject(ob, GetOrderBackupDescription());
289 }
290 }
291
292 extern const ChunkHandler _order_chunk_handlers[] = {
293 { 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, nullptr, CH_ARRAY},
294 { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, nullptr, CH_ARRAY},
295 { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, nullptr, CH_ARRAY | CH_LAST},
296 };
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:390
UnpackOldOrder
Order UnpackOldOrder(uint16 packed)
Unpacks a order from savegames made with TTD(Patch)
Definition: order_sl.cpp:89
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:61
Order::SetLoadType
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition: order_base.h:148
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:340
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
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
GUISettings::sg_new_nonstop
bool sg_new_nonstop
ttdpatch compatible nonstop handling read from pre v93 savegames
Definition: settings_type.h:98
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
GUISettings::new_nonstop
bool new_nonstop
ttdpatch compatible nonstop handling
Definition: settings_type.h:99
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
ODTF_MANUAL
@ ODTF_MANUAL
Manually initiated order.
Definition: order_type.h:94
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
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:79
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:69
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLV_172
@ SLV_172
172 23947
Definition: saveload.h:249
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:61
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:629
Order::GetType
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:67
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:43
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
SGT_TTO
@ SGT_TTO
TTO savegame.
Definition: saveload.h:358
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:123
SLV_176
@ SLV_176
176 24446
Definition: saveload.h:254
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:561
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:815
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
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:23
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:391
SLV_182
@ SLV_182
182 25115 FS#5492, r25259, r25296 Goal status
Definition: saveload.h:261
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:154
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:328
SGT_TTD
@ SGT_TTD
TTD savegame (can be detected incorrectly)
Definition: saveload.h:354
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
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:378
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:737
SLV_36
@ SLV_36
36 6624
Definition: saveload.h:86
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
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:93
Order::SetDepotOrderType
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition: order_base.h:156
Order::SetDepotActionType
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition: order_base.h:158
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:655
saveload_internal.h
SLV_180
@ SLV_180
180 24998 1.3.x
Definition: saveload.h:259
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:274
Order::SetUnloadType
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition: order_base.h:150
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:35
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
UnpackVersion4Order
static Order UnpackVersion4Order(uint16 packed)
Unpacks a order from savegames with version 4 and lower.
Definition: order_sl.cpp:79
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1051
Order::next
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:49
Order::flags
uint8 flags
Load/unload types, depot order/action types.
Definition: order_base.h:39
Order
Definition: order_base.h:32
Order::SetNonStopType
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition: order_base.h:152
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:577
OLFB_NO_LOAD
@ OLFB_NO_LOAD
Do not load anything.
Definition: order_type.h:66