1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-12-05 23:28:14 +02:00

small pathfinder fix (please test a bit)

This commit is contained in:
mateuszb
2008-06-13 18:56:30 +00:00
parent afe2bc4cfa
commit bd13d80df0

View File

@@ -171,11 +171,11 @@ void CPathfinder::AddNeighbors(vector<Coordinate>* branch)
toAdd.push_back(*c); toAdd.push_back(*c);
Open.push(toAdd); Open.push(toAdd);
} }
delete c;
} }
} }
} }
delete c;
} }
/* /*
@@ -238,6 +238,8 @@ CPath* CPathfinder::ConvertToOldFormat(vector<Coordinate>* p)
CPath* path = new CPath(); CPath* path = new CPath();
std::vector<int> costs; //vector with costs of tiles
for(int i = 0; i < p->size(); i++) for(int i = 0; i < p->size(); i++)
{ {
CPathNode temp; CPathNode temp;
@@ -256,19 +258,16 @@ CPath* CPathfinder::ConvertToOldFormat(vector<Coordinate>* p)
} }
//set diagonality //set diagonality
float diagonal = 1.0f; //by default float diagonal = 1.0f; //by default
if(i+1<p->size()) if(i>0)
{ {
if(p->at(i+1).x != temp.coord.x && p->at(i+1).y != temp.coord.y) if(p->at(i-1).x != temp.coord.x && p->at(i-1).y != temp.coord.y)
{ {
diagonal = sqrt(2.0f); diagonal = sqrt(2.0f);
} }
} }
//Set distance //Set distance
if(i == 0) costs.push_back( i==0 ? 0 : p->at(i - 1).h * diagonal );
temp.dist = p->at(i).h * diagonal;
else
temp.dist = p->at(i).h * diagonal + path->nodes.back().dist;
//theNodeBefore is never used outside of pathfinding? //theNodeBefore is never used outside of pathfinding?
@@ -278,13 +277,10 @@ CPath* CPathfinder::ConvertToOldFormat(vector<Coordinate>* p)
path->nodes.push_back(temp); path->nodes.push_back(temp);
} }
//YOU ARE ALL BACKWARDS!! =P costs.push_back(0);
//Flip the distances. for(int i=path->nodes.size()-1; i>=0; --i)
for(int i = 0; i < path->nodes.size()/2;i++)
{ {
int t = path->nodes[i].dist; path->nodes[i].dist = costs[i+1] + ((i == path->nodes.size()-1) ? 0 : path->nodes[i+1].dist);
path->nodes[i].dist = path->nodes[path->nodes.size()-i-1].dist;
path->nodes[path->nodes.size()-i-1].dist = t;
} }
return path; return path;