OpenTTD Source 20260711-master-g3fb3006dff
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
12#include "saveload.h"
14
15#include "../station_base.h"
16#include "../waypoint_base.h"
17#include "../roadstop_base.h"
18#include "../vehicle_base.h"
19#include "../newgrf_station.h"
20#include "../newgrf_roadstop.h"
22
23#include "table/strings.h"
24
25#include "../safeguards.h"
26
32{
33 if (!o.IsType(OT_GOTO_STATION)) return;
34
35 const Station *st = Station::Get(o.GetDestination().ToStationID());
36 if (!st->had_vehicle_of_type.Test(StationVehicleType::Waypoint)) return;
37
38 o.MakeGoToWaypoint(o.GetDestination().ToStationID());
39}
40
46{
47 /* Buoy orders become waypoint orders */
48 for (OrderList *ol : OrderList::Iterate()) {
49 VehicleType vt = ol->GetFirstSharedVehicle()->type;
50 if (vt != VehicleType::Ship && vt != VehicleType::Train) continue;
51
52 for (Order &o : ol->GetOrders()) UpdateWaypointOrder(o);
53 }
54
55 for (Vehicle *v : Vehicle::Iterate()) {
56 VehicleType vt = v->type;
57 if (vt != VehicleType::Ship && vt != VehicleType::Train) continue;
58
59 UpdateWaypointOrder(v->current_order);
60 }
61
62 /* Now make the stations waypoints */
63 for (Station *st : Station::Iterate()) {
64 if (!st->had_vehicle_of_type.Test(StationVehicleType::Waypoint)) continue;
65
66 StationID index = st->index;
67 TileIndex xy = st->xy;
68 Town *town = st->town;
69 StringID string_id = st->string_id;
70 std::string name = st->name;
71 TimerGameCalendar::Date build_date = st->build_date;
72 /* TTDPatch could use "buoys with rail station" for rail waypoints */
73 bool train = st->train_station.tile != INVALID_TILE;
74 TileArea train_st = st->train_station;
75
76 /* Delete the station, so we can make it a real waypoint. */
77 delete st;
78
79 /* Stations and waypoints are in the same pool, so if a station
80 * is deleted there must be place for a Waypoint. */
82 Waypoint *wp = Waypoint::CreateAtIndex(index, xy);
83 wp->town = town;
84 wp->string_id = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY;
85 wp->name = std::move(name);
86 wp->delete_ctr = 0; // Just reset delete counter for once.
87 wp->build_date = build_date;
88 wp->owner = train ? GetTileOwner(xy) : OWNER_NONE;
89
90 if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY;
91
92 if (train) {
93 /* When we make a rail waypoint of the station, convert the map as well. */
94 for (TileIndex t : train_st) {
95 Tile tile(t);
96 if (!IsTileType(tile, TileType::Station) || GetStationIndex(tile) != index) continue;
97
99 wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
100 }
101
102 wp->train_station = train_st;
104 } else if (IsBuoyTile(xy) && GetStationIndex(xy) == index) {
105 wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE);
107 }
108 }
109}
110
111void AfterLoadStations()
112{
113 /* Update the speclists of all stations to point to the currently loaded custom stations. */
114 for (BaseStation *st : BaseStation::Iterate()) {
115 for (auto &sm : GetStationSpecList<StationSpec>(st)) {
116 if (sm.grfid == 0) continue;
117 sm.spec = StationClass::GetByGrf(sm.grfid, sm.localidx);
118 }
119 for (auto &sm : GetStationSpecList<RoadStopSpec>(st)) {
120 if (sm.grfid == 0) continue;
121 sm.spec = RoadStopClass::GetByGrf(sm.grfid, sm.localidx);
122 }
123
124 if (Station::IsExpected(st)) {
125 Station *sta = Station::From(st);
126 for (const RoadStop *rs = sta->bus_stops; rs != nullptr; rs = rs->next) sta->bus_station.Add(rs->xy);
127 for (const RoadStop *rs = sta->truck_stops; rs != nullptr; rs = rs->next) sta->truck_station.Add(rs->xy);
128 }
129
132 }
133
134 /* Station blocked, wires and pylon flags need to be stored in the map. This is effectively cached data, so no
135 * version check is necessary. */
136 for (const auto t : Map::Iterate()) {
137 if (HasStationTileRail(t)) SetRailStationTileFlags(t, GetStationSpec(t));
138 }
139}
140
145{
146 /* First construct the drive through entries */
147 for (RoadStop *rs : RoadStop::Iterate()) {
148 if (IsDriveThroughStopTile(rs->xy)) rs->MakeDriveThrough();
149 }
150 /* And then rebuild the data in those entries */
151 for (RoadStop *rs : RoadStop::Iterate()) {
152 if (!rs->status.Test(RoadStop::RoadStopStatusFlag::BaseEntry)) continue;
153
154 rs->GetEntry(DiagDirection::NE).Rebuild(rs);
155 rs->GetEntry(DiagDirection::NW).Rebuild(rs);
156 }
157}
158
159static const SaveLoad _roadstop_desc[] = {
161 SLE_VAR(RoadStop, status, VarTypes::U8),
163};
164
165static uint16_t _waiting_acceptance;
166static uint32_t _old_num_flows;
167static uint16_t _cargo_source;
168static uint32_t _cargo_source_xy;
169static uint8_t _cargo_periods;
170static Money _cargo_feeder_share;
171
172std::list<CargoPacket *> _packets;
173uint32_t _old_num_dests;
174
176 StationID source;
177 StationID via;
178 uint32_t share;
179 bool restricted;
180};
181
182typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair;
183
184static OldPersistentStorage _old_st_persistent_storage;
185
191static void SwapPackets(GoodsEntry *ge)
192{
193 StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->GetOrCreateData().cargo.Packets());
194
195 if (_packets.empty()) {
196 std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(StationID::Invalid()));
197 if (it == ge_packets.end()) {
198 return;
199 } else {
200 it->second.swap(_packets);
201 }
202 } else {
203 assert(ge_packets[StationID::Invalid()].empty());
204 ge_packets[StationID::Invalid()].swap(_packets);
205 }
206}
207
208template <typename T>
209class SlStationSpecList : public VectorSaveLoadHandler<SlStationSpecList<T>, BaseStation, SpecMapping<T>> {
210public:
211 static inline const SaveLoad description[] = {
215 };
216 static inline const SaveLoadCompatTable compat_description = _station_spec_list_sl_compat;
217
218 static inline uint8_t last_num_specs;
219
220 std::vector<SpecMapping<T>> &GetVector(BaseStation *bst) const override { return GetStationSpecList<T>(bst); }
221
226};
227
228/* Instantiate SlStationSpecList classes. */
229template class SlStationSpecList<StationSpec>;
231
233public:
234 using StationWaitingTriggersPair = std::pair<const TileIndex, StationRandomTriggers>;
235
236 static inline const SaveLoad description[] = {
237 SLE_VAR(StationWaitingTriggersPair, first, VarTypes::U32),
238 SLE_VAR(StationWaitingTriggersPair, second, VarTypes::U8),
239 };
240 static inline const SaveLoadCompatTable compat_description = _station_cargo_sl_compat;
241
242 void Save(BaseStation *st) const override
243 {
244 SlSetStructListLength(st->tile_waiting_random_triggers.size());
245 for (const auto &pair : st->tile_waiting_random_triggers) {
246 SlObject(const_cast<StationWaitingTriggersPair *>(&pair), this->GetDescription());
247 }
248 }
249
250 void Load(BaseStation *st) const override
251 {
252 size_t num = SlGetStructListLength(UINT32_MAX);
253 if (num == 0) return;
254
255 StationWaitingTriggersPair pair;
256 for (uint j = 0; j < num; ++j) {
257 SlObject(&pair, this->GetLoadDescription());
258 st->tile_waiting_random_triggers.emplace(pair.first, pair.second);
259 }
260 }
261};
262
264public:
265 static inline const SaveLoad description[] = {
266 SLE_VAR(StationCargoPair, first, VarTypes::U16),
267 SLE_REFLIST(StationCargoPair, second, SLRefType::CargoPacket),
268 };
269 static inline const SaveLoadCompatTable compat_description = _station_cargo_sl_compat;
270
271 void Save(GoodsEntry *ge) const override
272 {
273 if (!ge->HasData()) {
275 return;
276 }
277
278 const auto *packets = ge->GetData().cargo.Packets();
279 SlSetStructListLength(packets->MapSize());
280 for (StationCargoPacketMap::ConstMapIterator it(packets->begin()); it != packets->end(); ++it) {
281 SlObject(const_cast<StationCargoPacketMap::value_type *>(&(*it)), this->GetDescription());
282 }
283 }
284
285 void Load(GoodsEntry *ge) const override
286 {
287 size_t num_dests = IsSavegameVersionBefore(SaveLoadVersion::SaveloadListLength) ? _old_num_dests : SlGetStructListLength(UINT32_MAX);
288 if (num_dests == 0) return;
289
291 StationCargoPair pair;
292 for (uint j = 0; j < num_dests; ++j) {
293 SlObject(&pair, this->GetLoadDescription());
294 const_cast<StationCargoPacketMap &>(*(data.cargo.Packets()))[pair.first].swap(pair.second);
295 assert(pair.second.empty());
296 }
297 }
298
299 void FixPointers(GoodsEntry *ge) const override
300 {
301 if (!ge->HasData()) return;
302
303 for (StationCargoPacketMap::ConstMapIterator it = ge->GetData().cargo.Packets()->begin(); it != ge->GetData().cargo.Packets()->end(); ++it) {
304 SlObject(const_cast<StationCargoPair *>(&(*it)), this->GetDescription());
305 }
306 }
307};
308
310public:
311 static inline const SaveLoad description[] = {
316 };
317 static inline const SaveLoadCompatTable compat_description = _station_flow_sl_compat;
318
319 void Save(GoodsEntry *ge) const override
320 {
321 if (!ge->HasData()) {
323 return;
324 }
325
326 size_t num_flows = 0;
327 for (const auto &it : ge->GetData().flows) {
328 num_flows += it.second.GetShares()->size();
329 }
330 SlSetStructListLength(num_flows);
331
332 for (const auto &outer_it : ge->GetData().flows) {
333 const FlowStat::SharesMap *shares = outer_it.second.GetShares();
334 uint32_t sum_shares = 0;
335 FlowSaveLoad flow{};
336 flow.source = outer_it.first;
337 for (auto &inner_it : *shares) {
338 flow.via = inner_it.second;
339 flow.share = inner_it.first - sum_shares;
340 flow.restricted = inner_it.first > outer_it.second.GetUnrestricted();
341 sum_shares = inner_it.first;
342 assert(flow.share > 0);
343 SlObject(&flow, this->GetDescription());
344 }
345 }
346 }
347
348 void Load(GoodsEntry *ge) const override
349 {
350 size_t num_flows = IsSavegameVersionBefore(SaveLoadVersion::SaveloadListLength) ? _old_num_flows : SlGetStructListLength(UINT32_MAX);
351 if (num_flows == 0) return;
352
354 FlowSaveLoad flow{};
355 FlowStat *fs = nullptr;
356 StationID prev_source = StationID::Invalid();
357 for (uint32_t j = 0; j < num_flows; ++j) {
358 SlObject(&flow, this->GetLoadDescription());
359 if (fs == nullptr || prev_source != flow.source) {
360 fs = &(data.flows.emplace(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second;
361 } else {
362 fs->AppendShare(flow.via, flow.share, flow.restricted);
363 }
364 prev_source = flow.source;
365 }
366 }
367};
368
370public:
371 static inline uint cargo_reserved_count;
372
373 static inline const SaveLoad description[] = {
374 SLEG_CONDVAR("waiting_acceptance", _waiting_acceptance, VarTypes::U16, SaveLoadVersion::MinVersion, SaveLoadVersion::CargoPackets),
376 SLE_VAR(GoodsEntry, time_since_pickup, VarTypes::U8),
382 SLE_VAR(GoodsEntry, last_speed, VarTypes::U8),
383 SLE_VAR(GoodsEntry, last_age, VarTypes::U8),
389 SLEG_CONDVAR("cargo.reserved_count", SlStationGoods::cargo_reserved_count, VarTypes::U32, SaveLoadVersion::CargoReservation, SaveLoadVersion::MaxVersion),
396 };
397
398 static inline const SaveLoadCompatTable compat_description = _station_goods_sl_compat;
399
404 size_t GetNumCargo() const
405 {
409 /* Read from the savegame how long the list is. */
411 }
412
413 void Save(BaseStation *bst) const override
414 {
415 Station *st = Station::From(bst);
416
418
419 for (GoodsEntry &ge : st->goods) {
420 SlStationGoods::cargo_reserved_count = ge.HasData() ? ge.GetData().cargo.reserved_count : 0;
421 SlObject(&ge, this->GetDescription());
422 }
423 }
424
425 void Load(BaseStation *bst) const override
426 {
427 Station *st = Station::From(bst);
428
429 /* Before savegame version 161, persistent storages were not stored in a pool. */
431 /* Store the old persistent storage. The GRFID will be added later. */
434 std::copy(std::begin(_old_st_persistent_storage.storage), std::end(_old_st_persistent_storage.storage), std::begin(st->airport.psa->storage));
435 }
436
437 auto end = std::next(std::begin(st->goods), std::min(this->GetNumCargo(), std::size(st->goods)));
438 for (auto it = std::begin(st->goods); it != end; ++it) {
439 GoodsEntry &ge = *it;
440 SlObject(&ge, this->GetLoadDescription());
441 if (!IsSavegameVersionBefore(SaveLoadVersion::CargoReservation) && SlStationGoods::cargo_reserved_count != 0) {
442 ge.GetOrCreateData().cargo.reserved_count = SlStationGoods::cargo_reserved_count;
443 }
445 SwapPackets(&ge);
446 }
448 ge.status.Set(GoodsEntry::State::Acceptance, HasBit(_waiting_acceptance, 15));
449 if (GB(_waiting_acceptance, 0, 12) != 0) {
450 /* In old versions, enroute_from used 0xFF as StationID::Invalid() */
451 StationID source = (IsSavegameVersionBefore(SaveLoadVersion::LargerCargoSource) && _cargo_source == 0xFF) ? StationID::Invalid() : static_cast<StationID>(_cargo_source);
452
453 /* Make sure we can allocate the CargoPacket. This is safe
454 * as there can only be ~64k stations and 32 cargoes in these
455 * savegame versions. As the CargoPacketPool has more than
456 * 16 million entries; it fits by an order of magnitude. */
458
459 /* Don't construct the packet with station here, because that'll fail with old savegames */
460 CargoPacket *cp = CargoPacket::Create(GB(_waiting_acceptance, 0, 12), _cargo_periods, source, TileIndex{_cargo_source_xy}, _cargo_feeder_share);
461 ge.GetOrCreateData().cargo.Append(cp, StationID::Invalid());
463 }
464 }
465 }
466 }
467
468 void FixPointers(BaseStation *bst) const override
469 {
470 Station *st = Station::From(bst);
471
473 auto end = std::next(std::begin(st->goods), std::min(num_cargo, std::size(st->goods)));
474 for (auto it = std::begin(st->goods); it != end; ++it) {
475 GoodsEntry &ge = *it;
477 SwapPackets(&ge); // We have to swap back again to be in the format pre-183 expects.
478 SlObject(&ge, this->GetDescription());
479 SwapPackets(&ge);
480 } else {
481 SlObject(&ge, this->GetDescription());
482 }
483 }
484 }
485};
486
487static const SaveLoad _old_station_desc[] = {
495 SLE_VAR(Station, train_station.w, VarFileType::U8 | VarMemType::U16),
497
503
504 SLE_VAR(Station, time_since_load, VarTypes::U8),
505 SLE_VAR(Station, time_since_unload, VarTypes::U8),
506 SLE_VAR(Station, delete_ctr, VarTypes::U8),
508 SLE_VAR(Station, facilities, VarTypes::U8),
509 SLE_VAR(Station, airport.type, VarTypes::U8),
513
515
518
521
522 /* Used by newstations for graphic variations */
526
528
531};
532
533struct STNSChunkHandler : ChunkHandler {
534 STNSChunkHandler() : ChunkHandler('STNS', ChunkType::ReadOnly) {}
535
536 void Load() const override
537 {
538 const std::vector<SaveLoad> slt = SlCompatTableHeader(_old_station_desc, _old_station_sl_compat);
539
540 _cargo_source_xy = 0;
541 _cargo_periods = 0;
542 _cargo_feeder_share = 0;
543
544 int index;
545 while ((index = SlIterateArray()) != -1) {
546 Station *st = Station::CreateAtIndex(StationID(index));
547
548 _waiting_acceptance = 0;
549 SlObject(st, slt);
550 }
551 }
552
553 void FixPointers() const override
554 {
555 /* From SaveLoadVersion::UnifyWaypointAndStation we store stations in STNN; before that in STNS. So do not
556 * fix pointers when the version is SaveLoadVersion::UnifyWaypointAndStation or up, as that would fix
557 * pointers twice: once in STNN chunk and once here. */
559
560 for (Station *st : Station::Iterate()) {
561 SlObject(st, _old_station_desc);
562 }
563 }
564};
565
567public:
568 static inline const SaveLoad description[] = {
571 SLE_VAR(RoadStopTileData, animation_frame, VarTypes::U8),
572 };
573 static inline const SaveLoadCompatTable compat_description = {};
574
575 std::vector<RoadStopTileData> &GetVector(BaseStation *bst) const override { return bst->custom_roadstop_tile_data; }
576};
577
583public:
584 static inline const SaveLoad description[] = {
589 SLE_VAR(BaseStation, delete_ctr, VarTypes::U8),
591 SLE_VAR(BaseStation, facilities, VarTypes::U8),
592 SLE_VAR(BaseStation, build_date, VarTypes::I32),
593
594 /* Used by newstations for graphic variations */
595 SLE_VAR(BaseStation, random_bits, VarTypes::U16),
596 SLE_VARNAME(BaseStation, waiting_random_triggers, "waiting_triggers", VarTypes::U8),
597 SLEG_STRUCTLIST("tile_waiting_triggers", SlStationWaitingTriggers),
599 };
600 static inline const SaveLoadCompatTable compat_description = _station_base_sl_compat;
601
602 void Save(BaseStation *bst) const override
603 {
604 SlObject(bst, this->GetDescription());
605 }
606
607 void Load(BaseStation *bst) const override
608 {
609 SlObject(bst, this->GetLoadDescription());
610 }
611
612 void FixPointers(BaseStation *bst) const override
613 {
614 SlObject(bst, this->GetDescription());
615 }
616};
617
622public:
623 static inline const SaveLoad description[] = {
624 SLEG_STRUCT("base", SlStationBase),
625 SLE_VAR(Station, train_station.tile, VarTypes::U32),
626 SLE_VAR(Station, train_station.w, VarFileType::U8 | VarMemType::U16),
627 SLE_VAR(Station, train_station.h, VarFileType::U8 | VarMemType::U16),
628
630 SLE_REF(Station, truck_stops, SLRefType::RoadStop),
637 SLE_VAR(Station, airport.tile, VarTypes::U32),
640 SLE_VAR(Station, airport.type, VarTypes::U8),
642 SLE_VARNAME(Station, airport.blocks, "airport.flags", VarTypes::U64),
646
647 SLE_VAR(Station, indtype, VarTypes::U8),
648
649 SLE_VAR(Station, time_since_load, VarTypes::U8),
650 SLE_VAR(Station, time_since_unload, VarTypes::U8),
651 SLE_VAR(Station, last_vehicle_type, VarTypes::U8),
652 SLE_VAR(Station, had_vehicle_of_type, VarTypes::U8),
653 SLE_REFLIST(Station, loading_vehicles, SLRefType::Vehicle),
658 };
659 static inline const SaveLoadCompatTable compat_description = _station_normal_sl_compat;
660
661 void Save(BaseStation *bst) const override
662 {
663 if (bst->facilities.Test(StationFacility::Waypoint)) return;
664 SlObject(bst, this->GetDescription());
665 }
666
667 void Load(BaseStation *bst) const override
668 {
669 if (bst->facilities.Test(StationFacility::Waypoint)) return;
670 SlObject(bst, this->GetLoadDescription());
671 }
672
673 void FixPointers(BaseStation *bst) const override
674 {
675 if (bst->facilities.Test(StationFacility::Waypoint)) return;
676 SlObject(bst, this->GetDescription());
677 }
678};
679
681public:
682 static inline const SaveLoad description[] = {
683 SLEG_STRUCT("base", SlStationBase),
684 SLE_VAR(Waypoint, town_cn, VarTypes::U16),
685
693 };
694 static inline const SaveLoadCompatTable compat_description = _station_waypoint_sl_compat;
695
696 void Save(BaseStation *bst) const override
697 {
698 if (!bst->facilities.Test(StationFacility::Waypoint)) return;
699 SlObject(bst, this->GetDescription());
700 }
701
702 void Load(BaseStation *bst) const override
703 {
704 if (!bst->facilities.Test(StationFacility::Waypoint)) return;
705 SlObject(bst, this->GetLoadDescription());
706 }
707
708 void FixPointers(BaseStation *bst) const override
709 {
710 if (!bst->facilities.Test(StationFacility::Waypoint)) return;
711 SlObject(bst, this->GetDescription());
712 }
713};
714
715static const SaveLoad _station_desc[] = {
716 SLE_SAVEBYTE(BaseStation, facilities),
717 SLEG_STRUCT("normal", SlStationNormal),
718 SLEG_STRUCT("waypoint", SlStationWaypoint),
722};
723
724struct STNNChunkHandler : ChunkHandler {
725 STNNChunkHandler() : ChunkHandler('STNN', ChunkType::Table) {}
726
727 void Save() const override
728 {
729 SlTableHeader(_station_desc);
730
731 /* Write the stations */
732 for (BaseStation *st : BaseStation::Iterate()) {
733 SlSetArrayIndex(st->index);
734 SlObject(st, _station_desc);
735 }
736 }
737
738
739 void Load() const override
740 {
741 const std::vector<SaveLoad> slt = SlCompatTableHeader(_station_desc, _station_sl_compat);
742
743 _old_num_flows = 0;
744
745 int index;
746 while ((index = SlIterateArray()) != -1) {
747 bool waypoint = static_cast<StationFacilities>(SlReadByte()).Test(StationFacility::Waypoint);
748
749 BaseStation *bst = waypoint ? (BaseStation *)Waypoint::CreateAtIndex(StationID(index)) : Station::CreateAtIndex(StationID(index));
750 SlObject(bst, slt);
751 }
752 }
753
754 void FixPointers() const override
755 {
756 /* From SaveLoadVersion::UnifyWaypointAndStation we store stations in STNN; before that in STNS. So do not
757 * fix pointers when the version is below SaveLoadVersion::UnifyWaypointAndStation, as that would fix
758 * pointers twice: once in STNS chunk and once here. */
760
761 for (BaseStation *bst : BaseStation::Iterate()) {
762 SlObject(bst, _station_desc);
763 }
764 }
765};
766
767struct ROADChunkHandler : ChunkHandler {
768 ROADChunkHandler() : ChunkHandler('ROAD', ChunkType::Table) {}
769
770 void Save() const override
771 {
772 SlTableHeader(_roadstop_desc);
773
774 for (RoadStop *rs : RoadStop::Iterate()) {
775 SlSetArrayIndex(rs->index);
776 SlObject(rs, _roadstop_desc);
777 }
778 }
779
780 void Load() const override
781 {
782 const std::vector<SaveLoad> slt = SlCompatTableHeader(_roadstop_desc, _roadstop_sl_compat);
783
784 int index;
785
786 while ((index = SlIterateArray()) != -1) {
787 RoadStop *rs = RoadStop::CreateAtIndex(RoadStopID(index), INVALID_TILE);
788
789 SlObject(rs, slt);
790 }
791 }
792
793 void FixPointers() const override
794 {
795 for (RoadStop *rs : RoadStop::Iterate()) {
796 SlObject(rs, _roadstop_desc);
797 }
798 }
799};
800
801static const STNSChunkHandler STNS;
802static const STNNChunkHandler STNN;
803static const ROADChunkHandler ROAD;
804static const ChunkHandlerRef station_chunk_handlers[] = {
805 STNS,
806 STNN,
807 ROAD,
808};
809
810extern const ChunkHandlerTable _station_chunk_handlers(station_chunk_handlers);
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Default handler for saving/loading an object to/from disk.
Definition saveload.h:606
Flow statistics telling how much flow should be sent along a link.
void AppendShare(StationID st, uint flow, bool restricted=false)
Add some flow to the end of the shares map.
static const StationSpec * GetByGrf(GrfID grfid, uint16_t local_id)
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
std::vector< RoadStopTileData > & GetVector(BaseStation *bst) const override
Get instance of vector to load/save.
SaveLoad handler for the BaseStation, which all other stations / waypoints make use of.
void Save(BaseStation *bst) const override
Save the object to disk.
void Load(BaseStation *bst) const override
Load the object from disk.
void FixPointers(BaseStation *bst) const override
A post-load callback to fix SaveLoadType::Reference integers into pointers.
void Save(GoodsEntry *ge) const override
Save the object to disk.
void FixPointers(GoodsEntry *ge) const override
A post-load callback to fix SaveLoadType::Reference integers into pointers.
void Load(GoodsEntry *ge) const override
Load the object from disk.
void Load(GoodsEntry *ge) const override
Load the object from disk.
void Save(GoodsEntry *ge) const override
Save the object to disk.
size_t GetNumCargo() const
Get the number of cargoes used by this savegame version.
void FixPointers(BaseStation *bst) const override
A post-load callback to fix SaveLoadType::Reference integers into pointers.
void Load(BaseStation *bst) const override
Load the object from disk.
void Save(BaseStation *bst) const override
Save the object to disk.
SaveLoad handler for a normal station (read: not a waypoint).
void FixPointers(BaseStation *bst) const override
A post-load callback to fix SaveLoadType::Reference integers into pointers.
void Load(BaseStation *bst) const override
Load the object from disk.
void Save(BaseStation *bst) const override
Save the object to disk.
size_t GetLength() const override
Get number of elements to load into vector.
static uint8_t last_num_specs
Number of specs of the last loaded station.
std::vector< SpecMapping< T > > & GetVector(BaseStation *bst) const override
Get instance of vector to load/save.
void Load(BaseStation *st) const override
Load the object from disk.
void Save(BaseStation *st) const override
Save the object to disk.
void Save(BaseStation *bst) const override
Save the object to disk.
void FixPointers(BaseStation *bst) const override
A post-load callback to fix SaveLoadType::Reference integers into pointers.
void Load(BaseStation *bst) const override
Load the object from disk.
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
uint reserved_count
Amount of cargo being reserved for loading.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m6()
General purpose.
Definition map_func.h:167
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1385
static constexpr Owner OWNER_NONE
The tile has no ownership.
@ NW
Northwest.
@ NE
Northeast, upper right on your monitor.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
constexpr 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.
@ Invalid
An invalid spec feature.
Definition newgrf.h:109
void RoadStopUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
NewGRF definitions and structures for road stops.
void StationUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
Header file for NewGRF stations.
Base class for roadstops.
A number of safeguards to prevent using unsafe methods.
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:734
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition saveload.cpp:410
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
void SlSetStructListLength(size_t length)
Set the length of this list.
Functions/types related to saving and loading games.
@ U64
A 64 bit unsigned int.
Definition saveload.h:686
@ U8
A 8 bit unsigned int.
Definition saveload.h:680
@ U32
A 32 bit unsigned int.
Definition saveload.h:684
@ I32
A 32 bit signed int.
Definition saveload.h:683
@ I64
A 64 bit signed int.
Definition saveload.h:685
@ U16
A 16 bit unsigned int.
Definition saveload.h:682
#define SLEG_CONDARR(name, variable, type, length, from, to)
Storage of a global fixed-size array of SaveLoadType::Variable elements in some savegame versions.
Definition saveload.h:1145
@ U8
A 8 bit unsigned int.
Definition saveload.h:662
@ U32
A 32 bit unsigned int.
Definition saveload.h:666
@ U16
A 16 bit unsigned int.
Definition saveload.h:664
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition saveload.h:1256
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
#define SLE_VARNAME(base, variable, name, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1026
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:530
@ CargoPacket
Load/save a reference to a cargo packet.
Definition saveload.h:649
@ Storage
Load/save a reference to a persistent storage.
Definition saveload.h:651
@ Town
Load/save a reference to a town.
Definition saveload.h:645
@ Vehicle
Load/save a reference to a vehicle.
Definition saveload.h:643
@ RoadStop
Load/save a reference to a bus/truck stop.
Definition saveload.h:647
#define SLEG_CONDREFLIST(name, variable, type, from, to)
Storage of a global reference list in some savegame versions.
Definition saveload.h:1174
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:533
#define SLE_SAVEBYTE(base, variable)
Only write byte during saving; never read it during loading.
Definition saveload.h:1098
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
#define SLE_CONDREFLIST(base, variable, type, from, to)
Storage of a list of SaveLoadType::Reference elements in some savegame versions.
Definition saveload.h:979
#define SLE_REFLIST(base, variable, type)
Storage of a list of SaveLoadType::Reference elements in every savegame version.
Definition saveload.h:1078
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:539
#define SLEG_STRUCT(name, handler)
Storage of a structs in every savegame version.
Definition saveload.h:1233
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:958
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:904
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1061
@ BigDates
Saveload version: 31, SVN revision: 5999 Change date from 1920 - 2090 to 0 - 5 000 000.
Definition saveload.h:81
@ ExtendCargotypes
Saveload version: 199, GitHub pull request: 6802 Extend cargotypes to 64.
Definition saveload.h:282
@ UnifyWaypointAndStation
Saveload version: 123, SVN revision: 16909 Unify stations and waypoints.
Definition saveload.h:191
@ UnifyCurrency
Saveload version: 65, SVN revision: 10210 Make all variables related to currency 64 bits.
Definition saveload.h:122
@ LinkgraphRestrictedFlow
Saveload version: 187, SVN revision: 25899 Linkgraph - restricted flows.
Definition saveload.h:268
@ CargoReservation
Saveload version: 181, SVN revision: 25012 Persist the reservation of cargo for vehicles instead of ...
Definition saveload.h:261
@ FifoLoading
Saveload version: 57, SVN revision: 9691 First-in-first-out loading of vehicles.
Definition saveload.h:112
@ RoadWaypoints
Saveload version: 338, GitHub pull request: 12572 Road waypoints.
Definition saveload.h:385
@ ReplaceCustomNameArray
Saveload version: 84, SVN revision: 11822 Replace single fixed size array of custom names,...
Definition saveload.h:144
@ LastVehicleType
Saveload version: 26, SVN revision: 4466 Store the last vehicle type at stations instead of the vehi...
Definition saveload.h:75
@ NewGRFSuppliedStationName
Saveload version: 103, SVN revision: 14598 NewGRF industry supplying default names for nearby statio...
Definition saveload.h:167
@ VehicleCurrencyStationChanges
Saveload version: 2.0, release: 0.3.0 Adding vehicle state, larger currency size for statistics,...
Definition saveload.h:34
@ MultitileDocks
Saveload version: 216, GitHub pull request: 7380 Multiple docks per station.
Definition saveload.h:303
@ LargerCargoSource
Saveload version: 7.0, SVN revision: 1770 With more stations, the size of the cargo source needed to...
Definition saveload.h:49
@ TownAcceptance
Saveload version: 127, SVN revision: 17439 Store mask of cargos accepted by town houses and head qua...
Definition saveload.h:196
@ TransferOrder
Saveload version: 14.0, SVN revision: 2441 Transfer orders for feeder systems.
Definition saveload.h:58
@ FractionalCargoDelivery
Saveload version: 150, SVN revision: 20857 When spreading cargo over stations, spread fractional amo...
Definition saveload.h:224
@ NewGRFRoadStops
Saveload version: 303, GitHub pull request: 10144 NewGRF road stops.
Definition saveload.h:343
@ CargoSourceTile
Saveload version: 44, SVN revision: 8144 Store the source tile of the cargo, so accurate payment can...
Definition saveload.h:96
@ StoreAirportSize
Saveload version: 140, SVN revision: 19382 Store the size of the airport in the station.
Definition saveload.h:212
@ ExtendEntityMapping
Saveload version: 311, GitHub pull request: 10672 Extend entity mapping range.
Definition saveload.h:353
@ MultipleRoadStops
Saveload version: 6.0, SVN revision: 1721 Multi tile road stops, and some map size related fixes.
Definition saveload.h:47
@ RoadStopTileData
Saveload version: 340, GitHub pull request: 12883 Move storage of road stop tile data,...
Definition saveload.h:388
@ Cargodist
Saveload version: 183, SVN revision: 25363 Cargodist.
Definition saveload.h:263
@ MoreAirportBlocks
Saveload version: 46, SVN revision: 8705 Increase number of blocks an airport can have.
Definition saveload.h:99
@ MinVersion
First savegame version.
Definition saveload.h:31
@ WaypointMoreLikeStation
Saveload version: 122, SVN revision: 16855 Make waypoint data look more like stations.
Definition saveload.h:190
@ BiggerStationVariables
Saveload version: 3.0 Increase size of airport blocks/station build date.
Definition saveload.h:37
@ NewGRFAirportSmoke
Saveload version: 145, SVN revision: 20376 NewGRF support for airport and configurable amount of smo...
Definition saveload.h:218
@ SaveloadListLength
Saveload version: 293, GitHub pull request: 9374 Consistency in list length with SaveLoadType::Struc...
Definition saveload.h:331
@ MultiTileWaypoints
Saveload version: 124, SVN revision: 16993 Waypoints can be bigger than a single tile.
Definition saveload.h:192
@ NewGRFCargo
Saveload version: 55, SVN revision: 9638 Increase number of cargos and NewGRF control of cargos.
Definition saveload.h:110
@ CargoPackets
Saveload version: 68, SVN revision: 10266 Account for individual units of cargo, i....
Definition saveload.h:125
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:421
@ NewGRFStations
Saveload version: 27, SVN revision: 4757 NewGRF graphics for stations.
Definition saveload.h:76
@ PersistentStoragePool
Saveload version: 161, SVN revision: 22567 Store persistent storage in a pool.
Definition saveload.h:237
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:925
#define SLEG_CONDSTRUCTLIST(name, handler, from, to)
Storage of a list of structs in some savegame versions.
Definition saveload.h:1193
@ ReadOnly
Chunk is never saved.
Definition saveload.h:479
@ Table
An Array with a header describing the elements.
Definition saveload.h:475
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
#define SLE_CONDVARNAME(base, variable, name, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:915
Base classes/functions for stations.
void SetRailStationTileFlags(TileIndex tile, const StationSpec *statspec)
Set rail station tile flags for the given tile.
bool IsDriveThroughStopTile(Tile t)
Is tile t a drive through road stop station or waypoint?
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
bool HasStationTileRail(Tile t)
Has this station tile a rail?
bool IsBuoyTile(Tile t)
Is tile t a buoy tile?
void MoveBuoysToWaypoints()
Perform all steps to upgrade from the old station buoys to the new version that uses waypoints.
static void UpdateWaypointOrder(Order &o)
Update the buoy orders to be waypoint orders.
void AfterLoadRoadStops()
(Re)building of road stop caches after loading a savegame.
static void SwapPackets(GoodsEntry *ge)
Swap the temporary packets with the packets without specific destination in the given goods entry.
Loading of station chunks before table headers were added.
const SaveLoadCompat _station_base_sl_compat[]
Original field order for SlStationBase.
const SaveLoadCompat _roadstop_sl_compat[]
Original field order for _roadstop_desc.
const SaveLoadCompat _station_sl_compat[]
Original field order for _station_desc.
const SaveLoadCompat _station_cargo_sl_compat[]
Original field order for SlStationCargo.
const SaveLoadCompat _station_goods_sl_compat[]
Original field order for SlStationGoods.
const SaveLoadCompat _station_flow_sl_compat[]
Original field order for SlStationFlow.
const SaveLoadCompat _station_waypoint_sl_compat[]
Original field order for SlStationWaypoint.
const SaveLoadCompat _station_normal_sl_compat[]
Original field order for SlStationNormal.
const SaveLoadCompat _old_station_sl_compat[]
Original field order for _old_station_desc.
const SaveLoadCompat _station_spec_list_sl_compat[]
Original field order for SlStationSpecList.
@ Dock
Station with a dock.
@ Waypoint
Station is a waypoint.
@ Train
Station with train station.
@ Airport
Station with an airport.
@ RailWaypoint
Waypoint for trains.
EnumBitSet< StationFacility, uint8_t > StationFacilities
Bitset of StationFacility elements.
@ Waypoint
Station is a waypoint (Save load conversion and NewGRF only!).
Definition of base types and functions in a cross-platform compatible way.
@ AllowControlCode
Allow the special control codes.
Definition string_type.h:47
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
PersistentStorage * psa
Persistent storage for NewGRF airports.
Base class for all station-ish types.
StringID string_id
Default name (town area) of station.
StationFacilities facilities
The facilities that this station has.
TileArea train_station
Tile area the train 'station' part covers.
Owner owner
The owner of this station.
uint8_t delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Town * town
The town this station is associated with.
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
TimerGameCalendar::Date build_date
Date of construction.
std::string name
Custom name.
Container for cargo from the same location and time.
Definition cargopacket.h:41
FlowStatMap flows
Planned flows through this station.
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Stores station stats for a single cargo.
States status
Status of this cargo, see State.
@ Acceptance
Set when the station accepts the cargo currently for final deliveries.
@ Rating
This indicates whether a cargo has a rating at the station.
const GoodsEntryData & GetData() const
Get optional cargo packet/flow data.
bool HasData() const
Test if this goods entry has optional cargo packet/flow data.
GoodsEntryData & GetOrCreateData()
Get optional cargo packet/flow data.
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:366
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:384
If you change this, keep in mind that it is also saved in 2 other places:
Definition order_base.h:34
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:100
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:67
void MakeGoToWaypoint(StationID destination)
Makes this order a Go To Waypoint order.
Definition order_cmd.cpp:87
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition tilearea.cpp:43
StorageType storage
Memory for the storage array.
static Pool::IterateWrapper< OrderList > Iterate(size_t from=0)
static T * CreateAtIndex(RoadStopID index, Targs &&... args)
void FixPointers() const override
Fix the pointers.
void Load() const override
Load the chunk.
void Save() const override
Save the chunk.
A Stop for a Road Vehicle.
@ BaseEntry
Non-zero when the entries on this road stop are the primary, i.e. the ones to delete.
RoadStop * next
Next stop of the given type at this station.
void FixPointers() const override
Fix the pointers.
void Save() const override
Save the chunk.
void Load() const override
Load the chunk.
void Load() const override
Load the chunk.
void FixPointers() const override
Fix the pointers.
SaveLoad type struct.
Definition saveload.h:787
static bool IsExpected(const BaseStation *st)
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
static Waypoint * CreateAtIndex(StationID index, Targs &&... args)
static Station * Get(auto index)
static Station * From(BaseStation *st)
Station data structure.
RoadStop * bus_stops
All the road stops.
std::array< GoodsEntry, NUM_CARGO > goods
Goods at this station.
TileArea bus_station
Tile area the bus 'station' part covers.
Airport airport
Tile area the airport covers.
TileArea truck_station
Tile area the truck 'station' part covers.
RoadStop * truck_stops
All the truck stops.
Town data structure.
Definition town.h:64
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:754
static constexpr VarType U8
Store a 8 bits unsigned int.
Definition saveload.h:752
static constexpr VarType STR
Store string.
Definition saveload.h:760
static constexpr VarType I64
Store a 64 bits signed int.
Definition saveload.h:757
static constexpr VarType U64
Store a 64 bits unsigned int.
Definition saveload.h:758
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:756
static constexpr VarType STRINGID
Store a StringID.
Definition saveload.h:759
static constexpr VarType I32
Store a 32 bits signed int.
Definition saveload.h:755
static constexpr VarType BOOL
Store a boolean (as int8).
Definition saveload.h:750
Vehicle data structure.
Representation of a waypoint.
uint16_t town_cn
The N-1th waypoint for this town (consecutive number).
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
@ Station
A tile of a station or airport.
Definition tile_type.h:54
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition of the game-calendar-timer.
Base class for all vehicles.
VehicleType
Available vehicle types.
@ Ship
Ship vehicle type.
@ Train
Train vehicle type.
Base of waypoints.