Added experimental server block syncing
This commit is contained in:
@@ -275,7 +275,7 @@ public class Vars implements Loadable{
|
||||
Core.settings.setDataDirectory(Core.files.local("saves/"));
|
||||
}
|
||||
|
||||
Core.settings.defaults("locale", "default");
|
||||
Core.settings.defaults("locale", "default", "blocksync", true);
|
||||
Core.keybinds.setDefaults(Binding.values());
|
||||
Core.settings.load();
|
||||
|
||||
|
||||
@@ -342,6 +342,26 @@ public class NetClient implements ApplicationListener{
|
||||
}
|
||||
}
|
||||
|
||||
@Remote(variants = Variant.both, priority = PacketPriority.low, unreliable = true)
|
||||
public static void onBlockSnapshot(short amount, short dataLen, byte[] data){
|
||||
try{
|
||||
netClient.byteStream.setBytes(net.decompressSnapshot(data, dataLen));
|
||||
DataInputStream input = netClient.dataStream;
|
||||
|
||||
for(int i = 0; i < amount; i++){
|
||||
int pos = input.readInt();
|
||||
Tile tile = world.tile(pos);
|
||||
if(tile == null || tile.entity == null){
|
||||
Log.warn("Missing entity at {0}. Skipping block snapshot.", tile);
|
||||
break;
|
||||
}
|
||||
tile.entity.read(input, tile.entity.version());
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Remote(variants = Variant.one, priority = PacketPriority.low, unreliable = true)
|
||||
public static void onStateSnapshot(float waveTime, int wave, int enemies, short coreDataLen, byte[] coreData){
|
||||
try{
|
||||
|
||||
@@ -30,8 +30,8 @@ import java.util.zip.*;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class NetServer implements ApplicationListener{
|
||||
public final static int maxSnapshotSize = 430;
|
||||
private final static float serverSyncTime = 12, kickDuration = 30 * 1000;
|
||||
private final static int maxSnapshotSize = 430, timerBlockSync = 0;
|
||||
private final static float serverSyncTime = 12, kickDuration = 30 * 1000, blockSyncTime = 60 * 10;
|
||||
private final static Vector2 vector = new Vector2();
|
||||
private final static Rectangle viewport = new Rectangle();
|
||||
/** If a player goes away of their server-side coordinates by this distance, they get teleported back. */
|
||||
@@ -41,6 +41,7 @@ public class NetServer implements ApplicationListener{
|
||||
public final CommandHandler clientCommands = new CommandHandler("/");
|
||||
|
||||
private boolean closing = false;
|
||||
private Interval timer = new Interval();
|
||||
|
||||
private ByteBuffer writeBuffer = ByteBuffer.allocate(127);
|
||||
private ByteBufferOutput outputBuffer = new ByteBufferOutput(writeBuffer);
|
||||
@@ -612,7 +613,35 @@ public class NetServer implements ApplicationListener{
|
||||
}
|
||||
}
|
||||
|
||||
public void writeSnapshot(Player player) throws IOException{
|
||||
/** Sends a block snapshot to all players. */
|
||||
public void writeBlockSnapshots() throws IOException{
|
||||
syncStream.reset();
|
||||
|
||||
short sent = 0;
|
||||
for(TileEntity entity : tileGroup.all()){
|
||||
if(!entity.block.sync) continue;
|
||||
sent ++;
|
||||
|
||||
dataStream.writeInt(entity.tile.pos());
|
||||
entity.write(dataStream);
|
||||
|
||||
if(syncStream.size() > maxSnapshotSize){
|
||||
dataStream.close();
|
||||
byte[] stateBytes = syncStream.toByteArray();
|
||||
Call.onBlockSnapshot(sent, (short)stateBytes.length, net.compressSnapshot(stateBytes));
|
||||
sent = 0;
|
||||
syncStream.reset();
|
||||
}
|
||||
}
|
||||
|
||||
if(sent > 0){
|
||||
dataStream.close();
|
||||
byte[] stateBytes = syncStream.toByteArray();
|
||||
Call.onBlockSnapshot(sent, (short)stateBytes.length, net.compressSnapshot(stateBytes));
|
||||
}
|
||||
}
|
||||
|
||||
public void writeEntitySnapshot(Player player) throws IOException{
|
||||
syncStream.reset();
|
||||
ObjectSet<Tile> cores = state.teams.get(player.getTeam()).cores;
|
||||
|
||||
@@ -726,7 +755,6 @@ public class NetServer implements ApplicationListener{
|
||||
void sync(){
|
||||
|
||||
try{
|
||||
|
||||
//iterate through each player
|
||||
for(int i = 0; i < playerGroup.size(); i++){
|
||||
Player player = playerGroup.all().get(i);
|
||||
@@ -741,7 +769,11 @@ public class NetServer implements ApplicationListener{
|
||||
|
||||
if(!player.timer.get(Player.timerSync, serverSyncTime) || !connection.hasConnected) continue;
|
||||
|
||||
writeSnapshot(player);
|
||||
writeEntitySnapshot(player);
|
||||
}
|
||||
|
||||
if(playerGroup.size() > 0 && Core.settings.getBool("blocksync") && timer.get(timerBlockSync, blockSyncTime)){
|
||||
writeBlockSnapshots();
|
||||
}
|
||||
|
||||
}catch(IOException e){
|
||||
|
||||
@@ -94,6 +94,8 @@ public class Block extends BlockStorage{
|
||||
public boolean drawLiquidLight = true;
|
||||
/** Whether the config is positional and needs to be shifted. */
|
||||
public boolean posConfig;
|
||||
/** Whether to periodically sync this block across the network.*/
|
||||
public boolean sync;
|
||||
/** Whether this block uses conveyor-type placement mode.*/
|
||||
public boolean conveyorPlacement;
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ public class PowerGenerator extends PowerDistributor{
|
||||
|
||||
public PowerGenerator(String name){
|
||||
super(name);
|
||||
sync = true;
|
||||
baseExplosiveness = 5f;
|
||||
flags = EnumSet.of(BlockFlag.producer);
|
||||
entityType = GeneratorEntity::new;
|
||||
|
||||
@@ -35,6 +35,7 @@ public class GenericCrafter extends Block{
|
||||
hasItems = true;
|
||||
health = 60;
|
||||
idleSound = Sounds.machine;
|
||||
sync = true;
|
||||
idleSoundVolume = 0.03f;
|
||||
entityType = GenericCrafterEntity::new;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user