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) {
// System.out.println("NPC: " + getX() + " == " + endX + " | " + getY() + " == " + endY);
x = this.getX();
y = this.getY();
PathFinder pathFinder = new PathFinder(getMap());
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){
case UP:
moveUp();
@ -42,6 +45,8 @@ public class Npc extends Person {
}
mapPainter.repaintCanvas();
}
System.out.println("-----------------------");
System.out.println("WTF ARE YOU?! " + getX() + " " + getY());
}
public void moveUp() {

View File

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

View File

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

View File

@ -1,5 +1,5 @@
package ruscz.game.gameOfProd.Scripts;
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 {
private Map map;
private boolean noWayFound;
private boolean foundWay;
private boolean dontXMove;
private int nowX;
private int nowY;
public PathFinder(Map map) {
this.map = map;
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) {
nowX = startX;
nowY = startY;
int nowX = startX;
int nowY = startY;
LinkedList<Direction> directions = new LinkedList<>();
while (!foundWay) {
if (nowX == endX && nowY == endY) {
@ -46,113 +46,52 @@ public class PathFinder {
return directions;
}
public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY) {
nowX = startX;
nowY = startY;
LinkedList<Direction> directions = new LinkedList<>();
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;
private LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList<Direction> directions) {
// System.out.println(startX + " == " + endX + " | " + startY + " == " + endY);
if (startX == endX && startY == endY){
System.out.println("YAS!");
return directions;
}
for (Direction direction: getAllWays()){
switch (direction){
case UP:
if (!map.isBusyCell(startX, startY - 1) && directions.getLast() != Direction.DOWN){
directions.add(Direction.UP);
} else {
directions.add(findOptimalStep(nowX, nowY, nowX, nowY - 1, directions));
startY--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
}
break;
case DOWN:
if (!map.isBusyCell(startX, startY + 1) && directions.getLast() != Direction.UP){
directions.add(Direction.DOWN);
startY++;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
case LEFT:
if (!map.isBusyCell(startX - 1, startY) && directions.getLast() != Direction.RIGHT){
directions.add(Direction.LEFT);
startX--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
case RIGHT:
if (!map.isBusyCell(startX + 1, startY) && directions.getLast() != Direction.LEFT){
directions.add(Direction.RIGHT);
startX++;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
}
}
if (noWayFound) {
return new LinkedList<>();
}
return directions;
directions.removeLast();
return getWayWithCollision(startX, startY, endX, endY, directions);
}
private Direction findOptimalStep(int nowX, int nowY, int blockX, int blockY, LinkedList<Direction> directions) {
if (nowX == blockX) {
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:
this.nowY--;
dontXMove = false;
dir = Direction.UP;
break;
case DOWN:
this.nowY++;
dontXMove = false;
dir = Direction.DOWN;
break;
case LEFT:
this.nowX--;
dontXMove = true;
dir = Direction.LEFT;
break;
case RIGHT:
this.nowX++;
dontXMove = true;
dir = Direction.RIGHT;
break;
default:
dir = null;
}
return dir;
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;
canvas.repaint();
Npc npc2 = new Npc(1,1, m1, mapPainter);
m1.setNpcs(npc2);
Npc npc2 = new Npc(1,26, m6, mapPainter);
m6.setNpcs(npc2);
Timer timer = new Timer(10, new ActionListener() {
@Override
@ -213,8 +213,7 @@ public class WindowOfFreeMove extends JFrame {
});
timer.start();
while (true){
npc2.move(6,1);
npc2.move(1, 6);
npc2.move(9,34);
}
}