Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor headshot #591

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions [gameplay]/headshot/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Headshot
Headshot feature for enhanced combat realism and damage effect — Made with 💖 by Multi Theft Developers.

> [!NOTE]
> This resource is a part of the [mtasa-blue](https://github.com/multitheftauto/mtasa-resources) repository, any updates, fixes or patches are only available there.

> [!IMPORTANT]
> The minimum version requirement for the server is 1.6.0. You can always download the latest binaries [here](https://nightly.multitheftauto.com/).

## Settings
| name | description | default | accept |
|-------|-----------------------------------------------------------------|---------|-------------|
| decap | determines whether the player becomes headless after a headshot | true | false, true |

## Events

### onPlayerHeadshot
This event is called when a player is shot in the head and dies.

#### Parameters
```lua
player attacker, int cause
```
- attacker: a player element who was the attacker
- cause: a number representing the attacker weapon or other damage type

#### Cancel
This event cannot be cancelled.

#### Example
```lua
addEventHandler("onPlayerHeadshot", root,
function(attacker, cause)
local name = getPlayerName(source)

if attacker and isElement(attacker) and getElementType(attacker) == "player" then
attacker = getPlayerName(attacker)
else
attacker = "Unknown"
end

if cause and type(cause) == "number" then
cause = getWeaponNameFromID(cause)
else
cause = "Unknown"
end

outputServerLog(name .. " was shot in the head by " .. attacker .. " using " .. cause)
end
)
```

### onPlayerPreHeadshot
This event is called when a player is shot in the head but has not yet been killed by the resource.

#### Parameters
```lua
player attacker, int cause, int damage
```
- attacker: a player element who was the attacker
- cause: a number representing the attacker weapon or other damage type
- damage: a number representing the health originally lost by the shot

#### Cancel
This event can be cancelled, preventing the player from being killed.

#### Example
```lua
addEventHandler("onPlayerPreHeadshot", root,
function(_, _, damage)
if damage < 5 then
cancelEvent()
end
end
)
```
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="Multi Theft Developers" 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
)
Loading