Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
141 changes: 92 additions & 49 deletions [gameplay]/deathpickups/deathpickups.lua
Original file line number Diff line number Diff line change
@@ -1,60 +1,103 @@
local timers = {} -- timers for existing pickups

local function onDeathPickupHit ( player, matchingDimension )
if matchingDimension then
killTimer ( timers[source] )
timers[source] = nil
removeEventHandler ( "onPickupHit", source, onDeathPickupHit )
local weapid = getPickupWeapon ( source )
local weapammo = getPickupAmmo ( source )
destroyElement ( source )
giveWeapon ( player, weapid, weapammo, false )
local pickupTimers = {}
local expireTime = get("timeout")
local onlyCurrentWeapon = get("only_current")
local dropRadius = get("radius")

local function destroyDeathPickup(pickupElement)
if isElement(pickupElement) then
destroyElement(pickupElement)
end
end

local function dropAllWeapons(posX, posY, posZ, droppedWeapons)
local weaponsCount = #droppedWeapons

for wID = 1, weaponsCount do
local weaponData = droppedWeapons[wID]
local weaponID = weaponData[1]
local weaponAmmo = weaponData[2]
local pickupX = posX + dropRadius * math.cos((wID - 1) * 2 * math.pi/weaponsCount)
local pickupY = posY + dropRadius * math.sin((wID - 1) * 2 * math.pi/weaponsCount)
local pickupElement = createPickup(pickupX, pickupY, posZ, 2, weaponID, expireTime, weaponAmmo)

pickupTimers[pickupElement] = setTimer(destroyDeathPickup, expireTime, 1, pickupElement)
end
end

local function destroyDeathPickup ( pickup )
timers[pickup] = nil
removeEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
destroyElement ( pickup )
function onDeathPickupHit(playerElement)
cancelEvent()
giveWeapon(playerElement, getPickupWeapon(source), getPickupAmmo(source), false)
destroyDeathPickup(source)
end
addEventHandler("onPickupHit", resourceRoot, onDeathPickupHit)

function onPlayerWasted()
local posX, posY, posZ = getElementPosition(source)

if onlyCurrentWeapon then
local playerWeapon = getPedWeapon(source)
local validWeapon = playerWeapon and playerWeapon ~= 0

addEventHandler ( "onPlayerWasted", getRootElement (),
function ( source_ammo, killer, killer_weapon, bodypart )
local pX, pY, pZ = getElementPosition ( source )
local timeout = get("timeout")

if get("only_current") then
local source_weapon = getPedWeapon ( source )
if ( source_weapon and source_weapon ~= 0 and source_ammo ) then
local pickup = createPickup ( pX, pY, pZ, 2, source_weapon, timeout, source_ammo )
addEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
timers[pickup] = setTimer ( destroyDeathPickup, timeout, 1, pickup )
if validWeapon then
local totalAmmo = getPedTotalAmmo(source)
local pickupElement = createPickup(posX, posY, posZ, 2, playerWeapon, expireTime, totalAmmo)

pickupTimers[pickupElement] = setTimer(destroyDeathPickup, expireTime, 1, pickupElement)
end
else
local droppedWeapons = {}

for weaponSlot = 0, 12 do
local playerWeapon = getPedWeapon(source, weaponSlot)
local validWeapon = playerWeapon ~= 0

if validWeapon then
local ammoInSlot = getPedTotalAmmo(source, weaponSlot)

droppedWeapons[#droppedWeapons + 1] = {playerWeapon, ammoInSlot}
end
else
local droppedWeapons = {}
for slot=0, 12 do
local ammo = getPedTotalAmmo(source, slot)
if (getPedWeapon(source, slot) ~= 0) then
local weapon = getPedWeapon(source, slot)
local ammo = getPedTotalAmmo(source, slot)
table.insert(droppedWeapons, {weapon, ammo})
end
end

dropAllWeapons(posX, posY, posZ, droppedWeapons)
end
end
addEventHandler("onPlayerWasted", root, onPlayerWasted)

function onElementDestroyPickup()
local validElement = isElement(source)

if validElement then
local pickupType = getElementType(source) == "pickup"

if pickupType then
local pickupTimer = pickupTimers[source]

if pickupTimer then
killTimer(pickupTimer)
pickupTimers[source] = nil
end
DropAllWeapons(droppedWeapons)
end
end
)

function DropAllWeapons ( droppedWeapons )
local radius = get("radius")
local numberDropped = #droppedWeapons
for i, t in ipairs(droppedWeapons) do
local pX, pY, pZ = getElementPosition ( source )
local x = pX + radius * math.cos((i-1) * 2 * math.pi / numberDropped)
local y = pY + radius * math.sin((i-1) * 2 * math.pi / numberDropped)
local timeout = get("timeout")
local pickup = createPickup(x, y, pZ, 2, t[1], timeout, t[2])
addEventHandler ( "onPickupHit", pickup, onDeathPickupHit )
timers[pickup] = setTimer ( destroyDeathPickup, timeout, 1, pickup )
end
addEventHandler("onElementDestroy", resourceRoot, onElementDestroyPickup)

function onSettingChange(settingName, _, newValue)
local expireSetting = settingName == "*deathpickups.timeout"

if expireSetting then
expireTime = fromJSON(newValue)
end

local weaponSetting = settingName == "*deathpickups.only_current"

if weaponSetting then
onlyCurrentWeapon = fromJSON(newValue)
end

local radiusSetting = settingName == "*deathpickups.radius"

if radiusSetting then
dropRadius = fromJSON(newValue)
end
end
addEventHandler("onSettingChange", resourceRoot, onSettingChange)
6 changes: 3 additions & 3 deletions [gameplay]/deathpickups/meta.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<meta>
<info author="erorr404" type="script" version="1.0.1" />
<script src="deathpickups.lua" />
<info author="erorr404" type="script" version="1.1"/>
<script src="deathpickups.lua" type="server"/>
<settings>
<setting name="*timeout" value="[30000]"/>
<setting name="*only_current" value="[false]"/>
<setting name="*radius" value="[1]"/>
</settings>
</meta>
</meta>