137 lines
4.2 KiB
Lua
137 lines
4.2 KiB
Lua
print("^2[turfwar]^7 police_wanted_escalate.lua loaded (client)")
|
|
|
|
-- -------------------------
|
|
-- Tunables
|
|
-- -------------------------
|
|
local DMG_POLL_MS = 200
|
|
local DMG_COOLDOWN_MS = 2500 -- per killer, per victim
|
|
|
|
-- -------------------------
|
|
-- State
|
|
-- -------------------------
|
|
local sentThisDeath = false
|
|
local lastDeathAt = 0
|
|
local dmgCooldown = {} -- [killerSid] = lastSentAt (GetGameTimer ms)
|
|
|
|
-- -------------------------
|
|
-- Helpers
|
|
-- -------------------------
|
|
local function killerServerIdFromEntity(ent)
|
|
if not ent or ent == 0 or not DoesEntityExist(ent) then return 0 end
|
|
|
|
local killerPed = ent
|
|
if IsEntityAVehicle(ent) then
|
|
killerPed = GetPedInVehicleSeat(ent, -1)
|
|
end
|
|
|
|
if not killerPed or killerPed == 0 or not DoesEntityExist(killerPed) then return 0 end
|
|
if not IsEntityAPed(killerPed) then return 0 end
|
|
if not IsPedAPlayer(killerPed) then return 0 end
|
|
|
|
local idx = NetworkGetPlayerIndexFromPed(killerPed)
|
|
if idx == -1 then return 0 end
|
|
|
|
return tonumber(GetPlayerServerId(idx)) or 0
|
|
end
|
|
|
|
-- Reset death flag when alive again
|
|
CreateThread(function()
|
|
while true do
|
|
Wait(250)
|
|
if sentThisDeath and not IsEntityDead(PlayerPedId()) then
|
|
sentThisDeath = false
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- -------------------------
|
|
-- KILL detection (keep what works)
|
|
-- -------------------------
|
|
AddEventHandler('gameEventTriggered', function(name, args)
|
|
if name ~= 'CEventNetworkEntityDamage' then return end
|
|
|
|
local victimEntity = args[1]
|
|
local attackerEntity = args[2]
|
|
local victimDied = args[6]
|
|
|
|
if victimEntity ~= PlayerPedId() then return end
|
|
if not victimDied then return end
|
|
|
|
if sentThisDeath then return end
|
|
if not IsEntityDead(PlayerPedId()) then return end
|
|
|
|
local now = GetGameTimer()
|
|
if now - lastDeathAt < 1500 then return end
|
|
lastDeathAt = now
|
|
sentThisDeath = true
|
|
|
|
local killerSid = killerServerIdFromEntity(attackerEntity)
|
|
if killerSid <= 0 then
|
|
-- fallback
|
|
killerSid = killerServerIdFromEntity(GetEntityLastDamageEntity(PlayerPedId()))
|
|
end
|
|
if killerSid > 0 then
|
|
TriggerServerEvent('turfwar:wanted:policeKilledBy', killerSid)
|
|
end
|
|
end)
|
|
|
|
-- -------------------------
|
|
-- DAMAGE detection (reliable polling)
|
|
-- -------------------------
|
|
CreateThread(function()
|
|
while true do
|
|
Wait(DMG_POLL_MS)
|
|
|
|
local ped = PlayerPedId()
|
|
if ped == 0 or not DoesEntityExist(ped) then goto continue end
|
|
if IsEntityDead(ped) then goto continue end
|
|
|
|
-- Was I damaged by any ped since last clear?
|
|
if HasEntityBeenDamagedByAnyPed(ped) then
|
|
local lastEnt = GetEntityLastDamageEntity(ped)
|
|
local killerSid = killerServerIdFromEntity(lastEnt)
|
|
|
|
if killerSid > 0 then
|
|
local now = GetGameTimer()
|
|
local last = dmgCooldown[killerSid] or 0
|
|
|
|
if (now - last) >= DMG_COOLDOWN_MS then
|
|
dmgCooldown[killerSid] = now
|
|
TriggerServerEvent('turfwar:wanted:policeDamagedBy', killerSid)
|
|
end
|
|
end
|
|
|
|
-- Clear flags so we don't spam forever
|
|
ClearEntityLastDamageEntity(ped)
|
|
ClearPedLastWeaponDamage(ped)
|
|
end
|
|
|
|
::continue::
|
|
end
|
|
end)
|
|
|
|
-- -------------------------
|
|
-- Wanted application (killer client)
|
|
-- -------------------------
|
|
RegisterNetEvent('turfwar:wanted:setMinimum', function(minStars)
|
|
local pid = PlayerId()
|
|
local current = GetPlayerWantedLevel(pid)
|
|
local newStars = math.max(current, tonumber(minStars) or 0)
|
|
|
|
if newStars ~= current then
|
|
ClearPlayerWantedLevel(pid)
|
|
SetPlayerWantedLevel(pid, newStars, false)
|
|
SetPlayerWantedLevelNow(pid, true)
|
|
end
|
|
end)
|
|
|
|
RegisterNetEvent('turfwar:wanted:setEscalated', function(targetStars, count, windowSec)
|
|
local pid = PlayerId()
|
|
local current = GetPlayerWantedLevel(pid)
|
|
local newStars = math.max(current, tonumber(targetStars) or 0)
|
|
|
|
ClearPlayerWantedLevel(pid)
|
|
SetPlayerWantedLevel(pid, newStars, false)
|
|
SetPlayerWantedLevelNow(pid, true)
|
|
end)
|