Skip to content

Commit

Permalink
refactor: weapons that damage player synced more efficiently with server
Browse files Browse the repository at this point in the history
* refactor: weapons that damage player

* refactor: weapons that damaged players synced more efficiently with server

* fix: added weapon back
  • Loading branch information
Manason authored Nov 20, 2023
1 parent f43be38 commit 29d91ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 48 deletions.
26 changes: 4 additions & 22 deletions client/damage/damage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,20 @@ local function injureBodyPart(bone)
end
end

---@param array any[]
---@param value any
---@return boolean found
local function isInArray(array, value)
if not array then return false end

for i = 1, #array do
if array[i] == value then
return true
end
end

return false
end

---Adds weapon hashes that damaged the ped that aren't already in the CurrentDamagedList and syncs to the server.
local function findDamageCause()
local detected = false
for hash, weapon in pairs(exports.qbx_core:GetWeapons()) do
if HasPedBeenDamagedByWeapon(cache.ped, hash, 0) and not isInArray(CurrentDamageList, hash) then
detected = true
if HasPedBeenDamagedByWeapon(cache.ped, hash, 0) and not WeaponsThatDamagedPlayer[hash] then
TriggerEvent('chat:addMessage', {
color = { 255, 0, 0 },
multiline = false,
args = { Lang:t('info.status'), weapon.damagereason }
})
CurrentDamageList[#CurrentDamageList + 1] = hash
WeaponsThatDamagedPlayer[hash] = true
---TODO: verify that this only executes once for the loop and can therefore return early.
TriggerServerEvent('qbx_medical:server:playerDamagedByWeapon', hash)
end
end
if detected then
lib.callback('qbx_medical:server:SetWeaponWounds', false, false, CurrentDamageList)
end
end

---returns true if player took damage in their upper body or if the weapon class is nothing.
Expand Down
23 changes: 9 additions & 14 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---@type number[] weapon hashes
CurrentDamageList = {}
---@type table<number, boolean> weapon hashes as a set
WeaponsThatDamagedPlayer = {}

NumInjuries = 0

Expand Down Expand Up @@ -143,7 +143,7 @@ end
--- TODO: this export should not check any conditions, but force the ped to limp instead.
exports('makePedLimp', MakePedLimp)

function ResetMinorInjuries()
local function resetMinorInjuries()
for _, bodyPart in pairs(BodyParts) do
if bodyPart.severity > 0 and bodyPart.severity <= 2 then
bodyPart.severity = 0
Expand All @@ -168,9 +168,7 @@ function ResetMinorInjuries()
doLimbAlert()
end

exports('resetMinorInjuries', ResetMinorInjuries)

function ResetAllInjuries()
local function resetAllInjuries()
for _, v in pairs(BodyParts) do
v.severity = 0
end
Expand All @@ -187,17 +185,14 @@ function ResetAllInjuries()
isBleeding = BleedLevel
})

CurrentDamageList = {}
lib.callback('qbx_medical:server:SetWeaponWounds', false, false, CurrentDamageList)
WeaponsThatDamagedPlayer = {}

SendBleedAlert()
MakePedLimp()
doLimbAlert()
lib.callback('qbx_medical:server:resetHungerAndThirst')
end

exports('resetAllInjuries', ResetAllInjuries)

function DamageBodyPart(bone, severity)
BodyParts[bone].severity = severity
end
Expand Down Expand Up @@ -244,9 +239,9 @@ end)
---@param type? "full"|any heals all wounds if full otherwise heals only major wounds.
lib.callback.register('qbx_medical:client:heal', function(type)
if type == "full" then
ResetAllInjuries()
resetAllInjuries()
else
ResetMinorInjuries()
resetMinorInjuries()
end
exports.qbx_core:Notify(Lang:t('success.wounds_healed'), 'success')
end)
Expand All @@ -272,8 +267,8 @@ end
exports('getPatientStatus', getPatientStatus)

---Revives player, healing all injuries
---Intended to be called from client or server.
RegisterNetEvent('qbx_medical:client:playerRevived', function()
if source then return end
local ped = cache.ped

if IsDead or InLaststand then
Expand All @@ -288,7 +283,7 @@ RegisterNetEvent('qbx_medical:client:playerRevived', function()
SetEntityHealth(ped, 200)
ClearPedBloodDamage(ped)
SetPlayerSprint(cache.playerId, true)
ResetAllInjuries()
resetAllInjuries()
ResetPedMovementClipset(ped, 0.0)
TriggerServerEvent('hud:server:RelieveStress', 100)
exports.qbx_core:Notify(Lang:t('info.healthy'), 'inform')
Expand Down
22 changes: 10 additions & 12 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
---@field limbs BodyParts
---@field isBleeding number

---@type table<source, PlayerStatus>
---@alias Source number

---@type table<Source, PlayerStatus>
local playerStatus = {}

---@type table<source, number[]> weapon hashes
local playerWeaponWounds = {}
---@type table<Source, table<number, boolean>> weapon hashes
local WeaponsThatDamagedPlayers = {}

local triggerEventHooks = require 'modules.hooks.server'

---@param data number[] weapon hashes
lib.callback.register('qbx_medical:server:setWeaponWounds', function(source, data)
playerWeaponWounds[source] = data
end)

lib.callback.register('qbx_medical:server:clearWeaponWounds', function(source)
playerWeaponWounds[source] = nil
RegisterNetEvent('qbx_medical:server:playerDamagedByWeapon', function(hash)
if WeaponsThatDamagedPlayers[source][hash] then return end
WeaponsThatDamagedPlayers[source][hash] = true
end)

---@param player table|number
Expand All @@ -26,7 +24,7 @@ local function revivePlayer(player)
end
player.Functions.SetMetaData("isdead", false)
player.Functions.SetMetaData("inlaststand", false)
playerWeaponWounds[player.PlayerData.source] = nil
WeaponsThatDamagedPlayers[player.PlayerData.source] = nil
TriggerClientEvent('qbx_medical:client:playerRevived', player.PlayerData.source)
end

Expand Down Expand Up @@ -86,7 +84,7 @@ lib.callback.register('hospital:GetPlayerStatus', function(_, playerId)
damage.damagedBodyParts = getDamagedBodyParts(playerInjuries.limbs)
end

damage.weaponWounds = playerWeaponWounds[playerSource] or {}
damage.weaponWounds = WeaponsThatDamagedPlayers[playerSource] or {}
return damage
end)

Expand Down

0 comments on commit 29d91ad

Please sign in to comment.