74 lines
2.5 KiB
Lua
74 lines
2.5 KiB
Lua
-- server/gang_ff.lua
|
||
print("^2[turfwar]^7 server/gang_ff.lua loaded (server-side same-gang FF block)")
|
||
|
||
-- Requires PlayerGang to be GLOBAL (see patch in server/main.lua)
|
||
PlayerGang = PlayerGang or {}
|
||
|
||
local function gangOf(src)
|
||
return tonumber(PlayerGang[src]) or 0
|
||
end
|
||
|
||
local function sameGangNonNeutral(a, b)
|
||
local ga = gangOf(a)
|
||
local gb = gangOf(b)
|
||
return ga ~= 0 and ga == gb
|
||
end
|
||
|
||
-- Helper: resolve victim player from weaponDamageEvent payload
|
||
local function resolveVictimPlayerFromWeaponData(data)
|
||
if not data then return nil end
|
||
|
||
-- Different builds/resources may use different keys; try common ones
|
||
local netId =
|
||
data.hitGlobalId or
|
||
data.hitEntity or
|
||
data.hitNetId or
|
||
data.entity or
|
||
data.victimNetId
|
||
|
||
if type(netId) ~= "number" then return nil end
|
||
|
||
local ent = NetworkGetEntityFromNetworkId(netId)
|
||
if not ent or ent == 0 or not DoesEntityExist(ent) then return nil end
|
||
|
||
if not IsEntityAPed(ent) then return nil end
|
||
if not IsPedAPlayer(ent) then return nil end
|
||
|
||
-- For player peds, owner should be that player’s source
|
||
local victimSrc = NetworkGetEntityOwner(ent)
|
||
if not victimSrc or victimSrc == 0 then return nil end
|
||
|
||
return victimSrc
|
||
end
|
||
|
||
-- Blocks bullets/melee/etc
|
||
AddEventHandler("weaponDamageEvent", function(sender, data)
|
||
-- sender is attacker (source)
|
||
if not sender or sender == 0 then return end
|
||
|
||
local victimSrc = resolveVictimPlayerFromWeaponData(data)
|
||
if not victimSrc then return end
|
||
|
||
if sameGangNonNeutral(sender, victimSrc) then
|
||
CancelEvent()
|
||
-- Uncomment for debug:
|
||
-- print(("[turfwar_ff] BLOCK weaponDamageEvent: %s -> %s gang=%s"):format(sender, victimSrc, gangOf(sender)))
|
||
end
|
||
end)
|
||
|
||
-- Blocks explosions (grenades, rockets, etc)
|
||
AddEventHandler("explosionEvent", function(sender, ev)
|
||
if not sender or sender == 0 then return end
|
||
if not ev then return end
|
||
|
||
-- explosionEvent has a 'pos' and can have 'ownerNetId' etc; there isn't always a single victim.
|
||
-- But it can still stop friendly grenade spam within gang by blocking the explosion entirely.
|
||
-- If you don't use explosives, you can remove this handler.
|
||
local attackerGang = gangOf(sender)
|
||
if attackerGang == 0 then return end
|
||
|
||
-- If you want to allow explosives vs other gangs but still protect same-gang,
|
||
-- you’d need per-victim explosion damage (not provided directly here).
|
||
-- So we leave explosives allowed by default.
|
||
end)
|