static final int pdimX=120;
static final int pdimY=60;
final int[][] terrainGrid = new int[STUCTURE_FIELD_SIZEX][STUCTURE_FIELD_SIZEY];
final int gridFlood[][][]=new int[60][pdimX][pdimY];
final int floodCurrentID=0; //gets counted up for each new floodfill
//if target is on inaccebible floor, create a curcular floodfill, so units can walk towards it
void floodfill (int targetX,int targetY, int gridId)
{
//set the targetzone
int x=targetX;
int y=targetY;
//make the floodfillgrid
//Logger.out("steps:"+generateGrid());
int testvalue=0; //remove!
//check if target is on a valid square?
//create the basegrid
// grid = new int[pdimX][pdimY];
//reset all squares to high count (system copy array faster)
//System.arraycopy(gridInit, 0, grid, 0, gridInit.length);
for (int ix=0;ix<pdimX;ix++)
{
for (int iy=0;iy<pdimY;iy++)
{
//grid[ix][iy]=gridInit[ix][iy]; //set to a high value
gridFlood[gridId][ix][iy]=8192;//set to a high value
}
}
//checkif target is on inaccebile position, do a "no matter what" floodfill,
//so units can walk toward the destination
boolean dontCareForObstacles=false;
if(terrainGrid[targetX][targetY]==0) dontCareForObstacles=true;
//create the list of currently used squares
//int worklist[][]=new int[dimX*dimY][2]; //trying a lower dimension reduces the memory use..
int worklistLenght=0;
//int nextlist[][]=new int[dimX*dimY][2]; //trying a lower dimension reduces the memory use..
int nextlistLenght=0;
//workarrays
worklist=new int[pdimX*pdimY][2];
// Logger.out("worklist lenght:"+worklist.length);
nextlist=new int[pdimX*pdimY][2];
//set the targetpoint to 0
gridFlood[gridId][x][y]=0; //the targetsqare start with the lowest count
//targetnode is the first entry
worklist[1][0]=x;
worklist[1][1]=y;
worklistLenght=1;
int testsquare=0;
int xTemp=0;
int yTemp=0;
int xNow=0;
int yNow=0;
int countThrough=0;
boolean diagonal=false;
int walkdist=10; //10 for normal, 14 for diagonal
//TODO:
//when the target is on a building, make this building invisible, right now
//the trick is to have a little gap in 3x3 buildings, wich is just a workaround
//as long as squares active:
while(worklistLenght>0)
{
countThrough=1; //start with the first entry
nextlistLenght=0; //"erase" the nextlist
//for all in the worklist
while(countThrough<=worklistLenght)
{
testsquare=0;
//get the nodes position
xNow=worklist[countThrough][0];
yNow=worklist[countThrough][1];
while(testsquare<8)
{
testvalue++;//erase
//make that smaller
//select a neighbor square
xTemp=0;yTemp = 0;
diagonal=false;
//smaller classfile than switch..
if(testsquare==0) {xTemp = 1;}
if(testsquare==1) {xTemp = 1;yTemp = 1;diagonal=true;}
if(testsquare==2) {yTemp = 1;}
if(testsquare==3) {xTemp = -1;yTemp = 1;diagonal=true;}
if(testsquare==4) {xTemp = -1;}
if(testsquare==5) {xTemp = -1;yTemp = -1;diagonal=true;}
if(testsquare==6) {yTemp = -1;}
if(testsquare==7) {xTemp = 1;yTemp = -1;diagonal=true;}
xTemp=xNow+xTemp;
yTemp=yNow+yTemp;
//check if neighbor is in valid range
if((xTemp>-1)&&(xTemp<pdimX)&&(yTemp>-1)&&(yTemp<pdimY))
{
//is it a passable tile? (also checking for the buildingblockings)
// if(basegrid.LayerPathfindingMain[xTemp][yTemp]==0)
if(terrainGrid[xTemp][yTemp]==1 || dontCareForObstacles) //1-> means ist passable here ///could check for Buildingsquares here too
{
//each square finding a valid neightbor with acount higher then this squares count+1 -> set to thiscount+1 and add to next list
// Logger.out("nextlistLenght:"+nextlistLenght+" x:"+xNow+" y:"+yNow+"grid:"+grid[xNow][yNow]+" other:"+grid[xTemp][yTemp]);
//this takes into account that diagonal movement costs more
walkdist=10;
if(diagonal) walkdist=14;
if(gridFlood[gridId][xTemp][yTemp]>(gridFlood[gridId][xNow][yNow]+walkdist)) //neighbor has bigger stepcount than me?
{
//mark the neightbor
gridFlood[gridId][xTemp][yTemp]=(short)(gridFlood[gridId][xNow][yNow]+walkdist); //set the stepcount to my stepcount plus one
//TODO: should be a different value for diagonal movement!, to make paths smoother
//grid[xTemp][yTemp]+=basegrid.LayerBuildingblocking[xTemp][yTemp]*3; //this can be used to make building "half passable"
//add this neighbor for the next check
nextlistLenght++;
nextlist[nextlistLenght][0]=(short)xTemp;
nextlist[nextlistLenght][1]=(short)yTemp;
}
}
}
//choose the next possible neighbor
testsquare++;
}
//test the next node in the worklist
countThrough++;
}
//copy the nextlists contents to the current worklist
//System.arraycopy(nextlist, 0, worklist, 0,nextlistLenght*2);
worklistLenght=nextlistLenght;
for (int ix=0;ix<=nextlistLenght;ix++)
{
worklist[ix][0]=nextlist[ix][0];
worklist[ix][1]=nextlist[ix][1];
}
}
}
//create the list of currently used squares
static int worklist[][]=null; //trying a lower dimension reduces the memory use..
static int nextlist[][]=null; //trying a lower dimension reduces the memory use..