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