Skip to content

Instantly share code, notes, and snippets.

@grifdail
Created June 24, 2013 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grifdail/5851510 to your computer and use it in GitHub Desktop.
Save grifdail/5851510 to your computer and use it in GitHub Desktop.
Game.passability(x,y) == true if there is an obstacle use PathFinding.go(startX,startY,endX,endY)
PathFinding = {}
PathFinding.go = function(sx,sy,ex,ey) {
this.list = []
this.Cl = []
this.ex = ex;
this.ey = ey;
this.sx = sx;
this.sy = sy;
var num = PathFinding.dist(sx,sy,ex,ey);
this.firstNode= {X:sx,Y:sy,F:num,G:0,H:num}
this.list.push(this.firstNode)
var f =0
while (true){
//boucle principal
PathFinding.sort("F") // trie la liste de maniere opti
var node = this.list.shift()// recupere la plus facile
this.Cl.push(node) //ajoute la plus facile a la liste fermé
if (node != undefined) { // si il quelle que chose
if (PathFinding.validate(node) /* verifie les solution*/) { //si sortie trouvé
var n = {X:this.ex,Y:this.ey,F:0+node.G+1,G:node.G+1,H:0}
this.Cl.push(n)
return PathFinding.generate()
}
}
else {
//sinon il n'y a pas de solution
return undefined
}
}
}
PathFinding.validate =function(node) {
first:
for(var i = 0; i<4; i++ ) { //A chaque coté
var x=node.X-(i==3)+(i==1); //defini coordonné
var y=node.Y-(i==0)+(i==2); //defini coordonné
if (x == this.ex && y == this.ey) {return "find"}
if (Game.passability(x,y)) { // si passable <=== A modifier /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ /!\ vrai = bloqué
for(var l = 0; l<PathFinding.list.length;l++) { // et que ce n'est pas deja dans la liste principal
if (this.list[l].X == x && this.list[l].Y == y) {
continue first; // repart au debut
}
}
for(var l = 0; l<this.Cl.length;l++) { // ni dans la liste fermé.
if (this.Cl[l].X == x && this.Cl[l].Y == y) {
continue first; // repart au debut
}
}
// si pas de pb, ajoute a la list principal
var num = PathFinding.dist(x,y,this.ex,this.ey)
var thing = {X:x,Y:y,F:num+node.G+1,G:node.G+1,H:num}
this.list.push(thing)
}
else {
}
}
return null
}
PathFinding.dist = function(sx,sy,ex,ey) {
return(Math.abs(sx-ex) + Math.abs(sy-ey))
}
PathFinding.sort =function(hello) {
this.Cl.sort(function(a,b) {return(a[hello] - b[hello])})
return(this.list.sort(function(a,b) {return(a[hello] - b[hello])}))
}
PathFinding.generate = function() {
//PathFinding.sort("G") // trie Cl
this.path = [] // intialise la liste de solution
var node = this.Cl.pop() // recupere le dernier neud (la solution)
var g = 0
PathFinding.sort("H") // trie Cl
first:
while (true) { // boucle principal
for(var i = 0; i < this.Cl.length; i++) { // boucle pour trouver le prochain neud
if (this.Cl[i].G == node.G-1) { // si le neud a ete parcouru juste avant
var result = PathFinding.direction(node,this.Cl[i]) // verifie que c'est bien lui on niveau des cordonné
if (result) { //si c'est bien lui
this.path.push(result) // ajoute la direction au chemin
node = this.Cl[i]
if (this.Cl[i] == this.firstNode) { // si on est retourné au depart
break first; // alors l'algo est fini
}
}
}
}
}
return this.path.reverse();
}
PathFinding.direction =function(lnode,nnode) {
if (lnode.X == (nnode.X-1) && lnode.Y==nnode.Y) {return("Left")}
if (lnode.X == (nnode.X+1) && lnode.Y==nnode.Y) {return("Right")}
if (lnode.Y == (nnode.Y-1) && lnode.X==nnode.X) {return("Up")}
if (lnode.Y == (nnode.Y+1) && lnode.X==nnode.X) {return("Down")}
return undefined
}
PathFinding.follow = function(move,step,sx,sy) {
for (var i = 0; i < step; i++) {
sx += (move[i] =="goRight" ? 1 : 0 );
sx -= (move[i] =="goLeft" ? 1 : 0 );
sy += (move[i] =="goDown" ? 1 : 0 );
sy -= (move[i] =="goUp" ? 1 : 0 );
}
return {x:sx,y:sy}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment