00001
00002
00003
00004
00005
00006
00007
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
00068
00069
00070
00071 if (IsSavegameVersionBefore(17)) {
00072 for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00073 if (wp->delete_ctr != 0) continue;
00074
00075
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)->GetSpec(_m[wp->xy].m4 + 1);
00080 }
00081 }
00082 } else {
00083
00084
00085 for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00086 StationClass* stclass = StationClass::Get(STAT_CLASS_WAYP);
00087 for (uint i = 0; i < stclass->GetSpecCount(); i++) {
00088 const StationSpec *statspec = stclass->GetSpec(i);
00089 if (statspec != NULL && statspec->grf_prop.grffile->grfid == wp->grfid && statspec->grf_prop.local_id == wp->localidx) {
00090 wp->spec = statspec;
00091 break;
00092 }
00093 }
00094 }
00095 }
00096
00097 if (!Waypoint::CanAllocateItem(_old_waypoints.Length())) SlError(STR_ERROR_TOO_MANY_STATIONS_LOADING);
00098
00099
00100 for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00101 Waypoint *new_wp = new Waypoint(wp->xy);
00102 new_wp->town = wp->town;
00103 new_wp->town_cn = wp->town_cn;
00104 new_wp->name = wp->name;
00105 new_wp->delete_ctr = 0;
00106 new_wp->build_date = wp->build_date;
00107 new_wp->owner = wp->owner;
00108
00109 new_wp->string_id = STR_SV_STNAME_WAYPOINT;
00110
00111 TileIndex t = wp->xy;
00112 if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 && _m[t].m2 == wp->index) {
00113
00114 bool reserved = !IsSavegameVersionBefore(100) && HasBit(_m[t].m5, 4);
00115
00116
00117 MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
00118 new_wp->facilities |= FACIL_TRAIN;
00119 new_wp->owner = GetTileOwner(t);
00120
00121 SetRailStationReservation(t, reserved);
00122
00123 if (wp->spec != NULL) {
00124 SetCustomStationSpecIndex(t, AllocateSpecToStation(wp->spec, new_wp, true));
00125 }
00126 new_wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
00127 }
00128
00129 wp->new_index = new_wp->index;
00130 }
00131
00132
00133 OrderList *ol;
00134 FOR_ALL_ORDER_LISTS(ol) {
00135 if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
00136
00137 for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
00138 }
00139
00140 Vehicle *v;
00141 FOR_ALL_VEHICLES(v) {
00142 if (v->type != VEH_TRAIN) continue;
00143
00144 UpdateWaypointOrder(&v->current_order);
00145 }
00146
00147 _old_waypoints.Reset();
00148 }
00149
00150 static const SaveLoad _old_waypoint_desc[] = {
00151 SLE_CONDVAR(OldWaypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
00152 SLE_CONDVAR(OldWaypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION),
00153 SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16, 12, 121),
00154 SLE_CONDREF(OldWaypoint, town, REF_TOWN, 122, SL_MAX_VERSION),
00155 SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88),
00156 SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION),
00157 SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, 0, 83),
00158 SLE_CONDSTR(OldWaypoint, name, SLE_STR, 0, 84, SL_MAX_VERSION),
00159 SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
00160
00161 SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
00162 SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION),
00163 SLE_CONDVAR(OldWaypoint, localidx, SLE_UINT8, 3, SL_MAX_VERSION),
00164 SLE_CONDVAR(OldWaypoint, grfid, SLE_UINT32, 17, SL_MAX_VERSION),
00165 SLE_CONDVAR(OldWaypoint, owner, SLE_UINT8, 101, SL_MAX_VERSION),
00166
00167 SLE_END()
00168 };
00169
00170 static void Load_WAYP()
00171 {
00172
00173 _old_waypoints.Clear();
00174
00175 int index;
00176
00177 while ((index = SlIterateArray()) != -1) {
00178 OldWaypoint *wp = _old_waypoints.Append();
00179 memset(wp, 0, sizeof(*wp));
00180
00181 wp->index = index;
00182 SlObject(wp, _old_waypoint_desc);
00183 }
00184 }
00185
00186 static void Ptrs_WAYP()
00187 {
00188 for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
00189 SlObject(wp, _old_waypoint_desc);
00190
00191 if (IsSavegameVersionBefore(12)) {
00192 wp->town_cn = (wp->string_id & 0xC000) == 0xC000 ? (wp->string_id >> 8) & 0x3F : 0;
00193 wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
00194 } else if (IsSavegameVersionBefore(122)) {
00195
00196 if (!Town::IsValidID(wp->town_index)) {
00197
00198
00199
00200
00201
00202 _old_waypoints.Clear();
00203 SlErrorCorrupt("Referencing invalid Town");
00204 }
00205 wp->town = Town::Get(wp->town_index);
00206 }
00207 if (IsSavegameVersionBefore(84)) {
00208 wp->name = CopyFromOldName(wp->string_id);
00209 }
00210 }
00211 }
00212
00213 extern const ChunkHandler _waypoint_chunk_handlers[] = {
00214 { 'CHKP', NULL, Load_WAYP, Ptrs_WAYP, NULL, CH_ARRAY | CH_LAST},
00215 };