# order_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: order_sl.cpp 18683 2010-01-01 18:45:40Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../order_base.h"
    00014 #include "../settings_type.h"
    00015
    00016 #include "saveload.h"
    00017
    00018 void Order::ConvertFromOldSavegame()
    00019 {
    00020   uint8 old_flags = this->flags;
    00021   this->flags = 0;
    00022
    00023   /* First handle non-stop - use value from savegame if possible, else use value from config file */
    00024   if (_settings_client.gui.sg_new_nonstop || (CheckSavegameVersion(22) && _settings_client.gui.new_nonstop)) {
    00025     /* OFB_NON_STOP */
    00026     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    00027   } else {
    00028     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00029   }
    00030
    00031   switch (this->GetType()) {
    00032     /* Only a few types need the other savegame conversions. */
    00033     case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
    00034     default: return;
    00035   }
    00036
    00037   if (this->GetType() != OT_GOTO_DEPOT) {
    00038     /* Then the load flags */
    00039     if ((old_flags & 2) != 0) { // OFB_UNLOAD
    00040       this->SetLoadType(OLFB_NO_LOAD);
    00041     } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
    00042       this->SetLoadType(OLF_LOAD_IF_POSSIBLE);
    00043     } else {
    00044       /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
    00045       this->SetLoadType(_settings_client.gui.sg_full_load_any || CheckSavegameVersion(22) ? OLF_FULL_LOAD_ANY : OLFB_FULL_LOAD);
    00046     }
    00047
    00048     if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OSL_PLATFORM_FAR_END);
    00049
    00050     /* Finally fix the unload flags */
    00051     if ((old_flags & 1) != 0) { // OFB_TRANSFER
    00052       this->SetUnloadType(OUFB_TRANSFER);
    00053     } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
    00054       this->SetUnloadType(OUFB_UNLOAD);
    00055     } else {
    00056       this->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
    00057     }
    00058   } else {
    00059     /* Then the depot action flags */
    00060     this->SetDepotActionType(((old_flags & 6) == 4) ? ODATFB_HALT : ODATF_SERVICE_ONLY);
    00061
    00062     /* Finally fix the depot type flags */
    00063     uint t = ((old_flags & 6) == 6) ? ODTFB_SERVICE : ODTF_MANUAL;
    00064     if ((old_flags & 2) != 0) t |= ODTFB_PART_OF_ORDERS;
    00065     this->SetDepotOrderType((OrderDepotTypeFlags)t);
    00066   }
    00067 }
    00068
    00073 static Order UnpackVersion4Order(uint16 packed)
    00074 {
    00075   return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
    00076 }
    00077
    00082 Order UnpackOldOrder(uint16 packed)
    00083 {
    00084   Order order = UnpackVersion4Order(packed);
    00085
    00086   /*
    00087    * Sanity check
    00088    * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
    00089    */
    00090   if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
    00091
    00092   return order;
    00093 }
    00094
    00095 const SaveLoad *GetOrderDescription()
    00096 {
    00097   static const SaveLoad _order_desc[] = {
    00098          SLE_VAR(Order, type,           SLE_UINT8),
    00099          SLE_VAR(Order, flags,          SLE_UINT8),
    00100          SLE_VAR(Order, dest,           SLE_UINT16),
    00101          SLE_REF(Order, next,           REF_ORDER),
    00102      SLE_CONDVAR(Order, refit_cargo,    SLE_UINT8,   36, SL_MAX_VERSION),
    00103      SLE_CONDVAR(Order, refit_subtype,  SLE_UINT8,   36, SL_MAX_VERSION),
    00104      SLE_CONDVAR(Order, wait_time,      SLE_UINT16,  67, SL_MAX_VERSION),
    00105      SLE_CONDVAR(Order, travel_time,    SLE_UINT16,  67, SL_MAX_VERSION),
    00106
    00107     /* Leftover from the minor savegame version stuff
    00108      * We will never use those free bytes, but we have to keep this line to allow loading of old savegames */
    00109     SLE_CONDNULL(10,                                  5,  35),
    00110          SLE_END()
    00111   };
    00112
    00113   return _order_desc;
    00114 }
    00115
    00116 static void Save_ORDR()
    00117 {
    00118   Order *order;
    00119
    00120   FOR_ALL_ORDERS(order) {
    00121     SlSetArrayIndex(order->index);
    00122     SlObject(order, GetOrderDescription());
    00123   }
    00124 }
    00125
    00126 static void Load_ORDR()
    00127 {
    00128   if (CheckSavegameVersionOldStyle(5, 2)) {
    00129     /* Version older than 5.2 did not have a ->next pointer. Convert them
    00130      * (in the old days, the orderlist was 5000 items big) */
    00131     size_t len = SlGetFieldLength();
    00132
    00133     if (CheckSavegameVersion(5)) {
    00134       /* Pre-version 5 had another layout for orders
    00135        * (uint16 instead of uint32) */
    00136       len /= sizeof(uint16);
    00137       uint16 *orders = MallocT<uint16>(len + 1);
    00138
    00139       SlArray(orders, len, SLE_UINT16);
    00140
    00141       for (size_t i = 0; i < len; ++i) {
    00142         Order *o = new (i) Order();
    00143         o->AssignOrder(UnpackVersion4Order(orders[i]));
    00144       }
    00145
    00146       free(orders);
    00147     } else if (CheckSavegameVersionOldStyle(5, 2)) {
    00148       len /= sizeof(uint32);
    00149       uint32 *orders = MallocT<uint32>(len + 1);
    00150
    00151       SlArray(orders, len, SLE_UINT32);
    00152
    00153       for (size_t i = 0; i < len; ++i) {
    00154         new (i) Order(orders[i]);
    00155       }
    00156
    00157       free(orders);
    00158     }
    00159
    00160     /* Update all the next pointer */
    00161     Order *o;
    00162     FOR_ALL_ORDERS(o) {
    00163       /* Delete invalid orders */
    00164       if (o->IsType(OT_NOTHING)) {
    00165         delete o;
    00166         continue;
    00167       }
    00168       /* The orders were built like this:
    00169        * While the order is valid, set the previous will get its next pointer set */
    00170       Order *prev = Order::GetIfValid(order_index - 1);
    00171       if (prev != NULL) prev->next = o;
    00172     }
    00173   } else {
    00174     int index;
    00175
    00176     while ((index = SlIterateArray()) != -1) {
    00177       Order *order = new (index) Order();
    00178       SlObject(order, GetOrderDescription());
    00179     }
    00180   }
    00181 }
    00182
    00183 static void Ptrs_ORDR()
    00184 {
    00185   /* Orders from old savegames have pointers corrected in Load_ORDR */
    00186   if (CheckSavegameVersionOldStyle(5, 2)) return;
    00187
    00188   Order *o;
    00189
    00190   FOR_ALL_ORDERS(o) {
    00191     SlObject(o, GetOrderDescription());
    00192   }
    00193 }
    00194
    00195 const SaveLoad *GetOrderListDescription()
    00196 {
    00197   static const SaveLoad _orderlist_desc[] = {
    00198     SLE_REF(OrderList, first,              REF_ORDER),
    00199     SLE_END()
    00200   };
    00201
    00202   return _orderlist_desc;
    00203 }
    00204
    00205 static void Save_ORDL()
    00206 {
    00207   OrderList *list;
    00208
    00209   FOR_ALL_ORDER_LISTS(list) {
    00210     SlSetArrayIndex(list->index);
    00211     SlObject(list, GetOrderListDescription());
    00212   }
    00213 }
    00214
    00215 static void Load_ORDL()
    00216 {
    00217   int index;
    00218
    00219   while ((index = SlIterateArray()) != -1) {
    00220     /* set num_orders to 0 so it's a valid OrderList */
    00221     OrderList *list = new (index) OrderList(0);
    00222     SlObject(list, GetOrderListDescription());
    00223   }
    00224
    00225 }
    00226
    00227 static void Ptrs_ORDL()
    00228 {
    00229   OrderList *list;
    00230
    00231   FOR_ALL_ORDER_LISTS(list) {
    00232     SlObject(list, GetOrderListDescription());
    00233   }
    00234 }
    00235
    00236 extern const ChunkHandler _order_chunk_handlers[] = {
    00237   { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, CH_ARRAY},
    00238   { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, CH_ARRAY | CH_LAST},
    00239 };