00001
00002
00003
00004
00005
00006
00007
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
00019 #include "saveload.h"
00020 #include "table/strings.h"
00021
00026 static void UpdateWaypointOrder(Order *o)
00027 {
00028 if (!o->IsType(OT_GOTO_STATION)) return;
00029
00030 const Station *st = Station::Get(o->GetDestination());
00031 if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) return;
00032
00033 o->MakeGoToWaypoint(o->GetDestination());
00034 }
00035
00040 void MoveBuoysToWaypoints()
00041 {
00042
00043 OrderList *ol;
00044 FOR_ALL_ORDER_LISTS(ol) {
00045 VehicleType vt = ol->GetFirstSharedVehicle()->type;
00046 if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
00047
00048 for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
00049 }
00050
00051 Vehicle *v;
00052 FOR_ALL_VEHICLES(v) {
00053 VehicleType vt = v->type;
00054 if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
00055
00056 UpdateWaypointOrder(&v->current_order);
00057 }
00058
00059
00060 Station *st;
00061 FOR_ALL_STATIONS(st) {
00062 if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue;
00063
00064 StationID index = st->index;
00065 TileIndex xy = st->xy;
00066 Town *town = st->town;
00067 StringID string_id = st->string_id;
00068 char *name = st->name;
00069 st->name = NULL;
00070 Date build_date = st->build_date;
00071
00072 bool train = st->train_station.tile != INVALID_TILE;
00073 TileArea train_st = st->train_station;
00074
00075
00076 delete st;
00077
00078
00079
00080 assert(Waypoint::CanAllocateItem());
00081 Waypoint *wp = new (index) Waypoint(xy);
00082 wp->town = town;
00083 wp->string_id = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY;
00084 wp->name = name;
00085 wp->delete_ctr = 0;
00086 wp->build_date = build_date;
00087 wp->owner = train ? GetTileOwner(xy) : OWNER_NONE;
00088
00089 if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY;
00090
00091 if (train) {
00092
00093 TILE_AREA_LOOP(t, train_st) {
00094 if (!IsTileType(t, MP_STATION) || GetStationIndex(t) != index) continue;
00095
00096 SB(_m[t].m6, 3, 3, STATION_WAYPOINT);
00097 wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
00098 }
00099
00100 wp->train_station = train_st;
00101 wp->facilities |= FACIL_TRAIN;
00102 } else if (IsBuoyTile(xy) && GetStationIndex(xy) == index) {
00103 wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE);
00104 wp->facilities |= FACIL_DOCK;
00105 }
00106 }
00107 }
00108
00109 void AfterLoadStations()
00110 {
00111
00112 BaseStation *st;
00113 FOR_ALL_BASE_STATIONS(st) {
00114 for (uint i = 0; i < st->num_specs; i++) {
00115 if (st->speclist[i].grfid == 0) continue;
00116
00117 st->speclist[i].spec = StationClass::GetByGrf(st->speclist[i].grfid, st->speclist[i].localidx, NULL);
00118 }
00119
00120 if (Station::IsExpected(st)) {
00121 Station *sta = Station::From(st);
00122 for (const RoadStop *rs = sta->bus_stops; rs != NULL; rs = rs->next) sta->bus_station.Add(rs->xy);
00123 for (const RoadStop *rs = sta->truck_stops; rs != NULL; rs = rs->next) sta->truck_station.Add(rs->xy);
00124 }
00125
00126 StationUpdateAnimTriggers(st);
00127 }
00128 }
00129
00133 void AfterLoadRoadStops()
00134 {
00135
00136 RoadStop *rs;
00137 FOR_ALL_ROADSTOPS(rs) {
00138 if (IsDriveThroughStopTile(rs->xy)) rs->MakeDriveThrough();
00139 }
00140
00141 FOR_ALL_ROADSTOPS(rs) {
00142 if (!HasBit(rs->status, RoadStop::RSSFB_BASE_ENTRY)) continue;
00143
00144 rs->GetEntry(DIAGDIR_NE)->Rebuild(rs);
00145 rs->GetEntry(DIAGDIR_NW)->Rebuild(rs);
00146 }
00147 }
00148
00149 static const SaveLoad _roadstop_desc[] = {
00150 SLE_VAR(RoadStop, xy, SLE_UINT32),
00151 SLE_CONDNULL(1, 0, 44),
00152 SLE_VAR(RoadStop, status, SLE_UINT8),
00153
00154 SLE_CONDNULL(4, 0, 8),
00155 SLE_CONDNULL(2, 0, 44),
00156 SLE_CONDNULL(1, 0, 25),
00157
00158 SLE_REF(RoadStop, next, REF_ROADSTOPS),
00159 SLE_CONDNULL(2, 0, 44),
00160
00161 SLE_CONDNULL(4, 0, 24),
00162 SLE_CONDNULL(1, 25, 25),
00163
00164 SLE_END()
00165 };
00166
00167 static const SaveLoad _old_station_desc[] = {
00168 SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
00169 SLE_CONDVAR(Station, xy, SLE_UINT32, 6, SL_MAX_VERSION),
00170 SLE_CONDNULL(4, 0, 5),
00171 SLE_CONDVAR(Station, train_station.tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
00172 SLE_CONDVAR(Station, train_station.tile, SLE_UINT32, 6, SL_MAX_VERSION),
00173 SLE_CONDVAR(Station, airport.tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
00174 SLE_CONDVAR(Station, airport.tile, SLE_UINT32, 6, SL_MAX_VERSION),
00175 SLE_CONDVAR(Station, dock_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
00176 SLE_CONDVAR(Station, dock_tile, SLE_UINT32, 6, SL_MAX_VERSION),
00177 SLE_REF(Station, town, REF_TOWN),
00178 SLE_VAR(Station, train_station.w, SLE_FILE_U8 | SLE_VAR_U16),
00179 SLE_CONDVAR(Station, train_station.h, SLE_FILE_U8 | SLE_VAR_U16, 2, SL_MAX_VERSION),
00180
00181 SLE_CONDNULL(1, 0, 3),
00182
00183 SLE_VAR(Station, string_id, SLE_STRINGID),
00184 SLE_CONDSTR(Station, name, SLE_STR, 0, 84, SL_MAX_VERSION),
00185 SLE_CONDVAR(Station, indtype, SLE_UINT8, 103, SL_MAX_VERSION),
00186 SLE_CONDVAR(Station, had_vehicle_of_type, SLE_FILE_U16 | SLE_VAR_U8, 0, 121),
00187 SLE_CONDVAR(Station, had_vehicle_of_type, SLE_UINT8, 122, SL_MAX_VERSION),
00188
00189 SLE_VAR(Station, time_since_load, SLE_UINT8),
00190 SLE_VAR(Station, time_since_unload, SLE_UINT8),
00191 SLE_VAR(Station, delete_ctr, SLE_UINT8),
00192 SLE_VAR(Station, owner, SLE_UINT8),
00193 SLE_VAR(Station, facilities, SLE_UINT8),
00194 SLE_VAR(Station, airport.type, SLE_UINT8),
00195
00196 SLE_CONDNULL(2, 0, 5),
00197 SLE_CONDNULL(1, 0, 4),
00198
00199 SLE_CONDVAR(Station, airport.flags, SLE_VAR_U64 | SLE_FILE_U16, 0, 2),
00200 SLE_CONDVAR(Station, airport.flags, SLE_VAR_U64 | SLE_FILE_U32, 3, 45),
00201 SLE_CONDVAR(Station, airport.flags, SLE_UINT64, 46, SL_MAX_VERSION),
00202
00203 SLE_CONDNULL(2, 0, 25),
00204 SLE_CONDVAR(Station, last_vehicle_type, SLE_UINT8, 26, SL_MAX_VERSION),
00205
00206 SLE_CONDNULL(2, 3, 25),
00207 SLE_CONDVAR(Station, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
00208 SLE_CONDVAR(Station, build_date, SLE_INT32, 31, SL_MAX_VERSION),
00209
00210 SLE_CONDREF(Station, bus_stops, REF_ROADSTOPS, 6, SL_MAX_VERSION),
00211 SLE_CONDREF(Station, truck_stops, REF_ROADSTOPS, 6, SL_MAX_VERSION),
00212
00213
00214 SLE_CONDVAR(Station, random_bits, SLE_UINT16, 27, SL_MAX_VERSION),
00215 SLE_CONDVAR(Station, waiting_triggers, SLE_UINT8, 27, SL_MAX_VERSION),
00216 SLE_CONDVAR(Station, num_specs, SLE_UINT8, 27, SL_MAX_VERSION),
00217
00218 SLE_CONDLST(Station, loading_vehicles, REF_VEHICLE, 57, SL_MAX_VERSION),
00219
00220
00221 SLE_CONDNULL(32, 2, SL_MAX_VERSION),
00222
00223 SLE_END()
00224 };
00225
00226 static uint16 _waiting_acceptance;
00227 static uint16 _cargo_source;
00228 static uint32 _cargo_source_xy;
00229 static uint16 _cargo_days;
00230 static Money _cargo_feeder_share;
00231
00232 static const SaveLoad _station_speclist_desc[] = {
00233 SLE_CONDVAR(StationSpecList, grfid, SLE_UINT32, 27, SL_MAX_VERSION),
00234 SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8, 27, SL_MAX_VERSION),
00235
00236 SLE_END()
00237 };
00238
00244 const SaveLoad *GetGoodsDesc()
00245 {
00246 static const SaveLoad goods_desc[] = {
00247 SLEG_CONDVAR( _waiting_acceptance, SLE_UINT16, 0, 67),
00248 SLE_CONDVAR(GoodsEntry, acceptance_pickup, SLE_UINT8, 68, SL_MAX_VERSION),
00249 SLE_CONDNULL(2, 51, 67),
00250 SLE_VAR(GoodsEntry, days_since_pickup, SLE_UINT8),
00251 SLE_VAR(GoodsEntry, rating, SLE_UINT8),
00252 SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
00253 SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67),
00254 SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67),
00255 SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67),
00256 SLE_VAR(GoodsEntry, last_speed, SLE_UINT8),
00257 SLE_VAR(GoodsEntry, last_age, SLE_UINT8),
00258 SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64),
00259 SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
00260 SLE_CONDVAR(GoodsEntry, amount_fract, SLE_UINT8, 150, SL_MAX_VERSION),
00261 SLE_CONDLST(GoodsEntry, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
00262
00263 SLE_END()
00264 };
00265
00266 return goods_desc;
00267 }
00268
00269
00270 static void Load_STNS()
00271 {
00272 int index;
00273 while ((index = SlIterateArray()) != -1) {
00274 Station *st = new (index) Station();
00275
00276 SlObject(st, _old_station_desc);
00277
00278 _waiting_acceptance = 0;
00279
00280 uint num_cargo = IsSavegameVersionBefore(55) ? 12 : NUM_CARGO;
00281 for (CargoID i = 0; i < num_cargo; i++) {
00282 GoodsEntry *ge = &st->goods[i];
00283 SlObject(ge, GetGoodsDesc());
00284 if (IsSavegameVersionBefore(68)) {
00285 SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
00286 if (GB(_waiting_acceptance, 0, 12) != 0) {
00287
00288 StationID source = (IsSavegameVersionBefore(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
00289
00290
00291
00292
00293
00294 assert(CargoPacket::CanAllocateItem());
00295 ge->cargo.Append(new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_days, source, _cargo_source_xy, _cargo_source_xy, _cargo_feeder_share));
00296 SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, 1);
00297 }
00298 }
00299 }
00300
00301 if (st->num_specs != 0) {
00302
00303 st->speclist = CallocT<StationSpecList>(st->num_specs);
00304 for (uint i = 0; i < st->num_specs; i++) {
00305 SlObject(&st->speclist[i], _station_speclist_desc);
00306 }
00307 }
00308 }
00309 }
00310
00311 static void Ptrs_STNS()
00312 {
00313
00314 if (!IsSavegameVersionBefore(123)) return;
00315
00316 Station *st;
00317 FOR_ALL_STATIONS(st) {
00318 if (!IsSavegameVersionBefore(68)) {
00319 for (CargoID i = 0; i < NUM_CARGO; i++) {
00320 GoodsEntry *ge = &st->goods[i];
00321 SlObject(ge, GetGoodsDesc());
00322 }
00323 }
00324 SlObject(st, _old_station_desc);
00325 }
00326 }
00327
00328
00329 static const SaveLoad _base_station_desc[] = {
00330 SLE_VAR(BaseStation, xy, SLE_UINT32),
00331 SLE_REF(BaseStation, town, REF_TOWN),
00332 SLE_VAR(BaseStation, string_id, SLE_STRINGID),
00333 SLE_STR(BaseStation, name, SLE_STR, 0),
00334 SLE_VAR(BaseStation, delete_ctr, SLE_UINT8),
00335 SLE_VAR(BaseStation, owner, SLE_UINT8),
00336 SLE_VAR(BaseStation, facilities, SLE_UINT8),
00337 SLE_VAR(BaseStation, build_date, SLE_INT32),
00338
00339
00340 SLE_VAR(BaseStation, random_bits, SLE_UINT16),
00341 SLE_VAR(BaseStation, waiting_triggers, SLE_UINT8),
00342 SLE_VAR(BaseStation, num_specs, SLE_UINT8),
00343
00344 SLE_END()
00345 };
00346
00347 static const SaveLoad _station_desc[] = {
00348 SLE_WRITEBYTE(Station, facilities, FACIL_NONE),
00349 SLE_ST_INCLUDE(),
00350
00351 SLE_VAR(Station, train_station.tile, SLE_UINT32),
00352 SLE_VAR(Station, train_station.w, SLE_FILE_U8 | SLE_VAR_U16),
00353 SLE_VAR(Station, train_station.h, SLE_FILE_U8 | SLE_VAR_U16),
00354
00355 SLE_REF(Station, bus_stops, REF_ROADSTOPS),
00356 SLE_REF(Station, truck_stops, REF_ROADSTOPS),
00357 SLE_VAR(Station, dock_tile, SLE_UINT32),
00358 SLE_VAR(Station, airport.tile, SLE_UINT32),
00359 SLE_CONDVAR(Station, airport.w, SLE_FILE_U8 | SLE_VAR_U16, 140, SL_MAX_VERSION),
00360 SLE_CONDVAR(Station, airport.h, SLE_FILE_U8 | SLE_VAR_U16, 140, SL_MAX_VERSION),
00361 SLE_VAR(Station, airport.type, SLE_UINT8),
00362 SLE_CONDVAR(Station, airport.layout, SLE_UINT8, 145, SL_MAX_VERSION),
00363 SLE_VAR(Station, airport.flags, SLE_UINT64),
00364 SLE_CONDVAR(Station, airport.rotation, SLE_UINT8, 145, SL_MAX_VERSION),
00365 SLE_CONDARR(Station, airport.psa.storage, SLE_UINT32, 16, 145, SL_MAX_VERSION),
00366
00367 SLE_VAR(Station, indtype, SLE_UINT8),
00368
00369 SLE_VAR(Station, time_since_load, SLE_UINT8),
00370 SLE_VAR(Station, time_since_unload, SLE_UINT8),
00371 SLE_VAR(Station, last_vehicle_type, SLE_UINT8),
00372 SLE_VAR(Station, had_vehicle_of_type, SLE_UINT8),
00373 SLE_LST(Station, loading_vehicles, REF_VEHICLE),
00374 SLE_CONDVAR(Station, always_accepted, SLE_UINT32, 127, SL_MAX_VERSION),
00375
00376 SLE_END()
00377 };
00378
00379 static const SaveLoad _waypoint_desc[] = {
00380 SLE_WRITEBYTE(Waypoint, facilities, FACIL_WAYPOINT),
00381 SLE_ST_INCLUDE(),
00382
00383 SLE_VAR(Waypoint, town_cn, SLE_UINT16),
00384
00385 SLE_CONDVAR(Waypoint, train_station.tile, SLE_UINT32, 124, SL_MAX_VERSION),
00386 SLE_CONDVAR(Waypoint, train_station.w, SLE_FILE_U8 | SLE_VAR_U16, 124, SL_MAX_VERSION),
00387 SLE_CONDVAR(Waypoint, train_station.h, SLE_FILE_U8 | SLE_VAR_U16, 124, SL_MAX_VERSION),
00388
00389 SLE_END()
00390 };
00391
00396 const SaveLoad *GetBaseStationDescription()
00397 {
00398 return _base_station_desc;
00399 }
00400
00401 static void RealSave_STNN(BaseStation *bst)
00402 {
00403 bool waypoint = (bst->facilities & FACIL_WAYPOINT) != 0;
00404 SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
00405
00406 if (!waypoint) {
00407 Station *st = Station::From(bst);
00408 for (CargoID i = 0; i < NUM_CARGO; i++) {
00409 SlObject(&st->goods[i], GetGoodsDesc());
00410 }
00411 }
00412
00413 for (uint i = 0; i < bst->num_specs; i++) {
00414 SlObject(&bst->speclist[i], _station_speclist_desc);
00415 }
00416 }
00417
00418 static void Save_STNN()
00419 {
00420 BaseStation *st;
00421
00422 FOR_ALL_BASE_STATIONS(st) {
00423 SlSetArrayIndex(st->index);
00424 SlAutolength((AutolengthProc*)RealSave_STNN, st);
00425 }
00426 }
00427
00428 static void Load_STNN()
00429 {
00430 int index;
00431
00432 while ((index = SlIterateArray()) != -1) {
00433 bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0;
00434
00435 BaseStation *bst = waypoint ? (BaseStation *)new (index) Waypoint() : new (index) Station();
00436 SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
00437
00438 if (!waypoint) {
00439 Station *st = Station::From(bst);
00440 for (CargoID i = 0; i < NUM_CARGO; i++) {
00441 SlObject(&st->goods[i], GetGoodsDesc());
00442 }
00443 }
00444
00445 if (bst->num_specs != 0) {
00446
00447 bst->speclist = CallocT<StationSpecList>(bst->num_specs);
00448 for (uint i = 0; i < bst->num_specs; i++) {
00449 SlObject(&bst->speclist[i], _station_speclist_desc);
00450 }
00451 }
00452 }
00453 }
00454
00455 static void Ptrs_STNN()
00456 {
00457
00458 if (IsSavegameVersionBefore(123)) return;
00459
00460 Station *st;
00461 FOR_ALL_STATIONS(st) {
00462 for (CargoID i = 0; i < NUM_CARGO; i++) {
00463 GoodsEntry *ge = &st->goods[i];
00464 SlObject(ge, GetGoodsDesc());
00465 }
00466 SlObject(st, _station_desc);
00467 }
00468
00469 Waypoint *wp;
00470 FOR_ALL_WAYPOINTS(wp) {
00471 SlObject(wp, _waypoint_desc);
00472 }
00473 }
00474
00475 static void Save_ROADSTOP()
00476 {
00477 RoadStop *rs;
00478
00479 FOR_ALL_ROADSTOPS(rs) {
00480 SlSetArrayIndex(rs->index);
00481 SlObject(rs, _roadstop_desc);
00482 }
00483 }
00484
00485 static void Load_ROADSTOP()
00486 {
00487 int index;
00488
00489 while ((index = SlIterateArray()) != -1) {
00490 RoadStop *rs = new (index) RoadStop(INVALID_TILE);
00491
00492 SlObject(rs, _roadstop_desc);
00493 }
00494 }
00495
00496 static void Ptrs_ROADSTOP()
00497 {
00498 RoadStop *rs;
00499 FOR_ALL_ROADSTOPS(rs) {
00500 SlObject(rs, _roadstop_desc);
00501 }
00502 }
00503
00504 extern const ChunkHandler _station_chunk_handlers[] = {
00505 { 'STNS', NULL, Load_STNS, Ptrs_STNS, NULL, CH_ARRAY },
00506 { 'STNN', Save_STNN, Load_STNN, Ptrs_STNN, NULL, CH_ARRAY },
00507 { 'ROAD', Save_ROADSTOP, Load_ROADSTOP, Ptrs_ROADSTOP, NULL, CH_ARRAY | CH_LAST},
00508 };