OpenTTD Source 20261129-master-g72a8bdc861
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"
13#include "saveload_internal.h"
15
16#include "../autoreplace_type.h"
17
18#include "../safeguards.h"
19
22
23struct ERNWChunkHandler : ChunkHandler {
24 ERNWChunkHandler() : ChunkHandler("ERNW", ChunkType::ReadOnly) {}
25
27 static inline const SaveLoad description[] = {
28 SaveLoad::Variable<VarFileType::U16>("from", SLE_OBJECT_ADDRESS(OldEngineRenewPool::Item, value.from)),
29 SaveLoad::Variable<VarFileType::U16>("to", SLE_OBJECT_ADDRESS(OldEngineRenewPool::Item, value.to)),
32 SaveLoad::Variable<VarFileType::U16>("group_id", SLE_OBJECT_ADDRESS(OldEngineRenewPool::Item, value.group_id), SaveLoadVersion::VehicleGroups),
33 SaveLoad::Variable<VarFileType::Bool>("replace_when_old", SLE_OBJECT_ADDRESS(OldEngineRenewPool::Item, value.replace_when_old), SaveLoadVersion::AutoreplaceWhenOldTreeLimit),
34 };
35
36 void Load() const override
37 {
38 const std::vector<SaveLoad> slt = SlCompatTableHeader(description, _engine_renew_sl_compat);
39
40 int index;
41
42 while ((index = SlIterateArray()) != -1) {
43 auto &item = _old_engine_renew_saveload_pool.Allocate(index);
44 SlObject(&item, slt);
45
46 /* Advanced vehicle lists, ungrouped vehicles got added */
48 item.value.group_id = ALL_GROUP;
50 if (item.value.group_id == DEFAULT_GROUP) item.value.group_id = ALL_GROUP;
51 }
52 /* Since SaveLoadVersion::EngineRenewsOwnership like-for-like replacements are always explicitly replace when old. */
53 if (item.value.from == item.value.to) item.value.replace_when_old = true;
54 }
55 }
56};
57
58static const ERNWChunkHandler ERNW;
59static const ChunkHandlerRef autoreplace_chunk_handlers[] = {
60 ERNW,
61};
62
63extern const ChunkHandlerTable _autoreplace_chunk_handlers(autoreplace_chunk_handlers);
OldEngineRenewPool _old_engine_renew_saveload_pool
Temporary storage for conversion from old engine renew pool.
Loading for autoreplace chunks before table headers were added.
const SaveLoadCompat _engine_renew_sl_compat[]
Original field order for _engine_renew_desc.
Types related to autoreplacing.
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:747
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Functions/types related to saving and loading games.
#define SLE_OBJECT_ADDRESS(base, variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a base c...
Definition saveload.h:752
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1223
@ MoreCargoPackets
Saveload version: 69, SVN revision: 10319 Allow more than ~65k cargo packets.
Definition saveload.h:130
@ UngroupedVehicles
Saveload version: 71, SVN revision: 10567 Add a group with vehicles that aren't in any other group.
Definition saveload.h:133
@ VehicleGroups
Saveload version: 60, SVN revision: 9874 Arbitrary grouping, by the player, of vehicles.
Definition saveload.h:120
@ AutoreplaceWhenOldTreeLimit
Saveload version: 175, SVN revision: 24136 Autoreplace vehicle only when they are old,...
Definition saveload.h:258
@ MinVersion
First savegame version.
Definition saveload.h:35
@ ReadOnly
Chunk is never saved.
Definition saveload.h:453
Declaration of functions used in more save/load files.
OldSaveLoadPool< OldEngineRenew > OldEngineRenewPool
Type for conversion of old engine renew pool.
Definition of base types and functions in a cross-platform compatible way.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
void Load() const override
Load the chunk.
static const SaveLoad description[]
Saveload description for handler.
SaveLoad type struct.
Definition saveload.h:762
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:923