PathFinder AI (buged!)

This commit is contained in:
iryupnik 2020-03-22 21:20:59 +01:00
parent 1f37fb04fa
commit e1b52ca627
6 changed files with 64 additions and 120 deletions

View File

@ -21,11 +21,14 @@ public class Npc extends Person {
} }
public void move(int endX, int endY) { public void move(int endX, int endY) {
// System.out.println("NPC: " + getX() + " == " + endX + " | " + getY() + " == " + endY);
x = this.getX(); x = this.getX();
y = this.getY(); y = this.getY();
PathFinder pathFinder = new PathFinder(getMap()); PathFinder pathFinder = new PathFinder(getMap());
for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){ for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){
WindowOfFreeMove.sleep(500); System.out.println(getX() + " " + getY());
System.out.println(direction);
// WindowOfFreeMove.sleep(500);
switch (direction){ switch (direction){
case UP: case UP:
moveUp(); moveUp();
@ -42,6 +45,8 @@ public class Npc extends Person {
} }
mapPainter.repaintCanvas(); mapPainter.repaintCanvas();
} }
System.out.println("-----------------------");
System.out.println("WTF ARE YOU?! " + getX() + " " + getY());
} }
public void moveUp() { public void moveUp() {

View File

@ -113,7 +113,8 @@ public class Person extends Cell {
map.findEventByXY(x, y).moveToMap(); map.findEventByXY(x, y).moveToMap();
} }
} else { } else {
if (!map.isBusyCell(x, y) && !map.isInteractionCell(x, y)) { if (!map.isInteractionCell(x, y)) //TODO isBusyCell
{
set(x, y); set(x, y);
} }
} }

View File

