path find global class backup
This commit is contained in:
296
PathFinding.pde
296
PathFinding.pde
@@ -1,6 +1,9 @@
|
|||||||
import pathfinder.*;
|
import pathfinder.*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Graph gs;
|
Graph gs;
|
||||||
GraphNode[] gNodes, rNodes;
|
GraphNode[] gNodes, rNodes;
|
||||||
GraphEdge[] gEdges, exploredEdges;
|
GraphEdge[] gEdges, exploredEdges;
|
||||||
@@ -9,9 +12,47 @@ int pathAlgorithm = 3;
|
|||||||
float nodeSize;
|
float nodeSize;
|
||||||
IGraphSearch pathFinder;
|
IGraphSearch pathFinder;
|
||||||
GraphNode startNode, endNode; //put inside citizen-class
|
GraphNode startNode, endNode; //put inside citizen-class
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean selectMode = false; //put inside citizen-class, only for manual interference
|
boolean selectMode = false; //put inside citizen-class, only for manual interference
|
||||||
|
|
||||||
void nodeDisplay() {
|
|
||||||
|
void initPathFinding() {
|
||||||
|
img_nodes = createImage(img_houses.width, img_houses.height, ARGB);
|
||||||
|
nodeSize = 5.0f;
|
||||||
|
gs = new Graph();
|
||||||
|
|
||||||
|
makeGraphFromImage(gs, img_houses, int(img_houses.width * f_nodeResolution), int(img_houses.height * f_nodeResolution), true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gNodes = gs.getNodeArray();
|
||||||
|
//end = gNodes[(int) random(0, gNodes.length / 4)].id();
|
||||||
|
// do
|
||||||
|
// start = gNodes[(int) random((3 * gNodes.length) / 4, gNodes.length/4)].id();
|
||||||
|
//while (start == end);
|
||||||
|
|
||||||
|
gs.compact();
|
||||||
|
|
||||||
|
// Get arrays of both the nodes and edges used by the
|
||||||
|
// selected graph.
|
||||||
|
gNodes = gs.getNodeArray();
|
||||||
|
gEdges = gs.getAllEdgeArray();
|
||||||
|
// Create a path finder object based on the algorithm
|
||||||
|
pathFinder = makePathFinder(gs, pathAlgorithm);
|
||||||
|
usePathFinder(pathFinder);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void usePathFinder(IGraphSearch pf) {
|
||||||
|
pf.search(start, end, true);
|
||||||
|
rNodes = pf.getRoute();
|
||||||
|
exploredEdges = pf.getExaminedEdges();
|
||||||
|
}
|
||||||
|
|
||||||
|
void displayPathFinding() {
|
||||||
drawNodes();
|
drawNodes();
|
||||||
drawRoute(rNodes, color(200, 0, 0), 5.0f);
|
drawRoute(rNodes, color(200, 0, 0), 5.0f);
|
||||||
|
|
||||||
@@ -25,6 +66,121 @@ void nodeDisplay() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void mousePressed() {
|
||||||
|
startNode = gs.getNodeAt(mouseX, mouseY, 0, 10.0f);
|
||||||
|
if (startNode != null)
|
||||||
|
selectMode = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouseDragged() {
|
||||||
|
if (selectMode)
|
||||||
|
endNode = gs.getNodeAt(mouseX, mouseY, 0, 10.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouseReleased() {
|
||||||
|
if (selectMode && endNode!= null && startNode != null && startNode != endNode) {
|
||||||
|
start = startNode.id();
|
||||||
|
end = endNode.id();
|
||||||
|
usePathFinder(pathFinder);
|
||||||
|
}
|
||||||
|
selectMode = false;
|
||||||
|
startNode = endNode = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PImage img_nodes;
|
||||||
|
void makeGraphFromImage(Graph g, PImage map, int tilesX, int tilesY, boolean allowDiagonals) {
|
||||||
|
img_nodes.loadPixels();
|
||||||
|
for (int i = 0; i < img_nodes.pixels.length; i++) {
|
||||||
|
if (map.pixels[i] != -1) {
|
||||||
|
img_nodes.pixels[i] = int(color(0));
|
||||||
|
} else {
|
||||||
|
img_nodes.pixels[i] = int(color(255));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
img_nodes.updatePixels();
|
||||||
|
|
||||||
|
int dx = (img_nodes.width / tilesX) +1;
|
||||||
|
int dy = (img_nodes.height / tilesY) +1;
|
||||||
|
|
||||||
|
int sx = dx / 2, sy = dy / 2;
|
||||||
|
|
||||||
|
|
||||||
|
// use deltaX to avoid horizontal wrap around edges
|
||||||
|
int deltaX = tilesX + 1; // must be > tilesX
|
||||||
|
|
||||||
|
float hCost = dx, vCost = dy, dCost = sqrt(dx*dx + dy*dy);
|
||||||
|
float cost = 0;
|
||||||
|
int px, py, nodeID, col;
|
||||||
|
GraphNode aNode;
|
||||||
|
|
||||||
|
py = sy;
|
||||||
|
for (int y = 0; y < tilesY; y++) {
|
||||||
|
nodeID = deltaX * y + deltaX;
|
||||||
|
px = sx;
|
||||||
|
for (int x = 0; x < tilesX; x++) {
|
||||||
|
// Calculate the cost
|
||||||
|
col = img_nodes.get(px, py) & 0xFF;
|
||||||
|
cost = 1;
|
||||||
|
// If col is not black then create the node and edges
|
||||||
|
if (col != 0) {
|
||||||
|
aNode = new GraphNode(nodeID, px, py);
|
||||||
|
g.addNode(aNode);
|
||||||
|
if (x > 0) {
|
||||||
|
g.addEdge(nodeID, nodeID - 1, hCost * cost);
|
||||||
|
if (allowDiagonals) {
|
||||||
|
g.addEdge(nodeID, nodeID - deltaX - 1, dCost * cost);
|
||||||
|
g.addEdge(nodeID, nodeID + deltaX - 1, dCost * cost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (x < tilesX -1) {
|
||||||
|
g.addEdge(nodeID, nodeID + 1, hCost * cost);
|
||||||
|
if (allowDiagonals) {
|
||||||
|
g.addEdge(nodeID, nodeID - deltaX + 1, dCost * cost);
|
||||||
|
g.addEdge(nodeID, nodeID + deltaX + 1, dCost * cost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (y > 0)
|
||||||
|
g.addEdge(nodeID, nodeID - deltaX, vCost * cost);
|
||||||
|
if (y < tilesY - 1)
|
||||||
|
g.addEdge(nodeID, nodeID + deltaX, vCost * cost);
|
||||||
|
}
|
||||||
|
px += dx;
|
||||||
|
nodeID++;
|
||||||
|
}
|
||||||
|
py += dy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
IGraphSearch makePathFinder(Graph graph, int pathFinder) {
|
||||||
|
IGraphSearch pf = null;
|
||||||
|
float f = 1.0f;
|
||||||
|
switch(pathFinder) {
|
||||||
|
case 0:
|
||||||
|
pf = new GraphSearch_DFS(gs);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
pf = new GraphSearch_BFS(gs);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
pf = new GraphSearch_Dijkstra(gs);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
pf = new GraphSearch_Astar(gs, new AshCrowFlight(f));
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
pf = new GraphSearch_Astar(gs, new AshManhattan(f));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return pf;
|
||||||
|
}
|
||||||
|
|
||||||
void drawNodes(){
|
void drawNodes(){
|
||||||
pushStyle();
|
pushStyle();
|
||||||
noStroke();
|
noStroke();
|
||||||
@@ -109,141 +265,3 @@ void drawArrow(GraphNode fromNode, GraphNode toNode, float nodeRad, float arrowS
|
|||||||
vertex(ax + awidthx, ay + awidthy);
|
vertex(ax + awidthx, ay + awidthy);
|
||||||
endShape();
|
endShape();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void initPathFinding() {
|
|
||||||
img_nodes = createImage(img_houses.width, img_houses.height, ARGB);
|
|
||||||
nodeSize = 10.0f;
|
|
||||||
gs = new Graph();
|
|
||||||
makeGraphFromImage(gs, img_houses, 20, 20, true);
|
|
||||||
gNodes = gs.getNodeArray();
|
|
||||||
end = gNodes[(int) random(0, gNodes.length / 4)].id();
|
|
||||||
do
|
|
||||||
start = gNodes[(int) random((3 * gNodes.length) / 4, gNodes.length/4)].id();
|
|
||||||
while (start == end);
|
|
||||||
gs.compact();
|
|
||||||
|
|
||||||
// Get arrays of both the nodes and edges used by the
|
|
||||||
// selected graph.
|
|
||||||
gNodes = gs.getNodeArray();
|
|
||||||
gEdges = gs.getAllEdgeArray();
|
|
||||||
// Create a path finder object based on the algorithm
|
|
||||||
pathFinder = makePathFinder(gs, pathAlgorithm);
|
|
||||||
usePathFinder(pathFinder);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void mousePressed() {
|
|
||||||
startNode = gs.getNodeAt(mouseX, mouseY, 0, 30.0f);
|
|
||||||
if (startNode != null)
|
|
||||||
selectMode = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouseDragged() {
|
|
||||||
if (selectMode)
|
|
||||||
endNode = gs.getNodeAt(mouseX, mouseY, 0, 30.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouseReleased() {
|
|
||||||
if (selectMode && endNode!= null && startNode != null && startNode != endNode) {
|
|
||||||
start = startNode.id();
|
|
||||||
end = endNode.id();
|
|
||||||
usePathFinder(pathFinder);
|
|
||||||
}
|
|
||||||
selectMode = false;
|
|
||||||
startNode = endNode = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PImage img_nodes;
|
|
||||||
void makeGraphFromImage(Graph g, PImage map, int tilesX, int tilesY, boolean allowDiagonals) {
|
|
||||||
img_nodes.loadPixels();
|
|
||||||
for (int i = 0; i < img_nodes.pixels.length; i++) {
|
|
||||||
if (map.pixels[i] != -1) {
|
|
||||||
img_nodes.pixels[i] = int(color(0));
|
|
||||||
} else {
|
|
||||||
img_nodes.pixels[i] = int(color(255));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
img_nodes.updatePixels();
|
|
||||||
|
|
||||||
int dx = img_nodes.width / tilesX;
|
|
||||||
int dy = img_nodes.height / tilesY;
|
|
||||||
int sx = dx / 2, sy = dy / 2;
|
|
||||||
// use deltaX to avoid horizontal wrap around edges
|
|
||||||
int deltaX = tilesX + 1; // must be > tilesX
|
|
||||||
|
|
||||||
float hCost = dx, vCost = dy, dCost = sqrt(dx*dx + dy*dy);
|
|
||||||
float cost = 0;
|
|
||||||
int px, py, nodeID, col;
|
|
||||||
GraphNode aNode;
|
|
||||||
|
|
||||||
py = sy;
|
|
||||||
for (int y = 0; y < tilesY; y++) {
|
|
||||||
nodeID = deltaX * y + deltaX;
|
|
||||||
px = sx;
|
|
||||||
for (int x = 0; x < tilesX; x++) {
|
|
||||||
// Calculate the cost
|
|
||||||
col = img_nodes.get(px, py) & 0xFF;
|
|
||||||
cost = 1;
|
|
||||||
// If col is not black then create the node and edges
|
|
||||||
if (col != 0) {
|
|
||||||
aNode = new GraphNode(nodeID, px, py);
|
|
||||||
g.addNode(aNode);
|
|
||||||
if (x > 0) {
|
|
||||||
g.addEdge(nodeID, nodeID - 1, hCost * cost);
|
|
||||||
if (allowDiagonals) {
|
|
||||||
g.addEdge(nodeID, nodeID - deltaX - 1, dCost * cost);
|
|
||||||
g.addEdge(nodeID, nodeID + deltaX - 1, dCost * cost);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (x < tilesX -1) {
|
|
||||||
g.addEdge(nodeID, nodeID + 1, hCost * cost);
|
|
||||||
if (allowDiagonals) {
|
|
||||||
g.addEdge(nodeID, nodeID - deltaX + 1, dCost * cost);
|
|
||||||
g.addEdge(nodeID, nodeID + deltaX + 1, dCost * cost);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (y > 0)
|
|
||||||
g.addEdge(nodeID, nodeID - deltaX, vCost * cost);
|
|
||||||
if (y < tilesY - 1)
|
|
||||||
g.addEdge(nodeID, nodeID + deltaX, vCost * cost);
|
|
||||||
}
|
|
||||||
px += dx;
|
|
||||||
nodeID++;
|
|
||||||
}
|
|
||||||
py += dy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void usePathFinder(IGraphSearch pf) {
|
|
||||||
pf.search(start, end, true);
|
|
||||||
rNodes = pf.getRoute();
|
|
||||||
exploredEdges = pf.getExaminedEdges();
|
|
||||||
}
|
|
||||||
|
|
||||||
IGraphSearch makePathFinder(Graph graph, int pathFinder) {
|
|
||||||
IGraphSearch pf = null;
|
|
||||||
float f = 1.0f;
|
|
||||||
switch(pathFinder) {
|
|
||||||
case 0:
|
|
||||||
pf = new GraphSearch_DFS(gs);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
pf = new GraphSearch_BFS(gs);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
pf = new GraphSearch_Dijkstra(gs);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
pf = new GraphSearch_Astar(gs, new AshCrowFlight(f));
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
pf = new GraphSearch_Astar(gs, new AshManhattan(f));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return pf;
|
|
||||||
}
|
|
||||||
|
|||||||
23
citizen.pde
23
citizen.pde
@@ -1,8 +1,9 @@
|
|||||||
Citizen mensch;
|
Citizen[] mensch = new Citizen[3];
|
||||||
|
|
||||||
//debug settings
|
//settings
|
||||||
boolean b_smallerImage = true; //use a cropped, smaller version of the 1920x1080 image for faster development
|
boolean b_smallerImage = true; //use a cropped, smaller version of the 1920x1080 image for faster development
|
||||||
boolean b_loadSun = false; //load sun from web-api instead of local json file (local file is adressed to january 4th 2019. might implement a check that downloads today's information only once...
|
boolean b_loadSun = false; //load sun from web-api instead of local json file (local file is adressed to january 4th 2019. might implement a check that downloads today's information only once...
|
||||||
|
float f_nodeResolution = 0.2; //defines density of path-finding nodes, multiplied with resolution
|
||||||
|
|
||||||
//void mouseClicked(){
|
//void mouseClicked(){
|
||||||
//mensch.goTo(112);
|
//mensch.goTo(112);
|
||||||
@@ -80,8 +81,16 @@ void setup() {
|
|||||||
initWeather();
|
initWeather();
|
||||||
|
|
||||||
|
|
||||||
mensch = new Citizen(59, "Pe");
|
|
||||||
mensch.spawn(59);
|
mensch[0] = new Citizen(59, "Pe");
|
||||||
|
mensch[1] = new Citizen(121, "Paul");
|
||||||
|
mensch[2] = new Citizen(18, "Freddy");
|
||||||
|
|
||||||
|
for(int i = 0; i < mensch.length; i++){
|
||||||
|
mensch[i].spawn(int(random(0,spawnAreas.size())));
|
||||||
|
}
|
||||||
|
|
||||||
|
//mensch[2].spawn(59);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -97,9 +106,11 @@ void draw() {
|
|||||||
spawn.display();
|
spawn.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeDisplay();
|
displayPathFinding();
|
||||||
|
|
||||||
mensch.display();
|
for (int i = 0; i < mensch.length; i++){
|
||||||
|
if(mensch[i].b_linked) mensch[i].display();
|
||||||
|
}
|
||||||
|
|
||||||
UI();
|
UI();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user