OpenTTD Source 20241224-master-gf74b0cf984
group_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 "../group.h"
12#include "../company_base.h"
13
14#include "saveload.h"
16
17#include "../safeguards.h"
18
19static const SaveLoad _group_desc[] = {
20 SLE_CONDVAR(Group, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
22 SLE_VAR(Group, owner, SLE_UINT8),
23 SLE_VAR(Group, vehicle_type, SLE_UINT8),
24 SLE_VAR(Group, flags, SLE_UINT8),
25 SLE_CONDVAR(Group, livery.in_use, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION),
26 SLE_CONDVAR(Group, livery.colour1, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION),
27 SLE_CONDVAR(Group, livery.colour2, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION),
28 SLE_CONDVAR(Group, parent, SLE_UINT16, SLV_189, SL_MAX_VERSION),
30};
31
33 GRPSChunkHandler() : ChunkHandler('GRPS', CH_TABLE) {}
34
35 void Save() const override
36 {
37 SlTableHeader(_group_desc);
38
39 for (Group *g : Group::Iterate()) {
40 SlSetArrayIndex(g->index);
41 SlObject(g, _group_desc);
42 }
43 }
44
45
46 void Load() const override
47 {
48 const std::vector<SaveLoad> slt = SlCompatTableHeader(_group_desc, _group_sl_compat);
49
50 int index;
51
52 while ((index = SlIterateArray()) != -1) {
53 Group *g = new (index) Group();
54 SlObject(g, slt);
55
57 }
58 }
59};
60
61static const GRPSChunkHandler GRPS;
62static const ChunkHandlerRef group_chunk_handlers[] = {
63 GRPS,
64};
65
66extern const ChunkHandlerTable _group_chunk_handlers(group_chunk_handlers);
Loading of group chunks before table headers were added.
const SaveLoadCompat _group_sl_compat[]
Original field order for _group_desc.
static const GroupID INVALID_GROUP
Sentinel for invalid groups.
Definition group_type.h:18
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:659
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.
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:688
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:509
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:512
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:933
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:868
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1263
@ SLV_84
84 11822
Definition saveload.h:143
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:399
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
@ SLV_GROUP_LIVERIES
205 PR#7108 Livery storage change and group liveries.
Definition saveload.h:290
@ SLV_189
189 26450 Hierarchical vehicle subgroups
Definition saveload.h:269
@ SLV_GROUP_NUMBERS
336 PR#12297 Add per-company group numbers.
Definition saveload.h:383
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1002
Handlers and description of chunk.
Definition saveload.h:463
void Load() const override
Load the chunk.
Definition group_sl.cpp:46
void Save() const override
Save the chunk.
Definition group_sl.cpp:35
Group data.
Definition group.h:72
GroupID parent
Parent group.
Definition group.h:83
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
SaveLoad type struct.
Definition saveload.h:717