Add: script specific Randomizer instances

This commit is contained in:
Rubidium
2023-01-14 13:26:17 +01:00
committed by rubidium42
parent 6abad681bd
commit c5ff61c5f2
5 changed files with 167 additions and 147 deletions

View File

@@ -96,6 +96,7 @@ static void _GenerateWorld()
_random.SetSeed(_settings_game.game_creation.generation_seed);
SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
ScriptObject::InitializeRandomizers();
BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP);

View File

@@ -251,6 +251,8 @@ 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,

View File

@@ -312,9 +312,18 @@ bool ScriptObject::DoCommandProcessResult(const CommandCost &res, Script_Suspend
NOT_REACHED();
}
Randomizer &ScriptObject::GetRandomizer()
/* static */ Randomizer ScriptObject::random_states[OWNER_END];
Randomizer &ScriptObject::GetRandomizer(Owner owner)
{
/* We pick _random if we are in SP (so when saved, we do the same over and over)
* but we pick _interactive_random if we are a network_server or network-client. */
return _networking ? _interactive_random : _random;
return ScriptObject::random_states[owner];
}
void ScriptObject::InitializeRandomizers()
{
Randomizer random = _random;
for (Owner owner = OWNER_BEGIN; owner < OWNER_END; owner++) {
ScriptObject::GetRandomizer(owner).SetSeed(random.Next());
}
}

View File

@@ -76,8 +76,15 @@ public:
/**
* Get a reference of the randomizer that brings this script random values.
* @param owner The owner/script to get the randomizer for. This defaults to ScriptObject::GetRootCompany()
*/
static Randomizer &GetRandomizer();
static Randomizer &GetRandomizer(Owner owner = ScriptObject::GetRootCompany());
/**
* Initialize/reset the script random states. The state of the scripts are
* based on the current _random seed, but _random does not get changed.
*/
static void InitializeRandomizers();
protected:
template<Commands TCmd, typename T> struct ScriptDoCommandHelper;
@@ -279,6 +286,7 @@ private:
static std::tuple<bool, bool, bool> DoCommandPrep();
static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only);
static CommandCallbackData *GetDoCommandCallback();
static Randomizer random_states[OWNER_END]; ///< Random states for each of the scripts (game script uses OWNER_DEITY)
};
namespace ScriptObjectInternal {