Skip to content

Commit

Permalink
Refactor headshot (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 authored Feb 8, 2025
1 parent 5ac6fb3 commit 66a8b20
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 35 deletions.
25 changes: 0 additions & 25 deletions [gameplay]/headshot/headshot.lua

This file was deleted.

21 changes: 11 additions & 10 deletions [gameplay]/headshot/meta.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<meta>
<info author="jbeta" type="script" version="1.1.0" />
<script src="headshot.lua" type="server" />
<settings>
<setting name="*removeHeadOnHeadshot" value="true"
friendlyname="Blows off player's head on headshot"
group="General"
accept="false,true"
desc="Should player lose his head when he gets headshot?" />
</settings>
</meta>
<info name="Headshot" description="Headshot feature for enhanced combat realism and damage effect" author="MTA contributors (github.com/multitheftauto/mtasa-resources)" version="1.0.0" type="script" />
<min_mta_version server="1.6.0" />

<settings>
<setting name="*decap" desc="determines whether the player becomes headless after a headshot" value="[true]" accept="false, true" />
</settings>

<script type="server" src="src/headshotSettings.lua" />
<script type="server" src="src/headshotDamage.lua" />
<script type="server" src="src/headshotSpawn.lua" />
</meta>
30 changes: 30 additions & 0 deletions [gameplay]/headshot/src/headshotDamage.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
addEvent("onPlayerHeadshot", false)
addEvent("onPlayerPreHeadshot", false)

addEventHandler("onPlayerDamage", root,
function(headshotAttacker, headshotCause, headshotBodypart, headshotDamage)
if not headshotAttacker or not isElement(headshotAttacker) or getElementType(headshotAttacker) ~= "player" then
return
end

if headshotBodypart ~= 9 then
return
end

triggerEvent("onPlayerPreHeadshot", source, headshotAttacker, headshotCause, headshotDamage)

if wasEventCancelled() then
return
end

killPed(source, headshotAttacker, headshotCause, headshotBodypart)

triggerEvent("onPlayerHeadshot", source, headshotAttacker, headshotCause)

if not headshotSettingsGet("decap") then
return
end

setPedHeadless(source, true)
end
)
98 changes: 98 additions & 0 deletions [gameplay]/headshot/src/headshotSettings.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
local headshotSettings = {}
local headshotSettingsAvailable = {
{"decap", "boolean"}
}

local function headshotSettingsNumberize(headshotValue)
if not headshotValue then
return
end

if type(headshotValue) == "number" then
return headshotValue
end

if type(headshotValue) == "string" then
return tonumber(headshotValue)
end

return 0
end

local function headshotSettingsBooleanize(headshotValue)
if type(headshotValue) == "boolean" then
return headshotValue
end

if type(headshotValue) == "string" then
return headshotValue == "[true]"
end

return false
end

function headshotSettingsGet(headshotSetting)
if not headshotSetting or type(headshotSetting) ~= "string" then
return
end

return headshotSettings[headshotSetting]
end

function headshotSettingsSet(headshotSetting, headshotValue)
if not headshotSetting or type(headshotSetting) ~= "string" then
return
end

local headshotDot = string.find(headshotSetting, "%.")

if headshotDot and type(headshotDot) == "number" then
headshotSetting = string.sub(headshotSetting, headshotDot + 1)
end

local headshotFound

for _, headshotEntry in ipairs(headshotSettingsAvailable) do
if headshotEntry[1] == headshotSetting then
headshotFound = headshotEntry
break
end
end

if not headshotFound then
return
end

local headshotType = headshotFound[2]

if headshotType == "string" and type(headshotValue) == "string" then
headshotSettings[headshotSetting] = headshotValue
return
end

if headshotType == "number" and type(headshotValue) == "string" then
headshotSettings[headshotSetting] = headshotSettingsNumberize(headshotValue)
return
end

if headshotType == "boolean" then
headshotSettings[headshotSetting] = headshotSettingsBooleanize(headshotValue)
end
end

addEventHandler("onResourceStart", resourceRoot,
function()
for _, headshotEntry in ipairs(headshotSettingsAvailable) do
local headshotSetting = headshotEntry[1]
local headshotValue = get(headshotSetting)

headshotSettingsSet(headshotSetting, headshotValue)
end
end
)

addEventHandler("onSettingChange", root,
function(headshotSetting, _, headshotValue)
headshotSettingsSet(headshotSetting, headshotValue)
end
)
13 changes: 13 additions & 0 deletions [gameplay]/headshot/src/headshotSpawn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
addEventHandler("onPlayerSpawn", root,
function()
if not isPedHeadless(source) then
return
end

if not headshotSettingsGet("decap") then
return
end

setPedHeadless(source, false)
end
)

0 comments on commit 66a8b20

Please # to comment.