From e7c24792162329965cd5e8faee5dbb51212b778c Mon Sep 17 00:00:00 2001 From: smatz Date: Thu, 26 Feb 2009 14:07:42 +0000 Subject: [PATCH] (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts --- src/economy.cpp | 18 ++++++++++++++++++ src/saveload/afterload.cpp | 28 ++++++++++++++++++++-------- src/station_cmd.cpp | 4 +--- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index eaea1dd6e5..c4a6e9a849 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -398,6 +398,24 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) UpdateSignalsInBuffer(); } + /* convert owner of stations (including deleted ones, but excluding buoys) */ + Station *st; + FOR_ALL_STATIONS(st) { + if (st->owner == old_owner) { + /* if a company goes bankrupt, set owner to OWNER_NONE so the sign doesn't disappear immediately + * also, drawing station window would cause reading invalid company's colour */ + st->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner; + } + } + + /* do the same for waypoints (we need to do this here so deleted waypoints are converted too) */ + Waypoint *wp; + FOR_ALL_WAYPOINTS(wp) { + if (wp->owner == old_owner) { + wp->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner; + } + } + /* In all cases clear replace engine rules. * Even if it was copied, it could interfere with new owner's rules */ RemoveAllEngineReplacementForCompany(GetCompany(old_owner)); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 97f114c4e8..2f2362a81b 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1615,14 +1615,6 @@ bool AfterLoadGame() } } } - - /* Give owners to waypoints, based on rail tracks it is sitting on. - * If none is available, specify OWNER_NONE */ - Waypoint *wp; - FOR_ALL_WAYPOINTS(wp) { - Owner owner = (IsRailWaypointTile(wp->xy) ? GetTileOwner(wp->xy) : OWNER_NONE); - wp->owner = IsValidCompanyID(owner) ? owner : OWNER_NONE; - } } if (CheckSavegameVersion(102)) { @@ -1722,6 +1714,26 @@ bool AfterLoadGame() } } + if (CheckSavegameVersion(114)) { + /* There could be (deleted) stations with invalid owner, set owner to OWNER NONE. + * The conversion affects oil rigs and buoys too, but it doesn't matter as + * they have st->owner == OWNER_NONE already. */ + Station *st; + FOR_ALL_STATIONS(st) { + if (!IsValidCompanyID(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 = IsValidCompanyID(owner) ? owner : OWNER_NONE; + } + } + GamelogPrintDebug(1); bool ret = InitializeWindowsAndCaches(); diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 8c3b1c7377..1e19cc55d4 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3120,10 +3120,8 @@ static void ChangeTileOwner_Station(TileIndex tile, Owner old_owner, Owner new_o if (!IsTileOwner(tile, old_owner)) return; if (new_owner != INVALID_OWNER) { - Station *st = GetStationByTile(tile); - + /* for buoys, owner of tile is owner of water, st->owner == OWNER_NONE */ SetTileOwner(tile, new_owner); - if (!IsBuoy(tile)) st->owner = new_owner; // do not set st->owner for buoys InvalidateWindowClassesData(WC_STATION_LIST, 0); } else { if (IsDriveThroughStopTile(tile)) {