Skip to content

Commit 66a8b20

Browse files
authored
Refactor headshot (#591)
1 parent 5ac6fb3 commit 66a8b20

File tree

5 files changed

+152
-35
lines changed

5 files changed

+152
-35
lines changed

[gameplay]/headshot/headshot.lua

Lines changed: 0 additions & 25 deletions
This file was deleted.

[gameplay]/headshot/meta.xml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<meta>
2-
<info author="jbeta" type="script" version="1.1.0" />
3-
<script src="headshot.lua" type="server" />
4-
<settings>
5-
<setting name="*removeHeadOnHeadshot" value="true"
6-
friendlyname="Blows off player's head on headshot"
7-
group="General"
8-
accept="false,true"
9-
desc="Should player lose his head when he gets headshot?" />
10-
</settings>
11-
</meta>
2+
<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" />
3+
<min_mta_version server="1.6.0" />
4+
5+
<settings>
6+
<setting name="*decap" desc="determines whether the player becomes headless after a headshot" value="[true]" accept="false, true" />
7+
</settings>
8+
9+
<script type="server" src="src/headshotSettings.lua" />
10+
<script type="server" src="src/headshotDamage.lua" />
11+
<script type="server" src="src/headshotSpawn.lua" />
12+
</meta>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
addEvent("onPlayerHeadshot", false)
2+
addEvent("onPlayerPreHeadshot", false)
3+
4+
addEventHandler("onPlayerDamage", root,
5+
function(headshotAttacker, headshotCause, headshotBodypart, headshotDamage)
6+
if not headshotAttacker or not isElement(headshotAttacker) or getElementType(headshotAttacker) ~= "player" then
7+
return
8+
end
9+
10+
if headshotBodypart ~= 9 then
11+
return
12+
end
13+
14+
triggerEvent("onPlayerPreHeadshot", source, headshotAttacker, headshotCause, headshotDamage)
15+
16+
if wasEventCancelled() then
17+
return
18+
end
19+
20+
killPed(source, headshotAttacker, headshotCause, headshotBodypart)
21+
22+
triggerEvent("onPlayerHeadshot", source, headshotAttacker, headshotCause)
23+
24+
if not headshotSettingsGet("decap") then
25+
return
26+
end
27+
28+
setPedHeadless(source, true)
29+
end
30+
)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
local headshotSettings = {}
2+
local headshotSettingsAvailable = {
3+
{"decap", "boolean"}
4+
}
5+
6+
local function headshotSettingsNumberize(headshotValue)
7+
if not headshotValue then
8+
return
9+
end
10+
11+
if type(headshotValue) == "number" then
12+
return headshotValue
13+
end
14+
15+
if type(headshotValue) == "string" then
16+
return tonumber(headshotValue)
17+
end
18+
19+
return 0
20+
end
21+
22+
local function headshotSettingsBooleanize(headshotValue)
23+
if type(headshotValue) == "boolean" then
24+
return headshotValue
25+
end
26+
27+
if type(headshotValue) == "string" then
28+
return headshotValue == "[true]"
29+
end
30+
31+
return false
32+
end
33+
34+
function headshotSettingsGet(headshotSetting)
35+
if not headshotSetting or type(headshotSetting) ~= "string" then
36+
return
37+
end
38+
39+
return headshotSettings[headshotSetting]
40+
end
41+
42+
function headshotSettingsSet(headshotSetting, headshotValue)
43+
if not headshotSetting or type(headshotSetting) ~= "string" then
44+
return
45+
end
46+
47+
local headshotDot = string.find(headshotSetting, "%.")
48+
49+
if headshotDot and type(headshotDot) == "number" then
50+
headshotSetting = string.sub(headshotSetting, headshotDot + 1)
51+
end
52+
53+
local headshotFound
54+
55+
for _, headshotEntry in ipairs(headshotSettingsAvailable) do
56+
if headshotEntry[1] == headshotSetting then
57+
headshotFound = headshotEntry
58+
break
59+
end
60+
end
61+
62+
if not headshotFound then
63+
return
64+
end
65+
66+
local headshotType = headshotFound[2]
67+
68+
if headshotType == "string" and type(headshotValue) == "string" then
69+
headshotSettings[headshotSetting] = headshotValue
70+
return
71+
end
72+
73+
if headshotType == "number" and type(headshotValue) == "string" then
74+
headshotSettings[headshotSetting] = headshotSettingsNumberize(headshotValue)
75+
return
76+
end
77+
78+
if headshotType == "boolean" then
79+
headshotSettings[headshotSetting] = headshotSettingsBooleanize(headshotValue)
80+
end
81+
end
82+
83+
addEventHandler("onResourceStart", resourceRoot,
84+
function()
85+
for _, headshotEntry in ipairs(headshotSettingsAvailable) do
86+
local headshotSetting = headshotEntry[1]
87+
local headshotValue = get(headshotSetting)
88+
89+
headshotSettingsSet(headshotSetting, headshotValue)
90+
end
91+
end
92+
)
93+
94+
addEventHandler("onSettingChange", root,
95+
function(headshotSetting, _, headshotValue)
96+
headshotSettingsSet(headshotSetting, headshotValue)
97+
end
98+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
addEventHandler("onPlayerSpawn", root,
2+
function()
3+
if not isPedHeadless(source) then
4+
return
5+
end
6+
7+
if not headshotSettingsGet("decap") then
8+
return
9+
end
10+
11+
setPedHeadless(source, false)
12+
end
13+
)

0 commit comments

Comments
 (0)