mirror of https://github.com/OpenTTD/OpenTTD
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)] rangepull/10464/head
parent
7bd475b86c
commit
6b99b6672e
|
@ -88,9 +88,9 @@
|
||||||
abs( 21): 21
|
abs( 21): 21
|
||||||
|
|
||||||
--AIBase--
|
--AIBase--
|
||||||
Rand(): -2062310602
|
Rand(): 2232656694
|
||||||
Rand(): -1780331126
|
Rand(): 2514636170
|
||||||
Rand(): -397928569
|
Rand(): 3897038727
|
||||||
RandRange(0): 0
|
RandRange(0): 0
|
||||||
RandRange(0): 0
|
RandRange(0): 0
|
||||||
RandRange(0): 0
|
RandRange(0): 0
|
||||||
|
@ -421,36 +421,36 @@
|
||||||
1099 => 46158
|
1099 => 46158
|
||||||
Randomize ListDump:
|
Randomize ListDump:
|
||||||
1 => 688298322
|
1 => 688298322
|
||||||
2 => -1709546982
|
2 => 2585420314
|
||||||
1000 => 1701392078
|
1000 => 1701392078
|
||||||
1001 => -1630848421
|
1001 => 2664118875
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1003 => -196324972
|
1003 => 4098642324
|
||||||
1004 => -436037402
|
1004 => 3858929894
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
1006 => -1485224804
|
1006 => 2809742492
|
||||||
1007 => -311036236
|
1007 => 3983931060
|
||||||
1008 => -1503442439
|
1008 => 2791524857
|
||||||
1009 => -110945695
|
1009 => 4184021601
|
||||||
1010 => -82825175
|
1010 => 4212142121
|
||||||
1011 => 46859773
|
1011 => 46859773
|
||||||
1012 => -1199223018
|
1012 => 3095744278
|
||||||
1013 => -1190555925
|
1013 => 3104411371
|
||||||
1014 => 326384434
|
1014 => 326384434
|
||||||
1015 => 1486817960
|
1015 => 1486817960
|
||||||
1016 => -1411425597
|
1016 => 2883541699
|
||||||
1017 => -508426854
|
1017 => 3786540442
|
||||||
1018 => 820019294
|
1018 => 820019294
|
||||||
1019 => 710762995
|
1019 => 710762995
|
||||||
1020 => -760867032
|
1020 => 3534100264
|
||||||
1021 => -709611146
|
1021 => 3585356150
|
||||||
1022 => 732190215
|
1022 => 732190215
|
||||||
1023 => 236336673
|
1023 => 236336673
|
||||||
1024 => 740596257
|
1024 => 740596257
|
||||||
1025 => 1135321785
|
1025 => 1135321785
|
||||||
1026 => 2067474156
|
1026 => 2067474156
|
||||||
1027 => -1395683607
|
1027 => 2899283689
|
||||||
1028 => -240528699
|
1028 => 4054438597
|
||||||
1029 => 928616892
|
1029 => 928616892
|
||||||
1030 => 1712486685
|
1030 => 1712486685
|
||||||
1031 => 1994118287
|
1031 => 1994118287
|
||||||
|
@ -462,46 +462,46 @@
|
||||||
1037 => 1359697121
|
1037 => 1359697121
|
||||||
1038 => 1888433963
|
1038 => 1888433963
|
||||||
1039 => 941623020
|
1039 => 941623020
|
||||||
1040 => -1925663292
|
1040 => 2369304004
|
||||||
1041 => -771540264
|
1041 => 3523427032
|
||||||
1042 => -1058341359
|
1042 => 3236625937
|
||||||
1043 => 182127597
|
1043 => 182127597
|
||||||
1044 => 646955927
|
1044 => 646955927
|
||||||
1045 => -1424621714
|
1045 => 2870345582
|
||||||
1046 => 623062612
|
1046 => 623062612
|
||||||
1047 => -1986955586
|
1047 => 2308011710
|
||||||
1048 => -1268826980
|
1048 => 3026140316
|
||||||
1049 => -456776220
|
1049 => 3838191076
|
||||||
1051 => -1112555329
|
1051 => 3182411967
|
||||||
1052 => -1532134052
|
1052 => 2762833244
|
||||||
1053 => 1960404034
|
1053 => 1960404034
|
||||||
1054 => 1573325453
|
1054 => 1573325453
|
||||||
1055 => -316619303
|
1055 => 3978347993
|
||||||
1056 => 699712177
|
1056 => 699712177
|
||||||
1057 => 863274966
|
1057 => 863274966
|
||||||
1058 => 1728276475
|
1058 => 1728276475
|
||||||
1059 => -246695889
|
1059 => 4048271407
|
||||||
1060 => 1919485436
|
1060 => 1919485436
|
||||||
1061 => 111273464
|
1061 => 111273464
|
||||||
1062 => 125435213
|
1062 => 125435213
|
||||||
1063 => 155132602
|
1063 => 155132602
|
||||||
1064 => -171674076
|
1064 => 4123293220
|
||||||
1065 => 655046914
|
1065 => 655046914
|
||||||
1066 => 1577399562
|
1066 => 1577399562
|
||||||
1067 => 1028818150
|
1067 => 1028818150
|
||||||
1068 => 447058239
|
1068 => 447058239
|
||||||
1069 => -1057920269
|
1069 => 3237047027
|
||||||
1070 => -1326215323
|
1070 => 2968751973
|
||||||
1071 => -198688588
|
1071 => 4096278708
|
||||||
1072 => 1523643051
|
1072 => 1523643051
|
||||||
1073 => 231373233
|
1073 => 231373233
|
||||||
1074 => 1121759962
|
1074 => 1121759962
|
||||||
1075 => 1449439846
|
1075 => 1449439846
|
||||||
1076 => -1615270753
|
1076 => 2679696543
|
||||||
1077 => -1509293864
|
1077 => 2785673432
|
||||||
1078 => 2116903943
|
1078 => 2116903943
|
||||||
1079 => 672822173
|
1079 => 672822173
|
||||||
1080 => -969573911
|
1080 => 3325393385
|
||||||
1081 => 1589904755
|
1081 => 1589904755
|
||||||
1082 => 1148782015
|
1082 => 1148782015
|
||||||
1083 => 663503316
|
1083 => 663503316
|
||||||
|
@ -510,54 +510,54 @@
|
||||||
1086 => 402172048
|
1086 => 402172048
|
||||||
1087 => 1812250453
|
1087 => 1812250453
|
||||||
1088 => 667300501
|
1088 => 667300501
|
||||||
1089 => -1838825777
|
1089 => 2456141519
|
||||||
1090 => -856474776
|
1090 => 3438492520
|
||||||
1091 => 420696035
|
1091 => 420696035
|
||||||
1092 => 2131427774
|
1092 => 2131427774
|
||||||
1093 => -435303548
|
1093 => 3859663748
|
||||||
1094 => -160883878
|
1094 => 4134083418
|
||||||
1095 => 1969629634
|
1095 => 1969629634
|
||||||
1096 => -555794155
|
1096 => 3739173141
|
||||||
1097 => -835119691
|
1097 => 3459847605
|
||||||
1098 => -1460907909
|
1098 => 2834059387
|
||||||
1099 => -1146924084
|
1099 => 3148043212
|
||||||
KeepTop(10):
|
KeepTop(10):
|
||||||
1 => 688298322
|
1 => 688298322
|
||||||
2 => -1709546982
|
2 => 2585420314
|
||||||
1000 => 1701392078
|
1000 => 1701392078
|
||||||
1001 => -1630848421
|
1001 => 2664118875
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1003 => -196324972
|
1003 => 4098642324
|
||||||
1004 => -436037402
|
1004 => 3858929894
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
1006 => -1485224804
|
1006 => 2809742492
|
||||||
1007 => -311036236
|
1007 => 3983931060
|
||||||
KeepBottom(8):
|
KeepBottom(8):
|
||||||
1000 => 1701392078
|
1000 => 1701392078
|
||||||
1001 => -1630848421
|
1001 => 2664118875
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1003 => -196324972
|
1003 => 4098642324
|
||||||
1004 => -436037402
|
1004 => 3858929894
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
1006 => -1485224804
|
1006 => 2809742492
|
||||||
1007 => -311036236
|
1007 => 3983931060
|
||||||
RemoveBottom(2):
|
RemoveBottom(2):
|
||||||
1000 => 1701392078
|
1000 => 1701392078
|
||||||
1001 => -1630848421
|
1001 => 2664118875
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1003 => -196324972
|
1003 => 4098642324
|
||||||
1004 => -436037402
|
1004 => 3858929894
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
RemoveTop(2):
|
RemoveTop(2):
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1003 => -196324972
|
1003 => 4098642324
|
||||||
1004 => -436037402
|
1004 => 3858929894
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
RemoveList({1003, 1004}):
|
RemoveList({1003, 1004}):
|
||||||
1002 => -886500935
|
1002 => 3408466361
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
KeepList({1003, 1004, 1005}):
|
KeepList({1003, 1004, 1005}):
|
||||||
1005 => -520341784
|
1005 => 3774625512
|
||||||
AddList({1005, 4000, 4001, 4002}):
|
AddList({1005, 4000, 4001, 4002}):
|
||||||
1005 => 1005
|
1005 => 1005
|
||||||
4000 => 8000
|
4000 => 8000
|
||||||
|
|
|
@ -13,33 +13,36 @@
|
||||||
|
|
||||||
#include "../../safeguards.h"
|
#include "../../safeguards.h"
|
||||||
|
|
||||||
/* static */ uint32 ScriptBase::Rand()
|
/* static */ SQInteger ScriptBase::Rand()
|
||||||
{
|
{
|
||||||
return ScriptObject::GetRandomizer().Next();
|
return ScriptObject::GetRandomizer().Next();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ uint32 ScriptBase::RandItem(int unused_param)
|
/* static */ SQInteger ScriptBase::RandItem(SQInteger unused_param)
|
||||||
{
|
{
|
||||||
return ScriptBase::Rand();
|
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);
|
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);
|
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);
|
EnforcePrecondition(false, out <= max);
|
||||||
return ScriptBase::RandRange(max) < out;
|
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);
|
return ScriptBase::Chance(out, max);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,50 +25,56 @@ public:
|
||||||
* Get a random value.
|
* Get a random value.
|
||||||
* @return A random value between 0 and MAX(uint32).
|
* @return A random value between 0 and MAX(uint32).
|
||||||
*/
|
*/
|
||||||
static uint32 Rand();
|
static SQInteger Rand();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a random value.
|
* Get a random value.
|
||||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
* @param unused_param This parameter is not used, but is needed to work with lists.
|
||||||
* @return A random value between 0 and MAX(uint32).
|
* @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.
|
* Get a random value in a range.
|
||||||
* @param max The first number this function will never return (the maximum it returns is max - 1).
|
* @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.
|
* @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.
|
* Get a random value in a range.
|
||||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
* @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).
|
* @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.
|
* @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.
|
* Returns approximately 'out' times true when called 'max' times.
|
||||||
* After all, it is a random function.
|
* After all, it is a random function.
|
||||||
* @param out How many times it should return true.
|
* @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.
|
* @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.
|
* @pre \a out is at most equal to \a max.
|
||||||
* @return True if the chance worked out.
|
* @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.
|
* Returns approximately 'out' times true when called 'max' times.
|
||||||
* After all, it is a random function.
|
* After all, it is a random function.
|
||||||
* @param unused_param This parameter is not used, but is needed to work with lists.
|
* @param unused_param This parameter is not used, but is needed to work with lists.
|
||||||
* @param out How many times it should return true.
|
* @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.
|
* @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.
|
* @pre \a out is at most equal to \a max.
|
||||||
* @return True if the chance worked out.
|
* @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 */
|
#endif /* SCRIPT_BASE_HPP */
|
||||||
|
|
Loading…
Reference in New Issue