Skip to content

Commit ff69c8f

Browse files
committed
[KillTheRNG] Add world.updateLCG to randomness pool
1 parent 80beee5 commit ff69c8f

File tree

6 files changed

+96
-6
lines changed

6 files changed

+96
-6
lines changed

src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGWorldHandler.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ public static Map<Integer, WorldRandomness> getWorldRandomnessMap() {
2121
return out;
2222
}
2323

24+
public static Map<Integer, Integer> getWorldLCGMap() {
25+
Map<Integer, Integer> out = new HashMap<>();
26+
WorldServer[] worlds = TASmod.getServerInstance().worlds;
27+
int id = 0;
28+
for (WorldServer worldServer : worlds) {
29+
int updateLCG = worldServer.updateLCG;
30+
out.put(id, updateLCG);
31+
id++;
32+
}
33+
return out;
34+
}
35+
2436
public static void setWorldRandomnessMap(Map<Integer, WorldRandomness> randomnessList) {
2537
WorldServer[] worlds = TASmod.getServerInstance().worlds;
2638
int id = 0;
@@ -32,6 +44,17 @@ public static void setWorldRandomnessMap(Map<Integer, WorldRandomness> randomnes
3244
}
3345
}
3446

47+
public static void setWorldLCGMap(Map<Integer, Integer> lcgList) {
48+
WorldServer[] worlds = TASmod.getServerInstance().worlds;
49+
int id = 0;
50+
for (WorldServer worldServer : worlds) {
51+
Integer updateLCG = lcgList.get(id);
52+
if (updateLCG != null)
53+
worldServer.updateLCG = updateLCG;
54+
id++;
55+
}
56+
}
57+
3558
public static String getWorldRandom() {
3659
if (TASmod.getServerInstance().worlds[0] != null)
3760
return Long.toString(((WorldRandomness) TASmod.getServerInstance().worlds[0].rand).getSeed());

src/main/java/com/minecrafttas/tasmod/ktrng/RandomBase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ public boolean equals(Object obj) {
181181
}
182182

183183
public void fireEvent(long seed, String value) {
184-
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
184+
fireEvent(seed, value, 3);
185+
}
186+
187+
public void fireEvent(long seed, String value, int stackTraceOffset) {
188+
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[stackTraceOffset];
185189
String methodName = stackTraceElement.getMethodName();
186190
String[] classNames = stackTraceElement.getClassName().split("\\.");
187191
String className = classNames[classNames.length - 1];

src/main/java/com/minecrafttas/tasmod/ktrng/WorldRandomness.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public WorldRandomness() {
1313
}
1414

1515
@Override
16-
public void fireEvent(long seed, String value) {
17-
// super.fireEvent(seed, value);
16+
public void fireEvent(long seed, String value, int offset) {
17+
//super.fireEvent(seed, value, 5);
1818
}
1919
}

src/main/java/com/minecrafttas/tasmod/savestates/storage/builtin/KTRNGSeedStorage.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,23 @@ public JsonObject onSavestate(MinecraftServer server, JsonObject dataToSave) {
4040
dataToSave.add("entityRandom", entityRandomDataJson);
4141

4242
JsonObject worldRandomDataJson = new JsonObject();
43+
44+
JsonObject worldListJson = new JsonObject();
4345
Map<Integer, WorldRandomness> worldRandom = KTRNGWorldHandler.getWorldRandomnessMap();
4446
for (Entry<Integer, WorldRandomness> entry : worldRandom.entrySet()) {
45-
worldRandomDataJson.addProperty(entry.getKey().toString(), entry.getValue().getSeed());
47+
worldListJson.addProperty(entry.getKey().toString(), entry.getValue().getSeed());
4648
}
4749

50+
worldRandomDataJson.add("worldList", worldListJson);
51+
52+
JsonObject lcgListJson = new JsonObject();
53+
Map<Integer, Integer> lcgMap = KTRNGWorldHandler.getWorldLCGMap();
54+
for (Entry<Integer, Integer> entry : lcgMap.entrySet()) {
55+
lcgListJson.addProperty(entry.getKey().toString(), entry.getValue());
56+
}
57+
58+
worldRandomDataJson.add("lcgList", lcgListJson);
59+
4860
dataToSave.add("worldRandom", worldRandomDataJson);
4961

5062
return dataToSave;
@@ -69,15 +81,26 @@ public void onLoadstateComplete(MinecraftServer server, JsonObject loadedData) {
6981

7082
KTRNGEntityHandler.setRandomnessList(randomList);
7183

72-
JsonObject worldRandomListJson = loadedData.get("worldRandom").getAsJsonObject();
84+
JsonObject worldRandomJson = loadedData.get("worldRandom").getAsJsonObject();
85+
JsonObject worldListJson = worldRandomJson.get("worldList").getAsJsonObject();
7386

7487
Map<Integer, WorldRandomness> worldList = new HashMap<>();
75-
for (Entry<String, JsonElement> entry : worldRandomListJson.entrySet()) {
88+
for (Entry<String, JsonElement> entry : worldListJson.entrySet()) {
7689
int id = Integer.parseInt(entry.getKey());
7790
WorldRandomness worldRandomness = new WorldRandomness(entry.getValue().getAsLong());
7891

7992
worldList.put(id, worldRandomness);
8093
}
94+
95+
JsonObject worldLCGList = worldRandomJson.get("lcgList").getAsJsonObject();
96+
Map<Integer, Integer> lcgList = new HashMap<>();
97+
for (Entry<String, JsonElement> entry : worldLCGList.entrySet()) {
98+
int id = Integer.parseInt(entry.getKey());
99+
int lcg = entry.getValue().getAsInt();
100+
101+
lcgList.put(id, lcg);
102+
}
103+
81104
KTRNGWorldHandler.setWorldRandomnessMap(worldList);
82105
}
83106

src/main/resources/tasmod.accesswidener

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ accessible field net/minecraft/client/settings/KeyBinding CATEGORY_ORDER Ljava/u
1515

1616
#KillTheRNG
1717
accessible field net/minecraft/entity/Entity rand Ljava/util/Random;
18+
accessible field net/minecraft/world/World updateLCG I
1819
mutable field net/minecraft/world/World rand Ljava/util/Random;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tasmod.killtherng;
2+
3+
import com.minecrafttas.tasmod.ktrng.RandomBase;
4+
5+
public class RNGTest {
6+
7+
public static void main(String[] args) {
8+
long[] longlist = new long[] {
9+
//@formatter:off
10+
113621727298730L,
11+
161845404674820L
12+
//@formatter:on
13+
};
14+
15+
printDistance(longlist);
16+
17+
longlist = new long[] {
18+
//@formatter:off
19+
161845404674820L,
20+
107865211428631L,
21+
252587169991970L,
22+
223075662074294L,
23+
66246869218509L,
24+
81210955942352L,
25+
183553865074233L,
26+
11371681017552L
27+
//@formatter:on
28+
};
29+
// printDistance(longlist);
30+
}
31+
32+
private static void printDistance(long[] list) {
33+
long prev = list[0];
34+
for (long l : list) {
35+
System.out.println(RandomBase.distance(prev, l));
36+
}
37+
System.out.println("");
38+
}
39+
}

0 commit comments

Comments
 (0)