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:
Loïc Guilloux
2023-02-10 19:55:59 +01:00
committed by GitHub
parent 7bd475b86c
commit 6b99b6672e
3 changed files with 96 additions and 87 deletions

View File

@@ -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 */