Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
All changes I made so far
  • Loading branch information
rjm11010 committed May 1, 2018
1 parent 5a7f0e0 commit e41e67a
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 40 deletions.
56 changes: 28 additions & 28 deletions pathfinding/AbstractNode.java
@@ -1,4 +1,4 @@
/*
/*
Copyright (C) 2012 http://software-talk.org/ (developer@software-talk.org)
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -46,40 +46,40 @@ package pathfinding;
public abstract class AbstractNode {

/** costs to move sideways from one square to another. */
protected static final int BASICMOVEMENTCOST = 10;
protected static final float BASICMOVEMENTCOST = 10;
/** costs to move diagonally from one square to another. */
protected static final int DIAGONALMOVEMENTCOST = 14;
protected static final float DIAGONALMOVEMENTCOST = 14;

private int xPosition;
private int yPosition;
private boolean walkable;
protected float xPosition;
protected float yPosition;
protected boolean walkable;

// for pathfinding:

/** the previous AbstractNode of this one on the currently calculated path. */
private AbstractNode previous;
protected AbstractNode previous;

/** weather or not the move from previous to this AbstractNode is diagonally. */
private boolean diagonally;
protected boolean diagonally;

/** optional extra penalty. */
private int movementPanelty;
protected float movementPanelty;

//private int fCosts; // g + h costs
//private float fCosts; // g + h costs

/** calculated costs from start AbstractNode to this AbstractNode. */
private int gCosts;
protected float gCosts;

/** estimated costs to get from this AbstractNode to end AbstractNode. */
private int hCosts;
protected float hCosts;

