|
|
@ -11,7 +11,7 @@ void Pathfinding::Initialize(){ |
|
|
|
{ |
|
|
|
{ |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].x = x; // ...because we give each node its own coordinates
|
|
|
|
nodes[y * game->WORLD_SIZE.x + x].x = x; // ...because we give each node its own coordinates
|
|
|
|
nodes[y * game->WORLD_SIZE.x + x].y = y; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].y = y; |
|
|
|
geom2d::rect<int>tile=game->GetTileCollision(game->GetCurrentLevel(),{float(x),float(y)}); |
|
|
|
geom2d::rect<int>tile=game->GetTileCollision(game->GetCurrentLevel(),{float(x*24),float(y*24)}); |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].bObstacle = tile.pos!=game->NO_COLLISION.pos||tile.size!=game->NO_COLLISION.size; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].bObstacle = tile.pos!=game->NO_COLLISION.pos||tile.size!=game->NO_COLLISION.size; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].parent = nullptr; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].parent = nullptr; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].bVisited = false; |
|
|
|
nodes[y * game->WORLD_SIZE.x + x].bVisited = false; |
|
|
@ -35,13 +35,25 @@ void Pathfinding::Initialize(){ |
|
|
|
nodeEnd = &nodes[(game->WORLD_SIZE.y / 2) * game->WORLD_SIZE.x + game->WORLD_SIZE.x-2]; |
|
|
|
nodeEnd = &nodes[(game->WORLD_SIZE.y / 2) * game->WORLD_SIZE.x + game->WORLD_SIZE.x-2]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int Pathfinding::Solve_AStar(vf2d startPos,vf2d endPos){ |
|
|
|
int Pathfinding::Solve_AStar(vf2d startPos,vf2d endPos,float maxRange){ |
|
|
|
|
|
|
|
float dist=sqrt(pow(endPos.x-startPos.x,2)+pow(endPos.y-startPos.y,2)); |
|
|
|
|
|
|
|
if(dist>maxRange*24)return maxRange; |
|
|
|
|
|
|
|
|
|
|
|
nodeStart=&nodes[int(startPos.y/24)*game->WORLD_SIZE.x+int(startPos.x/24)]; |
|
|
|
nodeStart=&nodes[int(startPos.y/24)*game->WORLD_SIZE.x+int(startPos.x/24)]; |
|
|
|
nodeEnd=&nodes[int(endPos.y/24)*game->WORLD_SIZE.x+int(endPos.x/24)]; |
|
|
|
nodeEnd=&nodes[int(endPos.y/24)*game->WORLD_SIZE.x+int(endPos.x/24)]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
geom2d::rect<int>posPerimeter{{int(std::min(startPos.x,endPos.x)),int(std::min(startPos.y,endPos.y))},{int(abs(endPos.x-startPos.x)),int(abs(endPos.y-startPos.y))}}; |
|
|
|
|
|
|
|
posPerimeter.pos={int(std::clamp(posPerimeter.pos.x-maxRange*24,0.f,game->WORLD_SIZE.x*24.f)),int(std::clamp(posPerimeter.pos.y-maxRange*24,0.f,game->WORLD_SIZE.y*24.f))}; |
|
|
|
|
|
|
|
posPerimeter.size={int(std::clamp(posPerimeter.size.x+maxRange*24*2,0.f,game->WORLD_SIZE.x*24.f-posPerimeter.pos.x)),int(std::clamp(posPerimeter.size.y+maxRange*24*2,0.f,game->WORLD_SIZE.y*24.f-posPerimeter.pos.y))}; |
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < game->WORLD_SIZE.x; x++){ |
|
|
|
for (int x = 0; x < game->WORLD_SIZE.x; x++){ |
|
|
|
for (int y = 0; y < game->WORLD_SIZE.y; y++){ |
|
|
|
for (int y = 0; y < game->WORLD_SIZE.y; y++){ |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].bVisited = false; |
|
|
|
if(geom2d::overlaps(posPerimeter,vi2d{x*24,y*24})){ |
|
|
|
|
|
|
|
nodes[y*game->WORLD_SIZE.x + x].bVisited = false; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
nodes[y*game->WORLD_SIZE.x + x].bVisited = true; |
|
|
|
|
|
|
|
} |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].fGlobalGoal = INFINITY; |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].fGlobalGoal = INFINITY; |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].fLocalGoal = INFINITY; |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].fLocalGoal = INFINITY; |
|
|
|
nodes[y*game->WORLD_SIZE.x + x].parent = nullptr; // No parents
|
|
|
|
nodes[y*game->WORLD_SIZE.x + x].parent = nullptr; // No parents
|
|
|
@ -92,15 +104,21 @@ int Pathfinding::Solve_AStar(vf2d startPos,vf2d endPos){ |
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int pathLength=0; |
|
|
|
|
|
|
|
if (nodeEnd != nullptr) |
|
|
|
if (nodeEnd != nullptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
int pathLength=INFINITE; |
|
|
|
sNode *p = nodeEnd; |
|
|
|
sNode *p = nodeEnd; |
|
|
|
while (p->parent != nullptr) |
|
|
|
while (p->parent != nullptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
pathLength++; |
|
|
|
if(pathLength==INFINITE){ |
|
|
|
|
|
|
|
pathLength=1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
pathLength++; |
|
|
|
|
|
|
|
} |
|
|
|
p = p->parent; |
|
|
|
p = p->parent; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return pathLength; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return INFINITE; |
|
|
|
} |
|
|
|
} |
|
|
|
return pathLength; |
|
|
|
|
|
|
|
} |
|
|
|
} |