Write synced entities in world data
This commit is contained in:
@@ -359,6 +359,42 @@ public class NetClient implements ApplicationListener{
|
|||||||
Groups.player.removeByID(playerid);
|
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)
|
@Remote(variants = Variant.one, priority = PacketPriority.low, unreliable = true)
|
||||||
public static void entitySnapshot(short amount, byte[] data){
|
public static void entitySnapshot(short amount, byte[] data){
|
||||||
try{
|
try{
|
||||||
@@ -366,39 +402,7 @@ public class NetClient implements ApplicationListener{
|
|||||||
DataInputStream input = netClient.dataStream;
|
DataInputStream input = netClient.dataStream;
|
||||||
|
|
||||||
for(int j = 0; j < amount; j++){
|
for(int j = 0; j < amount; j++){
|
||||||
int id = input.readInt();
|
readSyncEntity(input, Reads.get(input));
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}catch(IOException e){
|
}catch(IOException e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
@@ -667,6 +667,13 @@ public class TypeIO{
|
|||||||
public Building unbox(){
|
public Building unbox(){
|
||||||
return world.build(pos);
|
return world.build(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return "BuildingBox{" +
|
||||||
|
"pos=" + pos +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Represents a unit that has not been resolved yet. TODO unimplemented / unused*/
|
/** Represents a unit that has not been resolved yet. TODO unimplemented / unused*/
|
||||||
@@ -680,5 +687,12 @@ public class TypeIO{
|
|||||||
public Unit unbox(){
|
public Unit unbox(){
|
||||||
return Groups.unit.getByID(id);
|
return Groups.unit.getByID(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return "UnitBox{" +
|
||||||
|
"id=" + id +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,8 +44,19 @@ public class NetworkIO{
|
|||||||
stream.writeLong(GlobalConstants.rand.seed0);
|
stream.writeLong(GlobalConstants.rand.seed0);
|
||||||
stream.writeLong(GlobalConstants.rand.seed1);
|
stream.writeLong(GlobalConstants.rand.seed1);
|
||||||
|
|
||||||
|
Writes write = new Writes(stream);
|
||||||
|
|
||||||
stream.writeInt(player.id);
|
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().writeContentHeader(stream);
|
||||||
SaveIO.getSaveWriter().writeMap(stream);
|
SaveIO.getSaveWriter().writeMap(stream);
|
||||||
@@ -68,13 +79,21 @@ public class NetworkIO{
|
|||||||
GlobalConstants.rand.seed0 = stream.readLong();
|
GlobalConstants.rand.seed0 = stream.readLong();
|
||||||
GlobalConstants.rand.seed1 = stream.readLong();
|
GlobalConstants.rand.seed1 = stream.readLong();
|
||||||
|
|
||||||
|
Reads read = new Reads(stream);
|
||||||
|
|
||||||
Groups.clear();
|
Groups.clear();
|
||||||
int id = stream.readInt();
|
int id = stream.readInt();
|
||||||
player.reset();
|
player.reset();
|
||||||
player.read(Reads.get(stream));
|
player.read(read);
|
||||||
player.id = id;
|
player.id = id;
|
||||||
player.add();
|
player.add();
|
||||||
|
|
||||||
|
int entities = stream.readInt();
|
||||||
|
|
||||||
|
for(int j = 0; j < entities; j++){
|
||||||
|
NetClient.readSyncEntity(stream, read);
|
||||||
|
}
|
||||||
|
|
||||||
SaveIO.getSaveWriter().readContentHeader(stream);
|
SaveIO.getSaveWriter().readContentHeader(stream);
|
||||||
SaveIO.getSaveWriter().readMap(stream, world.context);
|
SaveIO.getSaveWriter().readMap(stream, world.context);
|
||||||
SaveIO.getSaveWriter().readTeamBlocks(stream);
|
SaveIO.getSaveWriter().readTeamBlocks(stream);
|
||||||
|
|||||||
Reference in New Issue
Block a user