order_sl.cpp

Go to the documentation of this file.
00001 /* $Id: order_sl.cpp 21846 2011-01-18 23:09:43Z 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 "../order_backup.h"
00015 #include "../settings_type.h"
00016 #include "../network/network.h"
00017 
00018 #include "saveload.h"
00019 
00024 void Order::ConvertFromOldSavegame()
00025 {
00026   uint8 old_flags = this->flags;
00027   this->flags = 0;
00028 
00029   /* First handle non-stop - use value from savegame if possible, else use value from config file */
00030   if (_settings_client.gui.sg_new_nonstop || (IsSavegameVersionBefore(22) && _settings_client.gui.new_nonstop)) {
00031     /* OFB_NON_STOP */
00032     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
00033   } else {
00034     this->SetNonStopType((old_flags & 8) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
00035   }
00036 
00037   switch (this->GetType()) {
00038     /* Only a few types need the other savegame conversions. */
00039     case OT_GOTO_DEPOT: case OT_GOTO_STATION: case OT_LOADING: break;
00040     default: return;
00041   }
00042 
00043   if (this->GetType() != OT_GOTO_DEPOT) {
00044     /* Then the load flags */
00045     if ((old_flags & 2) != 0) { // OFB_UNLOAD
00046       this->SetLoadType(OLFB_NO_LOAD);
00047     } else if ((old_flags & 4) == 0) { // !OFB_FULL_LOAD
00048       this->SetLoadType(OLF_LOAD_IF_POSSIBLE);
00049     } else {
00050       /* old OTTD versions stored full_load_any in config file - assume it was enabled when loading */
00051       this->SetLoadType(_settings_client.gui.sg_full_load_any || IsSavegameVersionBefore(22) ? OLF_FULL_LOAD_ANY : OLFB_FULL_LOAD);
00052     }
00053 
00054     if (this->IsType(OT_GOTO_STATION)) this->SetStopLocation(OSL_PLATFORM_FAR_END);
00055 
00056     /* Finally fix the unload flags */
00057     if ((old_flags & 1) != 0) { // OFB_TRANSFER
00058       this->SetUnloadType(OUFB_TRANSFER);
00059     } else if ((old_flags & 2) != 0) { // OFB_UNLOAD
00060       this->SetUnloadType(OUFB_UNLOAD);
00061     } else {
00062       this->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
00063     }
00064   } else {
00065     /* Then the depot action flags */
00066     this->SetDepotActionType(((old_flags & 6) == 4) ? ODATFB_HALT : ODATF_SERVICE_ONLY);
00067 
00068     /* Finally fix the depot type flags */
00069     uint t = ((old_flags & 6) == 6) ? ODTFB_SERVICE : ODTF_MANUAL;
00070     if ((old_flags & 2) != 0) t |= ODTFB_PART_OF_ORDERS;
00071     this->SetDepotOrderType((OrderDepotTypeFlags)t);
00072   }
00073 }
00074 
00080 static Order UnpackVersion4Order(uint16 packed)
00081 {
00082   return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
00083 }
00084 
00090 Order UnpackOldOrder(uint16 packed)
00091 {
00092   Order order = UnpackVersion4Order(packed);
00093 
00094   /*
00095    * Sanity check
00096    * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
00097    */
00098   if (order.IsType(OT_NOTHING) && packed != 0) order.MakeDummy();
00099 
00100   return order;
00101 }
00102 
00103 const SaveLoad *GetOrderDescription()
00104 {
00105   static const SaveLoad _order_desc[] = {
00106          SLE_VAR(Order, type,           SLE_UINT8),
00107          SLE_VAR(Order, flags,          SLE_UINT8),
00108          SLE_VAR(Order, dest,           SLE_UINT16),
00109          SLE_REF(Order, next,           REF_ORDER),
00110      SLE_CONDVAR(Order, refit_cargo,    SLE_UINT8,   36, SL_MAX_VERSION),
00111      SLE_CONDVAR(Order, refit_subtype,  SLE_UINT8,   36, SL_MAX_VERSION),
00112      SLE_CONDVAR(Order, wait_time,      SLE_UINT16,  67, SL_MAX_VERSION),
00113      SLE_CONDVAR(Order, travel_time,    SLE_UINT16,  67, 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_INT32),
00251     SLE_STR(OrderBackup, name,                  SLE_STR, 0),
00252     SLE_VAR(OrderBackup, clone,                 SLE_UINT16),
00253     SLE_VAR(OrderBackup, orderindex,            SLE_UINT8),
00254     SLE_REF(OrderBackup, orders,                REF_ORDER),
00255     SLE_END()
00256   };
00257 
00258   return _order_backup_desc;
00259 }
00260 
00261 static void Save_BKOR()
00262 {
00263   /* We only save this when we're a network server
00264    * as we want this information on our clients. For
00265    * normal games this information isn't needed. */
00266   if (!_networking || !_network_server) return;
00267 
00268   OrderBackup *ob;
00269   FOR_ALL_ORDER_BACKUPS(ob) {
00270     SlSetArrayIndex(ob->index);
00271     SlObject(ob, GetOrderBackupDescription());
00272   }
00273 }
00274 
00275 void Load_BKOR()
00276 {
00277   int index;
00278 
00279   while ((index = SlIterateArray()) != -1) {
00280     /* set num_orders to 0 so it's a valid OrderList */
00281     OrderBackup *ob = new (index) OrderBackup();
00282     SlObject(ob, GetOrderBackupDescription());
00283   }
00284 
00285   /* If we are a network server, then we just loaded
00286    * a previously saved-by-server savegame. There are
00287    * no clients with a backup anymore, so clear it. */
00288   if (_networking && _network_server) {
00289     _order_backup_pool.CleanPool();
00290   }
00291 }
00292 
00293 static void Ptrs_BKOR()
00294 {
00295   OrderBackup *ob;
00296   FOR_ALL_ORDER_BACKUPS(ob) {
00297     SlObject(ob, GetOrderBackupDescription());
00298   }
00299 }
00300 
00301 extern const ChunkHandler _order_chunk_handlers[] = {
00302   { 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, NULL, CH_ARRAY},
00303   { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, NULL, CH_ARRAY},
00304   { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, NULL, CH_ARRAY | CH_LAST},
00305 };

Generated on Sun May 15 19:20:14 2011 for OpenTTD by  doxygen 1.6.1