From cb7fe3767d6bf4e5ed9044628655a8ffa697578b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Oct 2019 18:49:28 -0300 Subject: [PATCH] Improve the AI some more Signed-off-by: Chris Cromer --- src/main/java/cl/cromer/azaraka/Canvas.java | 8 +- .../java/cl/cromer/azaraka/Constants.java | 2 +- .../java/cl/cromer/azaraka/ai/PlayerAI.java | 2 +- .../cl/cromer/azaraka/ai/PlayerAStarAI.java | 52 ++-- .../azaraka/ai/PlayerBreadthFirstAI.java | 50 ++-- src/main/resources/scene.json | 230 ------------------ 6 files changed, 59 insertions(+), 285 deletions(-) diff --git a/src/main/java/cl/cromer/azaraka/Canvas.java b/src/main/java/cl/cromer/azaraka/Canvas.java index e1877f2..fff6ce9 100644 --- a/src/main/java/cl/cromer/azaraka/Canvas.java +++ b/src/main/java/cl/cromer/azaraka/Canvas.java @@ -40,11 +40,9 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.logging.Logger; /** @@ -309,10 +307,10 @@ public class Canvas extends java.awt.Canvas implements Constants { */ private void setupPlayerAI() { try { - player.getAi().addDestination(new State(2, 0, State.Type.EXIT, null, 3)); + player.getAi().addDestination(new State(2, 0, State.Type.EXIT, null, 4)); // Shuffle the chests so that the AI doesn't open the correct chests on the first go - Collections.shuffle(chests, new Random(23)); + //Collections.shuffle(chests, new Random(23)); for (Chest chest : chests) { player.getAi().addDestination(new State(chest.getCell().getX(), chest.getCell().getY() + 1, State.Type.CHEST, null, 2)); } @@ -320,8 +318,6 @@ public class Canvas extends java.awt.Canvas implements Constants { for (Key key : keys) { player.getAi().addDestination(new State(key.getCell().getX(), key.getCell().getY(), State.Type.KEY, null, 1)); } - - player.getAi().sortDestinations(); } catch (AIException e) { logger.warning(e.getMessage()); diff --git a/src/main/java/cl/cromer/azaraka/Constants.java b/src/main/java/cl/cromer/azaraka/Constants.java index 561859f..bdeba51 100644 --- a/src/main/java/cl/cromer/azaraka/Constants.java +++ b/src/main/java/cl/cromer/azaraka/Constants.java @@ -85,7 +85,7 @@ public interface Constants { /** * Generates the scene manually instead of from the JSON file if true */ - boolean GENERATE_SCENE = true; + boolean GENERATE_SCENE = false; /** * Exports the scene to a JSON file if true */ diff --git a/src/main/java/cl/cromer/azaraka/ai/PlayerAI.java b/src/main/java/cl/cromer/azaraka/ai/PlayerAI.java index 5380905..397433c 100644 --- a/src/main/java/cl/cromer/azaraka/ai/PlayerAI.java +++ b/src/main/java/cl/cromer/azaraka/ai/PlayerAI.java @@ -136,7 +136,7 @@ public interface PlayerAI extends Runnable, Constants { } player.interact(); if (!portalWasActive) { - addDestination(new State(portal.getCell().getX(), portal.getCell().getY(), State.Type.PORTAL, null, 1)); + addDestination(new State(portal.getCell().getX(), portal.getCell().getY(), State.Type.PORTAL, null, 3)); } sortDestinations(); return true; diff --git a/src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java b/src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java index e2661c4..caed964 100644 --- a/src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java +++ b/src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java @@ -208,7 +208,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants { 22222 */ - EnemyCost enemyCost = EnemyCost.DIRECT_CORNERS; + EnemyCost enemyCost = EnemyCost.FAR_CORNERS; if (enemyCost.getLevel() == EnemyCost.NONE.getLevel()) { return EnemyCost.NONE.getCost(); @@ -359,6 +359,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants { */ public void addDestination(State destination) { destinations.add(destination); + sortDestinations(); } /** @@ -385,43 +386,46 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants { if (checkCondition(scene, destination)) { getLogger().info("Check A* Search goal!"); found = search(initial, destination); - } - if (initial.equals(destination)) { - if (destinationArrived(scene, destination)) { - destinations.remove(destination); - destinationIndex = 0; + if (initial.equals(destination)) { + if (destinationArrived(scene, destination)) { + destinations.remove(destination); + destinationIndex = 0; + } + } + else { + if (!found) { + clearStates(); + // Don't run this because the destination might return to be available again at some point + //destinationArrived(objective); + } } } else { - if (!found) { - clearStates(); - // Don't run this because the destination might return to be available again at some point - //destinationArrived(objective); - } + clearStates(); } if (destinations.isEmpty()) { getLogger().info("No more destinations for A* Search!"); setActive(false); + return; } - destinationIndex++; - if (destinationIndex >= destinations.size()) { - getLogger().info("None of the destinations are reachable for A* Search!"); - // No destinations are reachable, make the player move around at random to help move the enemies - if (steps.size() == 0) { - steps.add(0, State.Type.PLAYER); + if (!found) { + destinationIndex++; + if (destinationIndex >= destinations.size()) { + getLogger().info("None of the destinations are reachable for A* Search!"); + // No destinations are reachable, make the player move around at random to help move the enemies + if (steps.size() == 0) { + steps.add(0, State.Type.PLAYER); + } + steps.add(1, getOpenSpaceAroundPlayer(scene)); + break; } - steps.add(1, getOpenSpaceAroundPlayer(scene)); - found = true; - break; } } - while (!found && !destinations.isEmpty()); + while (!found); - if (found) { - doAction(scene, steps); - } + doAction(scene, steps); } } diff --git a/src/main/java/cl/cromer/azaraka/ai/PlayerBreadthFirstAI.java b/src/main/java/cl/cromer/azaraka/ai/PlayerBreadthFirstAI.java index 3c67aa7..458bb82 100644 --- a/src/main/java/cl/cromer/azaraka/ai/PlayerBreadthFirstAI.java +++ b/src/main/java/cl/cromer/azaraka/ai/PlayerBreadthFirstAI.java @@ -219,6 +219,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants { */ public void addDestination(State destination) { destinations.add(destination); + sortDestinations(); } /** @@ -279,43 +280,46 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants { if (checkCondition(scene, destination)) { getLogger().info("Check Breadth-First Search goal!"); found = search(initial, destination); - } - if (initial.equals(destination)) { - if (destinationArrived(scene, destination)) { - destinations.remove(destination); - destinationIndex = 0; + if (initial.equals(destination)) { + if (destinationArrived(scene, destination)) { + destinations.remove(destination); + destinationIndex = 0; + } + } + else { + if (!found) { + clearStates(); + // Don't run this because the destination might return to be available again at some point + //destinationArrived(objective); + } } } else { - if (!found) { - clearStates(); - // Don't run this because the destination might return to be available again at some point - //destinationArrived(objective); - } + clearStates(); } if (destinations.isEmpty()) { getLogger().info("No more destinations for Breadth-First Search!"); setActive(false); + return; } - destinationIndex++; - if (destinationIndex >= destinations.size()) { - getLogger().info("None of the destinations are reachable for Breadth-First Search!"); - // No destinations are reachable, make the player move around at random to help move the enemies - if (steps.size() == 0) { - steps.add(0, State.Type.PLAYER); + if (!found) { + destinationIndex++; + if (destinationIndex >= destinations.size()) { + getLogger().info("None of the destinations are reachable for Breadth-First Search!"); + // No destinations are reachable, make the player move around at random to help move the enemies + if (steps.size() == 0) { + steps.add(0, State.Type.PLAYER); + } + steps.add(1, getOpenSpaceAroundPlayer(scene)); + break; } - steps.add(1, getOpenSpaceAroundPlayer(scene)); - found = true; - break; } } - while (!found && !destinations.isEmpty()); + while (!found); - if (found) { - doAction(scene, steps); - } + doAction(scene, steps); } } } diff --git a/src/main/resources/scene.json b/src/main/resources/scene.json index 549435b..abe8352 100644 --- a/src/main/resources/scene.json +++ b/src/main/resources/scene.json @@ -59,14 +59,6 @@ 65 ] }, - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 49, - 255 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -126,12 +118,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -190,12 +176,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -256,12 +236,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -321,12 +295,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -386,12 +354,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -451,12 +413,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -516,12 +472,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -580,12 +530,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -645,12 +589,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -710,12 +648,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -775,12 +707,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -840,12 +766,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -905,12 +825,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -970,142 +884,6 @@ 0 ] }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 98, - 207 - ] - } - ], - [ - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 35 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 99 - ] - } - ], - [ - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 34, - 222 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, - { - "type": "null", - "textures": [ - 0 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [ @@ -1175,14 +953,6 @@ 69 ] }, - { - "type": "cl.cromer.azaraka.object.Obstacle", - "textures": [ - 0, - 53, - 238 - ] - }, { "type": "cl.cromer.azaraka.object.Obstacle", "textures": [