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, _m[wp->xy].m4 + 1);
00080 }
00081 }
00082 } else {
00083
00084
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
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;
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 && _m[t].m2 == wp->index) {
00112
00113 bool reserved = !IsSavegameVersionBefore(100) && HasBit(_m[t].m5, 4);
00114
00115
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
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
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
00195 if (!Town::IsValidID(wp->town_index)) {
00196
00197
00198
00199
00200
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 };