Update: Translations from eints
[openttd-github.git] / src / saveload / waypoint_sl.cpp
blob9dc9de2376c4420cc999a053b15c58098933661c
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file waypoint_sl.cpp Code handling saving and loading of waypoints */
10 #include "../stdafx.h"
11 #include "../waypoint_base.h"
12 #include "../debug.h"
13 #include "../newgrf_station.h"
14 #include "../vehicle_base.h"
15 #include "../town.h"
16 #include "../newgrf.h"
17 #include "../timer/timer_game_calendar.h"
19 #include "table/strings.h"
21 #include "saveload_internal.h"
23 #include "../safeguards.h"
25 /** Helper structure to convert from the old waypoint system. */
26 struct OldWaypoint {
27 size_t index;
28 TileIndex xy;
29 TownID town_index;
30 Town *town;
31 uint16_t town_cn;
32 StringID string_id;
33 std::string name;
34 uint8_t delete_ctr;
35 TimerGameCalendar::Date build_date;
36 uint8_t localidx;
37 uint32_t grfid;
38 const StationSpec *spec;
39 Owner owner;
41 size_t new_index;
44 /** Temporary array with old waypoints. */
45 static std::vector<OldWaypoint> _old_waypoints;
47 /**
48 * Update the waypoint orders to get the new waypoint ID.
49 * @param o the order 'list' to check.
51 static void UpdateWaypointOrder(Order *o)
53 if (!o->IsType(OT_GOTO_WAYPOINT)) return;
55 for (OldWaypoint &wp : _old_waypoints) {
56 if (wp.index != o->GetDestination()) continue;
58 o->SetDestination((DestinationID)wp.new_index);
59 return;
63 /**
64 * Perform all steps to upgrade from the old waypoints to the new version
65 * that uses station. This includes some old saveload mechanics.
67 void MoveWaypointsToBaseStations()
69 /* In version 17, ground type is moved from m2 to m4 for depots and
70 * waypoints to make way for storing the index in m2. The custom graphics
71 * id which was stored in m4 is now saved as a grf/id reference in the
72 * waypoint struct. */
73 if (IsSavegameVersionBefore(SLV_17)) {
74 for (OldWaypoint &wp : _old_waypoints) {
75 if (wp.delete_ctr != 0) continue; // The waypoint was deleted
77 /* Waypoint indices were not added to the map prior to this. */
78 Tile tile = wp.xy;
79 tile.m2() = (StationID)wp.index;
81 if (HasBit(tile.m3(), 4)) {
82 wp.spec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(tile.m4() + 1);
85 } else {
86 /* As of version 17, we recalculate the custom graphic ID of waypoints
87 * from the GRF ID / station index. */
88 for (OldWaypoint &wp : _old_waypoints) {
89 const auto specs = StationClass::Get(STAT_CLASS_WAYP)->Specs();
90 auto found = std::find_if(std::begin(specs), std::end(specs), [&wp](const StationSpec *spec) { return spec != nullptr && spec->grf_prop.grffile->grfid == wp.grfid && spec->grf_prop.local_id == wp.localidx; });
91 if (found != std::end(specs)) wp.spec = *found;
95 if (!Waypoint::CanAllocateItem(_old_waypoints.size())) SlError(STR_ERROR_TOO_MANY_STATIONS_LOADING);
97 /* All saveload conversions have been done. Create the new waypoints! */
98 for (OldWaypoint &wp : _old_waypoints) {
99 TileIndex t = wp.xy;
100 /* Sometimes waypoint (sign) locations became disconnected from their actual location in
101 * the map array. If this is the case, try to locate the actual location in the map array */
102 if (!IsTileType(t, MP_RAILWAY) || GetRailTileType(t) != 2 /* RAIL_TILE_WAYPOINT */ || Tile(t).m2() != wp.index) {
103 Debug(sl, 0, "Found waypoint tile {} with invalid position", t);
104 for (t = 0; t < Map::Size(); t++) {
105 if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && Tile(t).m2() == wp.index) {
106 Debug(sl, 0, "Found actual waypoint position at {}", t);
107 break;
111 if (t == Map::Size()) {
112 SlErrorCorrupt("Waypoint with invalid tile");
115 Waypoint *new_wp = new Waypoint(t);
116 new_wp->town = wp.town;
117 new_wp->town_cn = wp.town_cn;
118 new_wp->name = wp.name;
119 new_wp->delete_ctr = 0; // Just reset delete counter for once.
120 new_wp->build_date = wp.build_date;
121 new_wp->owner = wp.owner;
122 new_wp->string_id = STR_SV_STNAME_WAYPOINT;
124 /* The tile might've been reserved! */
125 Tile tile(t);
126 bool reserved = !IsSavegameVersionBefore(SLV_100) && HasBit(tile.m5(), 4);
128 /* The tile really has our waypoint, so reassign the map array */
129 MakeRailWaypoint(tile, GetTileOwner(tile), new_wp->index, (Axis)GB(tile.m5(), 0, 1), 0, GetRailType(tile));
130 new_wp->facilities |= FACIL_TRAIN;
131 new_wp->owner = GetTileOwner(tile);
133 SetRailStationReservation(tile, reserved);
135 if (wp.spec != nullptr) {
136 SetCustomStationSpecIndex(tile, AllocateSpecToStation(wp.spec, new_wp, true));
138 new_wp->rect.BeforeAddTile(tile, StationRect::ADD_FORCE);
140 wp.new_index = new_wp->index;
143 /* Update the orders of vehicles */
144 for (OrderList *ol : OrderList::Iterate()) {
145 if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
147 for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o);
150 for (Vehicle *v : Vehicle::Iterate()) {
151 if (v->type != VEH_TRAIN) continue;
153 UpdateWaypointOrder(&v->current_order);
156 ResetOldWaypoints();
159 void ResetOldWaypoints()
161 _old_waypoints.clear();
162 _old_waypoints.shrink_to_fit();
165 static const SaveLoad _old_waypoint_desc[] = {
166 SLE_CONDVAR(OldWaypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
167 SLE_CONDVAR(OldWaypoint, xy, SLE_UINT32, SLV_6, SL_MAX_VERSION),
168 SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16, SLV_12, SLV_122),
169 SLE_CONDREF(OldWaypoint, town, REF_TOWN, SLV_122, SL_MAX_VERSION),
170 SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, SLV_12, SLV_89),
171 SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, SLV_89, SL_MAX_VERSION),
172 SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, SL_MIN_VERSION, SLV_84),
173 SLE_CONDSSTR(OldWaypoint, name, SLE_STR, SLV_84, SL_MAX_VERSION),
174 SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
176 SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31),
177 SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
178 SLE_CONDVAR(OldWaypoint, localidx, SLE_UINT8, SLV_3, SL_MAX_VERSION),
179 SLE_CONDVAR(OldWaypoint, grfid, SLE_UINT32, SLV_17, SL_MAX_VERSION),
180 SLE_CONDVAR(OldWaypoint, owner, SLE_UINT8, SLV_101, SL_MAX_VERSION),
183 struct CHKPChunkHandler : ChunkHandler {
184 CHKPChunkHandler() : ChunkHandler('CHKP', CH_READONLY) {}
186 void Load() const override
188 /* Precaution for when loading failed and it didn't get cleared */
189 ResetOldWaypoints();
191 int index;
193 while ((index = SlIterateArray()) != -1) {
194 OldWaypoint *wp = &_old_waypoints.emplace_back();
196 wp->index = index;
197 SlObject(wp, _old_waypoint_desc);
201 void FixPointers() const override
203 for (OldWaypoint &wp : _old_waypoints) {
204 SlObject(&wp, _old_waypoint_desc);
206 if (IsSavegameVersionBefore(SLV_12)) {
207 wp.town_cn = (wp.string_id & 0xC000) == 0xC000 ? (wp.string_id >> 8) & 0x3F : 0;
208 wp.town = ClosestTownFromTile(wp.xy, UINT_MAX);
209 } else if (IsSavegameVersionBefore(SLV_122)) {
210 /* Only for versions 12 .. 122 */
211 if (!Town::IsValidID(wp.town_index)) {
212 /* Upon a corrupted waypoint we'll likely get here. The next step will be to
213 * loop over all Ptrs procs to nullptr the pointers. However, we don't know
214 * whether we're in the nullptr or "normal" Ptrs proc. So just clear the list
215 * of old waypoints we constructed and then this waypoint (and the other
216 * possibly corrupt ones) will not be queried in the nullptr Ptrs proc run. */
217 _old_waypoints.clear();
218 SlErrorCorrupt("Referencing invalid Town");
220 wp.town = Town::Get(wp.town_index);
222 if (IsSavegameVersionBefore(SLV_84)) {
223 wp.name = CopyFromOldName(wp.string_id);
229 static const CHKPChunkHandler CHKP;
230 static const ChunkHandlerRef waypoint_chunk_handlers[] = {
231 CHKP,
234 extern const ChunkHandlerTable _waypoint_chunk_handlers(waypoint_chunk_handlers);