Jump to content

Find and warp to nearest ammunation


Recommended Posts

Looks like I'm on a roll today.

I've been looking at different ways to find the nearest ammunation and warp to it, and this is what I've come up with:

  
ammunations = { 
  { 2150.1499023438, 936.83428955078, 10.306272506714 }, 
  { 782.69812011719, 1872.9688720703, 4.4971857070923 }, 
  {-1508.8366699219, 2603.4729003906, 55.322311401367} 
} 
  
function findNearestAmmunation(thePlayer) 
  local nearest = nil 
  local min = 999999 
  for key,val in pairs(ammunations) do 
    local xx,yy,zz=getElementPosition(thePlayer) 
    local x1=val[1] 
    local y1=val[2] 
    local z1=val[3] 
    local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
    if distthen 
      nearest = val 
      min = dist 
    end 
  end 
  return nearest[1],nearest[2],nearest[3] 
end 
  
addCommandHandler("warp", function() 
  local xx,yy,zz = findNearestAmmunation(source) 
  spawnPlayer(source,xx,yy,zz) 
end) 
  

I get a "attempt to compare boolean with number" error at line 16. I'm totally lost here, I've been working on this for hours.

Any help is once again apreciated.

Link to comment
ammunations = { 
  { 2150.1499023438, 936.83428955078, 10.306272506714 }, 
  { 782.69812011719, 1872.9688720703, 4.4971857070923 }, 
  {-1508.8366699219, 2603.4729003906, 55.322311401367} 
} 
  
function findNearestAmmunation(thePlayer) 
  local i=0 
  local minDistance=100000 
  local minAmmu = 0 
  local posX, posY, posZ = getElementPosition( thePlayer ) 
  for key,val in ipairs(ammunations) do 
    i=i+1 
    local distance = getDistanceBetweenPoints2D(posX, posY, val[1], val[2]) 
    if (distance) then 
      minDistance=distance 
      minAmmu=i 
    end 
  end 
  return ammunations[minAmmu][1],ammunations[minAmmu][2],ammunations[minAmmu][3] 
end 
  
addCommandHandler("warp", function(thePlayer) 
  local xx,yy,zz = findNearestAmmunation(thePlayer) 
  spawnPlayer(thePlayer,xx,yy,zz) 
end) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...