• better bot pathfinding
    1 replies, posted
I'm trying to get bots to move to one location but they just keep running into walls and other things that get in the way. Is there a way to check if a bot is stuck and force them to take a different path? This is the code I'm using: local rePathDelay = 1 // How many seconds need to pass before we need to remake the path to keep it updated hook.Add( "StartCommand", "astar_example", function( ply, cmd )  if ( !ply:IsBot() || GetConVarNumber( "bot_mimic" ) != 0 ) then return end  cmd:ClearButtons()  cmd:ClearMovement()  local currentArea = navmesh.GetNearestNavArea( ply:GetPos() )  // internal variable to regenerate the path every X seconds to keep the pace with the target player  ply.lastRePath = ply.lastRePath or 0  // internal variable to limit how often the path can be ( re )generated  ply.lastRePath2 = ply.lastRePath2 or 0  if ( ply.path && ply.lastRePath + rePathDelay < CurTime() && currentArea != ply.targetArea ) then   ply.path = nil   ply.lastRePath = CurTime()  end  if ( !ply.path && ply.lastRePath2 + rePathDelay < CurTime() ) then   local targetPos = Vector( -420.171509, 2783.729980, 194.142242 )  // target position to go to, the first player on the server   local targetArea = navmesh.GetNearestNavArea( targetPos )   ply.targetArea = nil   ply.path = Astar( currentArea, targetArea )   if ( !istable( ply.path ) ) then // We are in the same area as the target, or we can't navigate to the target    ply.path = nil // Clear the path, bail and try again next time    ply.lastRePath2 = CurTime()    return   end   table.remove( ply.path ) // Just for this example, remove the starting area, we are already in it!  end  // We have no path, or its empty ( we arrived at the goal ), try to get a new path.  if ( !ply.path || #ply.path < 1 ) then   ply.path = nil   ply.targetArea = nil   return  end  // Select the next area we want to go into  if ( !IsValid( ply.targetArea ) ) then   ply.targetArea = ply.path[ #ply.path ]  end  // We got the target to go to, aim there and MOVE  local targetang = ( ply.targetArea:GetCenter() - ply:GetPos() ):GetNormalized():Angle()  cmd:SetViewAngles( targetang )  cmd:SetForwardMove( 1000 )     end )
I ended up just doing this:   timer.Simple( 5, function() targetPos = Vector(VectorRand() *math.random(-10000,10000), VectorRand() *math.random(-10000,10000), VectorRand() *math.random(-10000,10000)) targetArea = navmesh.GetNearestNavArea( targetPos )   end)         ply.path = Astar( currentArea, targetArea ) It sets the bot's path to a random point ever 10 seconds so even if it does get stuck it will get out of it hopefully every 10 seconds. Not a perfect solution but it works for me
Sorry, you need to Log In to post a reply to this thread.