package electrosphere.engine; import electrosphere.collision.dispatch.CollisionObject; import electrosphere.controls.ControlHandler; import electrosphere.entity.CameraEntityUtils; import electrosphere.entity.Entity; import electrosphere.entity.EntityUtils; import electrosphere.game.collision.CollisionEngine; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.game.client.cells.DrawCellManager; import electrosphere.game.client.player.ClientPlayerData; import electrosphere.game.client.terrain.manager.ClientTerrainManager; import electrosphere.game.client.world.ClientWorldData; import electrosphere.game.collision.CommonWorldData; import electrosphere.game.state.MacroSimulation; import electrosphere.game.server.terrain.manager.ServerTerrainManager; import electrosphere.game.server.world.ServerWorldData; import electrosphere.entity.types.attach.AttachUtils; import electrosphere.entity.types.particle.ParticleUtils; import electrosphere.entity.types.collision.CollisionObjUtils; import electrosphere.entity.types.foliage.FoliageUtils; import electrosphere.entity.types.structure.StructureUtils; import electrosphere.game.collision.PhysicsUtils; import electrosphere.game.collision.collidable.Collidable; import electrosphere.game.server.ai.creature.MindlessAttacker; import electrosphere.game.server.effects.ParticleEffects; import electrosphere.game.server.terrain.models.TerrainModification; import electrosphere.game.server.town.Town; import electrosphere.game.server.world.MacroData; import electrosphere.game.server.world.datacell.DataCellManager; import electrosphere.game.state.MicroSimulation; import electrosphere.logger.LoggerInterface; import electrosphere.main.Globals; import static electrosphere.main.Globals.loadingBox; import electrosphere.menu.MenuUtils; import electrosphere.net.NetUtils; import electrosphere.net.client.ClientNetworking; import electrosphere.net.server.Server; import electrosphere.renderer.ActorUtils; import electrosphere.renderer.Model; import electrosphere.renderer.RenderUtils; import electrosphere.renderer.assetmanager.AssetDataStrings; import java.util.Random; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import org.joml.Quaternionf; import org.joml.Vector2i; import org.joml.Vector3d; import org.joml.Vector3f; /** * * @author amaterasu */ public class LoadingThread extends Thread { public static final int LOAD_TITLE_MENU = 0; public static final int LOAD_MAIN_GAME = 1; public static final int LOAD_ARENA = 2; int threadType; Semaphore lock; public LoadingThread(int type){ threadType = type; lock = new Semaphore(1); } @Override public void run(){ lock.acquireUninterruptibly(); switch(threadType){ case LOAD_TITLE_MENU: Globals.currentMenu = MenuUtils.createTitleMenu(); loadingBox.setVisible(false); MenuUtils.makeMenuDrawable(Globals.currentMenu); break; case LOAD_MAIN_GAME: loadingBox.setVisible(true); //disable menu input Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.NO_INPUT); //initialize the terrain manager (server only) if(Globals.RUN_SERVER){ initServerGameTerrainManager(); } LoggerInterface.loggerEngine.INFO("run server: " + Globals.RUN_SERVER + " run client: " + Globals.RUN_CLIENT); //init the data of the world if(Globals.RUN_SERVER){ initServerGameWorldData(); createServerWorld(); initDataCellManager(); } //initialize the server thread (server only) if(Globals.RUN_SERVER){ initServerThread(); } //collision engine initCollisionEngine(Globals.RUN_SERVER); //initialize the "virtual" objects simulation initMacroSimulation(); //initialize the "real" objects simulation initMicroSimulation(); //initialize the client thread (client) if(Globals.RUN_CLIENT){ initClientThread(); stallForClientPlayerData(); } //init client terrain manager initClientTerrainManager(); //initialize the cell manager (client) initDrawCellManager(); //initialize the basic graphical entities of the world (skybox, camera) initWorldBaseGraphicalEntities(); while(Globals.clientConnection.getClientProtocol().isLoading()){ try { TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException ex) { } } //hide cursor Globals.controlHandler.hideMouse(); loadingBox.setVisible(false); RenderUtils.recaptureScreen(); Globals.RENDER_FLAG_RENDER_SHADOW_MAP = true; Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT = true; Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER = true; Globals.RENDER_FLAG_RENDER_UI = true; Globals.RENDER_FLAG_RENDER_BLACK_BACKGROUND = false; LoggerInterface.loggerEngine.INFO("Finished loading"); Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.MAIN_GAME); break; case LOAD_ARENA: loadingBox.setVisible(true); //disable menu input Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.NO_INPUT); //init server arena terrain manager separately initServerArenaTerrainManager(); //init the data of the world initServerArenaWorldData(); //init data cell manager initDataCellManager(); //initialize the server thread (server only) if(Globals.RUN_SERVER){ initServerThread(); } //collision engine initCollisionEngine(Globals.RUN_SERVER); //initialize the "virtual" objects simulation // initMacroSimulation(); //initialize the "real" objects simulation initMicroSimulation(); //initialize the client thread (client) if(Globals.RUN_CLIENT){ initClientThread(); stallForClientPlayerData(); } //init client terrain manager initClientTerrainManager(); //initialize the cell manager (client) initDrawCellManager(); //initialize the basic graphical entities of the world (skybox, camera) initWorldBaseGraphicalEntities(); creatingRandomEntities(); while(Globals.clientConnection.getClientProtocol().isLoading()){ try { TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException ex) { } } //hide cursor Globals.controlHandler.hideMouse(); loadingBox.setVisible(false); RenderUtils.recaptureScreen(); Globals.RENDER_FLAG_RENDER_SHADOW_MAP = true; Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT = true; Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER = true; Globals.RENDER_FLAG_RENDER_UI = true; Globals.RENDER_FLAG_RENDER_BLACK_BACKGROUND = false; LoggerInterface.loggerEngine.INFO("Finished loading"); Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.MAIN_GAME); break; } lock.release(); } public boolean isDone(){ boolean rVal = lock.tryAcquire(); if(rVal == true){ lock.release(); } return rVal; } static void initServerGameTerrainManager(){ /* Actually initialize the terrain manager */ float randomDampener = 0.0f; //0.25f; Globals.serverTerrainManager = new ServerTerrainManager(2000,50,100,randomDampener,0); if(Globals.RUN_SERVER){ if(Globals.userSettings.gameplayGenerateWorld()){ Globals.serverTerrainManager.generate(); Globals.serverTerrainManager.save(); } else { Globals.serverTerrainManager.load(); } } /* Set spawn point */ int playerStartX = 0; int playerStartY = 0; int discreteSize = Globals.serverTerrainManager.getWorldDiscreteSize(); int chunkSize = Globals.serverTerrainManager.getChunkWidth(); boolean found = false; for(int x = 0; x < discreteSize; x++){ for(int y = 0; y < discreteSize; y++){ if(Globals.serverTerrainManager.getDiscreteValue(x, y)>1800){ playerStartX = x; playerStartY = y; found = true; } if(found){ break; } } if(found){ break; } } // Globals.spawnPoint = new Vector3f(playerStartX * chunkSize, Globals.serverTerrainManager.getHeightAtPosition(playerStartX * chunkSize,playerStartY * chunkSize), playerStartY * chunkSize); } static void initServerArenaTerrainManager(){ Globals.serverTerrainManager = ServerTerrainManager.constructArenaTerrainManager(); } static void initClientTerrainManager(){ while(!Globals.clientConnection.getClientProtocol().hasReceivedWorld()){ try { TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException ex) { } } Globals.clientTerrainManager = new ClientTerrainManager(Globals.clientWorldData); } static void initServerArenaWorldData(){ Globals.serverWorldData = ServerWorldData.createArenaWorld(); Globals.spawnPoint = new Vector3f(1,3,1); Globals.serverTerrainManager.getChunk(0, 0).addModification(new TerrainModification(0,0,5,5,5)); } static void createServerWorld(){ // Vector2i townLoc = Town.findValidTownLocation(); // if(townLoc != null){ // int chunkSize = Globals.serverTerrainManager.getChunkWidth(); // Globals.spawnPoint = new Vector3f(townLoc.x * chunkSize, (float)Globals.serverTerrainManager.getHeightAtPosition(townLoc.x * chunkSize,townLoc.y * chunkSize), townLoc.y * chunkSize); //// System.out.println("Setting spawn point @ " + Globals.spawnPoint); //// Town.createTown(townLoc.x,townLoc.y); // } } static void initServerGameWorldData(){ Globals.serverWorldData = ServerWorldData.createGameWorld(Globals.serverTerrainManager); } static void initCollisionEngine(boolean FLAG_INIT_SERVER){ if(FLAG_INIT_SERVER){ Globals.commonWorldData = new CommonWorldData(Globals.serverWorldData, Globals.serverTerrainManager); } else { Globals.commonWorldData = new CommonWorldData(Globals.clientWorldData, Globals.clientTerrainManager); } } static void initDataCellManager(){ Globals.dataCellManager = new DataCellManager(Globals.serverWorldData); } static void initServerThread(){ //start server networking if(Globals.RUN_SERVER){ Globals.server = new Server(NetUtils.getPort()); Globals.serverThread = new Thread(Globals.server); Globals.serverThread.start(); } } static void initClientThread(){ //start client networking Thread clientThread = null; if(Globals.RUN_CLIENT){ Globals.clientConnection = new ClientNetworking(NetUtils.getAddress(),NetUtils.getPort()); clientThread = new Thread(Globals.clientConnection); clientThread.start(); } } static void stallForClientPlayerData(){ while(!Globals.clientPlayerData.hasLoaded()){ try { TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException ex) { ex.printStackTrace(); } } } static void initDrawCellManager(){ Globals.drawCellManager = new DrawCellManager( Globals.clientWorldData, Globals.clientTerrainManager, Globals.clientPlayerData.getWorldPositionX(), Globals.clientPlayerData.getWorldPositionY() // Globals.terrainManager.getDynamicInterpolationRatio(), // Globals.terrainManager.getRandomDampener() ); while(Globals.drawCellManager.containsInvalidCell()){ // Globals.drawCellManager.updateInvalidCell(); try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException ex) { } // System.out.println("invalid cell"); } while(Globals.drawCellManager.containsUndrawableCell()){ // Globals.drawCellManager.makeCellDrawable(); try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException ex) { } // System.out.println("undrawable"); } while(Globals.drawCellManager.containsPhysicsNeedingCell()){ try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException ex) { } } } static void initWorldBaseGraphicalEntities(){ /* Skybox */ Model skyboxModel = Globals.assetManager.fetchModel(AssetDataStrings.ASSET_STRING_SKYBOX_BASIC); Globals.skybox = EntityUtils.spawnDrawableEntity(AssetDataStrings.ASSET_STRING_SKYBOX_BASIC); Globals.skyboxColors.add(new Vector3f(100,150,200)); Globals.skyboxColors.add(new Vector3f(100,150,200)); Globals.skyboxColors.add(new Vector3f(50,100,150)); Globals.skyboxColors.add(new Vector3f(50,100,150)); Globals.skyboxColors.add(new Vector3f(100,150,200)); Globals.skyboxColors.add(new Vector3f(100,150,200)); Globals.skyboxColors.add(new Vector3f(50,100,150)); Globals.skyboxColors.add(new Vector3f(50,100,150)); /* Player Camera */ Globals.playerCamera = CameraEntityUtils.spawnBasicCameraEntity(new Vector3f(1,0,1), new Vector3f(0,0,0)); } static void initMacroSimulation(){ Globals.macroData = MacroData.generateWorld(0); // Globals.macroData.describeWorld(); Globals.macroSimulation = new MacroSimulation(); Globals.macroSimulation.simulate(); Town startTown = Globals.macroData.getTowns().get(0); Vector2i firstPos = startTown.getPositions().get(0); double startX = firstPos.x * Globals.serverTerrainManager.getChunkWidth(); double startZ = firstPos.y * Globals.serverTerrainManager.getChunkWidth(); Globals.spawnPoint.set((float)startX,(float)Globals.commonWorldData.getElevationAtPoint(new Vector3d(startX,0,startZ)),(float)startZ); } static void initMicroSimulation(){ Globals.microSimulation = new MicroSimulation(); } static void creatingRandomEntities(){ // String unitCubeModelPath = Globals.assetManager.registerModel(ModelUtils.createUnitCube()); // Entity unitCube = EntityUtils.spawnDrawableEntity(unitCubeModelPath); // EntityUtils.getEntityPosition(unitCube).set(10,2,10); // String goundPlaneModelPath = "Models/groundplanemassiveuv.fbx"; // Entity groundPlane = EntityUtils.spawnDrawableEntity(goundPlaneModelPath); // EntityUtils.getEntityPosition(groundPlane).set(10f,2f,10f); // EntityUtils.getEntityRotation(groundPlane).rotateAxis((float)Math.PI/2, new Vector3f(1,0,0)); // EntityUtils.getEntityScale(groundPlane).set(5); // String unitsphereModelPath = "Models/unitsphere.fbx"; // Entity unitsphere = EntityUtils.spawnDrawableEntity(unitsphereModelPath); // EntityUtils.getEntityPosition(unitsphere).set(10f,2f,10f); // EntityUtils.getEntityScale(unitsphere).set(1); // String smallCubePath = "Models/SmallCube.fbx"; // Entity originCube = EntityUtils.spawnDrawableEntity(smallCubePath); // EntityUtils.getEntityPosition(originCube).set(0, 0, 0); // // originCube = EntityUtils.spawnDrawableEntity(smallCubePath); // EntityUtils.getEntityPosition(originCube).set(1, 0, 0); // // originCube = EntityUtils.spawnDrawableEntity(smallCubePath); // EntityUtils.getEntityPosition(originCube).set(0, 0, 1); // Entity font = FontUtils.makeFont(7, 1); // EntityUtils.getEntityPosition(font).set(new Vector3f(0.2f,0.2f,0.0f)); // for(int i = 0; i < 10; i++){ // Random rand = new Random(); // Entity creature = CreatureUtils.spawnBasicCreature(0, 0.01f, 0.01f); // EntityUtils.getEntityPosition(creature).set(rand.nextFloat() * 10, rand.nextFloat() * 10, rand.nextFloat() * 10); // EntityUtils.getEntityScale(creature).set(0.01f); // } //trees \:D/ // for(int i = 0; i < 10; i++){ // Random rand = new Random(); // String treePath = "Models/tree1.fbx"; // Entity tree = EntityUtils.spawnDrawableEntity(treePath); // EntityUtils.getPosition(tree).set(rand.nextFloat() * 150 + 10, 0, rand.nextFloat() * 150 + 10); //// EntityUtils.getEntityRotation(tree).rotateAxis((float)-Math.PI/2.0f, new Vector3f(1,0,0)); // } // for(int i = 0; i < 250; i++){ // Random rand = new Random(); // String treePath = "Models/falloak1.fbx"; // Entity tree = EntityUtils.spawnDrawableEntity(treePath); // EntityUtils.getPosition(tree).set(rand.nextFloat() * 105 + 1, 0, rand.nextFloat() * 105 + 1); // EntityUtils.getRotation(tree).rotateLocalX(-(float)Math.PI/2.0f).rotateZ(rand.nextFloat()); //// EntityUtils.getEntityRotation(tree).rotateAxis((float)-Math.PI/2.0f, new Vector3f(1,0,0)); // } Random rand = new Random(); for(int i = 0; i < 1000; i++){ String wheatPath = "Models/wheat2.fbx"; Entity wheatStalk = EntityUtils.spawnDrawableEntity(wheatPath); EntityUtils.getPosition(wheatStalk).set(rand.nextFloat() * 20, 0, rand.nextFloat() * 20); EntityUtils.getRotation(wheatStalk).rotateLocalX(-(float)Math.PI/2.0f); EntityUtils.getScale(wheatStalk).set(1, 1, 2); } // String buildingPath = "Models/building1.fbx"; // Entity building = EntityUtils.spawnDrawableEntity(buildingPath); // EntityUtils.getPosition(building).set(5,1.2f,5); // EntityUtils.getScale(building).set(0.5f); // EntityUtils.getRotation(building).rotateLocalY((float)(Math.PI)); // ActorUtils.applyBlenderTransformer(building); //spawn evil goblin // Entity goblin = CreatureUtils.spawnBasicCreature("Goblin"); // CollisionObjUtils.positionCharacter(goblin, new Vector3f(30, 0, 30)); // EntityUtils.getScale(goblin).set(0.005f); // //give evil goblin sword // Entity goblinSword = ItemUtils.spawnBasicItem("Katana"); // AttachUtils.attachEntityToEntityAtBone(goblin, goblinSword, "Bone.031"); // //attach ai to evil goblin // MindlessAttacker.attachToCreature(goblin); Entity fallOak = FoliageUtils.spawnBasicFoliage("FallOak1"); EntityUtils.getPosition(fallOak).set(1,0,3); Entity spark = ParticleUtils.spawnBillboardParticle("Textures/animetree1leaves1.png", 150000, new Vector3f(0,0,0), 0, 0); EntityUtils.getPosition(spark).set(new Vector3f(3,3,3)); EntityUtils.getScale(spark).mul(1f); // CollisionObjUtils.positionCharacter(fallOak, new Vector3f(1, 0, 3)); // // // Entity testHomie = CreatureUtils.spawnBasicCreature("Human"); // EntityUtils.getScale(testHomie).set(0.005f); // CreatureUtils.positionCharacter(testHomie, new Vector3f(10,1,10)); // // Entity sword = ItemUtils.spawnBasicItem("Katana"); // AttachUtils.attachEntityToEntityAtBone(testHomie, sword, "Bone.020"); // CollisionObjUtils.spawnCollisionPlane(new Vector3f(1,1,1), new Vector3f(8,2,10), new Quaternionf()); // .rotateLocalX(0.75f) // CollisionObjUtils.spawnCollisionCube(new Vector3f(1,1,1), new Vector3f(10,1,10), new Quaternionf()); // CreatureUtils.positionCharacter(Globals.playerCharacter, new Vector3f(10,3,10)); // StructureUtils.spawnBasicStructure("building1", new Vector3f(10,2.4f,15), new Quaternionf().rotateLocalY((float)Math.PI)); } }