OpenTTD Source  20240919-master-gdf0233f4c2
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);
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:501
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
OWNER_END
@ OWNER_END
Last + 1 owner.
Definition: company_type.h:28
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:971
SRNDChunkHandler
Definition: randomizer_sl.cpp:21
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:27
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
SRNDChunkHandler::Save
void Save() const override
Save the chunk.
Definition: randomizer_sl.cpp:25
SRNDChunkHandler::Load
void Load() const override
Load the chunk.
Definition: randomizer_sl.cpp:35
saveload_internal.h
OWNER_BEGIN
@ OWNER_BEGIN
First owner.
Definition: company_type.h:21
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