summaryrefslogtreecommitdiff
path: root/src/saveload
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-07-22 08:59:57 +0000
committerrubidium <rubidium@openttd.org>2009-07-22 08:59:57 +0000
commit68ead6b84f56ad3c93d05ad3e09b7fbb8173cf26 (patch)
treeee33bd19b96a138530a84e86e7ffd60c0700f9d9 /src/saveload
parent2646a99d29535a44c5998f080131cbca2e85bdcf (diff)
downloadopenttd-68ead6b84f56ad3c93d05ad3e09b7fbb8173cf26.tar.xz
(svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
-Change: make (rail) waypoints sub classes of 'base stations', make buoys waypoints and unify code between them where possible.
Diffstat (limited to 'src/saveload')
-rw-r--r--src/saveload/afterload.cpp70
-rw-r--r--src/saveload/saveload.cpp7
-rw-r--r--src/saveload/saveload.h2
-rw-r--r--src/saveload/saveload_internal.h5
-rw-r--r--src/saveload/station_sl.cpp292
-rw-r--r--src/saveload/waypoint_sl.cpp205
6 files changed, 403 insertions, 178 deletions
diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp
index 452dbb997..b69ca6e2d 100644
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -606,9 +606,7 @@ bool AfterLoadGame()
switch (GetTileType(t)) {
case MP_STATION: {
Station *st = Station::GetByTile(t);
-
- /* Set up station spread; buoys do not have one */
- if (!IsBuoy(t)) st->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
+ if (st == NULL) break;
switch (GetStationType(t)) {
case STATION_TRUCK:
@@ -987,29 +985,11 @@ bool AfterLoadGame()
FOR_ALL_COMPANIES(c) c->settings.renew_keep_length = false;
}
- /* In version 17, ground type is moved from m2 to m4 for depots and
- * waypoints to make way for storing the index in m2. The custom graphics
- * id which was stored in m4 is now saved as a grf/id reference in the
- * waypoint struct. */
- if (CheckSavegameVersion(17)) {
- Waypoint *wp;
-
- FOR_ALL_WAYPOINTS(wp) {
- if (wp->delete_ctr == 0) {
- if (HasBit(_m[wp->xy].m3, 4)) {
- AllocateSpecToStation(GetCustomStationSpec(STAT_CLASS_WAYP, _m[wp->xy].m4 + 1), wp, true);
- }
-
- /* Move ground type bits from m2 to m4. */
- _m[wp->xy].m4 = GB(_m[wp->xy].m2, 0, 4);
- /* Store waypoint index in the tile. */
- _m[wp->xy].m2 = wp->index;
- }
- }
- } else {
- /* As of version 17, we recalculate the custom graphic ID of waypoints
- * from the GRF ID / station index. */
- AfterLoadWaypoints();
+ if (CheckSavegameVersion(123)) {
+ /* Waypoints became subclasses of stations ... */
+ MoveWaypointsToBaseStations();
+ /* ... and buoys were moved to waypoints. */
+ MoveBuoysToWaypoints();
}
/* From version 15, we moved a semaphore bit from bit 2 to bit 3 in m4, making
@@ -1275,9 +1255,9 @@ bool AfterLoadGame()
/* Buoys do now store the owner of the previous water tile, which can never
* be OWNER_NONE. So replace OWNER_NONE with OWNER_WATER. */
if (CheckSavegameVersion(46)) {
- Station *st;
- FOR_ALL_STATIONS(st) {
- if (st->IsBuoy() && IsTileOwner(st->xy, OWNER_NONE) && TileHeight(st->xy) == 0) SetTileOwner(st->xy, OWNER_WATER);
+ Waypoint *wp;
+ FOR_ALL_WAYPOINTS(wp) {
+ if ((wp->facilities & FACIL_DOCK) != 0 && IsTileOwner(wp->xy, OWNER_NONE) && TileHeight(wp->xy) == 0) SetTileOwner(wp->xy, OWNER_WATER);
}
}
@@ -1487,15 +1467,6 @@ bool AfterLoadGame()
}
if (CheckSavegameVersion(84)) {
- /* Update go to buoy orders because they are just waypoints */
- Order *order;
- FOR_ALL_ORDERS(order) {
- if (order->IsType(OT_GOTO_STATION) && Station::Get(order->GetDestination())->IsBuoy()) {
- order->SetLoadType(OLF_LOAD_IF_POSSIBLE);
- order->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
- }
- }
-
/* Set all share owners to INVALID_COMPANY for
* 1) all inactive companies
* (when inactive companies were stored in the savegame - TTD, TTDP and some
@@ -1561,7 +1532,7 @@ bool AfterLoadGame()
if (IsBuoyTile(t)) {
/* reset buoy owner to OWNER_NONE in the station struct
* (even if it is owned by active company) */
- Station::GetByTile(t)->owner = OWNER_NONE;
+ Waypoint::GetByTile(t)->owner = OWNER_NONE;
}
} else if (IsTileType(t, MP_ROAD)) {
/* works for all RoadTileType */
@@ -1805,16 +1776,6 @@ bool AfterLoadGame()
FOR_ALL_STATIONS(st) {
if (!Company::IsValidID(st->owner)) st->owner = OWNER_NONE;
}
-
- /* Give owners to waypoints, based on rail tracks it is sitting on.
- * If none is available, specify OWNER_NONE.
- * This code was in CheckSavegameVersion(101) in the past, but in some cases,
- * the owner of waypoints could be incorrect. */
- Waypoint *wp;
- FOR_ALL_WAYPOINTS(wp) {
- Owner owner = IsTileType(wp->xy, MP_RAILWAY) ? GetTileOwner(wp->xy) : OWNER_NONE;
- wp->owner = Company::IsValidID(owner) ? owner : OWNER_NONE;
- }
}
/* Trains could now stop in a specific location. */
@@ -1913,14 +1874,6 @@ bool AfterLoadGame()
}
s->cargo_type = CT_INVALID;
}
-
- Order *o;
- FOR_ALL_ORDERS(o) {
- /* Buoys are now go to waypoint orders */
- if (!o->IsType(OT_GOTO_STATION) || !Station::Get(o->GetDestination())->IsBuoy()) continue;
-
- o->MakeGoToWaypoint(o->GetDestination());
- }
}
AfterLoadLabelMaps();
@@ -1950,8 +1903,7 @@ void ReloadNewGRFData()
AfterLoadVehicles(false);
StartupEngines();
SetCachedEngineCounts();
- /* update station and waypoint graphics */
- AfterLoadWaypoints();
+ /* update station graphics */
AfterLoadStations();
/* Check and update house and town values */
UpdateHousesAndTowns();
diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp
index 8b3178512..dd44cc4cd 100644
--- a/src/saveload/saveload.cpp
+++ b/src/saveload/saveload.cpp
@@ -41,7 +41,7 @@
#include "saveload_internal.h"
-extern const uint16 SAVEGAME_VERSION = 122;
+extern const uint16 SAVEGAME_VERSION = 123;
SavegameType _savegame_type; ///< type of savegame we are loading
@@ -873,6 +873,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
break;
case SL_WRITEBYTE: return 1; // a byte is logically of size 1
case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
+ case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
default: NOT_REACHED();
}
return 0;
@@ -934,6 +935,10 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
SlObject(ptr, GetVehicleDescription(VEH_END));
break;
+ case SL_ST_INCLUDE:
+ SlObject(ptr, GetBaseStationDescription());
+ break;
+
default: NOT_REACHED();
}
return true;
diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h
index cd1b9464c..25e1e35b6 100644
--- a/src/saveload/saveload.h
+++ b/src/saveload/saveload.h
@@ -182,6 +182,7 @@ enum SaveLoadTypes {
/* non-normal save-load types */
SL_WRITEBYTE = 8,
SL_VEH_INCLUDE = 9,
+ SL_ST_INCLUDE = 10,
SL_END = 15
};
@@ -235,6 +236,7 @@ typedef SaveLoad SaveLoadGlobVarList;
#define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, 0, something, 0)
#define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION)
+#define SLE_ST_INCLUDEX() SLE_GENERALX(SL_ST_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION)
/* End marker */
#define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
diff --git a/src/saveload/saveload_internal.h b/src/saveload/saveload_internal.h
index 21030751a..0e5eb791e 100644
--- a/src/saveload/saveload_internal.h
+++ b/src/saveload/saveload_internal.h
@@ -16,7 +16,10 @@ StringID RemapOldStringID(StringID s);
char *CopyFromOldName(StringID id);
void ResetOldNames();
-void AfterLoadWaypoints();
+void MoveBuoysToWaypoints();
+void MoveWaypointsToBaseStations();
+const SaveLoad *GetBaseStationDescription();
+
void AfterLoadVehicles(bool part_of_load);
void AfterLoadStations();
void AfterLoadLabelMaps();
diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp
index c8fbf1624..603be9cfd 100644
--- a/src/saveload/station_sl.cpp
+++ b/src/saveload/station_sl.cpp
@@ -4,27 +4,98 @@
#include "../stdafx.h"
#include "../station_base.h"
+#include "../waypoint.h"
#include "../roadstop_base.h"
+#include "../order_base.h"
+#include "../vehicle_base.h"
#include "../core/bitmath_func.hpp"
#include "../core/alloc_func.hpp"
#include "../variables.h"
#include "../newgrf_station.h"
#include "saveload.h"
+#include "table/strings.h"
+/**
+ * Update the buoy orders to be waypoint orders.
+ * @param o the order 'list' to check.
+ */
+static void UpdateWaypointOrder(Order *o)
+{
+ if (!o->IsType(OT_GOTO_STATION)) return;
-void AfterLoadStations()
+ const Station *st = Station::Get(o->GetDestination());
+ if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) return;
+
+ o->MakeGoToWaypoint(o->GetDestination());
+}
+
+/**
+ * Perform all steps to upgrade from the old station buoys to the new version
+ * that uses waypoints. This includes some old saveload mechanics.
+ */
+void MoveBuoysToWaypoints()
{
- /* Update the speclists of all stations to point to the currently loaded custom stations. */
+ /* Buoy orders become waypoint orders */
+ OrderList *ol;
+ FOR_ALL_ORDER_LISTS(ol) {
+ if (ol->GetFirstSharedVehicle()->type != VEH_SHIP) continue;
+
+ for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
+ }
+
+ Vehicle *v;
+ FOR_ALL_VEHICLES(v) {
+ if (v->type != VEH_SHIP) continue;
+
+ UpdateWaypointOrder(&v->current_order);
+ }
+
+ /* Now make the stations waypoints */
Station *st;
FOR_ALL_STATIONS(st) {
+ if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue;
+
+ StationID index = st->index;
+ TileIndex xy = st->xy;
+ Town *town = st->town;
+ StringID string_id = st->string_id;
+ char *name = st->name;
+ Date build_date = st->build_date;
+
+ /* Delete the station, so we can make it a real waypoint. */
+ delete st;
+
+ Waypoint *wp = new (index) Waypoint(xy);
+ wp->town = town;
+ wp->string_id = STR_SV_STNAME_BUOY;
+ wp->name = name;
+ wp->delete_ctr = 0; // Just reset delete counter for once.
+ wp->build_date = build_date;
+ wp->owner = OWNER_NONE;
+
+ if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY;
+
+ if (IsBuoyTile(xy) && GetStationIndex(xy) == index) {
+ wp->facilities |= FACIL_DOCK;
+ }
+ }
+}
+
+void AfterLoadStations()
+{
+ /* Update the speclists of all stations to point to the currently loaded custom stations. */
+ BaseStation *st;
+ FOR_ALL_BASE_STATIONS(st) {
for (uint i = 0; i < st->num_specs; i++) {
if (st->speclist[i].grfid == 0) continue;
st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx, NULL);
}
- for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
+ if (Station::IsExpected(st)) {
+ for (CargoID c = 0; c < NUM_CARGO; c++) Station::From(st)->goods[c].cargo.InvalidateCache();
+ }
StationUpdateAnimTriggers(st);
}
@@ -48,7 +119,7 @@ static const SaveLoad _roadstop_desc[] = {
SLE_END()
};
-static const SaveLoad _station_desc[] = {
+static const SaveLoad _old_station_desc[] = {
SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
SLE_CONDVAR(Station, xy, SLE_UINT32, 6, SL_MAX_VERSION),
SLE_CONDNULL(4, 0, 5), ///< bus/lorry tile
@@ -145,78 +216,202 @@ const SaveLoad *GetGoodsDesc()
}
-static void SaveLoad_STNS(Station *st)
+static void Load_STNS()
{
- SlObject(st, _station_desc);
-
- _waiting_acceptance = 0;
-
- uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
- for (CargoID i = 0; i < num_cargo; i++) {
- GoodsEntry *ge = &st->goods[i];
- SlObject(ge, GetGoodsDesc());
- if (CheckSavegameVersion(68)) {
- SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
- if (GB(_waiting_acceptance, 0, 12) != 0) {
- /* Don't construct the packet with station here, because that'll fail with old savegames */
- CargoPacket *cp = new CargoPacket();
- /* In old versions, enroute_from used 0xFF as INVALID_STATION */
- cp->source = (CheckSavegameVersion(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
- cp->count = GB(_waiting_acceptance, 0, 12);
- cp->days_in_transit = _cargo_days;
- cp->feeder_share = _cargo_feeder_share;
- cp->source_xy = _cargo_source_xy;
- cp->days_in_transit = _cargo_days;
- cp->feeder_share = _cargo_feeder_share;
- SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, 1);
- ge->cargo.Append(cp);
+ int index;
+ while ((index = SlIterateArray()) != -1) {
+ Station *st = new (index) Station();
+
+ SlObject(st, _old_station_desc);
+
+ _waiting_acceptance = 0;
+
+ uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
+ for (CargoID i = 0; i < num_cargo; i++) {
+ GoodsEntry *ge = &st->goods[i];
+ SlObject(ge, GetGoodsDesc());
+ if (CheckSavegameVersion(68)) {
+ SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
+ if (GB(_waiting_acceptance, 0, 12) != 0) {
+ /* Don't construct the packet with station here, because that'll fail with old savegames */
+ CargoPacket *cp = new CargoPacket();
+ /* In old versions, enroute_from used 0xFF as INVALID_STATION */
+ cp->source = (CheckSavegameVersion(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
+ cp->count = GB(_waiting_acceptance, 0, 12);
+ cp->days_in_transit = _cargo_days;
+ cp->feeder_share = _cargo_feeder_share;
+ cp->source_xy = _cargo_source_xy;
+ cp->days_in_transit = _cargo_days;
+ cp->feeder_share = _cargo_feeder_share;
+ SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, 1);
+ ge->cargo.Append(cp);
+ }
}
}
- }
- if (st->num_specs != 0) {
- /* Allocate speclist memory when loading a game */
- if (st->speclist == NULL) st->speclist = CallocT<StationSpecList>(st->num_specs);
- for (uint i = 0; i < st->num_specs; i++) {
- SlObject(&st->speclist[i], _station_speclist_desc);
+ if (st->num_specs != 0) {
+ /* Allocate speclist memory when loading a game */
+ st->speclist = CallocT<StationSpecList>(st->num_specs);
+ for (uint i = 0; i < st->num_specs; i++) {
+ SlObject(&st->speclist[i], _station_speclist_desc);
+ }
}
}
}
-static void Save_STNS()
+void Ptrs_STNS()
{
+ /* Don't run when savegame version is higher than or equal to 123. */
+ if (!CheckSavegameVersion(123)) return;
+
Station *st;
- /* Write the stations */
FOR_ALL_STATIONS(st) {
+ if (!CheckSavegameVersion(68)) {
+ for (CargoID i = 0; i < NUM_CARGO; i++) {
+ GoodsEntry *ge = &st->goods[i];
+ SlObject(ge, GetGoodsDesc());
+ }
+ }
+ SlObject(st, _old_station_desc);
+ }
+}
+
+
+static const SaveLoad _base_station_desc[] = {
+ SLE_VAR(BaseStation, xy, SLE_UINT32),
+ SLE_REF(BaseStation, town, REF_TOWN),
+ SLE_VAR(BaseStation, string_id, SLE_STRINGID),
+ SLE_STR(BaseStation, name, SLE_STR, 0),
+ SLE_VAR(BaseStation, delete_ctr, SLE_UINT8),
+ SLE_VAR(BaseStation, owner, SLE_UINT8),
+ SLE_VAR(BaseStation, facilities, SLE_UINT8),
+ SLE_VAR(BaseStation, build_date, SLE_INT32),
+
+ /* Used by newstations for graphic variations */
+ SLE_VAR(BaseStation, random_bits, SLE_UINT16),
+ SLE_VAR(BaseStation, waiting_triggers, SLE_UINT8),
+ SLE_VAR(BaseStation, num_specs, SLE_UINT8),
+
+ SLE_END()
+};
+
+static const SaveLoad _station_desc[] = {
+ SLE_WRITEBYTE(Station, facilities, FACIL_NONE),
+ SLE_ST_INCLUDEX(),
+
+ SLE_VAR(Station, train_tile, SLE_UINT32),
+ SLE_VAR(Station, trainst_w, SLE_UINT8),
+ SLE_VAR(Station, trainst_h, SLE_UINT8),
+
+ SLE_REF(Station, bus_stops, REF_ROADSTOPS),
+ SLE_REF(Station, truck_stops, REF_ROADSTOPS),
+ SLE_VAR(Station, dock_tile, SLE_UINT32),
+ SLE_VAR(Station, airport_tile, SLE_UINT32),
+ SLE_VAR(Station, airport_type, SLE_UINT8),
+ SLE_VAR(Station, airport_flags, SLE_UINT64),
+
+ SLE_VAR(Station, indtype, SLE_UINT8),
+
+ SLE_VAR(Station, time_since_load, SLE_UINT8),
+ SLE_VAR(Station, time_since_unload, SLE_UINT8),
+ SLE_VAR(Station, last_vehicle_type, SLE_UINT8),
+ SLE_VAR(Station, had_vehicle_of_type, SLE_UINT8),
+ SLE_LST(Station, loading_vehicles, REF_VEHICLE),
+
+ SLE_END()
+};
+
+static const SaveLoad _waypoint_desc[] = {
+ SLE_WRITEBYTE(Waypoint, facilities, FACIL_WAYPOINT),
+ SLE_ST_INCLUDEX(),
+
+ SLE_VAR(Waypoint, town_cn, SLE_UINT16),
+
+ SLE_END()
+};
+
+/**
+ * Get the base station description to be used for SL_ST_INCLUDE
+ * @return the base station description.
+ */
+const SaveLoad *GetBaseStationDescription()
+{
+ return _base_station_desc;
+}
+
+static void RealSave_STNN(BaseStation *bst)
+{
+ bool waypoint = (bst->facilities & FACIL_WAYPOINT) != 0;
+ SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
+
+ if (!waypoint) {
+ Station *st = Station::From(bst);
+ for (CargoID i = 0; i < NUM_CARGO; i++) {
+ SlObject(&st->goods[i], GetGoodsDesc());
+ }
+ }
+
+ for (uint i = 0; i < bst->num_specs; i++) {
+ SlObject(&bst->speclist[i], _station_speclist_desc);
+ }
+}
+
+static void Save_STNN()
+{
+ BaseStation *st;
+ /* Write the stations */
+ FOR_ALL_BASE_STATIONS(st) {
SlSetArrayIndex(st->index);
- SlAutolength((AutolengthProc*)SaveLoad_STNS, st);
+ SlAutolength((AutolengthProc*)RealSave_STNN, st);
}
}
-static void Load_STNS()
+static void Load_STNN()
{
int index;
+
while ((index = SlIterateArray()) != -1) {
- Station *st = new (index) Station();
+ bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0;
- SaveLoad_STNS(st);
+ BaseStation *bst = waypoint ? (BaseStation *)new (index) Waypoint() : new (index) Station();
+ SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
+
+ if (!waypoint) {
+ Station *st = Station::From(bst);
+ for (CargoID i = 0; i < NUM_CARGO; i++) {
+ SlObject(&st->goods[i], GetGoodsDesc());
+ }
+ }
+
+ if (bst->num_specs != 0) {
+ /* Allocate speclist memory when loading a game */
+ bst->speclist = CallocT<StationSpecList>(bst->num_specs);
+ for (uint i = 0; i < bst->num_specs; i++) {
+ SlObject(&bst->speclist[i], _station_speclist_desc);
+ }
+ }
}
}
-void Ptrs_STNS()
+static void Ptrs_STNN()
{
+ /* Don't run when savegame version lower than 123. */
+ if (CheckSavegameVersion(123)) return;
+
Station *st;
FOR_ALL_STATIONS(st) {
- if (!CheckSavegameVersion(68)) {
- for (CargoID i = 0; i < NUM_CARGO; i++) {
- GoodsEntry *ge = &st->goods[i];
- SlObject(ge, GetGoodsDesc());
- }
+ for (CargoID i = 0; i < NUM_CARGO; i++) {
+ GoodsEntry *ge = &st->goods[i];
+ SlObject(ge, GetGoodsDesc());
}
SlObject(st, _station_desc);
}
-}
+ Waypoint *wp;
+ FOR_ALL_WAYPOINTS(wp) {
+ SlObject(wp, _waypoint_desc);
+ }
+}
static void Save_ROADSTOP()
{
@@ -248,6 +443,7 @@ static void Ptrs_ROADSTOP()
}
extern const ChunkHandler _station_chunk_handlers[] = {
- { 'STNS', Save_STNS, Load_STNS, Ptrs_STNS, CH_ARRAY },
+ { 'STNS', NULL, Load_STNS, Ptrs_STNS, CH_ARRAY },
+ { 'STNN', Save_STNN, Load_STNN, Ptrs_STNN, CH_ARRAY },
{ 'ROAD', Save_ROADSTOP, Load_ROADSTOP, Ptrs_ROADSTOP, CH_ARRAY | CH_LAST},
};
diff --git a/src/saveload/waypoint_sl.cpp b/src/saveload/waypoint_sl.cpp
index 780271068..94e88996f 100644
--- a/src/saveload/waypoint_sl.cpp
+++ b/src/saveload/waypoint_sl.cpp
@@ -5,120 +5,187 @@
#include "../stdafx.h"
#include "../waypoint.h"
#include "../newgrf_station.h"
+#include "../vehicle_base.h"
#include "../town.h"
+#include "../station_map.h"
#include "table/strings.h"
#include "saveload.h"
#include "saveload_internal.h"
+/** Helper structure to convert from the old waypoint system. */
+struct OldWaypoint {
+ size_t index;
+ TileIndex xy;
+ TownID town_index;
+ Town *town;
+ uint16 town_cn;
+ StringID string_id;
+ char *name;
+ uint8 delete_ctr;
+ Date build_date;
+ uint8 localidx;
+ uint32 grfid;
+ const StationSpec *spec;
+ OwnerByte owner;
+
+ size_t new_index;
+};
+
+/** Temporary array with old waypoints. */
+static SmallVector<OldWaypoint, 16> _old_waypoints;
+
/**
- * Update waypoint graphics id against saved GRFID/localidx.
- * This is to ensure the chosen graphics are correct if GRF files are changed.
+ * Update the waypoint orders to get the new waypoint ID.
+ * @param o the order 'list' to check.
*/
-void AfterLoadWaypoints()
+static void UpdateWaypointOrder(Order *o)
{
- Waypoint *wp;
+ if (!o->IsType(OT_GOTO_WAYPOINT)) return;
- FOR_ALL_WAYPOINTS(wp) {
- if (wp->num_specs == 0) continue;
+ for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
+ if (wp->index != o->GetDestination()) continue;
- for (uint i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
- const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
- if (statspec != NULL && statspec->grffile->grfid == wp->speclist[1].grfid && statspec->localidx == wp->speclist[1].localidx) {
- wp->speclist[1].spec = statspec;
- break;
+ o->SetDestination(wp->new_index);
+ return;
+ }
+}
+
+/**
+ * Perform all steps to upgrade from the old waypoints to the new version
+ * that uses station. This includes some old saveload mechanics.
+ */
+void MoveWaypointsToBaseStations()
+{
+ /* In version 17, ground type is moved from m2 to m4 for depots and
+ * waypoints to make way for storing the index in m2. The custom graphics
+ * id which was stored in m4 is now saved as a grf/id reference in the
+ * waypoint struct. */
+ if (CheckSavegameVersion(17)) {
+ for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
+ if (wp->delete_ctr == 0 && HasBit(_m[wp->xy].m3, 4)) {
+ wp->spec = GetCustomStationSpec(STAT_CLASS_WAYP, _m[wp->xy].m4 + 1);
+ }
+ }
+ } else {
+ /* As of version 17, we recalculate the custom graphic ID of waypoints
+ * from the GRF ID / station index. */
+ for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
+ for (uint i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
+ const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
+ if (statspec != NULL && statspec->grffile->grfid == wp->grfid && statspec->localidx == wp->localidx) {
+ wp->spec = statspec;
+ break;
+ }
}
}
}
-}
-static uint16 _waypoint_town_index;
-static StringID _waypoint_string_id;
-static StationSpecList _waypoint_spec;
-
-static const SaveLoad _waypoint_desc[] = {
- SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
- SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION),
- SLEG_CONDVAR(_waypoint_town_index, SLE_UINT16, 12, 121),
- SLE_CONDREF(Waypoint, town, REF_TOWN, 122, SL_MAX_VERSION),
- SLE_CONDVAR(Waypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88),
- SLE_CONDVAR(Waypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION),
- SLEG_CONDVAR(_waypoint_string_id, SLE_STRINGID, 0, 83),
- SLE_CONDSTR(Waypoint, name, SLE_STR, 0, 84, SL_MAX_VERSION),
- SLE_VAR(Waypoint, delete_ctr, SLE_UINT8),
-
- SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
- SLE_CONDVAR(Waypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION),
- SLEG_CONDVAR(_waypoint_spec.localidx, SLE_UINT8, 3, SL_MAX_VERSION),
- SLEG_CONDVAR(_waypoint_spec.grfid, SLE_UINT32, 17, SL_MAX_VERSION),
- SLE_CONDVAR(Waypoint, owner, SLE_UINT8, 101, SL_MAX_VERSION),
+ /* All saveload conversions have been done. Create the new waypoints! */
+ for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
+ Waypoint *new_wp = new Waypoint(wp->xy);
+ new_wp->town = wp->town;
+ new_wp->town_cn = wp->town_cn;
+ new_wp->name = wp->name;
+ new_wp->delete_ctr = 0; // Just reset delete counter for once.
+ new_wp->build_date = wp->build_date;
+ new_wp->owner = wp->owner;
- SLE_END()
-};
+ new_wp->string_id = STR_SV_STNAME_WAYPOINT;
-static void Save_WAYP()
-{
- Waypoint *wp;
+ TileIndex t = wp->xy;
+ if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && _m[t].m2 == wp->index) {
+ /* The tile might've been reserved! */
+ bool reserved = !CheckSavegameVersion(100) && HasBit(_m[t].m5, 4);
+
+ /* The tile really has our waypoint, so reassign the map array */
+ MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
+ new_wp->facilities |= FACIL_TRAIN;
+ new_wp->owner = GetTileOwner(t);
- FOR_ALL_WAYPOINTS(wp) {
- if (wp->num_specs == 0) {
- _waypoint_spec.grfid = 0;
- } else {
- _waypoint_spec = wp->speclist[1];
+ SetRailwayStationReservation(t, reserved);
+
+ if (wp->spec != NULL) {
+ SetCustomStationSpecIndex(t, AllocateSpecToStation(wp->spec, new_wp, true));
+ }
}
- SlSetArrayIndex(wp->index);
- SlObject(wp, _waypoint_desc);
+ wp->new_index = new_wp->index;
}
+
+ /* Update the orders of vehicles */
+ OrderList *ol;
+ FOR_ALL_ORDER_LISTS(ol) {
+ if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
+
+ for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
+ }
+
+ Vehicle *v;
+ FOR_ALL_VEHICLES(v) {
+ if (v->type != VEH_TRAIN) continue;
+
+ UpdateWaypointOrder(&v->current_order);
+ }
+
+ _old_waypoints.Reset();
}
+static const SaveLoad _old_waypoint_desc[] = {
+ SLE_CONDVAR(OldWaypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
+ SLE_CONDVAR(OldWaypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16, 12, 121),
+ SLE_CONDREF(OldWaypoint, town, REF_TOWN, 122, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88),
+ SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, 0, 83),
+ SLE_CONDSTR(OldWaypoint, name, SLE_STR, 0, 84, SL_MAX_VERSION),
+ SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
+
+ SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
+ SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, localidx, SLE_UINT8, 3, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, grfid, SLE_UINT32, 17, SL_MAX_VERSION),
+ SLE_CONDVAR(OldWaypoint, owner, SLE_UINT8, 101, SL_MAX_VERSION),
+
+ SLE_END()
+};
+
static void Load_WAYP()
{
+ /* Precaution for when loading failed and it didn't get cleared */
+ _old_waypoints.Clear();
+
int index;
while ((index = SlIterateArray()) != -1) {
- _waypoint_string_id = 0;
- _waypoint_town_index = 0;
- _waypoint_spec.grfid = 0;
-
- Waypoint *wp = new (index) Waypoint();
- SlObject(wp, _waypoint_desc);
+ OldWaypoint *wp = _old_waypoints.Append();
+ memset(wp, 0, sizeof(*wp));
- wp->facilities |= FACIL_TRAIN;
-
- if (_waypoint_spec.grfid != 0) {
- wp->num_specs = 2;
- wp->speclist = CallocT<StationSpecList>(2);
- wp->speclist[1] = _waypoint_spec;
- }
-
- if (CheckSavegameVersion(84)) wp->name = (char *)(size_t)_waypoint_string_id;
- if (CheckSavegameVersion(122)) wp->town = (Town *)(size_t)_waypoint_town_index;
+ wp->index = index;
+ SlObject(wp, _old_waypoint_desc);
}
}
static void Ptrs_WAYP()
{
- Waypoint *wp;
-
- FOR_ALL_WAYPOINTS(wp) {
- SlObject(wp, _waypoint_desc);
+ for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
+ SlObject(wp, _old_waypoint_desc);
- StringID sid = (StringID)(size_t)wp->name;
if (CheckSavegameVersion(12)) {
- wp->town_cn = (sid & 0xC000) == 0xC000 ? (sid >> 8) & 0x3F : 0;
+ wp->town_cn = (wp->string_id & 0xC000) == 0xC000 ? (wp->string_id >> 8) & 0x3F : 0;
wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
} else if (CheckSavegameVersion(122)) {
/* Only for versions 12 .. 122 */
- wp->town = Town::Get((size_t)wp->town);
+ wp->town = Town::Get(wp->town_index);
}
if (CheckSavegameVersion(84)) {
- wp->name = CopyFromOldName(sid);
+ wp->name = CopyFromOldName(wp->string_id);
}
}
}
extern const ChunkHandler _waypoint_chunk_handlers[] = {
- { 'CHKP', Save_WAYP, Load_WAYP, Ptrs_WAYP, CH_ARRAY | CH_LAST},
+ { 'CHKP', NULL, Load_WAYP, Ptrs_WAYP, CH_ARRAY | CH_LAST},
};