From f2a961d1591f36bd13d1034ad28f70c20b14dd19 Mon Sep 17 00:00:00 2001 From: punchready Date: Sat, 5 Jun 2021 04:00:25 +0200 Subject: [PATCH] fix: resolve various issues with ai, cloning and turn order handling --- .../gamelibrary/entities/Inventory.java | 15 +++---- .../gamelibrary/gamelogic/EntityManager.java | 13 ++---- .../gamelibrary/gamelogic/GameLogic.java | 41 ++++++++++++++----- .../gamelibrary/gamelogic/GameState.java | 12 +++--- .../gamelibrary/gamelogic/AITest.java | 4 +- .../gamelogic/BaseGameLogicTest.java | 2 +- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java index 25ce242..90b2845 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java @@ -1,9 +1,6 @@ package uulm.teamname.marvelous.gamelibrary.entities; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Objects; +import java.util.*; /** Represents an inventory of 6 slots of {@link StoneType StoneTypes} that can be manipulated. */ public class Inventory implements Iterable { @@ -85,12 +82,12 @@ public class Inventory implements Iterable { * @return The array of stones the inventory has */ public StoneType[] getStonesAsArray() { - var toReturn = new StoneType[content.size()]; - var iterator = content.iterator(); - for (int i = 0; i < content.size(); i++) { - toReturn[i] = iterator.next(); + StoneType[] result = new StoneType[content.size()]; + int i = 0; + for(StoneType stone: content) { + result[i++] = stone; } - return toReturn; + return result; } /** diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java index 0cb4650..43e9693 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java @@ -60,7 +60,7 @@ public class EntityManager { if(entity.id.isSameType(EntityType.Rocks)) { usedRockSlots.add(entity.id.id); } - entities.add(entity); + entities.add(entity.clone()); } /** @@ -69,7 +69,7 @@ public class EntityManager { */ void addEntities(Entity... entities) { for(Entity e: entities) { - this.addEntity(e); + this.addEntity(e.clone()); } } @@ -89,18 +89,13 @@ public class EntityManager { * @param entityid The {@link EntityID} of the {@link Entity} to remove */ boolean removeEntity(EntityID entityid) { - Entity entity = findEntity(entityid); - if(entity != null) { - return entities.remove(entity); - }else { - return false; - } + return entities.removeIf(e -> e.id.equals(entityid)); } /** * Finds an entity with an {@link EntityID}. * @param id The id to search for - * @return The found {@link Entity} or null if none found + * @return The found {@link Entity} or {@code null} if none found */ public Entity findEntity(EntityID id) { for(Entity entity: entities) { diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index 223b64c..6a11da7 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -1,5 +1,6 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; +import org.tinylog.Logger; import uulm.teamname.marvelous.gamelibrary.ArrayTools; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.config.FieldType; @@ -187,7 +188,7 @@ public class GameLogic { } } }catch(InvalidRequestException exception) { - System.out.println("Request denied: " + exception.getMessage()); + Logger.debug("Request denied: " + exception.getMessage()); return false; }catch(Exception ignored) { return false; @@ -332,7 +333,6 @@ public class GameLogic { if(target.hp.getValue() <= data.value) { List stones = Arrays.asList(target.inventory.getStonesAsArray()); - target.inventory.clear(); Collections.shuffle(stones); // required by documents for(StoneType stone: stones) { @@ -504,6 +504,7 @@ public class GameLogic { EntityType opposing = target.id.type == EntityType.P1 ? EntityType.P2 : EntityType.P1; state.winConditions.increaseValue(opposing, WinCondition.TotalDamage, data.amount); if(!target.isAlive()) { + target.inventory.clear(); state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1); } @@ -716,7 +717,7 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ protected static ArrayList startGame(GameState state, ArrayList selectedCharacters1, ArrayList selectedCharacters2) { - System.out.println("Starting game"); + Logger.trace("Starting game"); ArrayList result = new ArrayList<>(); @@ -823,7 +824,11 @@ public class GameLogic { continue; } - Character character = ((Character)state.entities.findEntity(id)); + Entity found = state.entities.findEntity(id); + if(found == null) { + continue; + } + Character character = ((Character)found); if(character.isAlive()){ anyAlive = true; @@ -868,8 +873,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleRoundStart(GameState state) { - System.out.println("Starting round " + (state.roundNumber + 1)); - ArrayList result = new ArrayList<>(); state.roundNumber++; @@ -898,7 +901,11 @@ public class GameLogic { Collections.shuffle(state.turnOrder); for (EntityID id: state.turnOrder) { - if(id.type == EntityType.NPC || revived.contains(id) || ((Character)state.entities.findEntity(id)).isAlive()){ + Entity found = state.entities.findEntity(id); + if(found == null) { + continue; + } + if(id.type == EntityType.NPC || revived.contains(id) || ((Character)found).isAlive()){ state.activeCharacter = id; break; }else { // again send empty turns for knocked out characters @@ -973,6 +980,9 @@ public class GameLogic { ArrayList targetOptions = new ArrayList<>(); int lowest = -1; for(EntityID id: state.turnOrder) { + if(id.type == EntityType.NPC) { + continue; + } Character character = (Character)state.entities.findEntity(id); characters.add(character); @@ -1037,6 +1047,9 @@ public class GameLogic { int maxMP = -1; for(EntityID id: state.turnOrder) { + if(id.type == EntityType.NPC) { + continue; + } Character character = (Character)state.entities.findEntity(id); if(character.mp.getMax() > maxMP) { @@ -1095,7 +1108,7 @@ public class GameLogic { ArrayList path = GameLogic.Bresenham4Connected(thanos.getPosition(), picked); - int mp = thanos.mp.getValue(); + int mp = thanos.mp.getMax(); if(mp <= 0) { return result; @@ -1145,6 +1158,10 @@ public class GameLogic { break; } if(entity instanceof InfinityStone) { + result.add(new EventBuilder(EventType.DestroyedEntityEvent) + .withTargetEntity(entity.id) + .withTargetField(pos) + .buildEntityEvent()); break; } } @@ -1162,10 +1179,14 @@ public class GameLogic { if(id.type == EntityType.NPC) { continue; } + Entity found = state.entities.findEntity(id); + if(found == null) { + continue; + } if(rand.nextBoolean()) { result.add(new EventBuilder(EventType.DestroyedEntityEvent) .withTargetEntity(id) - .withTargetField(state.entities.findEntity(id).getPosition()) + .withTargetField(found.getPosition()) .buildEntityEvent()); } } @@ -1204,7 +1225,7 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handlePlayerWin(GameState state, EntityType winner) { - System.out.println("Player " + winner + " won"); + Logger.trace("Player " + winner + " won"); ArrayList result = new ArrayList<>(); diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java index 48edda8..fe560a4 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java @@ -133,22 +133,24 @@ class GameState { case NPC -> { switch(entities.get(0).id.id) { case 0 -> { - sb.append("G "); + sb.append("G"); } case 1 -> { - sb.append("S "); + sb.append("S"); } case 2 -> { - sb.append("T "); + sb.append("T"); } } + sb.append(entities.get(0).id.equals(activeCharacter) ? "'" : " "); } case P1, P2 -> { if(((Character)entities.get(0)).isAlive()) { - sb.append("X "); + sb.append("X"); }else { - sb.append("x "); + sb.append("x"); } + sb.append(entities.get(0).id.equals(activeCharacter) ? "'" : " "); } case Rocks -> { sb.append("O "); diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/AITest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/AITest.java index 9929ef7..8d74a23 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/AITest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/AITest.java @@ -39,9 +39,7 @@ class AITest extends BaseGameLogicTest { void main() throws InterruptedException { serverSend(server.startGame(player1Selection, player2Selection).toArray(new Event[0])); - Thread.sleep(1000); - - System.out.println(server.toString()); + Thread.sleep(10000); } private void clientSend(Request[] requests) { diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/BaseGameLogicTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/BaseGameLogicTest.java index 447f7a5..431a222 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/BaseGameLogicTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/BaseGameLogicTest.java @@ -17,7 +17,7 @@ public class BaseGameLogicTest { protected static final ArrayList player2Selection = new ArrayList<>(); protected static void generate() { - partyConfig.maxRounds = 100; + partyConfig.maxRounds = 50; partyConfig.mindStoneCD = 2; partyConfig.powerStoneCD = 3; partyConfig.realityStoneCD = 4;