Codechange: move choice for randomizer of scripts to a single location

This commit is contained in:
Rubidium 2023-01-13 23:48:59 +01:00 committed by rubidium42
parent 3373128233
commit 6abad681bd
7 changed files with 23 additions and 19 deletions

View File

@ -10,17 +10,12 @@
#include "../../stdafx.h" #include "../../stdafx.h"
#include "script_base.hpp" #include "script_base.hpp"
#include "script_error.hpp" #include "script_error.hpp"
#include "../../network/network.h"
#include "../../core/random_func.hpp"
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ uint32 ScriptBase::Rand() /* static */ uint32 ScriptBase::Rand()
{ {
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over) return ScriptObject::GetRandomizer().Next();
* but we pick InteractiveRandomRange if we are a network_server or network-client. */
if (_networking) return ::InteractiveRandom();
return ::Random();
} }
/* static */ uint32 ScriptBase::RandItem(int unused_param) /* static */ uint32 ScriptBase::RandItem(int unused_param)
@ -30,10 +25,7 @@
/* static */ uint ScriptBase::RandRange(uint max) /* static */ uint ScriptBase::RandRange(uint max)
{ {
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over) return ScriptObject::GetRandomizer().Next(max);
* but we pick InteractiveRandomRange if we are a network_server or network-client. */
if (_networking) return ::InteractiveRandomRange(max);
return ::RandomRange(max);
} }
/* static */ uint32 ScriptBase::RandRangeItem(int unused_param, uint max) /* static */ uint32 ScriptBase::RandRangeItem(int unused_param, uint max)

View File

@ -18,9 +18,6 @@
* *
* @note The random functions are not called Random and RandomRange, because * @note The random functions are not called Random and RandomRange, because
* RANDOM_DEBUG does some tricky stuff, which messes with those names. * 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 { class ScriptBase : public ScriptObject {
public: public:

View File

@ -97,9 +97,10 @@
EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE); EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE);
EnforcePrecondition(false, GetPresidentGender(ScriptCompany::COMPANY_SELF) != gender); EnforcePrecondition(false, GetPresidentGender(ScriptCompany::COMPANY_SELF) != gender);
Randomizer &randomizer = ScriptObject::GetRandomizer();
CompanyManagerFace cmf; CompanyManagerFace cmf;
GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (::InteractiveRandom() & (1 << ETHNICITY_BLACK))); GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (randomizer.Next() & (1 << ETHNICITY_BLACK)));
RandomCompanyManagerFaceBits(cmf, ge, false, _interactive_random); RandomCompanyManagerFaceBits(cmf, ge, false, randomizer);
return ScriptObject::Command<CMD_SET_COMPANY_MANAGER_FACE>::Do(cmf); return ScriptObject::Command<CMD_SET_COMPANY_MANAGER_FACE>::Do(cmf);
} }

View File

@ -9,6 +9,7 @@
#include "../../stdafx.h" #include "../../stdafx.h"
#include "script_industrytype.hpp" #include "script_industrytype.hpp"
#include "script_base.hpp"
#include "script_map.hpp" #include "script_map.hpp"
#include "script_error.hpp" #include "script_error.hpp"
#include "../../strings_func.h" #include "../../strings_func.h"
@ -121,8 +122,8 @@
EnforcePrecondition(false, CanBuildIndustry(industry_type)); EnforcePrecondition(false, CanBuildIndustry(industry_type));
EnforcePrecondition(false, ScriptMap::IsValidTile(tile)); EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
uint32 seed = ::InteractiveRandom(); uint32 seed = ScriptBase::Rand();
uint32 layout_index = ::InteractiveRandomRange((uint32)::GetIndustrySpec(industry_type)->layouts.size()); uint32 layout_index = ScriptBase::RandRange((uint32)::GetIndustrySpec(industry_type)->layouts.size());
return ScriptObject::Command<CMD_BUILD_INDUSTRY>::Do(tile, industry_type, layout_index, true, seed); return ScriptObject::Command<CMD_BUILD_INDUSTRY>::Do(tile, industry_type, layout_index, true, seed);
} }
@ -130,7 +131,7 @@
{ {
EnforcePrecondition(false, CanProspectIndustry(industry_type)); EnforcePrecondition(false, CanProspectIndustry(industry_type));
uint32 seed = ::InteractiveRandom(); uint32 seed = ScriptBase::Rand();
return ScriptObject::Command<CMD_BUILD_INDUSTRY>::Do(0, industry_type, 0, false, seed); return ScriptObject::Command<CMD_BUILD_INDUSTRY>::Do(0, industry_type, 0, false, seed);
} }

View File

@ -311,3 +311,10 @@ bool ScriptObject::DoCommandProcessResult(const CommandCost &res, Script_Suspend
NOT_REACHED(); 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;
}

View File

@ -15,6 +15,7 @@
#include "../../rail_type.h" #include "../../rail_type.h"
#include "../../string_func.h" #include "../../string_func.h"
#include "../../command_func.h" #include "../../command_func.h"
#include "../../core/random_func.hpp"
#include "script_types.hpp" #include "script_types.hpp"
#include "../script_suspend.hpp" #include "../script_suspend.hpp"
@ -73,6 +74,11 @@ public:
*/ */
static class ScriptInstance *GetActiveInstance(); static class ScriptInstance *GetActiveInstance();
/**
* Get a reference of the randomizer that brings this script random values.
*/
static Randomizer &GetRandomizer();
protected: protected:
template<Commands TCmd, typename T> struct ScriptDoCommandHelper; template<Commands TCmd, typename T> struct ScriptDoCommandHelper;

View File

@ -301,7 +301,7 @@
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_TOWN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG); EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_TOWN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
} }
uint32 townnameparts; uint32 townnameparts;
if (!GenerateTownName(_interactive_random, &townnameparts)) { if (!GenerateTownName(ScriptObject::GetRandomizer(), &townnameparts)) {
ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE); ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE);
return false; return false;
} }