From 499671a637c04a297527f6bfec3dbed314aac175 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 28 Jun 2018 21:04:01 -0400 Subject: [PATCH] Fixed some snapshot bugs, net crashes --- build.gradle | 2 +- core/src/io/anuke/mindustry/core/NetClient.java | 6 +++--- core/src/io/anuke/mindustry/core/NetServer.java | 10 ++++++++-- core/src/io/anuke/mindustry/net/Interpolator.java | 6 ++++++ core/src/io/anuke/mindustry/type/Weapon.java | 5 ++++- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 20035fcc3f..284eae948f 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ allprojects { gdxVersion = '1.9.8' roboVMVersion = '2.3.0' aiVersion = '1.8.1' - uCoreVersion = 'dbc818f4ab' + uCoreVersion = '5d685fc8b1' getVersionString = { String buildVersion = getBuildVersion() diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index 465c652c04..b45d23d5fc 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -56,7 +56,7 @@ public class NetClient extends Module { /**Counter of how many chunks have been recieved.*/ private int recievedChunkCounter; /**ID of snapshot that is currently being constructed.*/ - private int currentSnapshotID; + private int currentSnapshotID = -1; /**Last snapshot ID recieved.*/ private int lastSnapshotID = -1; /**Decoder for uncompressing snapshots.*/ @@ -82,7 +82,7 @@ public class NetClient extends Module { lastSent = 0; lastSnapshot = null; currentSnapshot = null; - currentSnapshotID = 0; + currentSnapshotID = -1; lastSnapshotID = -1; ui.chatfrag.clearMessages(); @@ -285,7 +285,7 @@ public class NetClient extends Module { if (snapshotID == 0) { //fresh snapshot result = snapshot; length = snapshot.length; - netClient.lastSnapshot = snapshot; + netClient.lastSnapshot = Arrays.copyOf(snapshot, snapshot.length); } else { //otherwise, last snapshot must not be null, decode it netClient.decoder.init(netClient.lastSnapshot, snapshot); result = netClient.decoder.decode(); diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index 59b9a1e096..f4d5e8bcb1 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -41,6 +41,7 @@ import static io.anuke.mindustry.Vars.*; public class NetServer extends Module{ public final static int maxSnapshotSize = 2047; + private final static byte[] reusableSnapArray = new byte[maxSnapshotSize]; private final static boolean showSnapshotSize = false; private final static float serverSyncTime = 4, kickDuration = 30 * 1000; private final static Vector2 vector = new Vector2(); @@ -429,8 +430,13 @@ public class NetServer extends Module{ int chunkid = 0; while(remaining > 0){ int used = Math.min(remaining, maxSnapshotSize); - //TODO optimize to *not* copy the bytes directly, but instead re-use all arrays that are of length = maxSnapshotSize - byte[] toSend = Arrays.copyOfRange(bytes, offset, Math.min(offset + maxSnapshotSize, bytes.length)); + byte[] toSend; + if(used == maxSnapshotSize){ + toSend = reusableSnapArray; + System.arraycopy(bytes, offset, toSend, 0, Math.min(offset + maxSnapshotSize, bytes.length) - offset); + }else { + toSend = Arrays.copyOfRange(bytes, offset, Math.min(offset + maxSnapshotSize, bytes.length)); + } Call.onSnapshot(userid, toSend, snapshotID, (short)chunkid, (short)bytes.length); remaining -= used; diff --git a/core/src/io/anuke/mindustry/net/Interpolator.java b/core/src/io/anuke/mindustry/net/Interpolator.java index 3f7905f9f1..1c0d4735d6 100644 --- a/core/src/io/anuke/mindustry/net/Interpolator.java +++ b/core/src/io/anuke/mindustry/net/Interpolator.java @@ -38,6 +38,12 @@ public class Interpolator { public void update(){ + if(pos.dst(target) > 128){ + pos.set(target); + lastUpdated = 0; + updateSpacing = 16; + } + if(lastUpdated != 0 && updateSpacing != 0){ float timeSinceUpdate = TimeUtils.timeSinceMillis(lastUpdated); float alpha = Math.min(timeSinceUpdate / updateSpacing, 2f); diff --git a/core/src/io/anuke/mindustry/type/Weapon.java b/core/src/io/anuke/mindustry/type/Weapon.java index 4fd6197682..137db5977a 100644 --- a/core/src/io/anuke/mindustry/type/Weapon.java +++ b/core/src/io/anuke/mindustry/type/Weapon.java @@ -126,8 +126,11 @@ public class Weapon extends Upgrade { } void bullet(ShooterTrait owner, float x, float y, float angle){ + if(owner == null || !owner.getInventory().hasAmmo()) return; + tr.trns(angle, 3f); - Bullet.create(owner.getInventory().getAmmo().bullet, owner, owner.getTeam(), x + tr.x, y + tr.y, angle, (1f-velocityRnd) + Mathf.random(velocityRnd)); + Bullet.create(owner.getInventory().getAmmo().bullet, + owner, owner.getTeam(), x + tr.x, y + tr.y, angle, (1f-velocityRnd) + Mathf.random(velocityRnd)); } @Remote(targets = Loc.server, called = Loc.both, in = In.entities, unreliable = true)