A-Star help :|

Posted By: EpsiloN

A-Star help :| - 08/19/07 14:15

I've been learning and trying to make A* pathfinding for a 50x50 grid with 'virtual' tiles. Everything is numbers.
The first 4 attempts I failed. Now , the fifth I started again from scratch. I created every single function separately testing it and everything worked until I started using them together to find a path.
I've added while space == 0 wait , while space == 1 wait to see what exactly happened each time the 'current_node' changed and the pathfinding function was setting current_node to absolutely diffrent nodes than the ones it should.
I've tested with X2,Y2 to X4,Y3 , or 52 to 153 node It sets 52 as the current node , next it sets 51 but it should set 53 or 102 as current. And next (51 is Y = 1) when it reaches the border of the grid Y = 0 it sets current as 0 and I get a crash when trying to set the adjacent node (current_node - 1).
This sounds like something is wrong with the evaluation of the F value but I looked hundreds of times at it and everything looks fine. Here it is :
Code:

function calc_h_value(current,goal)
{
var tmp1[2];
var tmp2[2];
tile_to_pos(current);
vec_set(tmp1,ttp);
tile_to_pos(goal);
vec_set(tmp2,ttp);

if(tmp1.x >= tmp2.x) { tmp1.x = tmp1.x - tmp2.x; }
else { tmp1.x = tmp2.x - tmp1.x; }
if(tmp1.y >= tmp2.y) { tmp1.y = tmp1.y - tmp2.y; }
else { tmp1.y = tmp2.y - tmp1.y; }
tmp1.x = (tmp1.x + tmp1.y) * 10;
h_value[current] = tmp1.x;
return(tmp1.x);
}

function calc_g_value(current)
{
var tmpg;
if(parent_list[current] != 0)
{
tmpg = g_value[parent_list[current]] + 10;
g_value[current] = tmpg;
return(tmpg);
}
return(-1);
}

function calc_f_value(current,goal)
{
var tmpf1;
var tmpf2;
tmpf1 = calc_h_value(current,goal);
tmpf2 = calc_g_value(current);
tmpf1 = tmpf1 + tmpf2;
f_value[current] = tmpf1;
return(tmpf1);
}

function compare_f_values(current,goal)
{
var tmp1;
while(tmp1 < 4)
{
f_val[tmp1] = calc_f_value(adj_node[tmp1],goal);
tmp1 += 1;
}
var i = 0;
var j;
var tmp;
while(i<4)
{
j=0;
while(j<i) // "<" or ">" for asc./desc. sort
{
if(f_val[testvar3[i]] < f_val[testvar3[j]])
{
tmp=testvar3[j];
testvar3[j]=testvar3[i];
testvar3[i]=tmp;
}
j+=1;
}
i+=1;
}
tmp = 0;
while(tmp < 4)
{
if(node_list[testvar3[tmp]] == 0)
{
return(testvar3[tmp]);
}
tmp += 1;
}
}


The only thing I'm not shure about is the sorting of f_val array , I took the script from here (bubble sorting) and it worked fine when I tested it alone.
Here's the PathFind function:
Code:

function pathfind(snode,gnode)
{
current_node = snode;
goal_node = gnode;
add_open_list(current_node);
while(open_list[0] != 0 || current_node != gnode)
{
set_adj_nodes(current_node);
var counter1;
while(counter1 < 4)
{
if(search_closed_list(adj_node[counter1]) == 0)
{
if(search_open_list(adj_node[counter1]) == 0)
{
add_open_list(adj_node[counter1]);
make_parent(adj_node[counter1],current_node);
}
}
counter1 += 1;
}
tmpstore1 = compare_f_values(current_node,goal_node);
add_closed_list(current_node);
current_node = adj_node[tmpstore1];
}
}



PS.: I tought I should have a deadline for this one,just to see if I can make it,but I guess I cant learn it and do it in 10 hours
Posted By: EpsiloN

Re: A-Star help :| - 08/19/07 18:21

I'm not shure if this would fix the problem,but I found a 'problem' in one of my functions. I guess not everything worked as expected.
So , now I need help with this one,to see if this will fix the whole pathfinding system.
Here's the function:
Code:

function tile_to_pos(tile)
{
var counter1 = 1;
var tmp1;
tmp1 = tile;
while(tmp1 > 0)
{
tmp1 -= glength;
counter1 += 1;
}
if(tmp1 < 0)
{
tmp1 += glength;
counter1 -= 1;
}
ttp.x = counter1;
ttp.y = tmp1;
}


