OpenTTD Source  1.11.2
station_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 "../station_base.h"
12 #include "../waypoint_base.h"
13 #include "../roadstop_base.h"
14 #include "../vehicle_base.h"
15 #include "../newgrf_station.h"
16 
17 #include "saveload.h"
18 #include "table/strings.h"
19 
20 #include "../safeguards.h"
21 
26 static void UpdateWaypointOrder(Order *o)
27 {
28  if (!o->IsType(OT_GOTO_STATION)) return;
29 
30  const Station *st = Station::Get(o->GetDestination());
31  if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) return;
32 
34 }
35 
41 {
42  /* Buoy orders become waypoint orders */
43  for (OrderList *ol : OrderList::Iterate()) {
44  VehicleType vt = ol->GetFirstSharedVehicle()->type;
45  if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
46 
47  for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o);
48  }
49 
50  for (Vehicle *v : Vehicle::Iterate()) {
51  VehicleType vt = v->type;
52  if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
53 
54  UpdateWaypointOrder(&v->current_order);
55  }
56 
57  /* Now make the stations waypoints */
58  for (Station *st : Station::Iterate()) {
59  if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue;
60 
61  StationID index = st->index;
62  TileIndex xy = st->xy;
63  Town *town = st->town;
64  StringID string_id = st->string_id;
65  std::string name = st->name;
66  Date build_date = st->build_date;
67  /* TTDPatch could use "buoys with rail station" for rail waypoints */
68  bool train = st->train_station.tile != INVALID_TILE;
69  TileArea train_st = st->train_station;
70 
71  /* Delete the station, so we can make it a real waypoint. */
72  delete st;
73 
74  /* Stations and waypoints are in the same pool, so if a station
75  * is deleted there must be place for a Waypoint. */
76  assert(Waypoint::CanAllocateItem());
77  Waypoint *wp = new (index) Waypoint(xy);
78  wp->town = town;
79  wp->string_id = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY;
80  wp->name = name;
81  wp->delete_ctr = 0; // Just reset delete counter for once.
82  wp->build_date = build_date;
83  wp->owner = train ? GetTileOwner(xy) : OWNER_NONE;
84 
85  if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY;
86 
87  if (train) {
88  /* When we make a rail waypoint of the station, convert the map as well. */
89  TILE_AREA_LOOP(t, train_st) {
90  if (!IsTileType(t, MP_STATION) || GetStationIndex(t) != index) continue;
91 
92  SB(_me[t].m6, 3, 3, STATION_WAYPOINT);
93  wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
94  }
95 
96  wp->train_station = train_st;
97  wp->facilities |= FACIL_TRAIN;
98  } else if (IsBuoyTile(xy) && GetStationIndex(xy) == index) {
99  wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE);
100  wp->facilities |= FACIL_DOCK;
101  }
102  }
103 }
104 
105 void AfterLoadStations()
106 {
107  /* Update the speclists of all stations to point to the currently loaded custom stations. */
108  for (BaseStation *st : BaseStation::Iterate()) {
109  for (uint i = 0; i < st->num_specs; i++) {
110  if (st->speclist[i].grfid == 0) continue;
111 
112  st->speclist[i].spec = StationClass::GetByGrf(st->speclist[i].grfid, st->speclist[i].localidx, nullptr);
113  }
114 
115  if (Station::IsExpected(st)) {
116  Station *sta = Station::From(st);
117  for (const RoadStop *rs = sta->bus_stops; rs != nullptr; rs = rs->next) sta->bus_station.Add(rs->xy);
118  for (const RoadStop *rs = sta->truck_stops; rs != nullptr; rs = rs->next) sta->truck_station.Add(rs->xy);
119  }
120 
122  }
123 }
124 
129 {
130  /* First construct the drive through entries */
131  for (RoadStop *rs : RoadStop::Iterate()) {
132  if (IsDriveThroughStopTile(rs->xy)) rs->MakeDriveThrough();
133  }
134  /* And then rebuild the data in those entries */
135  for (RoadStop *rs : RoadStop::Iterate()) {
136  if (!HasBit(rs->status, RoadStop::RSSFB_BASE_ENTRY)) continue;
137 
138  rs->GetEntry(DIAGDIR_NE)->Rebuild(rs);
139  rs->GetEntry(DIAGDIR_NW)->Rebuild(rs);
140  }
141 }
142 
143 static const SaveLoad _roadstop_desc[] = {
144  SLE_VAR(RoadStop, xy, SLE_UINT32),
146  SLE_VAR(RoadStop, status, SLE_UINT8),
147  /* Index was saved in some versions, but this is not needed */
151 
154 
157 
158  SLE_END()
159 };
160 
161 static const SaveLoad _old_station_desc[] = {
162  SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
163  SLE_CONDVAR(Station, xy, SLE_UINT32, SLV_6, SL_MAX_VERSION),
165  SLE_CONDVAR(Station, train_station.tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
166  SLE_CONDVAR(Station, train_station.tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
167  SLE_CONDVAR(Station, airport.tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
168  SLE_CONDVAR(Station, airport.tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
171  SLE_REF(Station, town, REF_TOWN),
172  SLE_VAR(Station, train_station.w, SLE_FILE_U8 | SLE_VAR_U16),
173  SLE_CONDVAR(Station, train_station.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_2, SL_MAX_VERSION),
174 
176 
177  SLE_VAR(Station, string_id, SLE_STRINGID),
179  SLE_CONDVAR(Station, indtype, SLE_UINT8, SLV_103, SL_MAX_VERSION),
180  SLE_CONDVAR(Station, had_vehicle_of_type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_122),
181  SLE_CONDVAR(Station, had_vehicle_of_type, SLE_UINT8, SLV_122, SL_MAX_VERSION),
182 
183  SLE_VAR(Station, time_since_load, SLE_UINT8),
184  SLE_VAR(Station, time_since_unload, SLE_UINT8),
185  SLE_VAR(Station, delete_ctr, SLE_UINT8),
186  SLE_VAR(Station, owner, SLE_UINT8),
187  SLE_VAR(Station, facilities, SLE_UINT8),
188  SLE_VAR(Station, airport.type, SLE_UINT8),
189 
192 
193  SLE_CONDVAR(Station, airport.flags, SLE_VAR_U64 | SLE_FILE_U16, SL_MIN_VERSION, SLV_3),
194  SLE_CONDVAR(Station, airport.flags, SLE_VAR_U64 | SLE_FILE_U32, SLV_3, SLV_46),
195  SLE_CONDVAR(Station, airport.flags, SLE_UINT64, SLV_46, SL_MAX_VERSION),
196 
198  SLE_CONDVAR(Station, last_vehicle_type, SLE_UINT8, SLV_26, SL_MAX_VERSION),
199 
200  SLE_CONDNULL(2, SLV_3, SLV_26),
201  SLE_CONDVAR(Station, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31),
202  SLE_CONDVAR(Station, build_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
203 
206 
207  /* Used by newstations for graphic variations */
208  SLE_CONDVAR(Station, random_bits, SLE_UINT16, SLV_27, SL_MAX_VERSION),
209  SLE_CONDVAR(Station, waiting_triggers, SLE_UINT8, SLV_27, SL_MAX_VERSION),
210  SLE_CONDVAR(Station, num_specs, SLE_UINT8, SLV_27, SL_MAX_VERSION),
211 
212  SLE_CONDLST(Station, loading_vehicles, REF_VEHICLE, SLV_57, SL_MAX_VERSION),
213 
214  /* reserve extra space in savegame here. (currently 32 bytes) */
216 
217  SLE_END()
218 };
219 
220 static uint16 _waiting_acceptance;
221 static uint32 _num_flows;
222 static uint16 _cargo_source;
223 static uint32 _cargo_source_xy;
224 static uint8 _cargo_days;
225 static Money _cargo_feeder_share;
226 
227 static const SaveLoad _station_speclist_desc[] = {
228  SLE_CONDVAR(StationSpecList, grfid, SLE_UINT32, SLV_27, SL_MAX_VERSION),
229  SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8, SLV_27, SL_MAX_VERSION),
230 
231  SLE_END()
232 };
233 
234 std::list<CargoPacket *> _packets;
235 uint32 _num_dests;
236 
237 struct FlowSaveLoad {
238  FlowSaveLoad() : source(0), via(0), share(0), restricted(false) {}
239  StationID source;
240  StationID via;
241  uint32 share;
242  bool restricted;
243 };
244 
245 static const SaveLoad _flow_desc[] = {
246  SLE_VAR(FlowSaveLoad, source, SLE_UINT16),
247  SLE_VAR(FlowSaveLoad, via, SLE_UINT16),
248  SLE_VAR(FlowSaveLoad, share, SLE_UINT32),
249  SLE_CONDVAR(FlowSaveLoad, restricted, SLE_BOOL, SLV_187, SL_MAX_VERSION),
250  SLE_END()
251 };
252 
259 {
260  static const SaveLoad goods_desc[] = {
261  SLEG_CONDVAR( _waiting_acceptance, SLE_UINT16, SL_MIN_VERSION, SLV_68),
262  SLE_CONDVAR(GoodsEntry, status, SLE_UINT8, SLV_68, SL_MAX_VERSION),
264  SLE_VAR(GoodsEntry, time_since_pickup, SLE_UINT8),
265  SLE_VAR(GoodsEntry, rating, SLE_UINT8),
266  SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_7),
267  SLEG_CONDVAR( _cargo_source, SLE_UINT16, SLV_7, SLV_68),
268  SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, SLV_44, SLV_68),
269  SLEG_CONDVAR( _cargo_days, SLE_UINT8, SL_MIN_VERSION, SLV_68),
270  SLE_VAR(GoodsEntry, last_speed, SLE_UINT8),
271  SLE_VAR(GoodsEntry, last_age, SLE_UINT8),
272  SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, SLV_14, SLV_65),
273  SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, SLV_65, SLV_68),
274  SLE_CONDVAR(GoodsEntry, amount_fract, SLE_UINT8, SLV_150, SL_MAX_VERSION),
276  SLEG_CONDVAR( _num_dests, SLE_UINT32, SLV_183, SL_MAX_VERSION),
277  SLE_CONDVAR(GoodsEntry, cargo.reserved_count, SLE_UINT, SLV_181, SL_MAX_VERSION),
278  SLE_CONDVAR(GoodsEntry, link_graph, SLE_UINT16, SLV_183, SL_MAX_VERSION),
279  SLE_CONDVAR(GoodsEntry, node, SLE_UINT16, SLV_183, SL_MAX_VERSION),
280  SLEG_CONDVAR( _num_flows, SLE_UINT32, SLV_183, SL_MAX_VERSION),
281  SLE_CONDVAR(GoodsEntry, max_waiting_cargo, SLE_UINT32, SLV_183, SL_MAX_VERSION),
282  SLE_END()
283  };
284 
285  return goods_desc;
286 }
287 
288 typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair;
289 
290 static const SaveLoad _cargo_list_desc[] = {
291  SLE_VAR(StationCargoPair, first, SLE_UINT16),
292  SLE_LST(StationCargoPair, second, REF_CARGO_PACKET),
293  SLE_END()
294 };
295 
301 static void SwapPackets(GoodsEntry *ge)
302 {
303  StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets());
304 
305  if (_packets.empty()) {
306  std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(INVALID_STATION));
307  if (it == ge_packets.end()) {
308  return;
309  } else {
310  it->second.swap(_packets);
311  }
312  } else {
313  assert(ge_packets[INVALID_STATION].empty());
314  ge_packets[INVALID_STATION].swap(_packets);
315  }
316 }
317 
318 static void Load_STNS()
319 {
320  _cargo_source_xy = 0;
321  _cargo_days = 0;
322  _cargo_feeder_share = 0;
323 
325  int index;
326  while ((index = SlIterateArray()) != -1) {
327  Station *st = new (index) Station();
328 
329  SlObject(st, _old_station_desc);
330 
331  _waiting_acceptance = 0;
332 
333  for (CargoID i = 0; i < num_cargo; i++) {
334  GoodsEntry *ge = &st->goods[i];
335  SlObject(ge, GetGoodsDesc());
336  SwapPackets(ge);
338  SB(ge->status, GoodsEntry::GES_ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
339  if (GB(_waiting_acceptance, 0, 12) != 0) {
340  /* In old versions, enroute_from used 0xFF as INVALID_STATION */
341  StationID source = (IsSavegameVersionBefore(SLV_7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
342 
343  /* Make sure we can allocate the CargoPacket. This is safe
344  * as there can only be ~64k stations and 32 cargoes in these
345  * savegame versions. As the CargoPacketPool has more than
346  * 16 million entries; it fits by an order of magnitude. */
348 
349  /* Don't construct the packet with station here, because that'll fail with old savegames */
350  CargoPacket *cp = new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_days, source, _cargo_source_xy, _cargo_source_xy, _cargo_feeder_share);
351  ge->cargo.Append(cp, INVALID_STATION);
352  SB(ge->status, GoodsEntry::GES_RATING, 1, 1);
353  }
354  }
355  }
356 
357  if (st->num_specs != 0) {
358  /* Allocate speclist memory when loading a game */
359  st->speclist = CallocT<StationSpecList>(st->num_specs);
360  for (uint i = 0; i < st->num_specs; i++) {
361  SlObject(&st->speclist[i], _station_speclist_desc);
362  }
363  }
364  }
365 }
366 
367 static void Ptrs_STNS()
368 {
369  /* Don't run when savegame version is higher than or equal to 123. */
370  if (!IsSavegameVersionBefore(SLV_123)) return;
371 
373  for (Station *st : Station::Iterate()) {
375  for (CargoID i = 0; i < num_cargo; i++) {
376  GoodsEntry *ge = &st->goods[i];
377  SwapPackets(ge);
378  SlObject(ge, GetGoodsDesc());
379  SwapPackets(ge);
380  }
381  }
382  SlObject(st, _old_station_desc);
383  }
384 }
385 
386 
387 static const SaveLoad _base_station_desc[] = {
388  SLE_VAR(BaseStation, xy, SLE_UINT32),
389  SLE_REF(BaseStation, town, REF_TOWN),
390  SLE_VAR(BaseStation, string_id, SLE_STRINGID),
392  SLE_VAR(BaseStation, delete_ctr, SLE_UINT8),
393  SLE_VAR(BaseStation, owner, SLE_UINT8),
394  SLE_VAR(BaseStation, facilities, SLE_UINT8),
395  SLE_VAR(BaseStation, build_date, SLE_INT32),
396 
397  /* Used by newstations for graphic variations */
398  SLE_VAR(BaseStation, random_bits, SLE_UINT16),
399  SLE_VAR(BaseStation, waiting_triggers, SLE_UINT8),
400  SLE_VAR(BaseStation, num_specs, SLE_UINT8),
401 
402  SLE_END()
403 };
404 
405 static OldPersistentStorage _old_st_persistent_storage;
406 
407 static const SaveLoad _station_desc[] = {
408  SLE_WRITEBYTE(Station, facilities),
409  SLE_ST_INCLUDE(),
410 
411  SLE_VAR(Station, train_station.tile, SLE_UINT32),
412  SLE_VAR(Station, train_station.w, SLE_FILE_U8 | SLE_VAR_U16),
413  SLE_VAR(Station, train_station.h, SLE_FILE_U8 | SLE_VAR_U16),
414 
415  SLE_REF(Station, bus_stops, REF_ROADSTOPS),
416  SLE_REF(Station, truck_stops, REF_ROADSTOPS),
418  SLE_CONDVAR(Station, ship_station.tile, SLE_UINT32, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
419  SLE_CONDVAR(Station, ship_station.w, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
420  SLE_CONDVAR(Station, ship_station.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
421  SLE_CONDVAR(Station, docking_station.tile, SLE_UINT32, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
422  SLE_CONDVAR(Station, docking_station.w, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
423  SLE_CONDVAR(Station, docking_station.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DOCKS, SL_MAX_VERSION),
424  SLE_VAR(Station, airport.tile, SLE_UINT32),
425  SLE_CONDVAR(Station, airport.w, SLE_FILE_U8 | SLE_VAR_U16, SLV_140, SL_MAX_VERSION),
426  SLE_CONDVAR(Station, airport.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_140, SL_MAX_VERSION),
427  SLE_VAR(Station, airport.type, SLE_UINT8),
428  SLE_CONDVAR(Station, airport.layout, SLE_UINT8, SLV_145, SL_MAX_VERSION),
429  SLE_VAR(Station, airport.flags, SLE_UINT64),
430  SLE_CONDVAR(Station, airport.rotation, SLE_UINT8, SLV_145, SL_MAX_VERSION),
431  SLEG_CONDARR(_old_st_persistent_storage.storage, SLE_UINT32, 16, SLV_145, SLV_161),
433 
434  SLE_VAR(Station, indtype, SLE_UINT8),
435 
436  SLE_VAR(Station, time_since_load, SLE_UINT8),
437  SLE_VAR(Station, time_since_unload, SLE_UINT8),
438  SLE_VAR(Station, last_vehicle_type, SLE_UINT8),
439  SLE_VAR(Station, had_vehicle_of_type, SLE_UINT8),
440  SLE_LST(Station, loading_vehicles, REF_VEHICLE),
441  SLE_CONDVAR(Station, always_accepted, SLE_FILE_U32 | SLE_VAR_U64, SLV_127, SLV_EXTEND_CARGOTYPES),
442  SLE_CONDVAR(Station, always_accepted, SLE_UINT64, SLV_EXTEND_CARGOTYPES, SL_MAX_VERSION),
443 
444  SLE_END()
445 };
446 
447 static const SaveLoad _waypoint_desc[] = {
448  SLE_WRITEBYTE(Waypoint, facilities),
449  SLE_ST_INCLUDE(),
450 
451  SLE_VAR(Waypoint, town_cn, SLE_UINT16),
452 
453  SLE_CONDVAR(Waypoint, train_station.tile, SLE_UINT32, SLV_124, SL_MAX_VERSION),
454  SLE_CONDVAR(Waypoint, train_station.w, SLE_FILE_U8 | SLE_VAR_U16, SLV_124, SL_MAX_VERSION),
455  SLE_CONDVAR(Waypoint, train_station.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_124, SL_MAX_VERSION),
456 
457  SLE_END()
458 };
459 
465 {
466  return _base_station_desc;
467 }
468 
469 static void RealSave_STNN(BaseStation *bst)
470 {
471  bool waypoint = (bst->facilities & FACIL_WAYPOINT) != 0;
472  SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
473 
474  if (!waypoint) {
475  Station *st = Station::From(bst);
476  for (CargoID i = 0; i < NUM_CARGO; i++) {
477  _num_dests = (uint32)st->goods[i].cargo.Packets()->MapSize();
478  _num_flows = 0;
479  for (FlowStatMap::const_iterator it(st->goods[i].flows.begin()); it != st->goods[i].flows.end(); ++it) {
480  _num_flows += (uint32)it->second.GetShares()->size();
481  }
482  SlObject(&st->goods[i], GetGoodsDesc());
483  for (FlowStatMap::const_iterator outer_it(st->goods[i].flows.begin()); outer_it != st->goods[i].flows.end(); ++outer_it) {
484  const FlowStat::SharesMap *shares = outer_it->second.GetShares();
485  uint32 sum_shares = 0;
486  FlowSaveLoad flow;
487  flow.source = outer_it->first;
488  for (FlowStat::SharesMap::const_iterator inner_it(shares->begin()); inner_it != shares->end(); ++inner_it) {
489  flow.via = inner_it->second;
490  flow.share = inner_it->first - sum_shares;
491  flow.restricted = inner_it->first > outer_it->second.GetUnrestricted();
492  sum_shares = inner_it->first;
493  assert(flow.share > 0);
494  SlObject(&flow, _flow_desc);
495  }
496  }
497  for (StationCargoPacketMap::ConstMapIterator it(st->goods[i].cargo.Packets()->begin()); it != st->goods[i].cargo.Packets()->end(); ++it) {
498  SlObject(const_cast<StationCargoPacketMap::value_type *>(&(*it)), _cargo_list_desc);
499  }
500  }
501  }
502 
503  for (uint i = 0; i < bst->num_specs; i++) {
504  SlObject(&bst->speclist[i], _station_speclist_desc);
505  }
506 }
507 
508 static void Save_STNN()
509 {
510  /* Write the stations */
511  for (BaseStation *st : BaseStation::Iterate()) {
512  SlSetArrayIndex(st->index);
513  SlAutolength((AutolengthProc*)RealSave_STNN, st);
514  }
515 }
516 
517 static void Load_STNN()
518 {
519  _num_flows = 0;
520 
522  int index;
523  while ((index = SlIterateArray()) != -1) {
524  bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0;
525 
526  BaseStation *bst = waypoint ? (BaseStation *)new (index) Waypoint() : new (index) Station();
527  SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
528 
529  if (!waypoint) {
530  Station *st = Station::From(bst);
531 
532  /* Before savegame version 161, persistent storages were not stored in a pool. */
534  /* Store the old persistent storage. The GRFID will be added later. */
536  st->airport.psa = new PersistentStorage(0, 0, 0);
537  memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(_old_st_persistent_storage.storage));
538  }
539 
540  for (CargoID i = 0; i < num_cargo; i++) {
541  SlObject(&st->goods[i], GetGoodsDesc());
542  FlowSaveLoad flow;
543  FlowStat *fs = nullptr;
544  StationID prev_source = INVALID_STATION;
545  for (uint32 j = 0; j < _num_flows; ++j) {
546  SlObject(&flow, _flow_desc);
547  if (fs == nullptr || prev_source != flow.source) {
548  fs = &(st->goods[i].flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second);
549  } else {
550  fs->AppendShare(flow.via, flow.share, flow.restricted);
551  }
552  prev_source = flow.source;
553  }
555  SwapPackets(&st->goods[i]);
556  } else {
557  StationCargoPair pair;
558  for (uint j = 0; j < _num_dests; ++j) {
559  SlObject(&pair, _cargo_list_desc);
560  const_cast<StationCargoPacketMap &>(*(st->goods[i].cargo.Packets()))[pair.first].swap(pair.second);
561  assert(pair.second.empty());
562  }
563  }
564  }
565  }
566 
567  if (bst->num_specs != 0) {
568  /* Allocate speclist memory when loading a game */
569  bst->speclist = CallocT<StationSpecList>(bst->num_specs);
570  for (uint i = 0; i < bst->num_specs; i++) {
571  SlObject(&bst->speclist[i], _station_speclist_desc);
572  }
573  }
574  }
575 }
576 
577 static void Ptrs_STNN()
578 {
579  /* Don't run when savegame version lower than 123. */
580  if (IsSavegameVersionBefore(SLV_123)) return;
581 
583  for (Station *st : Station::Iterate()) {
584  for (CargoID i = 0; i < num_cargo; i++) {
585  GoodsEntry *ge = &st->goods[i];
587  SwapPackets(ge);
588  SlObject(ge, GetGoodsDesc());
589  SwapPackets(ge);
590  } else {
591  SlObject(ge, GetGoodsDesc());
592  for (StationCargoPacketMap::ConstMapIterator it = ge->cargo.Packets()->begin(); it != ge->cargo.Packets()->end(); ++it) {
593  SlObject(const_cast<StationCargoPair *>(&(*it)), _cargo_list_desc);
594  }
595  }
596  }
597  SlObject(st, _station_desc);
598  }
599 
600  for (Waypoint *wp : Waypoint::Iterate()) {
601  SlObject(wp, _waypoint_desc);
602  }
603 }
604 
605 static void Save_ROADSTOP()
606 {
607  for (RoadStop *rs : RoadStop::Iterate()) {
608  SlSetArrayIndex(rs->index);
609  SlObject(rs, _roadstop_desc);
610  }
611 }
612 
613 static void Load_ROADSTOP()
614 {
615  int index;
616 
617  while ((index = SlIterateArray()) != -1) {
618  RoadStop *rs = new (index) RoadStop(INVALID_TILE);
619 
620  SlObject(rs, _roadstop_desc);
621  }
622 }
623 
624 static void Ptrs_ROADSTOP()
625 {
626  for (RoadStop *rs : RoadStop::Iterate()) {
627  SlObject(rs, _roadstop_desc);
628  }
629 }
630 
631 extern const ChunkHandler _station_chunk_handlers[] = {
632  { 'STNS', nullptr, Load_STNS, Ptrs_STNS, nullptr, CH_ARRAY },
633  { 'STNN', Save_STNN, Load_STNN, Ptrs_STNN, nullptr, CH_ARRAY },
634  { 'ROAD', Save_ROADSTOP, Load_ROADSTOP, Ptrs_ROADSTOP, nullptr, CH_ARRAY | CH_LAST},
635 };
SLV_187
@ SLV_187
187 25899 Linkgraph - restricted flows
Definition: saveload.h:267
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:63
SLV_65
@ SLV_65
65 10210
Definition: saveload.h:121
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
Station::goods
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:479
GetBaseStationDescription
const SaveLoad * GetBaseStationDescription()
Get the base station description to be used for SL_ST_INCLUDE.
Definition: station_sl.cpp:464
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:61
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
UpdateWaypointOrder
static void UpdateWaypointOrder(Order *o)
Update the buoy orders to be waypoint orders.
Definition: station_sl.cpp:26
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:394
REF_ROADSTOPS
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition: saveload.h:396
CargoList::Packets
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:246
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:594
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:61
Station
Station data structure.
Definition: station_base.h:450
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:94
SLV_124
@ SLV_124
124 16993
Definition: saveload.h:191
SLE_CONDLST
#define SLE_CONDLST(base, variable, type, from, to)
Storage of a list in some savegame versions.
Definition: saveload.h:604
StationSpecList
Definition: base_station_base.h:21
Waypoint::town_cn
uint16 town_cn
The N-1th waypoint for this town (consecutive number)
Definition: waypoint_base.h:17
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:552
SLV_84
@ SLV_84
84 11822
Definition: saveload.h:143
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:648
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_me
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
Waypoint
Representation of a waypoint.
Definition: waypoint_base.h:16
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:411
IsDriveThroughStopTile
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:233
saveload.h
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
OrthogonalTileArea::Add
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition: tilearea.cpp:43
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:82
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:380
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
MoveBuoysToWaypoints
void MoveBuoysToWaypoints()
Perform all steps to upgrade from the old station buoys to the new version that uses waypoints.
Definition: station_sl.cpp:40
SLV_181
@ SLV_181
181 25012
Definition: saveload.h:260
SLE_WRITEBYTE
#define SLE_WRITEBYTE(base, variable)
Translate values ingame to different values in the savegame and vv.
Definition: saveload.h:681
SLEG_CONDVAR
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:708
BaseStation::owner
Owner owner
The owner of this station.
Definition: base_station_base.h:62
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:678
GoodsEntry::status
byte status
Status of this cargo, see GoodsEntryStatus.
Definition: station_base.h:226
BaseStation::num_specs
uint8 num_specs
Number of specs in the speclist.
Definition: base_station_base.h:65
BaseStation::string_id
StringID string_id
Default name (town area) of station.
Definition: base_station_base.h:58
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:630
SLV_14
@ SLV_14
14.0 2441
Definition: saveload.h:57
RoadStop::RSSFB_BASE_ENTRY
@ RSSFB_BASE_ENTRY
Non-zero when the entries on this road stop are the primary, i.e. the ones to delete.
Definition: roadstop_base.h:27
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:270
FlowStat
Flow statistics telling how much flow should be sent along a link.
Definition: station_base.h:36
GoodsEntry::cargo
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Definition: station_base.h:255
RoadStop::next
struct RoadStop * next
Next stop of the given type at this station.
Definition: roadstop_base.h:69
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
SLV_161
@ SLV_161
161 22567
Definition: saveload.h:236
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
allow control codes in the strings
Definition: saveload.h:490
SLV_123
@ SLV_123
123 16909
Definition: saveload.h:190
SLV_150
@ SLV_150
150 20857
Definition: saveload.h:223
TILE_AREA_LOOP
#define TILE_AREA_LOOP(var, ta)
A loop which iterates over the tiles of a TileArea.
Definition: tilearea_type.h:232
AfterLoadRoadStops
void AfterLoadRoadStops()
(Re)building of road stop caches after loading a savegame.
Definition: station_sl.cpp:128
BaseStation::train_station
TileArea train_station
Tile area the train 'station' part covers.
Definition: base_station_base.h:75
SLV_55
@ SLV_55
55 9638
Definition: saveload.h:109
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:76
SLV_MULTITILE_DOCKS
@ SLV_MULTITILE_DOCKS
216 PR#7380 Multiple docks per station.
Definition: saveload.h:303
Order::MakeGoToWaypoint
void MakeGoToWaypoint(StationID destination)
Makes this order a Go To Waypoint order.
Definition: order_cmd.cpp:103
HVOT_WAYPOINT
@ HVOT_WAYPOINT
Station is a waypoint (NewGRF only!)
Definition: station_type.h:70
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
IsInsideBS
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:188
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
SpecializedStation< Station, false >::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition: base_station_base.h:200
Station::truck_station
TileArea truck_station
Tile area the truck 'station' part covers.
Definition: station_base.h:462
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:687
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
Station::airport
Airport airport
Tile area the airport covers.
Definition: station_base.h:464
SLV_27
@ SLV_27
27 4757
Definition: saveload.h:75
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:16
MultiMap::MapSize
size_t MapSize() const
Count the number of ranges with equal keys in this MultiMap.
Definition: multimap.hpp:350
SLV_46
@ SLV_46
46 8705
Definition: saveload.h:98
SwapPackets
static void SwapPackets(GoodsEntry *ge)
Swap the temporary packets with the packets without specific destination in the given goods entry.
Definition: station_sl.cpp:301
SLV_EXTEND_CARGOTYPES
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition: saveload.h:282
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:400
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:562
SLV_183
@ SLV_183
183 25363 Cargodist
Definition: saveload.h:262
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:816
SLV_45
@ SLV_45
45 8501
Definition: saveload.h:97
BaseStation::name
std::string name
Custom name.
Definition: base_station_base.h:57
Station::bus_station
TileArea bus_station
Tile area the bus 'station' part covers.
Definition: station_base.h:460
PersistentStorageArray::storage
TYPE storage[SIZE]
Memory to for the storage array.
Definition: newgrf_storage.h:67
SLV_7
@ SLV_7
7.0 1770
Definition: saveload.h:48
SLE_LST
#define SLE_LST(base, variable, type)
Storage of a list in every savegame version.
Definition: saveload.h:664
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:392
FACIL_DOCK
@ FACIL_DOCK
Station with a dock.
Definition: station_type.h:56
REF_CARGO_PACKET
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition: saveload.h:398
FlowStat::AppendShare
void AppendShare(StationID st, uint flow, bool restricted=false)
Add some flow to the end of the shares map.
Definition: station_base.h:70
SpecializedStation< Station, false >::From
static Station * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition: base_station_base.h:248
SLV_25
@ SLV_25
25 4259
Definition: saveload.h:73
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Station::truck_stops
RoadStop * truck_stops
All the truck stops.
Definition: station_base.h:461
SLV_145
@ SLV_145
145 20376
Definition: saveload.h:217
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
FACIL_WAYPOINT
@ FACIL_WAYPOINT
Station is a waypoint.
Definition: station_type.h:57
SLV_2
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:34
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:329
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
IsBuoyTile
static bool IsBuoyTile(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:316
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:170
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:622
Pool::PoolItem<&_orderlist_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
SLV_127
@ SLV_127
127 17439
Definition: saveload.h:195
SLEG_CONDLST
#define SLEG_CONDLST(variable, type, from, to)
Storage of a global list in some savegame versions.
Definition: saveload.h:755
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
FACIL_TRAIN
@ FACIL_TRAIN
Station with train station.
Definition: station_type.h:52
GoodsEntry::flows
FlowStatMap flows
Planned flows through this station.
Definition: station_base.h:259
SLV_44
@ SLV_44
44 8144
Definition: saveload.h:95
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
SlReadByte
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:418
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:51
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
GoodsEntry::GES_ACCEPTANCE
@ GES_ACCEPTANCE
Set when the station accepts the cargo currently for final deliveries.
Definition: station_base.h:177
GetStationIndex
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:28
SLV_122
@ SLV_122
122 16855
Definition: saveload.h:189
Pool::PoolItem<&_station_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:299
BaseStation
Base class for all station-ish types.
Definition: base_station_base.h:52
SLV_3
@ SLV_3
3.x lost
Definition: saveload.h:36
SLEG_CONDARR
#define SLEG_CONDARR(variable, type, length, from, to)
Storage of a global array in some savegame versions.
Definition: saveload.h:727
BaseStation::delete_ctr
byte delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
Definition: base_station_base.h:55
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:656
BaseStation::speclist
StationSpecList * speclist
List of station specs of this station.
Definition: base_station_base.h:66
SLV_68
@ SLV_68
68 10266
Definition: saveload.h:124
GetGoodsDesc
const SaveLoad * GetGoodsDesc()
Wrapper function to get the GoodsEntry's internal structure while some of the variables itself are pr...
Definition: station_sl.cpp:258
Town
Town data structure.
Definition: town.h:50
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:42
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
SLV_103
@ SLV_103
103 14598
Definition: saveload.h:166
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
StationCargoList::Append
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
Definition: cargopacket.cpp:690
Station::bus_stops
RoadStop * bus_stops
All the road stops.
Definition: station_base.h:459
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
FACIL_AIRPORT
@ FACIL_AIRPORT
Station with an airport.
Definition: station_type.h:55
Airport::psa
PersistentStorage * psa
Persistent storage for NewGRF airports.
Definition: station_base.h:313
SLV_4
@ SLV_4
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition: saveload.h:37
SaveLoad
SaveLoad type struct.
Definition: saveload.h:517
MultiMap
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:17
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:79
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
Town::name
std::string name
Custom town name. If empty, the town was not renamed and uses the generated name.
Definition: town.h:59
SLV_140
@ SLV_140
140 19382
Definition: saveload.h:211
SLV_26
@ SLV_26
26 4466
Definition: saveload.h:74
StationUpdateCachedTriggers
void StationUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Definition: newgrf_station.cpp:1048
SLV_9
@ SLV_9
9.0 1909
Definition: saveload.h:50
BaseStation::build_date
Date build_date
Date of construction.
Definition: base_station_base.h:68
Order::next
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:49
Order
Definition: order_base.h:32
SLV_51
@ SLV_51
51 8978
Definition: saveload.h:104
GoodsEntry::GES_RATING
@ GES_RATING
This indicates whether a cargo has a rating at the station.
Definition: station_base.h:187
SLV_57
@ SLV_57
57 9691
Definition: saveload.h:111
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
FlowSaveLoad
Definition: station_sl.cpp:237
PersistentStorageArray
Class for persistent storage of data.
Definition: newgrf_storage.h:66