Cohadar
master of fugue
- Reaction score
- 209
Code:
//==============================================================================
// ITEM DROP SYSTEM BY COHADAR v1.1
//==============================================================================
//
// PURPOUSE:
// Creating dynamic custom item tables
//
// PROS:
// * It can set a drop chance for every item (naturally)
// * It can drop items with a set number of charges
// * It can set a custom values for items.
// * If you don't use charges or custom values
// you can simpy ignore them and the system will work ok
// * function for checking if your tables are valid
//
// CONS:
// * It is boring to type all those tables
// But there is no better way anyway.
//
// HOW TO IMPORT:
// * Just create a trigger named >> ItemDropSystem <<
// * convert it to text and replace the whole trigger text with this one
//==============================================================================
library ItemDropSystem
globals
constant integer ITEM_TABLE_SIZE = 10 // you can increase this if you want
endglobals
//------------------------------------------------------------------------------
// entries in table with itemId == 0 represent the chance of nothing to be dropped
// total sum of chances in table must be between 0 and 100
//------------------------------------------------------------------------------
struct ItemTable
integer array itemId[ITEM_TABLE_SIZE]
integer array dropChance[ITEM_TABLE_SIZE]
integer array charges[ITEM_TABLE_SIZE]
integer array customValue[ITEM_TABLE_SIZE]
endstruct
//------------------------------------------------------------------------------
// Checks if table has valid dropChances
// You don't need to use this functions but it is recommended as safety method
// You can call this only once at map initialization
// probably right after you create your custom table
//------------------------------------------------------------------------------
function isValidTable takes ItemTable t returns boolean
local integer i = 0
local integer cumul = 0
loop
exitwhen i> ITEM_TABLE_SIZE
if (t.dropChance[i] < 0) then
debug call BJDebugMsg("ERROR in isValidTable() : negative drop chance")
return false
else
set cumul = cumul + t.dropChance[i]
endif
if (cumul > 100) then
debug call BJDebugMsg("ERROR in isValidTable() : total drop chance greater than 100 percent")
return false
endif
set i = i + 1
endloop
return true
endfunction
//------------------------------------------------------------------------------
// Returns random index in item table (i.e. random item from table)
// r must be a number between 0 and 100,
// it represents the percent by witch we calculate what item to drop
// You probably don't need to use this function directly
//------------------------------------------------------------------------------
function getItemFromTableAndNumber takes ItemTable t, integer r returns integer
local integer cumul = 0
local integer i = 0
if (r < 0 or r > 100) then
debug call BJDebugMsg("ERROR in getItemFromTableAndNumber() : invalid random value")
return 0
endif
loop
exitwhen i >= ITEM_TABLE_SIZE
set cumul = cumul + t.dropChance[i]
if (cumul >= r) then
return i
endif
set i = i + 1
endloop
return ITEM_TABLE_SIZE // invalid index, means no items are dropped
endfunction
//------------------------------------------------------------------------------
// Creates random item from table at (x,y) also returns that item
//------------------------------------------------------------------------------
function dropRandomItemXY takes real x, real y, ItemTable t returns item
local integer i // index in item table
local item ret // item we will create
set i = getItemFromTableAndNumber(t, GetRandomInt(0, 100))
if (i >= ITEM_TABLE_SIZE) then
// this is not an error, invalid table index means that no item should be dropped
// explanation:
// if all items in table have 25% total drop chance
// than there is 75% percent chance that no item is dropped
return null
endif
if (t.itemId[i] == 0) then
// this is not an error, table can contain "no item" entries
return null
else
set ret = CreateItem(t.itemId[i], x, y)
if (t.charges[i] > 0) then
call SetItemCharges( ret, t.charges[i] )
endif
call SetItemUserData( ret, t.customValue[i])
return ret
endif
endfunction
//------------------------------------------------------------------------------
// Creates random item from table at unit position also returns that item
// You will probably use this to drop items from dying unit
//------------------------------------------------------------------------------
function dropRantomItem takes unit u, ItemTable t returns item
return dropRandomItemXY(GetUnitX(u), GetUnitY(u), t)
endfunction
endlibrary
//==============================================================================
// END OF ITEM DROP SYSTEM
//==============================================================================
// Forced by WE
function InitTrig_ItemDropSystem takes nothing returns nothing
endfunction
And a little test map so you can see how to properly create tables.