From 38108454d315ac896f3926e22739c52247ffde11 Mon Sep 17 00:00:00 2001 From: Mipada Date: Thu, 22 Mar 2018 22:48:22 -0700 Subject: [PATCH 1/2] Upgraded to JME3.2 compatability Changed: nifty calls Added: Changes.txt (I didn't fork my local copy so i have to check and make sure I got everything.) --- Changes.txt | 19 ++ ClientMain.java | 454 +++++++++++++++++++++++++++++++ DefaultHUDControl.java | 121 +++++++++ ServerMain.java | 130 +++++++++ UserCommandControl.java | 575 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1299 insertions(+) create mode 100644 Changes.txt create mode 100644 ClientMain.java create mode 100644 DefaultHUDControl.java create mode 100644 ServerMain.java create mode 100644 UserCommandControl.java diff --git a/Changes.txt b/Changes.txt new file mode 100644 index 0000000..d08e014 --- /dev/null +++ b/Changes.txt @@ -0,0 +1,19 @@ +2018-03-11 +Upgraded to JME3.2 +Added: Changes.txt +Changed: nifty calls + +2017-??-?? +Upgraded to JME3.1 +Changed: +Because of the changes below, you must do a Clean and Build Project (Shift F11) +Application from class to interface +LegacyApplication implements Application +SimpleApplication extends LegacyApplication +Libraries: More might be required + +2016-??-?? +Upgraded for JME3.0 +??? + + diff --git a/ClientMain.java b/ClientMain.java new file mode 100644 index 0000000..15f3700 --- /dev/null +++ b/ClientMain.java @@ -0,0 +1,454 @@ +/* + * Copyright (c) 2009-2011 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.monkeyzone; + +import com.jme3.monkeyzone.controls.UserCommandControl; +import com.jme3.app.SimpleApplication; +import com.jme3.bullet.BulletAppState; +import com.jme3.monkeyzone.controls.DefaultHUDControl; +import com.jme3.monkeyzone.controls.UserInputControl; +import com.jme3.monkeyzone.messages.ActionMessage; +import com.jme3.monkeyzone.messages.AutoControlMessage; +import com.jme3.monkeyzone.messages.ChatMessage; +import com.jme3.monkeyzone.messages.ManualControlMessage; +import com.jme3.monkeyzone.messages.ServerAddEntityMessage; +import com.jme3.monkeyzone.messages.ServerAddPlayerMessage; +import com.jme3.monkeyzone.messages.ServerDisableEntityMessage; +import com.jme3.monkeyzone.messages.ServerEffectMessage; +import com.jme3.monkeyzone.messages.ServerEnableEntityMessage; +import com.jme3.monkeyzone.messages.ServerEnterEntityMessage; +import com.jme3.monkeyzone.messages.ServerEntityDataMessage; +import com.jme3.monkeyzone.messages.ServerRemoveEntityMessage; +import com.jme3.monkeyzone.messages.ServerRemovePlayerMessage; +import com.jme3.monkeyzone.messages.StartGameMessage; +import com.jme3.network.Network; +import com.jme3.network.NetworkClient; +import com.jme3.network.physicssync.PhysicsSyncManager; +import com.jme3.network.physicssync.SyncCharacterMessage; +import com.jme3.network.physicssync.SyncRigidBodyMessage; +import com.jme3.niftygui.NiftyJmeDisplay; +import com.jme3.renderer.RenderManager; +import com.jme3.system.AppSettings; +import de.lessvoid.nifty.Nifty; +import de.lessvoid.nifty.controls.dynamic.TextCreator; +import de.lessvoid.nifty.controls.textfield.TextFieldControl; +import de.lessvoid.nifty.elements.Element; +import de.lessvoid.nifty.elements.render.TextRenderer; +import de.lessvoid.nifty.screen.Screen; +import de.lessvoid.nifty.screen.ScreenController; +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * The client Main class, also the screen controller for most parts of the + * login and lobby GUI + * @author normenhansen + */ +public class ClientMain extends SimpleApplication implements ScreenController { + + private static ClientMain app; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + String string = args[i]; + if ("-server".equals(string)) { + ServerMain.main(args); + return; + } + } + AppSettings settings = new AppSettings(true); + settings.setFrameRate(Globals.SCENE_FPS); + settings.setSettingsDialogImage("/Interface/Images/splash-small.jpg"); + settings.setTitle("MonkeyZone"); + Util.registerSerializers(); + Util.setLogLevels(true); + app = new ClientMain(); + settings.setResolution(200 * 4, 200 * 3); + app.setShowSettings(false);//show settings dialog + app.setSettings(settings); + app.setPauseOnLostFocus(false); + app.start(); + } + private WorldManager worldManager; + private PhysicsSyncManager syncManager; + private ClientEffectsManager effectsManager; + private UserCommandControl commandControl; + private Nifty nifty; + private NiftyJmeDisplay niftyDisplay; + private TextRenderer statusText; + private NetworkClient client; + private ClientNetListener listenerManager; + private BulletAppState bulletState; +// private ChaseCamera chaseCam; + + @Override + public void simpleInitApp() { + startNifty(); + client = Network.createClient(); + bulletState = new BulletAppState(); + if (Globals.PHYSICS_THREADED) { + bulletState.setThreadingType(BulletAppState.ThreadingType.PARALLEL); + } + getStateManager().attach(bulletState); + bulletState.getPhysicsSpace().setAccuracy(Globals.PHYSICS_FPS); + if(Globals.PHYSICS_DEBUG){ + //bulletState.getPhysicsSpace().enableDebug(assetManager); + } + inputManager.setCursorVisible(true); + flyCam.setEnabled(false); +// chaseCam = new ChaseCamera(cam, inputManager); +// chaseCam.setSmoothMotion(true); +// chaseCam.setChasingSensitivity(100); +// chaseCam.setTrailingEnabled(true); + + syncManager = new PhysicsSyncManager(app, client); + syncManager.setMaxDelay(Globals.NETWORK_MAX_PHYSICS_DELAY); + syncManager.setMessageTypes(AutoControlMessage.class, + ManualControlMessage.class, + ActionMessage.class, + SyncCharacterMessage.class, + SyncRigidBodyMessage.class, + ServerEntityDataMessage.class, + ServerEnterEntityMessage.class, + ServerAddEntityMessage.class, + ServerAddPlayerMessage.class, + ServerEffectMessage.class, + ServerEnableEntityMessage.class, + ServerDisableEntityMessage.class, + ServerRemoveEntityMessage.class, + ServerRemovePlayerMessage.class); + stateManager.attach(syncManager); + + //ai manager for controlling units + commandControl = new UserCommandControl(nifty.getScreen("default_hud"), inputManager); + //world manager, manages entites and server commands + worldManager = new WorldManager(this, rootNode, commandControl); + //adding/creating controls later attached to user controlled spatial + worldManager.addUserControl(new UserInputControl(inputManager, cam)); + worldManager.addUserControl(commandControl); + worldManager.addUserControl(new DefaultHUDControl(nifty.getScreen("default_hud"))); + stateManager.attach(worldManager); + //effects manager for playing effects + effectsManager = new ClientEffectsManager(); + stateManager.attach(effectsManager); + //register effects manager and world manager with sync manager so that messages can apply their data + syncManager.addObject(-2, effectsManager); + syncManager.addObject(-1, worldManager); + + + listenerManager = new ClientNetListener(this, client, worldManager, effectsManager); + } + + /** + * starts the nifty gui system + */ + private void startNifty() { + guiNode.detachAllChildren(); + guiNode.attachChild(fpsText); + niftyDisplay = new NiftyJmeDisplay(assetManager, + inputManager, + audioRenderer, + guiViewPort); + nifty = niftyDisplay.getNifty(); + try { + nifty.fromXml("Interface/ClientUI.xml", "load_game", this); + } catch (Exception ex) { + ex.printStackTrace(); + } + statusText = nifty.getScreen("load_game").findElementById("layer").findElementById("panel").findElementById("status_text").getRenderer(TextRenderer.class); + guiViewPort.addProcessor(niftyDisplay); + } + + /** + * sets the status text of the main login view, threadsafe + * @param text + */ + public void setStatusText(final String text) { + enqueue(new Callable() { + + public Void call() throws Exception { + statusText.setText(text); + return null; + } + }); + } + + /** + * updates the list of players in the lobby gui, threadsafe + */ + public void updatePlayerData() { + Logger.getLogger(ClientMain.class.getName()).log(Level.INFO, "Updating player data"); + enqueue(new Callable() { + + public Void call() throws Exception { + Screen screen = nifty.getScreen("lobby"); + digger("Screen", screen); + List players = PlayerData.getHumanPlayers(); + //Element panel = screen.findElementById("layer").findElementById("panel").findElementById("players_panel").findElementById("players_list").findElementById("panel"); + Element panel = screen.findElementById("panel").findElementById("players_panel").findElementById("players_list").findElementById("players_list"); + digger("Panel", panel); + //for (Iterator it = new LinkedList(panel.getElements()).iterator(); it.hasNext();) { + for (Iterator it = panel.getChildren().iterator(); it.hasNext();) { + if (!it.hasNext()) System.out.println("it is empty"); + Element element = it.next(); + element.markForRemoval();//disable(); + } + TextCreator labelCreator = new TextCreator("unknown player"); + labelCreator.setStyle("my-listbox-item-style"); + for (Iterator it = players.iterator(); it.hasNext();) { + PlayerData data = it.next(); + Logger.getLogger(ClientMain.class.getName()).log(Level.INFO, "List player {0}", data); + labelCreator.setText(data.getStringData("name")); + labelCreator.create(nifty, screen, panel); + } + return null; + } + }); + } + + //display nifty elements + public void digger(String caption, Object obj){ + System.out.println("Display " + caption); + digger(obj, 0); + System.out.println(); + } + + public void digger(Object obj, int k){ + if (obj == null){ + System.out.println("dis obj is null"); + } + else if (obj instanceof Screen){ + for (int j = 0;j < k;j++){ + System.out.print("*"); + } + Screen screen = (Screen)obj; + System.out.println("Screen ID=" + screen.getScreenId()); + List list = screen.getLayerElements(); + for(int i = 0;i < list.size();i++){ + Object newObj = list.get(i); + digger(newObj, k + 1); + } + } + else if (obj instanceof Element){ + for (int j = 0;j < k;j++){ + System.out.print("*"); + } + Element element = (Element)obj; + System.out.println("Element ID=" + element.getId()); + List list = element.getChildren(); + for(int i = 0;i < list.size();i++){ + Object newObj = list.get(i); + digger(newObj, k + 1); + } + } + else{ + System.out.println("obj=" + obj.getClass()); + } + } + + /** + * add text to chat window, threadsafe + * @param text + */ + public void addChat(final String text) { + enqueue(new Callable() { + + public Void call() throws Exception { + Screen screen = nifty.getScreen("lobby"); + Element panel = screen.findElementById("layer").findElementById("bottom_panel").findElementById("chat_panel").findElementById("chat_list").findElementById("chat_list_panel"); + TextCreator labelCreator = new TextCreator(text); + labelCreator.setStyle("my-listbox-item-style"); + labelCreator.create(nifty, screen, panel); + return null; + } + }); + } + + /** + * gets the text currently entered in the textbox and sends it as a chat message + */ + public void sendChat() { + Logger.getLogger(ClientMain.class.getName()).log(Level.INFO, "Send chat message"); + enqueue(new Callable() { + + public Void call() throws Exception { + Screen screen = nifty.getScreen("lobby"); + TextFieldControl control = screen.findElementById("layer").findElementById("bottom_panel").findElementById("chat_panel").findElementById("chat_bottom_bar").findElementById("chat_text").getControl(TextFieldControl.class); + String text = control.getText(); + sendMessage(text); + control.setText(""); + return null; + } + }); + } + + //FIXME: nifty cannot find sendChat() when sendChat(String text) is existing too + public void sendMessage(String text) { + client.send(new ChatMessage(text)); + } + + /** + * connect to server (called from gui) + */ + public void connect() { + //TODO: not connect when already trying.. + final String userName = nifty.getScreen("load_game").findElementById("layer").findElementById("panel").findElementById("username_text").getControl(TextFieldControl.class).getText(); + if (userName.trim().length() == 0) { + setStatusText("Username invalid"); + return; + } + listenerManager.setName(userName); + statusText.setText("Connecting.."); + try { + client.connectToServer(Globals.DEFAULT_SERVER, Globals.DEFAULT_PORT_TCP, Globals.DEFAULT_PORT_UDP); + client.start(); + } catch (IOException ex) { + setStatusText(ex.getMessage()); + Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex); + } + } + + /** + * brings up the lobby display + */ + public void lobby() { +// chaseCam.setDragToRotate(false); + inputManager.setCursorVisible(true); + nifty.gotoScreen("lobby"); + } + + /** + * send message to start selected game + */ + public void startGame() { + //TODO: map selection + client.send(new StartGameMessage("Scenes/MonkeyZone.j3o")); + } + + /** + * loads a level, basically does everything on a seprate thread except + * updating the UI and attaching the level + * @param name + * @param modelNames + */ + public void loadLevel(final String name, final String[] modelNames) { + final TextRenderer statusText = nifty.getScreen("load_level").findElementById("layer").findElementById("panel").findElementById("status_text").getRenderer(TextRenderer.class); + if (name.equals("null")) { + enqueue(new Callable() { + + public Void call() throws Exception { + worldManager.closeLevel(); + lobby(); + return null; + } + }); + return; + } + new Thread(new Runnable() { + + public void run() { + try { + enqueue(new Callable() { + + public Void call() throws Exception { + nifty.gotoScreen("load_level"); + statusText.setText("Loading Terrain.."); + return null; + } + }).get(); + worldManager.loadLevel(name); + enqueue(new Callable() { + + public Void call() throws Exception { + statusText.setText("Creating NavMesh.."); + return null; + } + }).get(); + worldManager.createNavMesh(); + enqueue(new Callable() { + + public Void call() throws Exception { + statusText.setText("Loading Models.."); + return null; + } + }).get(); + worldManager.preloadModels(modelNames); + enqueue(new Callable() { + + public Void call() throws Exception { + worldManager.attachLevel(); + statusText.setText("Done Loading!"); + nifty.gotoScreen("default_hud"); + inputManager.setCursorVisible(false); +// chaseCam.setDragToRotate(false); + return null; + } + }).get(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + + public void bind(Nifty nifty, Screen screen) { + } + + public void onStartScreen() { + } + + public void onEndScreen() { + } + + @Override + public void simpleUpdate(float tpf) { + } + + @Override + public void simpleRender(RenderManager rm) { + } + + @Override + public void destroy() { + try { + client.close(); + } catch (Exception ex) { + Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex); + } + super.destroy(); + } +} diff --git a/DefaultHUDControl.java b/DefaultHUDControl.java new file mode 100644 index 0000000..2029eca --- /dev/null +++ b/DefaultHUDControl.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2009-2011 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.monkeyzone.controls; + +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.renderer.RenderManager; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Spatial; +import com.jme3.scene.control.Control; +import de.lessvoid.nifty.elements.render.TextRenderer; +import de.lessvoid.nifty.screen.Screen; +import java.io.IOException; + +/** + * Default HUD control, updates UI with spatial data. + * @author normenhansen + */ +public class DefaultHUDControl implements HUDControl { + + protected boolean enabled = true; + protected Spatial spatial; + protected Screen screen; + protected float updateTime = 0.25f; + protected float curTime = 1; + protected TextRenderer hitPoints; + protected TextRenderer speed; + protected TextRenderer vehicle; + + public DefaultHUDControl(Screen screen) { + this.screen = screen; + if (screen == null) { + throw new IllegalStateException("DefaultHUDControl nifty screen null!"); + } + hitPoints = screen.findElementById("layer").findElementById("panel_bottom").findElementById("bottom_panel_left").findElementById("status_text_01").getRenderer(TextRenderer.class); + speed = screen.findElementById("layer").findElementById("panel_bottom").findElementById("bottom_panel_left").findElementById("status_text_02").getRenderer(TextRenderer.class); + vehicle = screen.findElementById("layer").findElementById("panel_bottom").findElementById("bottom_panel_left").findElementById("status_text_03").getRenderer(TextRenderer.class); + } + + public void setSpatial(Spatial spatial) { + if (spatial == null) { + this.spatial = spatial; + return; + } + this.spatial = spatial; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isEnabled() { + return enabled; + } + + public void update(float tpf) { + if (!enabled) { + return; + } + curTime += tpf; + if (curTime > updateTime) { + curTime = 0; + Float hitPoints = (Float) spatial.getUserData("HitPoints"); + Float speed = (Float) spatial.getUserData("Speed"); + if (hitPoints != null) { + this.hitPoints.setText("HP:" + hitPoints); + } else { + this.hitPoints.setText("No HitPoints!"); + } + if (speed != null) { + this.speed.setText("Speed: " + speed); + } else { + this.speed.setText("No HitPoints!"); + } + } + } + + public void render(RenderManager rm, ViewPort vp) { + } + + public Control cloneForSpatial(Spatial spatial) { + throw new UnsupportedOperationException("Not supported."); + } + + public void write(JmeExporter ex) throws IOException { + throw new UnsupportedOperationException("Not supported."); + } + + public void read(JmeImporter im) throws IOException { + throw new UnsupportedOperationException("Not supported."); + } +} diff --git a/ServerMain.java b/ServerMain.java new file mode 100644 index 0000000..d955d23 --- /dev/null +++ b/ServerMain.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2009-2011 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.monkeyzone; + +import com.jme3.app.SimpleApplication; +import com.jme3.bullet.BulletAppState; +import com.jme3.monkeyzone.messages.ActionMessage; +import com.jme3.monkeyzone.messages.AutoControlMessage; +import com.jme3.monkeyzone.messages.ManualControlMessage; +import com.jme3.network.Network; +import com.jme3.network.physicssync.PhysicsSyncManager; +import com.jme3.renderer.RenderManager; +import com.jme3.system.AppSettings; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +//import Uti.*; + +/** + * The server Main class, basically just starts the application with a + * null renderer and then starts the managers. + * @author normenhansen + */ +public class ServerMain extends SimpleApplication { + + private static com.jme3.network.Server server; + private static ServerMain app; + + public static void main(String[] args) { + AppSettings settings = new AppSettings(true); + settings.setFrameRate(Globals.SCENE_FPS); + settings.setRenderer(null); + //FIXME: strange way of setting null audio renderer.. + settings.setAudioRenderer(null); + for (int i = 0; i < args.length; i++) { + String string = args[i]; + if ("-display".equals(string)) { + settings.setRenderer(AppSettings.LWJGL_OPENGL2); + } + } + Util.registerSerializers(); + Util.setLogLevels(true); + app = new ServerMain(); + app.setShowSettings(false); + app.setPauseOnLostFocus(false); + app.setSettings(settings); + app.start(); + } + private WorldManager worldManager; + private ServerGameManager gameManager; + private PhysicsSyncManager syncManager; + private ServerNetListener listenerManager; + private BulletAppState bulletState; + + + @Override + public void simpleInitApp() { + try { + server = Network.createServer(Globals.DEFAULT_PORT_TCP, Globals.DEFAULT_PORT_UDP); + server.start(); + } catch (IOException ex) { + Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, "Cannot start server: {0}", ex); + return; + } + bulletState = new BulletAppState(); + getStateManager().attach(bulletState); + bulletState.getPhysicsSpace().setAccuracy(Globals.PHYSICS_FPS); + //create sync manager + syncManager = new PhysicsSyncManager(app, server); + syncManager.setSyncFrequency(Globals.NETWORK_SYNC_FREQUENCY); + syncManager.setMessageTypes(AutoControlMessage.class, + ActionMessage.class, + ManualControlMessage.class); + stateManager.attach(syncManager); + //cerate world manager + worldManager = new WorldManager(this, rootNode); + stateManager.attach(worldManager); + //register world manager with sync manager so that messages can apply their data + syncManager.addObject(-1, worldManager); + //create server side game manager + gameManager = new ServerGameManager(); + stateManager.attach(gameManager); + listenerManager = new ServerNetListener(this, server, worldManager, gameManager); + System.out.println("Starting server..."); + } + + @Override + public void simpleUpdate(float tpf) { +// worldManager.update(tpf); + } + + @Override + public void simpleRender(RenderManager rm) { + } + + @Override + public void destroy() { + super.destroy(); + server.close(); + } +} diff --git a/UserCommandControl.java b/UserCommandControl.java new file mode 100644 index 0000000..4c282f5 --- /dev/null +++ b/UserCommandControl.java @@ -0,0 +1,575 @@ +/* + * Copyright (c) 2009-2011 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.monkeyzone.controls; + +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.input.InputManager; +import com.jme3.input.KeyInput; +import com.jme3.input.controls.ActionListener; +import com.jme3.input.controls.KeyTrigger; +import com.jme3.math.Vector3f; +import com.jme3.monkeyzone.WorldManager; +import com.jme3.monkeyzone.ai.commands.AttackCommand; +import com.jme3.monkeyzone.ai.Command; +import com.jme3.monkeyzone.ai.commands.FollowCommand; +import com.jme3.monkeyzone.ai.commands.MoveCommand; +import com.jme3.monkeyzone.ai.SphereTrigger; +import com.jme3.monkeyzone.ai.commands.EnterCommand; +import com.jme3.renderer.RenderManager; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Spatial; +import com.jme3.scene.control.Control; +import de.lessvoid.nifty.elements.render.TextRenderer; +import de.lessvoid.nifty.screen.Screen; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Manages the client input and UI for AI players + * @author normenhansen + */ +public class UserCommandControl implements Control, ActionListener { + + protected Screen screen; + protected TextRenderer[] selectionTexts = new TextRenderer[10]; + protected TextRenderer[] commandTexts = new TextRenderer[10]; + protected List> commands = new LinkedList>(); + protected HashMap players = new HashMap(); + protected List selectedEntities = new ArrayList(); + protected InputManager inputManager; + protected HashMap> playerGroups = new HashMap>(); + protected boolean shift = false; + protected SelectionMenu currentSelectionMenu = SelectionMenu.Main; + protected WorldManager world; + protected Spatial userEntity; + protected boolean enabled = true; + + protected enum SelectionMenu { + + Main, + Offensive, + Defensive, + Builder, + NavPoints + } + + public UserCommandControl(Screen screen, InputManager inputManager) { + this(inputManager); + this.screen = screen; + for (int i = 0; i < 10; i++) { + selectionTexts[i] = screen.findElementById("layer").findElementById("panel_bottom").findElementById("bottom_panel_right").findElementById("status_text_0" + i).getRenderer(TextRenderer.class); + commandTexts[i] = screen.findElementById("layer").findElementById("panel_top").findElementById("top_panel_left").findElementById("status_text_0" + i).getRenderer(TextRenderer.class); + } + setSelectionMenu(SelectionMenu.Main); + updateCommandMenu(); + } + + public UserCommandControl(InputManager inputManager) { + this.inputManager = inputManager; + inputManager.addMapping("Client_Key_SHIFT", new KeyTrigger(KeyInput.KEY_RSHIFT), new KeyTrigger(KeyInput.KEY_LSHIFT)); + inputManager.addMapping("Client_Key_0", new KeyTrigger(KeyInput.KEY_0)); + inputManager.addMapping("Client_Key_1", new KeyTrigger(KeyInput.KEY_1)); + inputManager.addMapping("Client_Key_2", new KeyTrigger(KeyInput.KEY_2)); + inputManager.addMapping("Client_Key_3", new KeyTrigger(KeyInput.KEY_3)); + inputManager.addMapping("Client_Key_4", new KeyTrigger(KeyInput.KEY_4)); + inputManager.addMapping("Client_Key_5", new KeyTrigger(KeyInput.KEY_5)); + inputManager.addMapping("Client_Key_6", new KeyTrigger(KeyInput.KEY_6)); + inputManager.addMapping("Client_Key_7", new KeyTrigger(KeyInput.KEY_7)); + inputManager.addMapping("Client_Key_8", new KeyTrigger(KeyInput.KEY_8)); + inputManager.addMapping("Client_Key_9", new KeyTrigger(KeyInput.KEY_9)); + inputManager.addMapping("Client_Key_F10", new KeyTrigger(KeyInput.KEY_F10)); + inputManager.addMapping("Client_Key_F1", new KeyTrigger(KeyInput.KEY_F1)); + inputManager.addMapping("Client_Key_F2", new KeyTrigger(KeyInput.KEY_F2)); + inputManager.addMapping("Client_Key_F3", new KeyTrigger(KeyInput.KEY_F3)); + inputManager.addMapping("Client_Key_F4", new KeyTrigger(KeyInput.KEY_F4)); + inputManager.addMapping("Client_Key_F5", new KeyTrigger(KeyInput.KEY_F5)); + inputManager.addMapping("Client_Key_F6", new KeyTrigger(KeyInput.KEY_F6)); + inputManager.addMapping("Client_Key_F7", new KeyTrigger(KeyInput.KEY_F7)); + inputManager.addMapping("Client_Key_F8", new KeyTrigger(KeyInput.KEY_F8)); + inputManager.addMapping("Client_Key_F9", new KeyTrigger(KeyInput.KEY_F9)); + inputManager.addListener(this, + "Client_Key_SHIFT", + "Client_Key_0", + "Client_Key_1", + "Client_Key_2", + "Client_Key_3", + "Client_Key_4", + "Client_Key_5", + "Client_Key_6", + "Client_Key_7", + "Client_Key_8", + "Client_Key_9", + "Client_Key_F10", + "Client_Key_F1", + "Client_Key_F2", + "Client_Key_F3", + "Client_Key_F4", + "Client_Key_F5", + "Client_Key_F6", + "Client_Key_F7", + "Client_Key_F8", + "Client_Key_F9"); + } + + public void setWorldManager(WorldManager world) { + this.world = world; + } + + /** + * adds a player id with entity to the list of user controlled entities, + * called from WorldManager when a player that belongs to this user has + * entered an entity. + * @param id + * @param entity + */ + public void setPlayerEntity(long id, Spatial entity) { + if (entity == null) { + players.remove(id); + return; + } + players.put(id, entity); + //TODO: apply sphere command type via menu + SphereTrigger sphereTrigger = entity.getControl(SphereTrigger.class); + if (sphereTrigger != null) { + sphereTrigger.setGhostRadius(20); + //adding a command that will be used by the sphere trigger by default + Command command = entity.getControl(CommandControl.class).initializeCommand(new AttackCommand()); + sphereTrigger.setCommand(command); + setSelectionMenu(currentSelectionMenu); + } + } + + /** + * clears the list of user controlled entities + */ + public void clearPlayers() { + players.clear(); + setSelectionMenu(currentSelectionMenu); + } + + /** + * removes a user controlled entity + * @param id + */ + public void removePlayerEntity(long id) { + selectedEntities.remove(players.remove(id)); + setSelectionMenu(currentSelectionMenu); + updateCommandMenu(); + } + + /** + * gets the command queue of a specific player + * @param id + * @return + */ + public CommandControl getCommandQueue(long id) { + if (!players.containsKey(id)) { + return null; + } + return players.get(id).getControl(CommandControl.class); + } + + /** + * gets the SphereTrigger of a specific player + * @param id + * @return + */ + public SphereTrigger getSphereTrigger(long id) { + if (!players.containsKey(id)) { + return null; + } + return players.get(id).getControl(SphereTrigger.class); + } + + /** + * clear the selection list and set entity as selected entity + * @param entity + */ + public void selectEntity(Spatial entity) { + this.selectedEntities.clear(); + this.selectedEntities.add(entity); + updateCommandMenu(); + } + + /** + * set multiple entitis as the list of selected entities + * @param entities + */ + public void selectEntities(List entities) { + this.selectedEntities.clear(); + this.selectedEntities.addAll(entities); + updateCommandMenu(); + } + + /** + * add a single entity to the list of selected entities + * @param entity + */ + public void addSelectEntity(Spatial entity) { + this.selectedEntities.add(entity); + updateCommandMenu(); + } + + /** + * remove a single entity from the list of selected entities + * @param entity + */ + public void removeSelectEntity(Spatial entity) { + this.selectedEntities.remove(entity); + updateCommandMenu(); + } + + public void update(float tpf) { + } + + public void onAction(String name, boolean isPressed, float tpf) { + int key = -1; + int fkey = -1; + if (name.equals("Client_Key_SHIFT")) { + shift = isPressed; + } else if (name.equals("Client_Key_0") && isPressed) { + key = 0; + } else if (name.equals("Client_Key_1") && isPressed) { + key = 1; + } else if (name.equals("Client_Key_2") && isPressed) { + key = 2; + } else if (name.equals("Client_Key_3") && isPressed) { + key = 3; + } else if (name.equals("Client_Key_4") && isPressed) { + key = 4; + } else if (name.equals("Client_Key_5") && isPressed) { + key = 5; + } else if (name.equals("Client_Key_6") && isPressed) { + key = 6; + } else if (name.equals("Client_Key_7") && isPressed) { + key = 7; + } else if (name.equals("Client_Key_8") && isPressed) { + key = 8; + } else if (name.equals("Client_Key_9") && isPressed) { + key = 9; + } else if (name.equals("Client_Key_F10") && isPressed) { + fkey = 10; + } else if (name.equals("Client_Key_F1") && isPressed) { + fkey = 1; + } else if (name.equals("Client_Key_F2") && isPressed) { + fkey = 2; + } else if (name.equals("Client_Key_F3") && isPressed) { + fkey = 3; + } else if (name.equals("Client_Key_F4") && isPressed) { + fkey = 4; + } else if (name.equals("Client_Key_F5") && isPressed) { + fkey = 5; + } else if (name.equals("Client_Key_F6") && isPressed) { + fkey = 6; + } else if (name.equals("Client_Key_F7") && isPressed) { + fkey = 7; + } else if (name.equals("Client_Key_F8") && isPressed) { + fkey = 8; + } else if (name.equals("Client_Key_F9") && isPressed) { + fkey = 9; + } + if (key != -1) { + processSelectionKey(key); + } else if (fkey != -1) { + processCommandKey(fkey); + } + } + + /** + * displays a specific selection menu, compiles list of current entities + * in that menu + * @param key + */ + private void setSelectionMenu(SelectionMenu key) { + currentSelectionMenu = key; + switch (currentSelectionMenu) { + case Main: + selectionTexts[1].setText("1 - Offensive Units"); +// selectionTexts[2].changeText("2 - Defensive Units"); +// selectionTexts[3].changeText("3 - Builder Units"); +// selectionTexts[4].changeText("4 - Nav Points"); + selectionTexts[2].setText(""); + selectionTexts[3].setText(""); + selectionTexts[4].setText(""); + selectionTexts[5].setText(""); + selectionTexts[6].setText(""); + selectionTexts[7].setText(""); + selectionTexts[8].setText(""); + selectionTexts[9].setText(""); + selectionTexts[0].setText(""); + break; + case Offensive: + clearSelectionMenu(); + int i = 0; + for (Iterator> it = players.entrySet().iterator(); it.hasNext();) { + i++; + Entry entry = it.next(); + if (i >= 10) { + i = 0; + if (selectedEntities.contains(entry.getValue())) { + selectionTexts[i].setText(i + " - " + entry.getValue().getName() + " *"); + } else { + selectionTexts[i].setText(i + " - " + entry.getValue().getName()); + } + return; + } else { + if (selectedEntities.contains(entry.getValue())) { + selectionTexts[i].setText(i + " - " + entry.getValue().getName() + " *"); + } else { + selectionTexts[i].setText(i + " - " + entry.getValue().getName()); + } + } + } + break; + } + } + + /** + * clears the selection menu ui + */ + private void clearSelectionMenu() { + for (int i = 0; i < selectionTexts.length; i++) { + TextRenderer textRenderer = selectionTexts[i]; + textRenderer.setText(""); + } + } + + /** + * processes selection key (1-0) being pressed + * @param key + */ + private void processSelectionKey(int key) { + switch (currentSelectionMenu) { + case Main: + switch (key) { + case 1: + setSelectionMenu(SelectionMenu.Offensive); + break; +// case 2: +// setSelectionMenu(SelectionMenu.Defensive); +// break; +// case 3: +// setSelectionMenu(SelectionMenu.Builder); +// break; +// case 4: +// setSelectionMenu(SelectionMenu.NavPoints); +// break; + } + break; + case Offensive: + selectUnit(currentSelectionMenu, key, shift); + if (!shift) { + setSelectionMenu(SelectionMenu.Main); + } + break; + } + } + + /** + * select a specific unit from the selection menu + * @param key + * @param add + */ + private void selectUnit(SelectionMenu menu, int key, boolean add) { + //TODO: filter for menu + int i = 0; + for (Iterator> it = players.entrySet().iterator(); it.hasNext();) { + i++; + Entry entry = it.next(); + if (i == key) { + if (selectedEntities.contains(entry.getValue())) { + removeSelectEntity(entry.getValue()); + } else { + if (add) { + addSelectEntity(entry.getValue()); + } else { + selectEntity(entry.getValue()); + } + } + } + } + //update menu + setSelectionMenu(currentSelectionMenu); + } + + /** + * updates the command menu based on the currently selected entities + */ + private void updateCommandMenu() { + commands.clear(); + if (selectedEntities.size() > 0) { + commands.add(MoveCommand.class); + commands.add(FollowCommand.class); + commands.add(EnterCommand.class); + commands.add(AttackCommand.class); + } + clearCommandMenu(); + int i = 0; + for (Iterator> it = commands.iterator(); it.hasNext();) { + i++; + try { + Class class1 = it.next(); + commandTexts[i].setText("F" + i + " - " + class1.newInstance().getName() + " "); + } catch (InstantiationException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } + if (i > 10) { + return; + } + } + } + + /** + * clears the command menu UI + */ + private void clearCommandMenu() { + for (int i = 0; i < commandTexts.length; i++) { + TextRenderer textRenderer = commandTexts[i]; + textRenderer.setText(""); + } + } + + /** + * processes a command key + * @param key + */ + private void processCommandKey(int key) { + if (userEntity == null) { + return; + } + Vector3f location = new Vector3f(); + Spatial spat = world.doRayTest(userEntity, 100, location); + if (spat != null) { + doCommand(key - 1, spat); + } else { + doCommand(key - 1, location); + } + } + + /** + * issues a command with a spatial target to the selected entities + * @param command + * @param spatial + */ + private void doCommand(int command, Spatial spatial) { + for (Iterator it = selectedEntities.iterator(); it.hasNext();) { + Spatial spatial1 = it.next(); + CommandControl commandControl = spatial1.getControl(CommandControl.class); + if (commandControl == null) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.WARNING, "Cannot apply command"); + continue; + } + try { + Command commandInst = commands.get(command).newInstance(); + commandInst.setPriority(10); + commandControl.clearCommands(); + commandControl.initializeCommand(commandInst); + Command.TargetResult info = commandInst.setTargetEntity(spatial); + if (info == Command.TargetResult.Accept || info == Command.TargetResult.AcceptEnemy || info == Command.TargetResult.AcceptFriendly) { + commandControl.addCommand(commandInst); + } + } catch (InstantiationException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + /** + * issues a command with a location target to the selected entities + * @param command + * @param location + */ + private void doCommand(int command, Vector3f location) { + for (Iterator it = selectedEntities.iterator(); it.hasNext();) { + Spatial spatial1 = it.next(); + CommandControl commandControl = spatial1.getControl(CommandControl.class); + if (commandControl == null) { + return; + } + try { + Command commandInst = commands.get(command).newInstance(); + commandInst.setPriority(10); + commandControl.clearCommands(); + commandControl.initializeCommand(commandInst); + Command.TargetResult info = commandInst.setTargetLocation(location); + if (info == Command.TargetResult.Accept || info == Command.TargetResult.AcceptEnemy || info == Command.TargetResult.AcceptFriendly) { + commandControl.addCommand(commandInst); + } + } catch (InstantiationException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + Logger.getLogger(UserCommandControl.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + + public void setSpatial(Spatial spatial) { + userEntity = spatial; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isEnabled() { + return enabled; + } + + public void render(RenderManager rm, ViewPort vp) { + } + + public Control cloneForSpatial(Spatial spatial) { + throw new UnsupportedOperationException("Not supported."); + } + + public void write(JmeExporter ex) throws IOException { + throw new UnsupportedOperationException("Not supported."); + } + + public void read(JmeImporter im) throws IOException { + throw new UnsupportedOperationException("Not supported."); + } + +} From 31d42deb9e34e06151a7ab03f60ddef9f763e1a4 Mon Sep 17 00:00:00 2001 From: Mipada Date: Fri, 23 Mar 2018 22:15:39 -0700 Subject: [PATCH 2/2] Took out debug stuff --- ClientMain.java | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/ClientMain.java b/ClientMain.java index 15f3700..d531df2 100644 --- a/ClientMain.java +++ b/ClientMain.java @@ -216,11 +216,9 @@ public void updatePlayerData() { public Void call() throws Exception { Screen screen = nifty.getScreen("lobby"); - digger("Screen", screen); List players = PlayerData.getHumanPlayers(); //Element panel = screen.findElementById("layer").findElementById("panel").findElementById("players_panel").findElementById("players_list").findElementById("panel"); Element panel = screen.findElementById("panel").findElementById("players_panel").findElementById("players_list").findElementById("players_list"); - digger("Panel", panel); //for (Iterator it = new LinkedList(panel.getElements()).iterator(); it.hasNext();) { for (Iterator it = panel.getChildren().iterator(); it.hasNext();) { if (!it.hasNext()) System.out.println("it is empty"); @@ -247,39 +245,6 @@ public void digger(String caption, Object obj){ System.out.println(); } - public void digger(Object obj, int k){ - if (obj == null){ - System.out.println("dis obj is null"); - } - else if (obj instanceof Screen){ - for (int j = 0;j < k;j++){ - System.out.print("*"); - } - Screen screen = (Screen)obj; - System.out.println("Screen ID=" + screen.getScreenId()); - List list = screen.getLayerElements(); - for(int i = 0;i < list.size();i++){ - Object newObj = list.get(i); - digger(newObj, k + 1); - } - } - else if (obj instanceof Element){ - for (int j = 0;j < k;j++){ - System.out.print("*"); - } - Element element = (Element)obj; - System.out.println("Element ID=" + element.getId()); - List list = element.getChildren(); - for(int i = 0;i < list.size();i++){ - Object newObj = list.get(i); - digger(newObj, k + 1); - } - } - else{ - System.out.println("obj=" + obj.getClass()); - } - } - /** * add text to chat window, threadsafe * @param text