OpenTTD
waypoint_sl.cpp
Go to the documentation of this file.
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  */
7 
10 #include "../stdafx.h"
11 #include "../waypoint_base.h"
12 #include "../newgrf_station.h"
13 #include "../vehicle_base.h"
14 #include "../town.h"
15 #include "../newgrf.h"
16 
17 #include "table/strings.h"
18 
19 #include "saveload_internal.h"
20 
21 #include "../safeguards.h"
22 
24 struct OldWaypoint {
25  size_t index;
26  TileIndex xy;
27  TownID town_index;
28  Town *town;
29  uint16 town_cn;
30  StringID string_id;
31  char *name;
32  uint8 delete_ctr;
33  Date build_date;
34  uint8 localidx;
35  uint32 grfid;
36  const StationSpec *spec;
37  Owner owner;
38 
39  size_t new_index;
40 };
41 
43 static std::vector<OldWaypoint> _old_waypoints;
44 
49 static void UpdateWaypointOrder(Order *o)
50 {
51  if (!o->IsType(OT_GOTO_WAYPOINT)) return;
52 
53  for (OldWaypoint &wp : _old_waypoints) {
54  if (wp.index != o->GetDestination()) continue;
55 
56  o->SetDestination((DestinationID)wp.new_index);
57  return;
58  }
59 }
60 
66 {
67  /* In version 17, ground type is moved from m2 to m4 for depots and
68  * waypoints to make way for storing the index in m2. The custom graphics
69  * id which was stored in m4 is now saved as a grf/id reference in the
70  * waypoint struct. */
72  for (OldWaypoint &wp : _old_waypoints) {
73  if (wp.delete_ctr != 0) continue; // The waypoint was deleted
74 
75  /* Waypoint indices were not added to the map prior to this. */
76  _m[wp.xy].m2 = (StationID)wp.index;
77 
78  if (HasBit(_m[wp.xy].m3, 4)) {
79  wp.spec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(_m[wp.xy].m4 + 1);
80  }
81  }
82  } else {
83  /* As of version 17, we recalculate the custom graphic ID of waypoints
84  * from the GRF ID / station index. */
85  for (OldWaypoint &wp : _old_waypoints) {
87  for (uint i = 0; i < stclass->GetSpecCount(); i++) {
88  const StationSpec *statspec = stclass->GetSpec(i);
89  if (statspec != nullptr && statspec->grf_prop.grffile->grfid == wp.grfid && statspec->grf_prop.local_id == wp.localidx) {
90  wp.spec = statspec;
91  break;
92  }
93  }
94  }
95  }
96 
97  if (!Waypoint::CanAllocateItem(_old_waypoints.size())) SlError(STR_ERROR_TOO_MANY_STATIONS_LOADING);
98 
99  /* All saveload conversions have been done. Create the new waypoints! */
100  for (OldWaypoint &wp : _old_waypoints) {
101  Waypoint *new_wp = new Waypoint(wp.xy);
102  new_wp->town = wp.town;
103  new_wp->town_cn = wp.town_cn;
104  new_wp->name = wp.name;
105  new_wp->delete_ctr = 0; // Just reset delete counter for once.
106  new_wp->build_date = wp.build_date;
107  new_wp->owner = wp.owner;
108 
109  new_wp->string_id = STR_SV_STNAME_WAYPOINT;
110 
111  TileIndex t = wp.xy;
112  if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && _m[t].m2 == wp.index) {
113  /* The tile might've been reserved! */
114  bool reserved = !IsSavegameVersionBefore(SLV_100) && HasBit(_m[t].m5, 4);
115 
116  /* The tile really has our waypoint, so reassign the map array */
117  MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
118  new_wp->facilities |= FACIL_TRAIN;
119  new_wp->owner = GetTileOwner(t);
120 
121  SetRailStationReservation(t, reserved);
122 
123  if (wp.spec != nullptr) {
124  SetCustomStationSpecIndex(t, AllocateSpecToStation(wp.spec, new_wp, true));
125  }
126  new_wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
127  }
128 
129  wp.new_index = new_wp->index;
130  }
131 
132  /* Update the orders of vehicles */
133  for (OrderList *ol : OrderList::Iterate()) {
134  if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
135 
136  for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o);
137  }
138 
139  for (Vehicle *v : Vehicle::Iterate()) {
140  if (v->type != VEH_TRAIN) continue;
141 
142  UpdateWaypointOrder(&v->current_order);
143  }
144 
145  _old_waypoints.clear();
146  _old_waypoints.shrink_to_fit();
147 }
148 
149 static const SaveLoad _old_waypoint_desc[] = {
150  SLE_CONDVAR(OldWaypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
151  SLE_CONDVAR(OldWaypoint, xy, SLE_UINT32, SLV_6, SL_MAX_VERSION),
152  SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16, SLV_12, SLV_122),
154  SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, SLV_12, SLV_89),
155  SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, SLV_89, SL_MAX_VERSION),
156  SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, SL_MIN_VERSION, SLV_84),
158  SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
159 
160  SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31),
161  SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
162  SLE_CONDVAR(OldWaypoint, localidx, SLE_UINT8, SLV_3, SL_MAX_VERSION),
163  SLE_CONDVAR(OldWaypoint, grfid, SLE_UINT32, SLV_17, SL_MAX_VERSION),
164  SLE_CONDVAR(OldWaypoint, owner, SLE_UINT8, SLV_101, SL_MAX_VERSION),
165 
166  SLE_END()
167 };
168 
169 static void Load_WAYP()
170 {
171  /* Precaution for when loading failed and it didn't get cleared */
172  _old_waypoints.clear();
173 
174  int index;
175 
176  while ((index = SlIterateArray()) != -1) {
177  /*C++17: OldWaypoint *wp = &*/ _old_waypoints.emplace_back();
178  OldWaypoint *wp = &_old_waypoints.back();
179  memset(wp, 0, sizeof(*wp));
180 
181  wp->index = index;
182  SlObject(wp, _old_waypoint_desc);
183  }
184 }
185 
186 static void Ptrs_WAYP()
187 {
188  for (OldWaypoint &wp : _old_waypoints) {
189  SlObject(&wp, _old_waypoint_desc);
190 
192  wp.town_cn = (wp.string_id & 0xC000) == 0xC000 ? (wp.string_id >> 8) & 0x3F : 0;
193  wp.town = ClosestTownFromTile(wp.xy, UINT_MAX);
194  } else if (IsSavegameVersionBefore(SLV_122)) {
195  /* Only for versions 12 .. 122 */
196  if (!Town::IsValidID(wp.town_index)) {
197  /* Upon a corrupted waypoint we'll likely get here. The next step will be to
198  * loop over all Ptrs procs to nullptr the pointers. However, we don't know
199  * whether we're in the nullptr or "normal" Ptrs proc. So just clear the list
200  * of old waypoints we constructed and then this waypoint (and the other
201  * possibly corrupt ones) will not be queried in the nullptr Ptrs proc run. */
202  _old_waypoints.clear();
203  SlErrorCorrupt("Referencing invalid Town");
204  }
205  wp.town = Town::Get(wp.town_index);
206  }
208  wp.name = CopyFromOldName(wp.string_id);
209  }
210  }
211 }
212 
213 extern const ChunkHandler _waypoint_chunk_handlers[] = {
214  { 'CHKP', nullptr, Load_WAYP, Ptrs_WAYP, nullptr, CH_ARRAY | CH_LAST},
215 };
Owner
Enum for all companies/owners.
Definition: company_type.h:18
StationFacility facilities
The facilities that this station has.
static void SetCustomStationSpecIndex(TileIndex t, byte specindex)
Set the custom station spec for this tile.
Definition: station_map.h:481
uint16 town_cn
The N-1th waypoint for this town (consecutive number)
Definition: waypoint_base.h:17
static void UpdateWaypointOrder(Order *o)
Update the waypoint orders to get the new waypoint ID.
Definition: waypoint_sl.cpp:49
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
Waypoint class.
12.1 2046
Definition: saveload.h:54
#define SLE_CONDSTR(base, variable, type, length, from, to)
Storage of a string in some savegame versions.
Definition: saveload.h:566
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:544
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:354
Train vehicle type.
Definition: vehicle_type.h:24
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
uint GetSpecCount() const
Get the number of allocated specs within the class.
Definition: newgrf_class.h:44
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:20
Tile * _m
Tiles of the map.
Definition: map.cpp:30
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:326
Station specification.
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:59
Vehicle data structure.
Definition: vehicle_base.h:210
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
101 14233
Definition: saveload.h:163
Load/save a reference to a town.
Definition: saveload.h:374
Representation of a waypoint.
Definition: waypoint_base.h:16
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:405
A railway.
Definition: tile_type.h:42
Town * town
The town this station is associated with.
17.0 3212 17.1 3218
Definition: saveload.h:61
Helper structure to convert from the old waypoint system.
Definition: waypoint_sl.cpp:24
byte delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:49
122 16855
Definition: saveload.h:188
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold...
Definition: town_cmd.cpp:3488
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:19
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:534
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
static void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
Make the given tile a rail waypoint tile.
Definition: station_map.h:573
Highest possible saveload version.
Definition: saveload.h:305
First savegame version.
Definition: saveload.h:30
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:61
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
GRFFilePropsBase< NUM_CARGO+3 > grf_prop
Properties related the the grf file.
int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
Allocate a StationSpec to a Station.
void MoveWaypointsToBaseStations()
Perform all steps to upgrade from the old waypoints to the new version that uses station.
Definition: waypoint_sl.cpp:65
84 11822
Definition: saveload.h:142
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
6.0 1721 6.1 1768
Definition: saveload.h:45
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Handlers and description of chunk.
Definition: saveload.h:356
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:36
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:651
3.x lost
Definition: saveload.h:35
89 12160
Definition: saveload.h:148
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:94
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
Town data structure.
Definition: town.h:53
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:261
Owner owner
The owner of this station.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
uint16 local_id
id defined by the grf file for this entity
void SetDestination(DestinationID destination)
Sets the destination of this order.
Definition: order_base.h:101
int32 Date
The type to store our dates in.
Definition: date_type.h:14
SaveLoad type struct.
Definition: saveload.h:496
const struct GRFFile * grffile
grf file that introduced this entity
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:594
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:620
Station with train station.
Definition: station_type.h:52
31 5999
Definition: saveload.h:79
char * name
Custom name.
StringID string_id
Default name (town area) of station.
byte m3
General purpose.
Definition: map_type.h:22
100 13952
Definition: saveload.h:162
Declaration of functions used in more save/load files.
Axis
Allow incrementing of DiagDirDiff variables.
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
static std::vector< OldWaypoint > _old_waypoints
Temporary array with old waypoints.
Definition: waypoint_sl.cpp:43
Date build_date
Date of construction.
Last chunk in this array.
Definition: saveload.h:391
byte m4
General purpose.
Definition: map_type.h:23