From c5ff61c5f2c30748ea03825685f0d9d68c0952de Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 14 Jan 2023 13:26:17 +0100 Subject: [PATCH] Add: script specific Randomizer instances --- regression/regression/result.txt | 284 +++++++++++++++---------------- src/genworld.cpp | 1 + src/saveload/afterload.cpp | 2 + src/script/api/script_object.cpp | 17 +- src/script/api/script_object.hpp | 10 +- 5 files changed, 167 insertions(+), 147 deletions(-) diff --git a/regression/regression/result.txt b/regression/regression/result.txt index dc3e294844..7ce9550599 100644 --- a/regression/regression/result.txt +++ b/regression/regression/result.txt @@ -88,24 +88,24 @@ abs( 21): 21 --AIBase-- - Rand(): -54346916 - Rand(): -937374575 - Rand(): 823953997 + Rand(): -2062310602 + Rand(): -1780331126 + Rand(): -397928569 RandRange(0): 0 RandRange(0): 0 RandRange(0): 0 RandRange(1): 0 RandRange(1): 0 RandRange(1): 0 - RandRange(2): 1 - RandRange(2): 1 - RandRange(2): 1 - RandRange(1000000): 966676 - RandRange(1000000): 289525 - RandRange(1000000): 170283 - Chance(1, 2): false + RandRange(2): 0 + RandRange(2): 0 + RandRange(2): 0 + RandRange(1000000): 666804 + RandRange(1000000): 624059 + RandRange(1000000): 697029 Chance(1, 2): true Chance(1, 2): false + Chance(1, 2): true --List-- IsEmpty(): true @@ -420,144 +420,144 @@ 1098 => 46116 1099 => 46158 Randomize ListDump: - 1 => -200078348 - 2 => -29799264 - 1000 => 1630721656 - 1001 => 959306175 - 1002 => 1527421791 - 1003 => 1259692483 - 1004 => -1289244298 - 1005 => -1572996668 - 1006 => -2069479746 - 1007 => -1819131606 - 1008 => -1007163964 - 1009 => -1185394870 - 1010 => -1471365065 - 1011 => 364354366 - 1012 => -1478084253 - 1013 => 405281367 - 1014 => -11170062 - 1015 => 156767750 - 1016 => 1288924796 - 1017 => 1796884876 - 1018 => -1947073702 - 1019 => -1999614238 - 1020 => -231292809 - 1021 => 966621566 - 1022 => -606766557 - 1023 => -1138727825 - 1024 => -749544262 - 1025 => 2004771271 - 1026 => 686734186 - 1027 => 923274744 - 1028 => -1672035149 - 1029 => -1642064950 - 1030 => 1363389551 - 1031 => -559500928 - 1032 => 1656196991 - 1033 => 1655354425 - 1034 => -1027156689 - 1035 => 1952644328 - 1036 => 1217870217 - 1037 => 242274100 - 1038 => 201816080 - 1039 => 2127464758 - 1040 => 446043650 - 1041 => -319728455 - 1042 => 204701002 - 1043 => -571265398 - 1044 => -1422217131 - 1045 => -391208397 - 1046 => -1822628371 - 1047 => -1499755350 - 1048 => -1422137641 - 1049 => 1621693134 - 1051 => -1428728134 - 1052 => -147587573 - 1053 => 681719500 - 1054 => 1172011190 - 1055 => -1834344882 - 1056 => 1157634586 - 1057 => 1902133676 - 1058 => -1967780161 - 1059 => -1618025531 - 1060 => -810220453 - 1061 => 1582854921 - 1062 => -410004643 - 1063 => 1159917159 - 1064 => -1377804984 - 1065 => -738843914 - 1066 => -1578756103 - 1067 => -464090986 - 1068 => 1711504679 - 1069 => 545330655 - 1070 => 379462570 - 1071 => 514511099 - 1072 => -1813251176 - 1073 => 1424958266 - 1074 => -825255131 - 1075 => 539054595 - 1076 => -1764192010 - 1077 => -1243277769 - 1078 => 2017874281 - 1079 => -1972353607 - 1080 => 1879761467 - 1081 => 1638986560 - 1082 => -1832287507 - 1083 => -492411882 - 1084 => 658940812 - 1085 => -1044199400 - 1086 => 1586504918 - 1087 => -125492611 - 1088 => -1562883174 - 1089 => -1013778441 - 1090 => 1560228607 - 1091 => -550265689 - 1092 => 524767105 - 1093 => -713387661 - 1094 => 1425927738 - 1095 => 942653932 - 1096 => 1233220698 - 1097 => 1313602368 - 1098 => -140318584 - 1099 => 1199179892 + 1 => 688298322 + 2 => -1709546982 + 1000 => 1701392078 + 1001 => -1630848421 + 1002 => -886500935 + 1003 => -196324972 + 1004 => -436037402 + 1005 => -520341784 + 1006 => -1485224804 + 1007 => -311036236 + 1008 => -1503442439 + 1009 => -110945695 + 1010 => -82825175 + 1011 => 46859773 + 1012 => -1199223018 + 1013 => -1190555925 + 1014 => 326384434 + 1015 => 1486817960 + 1016 => -1411425597 + 1017 => -508426854 + 1018 => 820019294 + 1019 => 710762995 + 1020 => -760867032 + 1021 => -709611146 + 1022 => 732190215 + 1023 => 236336673 + 1024 => 740596257 + 1025 => 1135321785 + 1026 => 2067474156 + 1027 => -1395683607 + 1028 => -240528699 + 1029 => 928616892 + 1030 => 1712486685 + 1031 => 1994118287 + 1032 => 1333321243 + 1033 => 194124284 + 1034 => 615083294 + 1035 => 628086450 + 1036 => 498957825 + 1037 => 1359697121 + 1038 => 1888433963 + 1039 => 941623020 + 1040 => -1925663292 + 1041 => -771540264 + 1042 => -1058341359 + 1043 => 182127597 + 1044 => 646955927 + 1045 => -1424621714 + 1046 => 623062612 + 1047 => -1986955586 + 1048 => -1268826980 + 1049 => -456776220 + 1051 => -1112555329 + 1052 => -1532134052 + 1053 => 1960404034 + 1054 => 1573325453 + 1055 => -316619303 + 1056 => 699712177 + 1057 => 863274966 + 1058 => 1728276475 + 1059 => -246695889 + 1060 => 1919485436 + 1061 => 111273464 + 1062 => 125435213 + 1063 => 155132602 + 1064 => -171674076 + 1065 => 655046914 + 1066 => 1577399562 + 1067 => 1028818150 + 1068 => 447058239 + 1069 => -1057920269 + 1070 => -1326215323 + 1071 => -198688588 + 1072 => 1523643051 + 1073 => 231373233 + 1074 => 1121759962 + 1075 => 1449439846 + 1076 => -1615270753 + 1077 => -1509293864 + 1078 => 2116903943 + 1079 => 672822173 + 1080 => -969573911 + 1081 => 1589904755 + 1082 => 1148782015 + 1083 => 663503316 + 1084 => 933352745 + 1085 => 577717039 + 1086 => 402172048 + 1087 => 1812250453 + 1088 => 667300501 + 1089 => -1838825777 + 1090 => -856474776 + 1091 => 420696035 + 1092 => 2131427774 + 1093 => -435303548 + 1094 => -160883878 + 1095 => 1969629634 + 1096 => -555794155 + 1097 => -835119691 + 1098 => -1460907909 + 1099 => -1146924084 KeepTop(10): - 1 => -200078348 - 2 => -29799264 - 1000 => 1630721656 - 1001 => 959306175 - 1002 => 1527421791 - 1003 => 1259692483 - 1004 => -1289244298 - 1005 => -1572996668 - 1006 => -2069479746 - 1007 => -1819131606 + 1 => 688298322 + 2 => -1709546982 + 1000 => 1701392078 + 1001 => -1630848421 + 1002 => -886500935 + 1003 => -196324972 + 1004 => -436037402 + 1005 => -520341784 + 1006 => -1485224804 + 1007 => -311036236 KeepBottom(8): - 1000 => 1630721656 - 1001 => 959306175 - 1002 => 1527421791 - 1003 => 1259692483 - 1004 => -1289244298 - 1005 => -1572996668 - 1006 => -2069479746 - 1007 => -1819131606 + 1000 => 1701392078 + 1001 => -1630848421 + 1002 => -886500935 + 1003 => -196324972 + 1004 => -436037402 + 1005 => -520341784 + 1006 => -1485224804 + 1007 => -311036236 RemoveBottom(2): - 1000 => 1630721656 - 1001 => 959306175 - 1002 => 1527421791 - 1003 => 1259692483 - 1004 => -1289244298 - 1005 => -1572996668 + 1000 => 1701392078 + 1001 => -1630848421 + 1002 => -886500935 + 1003 => -196324972 + 1004 => -436037402 + 1005 => -520341784 RemoveTop(2): - 1002 => 1527421791 - 1003 => 1259692483 - 1004 => -1289244298 - 1005 => -1572996668 + 1002 => -886500935 + 1003 => -196324972 + 1004 => -436037402 + 1005 => -520341784 RemoveList({1003, 1004}): - 1002 => 1527421791 - 1005 => -1572996668 + 1002 => -886500935 + 1005 => -520341784 KeepList({1003, 1004, 1005}): - 1005 => -1572996668 + 1005 => -520341784 AddList({1005, 4000, 4001, 4002}): 1005 => 1005 4000 => 8000 diff --git a/src/genworld.cpp b/src/genworld.cpp index 711a9868ba..ecdae28860 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -96,6 +96,7 @@ static void _GenerateWorld() _random.SetSeed(_settings_game.game_creation.generation_seed); SetGeneratingWorldProgress(GWP_MAP_INIT, 2); SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); + ScriptObject::InitializeRandomizers(); BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 9deed82e16..b8f80d1685 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -251,6 +251,8 @@ static void InitializeWindowsAndCaches() UpdateAllVirtCoords(); ResetViewportAfterLoadGame(); + ScriptObject::InitializeRandomizers(); + for (Company *c : Company::Iterate()) { /* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it * accordingly if it is not the case. No need to set it on companies that are not been used already, diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 2c798df9ab..1d02088376 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -312,9 +312,18 @@ bool ScriptObject::DoCommandProcessResult(const CommandCost &res, Script_Suspend NOT_REACHED(); } -Randomizer &ScriptObject::GetRandomizer() + +/* static */ Randomizer ScriptObject::random_states[OWNER_END]; + +Randomizer &ScriptObject::GetRandomizer(Owner owner) +{ + return ScriptObject::random_states[owner]; +} + +void ScriptObject::InitializeRandomizers() { - /* 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; + Randomizer random = _random; + for (Owner owner = OWNER_BEGIN; owner < OWNER_END; owner++) { + ScriptObject::GetRandomizer(owner).SetSeed(random.Next()); + } } diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 8b104a2e13..7a11e18cfe 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -76,8 +76,15 @@ public: /** * Get a reference of the randomizer that brings this script random values. + * @param owner The owner/script to get the randomizer for. This defaults to ScriptObject::GetRootCompany() */ - static Randomizer &GetRandomizer(); + static Randomizer &GetRandomizer(Owner owner = ScriptObject::GetRootCompany()); + + /** + * Initialize/reset the script random states. The state of the scripts are + * based on the current _random seed, but _random does not get changed. + */ + static void InitializeRandomizers(); protected: template struct ScriptDoCommandHelper; @@ -279,6 +286,7 @@ private: static std::tuple DoCommandPrep(); static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only); static CommandCallbackData *GetDoCommandCallback(); + static Randomizer random_states[OWNER_END]; ///< Random states for each of the scripts (game script uses OWNER_DEITY) }; namespace ScriptObjectInternal {