@ -250,10 +250,10 @@ public class Map {
public boolean isBusyCell(int x, int y) { public boolean isBusyCell(int x, int y) {
// for (Block cell : block) { for (Block cell : block) {
// if (cell.isInBlock(x, y)) if (cell.isInBlock(x, y))
// return true; return true;
// } }
return false; return false;
} }

View File

@ -1,5 +1,5 @@
package ruscz.game.gameOfProd.Scripts; package ruscz.game.gameOfProd.Scripts;
public enum Direction { public enum Direction {
UP, DOWN, LEFT, RIGHT UP, DOWN, LEFT, RIGHT, NONE
} }

View File

@ -6,20 +6,20 @@ import java.util.LinkedList;
public class PathFinder { public class PathFinder {
private Map map; private Map map;
private boolean noWayFound;
private boolean foundWay; private boolean foundWay;
private boolean dontXMove;
private int nowX;
private int nowY;
public PathFinder(Map map) { public PathFinder(Map map) {
this.map = map; this.map = map;
foundWay = false; foundWay = false;
} }
private Direction[] getAllWays(){
return new Direction[]{Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT};
}
public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) { public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) {
nowX = startX; int nowX = startX;
nowY = startY; int nowY = startY;
LinkedList<Direction> directions = new LinkedList<>(); LinkedList<Direction> directions = new LinkedList<>();
while (!foundWay) { while (!foundWay) {
if (nowX == endX && nowY == endY) { if (nowX == endX && nowY == endY) {
@ -46,113 +46,52 @@ public class PathFinder {
return directions; return directions;
} }
public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY) { private LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList<Direction> directions) {
nowX = startX; // System.out.println(startX + " == " + endX + " | " + startY + " == " + endY);
nowY = startY; if (startX == endX && startY == endY){
LinkedList<Direction> directions = new LinkedList<>(); System.out.println("YAS!");
while (!foundWay) {
System.out.println(directions);
System.out.println(nowX + " " + nowY);
if (directions.toArray().length > 50) {
noWayFound = true;
break;
}
if (nowX == endX && nowY == endY) {
foundWay = true;
}
if (nowX != endX && !dontXMove) {
if (endX > nowX) {
if (!map.isBusyCell(nowX + 1, nowY) && !map.isInteractionCell(nowX + 1, nowY)) {
nowX++;
System.out.println("YEAH");
directions.add(Direction.RIGHT);
} else {
directions.add(findOptimalStep(nowX, nowY, nowX + 1, nowY, directions));
}
} else {
if (!map.isBusyCell(nowX - 1, nowY) && !map.isInteractionCell(nowX - 1, nowY)) {
nowX--;
directions.add(Direction.LEFT);
} else {
directions.add(findOptimalStep(nowX, nowY, nowX - 1, nowY, directions));
}
}
} else {
if (endY > nowY) {
if (!map.isBusyCell(nowX, nowY + 1) && !map.isInteractionCell(nowX, nowY + 1)) {
nowY++;
dontXMove = false;
directions.add(Direction.DOWN);
} else {
directions.add(findOptimalStep(nowX, nowY, nowX, nowY + 1, directions));
}
} else {
if (!map.isBusyCell(nowX, nowY - 1) && !map.isInteractionCell(nowX, nowY - 1)) {
nowY--;
dontXMove = false;
directions.add(Direction.UP);
} else {
directions.add(findOptimalStep(nowX, nowY, nowX, nowY - 1, directions));
}
}
}
}
if (noWayFound) {
return new LinkedList<>();
}
return directions; return directions;
} }
private Direction findOptimalStep(int nowX, int nowY, int blockX, int blockY, LinkedList<Direction> directions) { for (Direction direction: getAllWays()){
if (nowX == blockX) { switch (direction){
if (!map.isBusyCell(nowX, nowY - 1) && !map.isInteractionCell(nowX, nowY - 1)) {
this.nowX--;
dontXMove = true;
return Direction.LEFT;
}
if (!map.isBusyCell(nowX, nowY + 1) && !map.isInteractionCell(nowX, nowY + 1)) {
this.nowX++;
dontXMove = true;
return Direction.RIGHT;
}
} else if (nowY == blockY) {
if (!map.isBusyCell(nowX + 1, nowY) && !map.isInteractionCell(nowX + 1, nowY)) {
this.nowY--;
dontXMove = false;
return Direction.UP;
}
if (!map.isBusyCell(nowX - 1, nowY) && !map.isInteractionCell(nowX - 1, nowY)) {
this.nowY++;
dontXMove = false;
return Direction.DOWN;
}
}
Direction dir;
switch (directions.getLast()){
case UP: case UP:
this.nowY--; if (!map.isBusyCell(startX, startY - 1) && directions.getLast() != Direction.DOWN){
dontXMove = false; directions.add(Direction.UP);
dir = Direction.UP; startY--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break; break;
case DOWN: case DOWN:
this.nowY++; if (!map.isBusyCell(startX, startY + 1) && directions.getLast() != Direction.UP){
dontXMove = false; directions.add(Direction.DOWN);
dir = Direction.DOWN; startY++;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break; break;
case LEFT: case LEFT:
this.nowX--; if (!map.isBusyCell(startX - 1, startY) && directions.getLast() != Direction.RIGHT){
dontXMove = true; directions.add(Direction.LEFT);
dir = Direction.LEFT; startX--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break; break;
case RIGHT: case RIGHT:
this.nowX++; if (!map.isBusyCell(startX + 1, startY) && directions.getLast() != Direction.LEFT){
dontXMove = true; directions.add(Direction.RIGHT);
dir = Direction.RIGHT; startX++;
break; return getWayWithCollision(startX, startY, endX, endY, directions);
default:
dir = null;
} }
return dir; break;
}
}
directions.removeLast();
return getWayWithCollision(startX, startY, endX, endY, directions);
}
public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY){
LinkedList<Direction> directions = new LinkedList<>();
directions.add(Direction.NONE);
return getWayWithCollision(startX, startY, endX, endY, directions);
} }
} }

View File

@ -200,8 +200,8 @@ public class WindowOfFreeMove extends JFrame {
isReadyToPaint = true; isReadyToPaint = true;
canvas.repaint(); canvas.repaint();
Npc npc2 = new Npc(1,1, m1, mapPainter); Npc npc2 = new Npc(1,26, m6, mapPainter);
m1.setNpcs(npc2); m6.setNpcs(npc2);
Timer timer = new Timer(10, new ActionListener() { Timer timer = new Timer(10, new ActionListener() {
@Override @Override
@ -213,8 +213,7 @@ public class WindowOfFreeMove extends JFrame {
}); });
timer.start(); timer.start();
while (true){ while (true){
npc2.move(6,1); npc2.move(9,34);
npc2.move(1, 6);
} }
} }