client.lua
--------------------------- --(c) by MazzMan/Maxim --------------------------- canDriverUseGun = true ammo = {} ammo.small = false ammo.middle = false ammo.big = true ammo.smallShots = 50 ammo.middleShots = 6 ammo.smallCanShoot = true ammo.middleCanShoot = true ammo.smallReloading = false ammo.middleReloading = false ammo.bigReloading = false setElementData(getLocalPlayer(), "ac130KeysBound", false) addEventHandler("onClientVehicleEnter", getRootElement(), function(thePlayer, seat) if(getLocalPlayer() == thePlayer and getElementModel(source) == 592 and getElementData(source, "ac130"))then if(canDriverUseGun)then bindKey("space", "down", setBinds) elseif(seat == 1)then bindKey("space", "down", setBinds) end end end) function removeAllGunControls() unbindKey("space", "down", setBinds) setCameraGoggleEffect("normal") setElementData(getLocalPlayer(), "ac130KeysBound", false) removeEventHandler("onClientRender", getRootElement(), keepCamAtAc130) removeEventHandler("onClientCursorMove", getRootElement(), cammera) unbindKey("mouse1", "down", shoot) setCameraTarget(getLocalPlayer()) unbindKey("mouse_wheel_up", "down", changeAmmo) unbindKey("mouse_wheel_down", "down", changeAmmo) unbindKey("v", "down", setVisionMode) end addEventHandler("onClientVehicleExit", getRootElement(), removeAllGunControls) addEventHandler("onClientResourceStop", getRootElement(), removeAllGunControls) addEventHandler("onClientPlayerWasted", getRootElement(), function() if(source == getLocalPlayer())then removeAllGunControls() end end) local windowX, windowY = guiGetScreenSize() function cammera(screenX, screenY, absoluteX, absoluteY, vx,vy,vz) local x,y,z = getElementPosition(getPedOccupiedVehicle(getLocalPlayer())) local rx, ry, rz = getElementRotation(getPedOccupiedVehicle(getLocalPlayer())) rz = rz +180 local cx = x + math.cos(math.rad(rz)) * 3 local cy = y + math.sin(math.rad(rz)) * 3 setCameraMatrix ( cx, cy, z, vx, vy, vz ) drawHud() end function keepCamAtAc130() local x,y,z = getElementPosition(getPedOccupiedVehicle(getLocalPlayer())) local rx, ry, rz = getElementRotation(getPedOccupiedVehicle(getLocalPlayer())) rz = rz +180 local cx = x + math.cos(math.rad(rz)) * 3 local cy = y + math.sin(math.rad(rz)) * 3 setCameraMatrix(cx, cy, z) end function setBinds() if(getElementData(getLocalPlayer(), "ac130KeysBound") == false)then setElementData(getLocalPlayer(), "ac130KeysBound", true) keepCamAtAc130 ( ) addEventHandler("onClientCursorMove", getRootElement(), cammera) addEventHandler ( "onClientRender", root, drawHud ) bindKey("mouse1", "down", shoot) bindKey("mouse_wheel_up", "down", changeAmmo) bindKey("mouse_wheel_down", "down", changeAmmo) bindKey("v", "down", setVisionMode) else setElementData(getLocalPlayer(), "ac130KeysBound", false) removeEventHandler("onClientCursorMove", getRootElement(), cammera) removeEventHandler ( "onClientRender", root, drawHud ) unbindKey("mouse1", "down", shoot) setCameraTarget(getLocalPlayer()) unbindKey("mouse_wheel_up", "down", changeAmmo) unbindKey("mouse_wheel_down", "down", changeAmmo) unbindKey("v", "down", setVisionMode) setCameraGoggleEffect("normal") end end function shoot() local cx, cy, cz = getCameraMatrix() local wX,wY,wZ = getWorldFromScreenPosition(windowX/2, windowY/2, 2000) local hit, x, y, z = processLineOfSight(cx, cy, cz, wX,wY,wZ) if(hit)then local distance = getDistanceBetweenPoints3D(x, y, z, cx, cy, cz) local time = distance/0.5 local ammoType if(ammo.small)then if(time >= 50)then if(ammo.smallCanShoot and ammo.smallReloading == false)then --im using a timer, becaus a while loop will cause to crash the game shootTimer = setTimer(function() if(getKeyState("mouse1"))then cx, cy, cz = getCameraMatrix() wX,wY,wZ = getWorldFromScreenPosition(windowX/2, windowY/2, 2000) hit, x, y, z = processLineOfSight(cx, cy, cz, wX,wY,wZ) if(hit)then playSound("sounds/shot_small.wav") ammoType = "small" ammo.smallShots = ammo.smallShots - 1 if(ammo.smallShots == 0)then setReloading("small") killTimer(shootTimer) end ammo.smallCanShoot = false setTimer(function() ammo.smallCanShoot = true end, 200, 1) triggerServerEvent("onAc130Shoot", getRootElement(),cx, cy, cz, x, y, z, time, ammoType) end else killTimer(shootTimer) end end, 50, 0) else return false end else return false end elseif(ammo.middle)then if(time >= 50)then if(ammo.middleCanShoot and ammo.middleReloading == false)then ammoType = "middle" ammo.middleShots = ammo.middleShots - 1 if(ammo.middleShots == 0)then setReloading("middle") end ammo.middleCanShoot = false playSound("sounds/shot_middle.wav") setTimer(function() ammo.middleCanShoot = true end, 500, 1) else return false end else return false end elseif(ammo.big)then if(ammo.bigReloading == false)then if(time >= 50)then ammoType = "big" playSound("sounds/shot_middle.wav") setReloading("big") else return false end else return false end end triggerServerEvent("onAc130Shoot", getRootElement(),cx, cy, cz, x, y, z, time, ammoType) end end function changeAmmo(key) if(key == "mouse_wheel_up")then if(ammo.small)then ammo.small = false ammo.big = true elseif(ammo.middle)then ammo.middle = false ammo.small = true elseif(ammo.big)then ammo.big = false ammo.middle = true end else if(ammo.small)then ammo.small = false ammo.middle = true elseif(ammo.middle)then ammo.middle = false ammo.big = true elseif(ammo.big)then ammo.big = false ammo.small = true end end end function setReloading(ammoType) if(ammoType == "small")then ammo.smallReloading = true setTimer(function() ammo.smallReloading = false ammo.smallShots = 50 end, 3000, 1) elseif(ammoType == "middle")then ammo.middleReloading = true setTimer(function() ammo.middleReloading = false ammo.middleShots = 6 end, 3000, 1) elseif(ammoType == "big")then ammo.bigReloading = true setTimer(function() ammo.bigReloading = false end, 3000, 1) end end function setVisionMode() if(getCameraGoggleEffect() == "normal")then setCameraGoggleEffect("nightvision") elseif(getCameraGoggleEffect() == "nightvision")then setCameraGoggleEffect("thermalvision") elseif(getCameraGoggleEffect() == "thermalvision")then setCameraGoggleEffect("normal") end end addEvent("onClientCreateEffect", true) addEventHandler("onClientCreateEffect", getRootElement(), function(x,y,z, lx, ly, lz, type, time) if(type ~= "small")then --playSound3D("sounds/shot_middle.wav", x,y,z) --local num = math.random(1, 3) --local projectileSound = playSound3D("sounds/projectile_"..num..".wav", lx, ly, lz)