- Joined
- Oct 24, 2012
- Messages
- 6,545
Use table. Really, there's no way why you should use arrays here. It limits the amount of registry entries, destroys flexibility and causes an unneccessary complicated and inefficient add method. Have you benchmarked this? I highly doubt your array approach is faster than simple table calls. Sure, table calls require roughly twice the time of an array read, but this doesn't matter in this situation as the additional overhead totally overshadows the minor speed gain.The looping in the add item is just to set the indexes since I use arrays in this instead of table array.
struct ItemDropSystem
.static method create takes integer unitTypeId returns thistype
static method specificUnitCreate takes unit u returns thistype
method changeMultiDrop takes integer multi returns nothing
static method returnTable takes integer id returns thistype
static method createForType takes integer unitTypeId returns thistype
static method createForUnit takes unit u returns thistype
// to replace the multidrop functions
method getDropCount takes nothing returns integer
method setDropCount takes integer i returns nothing
static getDataTable takes integer id returns thistype
// add 'I000' drop with weight of 5
gnollDrop.addItem('I000', 5)
// at this point:
// 'I000' -> 5/5 -> 100% drop chance
// add 'I001' drop with weight of 10
gnollDrop.addItem('I001', 10)
// at this point:
// 'I000' -> 5/15 -> 33.333% drop chance
// 'I001' -> 10/15 -> 66.666% drop chance
Pretty neat, though I still like mine better... hahahahaha...
mainly because its easier for me to use (coz I made it, lol)
and because it supports both %chance and weight registrations...
and also because aside from the drop chances of the items, you can also specify the chance that a unit can drop an item... (like a unit should only drop an item from the table, 15% of the time)
well, yours can too I guess using the noWeight drop though I think that's a bit slower since you would still go through the table and also, the user will need to adjust the weights in order to achieve his desired "NO DROP" chance...
static method copyForType takes integer unitID, integer copyFromID returns thistype
/*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 returns thistype
/ * - this is used for creating item tables for specific units do not call the create function for these units.
/ *
/ * static method destroy takes nothing returns nothing
/ * - to destroy an item drop table for the unit type id
*/
method destroy takes nothing returns nothing
if specificUnit then
call unitIdTable.remove( this.unitTypeId)
else
call unitTypeTable.remove( this.unitTypeId)
endif
call this.deallocate()
endmethod
non-100% chance
if unitTypeTable.has( id) then
set this = unitTypeTable[ id]
elseif unitIdTable.has( GetHandleId( u)) then
set this = unitIdTable[ GetHandleId( u)]
else
return false
endif
itemDropTable
itemIdTable
//you'll just need to add another set of that
itemDropTableUnit
itemIdTableUnit
//something like that
loop
exitwhen L > this.multiDrop
set r = GetRandomInt( 1, this.ttlWeight)
loop
exitwhen L1 > end
if r < weights[L1] then
call CreateItem( itemIdTable[this][L1], x, y)
set u = null
return false //THIS!!!
endif
set L1 = L1 + 1
endloop
set L = L + 1
endloop
loop
exitwhen L > this.multiDrop
set r = GetRandomInt( 1, this.ttlWeight)
loop
exitwhen L1 > end
if r < weights[L1] then
call CreateItem( itemIdTable[this][L1], x, y)
set L1 = end
endif
set L1 = L1 + 1
endloop
set L = L + 1
endloop
local unit u = GetTriggerUnit()
local integer id = GetUnitTypeId( GetTriggerUnit())
local thistype this
local integer r
local real x = GetUnitX( u)
local real y = GetUnitY( u)
local integer array weights
local integer L = 1
local integer L1 = 1
local integer end = 0
if unitTypeTable.has( id) then
set this = unitTypeTable[ id]
elseif unitIdTable.has( GetHandleId( u)) then
set this = unitIdTable[ GetHandleId( u)]
else
return false
//Since this terminates the process already
//you should have a set u = null here
endif
method destroy takes nothing returns nothing
if specificUnit then
call unitIdTable.remove( this.unitTypeId)
else
call unitTypeTable.remove( this.unitTypeId)
endif
call this.deallocate()
endmethod
local integer id = GetHandleId( u)
set this.unitTypeId = id
set this.unitTypeId = GetHandleId( u)