library CustomDrop initializer init
globals
private hashtable Hash_t = InitHashtable()
private location SendToItem = Location(0,0)
endglobals
private function OnDeath takes nothing returns nothing
local unit u = GetDyingUnit()
local integer unitId = GetUnitTypeId(u)
local integer count = LoadInteger(Hash_t,unitId,0) // if the unit type was not registred for drop it will be equal to 0, else it will be > 0
local real random
local integer i
local item drop
local integer dummy = 'I01Q'
loop // loop on all dropable item for unitID
exitwhen count == 0
set i = 0
loop // player loop
exitwhen i == 11
set dummy = 'I01Q'
if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
set random = GetRandomReal(0,100)
if random < LoadReal(Hash_t,unitId,count) then
if GetLocalPlayer() == Player(i) then
set dummy = LoadInteger(Hash_t,unitId,count)
endif
set drop = CreateItem(dummy,GetUnitX(u),GetUnitY(u))
call SetItemUserData(drop, LoadInteger(Hash_t,unitId,count))
endif
endif
set i = i+1
endloop
set count = count-1
endloop
set u = null
set drop = null
endfunction
private function RegisterDrop takes integer unitId , integer itemId, real chanceToDrop returns nothing // chanceToDrop in percent
local integer n = LoadInteger(Hash_t,unitId,0)
/* integer : unitId ; 0 -> number of possible item drop for the unit type unitId
integer : unitId ; x -> rawcode of the item to drop ; x > 0
real : unitId ; x -> chance of the item x to drop
*/
set n = n+1
call SaveInteger(Hash_t,unitId,0,n)
call SaveInteger(Hash_t,unitId,n,itemId)
call SaveReal(Hash_t,unitId,n,chanceToDrop)
endfunction
private function onItemPickup takes nothing returns nothing
local item it = GetManipulatedItem()
local unit u = GetTriggerUnit()
local boolean b
local integer i = 0
local integer i2 = 0
local item dummy
// set rawcode = GetItemUserData(it)
// call RemoveItem(it)
// set it = CreateItem(rawcode, GetUnitX(u), GetUnitY(u))
// call SetItemUserData(it,rawcode)
if GetItemTypeId(it) == GetItemUserData(it) or GetItemTypeId(it) == 'I01Q' then
set dummy = CreateItem(GetItemUserData(it), GetItemX(it), GetItemY(it))
call RemoveItem(it)
call UnitAddItem(u,dummy)
set it = null
set u = null
return
endif
/* loop
set b = UnitItemInSlot(u, i) == null
set i = i + 1
exitwhen i == 5 or b == true
endloop
if b == true then
call UnitAddItem(u, it)
else
set u = udg_Backpack[GetConvertedPlayerId(GetTriggerPlayer())]
set i = 0
loop
set b = UnitItemInSlot(u, i) == null
set i = i + 1
exitwhen i == 6 or b == true
endloop
if b == true then
call UnitAddItem(u, it)
call print("The item has been sent to your backpack")
else
call print("You do not have enough room for that item")
endif
endif*/
endfunction
// private function RegisterDrop takes integer unitId , integer itemId, real chanceToDrop returns nothing //
private function init takes nothing returns nothing
local trigger trig = CreateTrigger()
local trigger trig2 = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(trig, EVENT_PLAYER_UNIT_DEATH)
call TriggerRegisterAnyUnitEventBJ(trig2, EVENT_PLAYER_UNIT_PICKUP_ITEM )
call TriggerAddAction(trig,function OnDeath)
call TriggerAddAction(trig2, function onItemPickup)
call RegisterDrop('n000','I00U',7)
call RegisterDrop('n001','I00U',7)
call RegisterDrop('n002','I00U',7)
call RegisterDrop('n00H','I00U',7)
call RegisterDrop('n00G','I00U',7)
call RegisterDrop('n00B','I01M',10)
call RegisterDrop('n00C','I01M',10)
call RegisterDrop('n00D','I01M',10)
call RegisterDrop('n003','I01M',10)
call RegisterDrop('n005','I01N',10)
call RegisterDrop('n009','I01N',10)
call RegisterDrop('n004','I01N',10)
call RegisterDrop('n007','I01O',25)
endfunction
endlibrary