Skip to content

Commit

Permalink
Included new method called findDistToPositionInRange used by SCV's St…
Browse files Browse the repository at this point in the history
…rategy.
  • Loading branch information
rubensolv committed Jun 19, 2018
1 parent f1056cb commit 10f9311
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/ai/abstraction/pathfinding/AStarPathFinding.java
Expand Up @@ -250,5 +250,137 @@ void addToOpen(int x, int y, int newPos, int oldPos, int h) {
int manhattanDistance(int x, int y, int x2, int y2) {
return Math.abs(x-x2) + Math.abs(y-y2);
}

public int findDistToPositionInRange(Unit start, int targetpos, int range, GameState gs, ResourceUsage ru) {
PhysicalGameState pgs = gs.getPhysicalGameState();
int w = pgs.getWidth();
int h = pgs.getHeight();
if (free==null || free.length<w*h) {
free = new Boolean[pgs.getWidth()][pgs.getHeight()];
closed = new int[pgs.getWidth()*pgs.getHeight()];
open = new int[pgs.getWidth()*pgs.getHeight()];
heuristic = new int[pgs.getWidth()*pgs.getHeight()];
parents = new int[pgs.getWidth()*pgs.getHeight()];
inOpenOrClosed = new int[pgs.getWidth()*pgs.getHeight()];
cost = new int[pgs.getWidth()*pgs.getHeight()];
}
for(int y = 0, i = 0;y<pgs.getHeight();y++) {
for(int x = 0;x<w;x++,i++) {
free[x][y] = null;
closed[i] = -1;
inOpenOrClosed[i] = 0;
}
}
if (ru!=null) {
for(int pos:ru.getPositionsUsed()) {
free[pos%w][pos/w] = false;
}
}
int targetx = targetpos%w;
int targety = targetpos/w;
int sq_range = range*range;
int startPos = start.getY()*w + start.getX();

assert(targetx>=0);
assert(targetx<w);
assert(targety>=0);
assert(targety<h);
assert(start.getX()>=0);
assert(start.getX()<w);
assert(start.getY()>=0);
assert(start.getY()<h);

openinsert = 0;
open[openinsert] = startPos;
heuristic[openinsert] = manhattanDistance(start.getX(), start.getY(), targetx, targety);
parents[openinsert] = startPos;
inOpenOrClosed[startPos] = 1;
cost[startPos] = 0;
openinsert++;
// System.out.println("Looking for path from: " + start.getX() + "," + start.getY() + " to " + targetx + "," + targety);
while(openinsert>0) {

// debugging code:
/*
System.out.println("open: ");
for(int i = 0;i<openinsert;i++) {
System.out.print(" [" + (open[i]%w) + "," + (open[i]/w) + " -> "+ cost[open[i]] + "+" + heuristic[i] + "]");
}
System.out.println("");
for(int i = 0;i<h;i++) {
for(int j = 0;j<w;j++) {
if (j==start.getX() && i==start.getY()) {
System.out.print("s");
} else if (j==targetx && i==targety) {
System.out.print("t");
} else if (!free[j][i]) {
System.out.print("X");
} else {
if (inOpenOrClosed[j+i*w]==0) {
System.out.print(".");
} else {
System.out.print("o");
}
}
}
System.out.println("");
}
*/
iterations++;
openinsert--;
int pos = open[openinsert];
int parent = parents[openinsert];
if (closed[pos]!=-1) continue;
closed[pos] = parent;

int x = pos%w;
int y = pos/w;

if (((x-targetx)*(x-targetx)+(y-targety)*(y-targety))<=sq_range) {
// path found, backtrack:
int last = pos;
//System.out.println("- Path from " + start.getX() + "," + start.getY() + " to " + targetpos%w + "," + targetpos/w + " (range " + range + ") in " + iterations + " iterations");
int temp = 0;
while(parent!=pos) {
last = pos;
pos = parent;
parent = closed[pos];
accumlength++;
temp++;
//System.out.println(" " + pos%w + "," + pos/w);
}
return temp;

}
if (y>0 && inOpenOrClosed[pos-w] == 0) {
if (free[x][y-1]==null) free[x][y-1]=gs.free(x, y-1);
assert(free[x][y-1]!=null);
if (free[x][y-1]) {
addToOpen(x,y-1,pos-w,pos,manhattanDistance(x, y-1, targetx, targety));
}
}
if (x<pgs.getWidth()-1 && inOpenOrClosed[pos+1] == 0) {
if (free[x+1][y]==null) free[x+1][y]=gs.free(x+1, y);
assert(free[x+1][y]!=null);
if (free[x+1][y]) {
addToOpen(x+1,y,pos+1,pos,manhattanDistance(x+1, y, targetx, targety));
}
}
if (y<pgs.getHeight()-1 && inOpenOrClosed[pos+w] == 0) {
if (free[x][y+1]==null) free[x][y+1]=gs.free(x, y+1);
assert(free[x][y+1]!=null);
if (free[x][y+1]) {
addToOpen(x,y+1,pos+w,pos,manhattanDistance(x, y+1, targetx, targety));
}
}
if (x>0 && inOpenOrClosed[pos-1] == 0) {
if (free[x-1][y]==null) free[x-1][y]=gs.free(x-1, y);
assert(free[x-1][y]!=null);
if (free[x-1][y]) {
addToOpen(x-1,y,pos-1,pos,manhattanDistance(x-1, y, targetx, targety));
}
}
}
return -1;
}
}

0 comments on commit 10f9311

Please sign in to comment.