order_sl.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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_backup.h"
00014 #include "../settings_type.h"
00015 #include "../network/network.h"
00016 
00017 #include "saveload_internal.h"
00018 
00023 void Order::ConvertFromOldSavegame()
00024 {
00025   uint8 old_flags = this->flags;
00026   this->flags = 0;
00027 
00028   /* First handle non-stop - use value from savegame if possible, else use value from config file */
00029   if (_settings_client.gui.sg_new_nonstop || (IsSavegameVersionBefore(22) && _savegame_type != SGT_TTO && _savegame_type != SGT_TTD && _settings_client.gui.new_nonstop)) {
00030     /* OFB_NON_STOP */
00031     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
00032   } else {
00033     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
00034   }
00035 
00036   switch (this->GetType()) {
00037     /* Only a few types need the other savegame conversions. */
00038     case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
00039     default: return;
00040   }
00041 
00042   if (this->GetType() != OT_GOTO_DEPOT) {
00043     /* Then the load flags */
00044     if ((old_flags & 2) != 0) { // OFB_UNLOAD
00045       this->SetLoadType(OLFB_NO_LOAD);
00046     } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
00047       this->SetLoadType(OLF_LOAD_IF_POSSIBLE);
00048     } else {
00049       /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
00050       this->SetLoadType(_settings_client.gui.sg_full_load_any || IsSavegameVersionBefore(22) ? OLF_FULL_LOAD_ANY : OLFB_FULL_LOAD);
00051     }
00052 
00053     if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OSL_PLATFORM_FAR_END);
00054 
00055     /* Finally fix the unload flags */
00056     if ((old_flags & 1) != 0) { // OFB_TRANSFER
00057       this->SetUnloadType(OUFB_TRANSFER);
00058     } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
00059       this->SetUnloadType(OUFB_UNLOAD);
00060     } else {
00061       this->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
00062     }
00063   } else {
00064     /* Then the depot action flags */
00065     this->SetDepotActionType(((old_flags & 6) == 4) ? ODATFB_HALT : ODATF_SERVICE_ONLY);
00066 
00067     /* Finally fix the depot type flags */
00068     uint t = ((old_flags & 6) == 6) ? ODTFB_SERVICE : ODTF_MANUAL;
00069     if ((old_flags & 2) != 0) t |= ODTFB_PART_OF_ORDERS;
00070     this->SetDepotOrderType((OrderDepotTypeFlags)t);
00071   }
00072 }
00073 
00079 static Order UnpackVersion4Order(uint16 packed)
00080 {
00081   return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
00082 }
00083 
00089 Order UnpackOldOrder(uint16 packed)
00090 {
00091   Order order = UnpackVersion4Order(packed);
00092 
00093   /*
00094    * Sanity check
00095    * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
00096    */
00097   if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
00098 
00099   return order;
00100 }
00101 
00102 const SaveLoad *GetOrderDescription()
00103 {
00104   static const SaveLoad _order_desc[] = {
00105          SLE_VAR(Order, type,           SLE_UINT8),
00106          SLE_VAR(Order, flags,          SLE_UINT8),
00107          SLE_VAR(Order, dest,           SLE_UINT16),
00108          SLE_REF(Order, next,           REF_ORDER),
00109      SLE_CONDVAR(Order, refit_cargo,    SLE_UINT8,   36, SL_MAX_VERSION),
00110     SLE_CONDNULL(1,                                  36, 181), // refit_subtype
00111      SLE_CONDVAR(Order, wait_time,      SLE_UINT16,  67, SL_MAX_VERSION),
00112      SLE_CONDVAR(Order, travel_time,    SLE_UINT16,  67, SL_MAX_VERSION),
00113      SLE_CONDVAR(Order, max_speed,      SLE_UINT16, 172, SL_MAX_VERSION),
00114 
00115     /* Leftover from the minor savegame version stuff
00116      * We will never use those free bytes, but we have to keep this line to allow loading of old savegames */
00117     SLE_CONDNULL(10,                                  5,  35),
00118          SLE_END()
00119   };
00120 
00121   return _order_desc;
00122 }
00123 
00124 static void Save_ORDR()
00125 {
00126   Order *order;
00127 
00128   FOR_ALL_ORDERS(order) {
00129     SlSetArrayIndex(order->index);
00130     SlObject(order, GetOrderDescription());
00131   }
00132 }
00133 
00134 static void Load_ORDR()
00135 {
00136   if (IsSavegameVersionBefore(5, 2)) {
00137     /* Version older than 5.2 did not have a ->next pointer. Convert them
00138      * (in the old days, the orderlist was 5000 items big) */
00139     size_t len = SlGetFieldLength();
00140 
00141     if (IsSavegameVersionBefore(5)) {
00142       /* Pre-version 5 had another layout for orders
00143        * (uint16 instead of uint32) */
00144       len /= sizeof(uint16);
00145       uint16 *orders = MallocT<uint16>(len + 1);
00146 
00147       SlArray(orders, len, SLE_UINT16);
00148 
00149       for (size_t i = 0; i < len; ++i) {
00150         Order *o = new (i) Order();
00151         o->AssignOrder(UnpackVersion4Order(orders[i]));
00152       }
00153 
00154       free(orders);
00155     } else if (IsSavegameVersionBefore(5, 2)) {
00156       len /= sizeof(uint32);
00157       uint32 *orders = MallocT<uint32>(len + 1);
00158 
00159       SlArray(orders, len, SLE_UINT32);
00160 
00161       for (size_t i = 0; i < len; ++i) {
00162         new (i) Order(orders[i]);
00163       }
00164 
00165       free(orders);
00166     }
00167 
00168     /* Update all the next pointer */
00169     Order *o;
00170     FOR_ALL_ORDERS(o) {
00171       /* Delete invalid orders */
00172       if (o->IsType(OT_NOTHING)) {
00173         delete o;
00174         continue;
00175       }
00176       /* The orders were built like this:
00177        * While the order is valid, set the previous will get its next pointer set */
00178       Order *prev = Order::GetIfValid(order_index - 1);
00179       if (prev != NULL) prev->next = o;
00180     }
00181   } else {
00182     int index;
00183 
00184     while ((index = SlIterateArray()) != -1) {
00185       Order *order = new (index) Order();
00186       SlObject(order, GetOrderDescription());
00187     }
00188   }
00189 }
00190 
00191 static void Ptrs_ORDR()
00192 {
00193   /* Orders from old savegames have pointers corrected in Load_ORDR */
00194   if (IsSavegameVersionBefore(5, 2)) return;
00195 
00196   Order *o;
00197 
00198   FOR_ALL_ORDERS(o) {
00199     SlObject(o, GetOrderDescription());
00200   }
00201 }
00202 
00203 const SaveLoad *GetOrderListDescription()
00204 {
00205   static const SaveLoad _orderlist_desc[] = {
00206     SLE_REF(OrderList, first,              REF_ORDER),
00207     SLE_END()
00208   };
00209 
00210   return _orderlist_desc;
00211 }
00212 
00213 static void Save_ORDL()
00214 {
00215   OrderList *list;
00216 
00217   FOR_ALL_ORDER_LISTS(list) {
00218     SlSetArrayIndex(list->index);
00219     SlObject(list, GetOrderListDescription());
00220   }
00221 }
00222 
00223 static void Load_ORDL()
00224 {
00225   int index;
00226 
00227   while ((index = SlIterateArray()) != -1) {
00228     /* set num_orders to 0 so it's a valid OrderList */
00229     OrderList *list = new (index) OrderList(0);
00230     SlObject(list, GetOrderListDescription());
00231   }
00232 
00233 }
00234 
00235 static void Ptrs_ORDL()
00236 {
00237   OrderList *list;
00238 
00239   FOR_ALL_ORDER_LISTS(list) {
00240     SlObject(list, GetOrderListDescription());
00241   }
00242 }
00243 
00244 const SaveLoad *GetOrderBackupDescription()
00245 {
00246   static const SaveLoad _order_backup_desc[] = {
00247          SLE_VAR(OrderBackup, user,                     SLE_UINT32),
00248          SLE_VAR(OrderBackup, tile,                     SLE_UINT32),
00249          SLE_VAR(OrderBackup, group,                    SLE_UINT16),
00250          SLE_VAR(OrderBackup, service_interval,         SLE_UINT32),
00251          SLE_STR(OrderBackup, name,                     SLE_STR, 0),
00252          SLE_VAR(OrderBackup, clone,                    SLE_UINT16),
00253          SLE_VAR(OrderBackup, cur_real_order_index,     SLE_UINT8),
00254      SLE_CONDVAR(OrderBackup, cur_implicit_order_index, SLE_UINT8,                 176, SL_MAX_VERSION),
00255      SLE_CONDVAR(OrderBackup, current_order_time,       SLE_UINT32,                176, SL_MAX_VERSION),
00256      SLE_CONDVAR(OrderBackup, lateness_counter,         SLE_INT32,                 176, SL_MAX_VERSION),
00257      SLE_CONDVAR(OrderBackup, timetable_start,          SLE_INT32,                 176, SL_MAX_VERSION),
00258      SLE_CONDVAR(OrderBackup, vehicle_flags,            SLE_FILE_U8 | SLE_VAR_U16, 176, 179),
00259      SLE_CONDVAR(OrderBackup, vehicle_flags,            SLE_UINT16,                180, SL_MAX_VERSION),
00260          SLE_REF(OrderBackup, orders,                   REF_ORDER),
00261          SLE_END()
00262   };
00263 
00264   return _order_backup_desc;
00265 }
00266 
00267 static void Save_BKOR()
00268 {
00269   /* We only save this when we're a network server
00270    * as we want this information on our clients. For
00271    * normal games this information isn't needed. */
00272   if (!_networking || !_network_server) return;
00273 
00274   OrderBackup *ob;
00275   FOR_ALL_ORDER_BACKUPS(ob) {
00276     SlSetArrayIndex(ob->index);
00277     SlObject(ob, GetOrderBackupDescription());
00278   }
00279 }
00280 
00281 void Load_BKOR()
00282 {
00283   int index;
00284 
00285   while ((index = SlIterateArray()) != -1) {
00286     /* set num_orders to 0 so it's a valid OrderList */
00287     OrderBackup *ob = new (index) OrderBackup();
00288     SlObject(ob, GetOrderBackupDescription());
00289   }
00290 
00291   /* Only load order-backups for network clients.
00292    * If we are a network server or not networking, then we just loaded
00293    * a previously saved-by-server savegame. There are
00294    * no clients with a backup anymore, so clear it. */
00295   if (!_networking || _network_server) {
00296     _order_backup_pool.CleanPool();
00297   }
00298 }
00299 
00300 static void Ptrs_BKOR()
00301 {
00302   OrderBackup *ob;
00303   FOR_ALL_ORDER_BACKUPS(ob) {
00304     SlObject(ob, GetOrderBackupDescription());
00305   }
00306 }
00307 
00308 extern const ChunkHandler _order_chunk_handlers[] = {
00309   { 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, NULL, CH_ARRAY},
00310   { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, NULL, CH_ARRAY},
00311   { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, NULL, CH_ARRAY | CH_LAST},
00312 };