Skip to content

Commit aa9782e

Browse files
authored
Merge pull request #367 from srslyyyy/deathpickups-fix
deathpickups: some improvements (#367)
1 parent 3ed3219 commit aa9782e

File tree

2 files changed

+95
-52
lines changed

2 files changed

+95
-52
lines changed
Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,103 @@
1-
local timers = {} -- timers for existing pickups
2-
3-
local function onDeathPickupHit ( player, matchingDimension )
4-
if matchingDimension then
5-
killTimer ( timers[source] )
6-
timers[source] = nil
7-
removeEventHandler ( "onPickupHit", source, onDeathPickupHit )
8-
local weapid = getPickupWeapon ( source )
9-
local weapammo = getPickupAmmo ( source )
10-
destroyElement ( source )
11-
giveWeapon ( player, weapid, weapammo, false )
1+
local pickupTimers = {}
2+
local expireTime = get("timeout")
3+
local onlyCurrentWeapon = get("only_current")
4+
local dropRadius = get("radius")
5+
6+
local function destroyDeathPickup(pickupElement)
7+
if isElement(pickupElement) then
8+
destroyElement(pickupElement)
9+
end
10+
end
11+
12+
local function dropAllWeapons(posX, posY, posZ, droppedWeapons)
13+
local weaponsCount = #droppedWeapons
14+
15+
for wID = 1, weaponsCount do
16+
local weaponData = droppedWeapons[wID]
17+
local weaponID = weaponData[1]
18+
local weaponAmmo = weaponData[2]
19+
local pickupX = posX + dropRadius * math.cos((wID - 1) * 2 * math.pi/weaponsCount)
20+
local pickupY = posY + dropRadius * math.sin((wID - 1) * 2 * math.pi/weaponsCount)
21+
local pickupElement = createPickup(pickupX, pickupY, posZ, 2, weaponID, expireTime, weaponAmmo)
22+
23+
pickupTimers[pickupElement] = setTimer(destroyDeathPickup, expireTime, 1, pickupElement)
1224
end
1325
end
1426

15-
local function destroyDeathPickup ( pickup )
16-
timers[pickup] = nil
17-
removeEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
18-
destroyElement ( pickup )
27+
function onDeathPickupHit(playerElement)
28+
cancelEvent()
29+
giveWeapon(playerElement, getPickupWeapon(source), getPickupAmmo(source), false)
30+
destroyDeathPickup(source)
1931
end
32+
addEventHandler("onPickupHit", resourceRoot, onDeathPickupHit)
33+
34+
function onPlayerWasted()
35+
local posX, posY, posZ = getElementPosition(source)
36+
37+
if onlyCurrentWeapon then
38+
local playerWeapon = getPedWeapon(source)
39+
local validWeapon = playerWeapon and playerWeapon ~= 0
2040

21-
addEventHandler ( "onPlayerWasted", getRootElement (),
22-
function ( source_ammo, killer, killer_weapon, bodypart )
23-
local pX, pY, pZ = getElementPosition ( source )
24-
local timeout = get("timeout")
25-
26-
if get("only_current") then
27-
local source_weapon = getPedWeapon ( source )
28-
if ( source_weapon and source_weapon ~= 0 and source_ammo ) then
29-
local pickup = createPickup ( pX, pY, pZ, 2, source_weapon, timeout, source_ammo )
30-
addEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
31-
timers[pickup] = setTimer ( destroyDeathPickup, timeout, 1, pickup )
41+
if validWeapon then
42+
local totalAmmo = getPedTotalAmmo(source)
43+
local pickupElement = createPickup(posX, posY, posZ, 2, playerWeapon, expireTime, totalAmmo)
44+
45+
pickupTimers[pickupElement] = setTimer(destroyDeathPickup, expireTime, 1, pickupElement)
46+
end
47+
else
48+
local droppedWeapons = {}
49+
50+
for weaponSlot = 0, 12 do
51+
local playerWeapon = getPedWeapon(source, weaponSlot)
52+
local validWeapon = playerWeapon ~= 0
53+
54+
if validWeapon then
55+
local ammoInSlot = getPedTotalAmmo(source, weaponSlot)
56+
57+
droppedWeapons[#droppedWeapons + 1] = {playerWeapon, ammoInSlot}
3258
end
33-
else
34-
local droppedWeapons = {}
35-
for slot=0, 12 do
36-
local ammo = getPedTotalAmmo(source, slot)
37-
if (getPedWeapon(source, slot) ~= 0) then
38-
local weapon = getPedWeapon(source, slot)
39-
local ammo = getPedTotalAmmo(source, slot)
40-
table.insert(droppedWeapons, {weapon, ammo})
41-
end
59+
end
60+
61+
dropAllWeapons(posX, posY, posZ, droppedWeapons)
62+
end
63+
end
64+
addEventHandler("onPlayerWasted", root, onPlayerWasted)
65+
66+
function onElementDestroyPickup()
67+
local validElement = isElement(source)
68+
69+
if validElement then
70+
local pickupType = getElementType(source) == "pickup"
71+
72+
if pickupType then
73+
local pickupTimer = pickupTimers[source]
74+
75+
if pickupTimer then
76+
killTimer(pickupTimer)
77+
pickupTimers[source] = nil
4278
end
43-
DropAllWeapons(droppedWeapons)
4479
end
4580
end
46-
)
47-
48-
function DropAllWeapons ( droppedWeapons )
49-
local radius = get("radius")
50-
local numberDropped = #droppedWeapons
51-
for i, t in ipairs(droppedWeapons) do
52-
local pX, pY, pZ = getElementPosition ( source )
53-
local x = pX + radius * math.cos((i-1) * 2 * math.pi / numberDropped)
54-
local y = pY + radius * math.sin((i-1) * 2 * math.pi / numberDropped)
55-
local timeout = get("timeout")
56-
local pickup = createPickup(x, y, pZ, 2, t[1], timeout, t[2])
57-
addEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
58-
timers[pickup] = setTimer ( destroyDeathPickup, timeout, 1, pickup )
81+
end
82+
addEventHandler("onElementDestroy", resourceRoot, onElementDestroyPickup)
83+
84+
function onSettingChange(settingName, _, newValue)
85+
local expireSetting = settingName == "*deathpickups.timeout"
86+
87+
if expireSetting then
88+
expireTime = fromJSON(newValue)
89+
end
90+
91+
local weaponSetting = settingName == "*deathpickups.only_current"
92+
93+
if weaponSetting then
94+
onlyCurrentWeapon = fromJSON(newValue)
95+
end
96+
97+
local radiusSetting = settingName == "*deathpickups.radius"
98+
99+
if radiusSetting then
100+
dropRadius = fromJSON(newValue)
59101
end
60102
end
103+
addEventHandler("onSettingChange", resourceRoot, onSettingChange)

[gameplay]/deathpickups/meta.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<meta>
2-
<info author="erorr404" type="script" version="1.0.1" />
3-
<script src="deathpickups.lua" />
2+
<info author="erorr404" type="script" version="1.1"/>
3+
<script src="deathpickups.lua" type="server"/>
44
<settings>
55
<setting name="*timeout" value="[30000]"/>
66
<setting name="*only_current" value="[false]"/>
77
<setting name="*radius" value="[1]"/>
88
</settings>
9-
</meta>
9+
</meta>

0 commit comments

Comments
 (0)