OpenTTD
waypoint_sl.cpp
Go to the documentation of this file.
1 /* $Id: waypoint_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../waypoint_base.h"
14 #include "../newgrf_station.h"
15 #include "../vehicle_base.h"
16 #include "../town.h"
17 #include "../newgrf.h"
18 
19 #include "table/strings.h"
20 
21 #include "saveload_internal.h"
22 
23 #include "../safeguards.h"
24 
26 struct OldWaypoint {
27  size_t index;
28  TileIndex xy;
29  TownID town_index;
30  Town *town;
31  uint16 town_cn;
32  StringID string_id;
33  char *name;
34  uint8 delete_ctr;
35  Date build_date;
36  uint8 localidx;
37  uint32 grfid;
38  const StationSpec *spec;
39  OwnerByte owner;
40 
41  size_t new_index;
42 };
43 
46 
51 static void UpdateWaypointOrder(Order *o)
52 {
53  if (!o->IsType(OT_GOTO_WAYPOINT)) return;
54 
55  for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
56  if (wp->index != o->GetDestination()) continue;
57 
58  o->SetDestination((DestinationID)wp->new_index);
59  return;
60  }
61 }
62 
68 {
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(17)) {
74  for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
75  if (wp->delete_ctr != 0) continue; // The waypoint was deleted
76 
77  /* Waypoint indices were not added to the map prior to this. */
78  _m[wp->xy].m2 = (StationID)wp->index;
79 
80  if (HasBit(_m[wp->xy].m3, 4)) {
81  wp->spec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(_m[wp->xy].m4 + 1);
82  }
83  }
84  } else {
85  /* As of version 17, we recalculate the custom graphic ID of waypoints
86  * from the GRF ID / station index. */
87  for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
89  for (uint i = 0; i < stclass->GetSpecCount(); i++) {
90  const StationSpec *statspec = stclass->GetSpec(i);
91  if (statspec != NULL && statspec->grf_prop.grffile->grfid == wp->grfid && statspec->grf_prop.local_id == wp->localidx) {
92  wp->spec = statspec;
93  break;
94  }
95  }
96  }
97  }
98 
99  if (!Waypoint::CanAllocateItem(_old_waypoints.Length())) SlError(STR_ERROR_TOO_MANY_STATIONS_LOADING);
100 
101  /* All saveload conversions have been done. Create the new waypoints! */
102  for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
103  Waypoint *new_wp = new Waypoint(wp->xy);
104  new_wp->town = wp->town;
105  new_wp->town_cn = wp->town_cn;
106  new_wp->name = wp->name;
107  new_wp->delete_ctr = 0; // Just reset delete counter for once.
108  new_wp->build_date = wp->build_date;
109  new_wp->owner = wp->owner;
110 
111  new_wp->string_id = STR_SV_STNAME_WAYPOINT;
112 
113  TileIndex t = wp->xy;
114  if (IsTileType(t, MP_RAILWAY) && GetRailTileType(t) == 2 /* RAIL_TILE_WAYPOINT */ && _m[t].m2 == wp->index) {
115  /* The tile might've been reserved! */
116  bool reserved = !IsSavegameVersionBefore(100) && HasBit(_m[t].m5, 4);
117 
118  /* The tile really has our waypoint, so reassign the map array */
119  MakeRailWaypoint(t, GetTileOwner(t), new_wp->index, (Axis)GB(_m[t].m5, 0, 1), 0, GetRailType(t));
120  new_wp->facilities |= FACIL_TRAIN;
121  new_wp->owner = GetTileOwner(t);
122 
123  SetRailStationReservation(t, reserved);
124 
125  if (wp->spec != NULL) {
126  SetCustomStationSpecIndex(t, AllocateSpecToStation(wp->spec, new_wp, true));
127  }
128  new_wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
129  }
130 
131  wp->new_index = new_wp->index;
132  }
133 
134  /* Update the orders of vehicles */
135  OrderList *ol;
136  FOR_ALL_ORDER_LISTS(ol) {
137  if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
138 
139  for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o);
140  }
141 
142  Vehicle *v;
143  FOR_ALL_VEHICLES(v) {
144  if (v->type != VEH_TRAIN) continue;
145 
147  }
148 
149  _old_waypoints.Reset();
150 }
151 
152 static const SaveLoad _old_waypoint_desc[] = {
153  SLE_CONDVAR(OldWaypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
154  SLE_CONDVAR(OldWaypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION),
155  SLE_CONDVAR(OldWaypoint, town_index, SLE_UINT16, 12, 121),
157  SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88),
158  SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION),
159  SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, 0, 83),
161  SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
162 
163  SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
164  SLE_CONDVAR(OldWaypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION),
165  SLE_CONDVAR(OldWaypoint, localidx, SLE_UINT8, 3, SL_MAX_VERSION),
166  SLE_CONDVAR(OldWaypoint, grfid, SLE_UINT32, 17, SL_MAX_VERSION),
167  SLE_CONDVAR(OldWaypoint, owner, SLE_UINT8, 101, SL_MAX_VERSION),
168 
169  SLE_END()
170 };
171 
172 static void Load_WAYP()
173 {
174  /* Precaution for when loading failed and it didn't get cleared */
175  _old_waypoints.Clear();
176 
177  int index;
178 
179  while ((index = SlIterateArray()) != -1) {
180  OldWaypoint *wp = _old_waypoints.Append();
181  memset(wp, 0, sizeof(*wp));
182 
183  wp->index = index;
184  SlObject(wp, _old_waypoint_desc);
185  }
186 }
187 
188 static void Ptrs_WAYP()
189 {
190  for (OldWaypoint *wp = _old_waypoints.Begin(); wp != _old_waypoints.End(); wp++) {
191  SlObject(wp, _old_waypoint_desc);
192 
193  if (IsSavegameVersionBefore(12)) {
194  wp->town_cn = (wp->string_id & 0xC000) == 0xC000 ? (wp->string_id >> 8) & 0x3F : 0;
195  wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
196  } else if (IsSavegameVersionBefore(122)) {
197  /* Only for versions 12 .. 122 */
198  if (!Town::IsValidID(wp->town_index)) {
199  /* Upon a corrupted waypoint we'll likely get here. The next step will be to
200  * loop over all Ptrs procs to NULL the pointers. However, we don't know
201  * whether we're in the NULL or "normal" Ptrs proc. So just clear the list
202  * of old waypoints we constructed and then this waypoint (and the other
203  * possibly corrupt ones) will not be queried in the NULL Ptrs proc run. */
204  _old_waypoints.Clear();
205  SlErrorCorrupt("Referencing invalid Town");
206  }
207  wp->town = Town::Get(wp->town_index);
208  }
209  if (IsSavegameVersionBefore(84)) {
210  wp->name = CopyFromOldName(wp->string_id);
211  }
212  }
213 }
214 
215 extern const ChunkHandler _waypoint_chunk_handlers[] = {
216  { 'CHKP', NULL, Load_WAYP, Ptrs_WAYP, NULL, CH_ARRAY | CH_LAST},
217 };
static void SetCustomStationSpecIndex(TileIndex t, byte specindex)
Set the custom station spec for this tile.
Definition: station_map.h:482
uint16 town_cn
The N-1th waypoint for this town (consecutive number)
Definition: waypoint_base.h:19
static void UpdateWaypointOrder(Order *o)
Update the waypoint orders to get the new waypoint ID.
Definition: waypoint_sl.cpp:51
#define SLE_CONDSTR(base, variable, type, length, from, to)
Storage of a string in some savegame versions.
Definition: saveload.h:278
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:256
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
uint GetSpecCount() const
Get the number of allocated specs within the class.
Definition: newgrf_class.h:46
void Reset()
Remove all items from the list and free allocated memory.
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
Tile * _m
Tiles of the map.
Definition: map.cpp:32
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:533
Station specification.
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:61
Vehicle data structure.
Definition: vehicle_base.h:212
void Clear()
Remove all items from the list.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
const T * Begin() const
Get the pointer to the first item (const)
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Load/save a reference to a town.
Definition: saveload.h:84
Representation of a waypoint.
Definition: waypoint_base.h:18
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:406
Simple vector template class.
A railway.
Definition: tile_type.h:44
Town * town
The town this station is associated with.
Vehicle * GetFirstSharedVehicle() const
Get the first vehicle of this vehicle chain.
Definition: order_base.h:337
const T * End() const
Get the pointer behind the last valid item (const)
Waypoint class.
Helper structure to convert from the old waypoint system.
Definition: waypoint_sl.cpp:26
T * Append(uint to_add=1)
Append an item and return it.
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 NULL, end of list.
Definition: order_base.h:51
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:252
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:3239
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:21
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
uint Length() const
Get the number of items in the list.
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:572
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:171
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.
Order * GetFirstOrder() const
Get the first order of the order chain.
Definition: order_base.h:290
StationFacilityByte facilities
The facilities that this station has.
void MoveWaypointsToBaseStations()
Perform all steps to upgrade from the old waypoints to the new version that uses station.
Definition: waypoint_sl.cpp:67
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
Handlers and description of chunk.
Definition: saveload.h:66
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
OwnerByte owner
The owner of this station.
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:96
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
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:1612
Town data structure.
Definition: town.h:55
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Station with train station.
Definition: station_type.h:55
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
static SmallVector< OldWaypoint, 16 > _old_waypoints
Temporary array with old waypoints.
Definition: waypoint_sl.cpp:45
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
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:103
int32 Date
The type to store our dates in.
Definition: date_type.h:16
SaveLoad type struct.
Definition: saveload.h:208
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:296
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:986
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
char * name
Custom name.
StringID string_id
Default name (town area) of station.
byte m3
General purpose.
Definition: map_type.h:24
Declaration of functions used in more save/load files.
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:54
Axis
Allow incrementing of DiagDirDiff variables.
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:318
Date build_date
Date of construction.
Last chunk in this array.
Definition: saveload.h:104
Train vehicle type.
Definition: vehicle_type.h:24
byte m4
General purpose.
Definition: map_type.h:25