# waypoint_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: waypoint_sl.cpp 19623 2010-04-13 21:32:29Z rubidium $ */
    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 (CheckSavegameVersion(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 = wp->index;
    00077
    00078       if (HasBit(_m[wp->xy].m3, 4)) {
    00079         wp->spec = GetCustomStationSpec(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 < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
    00087         const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
    00088         if (statspec != NULL && statspec->grffile->grfid == wp->grfid && statspec->localidx == wp->localidx) {
    00089           wp->spec = statspec;
    00090           break;
    00091         }
    00092       }
    00093     }
    00094   }
    00095
    00096   /* All saveload conversions have been done. Create the new waypoints! */
    00097   for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
    00098     Waypoint *new_wp = new Waypoint(wp->xy);
    00099     new_wp->town       = wp->town;
    00100     new_wp->town_cn    = wp->town_cn;
    00101     new_wp->name       = wp->name;
    00102     new_wp->delete_ctr = 0; // Just reset delete counter for once.
    00103     new_wp->build_date = wp->build_date;
    00104     new_wp->owner      = wp->owner;
    00105
    00106     new_wp->string_id = STR_SV_STNAME_WAYPOINT;
    00107
    00108     TileIndex t = wp->xy;
    00109     if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && _m[t].m2 == wp->index) {
    00110       /* The tile might've been reserved! */
    00111       bool reserved = !CheckSavegameVersion(100) && HasBit(_m[t].m5, 4);
    00112
    00113       /* The tile really has our waypoint, so reassign the map array */
    00114       MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
    00115       new_wp->facilities |= FACIL_TRAIN;
    00116       new_wp->owner = GetTileOwner(t);
    00117
    00118       SetRailStationReservation(t, reserved);
    00119
    00120       if (wp->spec != NULL) {
    00121         SetCustomStationSpecIndex(t, AllocateSpecToStation(wp->spec, new_wp, true));
    00122       }
    00123       new_wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
    00124     }
    00125
    00126     wp->new_index = new_wp->index;
    00127   }
    00128
    00129   /* Update the orders of vehicles */
    00130   OrderList *ol;
    00131   FOR_ALL_ORDER_LISTS(ol) {
    00132     if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
    00133
    00134     for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
    00135   }
    00136
    00137   Vehicle *v;
    00138   FOR_ALL_VEHICLES(v) {
    00139     if (v->type != VEH_TRAIN) continue;
    00140
    00141     UpdateWaypointOrder(&v->current_order);
    00142   }
    00143
    00144   _old_waypoints.Reset();
    00145 }
    00146
    00147 static const SaveLoad _old_waypoint_desc[] = {
    00148   SLE_CONDVAR(OldWaypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
    00149   SLE_CONDVAR(OldWaypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
    00150   SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16,                 12, 121),
    00151   SLE_CONDREF(OldWaypoint, town,       REF_TOWN,                  122, SL_MAX_VERSION),
    00152   SLE_CONDVAR(OldWaypoint, town_cn,    SLE_FILE_U8 | SLE_VAR_U16,  12, 88),
    00153   SLE_CONDVAR(OldWaypoint, town_cn,    SLE_UINT16,                 89, SL_MAX_VERSION),
    00154   SLE_CONDVAR(OldWaypoint, string_id,  SLE_STRINGID,                0, 83),
    00155   SLE_CONDSTR(OldWaypoint, name,       SLE_STR, 0,                 84, SL_MAX_VERSION),
    00156       SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
    00157
    00158   SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
    00159   SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
    00160   SLE_CONDVAR(OldWaypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
    00161   SLE_CONDVAR(OldWaypoint, grfid,      SLE_UINT32,                 17, SL_MAX_VERSION),
    00162   SLE_CONDVAR(OldWaypoint, owner,      SLE_UINT8,                 101, SL_MAX_VERSION),
    00163
    00164   SLE_END()
    00165 };
    00166
    00167 static void Load_WAYP()
    00168 {
    00169   /* Precaution for when loading failed and it didn't get cleared */
    00170   _old_waypoints.Clear();
    00171
    00172   int index;
    00173
    00174   while ((index = SlIterateArray()) != -1) {
    00175     OldWaypoint *wp = _old_waypoints.Append();
    00176     memset(wp, 0, sizeof(*wp));
    00177
    00178     wp->index = index;
    00179     SlObject(wp, _old_waypoint_desc);
    00180   }
    00181 }
    00182
    00183 static void Ptrs_WAYP()
    00184 {
    00185   for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
    00186     SlObject(wp, _old_waypoint_desc);
    00187
    00188     if (CheckSavegameVersion(12)) {
    00189       wp->town_cn = (wp->string_id & 0xC000) == 0xC000 ? (wp->string_id >> 8) & 0x3F : 0;
    00190       wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
    00191     } else if (CheckSavegameVersion(122)) {
    00192       /* Only for versions 12 .. 122 */
    00193       wp->town = Town::Get(wp->town_index);
    00194     }
    00195     if (CheckSavegameVersion(84)) {
    00196       wp->name = CopyFromOldName(wp->string_id);
    00197     }
    00198   }
    00199 }
    00200
    00201 extern const ChunkHandler _waypoint_chunk_handlers[] = {
    00202   { 'CHKP', NULL, Load_WAYP, Ptrs_WAYP, CH_ARRAY | CH_LAST},
    00203 };