@ -8,68 +8,68 @@ void Pathfinding::Initialize(){
if ( nodes ! = nullptr ) {
delete [ ] nodes ;
}
nodes = new sNode [ game - > WORLD_SIZE . x * game - > WORLD_SIZE . y ] ;
for ( int x = 0 ; x < game - > WORLD_SIZE . x ; x + + )
for ( int y = 0 ; y < game - > WORLD_SIZE . y ; y + + )
nodes = new sNode [ game - > GetCurrentMap ( ) . width * game - > GetCurrentMap ( ) . height ] ;
for ( int x = 0 ; x < game - > GetCurrentMap ( ) . width ; x + + )
for ( int y = 0 ; y < game - > GetCurrentMap ( ) . height ; y + + )
{
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 - > GetCurrentMap ( ) . width + x ] . x = x ; // ...because we give each node its own coordinates
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . y = 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 - > GetCurrentMap ( ) . width + x ] . bObstacle = tile . pos ! = game - > NO_COLLISION . pos | | tile . size ! = game - > NO_COLLISION . size ;
tile = game - > GetTileCollision ( game - > GetCurrentLevel ( ) , { float ( x * 24 ) , float ( y * 24 ) } , true ) ;
nodes [ y * game - > WORLD_SIZE . x + x ] . bObstacleUpper = 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 ] . bVisited = false ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . bObstacleUpper = tile . pos ! = game - > NO_COLLISION . pos | | tile . size ! = game - > NO_COLLISION . size ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . parent = nullptr ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . bVisited = false ;
}
for ( int x = 0 ; x < game - > WORLD_SIZE . x ; x + + )
for ( int y = 0 ; y < game - > WORLD_SIZE . y ; y + + )
for ( int x = 0 ; x < game - > GetCurrentMap ( ) . width ; x + + )
for ( int y = 0 ; y < game - > GetCurrentMap ( ) . height ; y + + )
{
if ( y > 0 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > WORLD_SIZE . x + ( x + 0 ) ] ) ;
if ( y < game - > WORLD_SIZE . y - 1 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > WORLD_SIZE . x + ( x + 0 ) ] ) ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > GetCurrentMap ( ) . width + ( x + 0 ) ] ) ;
if ( y < game - > GetCurrentMap ( ) . height - 1 )
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > GetCurrentMap ( ) . width + ( x + 0 ) ] ) ;
if ( x > 0 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y + 0 ) * game - > WORLD_SIZE . x + ( x - 1 ) ] ) ;
if ( x < game - > WORLD_SIZE . x - 1 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y + 0 ) * game - > WORLD_SIZE . x + ( x + 1 ) ] ) ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y + 0 ) * game - > GetCurrentMap ( ) . width + ( x - 1 ) ] ) ;
if ( x < game - > GetCurrentMap ( ) . width - 1 )
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y + 0 ) * game - > GetCurrentMap ( ) . width + ( x + 1 ) ] ) ;
if ( y > 0 & & x > 0 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > WORLD_SIZE . x + ( x - 1 ) ] ) ;
if ( y < game - > WORLD_SIZE . y - 1 & & x > 0 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > WORLD_SIZE . x + ( x - 1 ) ] ) ;
if ( y > 0 & & x < game - > WORLD_SIZE . x - 1 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > WORLD_SIZE . x + ( x + 1 ) ] ) ;
if ( y < game - > WORLD_SIZE . y - 1 & & x < game - > WORLD_SIZE . x - 1 )
nodes [ y * game - > WORLD_SIZE . x + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > WORLD_SIZE . x + ( x + 1 ) ] ) ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > GetCurrentMap ( ) . width + ( x - 1 ) ] ) ;
if ( y < game - > GetCurrentMap ( ) . height - 1 & & x > 0 )
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > GetCurrentMap ( ) . width + ( x - 1 ) ] ) ;
if ( y > 0 & & x < game - > GetCurrentMap ( ) . width - 1 )
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y - 1 ) * game - > GetCurrentMap ( ) . width + ( x + 1 ) ] ) ;
if ( y < game - > GetCurrentMap ( ) . height - 1 & & x < game - > GetCurrentMap ( ) . width - 1 )
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . vecNeighbours . push_back ( & nodes [ ( y + 1 ) * game - > GetCurrentMap ( ) . width + ( x + 1 ) ] ) ;
}
// Manually position the start and end markers so they are not nullptr
nodeStart = & nodes [ ( game - > WORLD_SIZE . y / 2 ) * game - > WORLD_SIZE . x + 1 ] ;
nodeEnd = & nodes [ ( game - > WORLD_SIZE . y / 2 ) * game - > WORLD_SIZE . x + game - > WORLD_SIZE . x - 2 ] ;
nodeStart = & nodes [ ( game - > GetCurrentMap ( ) . height / 2 ) * game - > GetCurrentMap ( ) . width + 1 ] ;
nodeEnd = & nodes [ ( game - > GetCurrentMap ( ) . height / 2 ) * game - > GetCurrentMap ( ) . width + game - > GetCurrentMap ( ) . width - 2 ] ;
}
std : : vector < vf2d > Pathfinding : : Solve_AStar ( vf2d startPos , vf2d endPos , float maxRange , bool upperLevel ) {
float dist = sqrt ( pow ( endPos . x - startPos . x , 2 ) + pow ( endPos . y - startPos . y , 2 ) ) ;
if ( dist > maxRange * 24 ) return { } ;
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 ) ] ;
nodeStart = & nodes [ int ( startPos . y / 24 ) * game - > GetCurrentMap ( ) . width + int ( startPos . x / 24 ) ] ;
nodeEnd = & nodes [ int ( endPos . y / 24 ) * game - > GetCurrentMap ( ) . width + 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 ) ) } ;
posPerimeter . pos = { int ( std : : clamp ( posPerimeter . pos . x - maxRange * 24 , 0.f , game - > GetCurrentMap ( ) . width * 24.f ) ) , int ( std : : clamp ( posPerimeter . pos . y - maxRange * 24 , 0.f , game - > GetCurrentMap ( ) . height * 24.f ) ) } ;
posPerimeter . size = { int ( std : : clamp ( posPerimeter . size . x + maxRange * 24 * 2 , 0.f , game - > GetCurrentMap ( ) . width * 24.f - posPerimeter . pos . x ) ) , int ( std : : clamp ( posPerimeter . size . y + maxRange * 24 * 2 , 0.f , game - > GetCurrentMap ( ) . height * 24.f - posPerimeter . pos . y ) ) } ;
for ( int x = 0 ; x < game - > WORLD_SIZE . x ; x + + ) {
for ( int y = 0 ; y < game - > WORLD_SIZE . y ; y + + ) {
for ( int x = 0 ; x < game - > GetCurrentMap ( ) . width ; x + + ) {
for ( int y = 0 ; y < game - > GetCurrentMap ( ) . height ; y + + ) {
if ( geom2d : : overlaps ( posPerimeter , vi2d { x * 24 , y * 24 } ) ) {
nodes [ y * game - > WORLD_SIZE . x + x ] . bVisited = false ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . bVisited = false ;
} else {
nodes [ y * game - > WORLD_SIZE . x + x ] . bVisited = true ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . bVisited = true ;
}
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 ] . parent = nullptr ; // No parents
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . fGlobalGoal = INFINITY ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . fLocalGoal = INFINITY ;
nodes [ y * game - > GetCurrentMap ( ) . width + x ] . parent = nullptr ; // No parents
}
}