From 611ea8c0de35f6c793c617422a2b7dc68302e1da Mon Sep 17 00:00:00 2001 From: austin Date: Tue, 10 May 2022 17:14:11 -0400 Subject: [PATCH] fix movement tree networking again --- .../state/movement/GroundMovementTree.java | 152 ++++++------------ .../net/client/protocol/EntityProtocol.java | 14 +- .../net/server/ServerConnectionHandler.java | 20 ++- .../net/server/protocol/AuthProtocol.java | 3 +- .../server/protocol/CharacterProtocol.java | 2 +- 5 files changed, 77 insertions(+), 114 deletions(-) diff --git a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java index 93e4dbb4..6a316f8f 100644 --- a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java @@ -96,6 +96,26 @@ public class GroundMovementTree { if(canStartMoving()){ this.facing = facing; state = MovementTreeState.STARTUP; + //if we aren't the server, alert the server we intend to walk forward + if(!Globals.RUN_SERVER){ + Vector3d position = EntityUtils.getPosition(parent); + Vector3d facingVector = CreatureUtils.getFacingVector(parent); + float velocity = CreatureUtils.getVelocity(parent); + Globals.clientConnection.queueOutgoingMessage( + EntityMessage.constructmoveUpdateMessage( + parent.getId(), + Main.getCurrentFrame(), + position.x, + position.y, + position.z, + facingVector.x, + facingVector.y, + facingVector.z, + velocity, + 0 //magic number corresponding to state startup + ) + ); + } } } @@ -106,6 +126,26 @@ public class GroundMovementTree { public void slowdown(){ state = MovementTreeState.SLOWDOWN; + //if we aren't the server, alert the server we intend to slow down + if(!Globals.RUN_SERVER){ + Vector3d position = EntityUtils.getPosition(parent); + Vector3d facingVector = CreatureUtils.getFacingVector(parent); + float velocity = CreatureUtils.getVelocity(parent); + Globals.clientConnection.queueOutgoingMessage( + EntityMessage.constructmoveUpdateMessage( + parent.getId(), + Main.getCurrentFrame(), + position.x, + position.y, + position.z, + facingVector.x, + facingVector.y, + facingVector.z, + velocity, + 2 //magic number corresponding to state slowdown + ) + ); + } } public void simulate(){ @@ -159,17 +199,6 @@ public class GroundMovementTree { if(Globals.RUN_CLIENT){ position.set(message.getpositionX(), message.getpositionY(), message.getpositionZ()); } -// if(Globals.RUN_SERVER){ -// Globals.server.broadcastMessage( -// EntityMessage.constructMoveMessage( -// parent.getId(), -// System.currentTimeMillis(), -// message.getpositionX(), -// message.getpositionY(), -// message.getpositionZ() -// ) -// ); -// } break; case SETFACING: break; @@ -199,14 +228,16 @@ public class GroundMovementTree { } // System.out.println(EntityUtils.getEntityPosition(parent)); // System.out.println(message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ()); - if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_HARD_UPDATE_THRESHOLD){ - EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); - } else if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_SOFT_UPDATE_THRESHOLD){ - EntityUtils.getPosition(parent).add(new Vector3d(message.getpositionX(),message.getpositionY(),message.getpositionZ()).mul(SOFT_UPDATE_MULTIPLIER)); + //this should only fire on the client, we don't want the server snap updating due to client position reporting + if(!Globals.RUN_SERVER){ + if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_HARD_UPDATE_THRESHOLD){ + EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); + } else if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_SOFT_UPDATE_THRESHOLD){ + EntityUtils.getPosition(parent).add(new Vector3d(message.getpositionX(),message.getpositionY(),message.getpositionZ()).mul(SOFT_UPDATE_MULTIPLIER)); + } } + //we want to always update the server facing vector with where the client says they're facing CreatureUtils.setFacingVector(parent, new Vector3d(message.getrotationX(),message.getrotationY(),message.getrotationZ())); - // EntityUtils.getEntityRotation(parent).set(message.getrotationX(), message.getrotationY(), message.getrotationZ(), message.getrotationW()).normalize(); - // velocity = message.getvelocity(); break; } break; @@ -255,20 +286,6 @@ public class GroundMovementTree { GravityUtils.attemptActivateGravity(parent); if(Globals.RUN_SERVER){ -// Globals.server.broadcastMessage( -// EntityMessage.constructmoveUpdateMessage( -// parent.getId(), -// System.currentTimeMillis(), -// newPosition.x, -// newPosition.y, -// newPosition.z, -// movementVector.x, -// movementVector.y, -// movementVector.z, -// velocity, -// 0 -// ) -// ); Globals.dataCellManager.sendNetworkMessageToChunk( EntityMessage.constructmoveUpdateMessage( parent.getId(), @@ -285,21 +302,6 @@ public class GroundMovementTree { Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.z) ); - } else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){ - Globals.clientConnection.queueOutgoingMessage( - EntityMessage.constructmoveUpdateMessage( - parent.getId(), - Main.getCurrentFrame(), - position.x, - position.y, - position.z, - movementVector.x, - movementVector.y, - movementVector.z, - velocity, - 0 - ) - ); } break; case MOVE: @@ -335,20 +337,6 @@ public class GroundMovementTree { GravityUtils.attemptActivateGravity(parent); if(Globals.RUN_SERVER){ -// Globals.server.broadcastMessage( -// EntityMessage.constructmoveUpdateMessage( -// parent.getId(), -// System.currentTimeMillis(), -// newPosition.x, -// newPosition.y, -// newPosition.z, -// movementVector.x, -// movementVector.y, -// movementVector.z, -// velocity, -// 1 -// ) -// ); Globals.dataCellManager.sendNetworkMessageToChunk( EntityMessage.constructmoveUpdateMessage( parent.getId(), @@ -365,21 +353,6 @@ public class GroundMovementTree { Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.z) ); - } else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){ - Globals.clientConnection.queueOutgoingMessage( - EntityMessage.constructmoveUpdateMessage( - parent.getId(), - Main.getCurrentFrame(), - position.x, - position.y, - position.z, - movementVector.x, - movementVector.y, - movementVector.z, - velocity, - 1 - ) - ); } break; case SLOWDOWN: @@ -423,20 +396,6 @@ public class GroundMovementTree { GravityUtils.attemptActivateGravity(parent); if(Globals.RUN_SERVER){ -// Globals.server.broadcastMessage( -// EntityMessage.constructmoveUpdateMessage( -// parent.getId(), -// System.currentTimeMillis(), -// newPosition.x, -// newPosition.y, -// newPosition.z, -// movementVector.x, -// movementVector.y, -// movementVector.z, -// velocity, -// 2 -// ) -// ); Globals.dataCellManager.sendNetworkMessageToChunk( EntityMessage.constructmoveUpdateMessage( parent.getId(), @@ -453,21 +412,6 @@ public class GroundMovementTree { Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.z) ); - } else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){ - Globals.clientConnection.queueOutgoingMessage( - EntityMessage.constructmoveUpdateMessage( - parent.getId(), - Main.getCurrentFrame(), - position.x, - position.y, - position.z, - movementVector.x, - movementVector.y, - movementVector.z, - velocity, - 2 - ) - ); } break; case IDLE: diff --git a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java index 62b4e4d8..05d33de8 100644 --- a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java @@ -39,12 +39,14 @@ public class EntityProtocol { } break; case SPAWNCREATURE: - LoggerInterface.loggerNetworking.DEBUG("Spawn Creature " + message.getentityID() + " at " + message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ()); - CreatureTemplate template = Utilities.deserialize(message.getcreatureTemplate(), CreatureTemplate.class); - newlySpawnedEntity = CreatureUtils.spawnBasicCreature(template.getCreatureType(),template); - EntityUtils.getScale(newlySpawnedEntity).set(0.005f); - EntityUtils.getPosition(newlySpawnedEntity).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); - EntityUtils.setEntityID(newlySpawnedEntity, message.getentityID()); + if(!Globals.RUN_SERVER){ + LoggerInterface.loggerNetworking.DEBUG("Spawn Creature " + message.getentityID() + " at " + message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ()); + CreatureTemplate template = Utilities.deserialize(message.getcreatureTemplate(), CreatureTemplate.class); + newlySpawnedEntity = CreatureUtils.spawnBasicCreature(template.getCreatureType(),template); + EntityUtils.getScale(newlySpawnedEntity).set(0.005f); + EntityUtils.getPosition(newlySpawnedEntity).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); + EntityUtils.setEntityID(newlySpawnedEntity, message.getentityID()); + } break; case DESTROY: //only obey if we're not also the server diff --git a/src/main/java/electrosphere/net/server/ServerConnectionHandler.java b/src/main/java/electrosphere/net/server/ServerConnectionHandler.java index 20c3ec25..f5413d10 100644 --- a/src/main/java/electrosphere/net/server/ServerConnectionHandler.java +++ b/src/main/java/electrosphere/net/server/ServerConnectionHandler.java @@ -202,10 +202,14 @@ public class ServerConnectionHandler implements Runnable { case ENTITY_MESSAGE: switch(((EntityMessage)message).getMessageSubtype()){ case MOVEUPDATE: + //we don't want this to fire if it's the server client because other people will keep running indefinitely + //as the server broadcasts to itself that they're moving case ATTACKUPDATE: + //we don't want this to fire if it's the server client because other people will keep running indefinitely + //as the server broadcasts to itself that they're moving case SPAWNCREATURE: - if(((EntityMessage)message).getentityID()==playerCharacterID){ - //basically don't send the message if this is the player's character and it's a move update + if(isServerClient()){ + //basically don't send the message if this is the player that is also hosting the game } else { networkParser.addOutgoingMessage(message); } @@ -221,6 +225,18 @@ public class ServerConnectionHandler implements Runnable { } } + boolean isConnectionPlayerEntity(int id){ + return id == this.playerCharacterID; + } + + /** + * Returns true if running both client and server in same instance of the app and this is the player for that instance of the app + * @return + */ + boolean isServerClient(){ + return Globals.RUN_SERVER && Globals.RUN_CLIENT && Globals.clientPlayer != null && this.playerID == Globals.clientPlayer.getId(); + } + public void setCreatureTemplate(CreatureTemplate currentCreatureTemplate){ this.currentCreatureTemplate = currentCreatureTemplate; } diff --git a/src/main/java/electrosphere/net/server/protocol/AuthProtocol.java b/src/main/java/electrosphere/net/server/protocol/AuthProtocol.java index 60950ca9..b56be16a 100644 --- a/src/main/java/electrosphere/net/server/protocol/AuthProtocol.java +++ b/src/main/java/electrosphere/net/server/protocol/AuthProtocol.java @@ -18,7 +18,8 @@ public class AuthProtocol { connectionHandler.addMessagetoOutgoingQueue(AuthMessage.constructAuthSuccessMessage()); Player newPlayer = new Player(connectionHandler); Globals.playerManager.registerPlayer(newPlayer); - if(connectionHandler.getIPAddress().contains("127.0.0.1") && Globals.RUN_CLIENT == true){ + //there is a race condition here where if a local non-server client connects first then it breaks + if(connectionHandler.getIPAddress().contains("127.0.0.1") && Globals.RUN_CLIENT == true && Globals.clientPlayer == null){ Globals.clientPlayer = newPlayer; } connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerId())); diff --git a/src/main/java/electrosphere/net/server/protocol/CharacterProtocol.java b/src/main/java/electrosphere/net/server/protocol/CharacterProtocol.java index 688e6196..59307602 100644 --- a/src/main/java/electrosphere/net/server/protocol/CharacterProtocol.java +++ b/src/main/java/electrosphere/net/server/protocol/CharacterProtocol.java @@ -84,7 +84,7 @@ public class CharacterProtocol { TerrainMessage.constructSpawnPositionMessage(Globals.spawnPoint.x, Globals.spawnPoint.z) ); //tell them what player stats they are - connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerCharacterId())); + // connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerCharacterId())); } }