From 0a3b9a8d919b6a6229c90a4c54845b01e5e48192 Mon Sep 17 00:00:00 2001 From: iryupnik Date: Fri, 20 Mar 2020 20:00:37 +0100 Subject: [PATCH] PathFinder AI (buged) --- .../java/ruscz/game/gameOfProd/Cells/Npc.java | 3 +- .../java/ruscz/game/gameOfProd/Map/Map.java | 5 +- .../game/gameOfProd/Scripts/PathFinder.java | 120 +++++++++++++++++- .../gameOfProd/Windows/WindowOfFreeMove.java | 8 +- 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 8b36885..7fee58a 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -9,7 +9,6 @@ import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.awt.*; public class Npc extends Person { - private int[] path; private MapPainter mapPainter; int x; int y; @@ -25,7 +24,7 @@ public class Npc extends Person { x = this.getX(); y = this.getY(); PathFinder pathFinder = new PathFinder(getMap()); - for (Direction direction: pathFinder.getWayBySimpleAI(getX(), getY(), endX, endY)){ + for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){ WindowOfFreeMove.sleep(500); switch (direction){ case UP: diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index 04eb41a..f846e16 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -40,10 +40,7 @@ public class Map { imageCell = new ImageCell(); TypeOfCell type; - if (isBlocked) - type = TypeOfCell.BLOCK; - else - type = TypeOfCell.EMPTY; + type = TypeOfCell.EMPTY; for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray.length; j++) { diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java index a4e805a..d704acb 100644 --- a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java @@ -1,14 +1,16 @@ package ruscz.game.gameOfProd.Scripts; -import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Map.Map; -import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; 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; @@ -16,8 +18,8 @@ public class PathFinder { } public LinkedList getWayBySimpleAI(int startX, int startY, int endX, int endY) { - int nowX = startX; - int nowY = startY; + nowX = startX; + nowY = startY; LinkedList directions = new LinkedList<>(); while (!foundWay) { if (nowX == endX && nowY == endY) { @@ -43,4 +45,114 @@ public class PathFinder { } return directions; } + + public LinkedList getWayWithCollision(int startX, int startY, int endX, int endY) { + nowX = startX; + nowY = startY; + LinkedList 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; + directions.add(Direction.UP); + } else { + directions.add(findOptimalStep(nowX, nowY, nowX, nowY - 1, directions)); + } + } + } + } + if (noWayFound) { + return new LinkedList<>(); + } + return directions; + } + + private Direction findOptimalStep(int nowX, int nowY, int blockX, int blockY, LinkedList 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; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index e22974c..0724be0 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -184,8 +184,8 @@ public class WindowOfFreeMove extends JFrame { mapPainter = new MapPainter(mapLinkedList, canvas); - Npc npc = new Npc(1,1, m5, mapPainter); - m5.setNpcs(npc); + Npc npc1 = new Npc(1,1, m5, mapPainter); + m5.setNpcs(npc1); setMapPainter(m5); @@ -207,8 +207,8 @@ public class WindowOfFreeMove extends JFrame { }); timer.start(); while (true){ - npc2.move(5,1); - npc2.move(1, 5); + npc2.move(6,1); + npc2.move(1, 6); } }