Advertisement
maramizo

Untitled

Nov 6th, 2016 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. --Server
  2. local sCasingArray = {}
  3.  
  4. function gCreateEjectedCase(lArray)
  5.     sCasingArray = lArray
  6.     for i,v in ipairs(sCasingArray) do
  7.         local casingsObject = createObject(2061, sCasingArray[i][1], sCasingArray[i][2], sCasingArray[i][3]) -- Would need to be replaced with the owl object creation function, object name would be renamed to the serial.
  8.         setObjectScale(casingsObject, 0.2)
  9.     end
  10. end
  11. addEvent("ejectCase", true)
  12. addEventHandler("ejectCase", resourceRoot, gCreateEjectedCase)
  13.  
  14. function gCreateWeapon(player, command, value, ammo, cases)
  15.     if value ~= nil and ammo ~= nil then
  16.     cases = tonumber(cases)
  17.         if cases == 1 or cases == nil or cases == 0 then -- General idea, however, the admin command or the GUI should be changed to add this parameter.
  18.             giveWeapon(player, tonumber(value), ammo, true)
  19.             if cases == 1 then
  20.                 setElementData(player, "holdingCasesGun", 1) -- Would not be this way if weapons are elements on owl. A variable & function would be added to the weapons class.
  21.             else
  22.                 setElementData(player, "holdingCasesGun", 0)
  23.             end
  24.         else
  25.             outputChatBox("Cases must be either 1 or 0.")
  26.         end
  27.     else
  28.         outputChatBox("USAGE: /creategun <ID> <ammo> <Does Not Eject Casings = 0 or 1, default = 0>", player)
  29.     end
  30. end
  31. addCommandHandler("creategun", gCreateWeapon)
  32.  
  33. -- Weapon stats would need to be saved as boolean in the database. Slight modification of items res may be required so that administrators can change weapons to automatically eject cases or not.
  34. -- I'm assuming Owl's weapons are stored as elements, and hence setElementData would be stored in each weapon's object's variables themselves rather than an unefficient setElementData which updates to all players.
  35. --Client
  36. Casing = {}
  37. Casing.__index = Casing
  38. prepTimer = nil -- Timer for calling the server trigger and garbage collection.
  39. sentArray = {} -- Array to be sent to server, one server trigger is better than looping or sending repetitive ones.
  40. curArray = {} -- Current array to contain the object list of all cases created (Don't know how to iterate through objects under the Casings class, would appreciate input).
  41. function Casing.create(x, y, z)
  42.     if x ~= nil and y ~= nil and z ~= nil then -- Check valid co-ordinations.
  43.         local _casing = {}
  44.         setmetatable(_casing,Casing) -- Indexing the class.
  45.         _casing.object = createObject(2061, x, y, z) -- Informal object creation on client side.
  46.         curArray[#curArray+1] = _casing.object -- Assign the object element to the array.
  47.         setObjectScale(_casing.object, 0.2) -- Set scale smaller.
  48.         _casing.pos = {x, y, z} -- Store position.
  49.         _casing.time = getTickCount() -- To be later used to ensure the time > 10 seconds since the last shot.
  50.         sentArray[#sentArray+1] = {x,y,z} -- Co-ords in the array for the server sided creation.
  51.         return _casing -- Returning the object. S/e.
  52.     else
  53.         outputDebugString("Res: ejectCasings | Error: Could not produce a casing due to invalid co-ords.")
  54.         return false
  55.     end
  56. end
  57.  
  58. function Casing:recentlyFired(curTickCount)
  59.     if curTickCount - self.time < 10000 then
  60.         return true
  61.     else
  62.         return false
  63.     end
  64. end
  65.  
  66. function getSideOfPlayer()
  67.     return localPlayer.matrix.position + localPlayer.matrix.right * math.random(.2,3)
  68. end
  69.  
  70. function gEjectCases(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
  71.     local currentGun = getPedWeapon(localPlayer)
  72.     local casesEjected = getElementData(localPlayer, "holdingCasesGun") -- Would be different in Owl scripts assuming Owl's weapon scripts have a class.
  73.     if casesEjected ~= 1 then
  74.         local pVector = getSideOfPlayer() -- Getting the right side of the player.
  75.         local x,y,z = pVector:getX(),pVector:getY(),pVector:getZ()
  76.         z = getGroundPosition(x,y,z) -- Placing bullets on the ground.
  77.         caseObj = Casing.create(x,y,z) -- Cannot be local, as it cannot be passed as an arg through setTimer.
  78.         if isTimer(prepTimer) then
  79.             killTimer(prepTimer) -- Prevent multiple timers.
  80.         end
  81.         prepTimer = setTimer(gPrepareCases, 10000, 1)
  82.     end
  83. end
  84. addEventHandler("onClientPlayerWeaponFire", root, gEjectCases)
  85.  
  86. function gPrepareCases()
  87.     if caseObj:recentlyFired(getTickCount()) == false then
  88.         triggerServerEvent("ejectCase", resourceRoot, sentArray)
  89.         sentArray = {} -- Flush array with current objects.
  90.         for i,v in ipairs(curArray) do
  91.             destroyElement(curArray[i]) -- Don't know how to destroy all objects in a class. Would appreciate input on this for proper garbage collection.
  92.         end
  93.         curArray = {} -- Clear current array.
  94.     else
  95.         if isTimer(prepTimer) then
  96.             killTimer(prepTimer)
  97.         end
  98.         setTimer(gPrepareCases, 10000, 1)
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement