Improve the AI some more

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-10-26 18:49:28 -03:00
parent 4067055af4
commit cb7fe3767d
6 changed files with 59 additions and 285 deletions

View File

@ -40,11 +40,9 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -309,10 +307,10 @@ public class Canvas extends java.awt.Canvas implements Constants {
*/ */
private void setupPlayerAI() { private void setupPlayerAI() {
try { 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 // 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) { for (Chest chest : chests) {
player.getAi().addDestination(new State(chest.getCell().getX(), chest.getCell().getY() + 1, State.Type.CHEST, null, 2)); 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) { for (Key key : keys) {
player.getAi().addDestination(new State(key.getCell().getX(), key.getCell().getY(), State.Type.KEY, null, 1)); player.getAi().addDestination(new State(key.getCell().getX(), key.getCell().getY(), State.Type.KEY, null, 1));
} }
player.getAi().sortDestinations();
} }
catch (AIException e) { catch (AIException e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());

View File

@ -85,7 +85,7 @@ public interface Constants {
/** /**
* Generates the scene manually instead of from the JSON file if true * 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 * Exports the scene to a JSON file if true
*/ */

View File

@ -136,7 +136,7 @@ public interface PlayerAI extends Runnable, Constants {
} }
player.interact(); player.interact();
if (!portalWasActive) { 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(); sortDestinations();
return true; return true;

View File

@ -208,7 +208,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
22222 22222
*/ */
EnemyCost enemyCost = EnemyCost.DIRECT_CORNERS; EnemyCost enemyCost = EnemyCost.FAR_CORNERS;
if (enemyCost.getLevel() == EnemyCost.NONE.getLevel()) { if (enemyCost.getLevel() == EnemyCost.NONE.getLevel()) {
return EnemyCost.NONE.getCost(); return EnemyCost.NONE.getCost();
@ -359,6 +359,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
*/ */
public void addDestination(State destination) { public void addDestination(State destination) {
destinations.add(destination); destinations.add(destination);
sortDestinations();
} }
/** /**
@ -385,43 +386,46 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
if (checkCondition(scene, destination)) { if (checkCondition(scene, destination)) {
getLogger().info("Check A* Search goal!"); getLogger().info("Check A* Search goal!");
found = search(initial, destination); found = search(initial, destination);
}
if (initial.equals(destination)) { if (initial.equals(destination)) {
if (destinationArrived(scene, destination)) { if (destinationArrived(scene, destination)) {
destinations.remove(destination); destinations.remove(destination);
destinationIndex = 0; 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 { else {
if (!found) { clearStates();
clearStates();
// Don't run this because the destination might return to be available again at some point
//destinationArrived(objective);
}
} }
if (destinations.isEmpty()) { if (destinations.isEmpty()) {
getLogger().info("No more destinations for A* Search!"); getLogger().info("No more destinations for A* Search!");
setActive(false); setActive(false);
return;
} }
destinationIndex++; if (!found) {
if (destinationIndex >= destinations.size()) { destinationIndex++;
getLogger().info("None of the destinations are reachable for A* Search!"); if (destinationIndex >= destinations.size()) {
// No destinations are reachable, make the player move around at random to help move the enemies getLogger().info("None of the destinations are reachable for A* Search!");
if (steps.size() == 0) { // No destinations are reachable, make the player move around at random to help move the enemies
steps.add(0, State.Type.PLAYER); 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);
}
} }
} }

View File

@ -219,6 +219,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
*/ */
public void addDestination(State destination) { public void addDestination(State destination) {
destinations.add(destination); destinations.add(destination);
sortDestinations();
} }
/** /**
@ -279,43 +280,46 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
if (checkCondition(scene, destination)) { if (checkCondition(scene, destination)) {
getLogger().info("Check Breadth-First Search goal!"); getLogger().info("Check Breadth-First Search goal!");
found = search(initial, destination); found = search(initial, destination);
}
if (initial.equals(destination)) { if (initial.equals(destination)) {
if (destinationArrived(scene, destination)) { if (destinationArrived(scene, destination)) {
destinations.remove(destination); destinations.remove(destination);
destinationIndex = 0; 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 { else {
if (!found) { clearStates();
clearStates();
// Don't run this because the destination might return to be available again at some point
//destinationArrived(objective);
}
} }
if (destinations.isEmpty()) { if (destinations.isEmpty()) {
getLogger().info("No more destinations for Breadth-First Search!"); getLogger().info("No more destinations for Breadth-First Search!");
setActive(false); setActive(false);
return;
} }
destinationIndex++; if (!found) {
if (destinationIndex >= destinations.size()) { destinationIndex++;
getLogger().info("None of the destinations are reachable for Breadth-First Search!"); if (destinationIndex >= destinations.size()) {
// No destinations are reachable, make the player move around at random to help move the enemies getLogger().info("None of the destinations are reachable for Breadth-First Search!");
if (steps.size() == 0) { // No destinations are reachable, make the player move around at random to help move the enemies
steps.add(0, State.Type.PLAYER); 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);
}
} }
} }
} }

View File

@ -59,14 +59,6 @@
65 65
] ]
}, },
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
0,
49,
255
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -126,12 +118,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -190,12 +176,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -256,12 +236,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -321,12 +295,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -386,12 +354,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -451,12 +413,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -516,12 +472,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -580,12 +530,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -645,12 +589,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -710,12 +648,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -775,12 +707,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -840,12 +766,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -905,12 +825,6 @@
0 0
] ]
}, },
{
"type": "null",
"textures": [
0
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -970,142 +884,6 @@
0 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", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [
@ -1175,14 +953,6 @@
69 69
] ]
}, },
{
"type": "cl.cromer.azaraka.object.Obstacle",
"textures": [
0,
53,
238
]
},
{ {
"type": "cl.cromer.azaraka.object.Obstacle", "type": "cl.cromer.azaraka.object.Obstacle",
"textures": [ "textures": [