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;
}
public void setColor(Color color) {
this.color = color;
}
public int getX() { // get the X coordinate
return x;
}

View File

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

View File

@ -1,23 +1,34 @@
package ruscz.game.gameOfProd.Scripts;
import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.util.LinkedList;
public class PathFinder {
private static final int MAX_STEPS = 200;
private Map map;
private boolean foundWay;
private int startX;
private int startY;
public PathFinder(Map map) {
this.map = map;
foundWay = false;
}
private LinkedList<Direction> prepareDirection(){
LinkedList<Direction> directions = new LinkedList<>();
directions.add(Direction.NONE);
return directions;
}
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> getWayWithoutCollision(int startX, int startY, int endX, int endY) {
int nowX = startX;
int nowY = startY;
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) {
// System.out.println(startX + " == " + endX + " | " + startY + " == " + endY);
System.out.println(directions);
if (startX == endX && startY == endY){
System.out.println("YAS!");
foundWay = true;
return directions;
}
if (directions.toArray().length == MAX_STEPS){
return prepareDirection();
}
for (Direction direction: getAllWays()){
switch (direction){
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);
startY--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
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);
startY++;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
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);
startX--;
return getWayWithCollision(startX, startY, endX, endY, directions);
}
break;
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);
startX++;
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){
LinkedList<Direction> directions = new LinkedList<>();
directions.add(Direction.NONE);
return getWayWithCollision(startX, startY, endX, endY, directions);
this.startX = startX;
this.startY = startY;
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;
canvas.repaint();
Npc npc2 = new Npc(1,26, m6, mapPainter);
Npc npc2 = new Npc(1,1, m6, mapPainter);
m6.setNpcs(npc2);
Timer timer = new Timer(10, new ActionListener() {
@ -213,7 +213,8 @@ public class WindowOfFreeMove extends JFrame {
});
timer.start();
while (true){
npc2.move(9,34);
npc2.move(9,9);
npc2.move(1,1);
}
}