Initial version of pathfinding

This commit is contained in:
cognitivegears 2014-04-21 21:44:36 -05:00
parent a660adb81c
commit a8e68af6db
7 changed files with 524 additions and 127 deletions

View File

@ -3,6 +3,7 @@ package com.droidquest.avatars;
import com.droidquest.Room; import com.droidquest.Room;
import com.droidquest.RoomDisplay; import com.droidquest.RoomDisplay;
import com.droidquest.items.*; import com.droidquest.items.*;
import com.droidquest.pathfinder.Node;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -324,29 +325,9 @@ public class GameCursor extends Player {
return level.cheatmode; return level.cheatmode;
} }
public void Animate() {
if (automove == 1 && room == null) { @Override
automove = 0; protected void animateCharacter(int dx, int dy) {
}
if (automove == 1) {
int dx = autoX - x;
int dy = autoY - y;
if (dx == 0 && dy == 0) {
automove = 0;
return;
}
if (dx < -28) {
dx = -28;
}
if (dx > 28) {
dx = 28;
}
if (dy < -32) {
dy = -32;
}
if (dy > 32) {
dy = 32;
}
walk = 1 - walk; walk = 1 - walk;
if (dx == 0) { if (dx == 0) {
if (dy < 0) { if (dy < 0) {
@ -364,20 +345,10 @@ public class GameCursor extends Player {
currentIcon = icons[6 + walk].getImage(); currentIcon = icons[6 + walk].getImage();
} }
} }
if (dx > 0) {
moveRight(dx);
} }
if (dx < 0) {
moveLeft(-dx); @Override
} protected void autoMoveFull() {
if (dy > 0) {
moveDown(dy);
}
if (dy < 0) {
moveUp(-dy);
}
}
if (automove == 2) {
walk = 1 - walk; walk = 1 - walk;
if (autoX > 0) { if (autoX > 0) {
currentIcon = icons[6 + walk].getImage(); currentIcon = icons[6 + walk].getImage();
@ -398,7 +369,7 @@ public class GameCursor extends Player {
currentIcon = icons[0 + walk].getImage(); currentIcon = icons[0 + walk].getImage();
moveUp(-autoY); moveUp(-autoY);
} }
}
} }
public GenericRobot PlayerInRobot(GenericRobot robot) { public GenericRobot PlayerInRobot(GenericRobot robot) {

View File

@ -93,8 +93,8 @@ public class PaintBrush extends Player {
paintMats[1] = Material.FindSimiliar(new Material(Color.green, false, false)); paintMats[1] = Material.FindSimiliar(new Material(Color.green, false, false));
Item robot = null; Item robot = null;
for(Item item : level.items) { for (Item item : level.items) {
if(item instanceof OrangeRobot) { if (item instanceof OrangeRobot) {
robot = item; robot = item;
} }
} }
@ -104,14 +104,14 @@ public class PaintBrush extends Player {
paintMats[2] = Material.FindSimiliar(new RobotBlocker(robot, new Color(255, 128, 0))); paintMats[2] = Material.FindSimiliar(new RobotBlocker(robot, new Color(255, 128, 0)));
for (Item item : level.items) { for (Item item : level.items) {
if(item instanceof WhiteRobot) { if (item instanceof WhiteRobot) {
robot = item; robot = item;
} }
} }
paintMats[3] = Material.FindSimiliar(new RobotBlocker(robot, Color.white)); paintMats[3] = Material.FindSimiliar(new RobotBlocker(robot, Color.white));
for(Item item : level.items) { for (Item item : level.items) {
if(item instanceof BlueRobot) { if (item instanceof BlueRobot) {
robot = item; robot = item;
} }
} }
@ -213,6 +213,7 @@ public class PaintBrush extends Player {
return false; return false;
} }
@Override
public void moveUp(boolean nudge) { public void moveUp(boolean nudge) {
int dist = 32; int dist = 32;
if (nudge) { if (nudge) {
@ -231,6 +232,7 @@ public class PaintBrush extends Player {
} }
} }
@Override
public void moveDown(boolean nudge) { public void moveDown(boolean nudge) {
int dist = 32; int dist = 32;
if (nudge) { if (nudge) {
@ -249,6 +251,20 @@ public class PaintBrush extends Player {
} }
} }
// @Override
// protected void findPath(int startX, int startY, int endX, int endY) {
// // The paintbrush can go anywhere
//// autoPath = new ArrayList<Node>();
//// autoPath.add(new Node(endX * 28, endY * 32));
// autoX = endX * 28;
// autoY = endY * 28;
// autoX -= autoX % 2; // Even numbered pixel only!
// autoY -= autoY % 2;
// automove = 1;
//
// }
@Override
public void moveLeft(boolean nudge) { public void moveLeft(boolean nudge) {
int dist = 28; int dist = 28;
if (nudge) { if (nudge) {
@ -267,6 +283,7 @@ public class PaintBrush extends Player {
} }
} }
@Override
public void moveRight(boolean nudge) { public void moveRight(boolean nudge) {
int dist = 28; int dist = 28;
if (nudge) { if (nudge) {

View File

@ -225,6 +225,7 @@ public class SolderingPen extends Device implements Avatar {
CheckPort(); CheckPort();
} }
@Override
public void Animate() { public void Animate() {
Room tempRoom = room; Room tempRoom = room;
super.Animate(); super.Animate();

View File

@ -5,6 +5,8 @@ import com.droidquest.Wire;
import com.droidquest.devices.Device; import com.droidquest.devices.Device;
import com.droidquest.levels.Level; import com.droidquest.levels.Level;
import com.droidquest.materials.ChipTrash; import com.droidquest.materials.ChipTrash;
import com.droidquest.pathfinder.Node;
import com.droidquest.pathfinder.Pathfinder;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -15,6 +17,7 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
public class Item implements Serializable, Cloneable { public class Item implements Serializable, Cloneable {
public transient static Level level; public transient static Level level;
@ -28,6 +31,7 @@ public class Item implements Serializable, Cloneable {
public transient int autoX; // Destination of automovement public transient int autoX; // Destination of automovement
public transient int autoY; // Destination of automovement public transient int autoY; // Destination of automovement
public Room InternalRoom = null; // Room inside this item, if any. public Room InternalRoom = null; // Room inside this item, if any.
public transient ArrayList<Node> autoPath = new ArrayList<Node>();
protected int repeating = 0; // Keyboard repeat. protected int repeating = 0; // Keyboard repeat.
public int charge = 0; // Battery Charge of this item, if any. public int charge = 0; // Battery Charge of this item, if any.
@ -225,6 +229,11 @@ public class Item implements Serializable, Cloneable {
return false; return false;
} }
protected void findPath(int startX, int startY, int endX, int endY) {
Pathfinder pf = new Pathfinder(room);
autoPath = pf.search(startX, startY, endX, endY, this);
}
public void MouseClick(MouseEvent e) { public void MouseClick(MouseEvent e) {
int button = 0; int button = 0;
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
@ -236,12 +245,24 @@ public class Item implements Serializable, Cloneable {
if (button == 1) { if (button == 1) {
if (e.getClickCount() == 1) { if (e.getClickCount() == 1) {
autoX = e.getX() - width / 2; int endX = e.getX() / 28;
autoY = e.getY() - height / 2; int endY = e.getY() / 32;
int startX = x / 28;
int startY = y / 32;
findPath(startX, startY, endX, endY);
if(autoPath != null && autoPath.size() > 0) {
Node next = autoPath.remove(0);
autoX = next.getX();
autoY = next.getY();
autoX -= autoX % 2; // Even numbered pixel only! autoX -= autoX % 2; // Even numbered pixel only!
autoY -= autoY % 2; autoY -= autoY % 2;
automove = 1; automove = 1;
} }
}
else if (e.getClickCount() == 2) { else if (e.getClickCount() == 2) {
int dx = e.getX() - width / 2 - x; int dx = e.getX() - width / 2 - x;
int dy = e.getY() - height / 2 - y; int dy = e.getY() - height / 2 - y;
@ -436,6 +457,10 @@ public class Item implements Serializable, Cloneable {
moveRight(dist); moveRight(dist);
} }
protected void animateCharacter(int dx, int dy) {
}
public void Animate() { public void Animate() {
if (automove == 1 && room == null) { if (automove == 1 && room == null) {
automove = 0; automove = 0;
@ -443,8 +468,21 @@ public class Item implements Serializable, Cloneable {
if (automove == 1) { if (automove == 1) {
int dx = autoX - x; int dx = autoX - x;
int dy = autoY - y; int dy = autoY - y;
if (dx == 0 && dy == 0) { if (dx == 0 && dy == 0) {
if(autoPath.size() > 0) {
Node next = autoPath.remove(0);
autoX = next.getX();
autoY = next.getY();
autoX -= autoX % 2; // Even numbered pixel only!
autoY -= autoY % 2;
dx = autoX - x;
dy = autoY - y;
}
if(dx == 0 && dy == 0) {
automove = 0; automove = 0;
return;
}
} }
if (dx < -28) { if (dx < -28) {
dx = -28; dx = -28;
@ -458,6 +496,9 @@ public class Item implements Serializable, Cloneable {
if (dy > 32) { if (dy > 32) {
dy = 32; dy = 32;
} }
animateCharacter(dx, dy);
if (dx > 0) { if (dx > 0) {
moveRight(dx); moveRight(dx);
} }
@ -472,6 +513,11 @@ public class Item implements Serializable, Cloneable {
} }
} }
if (automove == 2) { if (automove == 2) {
autoMoveFull();
}
}
protected void autoMoveFull() {
if (autoX > 0) { if (autoX > 0) {
moveRight(autoX); moveRight(autoX);
} }
@ -485,7 +531,6 @@ public class Item implements Serializable, Cloneable {
moveUp(-autoY); moveUp(-autoY);
} }
} }
}
public void Decorate() { public void Decorate() {
} }

View File

@ -1,6 +1,7 @@
package com.droidquest.materials; package com.droidquest.materials;
import com.droidquest.RoomDisplay; import com.droidquest.RoomDisplay;
import com.droidquest.avatars.PaintBrush;
import com.droidquest.items.Item; import com.droidquest.items.Item;
import com.droidquest.levels.Level; import com.droidquest.levels.Level;
@ -62,6 +63,10 @@ public class Material implements Serializable, Cloneable {
} }
public boolean Passable(Item item) { public boolean Passable(Item item) {
// The PaintBrush can pass anything
if(item instanceof PaintBrush) {
return true;
}
return passable; return passable;
} }

View File

@ -0,0 +1,180 @@
package com.droidquest.pathfinder;
import com.droidquest.materials.Material;
/**
* Represents a Node in a path, using the A* algorithm.
*/
public class Node implements Cloneable {
private int x = -1;
private int y = -1;
private Material material = null;
private int gCost = 0;
private int hCost = 0;
private int fCost = 0;
Node parent = null;
/**
* Create a new, empty Node
*/
public Node() {
this(-1, -1, null, null);
}
/**
* Create a new Node with given coordinates
* @param x Coordinate (of the room array, not screen location)
* @param y Coordinate (of the room array, not screen location)
*/
public Node(int x, int y) {
this(x, y, null, null);
}
/**
* Create a new room Node with the given coordinates and material
* @param x Coordinate (of the room array, not screen location)
* @param y Coordinate (of the room array, not screen location)
* @param mat Material at the Node location
*/
public Node(int x, int y, Material mat) {
this(x, y, null, mat);
}
/**
* Create a new room Node with a given parent Node (for pathing)
* @param x Coordinate (of the room array, not screen location)
* @param y Coordinate (of the room array, not screen location)
* @param parent current Parent node
*/
public Node(int x, int y, Node parent) {
this(x, y, parent, null);
}
/**
* Create a new room node with a given parent Node and Material
* @param x Coordinate (of the room array, not screen location)
* @param y Coordinate (of the room array, not screen location)
* @param parent current Parent node
* @param mat Material at the Node location
*/
public Node(int x, int y, Node parent, Material mat) {
this.x = x;
this.y = y;
this.parent = parent;
this.material = mat;
}
/**
* Retrieve the current room location X coordinate (room array, not screen location)
* @return X coordinate
*/
public int getX() {
return x;
}
/**
* Set the current room location X coordinate (room array, not screen location)
* @param x coordinate
*/
public void setX(int x) {
this.x = x;
}
/**
* Retrieve the current room location Y coordinate (room array, not screen location)
* @return y coordinate
*/
public int getY() {
return y;
}
/**
* Set the current room Y coordinate (room array, not screen location)
* @param y coordinate
*/
public void setY(int y) {
this.y = y;
}
/**
* Retrieve the currently set Material for the Node (or null if not set)
* @return Material
* @see com.droidquest.materials.Material
*/
public Material getMaterial() {
return material;
}
/**
* Set the Material for the Node
* @param material Material
* @see com.droidquest.materials.Material
*/
public void setMaterial(Material material) {
this.material = material;
}
/**
* Create a clone of this Node
* @return Node clone (as Object)
*/
public Object clone() {
try {
return super.clone();
}
catch(CloneNotSupportedException cnse) {
System.out.println("Clone not supported!");
return this;
}
}
/**
* Set the gCost (cost to the current Node from the origin)
* @return int cost
*/
public int getgCost() {
return gCost;
}
/**
* Set the gCost (cost to the current Node from the origin)
* @param gCost int cost
*/
public void setgCost(int gCost) {
this.gCost = gCost;
}
/**
* Retrieves the hCost (heuristic cost) from the current Node to the end Node
* @return int cost
*/
public int gethCost() {
return hCost;
}
/**
* Sets the hCost (heuristic cost) from the current Node to the end Node
* @param hCost int cost
*/
public void sethCost(int hCost) {
this.hCost = hCost;
}
/**
* Retrieves the fCost (full cost) for the Node
* @return int cost
*/
public int getfCost() {
return fCost;
}
/**
* Sets the fCost (full cost) for the Node
* @param fCost int cost
*/
public void setfCost(int fCost) {
this.fCost = fCost;
}
}

View File

@ -0,0 +1,178 @@
package com.droidquest.pathfinder;
import com.droidquest.Room;
import com.droidquest.items.Item;
import java.util.ArrayList;
import java.util.Collections;
/**
* Finds a path between two places on the map, using the A* algorithm
*/
public class Pathfinder {
private ArrayList<ArrayList<Node>> nodeList = new ArrayList<ArrayList<Node>>();
private ArrayList<Node> openList = new ArrayList<Node>();
private ArrayList<Node> closedList = new ArrayList<Node>();
/**
* Begin a search for a path. Returns an empty ArrayList if no path found.
* @param startX int starting X coordinate (not screen location)
* @param startY int starting Y coordinate (not screen location)
* @param endX int ending X coordinate (not screen location)
* @param endY int ending Y coordinate (not screen location)
* @param player Item player used to verify whether a Node is passable.
* @return ArrayList of Nodes representing the path, or empty list
*/
public ArrayList<Node> search(int startX, int startY, int endX, int endY, Item player) {
ArrayList<Node> results = new ArrayList<Node>();
if(startY < 0 || startY > nodeList.size() || startX < 0 || startX > nodeList.get(startY).size()) {
System.out.println("Starting node outside of room bounds!");
return results;
}
if(endY < 0 || endY > nodeList.size() || endX < 0 || endX > nodeList.get(endY).size()) {
System.out.println("Ending node outside of room bounds!");
return results;
}
if(!nodeList.get(startY).get(startX).getMaterial().Passable(player)) {
System.out.println("Starting node isn't passable!");
return results;
}
if(!nodeList.get(endY).get(endX).getMaterial().Passable(player)) {
System.out.println("Ending node isn't passable!");
return results;
}
Node endNode = nodeList.get(endY).get(endX);
// Add nodes adjacent to starting node
Node startNode = nodeList.get(startY).get(startX);
startNode.setgCost(0);
startNode.sethCost(manhattanDistance(startNode, endNode));
startNode.setfCost(startNode.gethCost());
startNode.parent = null;
openList.add(startNode);
findLowestCost(results, endNode, player);
return results;
}
private void findLowestCost(ArrayList<Node> results, Node endNode, Item player) {
while(openList.size() > 0) {
Node lowestCost = null;
for (Node node : openList) {
if (lowestCost == null || node.getfCost() < lowestCost.getfCost()) {
lowestCost = node;
}
}
openList.remove(lowestCost);
closedList.add(lowestCost);
if (lowestCost != null && lowestCost == endNode) {
Node pathNode = lowestCost;
while (pathNode.parent != null) {
results.add(new Node(pathNode.getX() * 28, pathNode.getY() * 32));
pathNode = pathNode.parent;
}
Collections.reverse(results);
return;
}
addSurroundingNodes(lowestCost, endNode, player);
}
System.out.println("Could not find any nodes to process, no path.");
}
private void addSurroundingNodes(Node parent, Node endNode, Item player) {
if(parent == null) {
return;
}
int x = parent.getX();
int y = parent.getY();
for(int deltaY = -1; deltaY <= 1; deltaY++) {
for(int deltaX = -1; deltaX <=1; deltaX++) {
if(deltaX != 0 || deltaY != 0) {
int checkX = x + deltaX;
int checkY = y + deltaY;
if(checkY >= 0 && checkY < nodeList.size() && checkX >= 0 && checkX < nodeList.get(checkY).size() && nodeList.get(checkY).get(checkX).getMaterial().Passable(player)) {
Node currentNode = nodeList.get(checkY).get(checkX);
if(closedList.contains(currentNode)) {
continue;
}
int diagonalCost = 0;
if(deltaX != 0 && deltaY != 0) {
// Diagonal, check to make sure other squares are passable
if(!nodeList.get(y).get(checkX).getMaterial().Passable(player) || !nodeList.get(checkY).get(x).getMaterial().Passable(player)) {
diagonalCost = 500; // don't make impassible, just really expensive
}
}
if(openList.contains(currentNode)) {
// Set gCost (10 for orthoginal, 14 for diagonal)
int newgCost = parent.getgCost() + ((deltaX == 0 || deltaY == 0) ? 10 : 14) + diagonalCost;
if(newgCost < currentNode.getgCost()) {
currentNode.parent = parent;
currentNode.setgCost(newgCost);
currentNode.setfCost(newgCost + manhattanDistance(currentNode, endNode));
}
}
else {
openList.add(currentNode);
currentNode.parent = parent;
// Set gCost (10 for orthoginal, 14 for diagonal)
currentNode.setgCost(parent.getgCost() + ((deltaX == 0 || deltaY == 0) ? 10 : 14) + diagonalCost);
currentNode.sethCost(manhattanDistance(currentNode, endNode));
currentNode.setfCost(currentNode.getgCost() + currentNode.gethCost());
}
}
}
}
}
}
/**
* Create a new Pathfinder object and initialize with a Room
* @param room Room for path-finding.
* @see com.droidquest.Room
*/
public Pathfinder(Room room) {
// Generate node array for room
generate(room);
}
/**
* Generate the Node list for a Room. Generally should use new Pathfinder(room) instead of this method
* @param room Room for path-finding
*/
public void generate(Room room) {
for(int y = 0; y < room.RoomArray.length;y++) {
nodeList.add(new ArrayList<Node>());
for(int x=0;x<room.RoomArray[y].length;x++) {
nodeList.get(y).add(new Node(x, y, Room.level.materialAt(x, y, room)));
}
}
}
// Use the Manhattan distance as the heuristic
private int manhattanDistance(Node current, Node end) {
return 10 * Math.abs(end.getX()-current.getX()) + Math.abs(end.getY()-current.getY());
}
}