From 2c42b51907dc100b35fa3c8747020eb13235fb21 Mon Sep 17 00:00:00 2001 From: austin Date: Tue, 3 May 2022 23:34:38 -0400 Subject: [PATCH] streaming creature templates over network --- net/terrain.json | 4 +-- .../electrosphere/engine/LoadingThread.java | 6 ++-- .../types/collision/CollisionObjUtils.java | 4 +-- .../entity/types/creature/CreatureUtils.java | 3 +- .../game/server/unit/UnitUtils.java | 4 ++- src/main/java/electrosphere/main/Globals.java | 3 +- .../menu/MenuGeneratorsMultiplayer.java | 2 -- .../client/protocol/CharacterProtocol.java | 2 +- .../net/client/protocol/EntityProtocol.java | 2 +- .../net/client/protocol/TerrainProtocol.java | 4 ++- .../parser/net/message/TerrainMessage.java | 34 ++++++++++--------- .../net/parser/net/message/TypeBytes.java | 2 +- .../net/server/ServerConnectionHandler.java | 2 ++ .../net/server/protocol/EntityProtocol.java | 8 ++--- .../renderer/RenderingEngine.java | 19 +++++++---- .../renderer/ui/elements/ActorPanel.java | 10 ++++++ 16 files changed, 67 insertions(+), 42 deletions(-) diff --git a/net/terrain.json b/net/terrain.json index 177905b5..9f054f09 100644 --- a/net/terrain.json +++ b/net/terrain.json @@ -61,11 +61,11 @@ { "name" : "realLocationX", - "type" : "FIXED_FLOAT" + "type" : "FIXED_DOUBLE" }, { "name" : "realLocationY", - "type" : "FIXED_FLOAT" + "type" : "FIXED_DOUBLE" }, diff --git a/src/main/java/electrosphere/engine/LoadingThread.java b/src/main/java/electrosphere/engine/LoadingThread.java index 1406ffd5..31e357d3 100644 --- a/src/main/java/electrosphere/engine/LoadingThread.java +++ b/src/main/java/electrosphere/engine/LoadingThread.java @@ -250,7 +250,9 @@ public class LoadingThread extends Thread { //initialize the basic graphical entities of the world (skybox, camera) initWorldBaseGraphicalEntities(); //init arena specific stuff (ie different skybox colors) - initArenaGraphicalEntities(); + if(!Globals.RUN_SERVER){ + initArenaGraphicalEntities(); + } //sets micro and macro sims to ready if they exist if(!Globals.RUN_SERVER){ setSimulationsToReady(); @@ -356,7 +358,7 @@ public class LoadingThread extends Thread { static void initServerArenaWorldData(){ Globals.serverWorldData = ServerWorldData.createArenaWorld(); - Globals.spawnPoint = new Vector3f(1,0.1f,1); + Globals.spawnPoint = new Vector3d(1,2.0,1); // Globals.serverTerrainManager.getChunk(0, 0).addModification(new TerrainModification(0,0,5,5,5)); } diff --git a/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java b/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java index ba28d4da..e18c3584 100644 --- a/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java +++ b/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java @@ -174,11 +174,11 @@ public class CollisionObjUtils { return (CollisionObject)e.getData(EntityDataStrings.PHYSICS_COLLISION_BODY); } - public static void positionCharacter(Entity e, Vector3f position){ + public static void positionCharacter(Entity e, Vector3d position){ EntityUtils.getPosition(e).set(position); Quaternionf rotation = EntityUtils.getRotation(e); CollisionObject body = getCollisionBody(e); - PhysicsUtils.setRigidBodyTransform(position, rotation, body); + PhysicsUtils.setRigidBodyTransform(new Vector3f((float)position.x,(float)position.y,(float)position.z), rotation, body); } public static Collidable getCollidable(Entity e){ diff --git a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java index 5f7e4cc6..bad824f0 100644 --- a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java +++ b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java @@ -517,7 +517,8 @@ public class CreatureUtils { } } //reposition entity - CollisionObjUtils.positionCharacter(creature, new Vector3f(Globals.spawnPoint.x,Globals.spawnPoint.y,Globals.spawnPoint.z)); + // CreatureUtils.repositionCreature(creature, Globals.spawnPoint); + CollisionObjUtils.positionCharacter(creature, Globals.spawnPoint); } diff --git a/src/main/java/electrosphere/game/server/unit/UnitUtils.java b/src/main/java/electrosphere/game/server/unit/UnitUtils.java index a268b9e7..c57553a8 100644 --- a/src/main/java/electrosphere/game/server/unit/UnitUtils.java +++ b/src/main/java/electrosphere/game/server/unit/UnitUtils.java @@ -7,6 +7,8 @@ import electrosphere.entity.types.collision.CollisionObjUtils; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.game.server.ai.creature.MillAbout; + +import org.joml.Vector3d; import org.joml.Vector3f; /** @@ -17,7 +19,7 @@ public class UnitUtils { public static void spawnTextGoblin(float posX, float posY, float posZ){ Entity goblin = CreatureUtils.spawnBasicCreature("Goblin",null); - CollisionObjUtils.positionCharacter(goblin, new Vector3f(posX, posY, posZ)); + CollisionObjUtils.positionCharacter(goblin, new Vector3d(posX, posY, posZ)); EntityUtils.getScale(goblin).set(0.005f); //give evil goblin sword Entity goblinSword = ItemUtils.spawnBasicItem("Katana"); diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index 821f74f6..2a8503cd 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -70,6 +70,7 @@ import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import org.joml.Matrix4f; +import org.joml.Vector3d; import org.joml.Vector3f; import electrosphere.util.ModelLoader; import electrosphere.util.Utilities; @@ -249,7 +250,7 @@ public class Globals { //terrain manager // public static boolean LOAD_TERRAIN = true; public static ServerTerrainManager serverTerrainManager; - public static Vector3f spawnPoint = new Vector3f(0,0,0); + public static Vector3d spawnPoint = new Vector3d(0,0,0); //manages all models loaded into memory public static AssetManager assetManager; diff --git a/src/main/java/electrosphere/menu/MenuGeneratorsMultiplayer.java b/src/main/java/electrosphere/menu/MenuGeneratorsMultiplayer.java index 1d0fabd7..dbac792b 100644 --- a/src/main/java/electrosphere/menu/MenuGeneratorsMultiplayer.java +++ b/src/main/java/electrosphere/menu/MenuGeneratorsMultiplayer.java @@ -104,8 +104,6 @@ public class MenuGeneratorsMultiplayer { ActorPanel actorPanel = new ActorPanel(1200, 100, 500, 500, characterActor); actorPanel.setAnimation(Animation.ANIMATION_IDLE_1); actorPanel.setPosition(new Vector3f(0,-1,-0.5f)); - RenderingEngine.setFOV(50); - RenderingEngine.setAspectRatio(1.0f); // actorPanel.setRotation(new Quaternionf().rotateLocalY(0)); actorPanel.setScale(new Vector3f(0.01f,0.01f,0.01f)); diff --git a/src/main/java/electrosphere/net/client/protocol/CharacterProtocol.java b/src/main/java/electrosphere/net/client/protocol/CharacterProtocol.java index 22cc9e42..38072a18 100644 --- a/src/main/java/electrosphere/net/client/protocol/CharacterProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/CharacterProtocol.java @@ -11,8 +11,8 @@ public class CharacterProtocol { switch(message.getMessageSubtype()){ case RESPONSECREATECHARACTERSUCCESS: //trigger request to spawn character - Globals.clientConnection.queueOutgoingMessage(TerrainMessage.constructRequestMetadataMessage()); Globals.clientConnection.queueOutgoingMessage(CharacterMessage.constructRequestSpawnCharacterMessage()); + Globals.clientConnection.queueOutgoingMessage(TerrainMessage.constructRequestMetadataMessage()); LoadingThread clientThread = new LoadingThread(LoadingThread.LOAD_CLIENT_WORLD); Globals.loadingThreadsList.add(clientThread); clientThread.start(); diff --git a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java index 2001aa2b..05137ed1 100644 --- a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java @@ -36,7 +36,7 @@ public class EntityProtocol { } break; case SPAWNCREATURE: - LoggerInterface.loggerNetworking.DEBUG("Spawn ID " + message.getentityID() + " of type " + message.getentityCategory() + " subtype " + message.getentitySubtype()); + 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); diff --git a/src/main/java/electrosphere/net/client/protocol/TerrainProtocol.java b/src/main/java/electrosphere/net/client/protocol/TerrainProtocol.java index 06d45197..c9b22726 100644 --- a/src/main/java/electrosphere/net/client/protocol/TerrainProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/TerrainProtocol.java @@ -1,5 +1,7 @@ package electrosphere.net.client.protocol; +import javax.vecmath.Vector3d; + import org.joml.Vector3f; import electrosphere.game.client.world.ClientWorldData; @@ -26,7 +28,7 @@ public class TerrainProtocol { Globals.clientTerrainManager.attachTerrainMessage(message); break; case SPAWNPOSITION: - Globals.spawnPoint.set(new Vector3f(message.getrealLocationX(),0.25f,message.getrealLocationY())); + Globals.spawnPoint.set(message.getrealLocationX(),0.25,message.getrealLocationY()); break; case CHUNKLOADSTART: Globals.clientTerrainManager.attachTerrainMessage(message); diff --git a/src/main/java/electrosphere/net/parser/net/message/TerrainMessage.java b/src/main/java/electrosphere/net/parser/net/message/TerrainMessage.java index ab88be0f..ea4ba981 100644 --- a/src/main/java/electrosphere/net/parser/net/message/TerrainMessage.java +++ b/src/main/java/electrosphere/net/parser/net/message/TerrainMessage.java @@ -30,8 +30,8 @@ public class TerrainMessage extends NetworkMessage { float value; int locationX; int locationY; - float realLocationX; - float realLocationY; + double realLocationX; + double realLocationY; float macroValue00; float macroValue01; float macroValue02; @@ -188,19 +188,19 @@ public class TerrainMessage extends NetworkMessage { this.locationY = locationY; } - public float getrealLocationX() { + public double getrealLocationX() { return realLocationX; } - public void setrealLocationX(float realLocationX) { + public void setrealLocationX(double realLocationX) { this.realLocationX = realLocationX; } - public float getrealLocationY() { + public double getrealLocationY() { return realLocationY; } - public void setrealLocationY(float realLocationY) { + public void setrealLocationY(double realLocationY) { this.realLocationY = realLocationY; } @@ -892,12 +892,12 @@ public class TerrainMessage extends NetworkMessage { public static TerrainMessage parseSpawnPositionMessage(List byteStream){ TerrainMessage rVal = new TerrainMessage(TerrainMessageType.SPAWNPOSITION); stripPacketHeader(byteStream); - rVal.setrealLocationX(ByteStreamUtils.popFloatFromByteQueue(byteStream)); - rVal.setrealLocationY(ByteStreamUtils.popFloatFromByteQueue(byteStream)); + rVal.setrealLocationX(ByteStreamUtils.popDoubleFromByteQueue(byteStream)); + rVal.setrealLocationY(ByteStreamUtils.popDoubleFromByteQueue(byteStream)); return rVal; } - public static TerrainMessage constructSpawnPositionMessage(float realLocationX,float realLocationY){ + public static TerrainMessage constructSpawnPositionMessage(double realLocationX,double realLocationY){ TerrainMessage rVal = new TerrainMessage(TerrainMessageType.SPAWNPOSITION); rVal.setrealLocationX(realLocationX); rVal.setrealLocationY(realLocationY); @@ -1216,18 +1216,20 @@ public class TerrainMessage extends NetworkMessage { } break; case SPAWNPOSITION: - rawBytes = new byte[2+4+4]; + rawBytes = new byte[2+8+8]; //message header rawBytes[0] = TypeBytes.MESSAGE_TYPE_TERRAIN; //entity messaage header rawBytes[1] = TypeBytes.TERRAIN_MESSAGE_TYPE_SPAWNPOSITION; - intValues = ByteStreamUtils.serializeFloatToBytes(realLocationX); - for(int i = 0; i < 4; i++){ + intValues = ByteStreamUtils.serializeDoubleToBytes(realLocationX); + for(int i = 0; i < 8; i++){ rawBytes[2+i] = intValues[i]; - } intValues = ByteStreamUtils.serializeFloatToBytes(realLocationY); - for(int i = 0; i < 4; i++){ - rawBytes[6+i] = intValues[i]; - } break; + } + intValues = ByteStreamUtils.serializeDoubleToBytes(realLocationY); + for(int i = 0; i < 8; i++){ + rawBytes[10+i] = intValues[i]; + } + break; } serialized = true; } diff --git a/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java b/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java index 2f98c77f..dca2c28e 100644 --- a/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java +++ b/src/main/java/electrosphere/net/parser/net/message/TypeBytes.java @@ -84,7 +84,7 @@ Message categories public static final byte TERRAIN_MESSAGE_TYPE_CHUNKLOADSTART_SIZE = 14; public static final short TERRAIN_MESSAGE_TYPE_MACROVALUE_SIZE = 310; public static final byte TERRAIN_MESSAGE_TYPE_HEIGHTMAPMODIFICATION_SIZE = 22; - public static final byte TERRAIN_MESSAGE_TYPE_SPAWNPOSITION_SIZE = 10; + public static final byte TERRAIN_MESSAGE_TYPE_SPAWNPOSITION_SIZE = 18; /* Server subcategories */ diff --git a/src/main/java/electrosphere/net/server/ServerConnectionHandler.java b/src/main/java/electrosphere/net/server/ServerConnectionHandler.java index ddd39d81..4627c5a4 100644 --- a/src/main/java/electrosphere/net/server/ServerConnectionHandler.java +++ b/src/main/java/electrosphere/net/server/ServerConnectionHandler.java @@ -167,6 +167,8 @@ public class ServerConnectionHandler implements Runnable { networkParser.addOutgoingMessage(message); } break; + case SPAWNCREATURE: + break; default: networkParser.addOutgoingMessage(message); break; diff --git a/src/main/java/electrosphere/net/server/protocol/EntityProtocol.java b/src/main/java/electrosphere/net/server/protocol/EntityProtocol.java index 63752f05..d764a96b 100644 --- a/src/main/java/electrosphere/net/server/protocol/EntityProtocol.java +++ b/src/main/java/electrosphere/net/server/protocol/EntityProtocol.java @@ -17,10 +17,6 @@ public class EntityProtocol { protected static void handleEntityMessage(ServerConnectionHandler connectionHandler, EntityMessage message){ Entity targetEntity; switch(message.getMessageSubtype()){ - case CREATE: - break; - case DESTROY: - break; case MOVE: targetEntity = Globals.entityManager.getEntityFromId(message.getentityID()); if(targetEntity != null){ @@ -42,6 +38,10 @@ public class EntityProtocol { } break; //ignore stack + case KILL: + case SPAWNCREATURE: + case DESTROY: + case CREATE: case ATTACHENTITYTOENTITY: case SETFACING: case SETPOSITION: diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index d649f55a..17c308eb 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -198,6 +198,10 @@ public class RenderingEngine { ShaderProgram activeProgram; static int outputFramebuffer = 0; + + //used in calculating projection matrix + static float aspectRatio = 1.0f; + static float verticalFOV = 90.0f; public void createOpenglContext(){ @@ -388,10 +392,11 @@ public class RenderingEngine { // Globals.projectionMatrix = new Matrix4f(); Globals.viewMatrix = new Matrix4f(); - float verticalFOV = (float)(Globals.verticalFOV * Math.PI /180.0f); - float aspectRatio = (float)Globals.WINDOW_WIDTH / (float)Globals.WINDOW_HEIGHT; + verticalFOV = (float)(Globals.verticalFOV * Math.PI /180.0f); + //set local aspect ratio and global aspect ratio at the same time + aspectRatio = Globals.aspectRatio = Globals.WINDOW_WIDTH / (float)Globals.WINDOW_HEIGHT; float nearClip = 0.001f; - Globals.projectionMatrix.setPerspective(verticalFOV, aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); + Globals.projectionMatrix.setPerspective(verticalFOV, Globals.aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); Globals.viewMatrix.translation(new Vector3f(0.0f,0.0f,-3.0f)); } @@ -1314,19 +1319,19 @@ public class RenderingEngine { } public static void setFOV(float verticalFOV){ - Globals.verticalFOV = verticalFOV; + RenderingEngine.verticalFOV = verticalFOV; calculateProjectionMatrix(); } public static void setAspectRatio(float aspectRatio){ - Globals.aspectRatio = aspectRatio; + RenderingEngine.aspectRatio = aspectRatio; calculateProjectionMatrix(); } static void calculateProjectionMatrix(){ - float radVerticalFOV = (float)(Globals.verticalFOV * Math.PI /180.0f); + float radVerticalFOV = (float)(RenderingEngine.verticalFOV * Math.PI /180.0f); float nearClip = 0.001f; - Globals.projectionMatrix.setPerspective(radVerticalFOV, Globals.aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); + Globals.projectionMatrix.setPerspective(radVerticalFOV, RenderingEngine.aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); } } diff --git a/src/main/java/electrosphere/renderer/ui/elements/ActorPanel.java b/src/main/java/electrosphere/renderer/ui/elements/ActorPanel.java index cd9f75ab..b85d7745 100644 --- a/src/main/java/electrosphere/renderer/ui/elements/ActorPanel.java +++ b/src/main/java/electrosphere/renderer/ui/elements/ActorPanel.java @@ -22,6 +22,7 @@ import electrosphere.main.Globals; import electrosphere.main.Main; import electrosphere.renderer.Material; import electrosphere.renderer.Model; +import electrosphere.renderer.RenderingEngine; import electrosphere.renderer.actor.Actor; import electrosphere.renderer.framebuffer.Framebuffer; import electrosphere.renderer.framebuffer.FramebufferUtils; @@ -43,6 +44,8 @@ public class ActorPanel implements DrawableElement, DraggableElement { Vector3f actorPosition = new Vector3f(0,0,0); Quaternionf actorRotation = new Quaternionf(); Vector3f actorScale = new Vector3f(1,1,1); + float FOV = 50.0f; + float aspectRatio = 1.9f; Vector3f texPosition = new Vector3f(0,0,0); @@ -60,6 +63,7 @@ public class ActorPanel implements DrawableElement, DraggableElement { this.positionY = y; this.width = width; this.height = height; + this.aspectRatio = (float)width / (float)height; recalculateModelMatrix(); } @@ -70,6 +74,9 @@ public class ActorPanel implements DrawableElement, DraggableElement { elementBuffer.bind(); // Globals.renderingEngine.setViewportSize(width, height); + + RenderingEngine.setFOV(FOV); + RenderingEngine.setAspectRatio(aspectRatio); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); @@ -89,6 +96,9 @@ public class ActorPanel implements DrawableElement, DraggableElement { actor.applyModelMatrix(modelMatrix); actor.draw(true); + RenderingEngine.setFOV(Globals.verticalFOV); + RenderingEngine.setAspectRatio(Globals.aspectRatio); + glDisable(GL_DEPTH_TEST); //this call binds the screen as the "texture" we're rendering to