station_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 "../station_base.h"
00014 #include "../waypoint_base.h"
00015 #include "../roadstop_base.h"
00016 #include "../vehicle_base.h"
00017 #include "../newgrf_station.h"
00018 #include "../newgrf.h"
00019 
00020 #include "saveload.h"
00021 #include "table/strings.h"
00022 
00027 static void UpdateWaypointOrder(Order *o)
00028 {
00029   if (!o->IsType(OT_GOTO_STATION)) return;
00030 
00031   const Station *st = Station::Get(o->GetDestination());
00032   if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) return;
00033 
00034   o->MakeGoToWaypoint(o->GetDestination());
00035 }
00036 
00041 void MoveBuoysToWaypoints()
00042 {
00043   /* Buoy orders become waypoint orders */
00044   OrderList *ol;
00045   FOR_ALL_ORDER_LISTS(ol) {
00046     VehicleType vt = ol->GetFirstSharedVehicle()->type;
00047     if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
00048 
00049     for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
00050   }
00051 
00052   Vehicle *v;
00053   FOR_ALL_VEHICLES(v) {
00054     VehicleType vt = v->type;
00055     if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
00056 
00057     UpdateWaypointOrder(&v->current_order);
00058   }
00059 
00060   /* Now make the stations waypoints */
00061   Station *st;
00062   FOR_ALL_STATIONS(st) {
00063     if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue;
00064 
00065     StationID index    = st->index;
00066     TileIndex xy       = st->xy;
00067     Town *town         = st->town;
00068     StringID string_id = st->string_id;
00069     char *name         = st->name;
00070     st->name           = NULL;
00071     Date build_date    = st->build_date;
00072     /* TTDPatch could use "buoys with rail station" for rail waypoints */
00073     bool train         = st->train_station.tile != INVALID_TILE;
00074     TileArea train_st  = st->train_station;
00075 
00076     /* Delete the station, so we can make it a real waypoint. */
00077     delete st;
00078 
00079     /* Stations and waypoints are in the same pool, so if a station
00080      * is deleted there must be place for a Waypoint. */
00081     assert(Waypoint::CanAllocateItem());
00082     Waypoint *wp   = new (index) Waypoint(xy);
00083     wp->town       = town;
00084     wp->string_id  = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY;
00085     wp->name       = name;
00086     wp->delete_ctr = 0; // Just reset delete counter for once.
00087     wp->build_date = build_date;
00088     wp->owner      = train ? GetTileOwner(xy) : OWNER_NONE;
00089 
00090     if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY;
00091 
00092     if (train) {
00093       /* When we make a rail waypoint of the station, convert the map as well. */
00094       TILE_AREA_LOOP(t, train_st) {
00095         if (!IsTileType(t, MP_STATION) || GetStationIndex(t) != index) continue;
00096 
00097         SB(_m[t].m6, 3, 3, STATION_WAYPOINT);
00098         wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
00099       }
00100 
00101       wp->train_station = train_st;
00102       wp->facilities |= FACIL_TRAIN;
00103     } else if (IsBuoyTile(xy) && GetStationIndex(xy) == index) {
00104       wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE);
00105       wp->facilities |= FACIL_DOCK;
00106     }
00107   }
00108 }
00109 
00110 void AfterLoadStations()
00111 {
00112   /* Update the speclists of all stations to point to the currently loaded custom stations. */
00113   BaseStation *st;
00114   FOR_ALL_BASE_STATIONS(st) {
00115     for (uint i = 0; i < st->num_specs; i++) {
00116       if (st->speclist[i].grfid == 0) continue;
00117 
00118       st->speclist[i].spec = StationClass::GetByGrf(st->speclist[i].grfid, st->speclist[i].localidx, NULL);
00119     }
00120 
00121     if (Station::IsExpected(st)) {
00122       Station *sta = Station::From(st);
00123       for (const RoadStop *rs = sta->bus_stops; rs != NULL; rs = rs->next) sta->bus_station.Add(rs->xy);
00124       for (const RoadStop *rs = sta->truck_stops; rs != NULL; rs = rs->next) sta->truck_station.Add(rs->xy);
00125     }
00126 
00127     StationUpdateAnimTriggers(st);
00128   }
00129 }
00130 
00134 void AfterLoadRoadStops()
00135 {
00136   /* First construct the drive through entries */
00137   RoadStop *rs;
00138   FOR_ALL_ROADSTOPS(rs) {
00139     if (IsDriveThroughStopTile(rs->xy)) rs->MakeDriveThrough();
00140   }
00141   /* And then rebuild the data in those entries */
00142   FOR_ALL_ROADSTOPS(rs) {
00143     if (!HasBit(rs->status, RoadStop::RSSFB_BASE_ENTRY)) continue;
00144 
00145     rs->GetEntry(DIAGDIR_NE)->Rebuild(rs);
00146     rs->GetEntry(DIAGDIR_NW)->Rebuild(rs);
00147   }
00148 }
00149 
00150 static const SaveLoad _roadstop_desc[] = {
00151   SLE_VAR(RoadStop, xy,           SLE_UINT32),
00152   SLE_CONDNULL(1, 0, 44),
00153   SLE_VAR(RoadStop, status,       SLE_UINT8),
00154   /* Index was saved in some versions, but this is not needed */
00155   SLE_CONDNULL(4, 0, 8),
00156   SLE_CONDNULL(2, 0, 44),
00157   SLE_CONDNULL(1, 0, 25),
00158 
00159   SLE_REF(RoadStop, next,         REF_ROADSTOPS),
00160   SLE_CONDNULL(2, 0, 44),
00161 
00162   SLE_CONDNULL(4, 0, 24),
00163   SLE_CONDNULL(1, 25, 25),
00164 
00165   SLE_END()
00166 };
00167 
00168 static const SaveLoad _old_station_desc[] = {
00169   SLE_CONDVAR(Station, xy,                         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00170   SLE_CONDVAR(Station, xy,                         SLE_UINT32,                  6, SL_MAX_VERSION),
00171   SLE_CONDNULL(4, 0, 5),  
00172   SLE_CONDVAR(Station, train_station.tile,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00173   SLE_CONDVAR(Station, train_station.tile,         SLE_UINT32,                  6, SL_MAX_VERSION),
00174   SLE_CONDVAR(Station, airport.tile,               SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00175   SLE_CONDVAR(Station, airport.tile,               SLE_UINT32,                  6, SL_MAX_VERSION),
00176   SLE_CONDVAR(Station, dock_tile,                  SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00177   SLE_CONDVAR(Station, dock_tile,                  SLE_UINT32,                  6, SL_MAX_VERSION),
00178       SLE_REF(Station, town,                       REF_TOWN),
00179       SLE_VAR(Station, train_station.w,            SLE_FILE_U8 | SLE_VAR_U16),
00180   SLE_CONDVAR(Station, train_station.h,            SLE_FILE_U8 | SLE_VAR_U16,   2, SL_MAX_VERSION),
00181 
00182   SLE_CONDNULL(1, 0, 3),  
00183 
00184       SLE_VAR(Station, string_id,                  SLE_STRINGID),
00185   SLE_CONDSTR(Station, name,                       SLE_STR | SLF_ALLOW_CONTROL, 0, 84, SL_MAX_VERSION),
00186   SLE_CONDVAR(Station, indtype,                    SLE_UINT8,                 103, SL_MAX_VERSION),
00187   SLE_CONDVAR(Station, had_vehicle_of_type,        SLE_FILE_U16 | SLE_VAR_U8,   0, 121),
00188   SLE_CONDVAR(Station, had_vehicle_of_type,        SLE_UINT8,                 122, SL_MAX_VERSION),
00189 
00190       SLE_VAR(Station, time_since_load,            SLE_UINT8),
00191       SLE_VAR(Station, time_since_unload,          SLE_UINT8),
00192       SLE_VAR(Station, delete_ctr,                 SLE_UINT8),
00193       SLE_VAR(Station, owner,                      SLE_UINT8),
00194       SLE_VAR(Station, facilities,                 SLE_UINT8),
00195       SLE_VAR(Station, airport.type,               SLE_UINT8),
00196 
00197   SLE_CONDNULL(2, 0, 5),  
00198   SLE_CONDNULL(1, 0, 4),  
00199 
00200   SLE_CONDVAR(Station, airport.flags,              SLE_VAR_U64 | SLE_FILE_U16,  0,  2),
00201   SLE_CONDVAR(Station, airport.flags,              SLE_VAR_U64 | SLE_FILE_U32,  3, 45),
00202   SLE_CONDVAR(Station, airport.flags,              SLE_UINT64,                 46, SL_MAX_VERSION),
00203 
00204   SLE_CONDNULL(2, 0, 25), 
00205   SLE_CONDVAR(Station, last_vehicle_type,          SLE_UINT8,                  26, SL_MAX_VERSION),
00206 
00207   SLE_CONDNULL(2, 3, 25), 
00208   SLE_CONDVAR(Station, build_date,                 SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
00209   SLE_CONDVAR(Station, build_date,                 SLE_INT32,                  31, SL_MAX_VERSION),
00210 
00211   SLE_CONDREF(Station, bus_stops,                  REF_ROADSTOPS,               6, SL_MAX_VERSION),
00212   SLE_CONDREF(Station, truck_stops,                REF_ROADSTOPS,               6, SL_MAX_VERSION),
00213 
00214   /* Used by newstations for graphic variations */
00215   SLE_CONDVAR(Station, random_bits,                SLE_UINT16,                 27, SL_MAX_VERSION),
00216   SLE_CONDVAR(Station, waiting_triggers,           SLE_UINT8,                  27, SL_MAX_VERSION),
00217   SLE_CONDVAR(Station, num_specs,                  SLE_UINT8,                  27, SL_MAX_VERSION),
00218 
00219   SLE_CONDLST(Station, loading_vehicles,           REF_VEHICLE,                57, SL_MAX_VERSION),
00220 
00221   /* reserve extra space in savegame here. (currently 32 bytes) */
00222   SLE_CONDNULL(32, 2, SL_MAX_VERSION),
00223 
00224   SLE_END()
00225 };
00226 
00227 static uint16 _waiting_acceptance;
00228 static uint16 _num_links;
00229 static uint32 _num_flows;
00230 static uint16 _cargo_source;
00231 static uint32 _cargo_source_xy;
00232 static uint8  _cargo_days;
00233 static Money  _cargo_feeder_share;
00234 
00235 static const SaveLoad _station_speclist_desc[] = {
00236   SLE_CONDVAR(StationSpecList, grfid,    SLE_UINT32, 27, SL_MAX_VERSION),
00237   SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8,  27, SL_MAX_VERSION),
00238 
00239   SLE_END()
00240 };
00241 
00242 static StationID _station_id;
00243 
00249 const SaveLoad *GetLinkStatDesc()
00250 {
00251   static const SaveLoad linkstat_desc[] = {
00252     SLEG_VAR(          _station_id, SLE_UINT16),
00253      SLE_VAR(LinkStat, length,      SLE_UINT32),
00254      SLE_VAR(LinkStat, capacity,    SLE_UINT32),
00255      SLE_VAR(LinkStat, timeout,     SLE_UINT32),
00256      SLE_VAR(LinkStat, usage,       SLE_UINT32),
00257      SLE_END()
00258   };
00259 
00260   return linkstat_desc;
00261 }
00262 
00263 std::list<CargoPacket *> _packets;
00264 uint32 _num_dests;
00265 
00266 struct FlowSaveLoad {
00267   FlowSaveLoad() : via(0), share(0) {}
00268   StationID source;
00269   StationID via;
00270   uint32 share;
00271 };
00272 
00273 static const SaveLoad _flow_desc[] = {
00274   SLE_CONDVAR(FlowSaveLoad, source,             SLE_UINT16,         SL_FLOWMAP, SL_MAX_VERSION),
00275   SLE_CONDVAR(FlowSaveLoad, via,                SLE_UINT16,         SL_FLOWMAP, SL_MAX_VERSION),
00276   SLE_CONDVAR(FlowSaveLoad, share,              SLE_UINT32,         SL_FLOWMAP, SL_MAX_VERSION),
00277   SLE_END()
00278 };
00279 
00285 const SaveLoad *GetGoodsDesc()
00286 {
00287   static const SaveLoad goods_desc[] = {
00288     SLEG_CONDVAR(            _waiting_acceptance, SLE_UINT16,                  0, 67),
00289      SLE_CONDVAR(GoodsEntry, acceptance_pickup,   SLE_UINT8,                  68, SL_MAX_VERSION),
00290     SLE_CONDNULL(2,                                                           51, 67),
00291          SLE_VAR(GoodsEntry, days_since_pickup,   SLE_UINT8),
00292          SLE_VAR(GoodsEntry, rating,              SLE_UINT8),
00293     SLEG_CONDVAR(            _cargo_source,       SLE_FILE_U8 | SLE_VAR_U16,   0, 6),
00294     SLEG_CONDVAR(            _cargo_source,       SLE_UINT16,                  7, 67),
00295     SLEG_CONDVAR(            _cargo_source_xy,    SLE_UINT32,                 44, 67),
00296     SLEG_CONDVAR(            _cargo_days,         SLE_UINT8,                   0, 67),
00297          SLE_VAR(GoodsEntry, last_speed,          SLE_UINT8),
00298          SLE_VAR(GoodsEntry, last_age,            SLE_UINT8),
00299     SLEG_CONDVAR(            _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64),
00300     SLEG_CONDVAR(            _cargo_feeder_share, SLE_INT64,                  65, 67),
00301      SLE_CONDVAR(GoodsEntry, amount_fract,        SLE_UINT8,                 150, SL_MAX_VERSION),
00302     SLEG_CONDLST(            _packets,            REF_CARGO_PACKET,           68, SL_CARGOMAP - 1),
00303     SLEG_CONDVAR(            _num_dests,          SLE_UINT32,        SL_CARGOMAP, SL_MAX_VERSION),
00304      SLE_CONDVAR(GoodsEntry, supply,              SLE_UINT32,      SL_CAPACITIES, SL_MAX_VERSION),
00305      SLE_CONDVAR(GoodsEntry, supply_new,          SLE_UINT32,      SL_CAPACITIES, SL_MAX_VERSION),
00306     SLEG_CONDVAR(            _num_links,          SLE_UINT16,      SL_CAPACITIES, SL_MAX_VERSION),
00307     SLEG_CONDVAR(            _num_flows,          SLE_UINT32,         SL_FLOWMAP, SL_MAX_VERSION),
00308      SLE_CONDVAR(GoodsEntry, last_component,      SLE_UINT16,      SL_COMPONENTS, SL_MAX_VERSION),
00309      SLE_CONDVAR(GoodsEntry, max_waiting_cargo,   SLE_UINT32,      SL_EXT_RATING, SL_MAX_VERSION),
00310     SLE_END()
00311   };
00312 
00313   return goods_desc;
00314 }
00315 
00316 typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair;
00317 
00318 static const SaveLoad _cargo_list_desc[] = {
00319   SLE_VAR(StationCargoPair, first,  SLE_UINT16),
00320   SLE_LST(StationCargoPair, second, REF_CARGO_PACKET),
00321   SLE_END()
00322 };
00323 
00329 static void SwapPackets(GoodsEntry *ge)
00330 {
00331   StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets());
00332 
00333   if (_packets.empty()) {
00334     std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(INVALID_STATION));
00335     if (it == ge_packets.end()) {
00336       return;
00337     } else {
00338       it->second.swap(_packets);
00339     }
00340   } else {
00341     assert(ge_packets[INVALID_STATION].empty());
00342     ge_packets[INVALID_STATION].swap(_packets);
00343   }
00344 }
00345 
00346 static void Load_STNS()
00347 {
00348   int index;
00349   while ((index = SlIterateArray()) != -1) {
00350     Station *st = new (index) Station();
00351 
00352     SlObject(st, _old_station_desc);
00353 
00354     _waiting_acceptance = 0;
00355 
00356     uint num_cargo = IsSavegameVersionBefore(55) ? 12 : NUM_CARGO;
00357     for (CargoID i = 0; i < num_cargo; i++) {
00358       GoodsEntry *ge = &st->goods[i];
00359       SlObject(ge, GetGoodsDesc());
00360       SwapPackets(ge);
00361       if (IsSavegameVersionBefore(68)) {
00362         SB(ge->acceptance_pickup, GoodsEntry::GES_ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
00363         if (GB(_waiting_acceptance, 0, 12) != 0) {
00364           /* In old versions, enroute_from used 0xFF as INVALID_STATION */
00365           StationID source = (IsSavegameVersionBefore(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
00366 
00367           /* Make sure we can allocate the CargoPacket. This is safe
00368            * as there can only be ~64k stations and 32 cargoes in these
00369            * savegame versions. As the CargoPacketPool has more than
00370            * 16 million entries; it fits by an order of magnitude. */
00371           assert(CargoPacket::CanAllocateItem());
00372 
00373           /* Don't construct the packet with station here, because that'll fail with old savegames */
00374           CargoPacket *cp = new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_days, source, _cargo_source_xy, _cargo_source_xy, _cargo_feeder_share);
00375           ge->cargo.Append(INVALID_STATION, cp);
00376           SB(ge->acceptance_pickup, GoodsEntry::GES_PICKUP, 1, 1);
00377         }
00378       }
00379     }
00380 
00381     if (st->num_specs != 0) {
00382       /* Allocate speclist memory when loading a game */
00383       st->speclist = CallocT<StationSpecList>(st->num_specs);
00384       for (uint i = 0; i < st->num_specs; i++) {
00385         SlObject(&st->speclist[i], _station_speclist_desc);
00386       }
00387     }
00388   }
00389 }
00390 
00391 static void Ptrs_STNS()
00392 {
00393   /* Don't run when savegame version is higher than or equal to 123. */
00394   if (!IsSavegameVersionBefore(123)) return;
00395 
00396   Station *st;
00397   FOR_ALL_STATIONS(st) {
00398     if (!IsSavegameVersionBefore(68)) {
00399       for (CargoID i = 0; i < NUM_CARGO; i++) {
00400         GoodsEntry *ge = &st->goods[i];
00401         SwapPackets(ge);
00402         SlObject(ge, GetGoodsDesc());
00403         SwapPackets(ge);
00404       }
00405     }
00406     SlObject(st, _old_station_desc);
00407   }
00408 }
00409 
00410 
00411 static const SaveLoad _base_station_desc[] = {
00412         SLE_VAR(BaseStation, xy,                     SLE_UINT32),
00413         SLE_REF(BaseStation, town,                   REF_TOWN),
00414         SLE_VAR(BaseStation, string_id,              SLE_STRINGID),
00415         SLE_STR(BaseStation, name,                   SLE_STR | SLF_ALLOW_CONTROL, 0),
00416         SLE_VAR(BaseStation, delete_ctr,             SLE_UINT8),
00417         SLE_VAR(BaseStation, owner,                  SLE_UINT8),
00418         SLE_VAR(BaseStation, facilities,             SLE_UINT8),
00419         SLE_VAR(BaseStation, build_date,             SLE_INT32),
00420 
00421   /* Used by newstations for graphic variations */
00422         SLE_VAR(BaseStation, random_bits,            SLE_UINT16),
00423         SLE_VAR(BaseStation, waiting_triggers,       SLE_UINT8),
00424         SLE_VAR(BaseStation, num_specs,              SLE_UINT8),
00425 
00426         SLE_END()
00427 };
00428 
00429 static OldPersistentStorage _old_st_persistent_storage;
00430 
00431 static const SaveLoad _station_desc[] = {
00432   SLE_WRITEBYTE(Station, facilities,                 FACIL_NONE),
00433   SLE_ST_INCLUDE(),
00434 
00435         SLE_VAR(Station, train_station.tile,         SLE_UINT32),
00436         SLE_VAR(Station, train_station.w,            SLE_FILE_U8 | SLE_VAR_U16),
00437         SLE_VAR(Station, train_station.h,            SLE_FILE_U8 | SLE_VAR_U16),
00438 
00439         SLE_REF(Station, bus_stops,                  REF_ROADSTOPS),
00440         SLE_REF(Station, truck_stops,                REF_ROADSTOPS),
00441         SLE_VAR(Station, dock_tile,                  SLE_UINT32),
00442         SLE_VAR(Station, airport.tile,               SLE_UINT32),
00443     SLE_CONDVAR(Station, airport.w,                  SLE_FILE_U8 | SLE_VAR_U16, 140, SL_MAX_VERSION),
00444     SLE_CONDVAR(Station, airport.h,                  SLE_FILE_U8 | SLE_VAR_U16, 140, SL_MAX_VERSION),
00445         SLE_VAR(Station, airport.type,               SLE_UINT8),
00446     SLE_CONDVAR(Station, airport.layout,             SLE_UINT8,                 145, SL_MAX_VERSION),
00447         SLE_VAR(Station, airport.flags,              SLE_UINT64),
00448     SLE_CONDVAR(Station, airport.rotation,           SLE_UINT8,                 145, SL_MAX_VERSION),
00449    SLEG_CONDARR(_old_st_persistent_storage.storage,  SLE_UINT32, 16,            145, 160),
00450     SLE_CONDREF(Station, airport.psa,                REF_STORAGE,               161, SL_MAX_VERSION),
00451 
00452         SLE_VAR(Station, indtype,                    SLE_UINT8),
00453 
00454         SLE_VAR(Station, time_since_load,            SLE_UINT8),
00455         SLE_VAR(Station, time_since_unload,          SLE_UINT8),
00456         SLE_VAR(Station, last_vehicle_type,          SLE_UINT8),
00457         SLE_VAR(Station, had_vehicle_of_type,        SLE_UINT8),
00458         SLE_LST(Station, loading_vehicles,           REF_VEHICLE),
00459     SLE_CONDVAR(Station, always_accepted,            SLE_UINT32, 127, SL_MAX_VERSION),
00460 
00461         SLE_END()
00462 };
00463 
00464 static const SaveLoad _waypoint_desc[] = {
00465   SLE_WRITEBYTE(Waypoint, facilities,                FACIL_WAYPOINT),
00466   SLE_ST_INCLUDE(),
00467 
00468         SLE_VAR(Waypoint, town_cn,                   SLE_UINT16),
00469 
00470     SLE_CONDVAR(Waypoint, train_station.tile,        SLE_UINT32,                  124, SL_MAX_VERSION),
00471     SLE_CONDVAR(Waypoint, train_station.w,           SLE_FILE_U8 | SLE_VAR_U16,   124, SL_MAX_VERSION),
00472     SLE_CONDVAR(Waypoint, train_station.h,           SLE_FILE_U8 | SLE_VAR_U16,   124, SL_MAX_VERSION),
00473 
00474         SLE_END()
00475 };
00476 
00481 const SaveLoad *GetBaseStationDescription()
00482 {
00483   return _base_station_desc;
00484 }
00485 
00486 static void RealSave_STNN(BaseStation *bst)
00487 {
00488   bool waypoint = (bst->facilities & FACIL_WAYPOINT) != 0;
00489   SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
00490 
00491   if (!waypoint) {
00492     Station *st = Station::From(bst);
00493     for (CargoID i = 0; i < NUM_CARGO; i++) {
00494       _num_dests = (uint32)st->goods[i].cargo.Packets()->MapSize();
00495       _num_links = (uint16)st->goods[i].link_stats.size();
00496       _num_flows = 0;
00497       for (FlowStatMap::const_iterator it(st->goods[i].flows.begin()); it != st->goods[i].flows.end(); ++it) {
00498         _num_flows += (uint32)it->second.GetShares()->size();
00499       }
00500       SlObject(&st->goods[i], GetGoodsDesc());
00501       for (LinkStatMap::const_iterator it(st->goods[i].link_stats.begin()); it != st->goods[i].link_stats.end(); ++it) {
00502         _station_id = it->first;
00503         LinkStat ls(it->second); // make a copy to avoid constness problems
00504         SlObject(&ls, GetLinkStatDesc());
00505       }
00506       for (FlowStatMap::const_iterator outer_it(st->goods[i].flows.begin()); outer_it != st->goods[i].flows.end(); ++outer_it) {
00507         const FlowStat::SharesMap *shares = outer_it->second.GetShares();
00508         uint32 sum_shares = 0;
00509         FlowSaveLoad flow;
00510         flow.source = outer_it->first;
00511         for (FlowStat::SharesMap::const_iterator inner_it(shares->begin()); inner_it != shares->end(); ++inner_it) {
00512           flow.via = inner_it->second;
00513           flow.share = inner_it->first - sum_shares;
00514           sum_shares = inner_it->first;
00515           assert(flow.share > 0);
00516           SlObject(&flow, _flow_desc);
00517         }
00518       }
00519       for (StationCargoPacketMap::ConstMapIterator it(st->goods[i].cargo.Packets()->begin()); it != st->goods[i].cargo.Packets()->end(); ++it) {
00520         SlObject(const_cast<StationCargoPacketMap::value_type *>(&(*it)), _cargo_list_desc);
00521       }
00522     }
00523   }
00524 
00525   for (uint i = 0; i < bst->num_specs; i++) {
00526     SlObject(&bst->speclist[i], _station_speclist_desc);
00527   }
00528 }
00529 
00530 static void Save_STNN()
00531 {
00532   BaseStation *st;
00533   /* Write the stations */
00534   FOR_ALL_BASE_STATIONS(st) {
00535     SlSetArrayIndex(st->index);
00536     SlAutolength((AutolengthProc*)RealSave_STNN, st);
00537   }
00538 }
00539 
00540 static void Load_STNN()
00541 {
00542   int index;
00543 
00544   while ((index = SlIterateArray()) != -1) {
00545     bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0;
00546 
00547     BaseStation *bst = waypoint ? (BaseStation *)new (index) Waypoint() : new (index) Station();
00548     SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
00549 
00550     if (!waypoint) {
00551       Station *st = Station::From(bst);
00552 
00553       /* Before savegame version 161, persistent storages were not stored in a pool. */
00554       if (IsSavegameVersionBefore(161) && !IsSavegameVersionBefore(145) && st->facilities & FACIL_AIRPORT) {
00555         /* Store the old persistent storage. The GRFID will be added later. */
00556         assert(PersistentStorage::CanAllocateItem());
00557         st->airport.psa = new PersistentStorage(0);
00558         memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(st->airport.psa->storage));
00559       }
00560 
00561       for (CargoID i = 0; i < NUM_CARGO; i++) {
00562         SlObject(&st->goods[i], GetGoodsDesc());
00563         LinkStat ls(1);
00564         for (uint16 j = 0; j < _num_links; ++j) {
00565           SlObject(&ls, GetLinkStatDesc());
00566           assert(ls.IsValid());
00567           st->goods[i].link_stats.insert(std::make_pair(_station_id, ls));
00568         }
00569         FlowSaveLoad flow;
00570         FlowStat *fs = NULL;
00571         StationID prev_source = INVALID_STATION;
00572         for (uint32 j = 0; j < _num_flows; ++j) {
00573           SlObject(&flow, _flow_desc);
00574           if (fs == NULL || prev_source != flow.source) {
00575             fs = &(st->goods[i].flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share))).first->second);
00576           } else {
00577             fs->AddShare(flow.via, flow.share);
00578           }
00579           prev_source = flow.source;
00580         }
00581         if (IsSavegameVersionBefore(SL_CARGOMAP -1)) {
00582           SwapPackets(&st->goods[i]);
00583         } else {
00584           StationCargoPair pair;
00585           for (uint j = 0; j < _num_dests; ++j) {
00586             SlObject(&pair, _cargo_list_desc);
00587             const_cast<StationCargoPacketMap &>(*(st->goods[i].cargo.Packets()))[pair.first].swap(pair.second);
00588             assert(pair.second.empty());
00589           }
00590         }
00591       }
00592     }
00593 
00594     if (bst->num_specs != 0) {
00595       /* Allocate speclist memory when loading a game */
00596       bst->speclist = CallocT<StationSpecList>(bst->num_specs);
00597       for (uint i = 0; i < bst->num_specs; i++) {
00598         SlObject(&bst->speclist[i], _station_speclist_desc);
00599       }
00600     }
00601   }
00602 }
00603 
00604 static void Ptrs_STNN()
00605 {
00606   /* Don't run when savegame version lower than 123. */
00607   if (IsSavegameVersionBefore(123)) return;
00608 
00609   Station *st;
00610   FOR_ALL_STATIONS(st) {
00611     for (CargoID i = 0; i < NUM_CARGO; i++) {
00612       GoodsEntry *ge = &st->goods[i];
00613       if (IsSavegameVersionBefore(SL_CARGOMAP)) {
00614         SwapPackets(ge);
00615         SlObject(ge, GetGoodsDesc());
00616         SwapPackets(ge);
00617       } else {
00618         SlObject(ge, GetGoodsDesc());
00619         for (StationCargoPacketMap::ConstMapIterator it = ge->cargo.Packets()->begin(); it != ge->cargo.Packets()->end(); ++it) {
00620           SlObject(const_cast<StationCargoPair *>(&(*it)), _cargo_list_desc);
00621         }
00622       }
00623     }
00624     SlObject(st, _station_desc);
00625   }
00626 
00627   Waypoint *wp;
00628   FOR_ALL_WAYPOINTS(wp) {
00629     SlObject(wp, _waypoint_desc);
00630   }
00631 }
00632 
00633 static void Save_ROADSTOP()
00634 {
00635   RoadStop *rs;
00636 
00637   FOR_ALL_ROADSTOPS(rs) {
00638     SlSetArrayIndex(rs->index);
00639     SlObject(rs, _roadstop_desc);
00640   }
00641 }
00642 
00643 static void Load_ROADSTOP()
00644 {
00645   int index;
00646 
00647   while ((index = SlIterateArray()) != -1) {
00648     RoadStop *rs = new (index) RoadStop(INVALID_TILE);
00649 
00650     SlObject(rs, _roadstop_desc);
00651   }
00652 }
00653 
00654 static void Ptrs_ROADSTOP()
00655 {
00656   RoadStop *rs;
00657   FOR_ALL_ROADSTOPS(rs) {
00658     SlObject(rs, _roadstop_desc);
00659   }
00660 }
00661 
00662 extern const ChunkHandler _station_chunk_handlers[] = {
00663   { 'STNS', NULL,          Load_STNS,     Ptrs_STNS,     NULL, CH_ARRAY },
00664   { 'STNN', Save_STNN,     Load_STNN,     Ptrs_STNN,     NULL, CH_ARRAY },
00665   { 'ROAD', Save_ROADSTOP, Load_ROADSTOP, Ptrs_ROADSTOP, NULL, CH_ARRAY | CH_LAST},
00666 };