-
-
Notifications
You must be signed in to change notification settings - Fork 844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: [Script] ScriptBase::Rand() return value was between -MIN(int32) and MAX(int32) #10443
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused_param doesn't really need to change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, but my idea would be to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then, as a reminder for a later (cleanup) story: remove the casts from squirrel_helper_type.hpp:49-51 as well as 57. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah you mean squirrel_helper.hpp |
||
{ | ||
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to use int64 than SQInteger? I always thought SQInteger was specifically used outside the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQInteger
is the type used in every squirrel internal functions (especiallysq_pushinteger()
to pass a number to the squirrel VM andsq_getinteger()
to get a number from the VM). It is indeed an int64, but I think using SQInteger is better as it hides implementation details from the API.