OpenTTD Source: src/saveload/autoreplace_sl.cpp Source File

OpenTTD Source 15.3

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
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/autoreplace_sl_compat.h"
14
15#include "../autoreplace_base.h"
16
17#include "../safeguards.h"
18
19static const SaveLoad _engine_renew_desc[] = {
20 SLE_VAR(EngineRenew, from, SLE_UINT16),
21 SLE_VAR(EngineRenew, to, SLE_UINT16),
22
23 SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS),
24 SLE_CONDVAR(EngineRenew, group_id, SLE_UINT16, SLV_60, SL_MAX_VERSION),
25 SLE_CONDVAR(EngineRenew, replace_when_old, SLE_BOOL, SLV_175, SL_MAX_VERSION),
26};
27
28 struct ERNWChunkHandler : ChunkHandler {
29 ERNWChunkHandler() : ChunkHandler('ERNW', CH_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 = new (EngineRenewID(index)) EngineRenew();
49 SlObject(er, slt);
50
51 /* Advanced vehicle lists, ungrouped vehicles got added */
52 if (IsSavegameVersionBefore(SLV_60)) {
53 er->group_id = ALL_GROUP;
54 } else if (IsSavegameVersionBefore(SLV_71)) {
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);
autoreplace_sl_compat.h
Loading for autoreplace chunks before table headers were added.
_engine_renew_sl_compat
const SaveLoadCompat _engine_renew_sl_compat[]
Original field order for _engine_renew_desc.
Definition autoreplace_sl_compat.h:16
DEFAULT_GROUP
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
ALL_GROUP
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
saveload.h
Functions/types related to saving and loading games.
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
REF_ENGINE_RENEWS
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition saveload.h:621
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLV_175
@ SLV_175
175 24136
Definition saveload.h:253
SLV_71
@ SLV_71
71 10567
Definition saveload.h:128
SLV_60
@ SLV_60
60 9874
Definition saveload.h:115
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
ERNWChunkHandler
Definition autoreplace_sl.cpp:28
ERNWChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition autoreplace_sl.cpp:60
ERNWChunkHandler::Load
void Load() const override
Load the chunk.
Definition autoreplace_sl.cpp:41
ERNWChunkHandler::Save
void Save() const override
Save the chunk.
Definition autoreplace_sl.cpp:31
EngineRenew
Struct to store engine replacements.
Definition autoreplace_base.h:33
PoolID
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
Pool::PoolItem<&_enginerenew_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
SaveLoad
SaveLoad type struct.
Definition saveload.h:732