Skip to content

Commit

Permalink
refactor: proper ox implementation and style fixes (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm authored Jan 14, 2024
1 parent 17a7e78 commit 451b98b
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 114 deletions.
12 changes: 5 additions & 7 deletions client/damage/apply-damage-effects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ local function disableArms(ped, leftArmDamaged)
DisableControlAction(0, 63, true) -- veh turn left
end

local playerId = cache.playerId
if IsPlayerFreeAiming(playerId) then
if IsPlayerFreeAiming(cache.playerId) then
if leftArmDamaged then
DisablePlayerFiring(playerId, true) -- Disable weapon firing
DisablePlayerFiring(cache.playerId, true) -- Disable weapon firing
else
DisableControlAction(0, 25, true) -- Disable weapon aiming
end
Expand Down Expand Up @@ -99,20 +98,19 @@ end

---applies disabling status effects based on injuries to specific body parts
function ApplyDamageEffects()
local ped = cache.ped
if DeathState ~= sharedConfig.deathState.ALIVE then return end
for bodyPartKey, injury in pairs(Injuries) do
if isLegDamaged(bodyPartKey, injury.severity) then
if legCount >= config.legInjuryTimer then
chancePedFalls(ped)
chancePedFalls(cache.ped)
legCount = 0
else
legCount += 1
end
elseif isArmDamaged(bodyPartKey, injury.severity) then
if armCount >= config.armInjuryTimer then
CreateThread(function()
disableArms(ped, isLeftArmDamaged(bodyPartKey, injury.severity))
disableArms(cache.ped, isLeftArmDamaged(bodyPartKey, injury.severity))
end)
armCount = 0
else
Expand All @@ -123,7 +121,7 @@ function ApplyDamageEffects()
local chance = math.random(100)

if chance <= config.headInjuryChance then
playBrainDamageEffectAndRagdoll(ped)
playBrainDamageEffectAndRagdoll(cache.ped)
end
headCount = 0
else
Expand Down
9 changes: 4 additions & 5 deletions client/damage/damage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ end

---detects if player took damage, applies injuries, and updates health/armor values
local function checkForDamage()
local ped = cache.ped
local health = GetEntityHealth(ped)
local armor = GetPedArmour(ped)
local health = GetEntityHealth(cache.ped)
local armor = GetPedArmour(cache.ped)

initHealthAndArmorIfNotSet(health, armor)

Expand All @@ -204,7 +203,7 @@ local function checkForDamage()

if isArmorDamaged or isHealthDamaged then
local damageDone = (Hp - health)
local weaponHash = applyDamage(ped, damageDone, isArmorDamaged)
local weaponHash = applyDamage(cache.ped, damageDone, isArmorDamaged)
if weaponHash and not WeaponsThatDamagedPlayer[weaponHash] then
TriggerEvent('chat:addMessage', {
color = { 255, 0, 0 },
Expand All @@ -213,7 +212,7 @@ local function checkForDamage()
})
WeaponsThatDamagedPlayer[weaponHash] = true
end
ClearEntityLastDamageEntity(ped)
ClearEntityLastDamageEntity(cache.ped)
end

Hp = health
Expand Down
46 changes: 23 additions & 23 deletions client/dead.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
local sharedConfig = require 'config.shared'
local WEAPONS = exports.qbx_core:GetWeapons()
local allowRespawn = false

local function playDeadAnimation()
local ped = cache.ped
local deadAnimDict = "dead"
local deadAnim = "dead_a"
local deadVehAnimDict = "veh@low@front_ps@idle_duck"
local deadVehAnim = "sit"
local deadAnimDict = 'dead'
local deadAnim = 'dead_a'
local deadVehAnimDict = 'veh@low@front_ps@idle_duck'
local deadVehAnim = 'sit'

if cache.vehicle then
if not IsEntityPlayingAnim(ped, deadVehAnimDict, deadVehAnim, 3) then
lib.requestAnimDict(deadVehAnimDict)
TaskPlayAnim(ped, deadVehAnimDict, deadVehAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
if not IsEntityPlayingAnim(cache.ped, deadVehAnimDict, deadVehAnim, 3) then
lib.requestAnimDict(deadVehAnimDict, 5000)
TaskPlayAnim(cache.ped, deadVehAnimDict, deadVehAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
end
elseif not IsEntityPlayingAnim(ped, deadAnimDict, deadAnim, 3) then
lib.requestAnimDict(deadAnimDict)
TaskPlayAnim(ped, deadAnimDict, deadAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
elseif not IsEntityPlayingAnim(cache.ped, deadAnimDict, deadAnim, 3) then
lib.requestAnimDict(deadAnimDict, 5000)
TaskPlayAnim(cache.ped, deadAnimDict, deadAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
end
end

Expand All @@ -27,19 +27,19 @@ function OnDeath()
SetDeathState(sharedConfig.deathState.DEAD)
TriggerEvent('qbx_medical:client:onPlayerDied')
TriggerServerEvent('qbx_medical:server:onPlayerDied')
TriggerServerEvent("InteractSound_SV:PlayOnSource", "demo", 0.1)
TriggerServerEvent('InteractSound_SV:PlayOnSource', 'demo', 0.1)
local player = cache.ped

WaitForPlayerToStopMoving()

CreateThread(function()
while DeathState == sharedConfig.deathState.DEAD do
DisableControls()
SetCurrentPedWeapon(cache.ped, `WEAPON_UNARMED`, true)
Wait(0)
end
end)

ResurrectPlayer()
playDeadAnimation()
SetEntityInvincible(player, true)
Expand All @@ -52,9 +52,9 @@ local function respawn()
local success = lib.callback.await('qbx_medical:server:respawn')
if not success then return end
if exports.qbx_policejob:IsHandcuffed() then
TriggerEvent("police:client:GetCuffed", -1)
TriggerEvent('police:client:GetCuffed', -1)
end
TriggerEvent("police:client:DeEscort")
TriggerEvent('police:client:DeEscort')
end

---Allow player to respawn
Expand Down Expand Up @@ -92,20 +92,20 @@ end)
---@param attacker number ped
---@param weapon string weapon hash
local function logDeath(victim, attacker, weapon)
local playerid = NetworkGetPlayerIndexFromPed(victim)
local playerName = GetPlayerName(playerid) .. " " .. "(" .. GetPlayerServerId(playerid) .. ")" or Lang:t('info.self_death')
local playerId = NetworkGetPlayerIndexFromPed(victim)
local playerName = GetPlayerName(playerId) .. ' ' .. '(' .. GetPlayerServerId(playerId) .. ')' or Lang:t('info.self_death')
local killerId = NetworkGetPlayerIndexFromPed(attacker)
local killerName = GetPlayerName(killerId) .. " " .. "(" .. GetPlayerServerId(killerId) .. ")" or Lang:t('info.self_death')
local weaponLabel = exports.qbx_core:GetWeapons()[weapon].label or 'Unknown'
local weaponName = exports.qbx_core:GetWeapons()[weapon].name or 'Unknown'
TriggerServerEvent("qb-log:server:CreateLog", "death", Lang:t('logs.death_log_title', { playername = playerName, playerid = GetPlayerServerId(playerid) }), "red", Lang:t('logs.death_log_message', { killername = killerName, playername = playerName, weaponlabel = weaponLabel, weaponname = weaponName }))
local killerName = GetPlayerName(killerId) .. ' ' .. '(' .. GetPlayerServerId(killerId) .. ')' or Lang:t('info.self_death')
local weaponLabel = WEAPONS[weapon].label or 'Unknown'
local weaponName = WEAPONS[weapon].name or 'Unknown'
TriggerServerEvent('qb-log:server:CreateLog', 'death', Lang:t('logs.death_log_title', { playername = playerName, playerid = GetPlayerServerId(playerId) }), 'red', Lang:t('logs.death_log_message', { killername = killerName, playername = playerName, weaponlabel = weaponLabel, weaponname = weaponName }))
end

---when player is killed by another player, set last stand mode, or if already in last stand mode, set player to dead mode.
---@param event string
---@param data table
AddEventHandler('gameEventTriggered', function(event, data)
if event ~= "CEventNetworkEntityDamage" then return end
if event ~= 'CEventNetworkEntityDamage' then return end
local victim, attacker, victimDied, weapon = data[1], data[2], data[4], data[7]
if not IsEntityAPed(victim) or not victimDied or NetworkGetPlayerIndexFromPed(victim) ~= cache.playerId or not IsEntityDead(cache.ped) then return end
if DeathState == sharedConfig.deathState.ALIVE then
Expand Down
39 changes: 15 additions & 24 deletions client/laststand.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@
local config = require 'config.client'
local sharedConfig = require 'config.shared'
local WEAPONS = exports.qbx_core:GetWeapons()

---blocks until ped is no longer moving
---@param ped number
function WaitForPlayerToStopMoving()
local ped = cache.ped
local TimeOut = 10000
while GetEntitySpeed(ped) > 0.5 or IsPedRagdoll(ped) and TimeOut > 1 do TimeOut -= 10 Wait(10) end
local timeOut = 10000
while GetEntitySpeed(cache.ped) > 0.5 or IsPedRagdoll(cache.ped) and timeOut > 1 do timeOut -= 10 Wait(10) end
end

--- low level GTA resurrection
function ResurrectPlayer()
local ped = cache.ped
local pos = GetEntityCoords(ped)
local heading = GetEntityHeading(ped)
local veh = cache.vehicle
local seat = cache.seat
local pos = GetEntityCoords(cache.ped)
local heading = GetEntityHeading(cache.ped)

NetworkResurrectLocalPlayer(pos.x, pos.y, pos.z + 0.5, heading, true, false)
if veh then
SetPedIntoVehicle(ped, veh, seat)
if cache.vehicle then
SetPedIntoVehicle(cache.ped, cache.vehicle, cache.seat)
end
end

---remove last stand mode from player.
function EndLastStand()
local ped = cache.ped
TaskPlayAnim(ped, LastStandDict, "exit", 1.0, 8.0, -1, 1, -1, false, false, false)
TaskPlayAnim(cache.ped, LastStandDict, 'exit', 1.0, 8.0, -1, 1, -1, false, false, false)
LaststandTime = 0
TriggerServerEvent('qbx_medical:server:onPlayerLaststandEnd')
end

local function logPlayerKiller()
local ped = cache.ped
local player = cache.playerId
local killer_2, killerWeapon = NetworkGetEntityKillerOfPlayer(player)
local killer = GetPedSourceOfDeath(ped)
local killer_2, killerWeapon = NetworkGetEntityKillerOfPlayer(cache.playerId)
local killer = GetPedSourceOfDeath(cache.ped)
if killer_2 ~= 0 and killer_2 ~= -1 then killer = killer_2 end
local killerId = NetworkGetPlayerIndexFromPed(killer)
local killerName = killerId ~= -1 and GetPlayerName(killerId) .. " " .. "(" .. GetPlayerServerId(killerId) .. ")" or Lang:t('info.self_death')
local killerName = killerId ~= -1 and GetPlayerName(killerId) .. ' ' .. '(' .. GetPlayerServerId(killerId) .. ')' or Lang:t('info.self_death')
local weaponLabel = Lang:t('info.wep_unknown')
local weaponName = Lang:t('info.wep_unknown')
local weaponItem = exports.qbx_core:GetWeapons()[killerWeapon]
local weaponItem = WEAPONS[killerWeapon]
if weaponItem then
weaponLabel = weaponItem.label
weaponName = weaponItem.name
end
TriggerServerEvent("qb-log:server:CreateLog", "death", Lang:t('logs.death_log_title', { playername = GetPlayerName(cache.playerId), playerid = GetPlayerServerId(player) }), "red", Lang:t('logs.death_log_message', { killername = killerName, playername = GetPlayerName(player), weaponlabel = weaponLabel, weaponname = weaponName }))
TriggerServerEvent('qb-log:server:CreateLog', 'death', Lang:t('logs.death_log_title', { playername = GetPlayerName(cache.playerId), playerid = GetPlayerServerId(cache.playerId) }), 'red', Lang:t('logs.death_log_message', { killername = killerName, playername = GetPlayerName(cache.playerId), weaponlabel = weaponLabel, weaponname = weaponName }))
end

---count down last stand, if last stand is over, put player in death mode and log the killer.
local function countdownLastStand()

if LaststandTime - 1 > 0 then
LaststandTime -= 1
else
Expand All @@ -66,13 +58,12 @@ end

---put player in last stand mode and notify EMS.
function StartLastStand()
local ped = cache.ped
Wait(1000)
WaitForPlayerToStopMoving()
TriggerServerEvent("InteractSound_SV:PlayOnSource", "demo", 0.1)
TriggerServerEvent('InteractSound_SV:PlayOnSource', 'demo', 0.1)
LaststandTime = config.laststandReviveInterval
ResurrectPlayer()
SetEntityHealth(ped, 150)
SetEntityHealth(cache.ped, 150)
PlayUnescortedLastStandAnimation()
SetDeathState(sharedConfig.deathState.LAST_STAND)
TriggerEvent('qbx_medical:client:onPlayerLaststand')
Expand Down
6 changes: 2 additions & 4 deletions client/load-unload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ local function onPlayerLoaded()
pcall(function() exports.spawnmanager:setAutoSpawn(false) end)
CreateThread(function()
Wait(1000)
local ped = cache.ped
local playerId = cache.playerId
initHealthAndArmor(ped, playerId, QBX.PlayerData.metadata)
initHealthAndArmor(cache.ped, cache.playerId, QBX.PlayerData.metadata)
initDeathAndLastStand(QBX.PlayerData.metadata)
end)
end

AddEventHandler('QBCore:Client:OnPlayerLoaded', onPlayerLoaded)

AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end
if cache.resource ~= resourceName then return end
onPlayerLoaded()
end)
30 changes: 14 additions & 16 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Hp = nil
DeathTime = 0
LaststandTime = 0
RespawnHoldTime = 5
LastStandDict = "combat@damage@writhe"
LastStandAnim = "writhe_loop"
LastStandDict = 'combat@damage@writhe'
LastStandAnim = 'writhe_loop'

exports('isDead', function()
return DeathState == sharedConfig.deathState.DEAD
Expand Down Expand Up @@ -101,7 +101,7 @@ local function doLimbAlert()
limbDamageMsg = limbDamageMsg .. Lang:t('info.pain_message', { limb = bodyPart.label, severity = sharedConfig.woundLevels[injury.severity].label})
i += 1
if i < NumInjuries then
limbDamageMsg = limbDamageMsg .. " | "
limbDamageMsg = limbDamageMsg .. ' | '
end
end
else
Expand All @@ -113,8 +113,8 @@ end
---sets ped animation to limping and prevents running.
function MakePedLimp()
if not isInjuryCausingLimp() then return end
lib.requestAnimSet("move_m@injured")
SetPedMovementClipset(cache.ped, "move_m@injured", 1)
lib.requestAnimSet('move_m@injured', 5000)
SetPedMovementClipset(cache.ped, 'move_m@injured', 1)
SetPlayerSprint(cache.playerId, false)
end

Expand Down Expand Up @@ -183,9 +183,9 @@ function ApplyBleed(level)
end

---heals player wounds.
---@param type? "full"|any heals all wounds if full otherwise heals only major wounds.
---@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
if type == 'full' then
resetAllInjuries()
else
resetMinorInjuries()
Expand All @@ -202,22 +202,20 @@ end)

---Revives player, healing all injuries
RegisterNetEvent('qbx_medical:client:playerRevived', function()
local ped = cache.ped

if DeathState ~= sharedConfig.deathState.ALIVE then
local pos = GetEntityCoords(ped, true)
NetworkResurrectLocalPlayer(pos.x, pos.y, pos.z, GetEntityHeading(ped), true, false)
local pos = GetEntityCoords(cache.ped, true)
NetworkResurrectLocalPlayer(pos.x, pos.y, pos.z, GetEntityHeading(cache.ped), true, false)
SetDeathState(sharedConfig.deathState.ALIVE)
SetEntityInvincible(ped, false)
SetEntityInvincible(cache.ped, false)
EndLastStand()
end

SetEntityMaxHealth(ped, 200)
SetEntityHealth(ped, 200)
ClearPedBloodDamage(ped)
SetEntityMaxHealth(cache.ped, 200)
SetEntityHealth(cache.ped, 200)
ClearPedBloodDamage(cache.ped)
SetPlayerSprint(cache.playerId, true)
resetAllInjuries()
ResetPedMovementClipset(ped, 0.0)
ResetPedMovementClipset(cache.ped, 0.0)
TriggerServerEvent('hud:server:RelieveStress', 100)
exports.qbx_core:Notify(Lang:t('info.healthy'), 'inform')
end)
21 changes: 10 additions & 11 deletions client/setdownedstate.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
local isEscorted = false
local vehicleDict = "veh@low@front_ps@idle_duck"
local vehicleAnim = "sit"
local vehicleDict = 'veh@low@front_ps@idle_duck'
local vehicleAnim = 'sit'

function PlayUnescortedLastStandAnimation()
local ped = cache.ped
if cache.vehicle then
lib.requestAnimDict(vehicleDict)
if not IsEntityPlayingAnim(ped, vehicleDict, vehicleAnim, 3) then
TaskPlayAnim(ped, vehicleDict, vehicleAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
lib.requestAnimDict(vehicleDict, 5000)
if not IsEntityPlayingAnim(cache.ped, vehicleDict, vehicleAnim, 3) then
TaskPlayAnim(cache.ped, vehicleDict, vehicleAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
end
else
lib.requestAnimDict(LastStandDict)
if not IsEntityPlayingAnim(ped, LastStandDict, LastStandAnim, 3) then
TaskPlayAnim(ped, LastStandDict, LastStandAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
lib.requestAnimDict(LastStandDict, 5000)
if not IsEntityPlayingAnim(cache.ped, LastStandDict, LastStandAnim, 3) then
TaskPlayAnim(cache.ped, LastStandDict, LastStandAnim, 1.0, 1.0, -1, 1, 0, false, false, false)
end
end
end

---@param ped number
local function playEscortedLastStandAnimation(ped)
if cache.vehicle then
lib.requestAnimDict(vehicleDict)
lib.requestAnimDict(vehicleDict, 5000)
if IsEntityPlayingAnim(ped, vehicleDict, vehicleAnim, 3) then
StopAnimTask(ped, vehicleDict, vehicleAnim, 3)
end
else
lib.requestAnimDict(LastStandDict)
lib.requestAnimDict(LastStandDict, 5000)
if IsEntityPlayingAnim(ped, LastStandDict, LastStandAnim, 3) then
StopAnimTask(ped, LastStandDict, LastStandAnim, 3)
end
Expand Down
Loading

0 comments on commit 451b98b

Please sign in to comment.