From 3f9f6e7928f9798dec8ffbba3fd67d46335bd79c Mon Sep 17 00:00:00 2001 From: David Date: Fri, 11 Dec 2020 15:10:55 -0600 Subject: [PATCH] Fixed inputMultiplexer bug; fixed logic to create working AI --- .../src/com/benberi/cadesim/GameContext.java | 2 + .../scene/impl/battle/SeaBattleScene.java | 7 - .../game/scene/impl/connect/ConnectScene.java | 3 +- .../server/codec/ServerChannelHandler.java | 3 +- .../codec/packet/ServerPacketManager.java | 2 +- .../cadesim/server/model/player/NPC_Type.java | 8 ++ .../server/model/player/NPC_Type1.java | 20 ++- .../server/model/player/NPC_Type2.java | 23 ++-- .../server/model/player/NPC_Type3.java | 20 ++- .../server/model/player/NPC_Type4.java | 22 +++- .../cadesim/server/model/player/Player.java | 16 ++- .../server/model/player/PlayerManager.java | 122 +++++++++++++++++- 12 files changed, 203 insertions(+), 45 deletions(-) create mode 100644 server/src/com/benberi/cadesim/server/model/player/NPC_Type.java diff --git a/client-core/src/com/benberi/cadesim/GameContext.java b/client-core/src/com/benberi/cadesim/GameContext.java index e4af60f..d350ed5 100644 --- a/client-core/src/com/benberi/cadesim/GameContext.java +++ b/client-core/src/com/benberi/cadesim/GameContext.java @@ -652,9 +652,11 @@ public void exitMapEditor() { */ public void handleServersideDisconnect() { if(getServerResponse()) { + inputMultiplexer.clear(); getServerChannel().disconnect(); System.out.println("Login error; handling response."); }else { + inputMultiplexer.clear(); setConnected(false); setIsInLobby(true); getServerChannel().disconnect(); diff --git a/client-core/src/com/benberi/cadesim/game/scene/impl/battle/SeaBattleScene.java b/client-core/src/com/benberi/cadesim/game/scene/impl/battle/SeaBattleScene.java index 36593f1..8062fff 100644 --- a/client-core/src/com/benberi/cadesim/game/scene/impl/battle/SeaBattleScene.java +++ b/client-core/src/com/benberi/cadesim/game/scene/impl/battle/SeaBattleScene.java @@ -169,13 +169,6 @@ public void createMap(int[][] tiles) { selectedIsland = islandList.get(context.getIslandId()); } - /** - * set up the input processor for the chat and game - */ - public void setup() { - - } - private void recountVessels() { vesselsCountWithCurrentPhase = context.getEntities().countVesselsByPhase(currentPhase); vesselsCountNonSinking = context.getEntities().countNonSinking(); diff --git a/client-core/src/com/benberi/cadesim/game/scene/impl/connect/ConnectScene.java b/client-core/src/com/benberi/cadesim/game/scene/impl/connect/ConnectScene.java index 14919ee..b5de49a 100644 --- a/client-core/src/com/benberi/cadesim/game/scene/impl/connect/ConnectScene.java +++ b/client-core/src/com/benberi/cadesim/game/scene/impl/connect/ConnectScene.java @@ -522,6 +522,7 @@ public void splitRoomInfo() { port_numbers.add(temp_room_info[j].replace("\\", "")); } else { + String[] print = temp_room_info[j].split(";"); server_codes.add(print[0]); room_names.add(print[1]); @@ -915,8 +916,6 @@ public void readURLServerConfig() { if(line.isEmpty() || line.startsWith("#")) { continue; } - //remove spaces - line = line.replaceAll("\\s", ""); //check getdown.txt for url if(line.startsWith("server.room_locations=")) { room_info = line.split("=")[1]; diff --git a/server/src/com/benberi/cadesim/server/codec/ServerChannelHandler.java b/server/src/com/benberi/cadesim/server/codec/ServerChannelHandler.java index 980939f..bfd20cd 100644 --- a/server/src/com/benberi/cadesim/server/codec/ServerChannelHandler.java +++ b/server/src/com/benberi/cadesim/server/codec/ServerChannelHandler.java @@ -26,7 +26,8 @@ public ServerChannelHandler(ServerContext ctx) { @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { try { - context.getPlayerManager().registerPlayer(ctx.channel()); + Player p = new Player(context, ctx.channel()); + context.getPlayerManager().registerPlayer(ctx.channel(), p); } catch (Exception e) { ServerContext.log("Channel register error: " + e.getMessage()); e.printStackTrace(); diff --git a/server/src/com/benberi/cadesim/server/codec/packet/ServerPacketManager.java b/server/src/com/benberi/cadesim/server/codec/packet/ServerPacketManager.java index 17718f7..f88b117 100644 --- a/server/src/com/benberi/cadesim/server/codec/packet/ServerPacketManager.java +++ b/server/src/com/benberi/cadesim/server/codec/packet/ServerPacketManager.java @@ -44,7 +44,7 @@ public ServerPacketManager(ServerContext context) { */ public void queuePackets() { for (Player p : context.getPlayerManager().getPlayers()) { - p.getPackets().queueIncomingPackets(); + p.getPackets().queueIncomingPackets(); } } diff --git a/server/src/com/benberi/cadesim/server/model/player/NPC_Type.java b/server/src/com/benberi/cadesim/server/model/player/NPC_Type.java new file mode 100644 index 0000000..e900131 --- /dev/null +++ b/server/src/com/benberi/cadesim/server/model/player/NPC_Type.java @@ -0,0 +1,8 @@ +package com.benberi.cadesim.server.model.player; + +public enum NPC_Type { + TYPE1(), + TYPE2(), + TYPE3(), + TYPE4(); +} diff --git a/server/src/com/benberi/cadesim/server/model/player/NPC_Type1.java b/server/src/com/benberi/cadesim/server/model/player/NPC_Type1.java index 7fbbaf6..0e213d2 100644 --- a/server/src/com/benberi/cadesim/server/model/player/NPC_Type1.java +++ b/server/src/com/benberi/cadesim/server/model/player/NPC_Type1.java @@ -1,6 +1,9 @@ package com.benberi.cadesim.server.model.player; import com.benberi.cadesim.server.ServerContext; +import com.benberi.cadesim.server.model.player.move.MoveType; + +import io.netty.channel.Channel; /* * AI Logic - moving type only (like green ships in flotilla with no cbs) @@ -10,10 +13,12 @@ public class NPC_Type1 extends Player { @SuppressWarnings("unused") private ServerContext context; - public NPC_Type1(ServerContext ctx) { - this.context = ctx; - setBot(true); - set(-1, -1); // not spawned + public NPC_Type1(ServerContext ctx, Channel c) { + super(ctx,c); + context = ctx; + super.setBot(true); + super.set(-1, -1); // not spawned + super.setType(NPC_Type.TYPE1); } @Override @@ -23,6 +28,11 @@ public void calculateRoute() { @Override public void performLogic() { - + getMoves().setMove(0, MoveType.FORWARD); + } + + @Override + public NPC_Type getType() { + return type; } } diff --git a/server/src/com/benberi/cadesim/server/model/player/NPC_Type2.java b/server/src/com/benberi/cadesim/server/model/player/NPC_Type2.java index e502fc6..56004f5 100644 --- a/server/src/com/benberi/cadesim/server/model/player/NPC_Type2.java +++ b/server/src/com/benberi/cadesim/server/model/player/NPC_Type2.java @@ -1,6 +1,9 @@ package com.benberi.cadesim.server.model.player; import com.benberi.cadesim.server.ServerContext; +import com.benberi.cadesim.server.model.player.move.MoveType; + +import io.netty.channel.Channel; /* * AI Logic - flag grabbing type (collect available flag points) @@ -10,19 +13,21 @@ public class NPC_Type2 extends Player { @SuppressWarnings("unused") private ServerContext context; - public NPC_Type2(ServerContext ctx) { - this.context = ctx; - setBot(true); - set(-1, -1); // not spawned + public NPC_Type2(ServerContext ctx, Channel c) { + super(ctx,c); + context = ctx; + super.setBot(true); + super.set(-1, -1); // not spawned + super.setType(NPC_Type.TYPE2); } @Override - public void calculateRoute() { - + public void performLogic() { + this.getMoves().setMove(0, MoveType.FORWARD); } - @Override - public void performLogic() { - + @Override + public NPC_Type getType() { + return type; } } diff --git a/server/src/com/benberi/cadesim/server/model/player/NPC_Type3.java b/server/src/com/benberi/cadesim/server/model/player/NPC_Type3.java index c268914..23cebcf 100644 --- a/server/src/com/benberi/cadesim/server/model/player/NPC_Type3.java +++ b/server/src/com/benberi/cadesim/server/model/player/NPC_Type3.java @@ -1,6 +1,9 @@ package com.benberi.cadesim.server.model.player; import com.benberi.cadesim.server.ServerContext; +import com.benberi.cadesim.server.model.player.move.MoveType; + +import io.netty.channel.Channel; /* * AI Logic - moving type but engage with player in range @@ -10,10 +13,12 @@ public class NPC_Type3 extends Player { @SuppressWarnings("unused") private ServerContext context; - public NPC_Type3(ServerContext ctx) { - this.context = ctx; - setBot(true); - set(-1, -1); // not spawned + public NPC_Type3(ServerContext ctx, Channel c) { + super(ctx,c); + context = ctx; + super.setBot(true); + super.set(-1, -1); // not spawned + super.setType(NPC_Type.TYPE3); } @Override @@ -23,6 +28,11 @@ public void calculateRoute() { @Override public void performLogic() { - + getMoves().setMove(0, MoveType.FORWARD); + } + + @Override + public NPC_Type getType() { + return type; } } diff --git a/server/src/com/benberi/cadesim/server/model/player/NPC_Type4.java b/server/src/com/benberi/cadesim/server/model/player/NPC_Type4.java index 92e8116..8058305 100644 --- a/server/src/com/benberi/cadesim/server/model/player/NPC_Type4.java +++ b/server/src/com/benberi/cadesim/server/model/player/NPC_Type4.java @@ -1,6 +1,9 @@ package com.benberi.cadesim.server.model.player; import com.benberi.cadesim.server.ServerContext; +import com.benberi.cadesim.server.model.player.move.MoveType; + +import io.netty.channel.Channel; /* * AI Logic - chaser type (goes after player) @@ -10,19 +13,26 @@ public class NPC_Type4 extends Player { @SuppressWarnings("unused") private ServerContext context; - public NPC_Type4(ServerContext ctx) { - this.context = ctx; - setBot(true); - set(-1, -1); // not spawned + public NPC_Type4(ServerContext ctx, Channel c) { + super(ctx,c); + context = ctx; + super.setBot(true); + super.set(-1, -1); // not spawned + super.setType(NPC_Type.TYPE4); } @Override public void calculateRoute() { - + } @Override public void performLogic() { - + getMoves().setMove(0, MoveType.FORWARD); + } + + @Override + public NPC_Type getType() { + return type; } } diff --git a/server/src/com/benberi/cadesim/server/model/player/Player.java b/server/src/com/benberi/cadesim/server/model/player/Player.java index 7ab0fa6..b617d30 100644 --- a/server/src/com/benberi/cadesim/server/model/player/Player.java +++ b/server/src/com/benberi/cadesim/server/model/player/Player.java @@ -83,7 +83,7 @@ public class Player extends Position { * The server context */ private ServerContext context; - + protected NPC_Type type; /** * If the player is registered */ @@ -422,7 +422,7 @@ public void register(String name, int ship, int team, int[] customPosition, Vess respawn(customPosition, customFace, customDamage, shouldSpawnFullCannons); } - /** + /** * wrapper for register */ public void register(String name, int ship, int team) { @@ -945,4 +945,16 @@ public String getChatChannel() { public void setTeam(Team team) { this.team = team; } + + public void printType() { + System.out.println(name + " ,"+ type); + } + + public NPC_Type getType() { + return type; + } + + public void setType(NPC_Type typeValue) { + type = typeValue; + } } diff --git a/server/src/com/benberi/cadesim/server/model/player/PlayerManager.java b/server/src/com/benberi/cadesim/server/model/player/PlayerManager.java index 7bbef4b..d68cdd1 100644 --- a/server/src/com/benberi/cadesim/server/model/player/PlayerManager.java +++ b/server/src/com/benberi/cadesim/server/model/player/PlayerManager.java @@ -37,7 +37,6 @@ public class PlayerManager { * List of players in the game */ private List players = new ArrayList<>(); - /** * List of temporarily banned IPs in the game */ @@ -334,6 +333,9 @@ public void tick() { * Handles and executes all turns */ public void handleTurns() { + for(Player player : listBots()) { + player.performLogic(); + } if (ServerConfiguration.isTestMode()) { if (!context.getRegressionTests().loadNextScenario()) { context.getRegressionTests().getSummary(); @@ -341,7 +343,7 @@ public void handleTurns() { System.exit(Constants.EXIT_SUCCESS); } } - + for (Player player : listRegisteredPlayers()) { player.getPackets().sendSelectedMoves(); // unglitch players' moves player.resetAnimationStructure(); // reset all of their animation structures before the animation builds up again @@ -648,6 +650,21 @@ public List listRegisteredPlayers() { return registered; } + /** + * Lists all bots + * + * @return Sorted list of {@link #players} with only registered players + */ + public List listBots() { + List bots = new ArrayList<>(); + for (Player p : players) { + if (p.isRegistered() && p.isBot()) { + bots.add(p); + } + } + return bots; + } + /** * unregistered players get a few seconds to properly register. */ @@ -733,7 +750,8 @@ public Player createBot( VesselFace startFace, float startDamage ) { - Player p = registerPlayer(null); + Player p = new Player(context,null); + registerPlayer(null, p); p.register( name, vesselID, @@ -745,6 +763,95 @@ public Player createBot( ); return p; } + /** + * Create new NPC. + * @param type its AI type if specified + * @param name its name + * @param vesselID its ship type enum + * @param team its team (team type enum) + * @param startPosition -1,-1 to start not spawned, or >=0,>=0 to start on the map. + * @param startFace which way to face to start with? + * @param startDamage how much damage to spawn in with + * + * ie. createBot(NPC_Type.TYPE1, "Type1", 11, Team.ATTACKER, null, VesselFace.SOUTH, 0.0f); + */ + public Player createBot( + NPC_Type type , + String name, + int vesselID, + Team team, + int[] startPosition, + VesselFace startFace, + float startDamage + ) { + switch(type) { + case TYPE1: + NPC_Type1 npc1 = new NPC_Type1(context, null); + registerPlayer(null, npc1); + npc1.register( + name, + vesselID, + team.getID(), + startPosition, + startFace, + startDamage, + true + ); + return npc1; + case TYPE2: + NPC_Type2 npc2 = new NPC_Type2(context, null); + registerPlayer(null, npc2); + npc2.register( + name, + vesselID, + team.getID(), + startPosition, + startFace, + startDamage, + true + ); + return npc2; + case TYPE3: + NPC_Type3 npc3 = new NPC_Type3(context, null); + registerPlayer(null, npc3); + npc3.register( + name, + vesselID, + team.getID(), + startPosition, + startFace, + startDamage, + true + ); + return npc3; + case TYPE4: + NPC_Type4 npc4 = new NPC_Type4(context, null); + registerPlayer(null, npc4); + npc4.register( + name, + vesselID, + team.getID(), + startPosition, + startFace, + startDamage, + true + ); + return npc4; + default: + Player p = new Player(context,null); + registerPlayer(null, p); + p.register( + name, + vesselID, + team.getID(), + startPosition, + startFace, + startDamage, + true + ); + return p; + } + } /** * Remove an NPC. @@ -761,8 +868,7 @@ public void removeBot(Player player) { * @param c The channel to register * @return the newly created player */ - public Player registerPlayer(Channel c) { - Player player = new Player(context, c); + public Player registerPlayer(Channel c, Player player) { String ip = player.getIP(); if ((!player.isBot()) && (temporaryBannedIPs.contains(ip))) // bots are exempt { @@ -1089,8 +1195,10 @@ public int getPointsAttacker() { public void queueOutgoing() { for (Player p : players) { - p.getPackets().queueOutgoingPackets(); - p.getChannel().flush(); + if(!p.isBot()) { + p.getPackets().queueOutgoingPackets(); + p.getChannel().flush(); + } } }