diff --git a/src/script/api/script_base.cpp b/src/script/api/script_base.cpp index 996d4d488f..52946520a2 100644 --- a/src/script/api/script_base.cpp +++ b/src/script/api/script_base.cpp @@ -10,17 +10,12 @@ #include "../../stdafx.h" #include "script_base.hpp" #include "script_error.hpp" -#include "../../network/network.h" -#include "../../core/random_func.hpp" #include "../../safeguards.h" /* static */ uint32 ScriptBase::Rand() { - /* We pick RandomRange if we are in SP (so when saved, we do the same over and over) - * but we pick InteractiveRandomRange if we are a network_server or network-client. */ - if (_networking) return ::InteractiveRandom(); - return ::Random(); + return ScriptObject::GetRandomizer().Next(); } /* static */ uint32 ScriptBase::RandItem(int unused_param) @@ -30,10 +25,7 @@ /* static */ uint ScriptBase::RandRange(uint max) { - /* We pick RandomRange if we are in SP (so when saved, we do the same over and over) - * but we pick InteractiveRandomRange if we are a network_server or network-client. */ - if (_networking) return ::InteractiveRandomRange(max); - return ::RandomRange(max); + return ScriptObject::GetRandomizer().Next(max); } /* static */ uint32 ScriptBase::RandRangeItem(int unused_param, uint max) diff --git a/src/script/api/script_base.hpp b/src/script/api/script_base.hpp index b8eebdf1b0..4184e79d69 100644 --- a/src/script/api/script_base.hpp +++ b/src/script/api/script_base.hpp @@ -18,9 +18,6 @@ * * @note The random functions are not called Random and RandomRange, because * RANDOM_DEBUG does some tricky stuff, which messes with those names. - * @note In MP we cannot use Random because that will cause desyncs (scripts are - * ran on the server only, not on all clients). This means that - * we use InteractiveRandom in MP. Rand() takes care of this for you. */ class ScriptBase : public ScriptObject { public: diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp index c38f85385a..33c0c0ea48 100644 --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -97,9 +97,10 @@ EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE); EnforcePrecondition(false, GetPresidentGender(ScriptCompany::COMPANY_SELF) != gender); + Randomizer &randomizer = ScriptObject::GetRandomizer(); CompanyManagerFace cmf; - GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (::InteractiveRandom() & (1 << ETHNICITY_BLACK))); - RandomCompanyManagerFaceBits(cmf, ge, false, _interactive_random); + GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (randomizer.Next() & (1 << ETHNICITY_BLACK))); + RandomCompanyManagerFaceBits(cmf, ge, false, randomizer); return ScriptObject::Command::Do(cmf); } diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp index 14b9a3f67a..3101ac7fdc 100644 --- a/src/script/api/script_industrytype.cpp +++ b/src/script/api/script_industrytype.cpp @@ -9,6 +9,7 @@ #include "../../stdafx.h" #include "script_industrytype.hpp" +#include "script_base.hpp" #include "script_map.hpp" #include "script_error.hpp" #include "../../strings_func.h" @@ -121,8 +122,8 @@ EnforcePrecondition(false, CanBuildIndustry(industry_type)); EnforcePrecondition(false, ScriptMap::IsValidTile(tile)); - uint32 seed = ::InteractiveRandom(); - uint32 layout_index = ::InteractiveRandomRange((uint32)::GetIndustrySpec(industry_type)->layouts.size()); + uint32 seed = ScriptBase::Rand(); + uint32 layout_index = ScriptBase::RandRange((uint32)::GetIndustrySpec(industry_type)->layouts.size()); return ScriptObject::Command::Do(tile, industry_type, layout_index, true, seed); } @@ -130,7 +131,7 @@ { EnforcePrecondition(false, CanProspectIndustry(industry_type)); - uint32 seed = ::InteractiveRandom(); + uint32 seed = ScriptBase::Rand(); return ScriptObject::Command::Do(0, industry_type, 0, false, seed); } diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 7d1042156c..2c798df9ab 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -311,3 +311,10 @@ bool ScriptObject::DoCommandProcessResult(const CommandCost &res, Script_Suspend NOT_REACHED(); } + +Randomizer &ScriptObject::GetRandomizer() +{ + /* 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; +} diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 96c78acb7f..8b104a2e13 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -15,6 +15,7 @@ #include "../../rail_type.h" #include "../../string_func.h" #include "../../command_func.h" +#include "../../core/random_func.hpp" #include "script_types.hpp" #include "../script_suspend.hpp" @@ -73,6 +74,11 @@ public: */ static class ScriptInstance *GetActiveInstance(); + /** + * Get a reference of the randomizer that brings this script random values. + */ + static Randomizer &GetRandomizer(); + protected: template struct ScriptDoCommandHelper; diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp index e3894b24ec..7138e22e79 100644 --- a/src/script/api/script_town.cpp +++ b/src/script/api/script_town.cpp @@ -301,7 +301,7 @@ EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_TOWN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG); } uint32 townnameparts; - if (!GenerateTownName(_interactive_random, &townnameparts)) { + if (!GenerateTownName(ScriptObject::GetRandomizer(), &townnameparts)) { ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE); return false; }