From dcfdf37b4f94b2f8732d13a5b09cc6c07ae8641a Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 7 Jan 2022 10:53:56 -0500 Subject: [PATCH] Write synced entities in world data --- core/src/mindustry/core/NetClient.java | 70 ++++++++++++++------------ core/src/mindustry/io/TypeIO.java | 14 ++++++ core/src/mindustry/net/NetworkIO.java | 23 ++++++++- 3 files changed, 72 insertions(+), 35 deletions(-) diff --git a/core/src/mindustry/core/NetClient.java b/core/src/mindustry/core/NetClient.java index 359cee22f0..5533425b12 100644 --- a/core/src/mindustry/core/NetClient.java +++ b/core/src/mindustry/core/NetClient.java @@ -359,6 +359,42 @@ public class NetClient implements ApplicationListener{ Groups.player.removeByID(playerid); } + public static void readSyncEntity(DataInputStream input, Reads read) throws IOException{ + int id = input.readInt(); + byte typeID = input.readByte(); + + Syncc entity = Groups.sync.getByID(id); + boolean add = false, created = false; + + if(entity == null && id == player.id()){ + entity = player; + add = true; + } + + //entity must not be added yet, so create it + if(entity == null){ + entity = (Syncc)EntityMapping.map(typeID).get(); + entity.id(id); + if(!netClient.isEntityUsed(entity.id())){ + add = true; + } + created = true; + } + + //read the entity + entity.readSync(read); + + if(created){ + //snap initial starting position + entity.snapSync(); + } + + if(add){ + entity.add(); + netClient.addRemovedEntity(entity.id()); + } + } + @Remote(variants = Variant.one, priority = PacketPriority.low, unreliable = true) public static void entitySnapshot(short amount, byte[] data){ try{ @@ -366,39 +402,7 @@ public class NetClient implements ApplicationListener{ DataInputStream input = netClient.dataStream; for(int j = 0; j < amount; j++){ - int id = input.readInt(); - byte typeID = input.readByte(); - - Syncc entity = Groups.sync.getByID(id); - boolean add = false, created = false; - - if(entity == null && id == player.id()){ - entity = player; - add = true; - } - - //entity must not be added yet, so create it - if(entity == null){ - entity = (Syncc)EntityMapping.map(typeID).get(); - entity.id(id); - if(!netClient.isEntityUsed(entity.id())){ - add = true; - } - created = true; - } - - //read the entity - entity.readSync(Reads.get(input)); - - if(created){ - //snap initial starting position - entity.snapSync(); - } - - if(add){ - entity.add(); - netClient.addRemovedEntity(entity.id()); - } + readSyncEntity(input, Reads.get(input)); } }catch(IOException e){ throw new RuntimeException(e); diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index 1083a6f1eb..82589e9571 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -667,6 +667,13 @@ public class TypeIO{ public Building unbox(){ return world.build(pos); } + + @Override + public String toString(){ + return "BuildingBox{" + + "pos=" + pos + + '}'; + } } /** Represents a unit that has not been resolved yet. TODO unimplemented / unused*/ @@ -680,5 +687,12 @@ public class TypeIO{ public Unit unbox(){ return Groups.unit.getByID(id); } + + @Override + public String toString(){ + return "UnitBox{" + + "id=" + id + + '}'; + } } } diff --git a/core/src/mindustry/net/NetworkIO.java b/core/src/mindustry/net/NetworkIO.java index 24a4b4a109..8a521ee3df 100644 --- a/core/src/mindustry/net/NetworkIO.java +++ b/core/src/mindustry/net/NetworkIO.java @@ -44,8 +44,19 @@ public class NetworkIO{ stream.writeLong(GlobalConstants.rand.seed0); stream.writeLong(GlobalConstants.rand.seed1); + Writes write = new Writes(stream); + stream.writeInt(player.id); - player.write(Writes.get(stream)); + player.write(write); + + stream.writeInt(Groups.sync.size()); + + //write all synced entities *immediately* + for(Syncc entity : Groups.sync){ + stream.writeInt(entity.id()); + stream.writeByte(entity.classId()); + entity.writeSync(write); + } SaveIO.getSaveWriter().writeContentHeader(stream); SaveIO.getSaveWriter().writeMap(stream); @@ -68,13 +79,21 @@ public class NetworkIO{ GlobalConstants.rand.seed0 = stream.readLong(); GlobalConstants.rand.seed1 = stream.readLong(); + Reads read = new Reads(stream); + Groups.clear(); int id = stream.readInt(); player.reset(); - player.read(Reads.get(stream)); + player.read(read); player.id = id; player.add(); + int entities = stream.readInt(); + + for(int j = 0; j < entities; j++){ + NetClient.readSyncEntity(stream, read); + } + SaveIO.getSaveWriter().readContentHeader(stream); SaveIO.getSaveWriter().readMap(stream, world.context); SaveIO.getSaveWriter().readTeamBlocks(stream);