# HG changeset patch # User Loïc Guilloux # Date 2024-02-12 00:22:57 # Node ID ea3c121812c9084fd936e3e0931683f91d283609 # Parent 23b9eb52e276de35e0ec3bf21b1e91f8cda8acbb Change: [Script] Store randomizers in savegame (#12063) diff --git a/src/saveload/CMakeLists.txt b/src/saveload/CMakeLists.txt --- a/src/saveload/CMakeLists.txt +++ b/src/saveload/CMakeLists.txt @@ -30,6 +30,7 @@ add_files( oldloader.h oldloader_sl.cpp order_sl.cpp + randomizer_sl.cpp saveload.cpp saveload.h saveload_filter.h diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -255,8 +255,6 @@ static void InitializeWindowsAndCaches() UpdateAllVirtCoords(); ResetViewportAfterLoadGame(); - ScriptObject::InitializeRandomizers(); - for (Company *c : Company::Iterate()) { /* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it * accordingly if it is not the case. No need to set it on companies that are not been used already, @@ -3288,6 +3286,10 @@ bool AfterLoadGame() } } + if (IsSavegameVersionBefore(SLV_SCRIPT_RANDOMIZER)) { + ScriptObject::InitializeRandomizers(); + } + for (Company *c : Company::Iterate()) { UpdateCompanyLiveries(c); } diff --git a/src/saveload/randomizer_sl.cpp b/src/saveload/randomizer_sl.cpp new file mode 100644 --- /dev/null +++ b/src/saveload/randomizer_sl.cpp @@ -0,0 +1,51 @@ +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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 . + */ + +/** @file randomizer_sl.cpp Code handling saving and loading of script randomizers */ + +#include "../stdafx.h" +#include "../script/api/script_object.hpp" +#include "saveload.h" +#include "saveload_internal.h" +#include "../safeguards.h" + +static const SaveLoad _randomizer_desc[] = { + SLE_VAR(Randomizer, state[0], SLE_UINT32), + SLE_VAR(Randomizer, state[1], SLE_UINT32), +}; + +struct SRNDChunkHandler : ChunkHandler { + SRNDChunkHandler() : ChunkHandler('SRND', CH_TABLE) + {} + + void Save() const override + { + SlTableHeader(_randomizer_desc); + + for (Owner owner = OWNER_BEGIN; owner < OWNER_END; owner++) { + SlSetArrayIndex(owner); + SlObject(&ScriptObject::GetRandomizer(owner), _randomizer_desc); + } + } + + void Load() const override + { + SlTableHeader(_randomizer_desc); + + Owner index; + while ((index = (Owner)SlIterateArray()) != (Owner)-1) { + SlObject(&ScriptObject::GetRandomizer(index), _randomizer_desc); + } + } +}; + +static const SRNDChunkHandler SRND; +static const ChunkHandlerRef randomizer_chunk_handlers[] = { + SRND, +}; + +extern const ChunkHandlerTable _randomizer_chunk_handlers(randomizer_chunk_handlers); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -250,6 +250,7 @@ static const std::vector _chunk_handlers; diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -376,6 +376,7 @@ enum SaveLoadVersion : uint16_t { SLV_MAX_LOAN_FOR_COMPANY, ///< 330 PR#11224 Separate max loan for each company. SLV_DEPOT_UNBUNCHING, ///< 331 PR#11945 Allow unbunching shared order vehicles at a depot. SLV_AI_LOCAL_CONFIG, ///< 332 PR#12003 Config of running AI is stored inside Company. + SLV_SCRIPT_RANDOMIZER, ///< 333 PR#12063 Save script randomizers. SL_MAX_VERSION, ///< Highest possible saveload version };