OpenTTD Source 20260711-master-g3fb3006dff
autoreplace_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 "../autoreplace_base.h"
16
17#include "../safeguards.h"
18
19static const SaveLoad _engine_renew_desc[] = {
22
26};
27
28struct ERNWChunkHandler : ChunkHandler {
29 ERNWChunkHandler() : ChunkHandler('ERNW', ChunkType::Table) {}
30
31 void Save() const override
32 {
33 SlTableHeader(_engine_renew_desc);
34
35 for (EngineRenew *er : EngineRenew::Iterate()) {
36 SlSetArrayIndex(er->index);
37 SlObject(er, _engine_renew_desc);
38 }
39 }
40
41 void Load() const override
42 {
43 const std::vector<SaveLoad> slt = SlCompatTableHeader(_engine_renew_desc, _engine_renew_sl_compat);
44
45 int index;
46
47 while ((index = SlIterateArray()) != -1) {
48 EngineRenew *er = EngineRenew::CreateAtIndex(EngineRenewID(index));
49 SlObject(er, slt);
50
51 /* Advanced vehicle lists, ungrouped vehicles got added */
53 er->group_id = ALL_GROUP;
55 if (er->group_id == DEFAULT_GROUP) er->group_id = ALL_GROUP;
56 }
57 }
58 }
59
60 void FixPointers() const override
61 {
62 for (EngineRenew *er : EngineRenew::Iterate()) {
63 SlObject(er, _engine_renew_desc);
64 }
65 }
66};
67
68static const ERNWChunkHandler ERNW;
69static const ChunkHandlerRef autoreplace_chunk_handlers[] = {
70 ERNW,
71};
72
73extern const ChunkHandlerTable _autoreplace_chunk_handlers(autoreplace_chunk_handlers);
Base class for autoreplaces/autorenews.
Loading for autoreplace chunks before table headers were added.
const SaveLoadCompat _engine_renew_sl_compat[]
Original field order for _engine_renew_desc.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
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.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:734
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Functions/types related to saving and loading games.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:530
@ EngineRenew
Load/save a reference to an engine renewal (autoreplace).
Definition saveload.h:648
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:533
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
#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
@ UngroupedVehicles
Saveload version: 71, SVN revision: 10567 Add a group with vehicles that aren't in any other group.
Definition saveload.h:129
@ VehicleGroups
Saveload version: 60, SVN revision: 9874 Arbitrary grouping, by the player, of vehicles.
Definition saveload.h:116
@ AutoreplaceWhenOldTreeLimit
Saveload version: 175, SVN revision: 24136 Autoreplace vehicle only when they are old,...
Definition saveload.h:254
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:421
@ 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
Definition of base types and functions in a cross-platform compatible way.
void FixPointers() const override
Fix the pointers.
void Load() const override
Load the chunk.
void Save() const override
Save the chunk.
Struct to store engine replacements.
static Pool::IterateWrapper< EngineRenew > Iterate(size_t from=0)
static T * CreateAtIndex(EngineRenewID index, Targs &&... args)
SaveLoad type struct.
Definition saveload.h:787
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:754
static constexpr VarType BOOL
Store a boolean (as int8).
Definition saveload.h:750