OpenTTD Source  20241108-master-g80f628063a
randomizer_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 "../script/api/script_object.hpp"
12 #include "saveload.h"
13 #include "saveload_internal.h"
14 #include "../safeguards.h"
15 
16 static const SaveLoad _randomizer_desc[] = {
17  SLE_VAR(Randomizer, state[0], SLE_UINT32),
18  SLE_VAR(Randomizer, state[1], SLE_UINT32),
19 };
20 
22  SRNDChunkHandler() : ChunkHandler('SRND', CH_TABLE)
23  {}
24 
25  void Save() const override
26  {
27  SlTableHeader(_randomizer_desc);
28 
29  for (Owner owner = OWNER_BEGIN; owner < OWNER_END; owner++) {
30  SlSetArrayIndex(owner);
31  SlObject(&ScriptObject::GetRandomizer(owner), _randomizer_desc);
32  }
33  }
34 
35  void Load() const override
36  {
37  SlTableHeader(_randomizer_desc);
38 
39  Owner index;
40  while ((index = (Owner)SlIterateArray()) != (Owner)-1) {
41  SlObject(&ScriptObject::GetRandomizer(index), _randomizer_desc);
42  }
43  }
44 };
45 
46 static const SRNDChunkHandler SRND;
47 static const ChunkHandlerRef randomizer_chunk_handlers[] = {
48  SRND,
49 };
50 
51 extern const ChunkHandlerTable _randomizer_chunk_handlers(randomizer_chunk_handlers);
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ OWNER_END
Last + 1 owner.
Definition: company_type.h:28
@ OWNER_BEGIN
First owner.
Definition: company_type.h:21
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
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.
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_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:975
Declaration of functions used in more save/load files.
Handlers and description of chunk.
Definition: saveload.h:459
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:27
void Save() const override
Save the chunk.
void Load() const override
Load the chunk.
SaveLoad type struct.
Definition: saveload.h:711