# OpenTTD: src/saveload/waypoint_sl.cpp Source File

# waypoint_sl.cpp

Go to the documentation of this file.

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