Fix: [Script] ScriptBase::Rand() return value was between -MIN(int32) and MAX(int32) (#10443)
Also ensure the parameters for ScriptBase::RandRange() and ScriptBase::Chance() are in [0-MAX(uint32)] range
This commit is contained in:
@@ -13,33 +13,36 @@
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
/* static */ uint32 ScriptBase::Rand()
|
||||
/* static */ SQInteger ScriptBase::Rand()
|
||||
{
|
||||
return ScriptObject::GetRandomizer().Next();
|
||||
}
|
||||
|
||||
/* static */ uint32 ScriptBase::RandItem(int unused_param)
|
||||
/* static */ SQInteger ScriptBase::RandItem(SQInteger unused_param)
|
||||
{
|
||||
return ScriptBase::Rand();
|
||||
}
|
||||
|
||||
/* static */ uint ScriptBase::RandRange(uint max)
|
||||
/* static */ SQInteger ScriptBase::RandRange(SQInteger max)
|
||||
{
|
||||
max = Clamp<SQInteger>(max, 0, UINT32_MAX);
|
||||
return ScriptObject::GetRandomizer().Next(max);
|
||||
}
|
||||
|
||||
/* static */ uint32 ScriptBase::RandRangeItem(int unused_param, uint max)
|
||||
/* static */ SQInteger ScriptBase::RandRangeItem(SQInteger unused_param, SQInteger max)
|
||||
{
|
||||
return ScriptBase::RandRange(max);
|
||||
}
|
||||
|
||||
/* static */ bool ScriptBase::Chance(uint out, uint max)
|
||||
/* static */ bool ScriptBase::Chance(SQInteger out, SQInteger max)
|
||||
{
|
||||
out = Clamp<SQInteger>(out, 0, UINT32_MAX);
|
||||
max = Clamp<SQInteger>(max, 0, UINT32_MAX);
|
||||
EnforcePrecondition(false, out <= max);
|
||||
return ScriptBase::RandRange(max) < out;
|
||||
}
|
||||
|
||||
/* static */ bool ScriptBase::ChanceItem(int unused_param, uint out, uint max)
|
||||
/* static */ bool ScriptBase::ChanceItem(SQInteger unused_param, SQInteger out, SQInteger max)
|
||||
{
|
||||
return ScriptBase::Chance(out, max);
|
||||
}
|
||||
|
@@ -25,50 +25,56 @@ public:
|
||||
* Get a random value.
|
||||
* @return A random value between 0 and MAX(uint32).
|
||||
*/
|
||||
static uint32 Rand();
|
||||
static SQInteger Rand();
|
||||
|
||||
/**
|
||||
* Get a random value.
|
||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
||||
* @return A random value between 0 and MAX(uint32).
|
||||
*/
|
||||
static uint32 RandItem(int unused_param);
|
||||
static SQInteger RandItem(SQInteger unused_param);
|
||||
|
||||
/**
|
||||
* Get a random value in a range.
|
||||
* @param max The first number this function will never return (the maximum it returns is max - 1).
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @return A random value between 0 .. max - 1.
|
||||
*/
|
||||
static uint RandRange(uint max);
|
||||
static SQInteger RandRange(SQInteger max);
|
||||
|
||||
/**
|
||||
* Get a random value in a range.
|
||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
||||
* @param max The first number this function will never return (the maximum it returns is max - 1).
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @return A random value between 0 .. max - 1.
|
||||
*/
|
||||
static uint RandRangeItem(int unused_param, uint max);
|
||||
static SQInteger RandRangeItem(SQInteger unused_param, SQInteger max);
|
||||
|
||||
/**
|
||||
* Returns approximately 'out' times true when called 'max' times.
|
||||
* After all, it is a random function.
|
||||
* @param out How many times it should return true.
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @param max Out of this many times.
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @pre \a out is at most equal to \a max.
|
||||
* @return True if the chance worked out.
|
||||
*/
|
||||
static bool Chance(uint out, uint max);
|
||||
static bool Chance(SQInteger out, SQInteger max);
|
||||
|
||||
/**
|
||||
* Returns approximately 'out' times true when called 'max' times.
|
||||
* After all, it is a random function.
|
||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
||||
* @param out How many times it should return true.
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @param max Out of this many times.
|
||||
* The value will be clamped to 0 .. MAX(uint32).
|
||||
* @pre \a out is at most equal to \a max.
|
||||
* @return True if the chance worked out.
|
||||
*/
|
||||
static bool ChanceItem(int unused_param, uint out, uint max);
|
||||
static bool ChanceItem(SQInteger unused_param, SQInteger out, SQInteger max);
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_BASE_HPP */
|
||||
|
Reference in New Issue
Block a user