waypoint_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 "../waypoint_base.h"
00014 #include "../newgrf_station.h"
00015 #include "../vehicle_base.h"
00016 #include "../town.h"
00017 #include "../newgrf.h"
00018 
00019 #include "table/strings.h"
00020 
00021 #include "saveload_internal.h"
00022 
00024 struct OldWaypoint {
00025   size_t index;
00026   TileIndex xy;
00027   TownID town_index;
00028   Town *town;
00029   uint16 town_cn;
00030   StringID string_id;
00031   char *name;
00032   uint8 delete_ctr;
00033   Date build_date;
00034   uint8 localidx;
00035   uint32 grfid;
00036   const StationSpec *spec;
00037   OwnerByte owner;
00038 
00039   size_t new_index;
00040 };
00041 
00043 static SmallVector<OldWaypoint, 16> _old_waypoints;
00044 
00049 static void UpdateWaypointOrder(Order *o)
00050 {
00051   if (!o->IsType(OT_GOTO_WAYPOINT)) return;
00052 
00053   for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00054     if (wp->index != o->GetDestination()) continue;
00055 
00056     o->SetDestination((DestinationID)wp->new_index);
00057     return;
00058   }
00059 }
00060 
00065 void MoveWaypointsToBaseStations()
00066 {
00067   /* In version 17, ground type is moved from m2 to m4 for depots and
00068    * waypoints to make way for storing the index in m2. The custom graphics
00069    * id which was stored in m4 is now saved as a grf/id reference in the
00070    * waypoint struct. */
00071   if (IsSavegameVersionBefore(17)) {
00072     for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00073       if (wp->delete_ctr != 0) continue; // The waypoint was deleted
00074 
00075       /* Waypoint indices were not added to the map prior to this. */
00076       _m[wp->xy].m2 = (StationID)wp->index;
00077 
00078       if (HasBit(_m[wp->xy].m3, 4)) {
00079         wp->spec = StationClass::Get(STAT_CLASS_WAYP, _m[wp->xy].m4 + 1);
00080       }
00081     }
00082   } else {
00083     /* As of version 17, we recalculate the custom graphic ID of waypoints
00084      * from the GRF ID / station index. */
00085     for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00086       for (uint i = 0; i < StationClass::GetCount(STAT_CLASS_WAYP); i++) {
00087         const StationSpec *statspec =  StationClass::Get(STAT_CLASS_WAYP, i);
00088         if (statspec != NULL && statspec->grf_prop.grffile->grfid == wp->grfid && statspec->grf_prop.local_id == wp->localidx) {
00089           wp->spec = statspec;
00090           break;
00091         }
00092       }
00093     }
00094   }
00095 
00096   if (!Waypoint::CanAllocateItem(_old_waypoints.Length())) SlError(STR_ERROR_TOO_MANY_STATIONS_LOADING);
00097 
00098   /* All saveload conversions have been done. Create the new waypoints! */
00099   for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00100     Waypoint *new_wp = new Waypoint(wp->xy);
00101     new_wp->town       = wp->town;
00102     new_wp->town_cn    = wp->town_cn;
00103     new_wp->name       = wp->name;
00104     new_wp->delete_ctr = 0; // Just reset delete counter for once.
00105     new_wp->build_date = wp->build_date;
00106     new_wp->owner      = wp->owner;
00107 
00108     new_wp->string_id = STR_SV_STNAME_WAYPOINT;
00109 
00110     TileIndex t = wp->xy;
00111     if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && _m[t].m2 == wp->index) {
00112       /* The tile might've been reserved! */
00113       bool reserved = !IsSavegameVersionBefore(100) && HasBit(_m[t].m5, 4);
00114 
00115       /* The tile really has our waypoint, so reassign the map array */
00116       MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
00117       new_wp->facilities |= FACIL_TRAIN;
00118       new_wp->owner = GetTileOwner(t);
00119 
00120       SetRailStationReservation(t, reserved);
00121 
00122       if (wp->spec != NULL) {
00123         SetCustomStationSpecIndex(t, AllocateSpecToStation(wp->spec, new_wp, true));
00124       }
00125       new_wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
00126     }
00127 
00128     wp->new_index = new_wp->index;
00129   }
00130 
00131   /* Update the orders of vehicles */
00132   OrderList *ol;
00133   FOR_ALL_ORDER_LISTS(ol) {
00134     if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
00135 
00136     for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
00137   }
00138 
00139   Vehicle *v;
00140   FOR_ALL_VEHICLES(v) {
00141     if (v->type != VEH_TRAIN) continue;
00142 
00143     UpdateWaypointOrder(&v->current_order);
00144   }
00145 
00146   _old_waypoints.Reset();
00147 }
00148 
00149 static const SaveLoad _old_waypoint_desc[] = {
00150   SLE_CONDVAR(OldWaypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00151   SLE_CONDVAR(OldWaypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
00152   SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16,                 12, 121),
00153   SLE_CONDREF(OldWaypoint, town,       REF_TOWN,                  122, SL_MAX_VERSION),
00154   SLE_CONDVAR(OldWaypoint, town_cn,    SLE_FILE_U8 | SLE_VAR_U16,  12, 88),
00155   SLE_CONDVAR(OldWaypoint, town_cn,    SLE_UINT16,                 89, SL_MAX_VERSION),
00156   SLE_CONDVAR(OldWaypoint, string_id,  SLE_STRINGID,                0, 83),
00157   SLE_CONDSTR(OldWaypoint, name,       SLE_STR, 0,                 84, SL_MAX_VERSION),
00158       SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
00159 
00160   SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
00161   SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
00162   SLE_CONDVAR(OldWaypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
00163   SLE_CONDVAR(OldWaypoint, grfid,      SLE_UINT32,                 17, SL_MAX_VERSION),
00164   SLE_CONDVAR(OldWaypoint, owner,      SLE_UINT8,                 101, SL_MAX_VERSION),
00165 
00166   SLE_END()
00167 };
00168 
00169 static void Load_WAYP()
00170 {
00171   /* Precaution for when loading failed and it didn't get cleared */
00172   _old_waypoints.Clear();
00173 
00174   int index;
00175 
00176   while ((index = SlIterateArray()) != -1) {
00177     OldWaypoint *wp = _old_waypoints.Append();
00178     memset(wp, 0, sizeof(*wp));
00179 
00180     wp->index = index;
00181     SlObject(wp, _old_waypoint_desc);
00182   }
00183 }
00184 
00185 static void Ptrs_WAYP()
00186 {
00187   for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00188     SlObject(wp, _old_waypoint_desc);
00189 
00190     if (IsSavegameVersionBefore(12)) {
00191       wp->town_cn = (wp->string_id & 0xC000) == 0xC000 ? (wp->string_id >> 8) & 0x3F : 0;
00192       wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
00193     } else if (IsSavegameVersionBefore(122)) {
00194       /* Only for versions 12 .. 122 */
00195       if (!Town::IsValidID(wp->town_index)) {
00196         /* Upon a corrupted waypoint we'll likely get here. The next step will be to
00197          * loop over all Ptrs procs to NULL the pointers. However, we don't know
00198          * whether we're in the NULL or "normal" Ptrs proc. So just clear the list
00199          * of old waypoints we constructed and then this waypoint (and the other
00200          * possibly corrupt ones) will not be queried in the NULL Ptrs proc run. */
00201         _old_waypoints.Clear();
00202         SlErrorCorrupt("Referencing invalid Town");
00203       }
00204       wp->town = Town::Get(wp->town_index);
00205     }
00206     if (IsSavegameVersionBefore(84)) {
00207       wp->name = CopyFromOldName(wp->string_id);
00208     }
00209   }
00210 }
00211 
00212 extern const ChunkHandler _waypoint_chunk_handlers[] = {
00213   { 'CHKP', NULL, Load_WAYP, Ptrs_WAYP, NULL, CH_ARRAY | CH_LAST},
00214 };

Generated on Thu Apr 14 00:48:19 2011 for OpenTTD by  doxygen 1.6.1