order_sl.cpp

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

Generated on Sun Jan 9 16:02:00 2011 for OpenTTD by  doxygen 1.6.1