diff options
Diffstat (limited to 'NET/worlds/scape/InterpolatedDrone.java')
| -rw-r--r-- | NET/worlds/scape/InterpolatedDrone.java | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/NET/worlds/scape/InterpolatedDrone.java b/NET/worlds/scape/InterpolatedDrone.java new file mode 100644 index 0000000..bf50bb4 --- /dev/null +++ b/NET/worlds/scape/InterpolatedDrone.java @@ -0,0 +1,247 @@ +package NET.worlds.scape; + +import NET.worlds.console.BBAppearDroneCommand; +import NET.worlds.console.BBDisappearDroneCommand; +import NET.worlds.console.BBDroneDeltaPosCommand; +import NET.worlds.console.BBMoveDroneCommand; +import NET.worlds.console.BlackBox; +import NET.worlds.core.Std; +import NET.worlds.network.ObjID; +import NET.worlds.network.URL; +import NET.worlds.network.WorldServer; +import java.io.IOException; + +public class InterpolatedDrone extends Drone implements FrameHandler { + private int _last_FrameTime; + private int _last_PosTime; + private int _vel_x; + private int _vel_y; + private int _vel_z; + private int _vel_yaw; + private int _last_x; + private int _last_y; + private int _last_z; + private int _last_yaw; + private int _x; + private int _y; + private int _z; + private int _yaw; + private boolean inited = false; + private static Object classCookie = new Object(); + + public InterpolatedDrone(ObjID objID, WorldServer serv) { + super(objID, serv); + } + + public InterpolatedDrone() { + } + + @Override + public Point3Temp getVelocity() { + return Point3Temp.make(this._vel_x, this._vel_y, this._vel_z); + } + + @Override + public int getYawRate() { + return this._vel_yaw; + } + + @Override + public boolean handle(FrameEvent fe) { + if (this._server == null) { + return true; + } else { + int timeNow = fe.time; + this.interpolate(timeNow, this._server.getUpdateTime(), this); + return true; + } + } + + @Override + public void interpolate(int timeNow, int updateTime, Transform target) { + if (this.inited) { + if (timeNow - this._last_PosTime > updateTime) { + this._last_PosTime = timeNow; + this._vel_x = this._last_x - this._x; + this._vel_y = this._last_y - this._y; + this._vel_z = this._last_z - this._z; + this._vel_yaw = ((this._last_yaw - this._yaw) % 360 + 360) % 360; + + assert this._vel_yaw >= 0; + + if (this._vel_yaw > 180) { + this._vel_yaw -= 360; + } + } + + double timeDiff = (double)(timeNow - this._last_FrameTime) / updateTime; + if (timeNow - this._last_FrameTime > updateTime) { + timeDiff = 0.0; + } + + this._x = this._x + (int)(timeDiff * this._vel_x); + this._y = this._y + (int)(timeDiff * this._vel_y); + this._z = this._z + (int)(timeDiff * this._vel_z); + this._yaw = this._yaw + (int)(timeDiff * this._vel_yaw) % 360; + target.makeIdentity().moveBy(this._x, this._y, this._z).yaw(this._yaw); + this._last_FrameTime = timeNow; + } + } + + @Override + public void reset(short x, short y, short z, short yaw) { + this._x = x; + this._y = y; + this._z = z; + this._yaw = yaw; + this._vel_x = this._vel_y = this._vel_z = 0; + this._vel_yaw = 0; + this._last_x = this._x; + this._last_y = this._y; + this._last_z = this._z; + this._last_yaw = this._yaw; + this._last_PosTime = this._last_FrameTime = Std.getRealTime(); + this.inited = true; + } + + @Override + public void appear(Room rm, short x, short y, short z, short yaw) { + assert rm != null; + + BlackBox.getInstance().submitEvent(new BBAppearDroneCommand(rm.toString(), this.getName(), x, y, z, yaw)); + if (this.getRoom() != rm) { + this.detach(); + rm.add(this); + } + + this.makeIdentity().moveBy(x, y, z).yaw(yaw); + this._x = x; + this._y = y; + this._z = z; + this._yaw = yaw; + this._vel_x = this._vel_y = this._vel_z = 0; + this._vel_yaw = 0; + this._last_x = x; + this._last_y = y; + this._last_z = z; + this._last_yaw = yaw; + this._last_PosTime = this._last_FrameTime = Std.getRealTime(); + URL u = this.getCurrentURL(); + if (u != null) { + this.setAvatarNow(u); + } + + this.inited = true; + } + + @Override + public void disappear() { + BlackBox.getInstance().submitEvent(new BBDisappearDroneCommand(this.getName())); + this.detachFromServer(true); + this.detach(); + } + + @Override + public void longLoc(short x, short y, short z, short yaw) { + if (!(this.getOwner() instanceof Pilot)) { + BlackBox.getInstance().submitEvent(new BBMoveDroneCommand(this.getName(), x, y, z, yaw)); + } + + this._last_x = x; + this._last_y = y; + this._last_z = z; + this._last_yaw = yaw; + this._vel_x = this._last_x - this._x; + this._vel_y = this._last_y - this._y; + this._vel_z = this._last_z - this._z; + this._vel_yaw = ((this._last_yaw - this._yaw) % 360 + 360) % 360; + + assert this._vel_yaw >= 0; + + if (this._vel_yaw > 180) { + this._vel_yaw -= 360; + } + + this._last_PosTime = Std.getRealTime(); + this.inited = true; + } + + @Override + protected void transferFrom(Drone d) { + super.transferFrom(d); + if (d instanceof InterpolatedDrone) { + InterpolatedDrone i = (InterpolatedDrone)d; + this._x = i._x; + this._y = i._y; + this._z = i._z; + this._yaw = i._yaw; + this._vel_x = i._vel_x; + this._vel_y = i._vel_y; + this._vel_z = i._vel_z; + this._vel_yaw = i._vel_yaw; + this._last_x = i._last_x; + this._last_y = i._last_y; + this._last_z = i._last_z; + this._last_yaw = i._last_yaw; + this._last_PosTime = i._last_PosTime; + this._last_FrameTime = i._last_FrameTime; + this.inited = true; + } + } + + @Override + public void roomChange(Room newRoom, short x, short y, short z, short yaw) { + this.detach(); + if (newRoom != null) { + this.appear(newRoom, x, y, z, yaw); + } + } + + @Override + public void shortLoc(byte dx, byte dy, byte dyaw) { + if (!(this.getOwner() instanceof Pilot)) { + BlackBox.getInstance().submitEvent(new BBDroneDeltaPosCommand(this.getName(), dx, dy, dyaw)); + } + + this._last_x += dx; + this._last_y += dy; + this._last_yaw += dyaw; + this._last_yaw %= 360; + this._vel_x = this._last_x - this._x; + this._vel_y = this._last_y - this._y; + this._vel_z = 0; + this._vel_yaw = ((this._last_yaw - this._yaw) % 360 + 360) % 360; + + assert this._vel_yaw >= 0; + + if (this._vel_yaw > 180) { + this._vel_yaw -= 360; + } + + this._last_PosTime = Std.getRealTime(); + } + + @Override + public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException { + Object ret = null; + int var10000 = index - offset; + return super.properties(index, offset + 0, mode, value); + } + + @Override + public void saveState(Saver s) throws IOException { + s.saveVersion(0, classCookie); + super.saveState(s); + } + + @Override + public void restoreState(Restorer r) throws IOException, TooNewException { + switch (r.restoreVersion(classCookie)) { + case 0: + super.restoreState(r); + return; + default: + throw new TooNewException(); + } + } +} |