This function takes a tile number and divides it by 50 a few times until the number reaches 0 or goes below. This way I can get the Y value of the nodes position on the grid and use a variable to pass it. Than , the remaining number is passed in the variable as X value. After this I can calculate the node possition,to set the adjacent nodes and calculate the h value. When it reaches one of the edges of the grid however the script returns incorect values and calculates incorect heuristics value,so from this point the whole pathfinding isnt working.
I hope this is the problem. I need help fast 02h:40min. till deadline I have to find a new way to calculate the X and Y pos. of a tile (eg. 3-rd row,2-nd column in the grid)

PS.: Sorry if the explanation is more than needed but I hope even not so experienced people could think on that with me. I really need help.
Posted By: quasarchangel

Re: A-Star help :| - 08/19/07 18:36

Would that not evaluate to a tmpl value that is always just above 0? and the only thing that would vary is counterl. I'm a newb when it comes to Astar, but I tried.

EDIT | Ok, that would depend on if the glength value is always above 0.

Good luck with the dead line, lol.
Posted By: EpsiloN

Re: A-Star help :| - 08/19/07 18:39

Exactly. Always above 0 , because the first tile is at possition X1,Y1. I dont have (and I dont want to have) a tile 0. Now I need to change this function to find the proper possition for the tile. Else , my whole system is useless and I'll have to think of another way. I can make a whole new system for 2 hours but I might snap and kill someone after that.
Posted By: quasarchangel

Re: A-Star help :| - 08/19/07 18:55

What sources did you use to get to the code that you have?
Posted By: EpsiloN

Re: A-Star help :| - 08/19/07 19:17

The function tile_to_pos is entirely made by me,maby thats why it doesnt work as it should I found a solution but it'll make my script useless. To put all node possitions in 2 arrays. Node 1 ... node_pos_x[node1] = 1 , node_pos_y[node1] = 1;
It'll make my script useless because the whole purpose of it should be to act dinamicly. You put glength & gwidth , you put tsize (grid length,width and tile size) and you have a pathfinding algorithym that'll adapt to the changes made. If I imput all the node possitions by hand it'll limit the size of the grid to whatever number I set it to. If I want to change from 50x50 grid to 49x49 grid I'll have to rearrange 1/10 of the arrays and this would be nececary for every level I create. Not to mention it takes time to find the correct values in the array to change,and additional time to rearrange the rest of the array to fit my new grid.
Is there a math forum or something over the net? I think I should ask there...
Posted By: quasarchangel

Re: A-Star help :| - 08/19/07 19:30

I have a C++ example that I got somewhere else if you want to have a look at it. It has an exe to test the thing, but you'll have to either get ideas from it or convert it to C-script, or make a dll. It has the ability to change the size of the array or should have.
http://quasar.ifastnet.com/files/C++Path.rar

I also have an old PDF Astar tutorial for A5.2 by Philipp Walser, you could maybe make out something from it.
http://quasar.ifastnet.com/files/Path.pdf

I hope it helps.
Posted By: EpsiloN

Re: A-Star help :| - 08/19/07 19:51

I cant find those files,and even if I can,the problem isnt related directly to pathfinding anymore its more related to tile based programming.
By adding:
Code:

if(tmp1 > 50)
{


Before the while loop it solves the problem,but only for the first row of tiles. If I make for every row if > 50 , if > 100 it will become useless again because I'm defining some limits to the script. I need to figure out a way to tell the engine how to identify if its bigger than some values. Or an entirely new way of finding that tile in the grid.
Posted By: quasarchangel

Re: A-Star help :| - 08/19/07 19:59

The links dont work from here either, but if I copy them to my address bar they work. It might be best to look at what other people did, unless you want to wait for someone who can help you in a better way.
Posted By: EpsiloN

Re: A-Star help :| - 08/19/07 20:18

I tought I should search in google for a formula to translate the tile number to rows and columns numbers but I dunno what to search for. Not everything is related to C-Script or even programming
So,I decided to wait for someone more experienced than me to answer with a solution until tommorow morning. Than if noone decided to help (there are people who know how to do that,and I'm shure about it) I'll figure it out tommorow on my own.
I rarely ask like this for help,because of this reason. You cant get answers for more complex things unless you know atleast 10 experienced people on this forum and you've worked with more than 5 of them.

PS.: No one realy knows me on this forum I prefer to work alone because I'll be responsible for my mistakes and wont ruin someone elses work.
Posted By: quasarchangel

Re: A-Star help :| - 08/19/07 20:42

Your not the only one who likes doing it. If you figure something out on your own you usually learn more and remember better, but you have to get some information from someone who knows. Thats why I got a bunch of tutors and reading stuff when I tried doing my own Astar script but I failed and ended up with a customized version which isnt really astar and doesn't work with a grid but waypoints. So, if you get it to work can you please post it on a forum or send me a link where I can get it from.
© 2024 lite-C Forums