cargodest_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 "../cargodest_base.h"
00014 #include "../town.h"
00015 #include "../industry.h"
00016 #include "saveload.h"
00017 
00018 static uint32 _cargolink_uint;
00019 static const SaveLoadGlobVarList _cargolink_uint_desc[] = {
00020   SLEG_VAR(_cargolink_uint, SLE_UINT32),
00021   SLEG_END()
00022 };
00023 
00024 static const SaveLoad _cargolink_desc[] = {
00025   SLE_VAR(CargoLink, amount.old_max, SLE_UINT32),
00026   SLE_VAR(CargoLink, amount.new_max, SLE_UINT32),
00027   SLE_VAR(CargoLink, amount.old_act, SLE_UINT32),
00028   SLE_VAR(CargoLink, amount.new_act, SLE_UINT32),
00029   SLE_VAR(CargoLink, weight,         SLE_UINT32),
00030   SLE_VAR(CargoLink, weight_mod,     SLE_UINT8),
00031   SLE_END()
00032 };
00033 
00034 void CargoSourceSink::SaveCargoSourceSink()
00035 {
00036   if (IsSavegameVersionBefore(161)) return;
00037 
00038   static const SaveLoad _cargosourcesink_desc[] = {
00039     SLE_ARR(CargoSourceSink, cargo_links_weight, SLE_UINT32, NUM_CARGO),
00040     SLE_END()
00041   };
00042   SlObject(this, _cargosourcesink_desc);
00043 
00044   for (uint cid = 0; cid < lengthof(this->cargo_links); cid++) {
00045     _cargolink_uint = this->cargo_links[cid].Length();
00046     SlObject(NULL, _cargolink_uint_desc);
00047     for (CargoLink *l = this->cargo_links[cid].Begin(); l != this->cargo_links[cid].End(); l++) {
00048       SourceID dest = INVALID_SOURCE;
00049       SourceTypeByte type;
00050       type = ST_TOWN;
00051 
00052       if (l->dest != NULL) {
00053         type = l->dest->GetType();
00054         dest = l->dest->GetID();
00055       }
00056 
00057       /* Pack type and destination index into temp variable. */
00058       assert_compile(sizeof(SourceID) <= 3);
00059       _cargolink_uint = type | (dest << 8);
00060 
00061       SlGlobList(_cargolink_uint_desc);
00062       SlObject(l, _cargolink_desc);
00063     }
00064   }
00065 }
00066 
00067 void CargoSourceSink::LoadCargoSourceSink()
00068 {
00069   if (IsSavegameVersionBefore(161)) return;
00070 
00071   static const SaveLoad _cargosourcesink_desc[] = {
00072     SLE_ARR(CargoSourceSink, cargo_links_weight, SLE_UINT32, NUM_CARGO),
00073     SLE_END()
00074   };
00075   SlObject(this, _cargosourcesink_desc);
00076 
00077   for (uint cid = 0; cid < lengthof(this->cargo_links); cid++) {
00078     /* Remove links created by constructors. */
00079     this->cargo_links[cid].Clear();
00080     /* Read vector length and allocate storage. */
00081     SlObject(NULL, _cargolink_uint_desc);
00082     this->cargo_links[cid].Append(_cargolink_uint);
00083 
00084     for (CargoLink *l = this->cargo_links[cid].Begin(); l != this->cargo_links[cid].End(); l++) {
00085       /* Read packed type and dest and store in dest pointer. */
00086       SlGlobList(_cargolink_uint_desc);
00087       *(size_t*)&l->dest = _cargolink_uint;
00088 
00089       SlObject(l, _cargolink_desc);
00090     }
00091   }
00092 }
00093 
00094 void CargoSourceSink::PtrsCargoSourceSink()
00095 {
00096   if (IsSavegameVersionBefore(161)) return;
00097 
00098   for (uint cid = 0; cid < lengthof(this->cargo_links); cid++) {
00099     for (CargoLink *l = this->cargo_links[cid].Begin(); l != this->cargo_links[cid].End(); l++) {
00100       /* Extract type and destination index. */
00101       SourceType type = (SourceType)((size_t)l->dest & 0xFF);
00102       SourceID dest = (SourceID)((size_t)l->dest >> 8);
00103 
00104       /* Resolve index. */
00105       l->dest = NULL;
00106       if (dest != INVALID_SOURCE) {
00107         switch (type) {
00108           case ST_TOWN:
00109             if (!Town::IsValidID(dest)) SlErrorCorrupt("Invalid cargo link destination");
00110             l->dest = Town::Get(dest);
00111             break;
00112 
00113           case ST_INDUSTRY:
00114             if (!Industry::IsValidID(dest)) SlErrorCorrupt("Invalid cargo link destination");
00115             l->dest = Industry::Get(dest);
00116             break;
00117 
00118           default:
00119             SlErrorCorrupt("Invalid cargo link destination type");
00120         }
00121       }
00122     }
00123   }
00124 }
00125 
00131 const SaveLoad *GetRouteLinkDescription()
00132 {
00133   static const SaveLoad _routelink_desc[] = {
00134     SLE_VAR(RouteLink, dest,         SLE_UINT16),
00135     SLE_VAR(RouteLink, prev_order,   SLE_UINT16),
00136     SLE_VAR(RouteLink, next_order,   SLE_UINT16),
00137     SLE_VAR(RouteLink, owner,        SLE_UINT8),
00138     SLE_VAR(RouteLink, vtype,        SLE_UINT8),
00139     SLE_VAR(RouteLink, travel_time,  SLE_UINT32),
00140     SLE_VAR(RouteLink, wait_time,    SLE_UINT16),
00141 
00142     SLE_END()
00143   };
00144   return _routelink_desc;
00145 }
00146 
00148 static void Save_RTLN()
00149 {
00150   RouteLink *link;
00151 
00152   FOR_ALL_ROUTELINKS(link) {
00153     SlSetArrayIndex(link->index);
00154     SlObject(link, GetRouteLinkDescription());
00155   }
00156 }
00157 
00159 static void Load_RTLN()
00160 {
00161   int index;
00162 
00163   while ((index = SlIterateArray()) != -1) {
00164     RouteLink *link = new (index) RouteLink();
00165     SlObject(link, GetRouteLinkDescription());
00166   }
00167 }
00168 
00170 static void Ptrs_RTLN()
00171 {
00172   RouteLink *link;
00173 
00174   FOR_ALL_ROUTELINKS(link) {
00175     SlObject(link, GetRouteLinkDescription());
00176   }
00177 }
00178 
00179 extern const ChunkHandler _routelink_chunk_handlers[] = {
00180   { 'RTLN', Save_RTLN, Load_RTLN, Ptrs_RTLN, NULL, CH_ARRAY | CH_LAST},
00181 };

Generated on Mon May 9 05:18:59 2011 for OpenTTD by  doxygen 1.6.1