/**
* constructs a walkable AbstractNode with given coordinates.
*
* @param xPosition
* @param yPosition
*/
public AbstractNode(int xPosition, int yPosition) {
public AbstractNode(float xPosition, float yPosition) {
this.xPosition = xPosition;
this.yPosition = yPosition;
this.walkable = true;
Expand Down Expand Up @@ -112,22 +112,22 @@ public abstract class AbstractNode {
* @param x
* @param y
*/
public void setCoordinates(int x, int y) {
public void setCoordinates(float x, float y) {
this.xPosition = x;
this.yPosition = y;
}

/**
* @return the xPosition
*/
public int getxPosition() {
public float getxPosition() {
return xPosition;
}

/**
* @return the yPosition
*/
public int getyPosition() {
public float getyPosition() {
return yPosition;
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public abstract class AbstractNode {
*
* @param movementPanelty the movementPanelty to set
*/
public void setMovementPanelty(int movementPanelty) {
public void setMovementPanelty(float movementPanelty) {
this.movementPanelty = movementPanelty;
}

Expand All @@ -177,7 +177,7 @@ public abstract class AbstractNode {
*
* @return the fCosts
*/
public int getfCosts() {
public float getfCosts() {
return gCosts + hCosts;
}

Expand All @@ -186,7 +186,7 @@ public abstract class AbstractNode {
*
* @return the gCosts
*/
public int getgCosts() {
public float getgCosts() {
return gCosts;
}

Expand All @@ -196,7 +196,7 @@ public abstract class AbstractNode {
*
* @param gCosts the gCosts to set
*/
private void setgCosts(int gCosts) {
private void setgCosts(float gCosts) {
this.gCosts = gCosts + movementPanelty;
}

Expand All @@ -208,7 +208,7 @@ public abstract class AbstractNode {
* @param previousAbstractNode
* @param basicCost
*/
public void setgCosts(AbstractNode previousAbstractNode, int basicCost) {
public void setgCosts(AbstractNode previousAbstractNode, float basicCost) {
setgCosts(previousAbstractNode.getgCosts() + basicCost);
}

Expand Down Expand Up @@ -244,7 +244,7 @@ public abstract class AbstractNode {
* @param previousAbstractNode
* @return gCosts
*/
public int calculategCosts(AbstractNode previousAbstractNode) {
public float calculategCosts(AbstractNode previousAbstractNode) {
if (diagonally) {
return (previousAbstractNode.getgCosts()
+ DIAGONALMOVEMENTCOST + movementPanelty);
Expand All @@ -261,7 +261,7 @@ public abstract class AbstractNode {
* @param movementCost costs from previous AbstractNode to this AbstractNode.
* @return gCosts
*/
public int calculategCosts(AbstractNode previousAbstractNode, int movementCost) {
public float calculategCosts(AbstractNode previousAbstractNode, float movementCost) {
return (previousAbstractNode.getgCosts() + movementCost + movementPanelty);
}

Expand All @@ -270,7 +270,7 @@ public abstract class AbstractNode {
*
* @return the hCosts
*/
public int gethCosts() {
public float gethCosts() {
return hCosts;
}

Expand All @@ -279,7 +279,7 @@ public abstract class AbstractNode {
*
* @param hCosts the hCosts to set
*/
protected void sethCosts(int hCosts) {
protected void sethCosts(float hCosts) {
this.hCosts = hCosts;
}

Expand All @@ -295,7 +295,7 @@ public abstract class AbstractNode {
/*
* @return the movementPanelty
*/
private int getMovementPanelty() {
private float getMovementPanelty() {
return movementPanelty;
}

Expand Down Expand Up @@ -343,8 +343,8 @@ public abstract class AbstractNode {
@Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + this.xPosition;
hash = 17 * hash + this.yPosition;
hash = 17 * hash + (int)this.xPosition;
hash = 17 * hash + (int)this.yPosition;
return hash;
}

Expand Down
8 changes: 4 additions & 4 deletions pathfinding/ExampleNode.java
@@ -1,4 +1,4 @@
/*
/*
Copyright (C) 2012 http://software-talk.org/ (developer@software-talk.org)
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -29,9 +29,9 @@ public class ExampleNode extends AbstractNode {
}

public void sethCosts(AbstractNode endNode) {
this.sethCosts((absolute(this.getxPosition() - endNode.getxPosition())
+ absolute(this.getyPosition() - endNode.getyPosition()))
* BASICMOVEMENTCOST);
// this.sethCosts((absolute(this.getxPosition() - endNode.getxPosition())
// + absolute(this.getyPosition() - endNode.getyPosition()))
// * BASICMOVEMENTCOST);
}

private int absolute(int a) {
Expand Down
4 changes: 2 additions & 2 deletions pathfinding/ExampleUsage.java
@@ -1,4 +1,4 @@
/*
/*
Copyright (C) 2012 http://software-talk.org/ (developer@software-talk.org)
This program is free software: you can redistribute it and/or modify
Expand All @@ -21,7 +21,7 @@ import java.util.List;

/**
* A simple example for the usage of this package.
*
*
* @see ExampleFactory
* @see ExampleNode
*/
Expand Down
16 changes: 10 additions & 6 deletions pathfinding/Map.java
Expand Up @@ -199,30 +199,34 @@ public class Map<T extends AbstractNode> {
// TODO check input
openList = new LinkedList<T>();
closedList = new LinkedList<T>();
openList.add(nodes[oldX][oldY]); // add starting node to open list
openList.add(nodes[oldX][oldY]); // TODO: add starting node to open list

done = false;
T current;
while (!done) {
current = lowestFInOpen(); // get node with lowest fCosts from openList
current = lowestFInOpen(); // TODO: get node with lowest fCosts from openList
closedList.add(current); // add current node to closed list
openList.remove(current); // delete current node from open list

if ((current.getxPosition() == newX)
&& (current.getyPosition() == newY)) { // found goal
return calcPath(nodes[oldX][oldY], current);
return calcPath(nodes[oldX][oldY], current); // NOTE: Looks like he back tracks from end to start t oproduce path
}

// for all adjacent nodes:
List<T> adjacentNodes = getAdjacent(current);
List<T> adjacentNodes = getAdjacent(current); // TODO:
for (int i = 0; i < adjacentNodes.size(); i++) {
T currentAdj = adjacentNodes.get(i);
if (!openList.contains(currentAdj)) { // node is not in openList
// TODO: Need to edit logic after this point
// NOTE: May not need any of this here
currentAdj.setPrevious(current); // set current node as previous for this node
currentAdj.sethCosts(nodes[newX][newY]); // set h costs of this node (estimated costs to goal)
currentAdj.setgCosts(current); // set g costs of this node (costs from start to this node)
// NOTE: This seems okay
openList.add(currentAdj); // add node to openList
} else { // node is in openList
// TODO: Implement these cost functions
if (currentAdj.getgCosts() > currentAdj.calculategCosts(current)) { // costs from current node are cheaper than previous costs
currentAdj.setPrevious(current); // set current node as previous for this node
currentAdj.setgCosts(current); // set g costs of this node (costs from start to this node)
Expand Down Expand Up @@ -285,8 +289,8 @@ public class Map<T extends AbstractNode> {
*/
private List<T> getAdjacent(T node) {
// TODO make loop
int x = node.getxPosition();
int y = node.getyPosition();
int x = (int)node.getxPosition();
int y = (int)node.getyPosition();
List<T> adj = new LinkedList<T>();

T temp;
Expand Down

0 comments on commit e41e67a

Please sign in to comment.