Extensive netcode changes, bugfixes

This commit is contained in:
Anuken
2018-06-29 14:13:31 -04:00
parent 3898602f0e
commit b4151a256b
20 changed files with 411 additions and 187 deletions

View File

@@ -6,20 +6,21 @@ public abstract class NetConnection {
public final int id;
public final String address;
/**ID of last snapshot this connection is guaranteed to have recieved.*/
public int lastSnapshotID = -1;
/**Byte array of last sent snapshot data that is confirmed to be recieved.*/
public byte[] lastSnapshot;
/**The current base snapshot that the client is absolutely confirmed to have recieved.
* All sent snapshots should be taking the diff from this base snapshot, if it isn't null.*/
public byte[] currentBaseSnapshot;
/**ID of the current base snapshot.*/
public int currentBaseID = -1;
/**ID of last sent snapshot.*/
public int lastSentSnapshotID = -1;
/**Byte array of last sent snapshot.*/
public int lastSentBase = -1;
public byte[] lastSentSnapshot;
public byte[] lastSentRawSnapshot;
public int lastSentSnapshotID = -1;
/**ID of last recieved client snapshot.*/
public int lastRecievedSnapshot = -1;
public int lastRecievedClientSnapshot = -1;
/**Timestamp of last recieved snapshot.*/
public long lastRecievedTime;
public long lastRecievedClientTime;
public boolean hasConnected = false;
@@ -28,6 +29,10 @@ public abstract class NetConnection {
this.address = address;
}
public boolean isConnected(){
return true;
}
public abstract void send(Object object, SendMode mode);
public abstract void close();
}

View File

@@ -59,29 +59,39 @@ public class NetworkIO {
stream.writeShort(world.width());
stream.writeShort(world.height());
for(int x = 0; x < world.width(); x ++){
for(int y = 0; y < world.height(); y ++){
Tile tile = world.tile(x, y);
for (int i = 0; i < world.width() * world.height(); i++) {
Tile tile = world.tile(i);
stream.writeByte(tile.floor().id); //floor ID
stream.writeByte(tile.block().id); //block ID
stream.writeByte(tile.elevation);
stream.writeByte(tile.getFloorID());
stream.writeByte(tile.getWallID());
stream.writeByte(tile.elevation);
if(tile.block() instanceof BlockPart){
stream.writeByte(tile.link);
}
if(tile.block() instanceof BlockPart){
stream.writeByte(tile.link);
}else if(tile.entity != null){
stream.writeByte(Bits.packByte(tile.getTeamID(), tile.getRotation())); //team + rotation
stream.writeShort((short)tile.entity.health); //health
if(tile.entity != null){
stream.writeByte(Bits.packByte((byte)tile.getTeam().ordinal(), tile.getRotation()));
stream.writeShort((short)tile.entity.health); //health
if(tile.entity.items != null) tile.entity.items.write(stream);
if(tile.entity.power != null) tile.entity.power.write(stream);
if(tile.entity.liquids != null) tile.entity.liquids.write(stream);
if(tile.entity.items != null) tile.entity.items.write(stream);
if(tile.entity.power != null) tile.entity.power.write(stream);
if(tile.entity.liquids != null) tile.entity.liquids.write(stream);
tile.entity.write(stream);
}else if(tile.getWallID() == 0){
int consecutives = 0;
tile.entity.write(stream);
for (int j = i + 1; j < world.width() * world.height() && consecutives < 255; j++) {
Tile nextTile = world.tile(j);
if(nextTile.getFloorID() != tile.getFloorID() || nextTile.getWallID() != 0 || nextTile.elevation != tile.elevation){
break;
}
consecutives ++;
}
stream.writeByte(consecutives);
i += consecutives;
}
}
@@ -158,38 +168,47 @@ public class NetworkIO {
Tile[][] tiles = world.createTiles(width, height);
for(int x = 0; x < width; x ++){
for(int y = 0; y < height; y ++){
byte floorid = stream.readByte();
byte blockid = stream.readByte();
byte elevation = stream.readByte();
for (int i = 0; i < width * height; i++) {
int x = i % width, y = i /width;
byte floorid = stream.readByte();
byte wallid = stream.readByte();
byte elevation = stream.readByte();
Tile tile = new Tile(x, y, floorid, blockid);
Tile tile = new Tile(x, y, floorid, wallid);
tile.elevation = elevation;
tile.elevation = elevation;
if (wallid == Blocks.blockpart.id) {
tile.link = stream.readByte();
}else if (tile.entity != null) {
byte tr = stream.readByte();
short health = stream.readShort();
if(tile.block() == Blocks.blockpart){
tile.link = stream.readByte();
byte team = Bits.getLeftByte(tr);
byte rotation = Bits.getRightByte(tr);
tile.setTeam(Team.all[team]);
tile.entity.health = health;
tile.setRotation(rotation);
if (tile.entity.items != null) tile.entity.items.read(stream);
if (tile.entity.power != null) tile.entity.power.read(stream);
if (tile.entity.liquids != null) tile.entity.liquids.read(stream);
tile.entity.read(stream);
}else if(wallid == 0){
int consecutives = stream.readUnsignedByte();
for (int j = i + 1; j < i + 1 + consecutives; j++) {
int newx = j % width, newy = j / width;
Tile newTile = new Tile(newx, newy, floorid, wallid);
newTile.elevation = elevation;
tiles[newx][newy] = newTile;
}
if(tile.entity != null) {
byte tr = stream.readByte();
short health = stream.readShort();
tile.setTeam(Team.all[Bits.getLeftByte(tr)]);
tile.setRotation(Bits.getRightByte(tr));
tile.entity.health = health;
if (tile.entity.items != null) tile.entity.items.read(stream);
if (tile.entity.power != null) tile.entity.power.read(stream);
if (tile.entity.liquids != null) tile.entity.liquids.read(stream);
tile.entity.read(stream);
}
tiles[x][y] = tile;
i += consecutives;
}
tiles[x][y] = tile;
}
player.reset();

View File

@@ -108,7 +108,7 @@ public class Packets {
//player snapshot data
public float x, y, pointerX, pointerY, rotation, baseRotation, xv, yv;
public Tile mining;
public boolean boosting;
public boolean boosting, shooting;
@Override
public void write(ByteBuffer buffer) {
@@ -123,6 +123,7 @@ public class Packets {
buffer.putFloat(player.pointerX);
buffer.putFloat(player.pointerY);
buffer.put(player.isBoosting ? (byte)1 : 0);
buffer.put(player.isShooting ? (byte)1 : 0);
buffer.put((byte)(Mathf.clamp(player.getVelocity().x, -Unit.maxAbsVelocity, Unit.maxAbsVelocity) * Unit.velocityPercision));
buffer.put((byte)(Mathf.clamp(player.getVelocity().y, -Unit.maxAbsVelocity, Unit.maxAbsVelocity) * Unit.velocityPercision));
@@ -144,6 +145,7 @@ public class Packets {
pointerX = buffer.getFloat();
pointerY = buffer.getFloat();
boosting = buffer.get() == 1;
shooting = buffer.get() == 1;
xv = buffer.get() / Unit.velocityPercision;
yv = buffer.get() / Unit.velocityPercision;
rotation = buffer.getShort()/2f;