Well here is the code, then…
#pragma strict
@System.NonSerialized
var i : int;
class node
{
var f : float;
var g : float;
var h : float;
var posX : int;
var posY : int;
var isCrate : boolean;
var parent : node;
var walkable : boolean;
function node()
{
f=0.0;
g=0.0;
h=0.0;
isCrate=false;
parent=null;
walkable=true;
f=0.0;
}
}
var finals : node[,]; //node array - grid
finals = new node[10,10];
function initObstacle(info : int[,])
{
var i:int;
var j :int;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
finals [i,j]=new node();
if (i==0 j==0)
{
finals [i,j].walkable=true;
}
else if (info [i,j]==1)
{
finals [i,j].walkable=false;
}
else
{
finals [i,j].walkable=true;
}
finals [i,j].posX=i;
finals [i,j].posY=j;
// print(i+","+j+"is"+finals [i,j].walkable);
}
}
}
function initGrid ()
{
var data : int[,];
var start : node;
var end : node;
data=new int[10,10];
var i: int;
var j: int;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
data [i,j]=Random.Range(0,2);
}
}
initObstacle(data);
}
function removeGraphNode(sd : Array,malu : node)
{
//var i : int;
sd.Remove(malu);
}
function findNeighbors (noud : node)
{
var ret = new Array ();
var x = noud.posX;
var y = noud.posY;
if(x!=0 finals[x-1,y] /* finals[x-1,y]*/)
{
ret.push(finals[x-1,y]);
}
if( x!=9 finals[x+1,y]/* finals[x+1,y]*/)
{
ret.push(finals[x+1,y]);
}
if(y!=0 finals[x,y-1]/* finals[x,y-1]*/)
{
ret.push(finals[x,y-1]);
}
if(y!=9 finals[x,y+1]/* finals[x,y+1]*/)
{
ret.push(finals[x,y+1]);
}
return ret;
}
function findGraphNode (ds : Array,val : node)
{
var found : boolean=false;
var i : int;
for (i=0;i<ds.length;i++)
{
if ((ds [i] as node).Equals(val))
{
found=true;break;
}
}
return found;
}
function heuristic (pos0: node,pos1:node)
{
var d1 = Mathf.Abs((pos1 as node).posX - (pos0 as node).posX);
var d2 = Mathf.Abs ((pos1 as node).posY - (pos0 as node).posY);
return d1 + d2;
}
function search(grid,start,end)
{
//initGrid();
var openList = new Array();
var closedList = new Array();
// for (i=0;i<50;i++)
// {
// openList [i]=new node();
// closedList [i] =new node();
// }
openList.push(start);
while(openList.length > 0)
{
// Grab the lowest f(x) to process next
var lowInd = 0;
var i:int;
for(i=0; i<openList.length; i++)
{
if((openList[i] as node).f < (openList[lowInd] as node).f)
{
lowInd = i;
}
}
var currentNode = openList[lowInd];
// End case -- result has been found, return the traced path
if((currentNode as node).posX == (end as node).posX (currentNode as node).posY==(end as node).posY)
{
var curr = currentNode;
var ret = new Array ();
while((curr as node).parent)
{
ret.push(curr);
curr = (curr as node).parent;
}
for (i=0;i<ret.length;i++)
{
print ("x:"+(ret [i] as node).posX+"y:"+(ret [i] as node).posY);
}
return ret.reverse();
}
// Normal case -- move currentNode from open to closed, process each of its neighbors
removeGraphNode(openList,currentNode);
closedList.push(currentNode);
var neighbors = findNeighbors(currentNode);
for(i=0; i<neighbors.length;i++)
{
var neighbor = neighbors[i];
if(findGraphNode(closedList,neighbor) || !(neighbor as node).walkable)
{
// not a valid node to process, skip to next neighbor
continue;
}
// g score is the shortest distance from start to current node, we need to check if
// the path we have arrived at this neighbor is the shortest one we have seen yet
var gScore = (currentNode as node).g + 1; // 1 is the distance from a node to it's neighbor
var gScoreIsBest = false;
if(!findGraphNode(openList,neighbor))
{
// This the the first time we have arrived at this node, it must be the best
// Also, we need to take the h (heuristic) score since we haven't done so yet
gScoreIsBest = true;
(neighbor as node).h = heuristic(neighbor, end);
openList.push(neighbor);
}
else if(gScore < (neighbor as node).g)
{
// We have already seen the node, but last time it had a worse g (distance from start)
gScoreIsBest = true;
}
if(gScoreIsBest)
{
// Found an optimal (so far) path to this node. Store info on how we got here and
// just how good it really is...
(neighbor as node).parent = currentNode;
(neighbor as node).g = gScore;
(neighbor as node).f = (neighbor as node).g + (neighbor as node).h;
//(neighbor as node).debug = "F: " + neighbor.f + "
G: " + neighbor.g + "
H: " + neighbor.h;
}
}
}
// No result was found -- empty array signifies failure to find path
// print ("Path not found");
return [];
}
function Start ()
{
initGrid();
var path =new Array ();
var src : node=new node();
var dst : node=new node();
src.posX=7;
src.posY=3;
dst.posX=4;
dst.posY=8;
finals [3,7].walkable=true;
finals [8,4].walkable=true;
path=search(finals,src,dst);
if (path==null)
print ("Path not found");
else
{
for(i=0;i<path.length;i++)
print("x:"+(path [i] as node).posX+"y:"+(path [i] as node).posY);
}
}