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

OpenTTD Source 15.3

signs_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/signs_sl_compat.h"
14
15#include "../signs_base.h"
16#include "../fios.h"
17
18#include "../safeguards.h"
19
21 static const SaveLoad _sign_desc[] = {
22 SLE_CONDVAR(Sign, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
23 SLE_CONDSSTR(Sign, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
24 SLE_CONDVAR(Sign, x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5),
25 SLE_CONDVAR(Sign, y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5),
26 SLE_CONDVAR(Sign, x, SLE_INT32, SLV_5, SL_MAX_VERSION),
27 SLE_CONDVAR(Sign, y, SLE_INT32, SLV_5, SL_MAX_VERSION),
28 SLE_CONDVAR(Sign, owner, SLE_UINT8, SLV_6, SL_MAX_VERSION),
29 SLE_CONDVAR(Sign, z, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_164),
30 SLE_CONDVAR(Sign, z, SLE_INT32, SLV_164, SL_MAX_VERSION),
31};
32
33 struct SIGNChunkHandler : ChunkHandler {
34 SIGNChunkHandler() : ChunkHandler('SIGN', CH_TABLE) {}
35
36 void Save() const override
37 {
38 SlTableHeader(_sign_desc);
39
40 for (Sign *si : Sign::Iterate()) {
41 SlSetArrayIndex(si->index);
42 SlObject(si, _sign_desc);
43 }
44 }
45
46 void Load() const override
47 {
48 const std::vector<SaveLoad> slt = SlCompatTableHeader(_sign_desc, _sign_sl_compat);
49
50 int index;
51 while ((index = SlIterateArray()) != -1) {
52 Sign *si = new (SignID(index)) Sign();
53 SlObject(si, slt);
54 /* Before version 6.1, signs didn't have owner.
55 * Before version 83, invalid signs were determined by si->str == 0.
56 * Before version 103, owner could be a bankrupted company.
57 * - we can't use IsValidCompany() now, so this is fixed in AfterLoadGame()
58 * All signs that were saved are valid (including those with just 'Sign' and INVALID_OWNER).
59 * - so set owner to OWNER_NONE if needed (signs from pre-version 6.1 would be lost) */
60 if (IsSavegameVersionBefore(SLV_6, 1) || (IsSavegameVersionBefore(SLV_83) && si->owner == INVALID_OWNER)) {
61 si->owner = OWNER_NONE;
62 }
63
64 /* Signs placed in scenario editor shall now be OWNER_DEITY */
65 if (IsSavegameVersionBefore(SLV_171) && si->owner == OWNER_NONE && _file_to_saveload.ftype.abstract == FT_SCENARIO) {
66 si->owner = OWNER_DEITY;
67 }
68 }
69 }
70};
71
72static const SIGNChunkHandler SIGN;
73static const ChunkHandlerRef sign_chunk_handlers[] = {
74 SIGN,
75};
76
77extern const ChunkHandlerTable _sign_chunk_handlers(sign_chunk_handlers);
OWNER_DEITY
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:28
OWNER_NONE
static constexpr Owner OWNER_NONE
The tile has no ownership.
Definition company_type.h:26
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:20
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
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition saveload.cpp:78
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.
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:702
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:948
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_84
@ SLV_84
84 11822
Definition saveload.h:143
SLV_171
@ SLV_171
171 23835
Definition saveload.h:248
SLV_83
@ SLV_83
83 11589
Definition saveload.h:142
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition saveload.h:46
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SLV_164
@ SLV_164
164 23290
Definition saveload.h:239
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition saveload.h:43
_sign_desc
static const SaveLoad _sign_desc[]
Description of a sign within the savegame.
Definition signs_sl.cpp:21
signs_sl_compat.h
Loading of signs chunks before table headers were added.
_sign_sl_compat
const SaveLoadCompat _sign_sl_compat[]
Original field order for _sign_desc.
Definition signs_sl_compat.h:16
SignID
PoolID< uint16_t, struct SignIDTag, 64000, 0xFFFF > SignID
The type of the IDs of signs.
Definition signs_type.h:16
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
FileToSaveLoad::ftype
FiosType ftype
File type.
Definition saveload.h:429
FiosType::abstract
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:64
Pool::PoolItem<&_sign_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
SIGNChunkHandler
Definition signs_sl.cpp:33
SIGNChunkHandler::Load
void Load() const override
Load the chunk.
Definition signs_sl.cpp:46
SIGNChunkHandler::Save
void Save() const override
Save the chunk.
Definition signs_sl.cpp:36
SaveLoad
SaveLoad type struct.
Definition saveload.h:732
Sign
Definition signs_base.h:21