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
25 void Order::ConvertFromOldSavegame()
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 */
31 if (_settings_client.gui.sg_new_nonstop || (IsSavegameVersionBefore(22) && _savegame_type != SGT_TTO && _savegame_type != SGT_TTD && _settings_client.gui.new_nonstop)) {
32 /* OFB_NON_STOP */
33 this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
34 } else {
35 this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
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
47 this->SetLoadType(OLFB_NO_LOAD);
48 } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
49 this->SetLoadType(OLF_LOAD_IF_POSSIBLE);
50 } else {
51 /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
52 this->SetLoadType(_settings_client.gui.sg_full_load_any || IsSavegameVersionBefore(22) ? OLF_FULL_LOAD_ANY : OLFB_FULL_LOAD);
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
59 this->SetUnloadType(OUFB_TRANSFER);
60 } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
61 this->SetUnloadType(OUFB_UNLOAD);
62 } else {
63 this->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
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;
72 this->SetDepotOrderType((OrderDepotTypeFlags)t);
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
104 const SaveLoad *GetOrderDescription()
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),
110 SLE_REF(Order, next, REF_ORDER),
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) {
205 SlObject(o, GetOrderDescription());
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)
260 SLE_CONDREF(OrderBackup, clone, REF_VEHICLE, 192, SL_MAX_VERSION),
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;
283 FOR_ALL_ORDER_BACKUPS(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;
303 FOR_ALL_ORDER_BACKUPS(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 };
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
Pool::PoolItem<&_order_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
Order::GetTravelTime
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
OLF_LOAD_IF_POSSIBLE
Load as long as there is cargo that fits in the train.
Definition: order_type.h:69
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:256
Order::SetDepotOrderType
void SetDepotOrderType(OrderDepotTypeFlags depot_order_type)
Set the cause to go to the depot.
Definition: order_base.h:158
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:304
Order::SetDepotActionType
void SetDepotActionType(OrderDepotActionFlags depot_service_type)
Set what we are going to do in the depot.
Definition: order_base.h:160
OUF_UNLOAD_IF_POSSIBLE
Unload all cargo that the station accepts.
Definition: order_type.h:59
ODATF_SERVICE_ONLY
Only service the vehicle.
Definition: order_type.h:109
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
SGT_TTD
TTD savegame (can be detected incorrectly)
Definition: saveload.h:41
Order::refit_cargo
CargoID refit_cargo
Refit CargoID.
Definition: order_base.h:44
Order::SetUnloadType
void SetUnloadType(OrderUnloadFlags unload_type)
Set how the consist must be unloaded.
Definition: order_base.h:152
Order
Definition: order_base.h:34
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:934
Order::next
Order * next
Pointer to next order. If NULL, end of list.
Definition: order_base.h:51
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1186
OSL_PLATFORM_FAR_END
Stop at the far end of the platform.
Definition: order_type.h:92
Order::dest
DestinationID dest
The destination of the order.
Definition: order_base.h:42
SGT_TTO
TTO savegame.
Definition: saveload.h:45
Order::travel_time
uint16 travel_time
How long in ticks the journey to this destination should take.
Definition: order_base.h:47
ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition: order_type.h:79
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:252
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
Order::type
uint8 type
The type of order + non-stop flags.
Definition: order_base.h:40
Order::SetNonStopType
void SetNonStopType(OrderNonStopFlags non_stop_type)
Set whether we must stop at stations or not.
Definition: order_base.h:154
FOR_ALL_ORDER_BACKUPS
#define FOR_ALL_ORDER_BACKUPS(var)
Iterator over all order backups.
Definition: order_backup.h:80
UnpackOldOrder
Order UnpackOldOrder(uint16 packed)
Unpacks a order from savegames made with TTD(Patch)
Definition: order_sl.cpp:91
ONSF_NO_STOP_AT_ANY_STATION
The vehicle will not stop at any stations it passes including the destination.
Definition: order_type.h:82
Order::MakeDummy
void MakeDummy()
Makes this order a Dummy order.
Definition: order_cmd.cpp:134
OLFB_NO_LOAD
Do not load anything.
Definition: order_type.h:72
ODTF_MANUAL
Manually initiated order.
Definition: order_type.h:100
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
Order::GetOrderDescription
friend const struct SaveLoad * GetOrderDescription()
Saving and loading of orders.
Definition: order_sl.cpp:104
Order::flags
uint8 flags
Load/unload types, depot order/action types.
Definition: order_base.h:41
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:271
REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:81
ODATFB_HALT
Service the vehicle and then halt it.
Definition: order_type.h:110
Order::SetWaitTimetabled
void SetWaitTimetabled(bool timetabled)
Set if the wait time is explicitly timetabled (unless the order is conditional).
Definition: order_base.h:197
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:82
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition: order_type.h:61
Order::SetLoadType
void SetLoadType(OrderLoadFlags load_type)
Set how the consist must be loaded.
Definition: order_base.h:150
Order::GetWaitTime
uint16 GetWaitTime() const
Get the time in ticks a vehicle will probably wait at the destination (timetabled or not)...
Definition: order_base.h:185
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:561
GUISettings::sg_new_nonstop
bool sg_new_nonstop
ttdpatch compatible nonstop handling read from pre v93 savegames
Definition: settings_type.h:81
Order::wait_time
uint16 wait_time
How long in ticks to wait at the destination.
Definition: order_base.h:46
Order::SetTravelTimetabled
void SetTravelTimetabled(bool timetabled)
Set if the travel time is explicitly timetabled (unless the order is conditional).
Definition: order_base.h:199
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:34
OLFB_FULL_LOAD
Full load all cargoes of the consist.
Definition: order_type.h:70
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
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:274
ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
The vehicle will not stop at any stations it passes except the destination.
Definition: order_type.h:80
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
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:76
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
Order::GetType
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:69
GUISettings::new_nonstop
bool new_nonstop
ttdpatch compatible nonstop handling
Definition: settings_type.h:82
OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:60
Order::ConvertFromOldSavegame
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
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:37
ODTFB_PART_OF_ORDERS
This depot order is because of a regular order.
Definition: order_type.h:102
UnpackVersion4Order
static Order UnpackVersion4Order(uint16 packed)
Unpacks a order from savegames with version 4 and lower.
Definition: order_sl.cpp:81
Order::max_speed
uint16 max_speed
How fast the vehicle may go on the way to the destination.
Definition: order_base.h:48
saveload_internal.h
Declaration of functions used in more save/load files.
OLF_FULL_LOAD_ANY
Full load a single cargo of the consist.
Definition: order_type.h:71
ODTFB_SERVICE
This depot order is because of the servicing limit.
Definition: order_type.h:101
Order::SetStopLocation
void SetStopLocation(OrderStopLocation stop_location)
Set where we must stop at the platform.
Definition: order_base.h:156
OrderDepotTypeFlags
OrderDepotTypeFlags
Reasons that could cause us to go to the depot.
Definition: order_type.h:99
CH_LAST
Last chunk in this array.
Definition: saveload.h:104