Jump to content

Help with nearest position


xXMADEXx

Recommended Posts

Posted

hey guys, i made this little spawn script, but im not sure how to find the nearest hospital..

spawnTable = { 
{1930.0747070313, -1457.0139160156, 26.112157821655, 1984.0969238281, -1469.8690185547, 29.183320999146, 90},   -- LS Brown 
{1177.4389648438, -1323.1046142578, 14.070738792419, 1218.4992675781, -1323.8115234375, 33.528995513916, -90},  -- LS White 
{-2650.94140625, 633.86663818359, 14.453125, -2564.9030761719, 563.16589355469, 44.11022567749, 180},           -- SF White 
{1607.1779785156, 1817.7474365234, 10.8203125, 1589.6560058594, 1859.5609130859, 30.602132797241, 0},           -- LV Main 
{1371.9439697266, 405.51559448242, 19.7578125, 1350.5229492188, 415.71002197266, 31.090511322021, 90}}          -- LV in LS 
  
-- create blips 
function unpackPos() 
end 
blip = {} 
for i,v in ipairs (spawnTable) do 
    local x,y,z,x2,y2,z2,r = unpack(spawnTable[i]) 
    createBlip(x, y, z, 22, 2, 255, 0, 0, 255, 0, 180) 
end 
  
addEventHandler("onPlayerWasted",root, 
    function () 
        local p = source 
        local skin = getElementModel(p) 
        local x,y,z,x2,y2,z2,r = unpack(spawnTable[math.random(#spawnTable)]) 
        fadeCamera(p,false) 
        setTimer( 
            function () 
                setCameraMatrix(p,x2,y2,z2,x,y,z) 
                fadeCamera(p,true) 
            end, 1000, 1 
        ) 
        setTimer( 
            function () 
                fadeCamera(p,false) 
                setTimer( 
                    function () 
                        setCameraTarget(p,p) 
                        spawnPlayer(p,x,y,z,r,skin) 
                    end, 1000, 1 
                ) 
            end, 4000, 1 
        ) 
         
    end 
) 
  

Posted

Cycle through all points using for loop, Calculate the distance to every point and get the one which has the shortest distance.

Posted

This will maybe help you:

it will be MUCH easier just to check which hospital is nearest one :)
hospitalsTable = { 
{ 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
{ -2656.2421875, 635.99420166016, 14.453125 }, 
{ 1607.1225585938, 1817.8732910156, 10.8203125 }, 
} 
  
  
function spawn( player ) 
  local i=0 
  local minDistance=100000 
  local minHospital = 0 
  local posX, posY, posZ = getElementPosition(player) 
  for key,val in ipairs(hospitalsTable) do 
    i=i+1 
    local distance = getDistanceBetweenPoints2D(posX, posY, val[1], val[2]) 
    if (distance) then 
      minDistance=distance 
      minHospital=i 
    end 
  end 
  spawnPlayer( player, hospitalsTable[minHospital][1], hospitalsTable [minHospital][2], hospitalsTable [minHospital][3], hospitalsTable [minHospital][4] ) 
end 
addEventHandler("onPlayerWasted", getRootElement(),spawn) 
  
-- btw: i dont see 4th value in your table (spawn player 5th argument) 
-- ps: i didnt test it, try it. im not sure if i should use ipairs or pairs, so if mta will complain about, try to change it. 

Quote from here.

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...