From 90d1770b3e3c129591224351821310b972f5c22e Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 22 Sep 2021 20:12:09 -0400 Subject: [PATCH] Attempt to synchronized logic random seed --- core/src/mindustry/core/NetClient.java | 8 +++++++- core/src/mindustry/core/NetServer.java | 4 +++- core/src/mindustry/logic/GlobalConstants.java | 3 +++ core/src/mindustry/logic/LogicOp.java | 4 ++-- core/src/mindustry/net/NetworkIO.java | 5 +++++ gradle.properties | 2 +- 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/core/src/mindustry/core/NetClient.java b/core/src/mindustry/core/NetClient.java index 6a0d0929d9..065cf8635c 100644 --- a/core/src/mindustry/core/NetClient.java +++ b/core/src/mindustry/core/NetClient.java @@ -19,6 +19,7 @@ import mindustry.game.EventType.*; import mindustry.game.*; import mindustry.game.Teams.*; import mindustry.gen.*; +import mindustry.logic.*; import mindustry.net.Administration.*; import mindustry.net.*; import mindustry.net.Packets.*; @@ -452,7 +453,7 @@ public class NetClient implements ApplicationListener{ } @Remote(variants = Variant.one, priority = PacketPriority.low, unreliable = true) - public static void stateSnapshot(float waveTime, int wave, int enemies, boolean paused, boolean gameOver, int timeData, byte tps, byte[] coreData){ + public static void stateSnapshot(float waveTime, int wave, int enemies, boolean paused, boolean gameOver, int timeData, byte tps, long rand0, long rand1, byte[] coreData){ try{ if(wave > state.wave){ state.wave = wave; @@ -466,6 +467,11 @@ public class NetClient implements ApplicationListener{ state.serverPaused = paused; state.serverTps = tps & 0xff; + //note that this is far from a guarantee that random state is synced - tiny changes in delta and ping can throw everything off again. + //syncing will only make much of a difference when rand() is called infrequently + GlobalConstants.rand.seed0 = rand0; + GlobalConstants.rand.seed1 = rand1; + universe.updateNetSeconds(timeData); netClient.byteStream.setBytes(coreData); diff --git a/core/src/mindustry/core/NetServer.java b/core/src/mindustry/core/NetServer.java index 9c6ae4cd83..d528f672f5 100644 --- a/core/src/mindustry/core/NetServer.java +++ b/core/src/mindustry/core/NetServer.java @@ -19,6 +19,7 @@ import mindustry.game.*; import mindustry.game.Teams.*; import mindustry.gen.*; import mindustry.graphics.*; +import mindustry.logic.*; import mindustry.net.*; import mindustry.net.Administration.*; import mindustry.net.Packets.*; @@ -867,7 +868,8 @@ public class NetServer implements ApplicationListener{ dataStream.close(); //write basic state data. - Call.stateSnapshot(player.con, state.wavetime, state.wave, state.enemies, state.serverPaused, state.gameOver, universe.seconds(), tps, syncStream.toByteArray()); + Call.stateSnapshot(player.con, state.wavetime, state.wave, state.enemies, state.serverPaused, state.gameOver, + universe.seconds(), tps, GlobalConstants.rand.seed0, GlobalConstants.rand.seed1, syncStream.toByteArray()); syncStream.reset(); diff --git a/core/src/mindustry/logic/GlobalConstants.java b/core/src/mindustry/logic/GlobalConstants.java index e631906760..d86735f405 100644 --- a/core/src/mindustry/logic/GlobalConstants.java +++ b/core/src/mindustry/logic/GlobalConstants.java @@ -2,6 +2,7 @@ package mindustry.logic; import arc.*; import arc.files.*; +import arc.math.*; import arc.struct.*; import arc.util.*; import mindustry.*; @@ -18,6 +19,8 @@ import java.io.*; public class GlobalConstants{ public static final int ctrlProcessor = 1, ctrlPlayer = 2, ctrlFormation = 3; public static final ContentType[] lookableContent = {ContentType.block, ContentType.unit, ContentType.item, ContentType.liquid}; + /** Global random state. */ + public static final Rand rand = new Rand(); private ObjectIntMap namesToIds = new ObjectIntMap<>(); private Seq vars = new Seq<>(Var.class); diff --git a/core/src/mindustry/logic/LogicOp.java b/core/src/mindustry/logic/LogicOp.java index 9604279584..79e56b1ca8 100644 --- a/core/src/mindustry/logic/LogicOp.java +++ b/core/src/mindustry/logic/LogicOp.java @@ -34,13 +34,13 @@ public enum LogicOp{ angle("angle", true, (x, y) -> Angles.angle((float)x, (float)y)), len("len", true, (x, y) -> Mathf.dst((float)x, (float)y)), noise("noise", true, (x, y) -> Simplex.raw2d(0, x, y)), - abs("abs", a -> Math.abs(a)), + abs("abs", a -> Math.abs(a)), //not a method reference because it fails to compile for some reason log("log", Math::log), log10("log10", Math::log10), floor("floor", Math::floor), ceil("ceil", Math::ceil), sqrt("sqrt", Math::sqrt), - rand("rand", d -> Mathf.rand.nextDouble() * d), + rand("rand", d -> GlobalConstants.rand.nextDouble() * d), sin("sin", d -> Math.sin(d * Mathf.doubleDegRad)), cos("cos", d -> Math.cos(d * Mathf.doubleDegRad)), diff --git a/core/src/mindustry/net/NetworkIO.java b/core/src/mindustry/net/NetworkIO.java index d81955fa42..dee54a92d1 100644 --- a/core/src/mindustry/net/NetworkIO.java +++ b/core/src/mindustry/net/NetworkIO.java @@ -9,6 +9,7 @@ import mindustry.ctype.*; import mindustry.game.*; import mindustry.gen.*; import mindustry.io.*; +import mindustry.logic.*; import mindustry.maps.Map; import mindustry.net.Administration.*; @@ -41,6 +42,8 @@ public class NetworkIO{ stream.writeInt(state.wave); stream.writeFloat(state.wavetime); stream.writeDouble(state.tick); + stream.writeLong(GlobalConstants.rand.seed0); + stream.writeLong(GlobalConstants.rand.seed1); stream.writeInt(player.id); player.write(Writes.get(stream)); @@ -63,6 +66,8 @@ public class NetworkIO{ state.wave = stream.readInt(); state.wavetime = stream.readFloat(); state.tick = stream.readDouble(); + GlobalConstants.rand.seed0 = stream.readLong(); + GlobalConstants.rand.seed1 = stream.readLong(); Groups.clear(); int id = stream.readInt(); diff --git a/gradle.properties b/gradle.properties index 96873ac245..10fbb45adc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,4 @@ android.useAndroidX=true #used for slow jitpack builds; TODO see if this actually works http.socketTimeout=80000 http.connectionTimeout=80000 -archash=3400de3323150ba91c94fc07a6d4ca1958f4d943 +archash=bc9bbf8312f69d5e6882ce8dab4136d2126473e8