- Joined
- Jan 9, 2005
- Messages
- 2,126
Made upon request from Kam.
Feedback welcome.
Feedback welcome.
library ItemHierarchy /*
ItemHierarchy v1.00
by Spellbound
*/ requires /*
*/ Table /*
DESCRIPTION
ItemHierarchy creates a list of items that automatically upgrade when another item in the
list, aka the hierarchy, is picked up. If you've played the Night Elf campaign in Frozen
Throne, Maieve can at one point collect Shadow Orb Fragments to increase the power of her
Shadow Orb. This is exactly what this system does.
API
ItemHierarchy.create( takes integer itemId, boolean automaticUpgrade )
This method will create the baseline of the hierarchy. The item HAS to be a power-up,
similar to how the Shadow Orb has Shadow Orb Fragments. The baseline is how to reference
the entire hierarchy later on by using it's item id in. See Soul Gem onKill for more info.
the boolean argument should be true by default unless you wish to handle the upgrade
manually and just need the hierarchy as a referrence.
ItemHierarchy.addItem( takes integer baselineId, integer itemId, integer i )
Once your hierarchy has been created, add every subsequent level to it with the above
method. baselineId will identify the hierarchy itself, itemId is the item you wish to
add, and the integer i argument says where in the hierarchy the item goes, aka what is
its level.
ItemHierarchy.setItemLevel( takes unit u, item itm, integer i ) returns item
In the event that you need to manually modify the level of an item, use the above
method. unit u is the carrier of the item, itm is the item itself, and i is the level
you wish the item to have. This method returns the item that was created to replace
the old one.
GetItemHierarchyLevel( takes integer itemId ) returns integer
If an item is inside of a hierarchy, this will return the level of the item inside
that hierarchy. It will NOT return the Object Editor item level.
GetHierarchyTable( takes integer itemId ) returns integer
This returns the instance of the Table in which the items of the hierarchy is stored.
See Soul Gem onKill for more information.
GetHierarchyInstance( takes integer itemId ) returns integer
This returns the instance of the hierarchy itself, allowing you to reference values like
this.maxItemLevels, etc. See Soul Gem onKill for more info.
*/
globals
private trigger itemChangeTrig = CreateTrigger()
private Table instanceStorage
endglobals
function GetItemHierarchyLevel takes integer itemId returns integer
local ItemHierarchy this = instanceStorage.integer[itemId]
return this.itemLevel
endfunction
function GetHierarchyTable takes integer itemId returns integer
local ItemHierarchy this = instanceStorage.integer[itemId]
return this.tableInstance
endfunction
function GetHierarchyInstance takes integer itemId returns integer
return instanceStorage.integer[itemId]
endfunction
struct IH
static Table LIST
endstruct
struct ItemHierarchy
boolean isBaselineItem
boolean isUpgradeAutomatic
integer instance
integer tableInstance
integer baselineInstance
integer itemLevel
integer maxItemLevels
static method setItemLevel takes unit u, item itm, integer i returns item
local integer itemId = GetItemTypeId(itm)
local thistype this = instanceStorage.integer[itemId]
local integer slot = 0
if itm != null then
call DisableTrigger(itemChangeTrig)
loop
exitwhen itm == UnitItemInSlot(u, slot) or slot >= bj_MAX_INVENTORY
set slot = slot + 1
endloop
call RemoveItem(itm)
set IH.LIST = this.tableInstance
call UnitAddItemToSlotById(u, IH.LIST.integer[i], slot)
call EnableTrigger(itemChangeTrig)
endif
return UnitItemInSlot(u, slot)
endmethod
private static method upgradeItemAutomatic takes nothing returns boolean
local item itm = GetManipulatedItem()
local unit u = GetTriggerUnit()
local integer itemId = GetItemTypeId(itm)
local thistype newItem = instanceStorage.integer[itemId]
local integer i
local integer slot
local item indexItem
local thistype baseline = newItem.baselineInstance
local integer levelCount = 0
local integer levelOverflow = 0
local real x
local real y
if baseline.isUpgradeAutomatic then
if newItem != 0 then
set IH.LIST = baseline.tableInstance
set i = 0
loop
set i = i + 1
exitwhen i >= baseline.maxItemLevels //set the loop to ignore max-levelled items
set slot = 0 //inventory slots start on zero in JASS
loop
set indexItem = UnitItemInSlot(u, slot)
if (indexItem != null) and (GetItemTypeId(indexItem) == IH.LIST.integer[i]) then
set levelCount = levelCount + i
call RemoveItem(indexItem)
endif
set slot = slot + 1
exitwhen slot >= bj_MAX_INVENTORY
endloop
endloop
set indexItem = null
call DisableTrigger(itemChangeTrig)
if newItem.isBaselineItem then
set levelCount = levelCount + 1
endif
set x = GetUnitX(u)
set y = GetUnitY(u)
if levelCount > baseline.maxItemLevels then
set levelOverflow = levelCount - baseline.maxItemLevels
call UnitAddItem(u, CreateItem(IH.LIST.integer[baseline.maxItemLevels], x, y))
loop
if levelOverflow > baseline.maxItemLevels then
call UnitAddItem(u, CreateItem(IH.LIST.integer[baseline.maxItemLevels], x, y))
set levelOverflow = levelOverflow - baseline.maxItemLevels
else
call UnitAddItem(u, CreateItem(IH.LIST.integer[levelOverflow], x, y))
set levelOverflow = 0
endif
exitwhen levelOverflow == 0
endloop
else
call UnitAddItem(u, CreateItem(IH.LIST.integer[levelCount], x, y))
endif
call EnableTrigger(itemChangeTrig)
endif
endif
return true
endmethod
static method addItem takes integer baselineId, integer itemId, integer i returns nothing
local thistype baseline = instanceStorage.integer[baselineId]
local thistype newItem = instanceStorage.integer[itemId]
if baseline != 0 and newItem == 0 then
set IH.LIST = baseline.tableInstance
set IH.LIST.integer[i] = itemId
set newItem = allocate()
set newItem.itemLevel = i
set newItem.tableInstance = IH.LIST
set newItem.isBaselineItem = false
set newItem.baselineInstance = baseline
set newItem.isUpgradeAutomatic = false
set instanceStorage.integer[itemId] = newItem
set baseline.maxItemLevels = baseline.maxItemLevels + 1
endif
endmethod
static method create takes integer itemId, boolean automaticUpgrade returns thistype
local thistype baseline
if instanceStorage.integer[itemId] == 0 then
set IH.LIST = Table.create()
set IH.LIST.integer[0] = itemId
set baseline = allocate()
set baseline.tableInstance = IH.LIST
set baseline.maxItemLevels = 0
set baseline.isBaselineItem = true
set baseline.itemLevel = 1
set baseline.baselineInstance = baseline
set baseline.isUpgradeAutomatic = automaticUpgrade
set instanceStorage.integer[itemId] = baseline
endif
return baseline
endmethod
private static method onInit takes nothing returns nothing
set instanceStorage = Table.create()
call TriggerRegisterAnyUnitEventBJ(itemChangeTrig, EVENT_PLAYER_UNIT_PICKUP_ITEM)
call TriggerAddCondition(itemChangeTrig, function thistype.upgradeItemAutomatic)
endmethod
endstruct
endlibrary
scope SetupItemLevels initializer init
private function Actions takes nothing returns boolean
local integer baseline
//Shadow Orb
set baseline = 'sorf'
call ItemHierarchy.create(baseline, true)
call ItemHierarchy.addItem(baseline, 'sor1', 1)
call ItemHierarchy.addItem(baseline, 'sor2', 2)
call ItemHierarchy.addItem(baseline, 'sor3', 3)
call ItemHierarchy.addItem(baseline, 'sor4', 4)
call ItemHierarchy.addItem(baseline, 'sor5', 5)
call ItemHierarchy.addItem(baseline, 'sor6', 6)
call ItemHierarchy.addItem(baseline, 'sor7', 7)
call ItemHierarchy.addItem(baseline, 'sor8', 8)
call ItemHierarchy.addItem(baseline, 'sor9', 9)
call ItemHierarchy.addItem(baseline, 'sora', 10)
//Soul Gem
set baseline = 'sg00'
call ItemHierarchy.create(baseline, false)
call ItemHierarchy.addItem(baseline, 'sg01', 1)
call ItemHierarchy.addItem(baseline, 'sg02', 2)
call ItemHierarchy.addItem(baseline, 'sg03', 3)
call ItemHierarchy.addItem(baseline, 'sg04', 4)
call ItemHierarchy.addItem(baseline, 'sg05', 5)
return false
endfunction
private function init takes nothing returns nothing
local trigger trig = CreateTrigger()
call TriggerRegisterTimerEvent(trig, 0., false)
call TriggerAddCondition(trig, function Actions)
set trig = null
endfunction
endscope