Browse Source

Improve the AI some more

Signed-off-by: Chris Cromer <chris@cromer.cl>
master
Chris Cromer 2 years ago
parent
commit
cb7fe3767d
  1. 8
      src/main/java/cl/cromer/azaraka/Canvas.java
  2. 2
      src/main/java/cl/cromer/azaraka/Constants.java
  3. 2
      src/main/java/cl/cromer/azaraka/ai/PlayerAI.java
  4. 52
      src/main/java/cl/cromer/azaraka/ai/PlayerAStarAI.java
  5. 50
      src/main/java/cl/cromer/azaraka/ai/PlayerBreadthFirstAI.java
  6. 230
      src/main/resources/scene.json

8
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());

2
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
*/

2
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;

52
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);
}
}

50
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);
}
}
}

230
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,142 +530,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": [
@ -775,12 +589,6 @@
0
]
},
{
"type": "null",
"textures": [
0
]
},
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
@ -840,12 +648,6 @@
0
]
},
{
"type": "null",
"textures": [
0
]
},
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
@ -905,12 +707,6 @@
0
]
},
{
"type": "null",
"textures": [
0
]
},
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
@ -970,12 +766,6 @@
0
]
},
{
"type": "null",
"textures": [
0
]
},
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
@ -1035,12 +825,6 @@
0
]
},
{
"type": "null",
"textures": [
0
]
},
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
@ -1100,12 +884,6 @@
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": [

Loading…
Cancel
Save