fix movement tree networking again

This commit is contained in:
austin 2022-05-10 17:14:11 -04:00
parent d8fccbe955
commit 611ea8c0de
5 changed files with 77 additions and 114 deletions

View File

@ -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());
//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:

View File

@ -39,12 +39,14 @@ public class EntityProtocol {
}
break;
case SPAWNCREATURE:
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

View File

@ -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;
}

View File

@ -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()));

View File

@ -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()));
}
}