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()){ if(canStartMoving()){
this.facing = facing; this.facing = facing;
state = MovementTreeState.STARTUP; 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(){ public void slowdown(){
state = MovementTreeState.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(){ public void simulate(){
@ -159,17 +199,6 @@ public class GroundMovementTree {
if(Globals.RUN_CLIENT){ if(Globals.RUN_CLIENT){
position.set(message.getpositionX(), message.getpositionY(), message.getpositionZ()); 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; break;
case SETFACING: case SETFACING:
break; break;
@ -199,14 +228,16 @@ public class GroundMovementTree {
} }
// System.out.println(EntityUtils.getEntityPosition(parent)); // System.out.println(EntityUtils.getEntityPosition(parent));
// System.out.println(message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ()); // System.out.println(message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ());
if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_HARD_UPDATE_THRESHOLD){ //this should only fire on the client, we don't want the server snap updating due to client position reporting
EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); if(!Globals.RUN_SERVER){
} else if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_SOFT_UPDATE_THRESHOLD){ if(position.distance(message.getpositionX(),message.getpositionY(),message.getpositionZ()) > STATE_DIFFERENCE_HARD_UPDATE_THRESHOLD){
EntityUtils.getPosition(parent).add(new Vector3d(message.getpositionX(),message.getpositionY(),message.getpositionZ()).mul(SOFT_UPDATE_MULTIPLIER)); 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())); 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;
} }
break; break;
@ -255,20 +286,6 @@ public class GroundMovementTree {
GravityUtils.attemptActivateGravity(parent); GravityUtils.attemptActivateGravity(parent);
if(Globals.RUN_SERVER){ 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( Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage( EntityMessage.constructmoveUpdateMessage(
parent.getId(), parent.getId(),
@ -285,21 +302,6 @@ public class GroundMovementTree {
Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.x),
Globals.serverWorldData.convertRealToChunkSpace(position.z) 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; break;
case MOVE: case MOVE:
@ -335,20 +337,6 @@ public class GroundMovementTree {
GravityUtils.attemptActivateGravity(parent); GravityUtils.attemptActivateGravity(parent);
if(Globals.RUN_SERVER){ 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( Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage( EntityMessage.constructmoveUpdateMessage(
parent.getId(), parent.getId(),
@ -365,21 +353,6 @@ public class GroundMovementTree {
Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.x),
Globals.serverWorldData.convertRealToChunkSpace(position.z) 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; break;
case SLOWDOWN: case SLOWDOWN:
@ -423,20 +396,6 @@ public class GroundMovementTree {
GravityUtils.attemptActivateGravity(parent); GravityUtils.attemptActivateGravity(parent);
if(Globals.RUN_SERVER){ 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( Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage( EntityMessage.constructmoveUpdateMessage(
parent.getId(), parent.getId(),
@ -453,21 +412,6 @@ public class GroundMovementTree {
Globals.serverWorldData.convertRealToChunkSpace(position.x), Globals.serverWorldData.convertRealToChunkSpace(position.x),
Globals.serverWorldData.convertRealToChunkSpace(position.z) 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; break;
case IDLE: case IDLE:

View File

@ -39,12 +39,14 @@ public class EntityProtocol {
} }
break; break;
case SPAWNCREATURE: case SPAWNCREATURE:
LoggerInterface.loggerNetworking.DEBUG("Spawn Creature " + message.getentityID() + " at " + message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ()); if(!Globals.RUN_SERVER){
CreatureTemplate template = Utilities.deserialize(message.getcreatureTemplate(), CreatureTemplate.class); LoggerInterface.loggerNetworking.DEBUG("Spawn Creature " + message.getentityID() + " at " + message.getpositionX() + " " + message.getpositionY() + " " + message.getpositionZ());
newlySpawnedEntity = CreatureUtils.spawnBasicCreature(template.getCreatureType(),template); CreatureTemplate template = Utilities.deserialize(message.getcreatureTemplate(), CreatureTemplate.class);
EntityUtils.getScale(newlySpawnedEntity).set(0.005f); newlySpawnedEntity = CreatureUtils.spawnBasicCreature(template.getCreatureType(),template);
EntityUtils.getPosition(newlySpawnedEntity).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); EntityUtils.getScale(newlySpawnedEntity).set(0.005f);
EntityUtils.setEntityID(newlySpawnedEntity, message.getentityID()); EntityUtils.getPosition(newlySpawnedEntity).set(message.getpositionX(),message.getpositionY(),message.getpositionZ());
EntityUtils.setEntityID(newlySpawnedEntity, message.getentityID());
}
break; break;
case DESTROY: case DESTROY:
//only obey if we're not also the server //only obey if we're not also the server

View File

@ -202,10 +202,14 @@ public class ServerConnectionHandler implements Runnable {
case ENTITY_MESSAGE: case ENTITY_MESSAGE:
switch(((EntityMessage)message).getMessageSubtype()){ switch(((EntityMessage)message).getMessageSubtype()){
case MOVEUPDATE: 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: 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: case SPAWNCREATURE:
if(((EntityMessage)message).getentityID()==playerCharacterID){ if(isServerClient()){
//basically don't send the message if this is the player's character and it's a move update //basically don't send the message if this is the player that is also hosting the game
} else { } else {
networkParser.addOutgoingMessage(message); 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){ public void setCreatureTemplate(CreatureTemplate currentCreatureTemplate){
this.currentCreatureTemplate = currentCreatureTemplate; this.currentCreatureTemplate = currentCreatureTemplate;
} }

View File

@ -18,7 +18,8 @@ public class AuthProtocol {
connectionHandler.addMessagetoOutgoingQueue(AuthMessage.constructAuthSuccessMessage()); connectionHandler.addMessagetoOutgoingQueue(AuthMessage.constructAuthSuccessMessage());
Player newPlayer = new Player(connectionHandler); Player newPlayer = new Player(connectionHandler);
Globals.playerManager.registerPlayer(newPlayer); 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; Globals.clientPlayer = newPlayer;
} }
connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerId())); connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerId()));

View File

@ -84,7 +84,7 @@ public class CharacterProtocol {
TerrainMessage.constructSpawnPositionMessage(Globals.spawnPoint.x, Globals.spawnPoint.z) TerrainMessage.constructSpawnPositionMessage(Globals.spawnPoint.x, Globals.spawnPoint.z)
); );
//tell them what player stats they are //tell them what player stats they are
connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerCharacterId())); // connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerCharacterId()));
} }
} }