order_sl.cpp

Go to the documentation of this file.
00001 /* $Id: order_sl.cpp 22914 2011-09-09 21:12:52Z 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_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_CONDVAR(Order, refit_subtype,  SLE_UINT8,   36, SL_MAX_VERSION),
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 
00114     /* Leftover from the minor savegame version stuff
00115      * We will never use those free bytes, but we have to keep this line to allow loading of old savegames */
00116     SLE_CONDNULL(10,                                  5,  35),
00117          SLE_END()
00118   };
00119 
00120   return _order_desc;
00121 }
00122 
00123 static void Save_ORDR()
00124 {
00125   Order *order;
00126 
00127   FOR_ALL_ORDERS(order) {
00128     SlSetArrayIndex(order->index);
00129     SlObject(order, GetOrderDescription());
00130   }
00131 }
00132 
00133 static void Load_ORDR()
00134 {
00135   if (IsSavegameVersionBefore(5, 2)) {
00136     /* Version older than 5.2 did not have a ->next pointer. Convert them
00137      * (in the old days, the orderlist was 5000 items big) */
00138     size_t len = SlGetFieldLength();
00139 
00140     if (IsSavegameVersionBefore(5)) {
00141       /* Pre-version 5 had another layout for orders
00142        * (uint16 instead of uint32) */
00143       len /= sizeof(uint16);
00144       uint16 *orders = MallocT<uint16>(len + 1);
00145 
00146       SlArray(orders, len, SLE_UINT16);
00147 
00148       for (size_t i = 0; i < len; ++i) {
00149         Order *o = new (i) Order();
00150         o->AssignOrder(UnpackVersion4Order(orders[i]));
00151       }
00152 
00153       free(orders);
00154     } else if (IsSavegameVersionBefore(5, 2)) {
00155       len /= sizeof(uint32);
00156       uint32 *orders = MallocT<uint32>(len + 1);
00157 
00158       SlArray(orders, len, SLE_UINT32);
00159 
00160       for (size_t i = 0; i < len; ++i) {
00161         new (i) Order(orders[i]);
00162       }
00163 
00164       free(orders);
00165     }
00166 
00167     /* Update all the next pointer */
00168     Order *o;
00169     FOR_ALL_ORDERS(o) {
00170       /* Delete invalid orders */
00171       if (o->IsType(OT_NOTHING)) {
00172         delete o;
00173         continue;
00174       }
00175       /* The orders were built like this:
00176        * While the order is valid, set the previous will get its next pointer set */
00177       Order *prev = Order::GetIfValid(order_index - 1);
00178       if (prev != NULL) prev->next = o;
00179     }
00180   } else {
00181     int index;
00182 
00183     while ((index = SlIterateArray()) != -1) {
00184       Order *order = new (index) Order();
00185       SlObject(order, GetOrderDescription());
00186     }
00187   }
00188 }
00189 
00190 static void Ptrs_ORDR()
00191 {
00192   /* Orders from old savegames have pointers corrected in Load_ORDR */
00193   if (IsSavegameVersionBefore(5, 2)) return;
00194 
00195   Order *o;
00196 
00197   FOR_ALL_ORDERS(o) {
00198     SlObject(o, GetOrderDescription());
00199   }
00200 }
00201 
00202 const SaveLoad *GetOrderListDescription()
00203 {
00204   static const SaveLoad _orderlist_desc[] = {
00205     SLE_REF(OrderList, first,              REF_ORDER),
00206     SLE_END()
00207   };
00208 
00209   return _orderlist_desc;
00210 }
00211 
00212 static void Save_ORDL()
00213 {
00214   OrderList *list;
00215 
00216   FOR_ALL_ORDER_LISTS(list) {
00217     SlSetArrayIndex(list->index);
00218     SlObject(list, GetOrderListDescription());
00219   }
00220 }
00221 
00222 static void Load_ORDL()
00223 {
00224   int index;
00225 
00226   while ((index = SlIterateArray()) != -1) {
00227     /* set num_orders to 0 so it's a valid OrderList */
00228     OrderList *list = new (index) OrderList(0);
00229     SlObject(list, GetOrderListDescription());
00230   }
00231 
00232 }
00233 
00234 static void Ptrs_ORDL()
00235 {
00236   OrderList *list;
00237 
00238   FOR_ALL_ORDER_LISTS(list) {
00239     SlObject(list, GetOrderListDescription());
00240   }
00241 }
00242 
00243 const SaveLoad *GetOrderBackupDescription()
00244 {
00245   static const SaveLoad _order_backup_desc[] = {
00246     SLE_VAR(OrderBackup, user,                  SLE_UINT32),
00247     SLE_VAR(OrderBackup, tile,                  SLE_UINT32),
00248     SLE_VAR(OrderBackup, group,                 SLE_UINT16),
00249     SLE_VAR(OrderBackup, service_interval,      SLE_INT32),
00250     SLE_STR(OrderBackup, name,                  SLE_STR, 0),
00251     SLE_VAR(OrderBackup, clone,                 SLE_UINT16),
00252     SLE_VAR(OrderBackup, orderindex,            SLE_UINT8),
00253     SLE_REF(OrderBackup, orders,                REF_ORDER),
00254     SLE_END()
00255   };
00256 
00257   return _order_backup_desc;
00258 }
00259 
00260 static void Save_BKOR()
00261 {
00262   /* We only save this when we're a network server
00263    * as we want this information on our clients. For
00264    * normal games this information isn't needed. */
00265   if (!_networking || !_network_server) return;
00266 
00267   OrderBackup *ob;
00268   FOR_ALL_ORDER_BACKUPS(ob) {
00269     SlSetArrayIndex(ob->index);
00270     SlObject(ob, GetOrderBackupDescription());
00271   }
00272 }
00273 
00274 void Load_BKOR()
00275 {
00276   int index;
00277 
00278   while ((index = SlIterateArray()) != -1) {
00279     /* set num_orders to 0 so it's a valid OrderList */
00280     OrderBackup *ob = new (index) OrderBackup();
00281     SlObject(ob, GetOrderBackupDescription());
00282   }
00283 
00284   /* If we are a network server, then we just loaded
00285    * a previously saved-by-server savegame. There are
00286    * no clients with a backup anymore, so clear it. */
00287   if (_networking && _network_server) {
00288     _order_backup_pool.CleanPool();
00289   }
00290 }
00291 
00292 static void Ptrs_BKOR()
00293 {
00294   OrderBackup *ob;
00295   FOR_ALL_ORDER_BACKUPS(ob) {
00296     SlObject(ob, GetOrderBackupDescription());
00297   }
00298 }
00299 
00300 extern const ChunkHandler _order_chunk_handlers[] = {
00301   { 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, NULL, CH_ARRAY},
00302   { 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, NULL, CH_ARRAY},
00303   { 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, NULL, CH_ARRAY | CH_LAST},
00304 };