Write synced entities in world data

This commit is contained in:
Anuken
2022-01-07 10:53:56 -05:00
parent 4fb2b70c4b
commit dcfdf37b4f
3 changed files with 72 additions and 35 deletions

View File

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

View File

@@ -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 +
'}';
}
}
}

View File

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