OpenTTD Source  20241108-master-g80f628063a
goal_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/goal_sl_compat.h"
14 
15 #include "../goal_base.h"
16 
17 #include "../safeguards.h"
18 
19 static const SaveLoad _goals_desc[] = {
20  SLE_VAR(Goal, company, SLE_FILE_U16 | SLE_VAR_U8),
21  SLE_VAR(Goal, type, SLE_FILE_U16 | SLE_VAR_U8),
22  SLE_VAR(Goal, dst, SLE_UINT32),
23  SLE_SSTR(Goal, text, SLE_STR | SLF_ALLOW_CONTROL),
25  SLE_CONDVAR(Goal, completed, SLE_BOOL, SLV_182, SL_MAX_VERSION),
26 };
27 
29  GOALChunkHandler() : ChunkHandler('GOAL', CH_TABLE) {}
30 
31  void Save() const override
32  {
33  SlTableHeader(_goals_desc);
34 
35  for (Goal *s : Goal::Iterate()) {
36  SlSetArrayIndex(s->index);
37  SlObject(s, _goals_desc);
38  }
39  }
40 
41  void Load() const override
42  {
43  const std::vector<SaveLoad> slt = SlCompatTableHeader(_goals_desc, _goals_sl_compat);
44 
45  int index;
46  while ((index = SlIterateArray()) != -1) {
47  Goal *s = new (index) Goal();
48  SlObject(s, slt);
49  }
50  }
51 };
52 
53 static const GOALChunkHandler GOAL;
54 static const ChunkHandlerRef goal_chunk_handlers[] = {
55  GOAL,
56 };
57 
58 extern const ChunkHandlerTable _goal_chunk_handlers(goal_chunk_handlers);
Loading of goal chunks before table headers were added.
const SaveLoadCompat _goals_sl_compat[]
Original field order for _goals_desc.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
Functions/types related to saving and loading games.
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:684
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:505
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:508
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:926
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:861
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:1019
@ SLV_182
182 25115 FS#5492, r25259, r25296 Goal status
Definition: saveload.h:261
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:395
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:975
Handlers and description of chunk.
Definition: saveload.h:459
void Save() const override
Save the chunk.
Definition: goal_sl.cpp:31
void Load() const override
Load the chunk.
Definition: goal_sl.cpp:41
Struct about goals, current and completed.
Definition: goal_base.h:21
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
SaveLoad type struct.
Definition: saveload.h:711