Skip to content

Instantly share code, notes, and snippets.

@throwarray
Created May 18, 2018 02:43
Show Gist options
  • Save throwarray/e528f28d5ee95e69700241d3fb32960b to your computer and use it in GitHub Desktop.
Save throwarray/e528f28d5ee95e69700241d3fb32960b to your computer and use it in GitHub Desktop.
local weapons = {}
function headsUp(text)
SetTextComponentFormat('STRING')
AddTextComponentString(text)
DisplayHelpTextFromStringLabel(0, 0, 1, -1)
end
function RestoreAmmoForWeapon (weapons, weapon, waitTime)
local playerPed = PlayerPedId()
local state = weapons[weapon]
local clipMax = state.clipMax
local clipAmmo = state.clipAmmo
local ammo = state.ammo
waitTime = waitTime or 300
state.busy = true
Citizen.SetTimeout(waitTime, function ()
if playerPed == PlayerPedId() then
if HasPedGotWeapon(playerPed, weapon, false) then
SetAmmoInClip(playerPed, weapon, clipAmmo)
SetPedAmmo(playerPed, weapon, ammo)
end
end
state.busy = false
end)
return state
end
function CacheAmmoForWeapon (weapons, weapon)
local playerPed = PlayerPedId()
local state = weapons[weapon]
if not state then
state = { weapon = weapon }
weapons[weapon] = state
end
state.clipMax = GetMaxAmmoInClip(playerPed, weapon, 1)
state._, state.clipAmmo = GetAmmoInClip(playerPed, weapon)
state.ammo = GetAmmoInPedWeapon(playerPed, weapon)
return state
end
Citizen.CreateThread(function ()
local playerPed
local currentWeapon
local current
local templ = '%d/%d - %d [%d]'
local isReloading
Wait(100)
while true do
if playerPed ~= PlayerPedId() then
weapons = {}
playerPed = PlayerPedId()
currentWeapon = GetSelectedPedWeapon(playerPed)
current = CacheAmmoForWeapon(weapons, currentWeapon)
end
if currentWeapon ~= GetSelectedPedWeapon(playerPed) then
if HasPedGotWeapon(playerPed, currentWeapon, false) then
CacheAmmoForWeapon(weapons, currentWeapon)
RestoreAmmoForWeapon(weapons, currentWeapon)
else
weapons[currentWeapon] = nil
end
currentWeapon = GetSelectedPedWeapon(playerPed)
current = weapons[currentWeapon]
if not current then
current = CacheAmmoForWeapon(weapons, currentWeapon)
else
RestoreAmmoForWeapon(weapons, current.weapon)
end
end
-- Debug
if not current.busy and not IsPedReloading(playerPed) then
CacheAmmoForWeapon(weapons, current.weapon)
headsUp(string.format(templ,
current.clipAmmo,
current.clipMax,
current.ammo,
current.weapon
))
end
Citizen.Wait(0)
end
end)
Citizen.CreateThread(function ()
local playerPed
local state
while true do
playerPed = PlayerPedId()
state = weapons[GetSelectedPedWeapon(playerPed)]
-- if not IsPedReloading(playerPed) then
-- PedSkipNextReloading(playerPed)
-- end
if state then
if IsControlJustPressed(1, 45) and not IsPedReloading(playerPed) then
-- TaskReloadWeapon(playerPed, 1)
state.needed = (state.clipMax - state.clipAmmo) or 0
if state.ammo < state.needed then
state.clipAmmo = state.ammo
state.ammo = state.clipAmmo
else
state.clipAmmo = state.clipAmmo + state.needed
state.ammo = state.ammo -- - state.needed
end
end
if IsPedShooting(playerPed) and state.clipAmmo <= 1 then
if state.clipAmmo == 1 then print('Last bullet') end
state.clipAmmo = 0
state.ammo = state.ammo - 1
if state.ammo < 0 then state.ammo = 0 end
SetAmmoInClip(playerPed, state.weapon, 0)
SetPedAmmo(playerPed, state.weapon, state.ammo)
end
end
Wait(0)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment