OpenTTD Source  20240919-master-gdf0233f4c2
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);
Goal
Struct about goals, current and completed.
Definition: goal_base.h:21
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:922
_goals_sl_compat
const SaveLoadCompat _goals_sl_compat[]
Original field order for _goals_desc.
Definition: goal_sl_compat.h:16
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:501
goal_sl_compat.h
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:857
GOALChunkHandler::Save
void Save() const override
Save the chunk.
Definition: goal_sl.cpp:31
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
GOALChunkHandler
Definition: goal_sl.cpp:28
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:680
SLV_182
@ SLV_182
182 25115 FS#5492, r25259, r25296 Goal status
Definition: saveload.h:261
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:391
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:971
Pool::PoolItem<&_goal_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
GOALChunkHandler::Load
void Load() const override
Load the chunk.
Definition: goal_sl.cpp:41
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:1015
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
SaveLoad
SaveLoad type struct.
Definition: saveload.h:707
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658