TIGSource Forums

Developer => Technical => Topic started by: kamac on May 28, 2012, 04:46:39 AM



Title: Infinite "for" loop mess. Pathfinding mess.
Post by: kamac on May 28, 2012, 04:46:39 AM
Hey.


I am trying to get some pathfinding done, but sadly my for never ends, even when it looks like:

for(int n=0; n<1; n++)

Well, here's the code. Could anybody find what's wrong with it that it runs forever?




Code:
std::vector<int> queque_x;
std::vector<int> queque_y;
std::vector<int> queque_var;
//Push the end values into the queque.
queque_x.push_back(19);
queque_y.push_back(9);
queque_var.push_back(0);
int got_path = 0;

while(got_path == 0)
{
for(int n = 0; n<(int)queque_x.size(); n++)
{
//Add surrounding cells.
int cell_x[4] = { queque_x[n]+1, queque_x[n], queque_x[n]-1, queque_x[n] };
int cell_y[4] = { queque_y[n], queque_y[n]+1, queque_y[n], queque_y[n]-1 };
int cell_ava[4] = { 1, 1, 1, 1 };
int cell_var = queque_var[n]+1;

for(int m=0; m<4; m++)
{
int mx = cell_x[m];
int my = cell_y[m];
if(map[my][mx] == 1)
{
//Damn! A wall!
cell_ava[m] = 0;
} else {
for(int g=0; g<queque_x.size(); g++)
{
if(queque_x[g] == cell_x[m] && queque_y[g] == cell_y[m] &&
queque_var[g] <= cell_var)
cell_ava[m] = 0;
}
}
}
for(int m=0; m<4; m++)
{
if(cell_ava[m] == 1)
{
queque_x.push_back(cell_x[m]);
queque_y.push_back(cell_y[m]);
queque_var.push_back(cell_var);
}
}
}
if(queque_x[queque_x.size()-1] == 9) // player x
{
if(queque_y[queque_y.size()-1] == 9) // player y
{
//YEEEY. We've got there.
got_path = 1;
}
}
}

PS. Do you guys know what value is tab key? I mean, under what value does it write it to a notepad when we save it.


Title: Re: Infinite "for" loop mess. Pathfinding mess.
Post by: rivon on May 28, 2012, 05:00:14 AM
Tab is 9dec, 9hex or 011oct.

To the code: shouldn't there be
Code:
queque_x.push_back(9);
on the fifth line? Currently, there is 19.


Title: Re: Infinite "for" loop mess. Pathfinding mess.
Post by: kamac on May 28, 2012, 05:13:37 AM
Nope, because I add the end position first.
The end coordinates are:
X: 19
Y: 9

The starting coordinates are:
X: 9
Y: 9

It is worth adding that nothing after end of for loop is reached, but I know that for is continously repeating.

@EDIT

Alright. I figured out that I should do it this way:
Code:
int queque_s = queque_x.size();
for(int n = 0; n<queque_s; n++)

 :facepalm:


Though, pathfinding still won't work.


Title: Re: Infinite "for" loop mess. Pathfinding mess.
Post by: Liosan on May 28, 2012, 06:06:32 AM
What's this for?
Quote
Code:
while(got_path == 0)
Nobody says you are going to find a route. The search is finished when your search options are exhausted :)

Liosan


Title: Re: Infinite "for" loop mess. Pathfinding mess.
Post by: kamac on May 28, 2012, 06:10:04 AM
Oh, well you're right.

It does find a path for me now, but it starts from random points or so...


I mean, my drawing is messed up  :biglaff: