mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: move choice for randomizer of scripts to a single location
This commit is contained in:
parent
3373128233
commit
6abad681bd
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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<CMD_SET_COMPANY_MANAGER_FACE>::Do(cmf);
|
||||
}
|
||||
|
|
|
@ -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<CMD_BUILD_INDUSTRY>::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<CMD_BUILD_INDUSTRY>::Do(0, industry_type, 0, false, seed);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<Commands TCmd, typename T> struct ScriptDoCommandHelper;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue