Attempt to synchronized logic random seed

This commit is contained in:
Anuken 2021-09-22 20:12:09 -04:00
parent 3f6d5b9dfe
commit 90d1770b3e
6 changed files with 21 additions and 5 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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<String> namesToIds = new ObjectIntMap<>();
private Seq<Var> vars = new Seq<>(Var.class);

View File

@ -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)),

View File

@ -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();

View File

@ -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