Get networking working on basic level

This commit is contained in:
austin 2022-04-23 22:05:50 -04:00
parent 5cc0998b26
commit d4513b84a4
9 changed files with 103 additions and 29 deletions

View File

@ -1,4 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx4G -Xms100m"
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx8G -Xms100m"
}

View File

@ -151,10 +151,10 @@ public class LoadingThread extends Thread {
LoggerInterface.loggerEngine.INFO("run server: " + Globals.RUN_SERVER + " run client: " + Globals.RUN_CLIENT);
//init the data of the world
// if(Globals.RUN_SERVER){
if(Globals.RUN_SERVER){
// initServerGameWorldData();
// initDataCellManager();
// }
initDataCellManager();
}
//initialize the server thread (server only)
if(Globals.RUN_SERVER){
@ -243,7 +243,9 @@ public class LoadingThread extends Thread {
initServerArenaWorldData();
//init data cell manager
initDataCellManager();
if(Globals.RUN_SERVER){
initDataCellManager();
}
//initialize the server thread (server only)
if(Globals.RUN_SERVER){

View File

@ -39,6 +39,7 @@ import electrosphere.game.data.creature.type.rotator.RotatorItem;
import electrosphere.game.data.creature.type.rotator.RotatorSystem;
import electrosphere.game.data.creature.type.visualattribute.AttributeVariant;
import electrosphere.game.data.creature.type.visualattribute.VisualAttribute;
import electrosphere.game.server.datacell.ServerDataCell;
import electrosphere.logger.LoggerInterface;
import electrosphere.main.Globals;
import electrosphere.main.Main;
@ -424,6 +425,32 @@ public class CreatureUtils {
public static SprintTree getSprintTree(Entity e){
return (SprintTree)e.getData(EntityDataStrings.SPRINT_TREE);
}
public static void repositionCreature(Entity creature, Vector3d position){
//if server, get current server data cell
if(Globals.RUN_SERVER){
ServerDataCell oldDataCell = Globals.dataCellManager.getDataCellAtPoint(EntityUtils.getPosition(creature));
ServerDataCell newDataCell = Globals.dataCellManager.getDataCellAtPoint(position);
if(newDataCell != null){
newDataCell.addEntity(creature);
if(oldDataCell != null && oldDataCell != newDataCell){
oldDataCell.removeEntity(creature);
}
} else {
//if it doesn't already exist, try creating it and if successfull move creature
newDataCell = Globals.dataCellManager.tryCreateGroundDataCell(EntityUtils.getPosition(creature));
if(newDataCell != null){
newDataCell.addEntity(creature);
if(oldDataCell != null && oldDataCell != newDataCell){
oldDataCell.removeEntity(creature);
}
}
}
}
//reposition entity
CollisionObjUtils.positionCharacter(creature, new Vector3f(Globals.spawnPoint.x,Globals.spawnPoint.y,Globals.spawnPoint.z));
}
}

View File

@ -148,5 +148,38 @@ public class DataCellManager {
public void unloadPlayerlessChunks(){
//TODO: implement and actually call it
}
public ServerDataCell getDataCellAtPoint(Vector3d point){
ServerDataCell rVal = null;
int worldX = Globals.serverWorldData.convertRealToChunkSpace(point.x);
int worldZ = Globals.serverWorldData.convertRealToChunkSpace(point.z);
if(
//in bounds of array
worldX >= 0 && worldX < groundDataCells.length &&
worldZ >= 0 && worldZ < groundDataCells[0].length &&
//isn't null
groundDataCells[worldX][worldZ] != null
){
rVal = groundDataCells[worldX][worldZ];
}
return rVal;
}
public ServerDataCell tryCreateGroundDataCell(Vector3d point){
int worldX = Globals.serverWorldData.convertRealToChunkSpace(point.x);
int worldZ = Globals.serverWorldData.convertRealToChunkSpace(point.z);
if(
//in bounds of array
worldX >= 0 && worldX < groundDataCells.length &&
worldZ >= 0 && worldZ < groundDataCells[0].length &&
//isn't null
groundDataCells[worldX][worldZ] == null
){
groundDataCells[worldX][worldZ] = new ServerDataCell();
}
return groundDataCells[worldX][worldZ];
}
}

View File

@ -80,8 +80,9 @@ public class ServerDataCell {
* @param character
*/
public void addCharacter(Character character){
Entity newEntity = new Entity();
loadedEntities.add(newEntity);
// Entity newEntity = new Entity();
// loadedEntities.add(newEntity);
throw new UnsupportedOperationException("Not implemented yet");
}
/**
@ -101,7 +102,6 @@ public class ServerDataCell {
*/
public void removeEntity(Entity e){
loadedEntities.remove(e);
}
/**

View File

@ -142,6 +142,10 @@ public class ServerConnectionHandler implements Runnable {
public int getPlayerCharacterId(){
return playerCharacterID;
}
public String getIPAddress(){
return socket.getRemoteSocketAddress().toString();
}
public void addMessagetoOutgoingQueue(NetworkMessage message){
switch(message.getType()){

View File

@ -28,6 +28,7 @@ public class Player {
id = idIncrementer;
idIncrementer++;
idIncrementerLock.release();
connectionHandler.setPlayerId(id);
this.simulationRadius = Globals.userSettings.getGameplayPhysicsCellRadius();
}

View File

@ -2,7 +2,9 @@ package electrosphere.net.server.protocol;
import electrosphere.main.Globals;
import electrosphere.net.parser.net.message.AuthMessage;
import electrosphere.net.parser.net.message.PlayerMessage;
import electrosphere.net.server.ServerConnectionHandler;
import electrosphere.net.server.player.Player;
public class AuthProtocol {
@ -14,6 +16,12 @@ public class AuthProtocol {
if(successfulLogin){
//TODO: actually set connection/protocol to authenticated
connectionHandler.addMessagetoOutgoingQueue(AuthMessage.constructAuthSuccessMessage());
Player newPlayer = new Player(connectionHandler);
Globals.playerManager.registerPlayer(newPlayer);
if(connectionHandler.getIPAddress().contains("127.0.0.1")){
Globals.serverPlayer = newPlayer;
}
connectionHandler.addMessagetoOutgoingQueue(PlayerMessage.constructSet_IDMessage(connectionHandler.getPlayerId()));
} else {
connectionHandler.addMessagetoOutgoingQueue(AuthMessage.constructAuthFailureMessage());
}

View File

@ -1,5 +1,6 @@
package electrosphere.net.server.protocol;
import org.joml.Vector3d;
import org.joml.Vector3f;
import electrosphere.entity.Entity;
@ -40,41 +41,39 @@ public class CharacterProtocol {
}
static void spawnPlayerCharacter(ServerConnectionHandler connectionHandler){
Player newPlayerObject = new Player(connectionHandler);
Globals.playerManager.registerPlayer(newPlayerObject);
Player playerObject = Globals.playerManager.getPlayerFromId(connectionHandler.getPlayerId());
//spawn player in world
Entity newPlayerCharacter = CreatureUtils.spawnBasicCreature("human");
int playerCharacterId = newPlayerCharacter.getId();
Entity newPlayerEntity = CreatureUtils.spawnBasicCreature("human");
int playerCharacterId = newPlayerEntity.getId();
connectionHandler.setPlayerCharacterId(playerCharacterId);
CollisionObjUtils.positionCharacter(newPlayerCharacter, new Vector3f(Globals.spawnPoint.x,Globals.spawnPoint.y,Globals.spawnPoint.z));
CreatureUtils.repositionCreature(newPlayerEntity, new Vector3d(Globals.spawnPoint.x,Globals.spawnPoint.y,Globals.spawnPoint.z));
CreatureUtils.setControllerPlayerId(newPlayerEntity, connectionHandler.getPlayerId());
//attach player object to player character
newPlayerObject.setPlayerEntity(newPlayerCharacter);
newPlayerObject.setWorldX(Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x));
newPlayerObject.setWorldY(Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
playerObject.setPlayerEntity(newPlayerEntity);
playerObject.setWorldX(Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x));
playerObject.setWorldY(Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
// System.out.println(EntityUtils.getRotation(newPlayerCharacter).set(0,1,0,1).normalize());
Globals.dataCellManager.addPlayerToGroundCells(newPlayerObject);
Globals.dataCellManager.movePlayerGroundCells(newPlayerObject, Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x), Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
IronSightTree.attachIronSightTree(newPlayerCharacter);
Globals.dataCellManager.addPlayerToGroundCells(playerObject);
Globals.dataCellManager.movePlayerGroundCells(playerObject, Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x), Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
IronSightTree.attachIronSightTree(newPlayerEntity);
// //spawn player sword
// Entity sword = ItemUtils.spawnBasicItem("Katana");
// AttachUtils.attachEntityToEntityAtBone(newPlayerCharacter, sword, "Bone.031");
//set controller id
CreatureUtils.setControllerPlayerId(newPlayerCharacter, playerCharacterId);
if(Globals.RUN_SERVER && Main.playerId == -1){
Globals.playerEntity = newPlayerCharacter;
Globals.serverPlayer = newPlayerObject;
if(playerObject == Globals.serverPlayer){
Globals.playerEntity = newPlayerEntity;
}
}
static void spawnClientEntity(ServerConnectionHandler connectionHandler){
spawnPlayerCharacter(connectionHandler);
//set client initial discrete position
connectionHandler.addMessagetoOutgoingQueue(
PlayerMessage.constructSetInitialDiscretePositionMessage(
Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x),
Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z)
)
);
// connectionHandler.addMessagetoOutgoingQueue(
// PlayerMessage.constructSetInitialDiscretePositionMessage(
// Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x),
// Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z)
// )
// );
//send spawn point
connectionHandler.addMessagetoOutgoingQueue(
TerrainMessage.constructSpawnPositionMessage(Globals.spawnPoint.x, Globals.spawnPoint.z)