Change: [Script] Store randomizers in savegame (#12063)

This commit is contained in:
Loïc Guilloux 2024-02-12 01:22:57 +01:00 committed by GitHub
parent d6f34a21e8
commit 3ffa176870
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 2 deletions

View File

@ -30,6 +30,7 @@ add_files(
oldloader.h
oldloader_sl.cpp
order_sl.cpp
randomizer_sl.cpp
saveload.cpp
saveload.h
saveload_filter.h

View File

@ -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);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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);

View File

@ -250,6 +250,7 @@ static const std::vector<ChunkHandlerRef> &ChunkHandlers()
extern const ChunkHandlerTable _object_chunk_handlers;
extern const ChunkHandlerTable _persistent_storage_chunk_handlers;
extern const ChunkHandlerTable _water_region_chunk_handlers;
extern const ChunkHandlerTable _randomizer_chunk_handlers;
/** List of all chunks in a savegame. */
static const ChunkHandlerTable _chunk_handler_tables[] = {
@ -288,6 +289,7 @@ static const std::vector<ChunkHandlerRef> &ChunkHandlers()
_object_chunk_handlers,
_persistent_storage_chunk_handlers,
_water_region_chunk_handlers,
_randomizer_chunk_handlers,
};
static std::vector<ChunkHandlerRef> _chunk_handlers;

View File

@ -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
};