Autogenerated interpolation

This commit is contained in:
Anuken
2020-05-24 14:00:53 -04:00
parent 1acb5fc56c
commit 7c06ba94c1
36 changed files with 252 additions and 183 deletions

View File

@@ -36,6 +36,7 @@ import static mindustry.Vars.*;
public class NetServer implements ApplicationListener{
private final static int maxSnapshotSize = 430, timerBlockSync = 0;
private final static float serverSyncTime = 12, blockSyncTime = 60 * 8;
private final static FloatBuffer fbuffer = FloatBuffer.allocate(20);
private final static Vec2 vector = new Vec2();
private final static Rect viewport = new Rect();
/** If a player goes away of their server-side coordinates by this distance, they get teleported back. */
@@ -598,34 +599,53 @@ public class NetServer implements ApplicationListener{
float maxSpeed = player.dead() ? Float.MAX_VALUE : player.unit().type().speed;
float maxMove = elapsed / 1000f * 60f * maxSpeed * 1.1f;
vector.set(x - unit.interpolator().target.x, y - unit.interpolator().target.y);
if(connection.lastUnit != unit && !player.dead()){
connection.lastUnit = unit;
connection.lastPosition.set(unit);
}
vector.set(x, y).sub(connection.lastPosition);
vector.limit(maxMove);
float prevx = unit.x(), prevy = unit.y();
unit.set(unit.interpolator().target.x, unit.interpolator().target.y);
unit.set(connection.lastPosition);
if(!unit.isFlying()){
unit.move(vector.x, vector.y);
}else{
unit.trns(vector.x, vector.y);
}
//set last position after movement
connection.lastPosition.set(unit);
float newx = unit.x(), newy = unit.y();
if(!verifyPosition){
unit.x(prevx);
unit.y(prevy);
unit.set(prevx, prevy);
newx = x;
newy = y;
}else if(Mathf.dst(x, y, newx, newy) > correctDist){
}else if(!Mathf.within(x, y, newx, newy, correctDist)){
Call.onPositionSet(player.con(), newx, newy); //teleport and correct position when necessary
}
//reset player to previous synced position so it gets interpolated
unit.x(prevx);
unit.y(prevy);
unit.set(prevx, prevy);
//set interpolator target to *new* position so it moves toward it
unit.interpolator().read(unit.x(), unit.y(), newx, newy, rotation, baseRotation);
unit.vel().set(xVelocity, yVelocity); //only for visual calculation purposes, doesn't actually update the player
//write sync data to the buffer
fbuffer.limit(20);
fbuffer.position(0);
//now, put the new position, rotation and baserotation into the buffer so it can be read
if(unit instanceof Legsc) fbuffer.put(baseRotation); //base rotation is optional
fbuffer.put(rotation); //rotation is always there
fbuffer.put(newx);
fbuffer.put(newy);
fbuffer.flip();
//read sync data so it can be used for interpolation for the server
unit.readSyncManual(fbuffer);
//TODO fix
unit.vel().set(xVelocity, yVelocity); //only for visual calculation purposes, doesn't actually update the player (TODO or does it?)
}else{
player.x(x);
player.y(y);
@@ -795,7 +815,7 @@ public class NetServer implements ApplicationListener{
//write all entities now
dataStream.writeInt(entity.id()); //write id
dataStream.writeByte(entity.classId()); //write type ID
entity.write(Writes.get(dataStream)); //write entity
entity.writeSync(Writes.get(dataStream)); //write entity
sent++;