Advertisement
Guest User

attach_func

a guest
Aug 2nd, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. attached_ped = {}
  2. attached_bone = {}
  3. attached_x = {}
  4. attached_y = {}
  5. attached_z = {}
  6. attached_rx = {}
  7. attached_ry = {}
  8. attached_rz = {}
  9.  
  10. function attachElementToBone(element,ped,bone,x,y,z,rx,ry,rz)
  11.         if not (isElement(element) and isElement(ped)) then return false end
  12.         if getElementType(ped) ~= "ped" and getElementType(ped) ~= "player" then return false end
  13.         bone = tonumber(bone)
  14.         if not bone or bone < 1 or bone > 20 then return false end
  15.         x,y,z,rx,ry,rz = tonumber(x) or 0,tonumber(y) or 0,tonumber(z) or 0,tonumber(rx) or 0,tonumber(ry) or 0,tonumber(rz) or 0
  16.         attached_ped[element] = ped
  17.         attached_bone[element] = bone
  18.         attached_x[element] = x
  19.         attached_y[element] = y
  20.         attached_z[element] = z
  21.         attached_rx[element] = rx
  22.         attached_ry[element] = ry
  23.         attached_rz[element] = rz
  24.         if setElementCollisionsEnabled then
  25.                 setElementCollisionsEnabled(element,false)
  26.         end
  27.         if script_serverside then
  28.                 triggerClientEvent("boneAttach_attach",root,element,ped,bone,x,y,z,rx,ry,rz)
  29.         end
  30.         return true
  31. end
  32.  
  33. function detachElementFromBone(element)
  34.         if not element then return false end
  35.         if not attached_ped[element] then return false end
  36.         clearAttachmentData(element)
  37.         if setElementCollisionsEnabled then
  38.                 setElementCollisionsEnabled(element,true)
  39.         end
  40.         if script_serverside then
  41.                 triggerClientEvent("boneAttach_detach",root,element)
  42.         end
  43.         return true
  44. end
  45.  
  46. function isElementAttachedToBone(element)
  47.         if not element then return false end
  48.         return isElement(attached_ped[element])
  49. end
  50.  
  51. function getElementBoneAttachmentDetails(element)
  52.         if not isElementAttachedToBone(element) then return false end
  53.         return attached_ped[element],attached_bone[element],
  54.                 attached_x[element],attached_y[element],attached_z[element],
  55.                 attached_rx[element],attached_ry[element],attached_rz[element]
  56. end
  57.  
  58. function setElementBonePositionOffset(element,x,y,z)
  59.         local ped,bone,ox,oy,oz,rx,ry,rz = getElementBoneAttachmentDetails(element)
  60.         if not ped then return false end
  61.         return attachElementToBone(element,ped,bone,x,y,z,rx,ry,rz)
  62. end
  63.  
  64. function setElementBoneRotationOffset(element,rx,ry,rz)
  65.         local ped,bone,x,y,z,ox,oy,oz = getElementBoneAttachmentDetails(element)
  66.         if not ped then return false end
  67.         return attachElementToBone(element,ped,bone,x,y,z,rx,ry,rz)
  68. end
  69.  
  70. if not script_serverside then
  71.         function getBonePositionAndRotation(ped,bone)
  72.                 bone = tonumber(bone)
  73.                 if not bone or bone < 1 or bone > 20 then return false end
  74.                 if not isElement(ped) then return false end
  75.                 if getElementType(ped) ~= "player" and getElementType(ped) ~= "ped" then return false end
  76.                 if not isElementStreamedIn(ped) then return false end
  77.                 local x,y,z = getPedBonePosition(ped,bone_0[bone])
  78.                 local rx,ry,rz = getEulerAnglesFromMatrix(getBoneMatrix(ped,bone))
  79.                 return x,y,z,rx,ry,rz
  80.         end
  81. end
  82.  
  83. ------------------------------------
  84.  
  85. function clearAttachmentData(element)
  86.         attached_ped[element] = nil
  87.         attached_bone[element] = nil
  88.         attached_x[element] = nil
  89.         attached_y[element] = nil
  90.         attached_z[element] = nil
  91.         attached_rx[element] = nil
  92.         attached_ry[element] = nil
  93.         attached_rz[element] = nil
  94. end
  95.  
  96. function forgetDestroyedElements()
  97.         if not attached_ped[source] then return end
  98.         clearAttachmentData(source)
  99. end
  100. addEventHandler(script_serverside and "onElementDestroy" or "onClientElementDestroy",root,forgetDestroyedElements)
  101.  
  102. function forgetNonExistingPeds()
  103.         local checkedcount = 0
  104.         while true do
  105.                 for element,ped in pairs(attached_ped) do
  106.                         if not isElement(ped) then clearAttachmentData(element) end
  107.                         checkedcount = checkedcount+1
  108.                         if checkedcount >= 1000 then
  109.                                 coroutine.yield()
  110.                                 checkedcount = 0
  111.                         end
  112.                 end
  113.                 coroutine.yield()
  114.         end
  115. end
  116. clearing_nonexisting_peds = coroutine.create(forgetNonExistingPeds)
  117. setTimer(function()     coroutine.resume(clearing_nonexisting_peds) end,1000,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement