Moderator
M
Moderator
21:14, 15th Jul 2013
PurgeandFire: Changes have been made. Approved!
PurgeandFire: Changes have been made. Approved!
library UnitItemDrops /* by Deathismyfriend version 1.1.0.1
================================ Requirements =================================
Requires JNGP
*/uses Table, /* [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url] by bribe
* Alloc * [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-alternative-221493/[/url] by nestharus
*************************************************************************************
*
* Used to easily make drop tables for item drops when unit dies.
*
************************************************************************************
*
* Installing
* STEP 1: Install JNGP first if you dont have JNGP.
* STEP 2: Install Table by bribe the link is above.
* STEP 3: Install Alloc by nestharus the link is above. You may be able to use some other alloc systems.
* STEP 4: Copy and paste this library to your map.
*
************************************************************************************
*
* Description
* -----------------------
* This is a very easy item drop system.
* Allows for multi-Chance dropping of items w a simple method call after u call the create method.
*
* Capabilities
* -----------------------
* Allows the user to easily create an item recipe.
* - Easily create item tables for units that drop on percent chance.
* - You can add as many items as wanted for each table for the corresponding unit type id.
*
******************************************************************************************************************************************************************************************************
/ ******************************************************************************************************************************************************************************************************
/ *
/ * This uses a Weighted System for dropping. It does not use a percent chance drop.
/ *
/ * CREATORS / DESTRUCTORS
/ * ----------------------
/ * struct ItemDropSystem extends array
/ *
/ * static method createForType takes integer unitTypeId returns thistype
/ * - this is what you need to start an item drop table.
/ * - Below is how u add the items to the table
/ *
/ * static method createForUnit takes unit u, boolean dropTypeTableAlso returns thistype
/ * - this is used for creating item tables for specific units do not call the create function for these units.
/ * - Below is how u add the items to the table
/ * - If you have a table for the unit type. Made by the createForType method and
/ * - - want both tables to drop an item set this to true.
/ *
/ * method destroy takes nothing returns nothing
/ * - to destroy an item drop table for the unit type id
/ *
/ *
/ * FIELDS
/ * ------
/ * method addItem takes integer itemId, integer weight returns nothing
/ * - this is how u add items to the unit drop table.
/ * - The weight is the chance this item will be dropped.
/ *
/ * method addZeroDropWeight takes integer weight returns nothing
/ * - this is how u add a weight for no item drops.
/ *
/ * static method duplicateDropTable takes integer newUnitID, boolean createForUnitNew, integer oldUnitID, boolean createForUnitOld returns thistype
/ * - this allows you to duplicate an item drop table u previously created.
/ * - You need to tell what the old drop table was.
/ * - If you used the createForUnit method in the old drop table then you set the old one to true.
/ * - As for the new drop table its the same way. If u want to create it for a specific unit you set it to true.
/ * - Note: if you want to create it for a specific unit you need to use GetHandle( ur unit) as the integer.
/ *
/ * method setDropCount takes integer multi returns nothing
/ * - allows you to change the multi drop.
/ * - Set multidrop integer to 2 or higher for multidrop chance
/ *
/ * method getDropCount takes nothing returns integer
/ * - returns the multi drop integer that u have set.
/ *
/ * static method getDataTable takes integer id returns thistype
/ * - this returns the item Table so u can easily add items to the table or destroy it if u want
/ *
/ ********************************************************************************************************************************/
globals
private Table unitTypeTable
private Table unitIdTable
private TableArray itemDropTable
private TableArray itemIdTable
endglobals
struct ItemDropSystem extends array
implement Alloc
private integer index
private integer arrayI
private integer checkPercent
private integer weight
private integer ttlWeight
private integer unitTypeId
private integer multiDrop
private boolean specificUnit
private boolean dropTypeTableAlso
private method setValues takes nothing returns nothing
set this.multiDrop = 1
set this.index = 0
set this.weight = 0
endmethod
static method createForType takes integer unitTypeId returns thistype
local thistype this
if unitTypeTable.has(unitTypeId) then
return unitTypeTable[unitTypeId]
else
set this = thistype.allocate()
set this.unitTypeId = unitTypeId
set unitTypeTable[ this.unitTypeId] = this
set this.specificUnit = false
set this.dropTypeTableAlso = false
call this.setValues()
endif
return this
endmethod
static method createForUnit takes unit u, boolean dropTypeTableAlso returns thistype
local thistype this
local integer id = GetHandleId( u)
if unitIdTable.has(id) then
return unitIdTable[id]
else
set this = thistype.allocate()
set this.unitTypeId = id
set unitIdTable[ this.unitTypeId] = this
set this.specificUnit = true
set this.dropTypeTableAlso = dropTypeTableAlso
call this.setValues()
endif
return this
endmethod
static method duplicateDropTable takes integer newUnitID, boolean createForUnitNew, integer oldUnitID, boolean createForUnitOld returns thistype
local thistype this = thistype.allocate()
local thistype data
local integer L = 1
local integer weight
local integer itemId
if createForUnitOld then
set data = unitIdTable[ this.unitTypeId]
else
set data = unitTypeTable[ this.unitTypeId]
endif
if createForUnitNew then
set unitIdTable[ this.unitTypeId] = this
else
set unitTypeTable[ this.unitTypeId] = this
endif
loop
exitwhen L > data.index
set itemId = itemIdTable[data][L]
set itemIdTable[this][L] = itemId
set weight = itemDropTable[data][itemId]
set this.ttlWeight = this.ttlWeight + weight
set itemDropTable[this][itemId] = this.ttlWeight
set L = L + 1
endloop
set this.specificUnit = createForUnitNew
call this.setValues()
return this
endmethod
method setDropCount takes integer multi returns nothing
set this.multiDrop = multi
endmethod
method getDropCount takes nothing returns integer
return this.multiDrop
endmethod
static method getDataTable takes integer id returns thistype
if unitTypeTable.has( id) then
return unitTypeTable[ id]
endif
return unitIdTable[ id]
endmethod
method addZeroDropWeight takes integer weight returns nothing
set this.index = this.index + 1
set this.ttlWeight = this.ttlWeight + weight
set itemIdTable[this][index] = 100000
set itemDropTable[this][100000] = this.ttlWeight
endmethod
method addItem takes integer itemId, integer weight returns nothing
set this.index = this.index + 1
set this.ttlWeight = this.ttlWeight + weight
set itemIdTable[this][index] = itemId
set itemDropTable[this][itemId] = this.ttlWeight
endmethod
private method dropItem takes real x, real y returns boolean
local integer r
local integer L = 1
local integer L1 = 1
local integer end = 0
local integer array weights
set weights[0] = 0
if this != 0 then
loop
exitwhen L > this.index
set weights[L] = itemDropTable[this][itemIdTable[this][L]]
set end = end + 1
set L = L + 1
endloop
set L = 1
loop
exitwhen L > this.multiDrop
set r = GetRandomInt( 1, this.ttlWeight)
loop
exitwhen L1 > end
if r > weights[L1 - 1] and r <= weights[L1] then
call CreateItem( itemIdTable[this][L1], x, y)
endif
set L1 = L1 + 1
endloop
set L = L + 1
endloop
endif
return this.dropTypeTableAlso
endmethod
method destroy takes nothing returns nothing
local integer L = 0
if this.specificUnit then
call unitIdTable.remove( this.unitTypeId)
else
call unitTypeTable.remove( this.unitTypeId)
endif
call this.deallocate()
endmethod
private static method deathEvent takes nothing returns boolean
local unit u = GetTriggerUnit()
local integer id = GetUnitTypeId( u)
local thistype this
local real x = GetUnitX( u)
local real y = GetUnitY( u)
local boolean b = true
if unitIdTable.has( GetHandleId( u)) then
set this = unitIdTable[ GetHandleId( u)]
set b = this.dropItem( x, y)
call destroy()
endif
if unitTypeTable.has( id) and b then
set this = unitTypeTable[ id]
call this.dropItem( x, y)
endif
set u = null
return false
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
set unitTypeTable = Table.create()
set unitIdTable = Table.create()
set itemIdTable = TableArray[0x2000]
set itemDropTable = TableArray[0x2000]
call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddCondition( t, function thistype.deathEvent)
set t = null
endmethod
endstruct
endlibrary