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

OpenTTD Source 14.0-beta1

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 <http://www.gnu.org/licenses/>.
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 (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.abstract_ftype == FT_SCENARIO) {
66 si->owner = OWNER_DEITY;
67 }
68 }
69 }
70 };
71
72 static const SIGNChunkHandler SIGN;
73 static const ChunkHandlerRef sign_chunk_handlers[] = {
74 SIGN,
75 };
76
77 extern const ChunkHandlerTable _sign_chunk_handlers(sign_chunk_handlers);
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition: fileio_type.h:19
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:909
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:488
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:844
SLV_83
@ SLV_83
83 11589
Definition: saveload.h:142
saveload.h
_sign_sl_compat
const SaveLoadCompat _sign_sl_compat[]
Original field order for _sign_desc.
Definition: signs_sl_compat.h:16
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:442
SIGNChunkHandler
Definition: signs_sl.cpp:33
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
FileToSaveLoad::abstract_ftype
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:393
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:667
_sign_desc
static const SaveLoad _sign_desc[]
Description of a sign within the savegame.
Definition: signs_sl.cpp:21
SIGNChunkHandler::Load
void Load() const override
Load the chunk.
Definition: signs_sl.cpp:46
signs_sl_compat.h
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1203
SLV_171
@ SLV_171
171 23835
Definition: saveload.h:248
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:379
SLV_164
@ SLV_164
164 23290
Definition: saveload.h:239
SLV_84
@ SLV_84
84 11822
Definition: saveload.h:143
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:384
SIGNChunkHandler::Save
void Save() const override
Save the chunk.
Definition: signs_sl.cpp:36
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:60
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
Sign
Definition: signs_base.h:21
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:491
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1843
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1652
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1705
SaveLoad
SaveLoad type struct.
Definition: saveload.h:694
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:647