PathFinder AI (VERY BUGED)

This commit is contained in:
iryupnik 2020-03-23 14:20:07 +01:00
parent e1b52ca627
commit 2bdc403f30
4 changed files with 52 additions and 32 deletions

View File

@ -18,6 +18,10 @@ public class Cell {
this.y = y; this.y = y;
} }
public void setColor(Color color) {
this.color = color;
}
public int getX() { // get the X coordinate public int getX() { // get the X coordinate
return x; return x;
} }

View File

@ -10,25 +10,18 @@ import java.awt.*;
public class Npc extends Person { public class Npc extends Person {
private MapPainter mapPainter; private MapPainter mapPainter;
int x; private Color color;
int y;
Color color;
public Npc(int x, int y, Map map, MapPainter mapPainter) { public Npc(int x, int y, Map map, MapPainter mapPainter) {
super(x, y, map); super(x, y, map);
this.mapPainter = mapPainter; this.mapPainter = mapPainter;
this.color = Color.cyan; setColor(Color.cyan);
} }
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();
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)){
System.out.println(getX() + " " + getY()); WindowOfFreeMove.sleep(500);
System.out.println(direction);
// WindowOfFreeMove.sleep(500);
switch (direction){ switch (direction){
case UP: case UP:
moveUp(); moveUp();
@ -45,24 +38,26 @@ public class Npc extends Person {
} }
mapPainter.repaintCanvas(); mapPainter.repaintCanvas();
} }
System.out.println("-----------------------");
System.out.println("WTF ARE YOU?! " + getX() + " " + getY());
} }
public void moveUp() { private void moveUp() {
set(x, y--); int y = getY() - 1;
set(getX(), y);
} }
public void moveDown() { private void moveDown() {
set(x, y++); int y = getY() + 1;
set(getX(), y);
} }
public void moveLeft() { private void moveLeft() {
set(x--, y); int x = getX() - 1;
set(x, getY());
} }
public void moveRight() { private void moveRight() {
set(x++, y); int x = getX() + 1;
set(x, getY());
} }
@Override @Override

View File

@ -1,23 +1,34 @@
package ruscz.game.gameOfProd.Scripts; package ruscz.game.gameOfProd.Scripts;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.util.LinkedList; import java.util.LinkedList;
public class PathFinder { public class PathFinder {
private static final int MAX_STEPS = 200;
private Map map; private Map map;
private boolean foundWay; private boolean foundWay;
private int startX;
private int startY;
public PathFinder(Map map) { public PathFinder(Map map) {
this.map = map; this.map = map;
foundWay = false; foundWay = false;
} }
private LinkedList<Direction> prepareDirection(){
LinkedList<Direction> directions = new LinkedList<>();
directions.add(Direction.NONE);
return directions;
}
private Direction[] getAllWays(){ private Direction[] getAllWays(){
return new Direction[]{Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT}; 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> getWayWithoutCollision(int startX, int startY, int endX, int endY) {
int nowX = startX; int nowX = startX;
int nowY = startY; int nowY = startY;
LinkedList<Direction> directions = new LinkedList<>(); LinkedList<Direction> directions = new LinkedList<>();
@ -47,37 +58,41 @@ public class PathFinder {
} }
private LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList<Direction> directions) { private LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList<Direction> directions) {
// System.out.println(startX + " == " + endX + " | " + startY + " == " + endY); System.out.println(directions);
if (startX == endX && startY == endY){ if (startX == endX && startY == endY){
System.out.println("YAS!"); foundWay = true;
return directions; return directions;
} }
if (directions.toArray().length == MAX_STEPS){
return prepareDirection();
}
for (Direction direction: getAllWays()){ for (Direction direction: getAllWays()){
switch (direction){ switch (direction){
case UP: case UP:
if (!map.isBusyCell(startX, startY - 1) && directions.getLast() != Direction.DOWN){ if (!map.isBusyCell(startX, startY - 1) && startY - 1 != -1 && directions.getLast() != Direction.DOWN){
directions.add(Direction.UP); directions.add(Direction.UP);
startY--; startY--;
return getWayWithCollision(startX, startY, endX, endY, directions); return getWayWithCollision(startX, startY, endX, endY, directions);
} }
break; break;
case DOWN: case DOWN:
if (!map.isBusyCell(startX, startY + 1) && directions.getLast() != Direction.UP){ if (!map.isBusyCell(startX, startY + 1) & startY + 1 != WindowOfFreeMove.CANVAS_HEIGHT + 1 && directions.getLast() != Direction.UP){
directions.add(Direction.DOWN); directions.add(Direction.DOWN);
startY++; startY++;
return getWayWithCollision(startX, startY, endX, endY, directions); return getWayWithCollision(startX, startY, endX, endY, directions);
} }
break; break;
case LEFT: case LEFT:
if (!map.isBusyCell(startX - 1, startY) && directions.getLast() != Direction.RIGHT){ if (!map.isBusyCell(startX - 1, startY) && startX - 1 != -1 && directions.getLast() != Direction.RIGHT){
directions.add(Direction.LEFT); directions.add(Direction.LEFT);
startX--; startX--;
return getWayWithCollision(startX, startY, endX, endY, directions); return getWayWithCollision(startX, startY, endX, endY, directions);
} }
break; break;
case RIGHT: case RIGHT:
if (!map.isBusyCell(startX + 1, startY) && directions.getLast() != Direction.LEFT){ if (!map.isBusyCell(startX + 1, startY) && startX - 1 != WindowOfFreeMove.CANVAS_WIDTH + 1 && directions.getLast() != Direction.LEFT){
directions.add(Direction.RIGHT); directions.add(Direction.RIGHT);
startX++; startX++;
return getWayWithCollision(startX, startY, endX, endY, directions); return getWayWithCollision(startX, startY, endX, endY, directions);
@ -90,8 +105,13 @@ public class PathFinder {
} }
public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY){ public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY){
LinkedList<Direction> directions = new LinkedList<>(); this.startX = startX;
directions.add(Direction.NONE); this.startY = startY;
return getWayWithCollision(startX, startY, endX, endY, directions);
LinkedList<Direction> directions = getWayWithCollision(startX, startY, endX, endY, prepareDirection());
if (foundWay)
return directions;
else
return prepareDirection();
} }
} }

View File

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