HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

SetItems Attaching

05-17-2009, 04:42 AM#1
wraithseeker
[jass] library CIS initializer Init requires Table, UnitProperties

globals
private constant integer BackpackId = 'BAG1'
private constant integer MAX_ABILITIES = 6
endglobals

globals
private trigger Pickup = null
private trigger Drop = null
Table DataTable = 0
Table SetTable = 0
endglobals

struct bonus
integer ItemId = 0
integer Str = 0
integer Agi = 0
integer Int = 0
real Damage = 0.00
real Armor = 0.00
real MaxLife = 0.00
real MaxMana = 0.00
real LifeRegen = 0.00
real ManaRegen = 0.00
real AttackSpeed = 0.00
real MoveSpeed = 0.00
integer array Abilities[MAX_ABILITIES]
integer array AbilityLevels[MAX_ABILITIES]
integer array ItemIds[6]

static method create takes integer ItemId returns bonus
local bonus d = bonus.allocate()
local integer i = 0

loop
set d.Abilities[i] = 0
set d.AbilityLevels[i] = 1
set i = i+1
exitwhen i >= MAX_ABILITIES
endloop

set d.ItemId = ItemId
set DataTable[ItemId] = d

return d
endmethod

static method SetItems takes integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns bonus
local bonus d = bonus.allocate()
local integer i = 0
if i1 != 0 then
set d.ItemIds[0] = i1
elseif i2 != 0 then
set d.ItemIds[1] = i2
elseif i3 != 0 then
set d.ItemIds[2] = i3
elseif i4 != 0 then
set d.ItemIds[3] = i4
elseif i5 != 0 then
set d.ItemIds[4] = i5
elseif i6 != 0 then
set d.ItemIds[5] = i6
endif
loop
set d.Abilities[i] = 0
set d.AbilityLevels[i] = 1
set i = i+1
exitwhen i >= MAX_ABILITIES
endloop
set i = 0
loop
exitwhen i > 6
set SetTable[d.ItemIds[i]] = d
set i = i + 1
endloop
return d
endmethod

static method Item takes item whichItem returns bonus
local bonus d = bonus.allocate()
local integer i = 0
loop
set d.Abilities[i] = 0
set d.AbilityLevels[i] = 1
set i = i+1
exitwhen i >= MAX_ABILITIES
endloop

call SetItemUserData(whichItem,d)
return d
endmethod
endstruct

//! textmacro CheckAndApply takes VALUE
if d.$VALUE$ != 0 then
set $VALUE$[u] = $VALUE$[u] + d.$VALUE$
endif
//! endtextmacro

//! textmacro CheckAndUnapply takes VALUE
if d.$VALUE$ != 0 then
set $VALUE$[u] = $VALUE$[u] + -1*d.$VALUE$
endif
//! endtextmacro

function UnitApplyItem takes unit U, bonus d returns nothing
local integer i

//! runtextmacro CheckAndApply("Str")
//! runtextmacro CheckAndApply("Agi")
//! runtextmacro CheckAndApply("Int")
//! runtextmacro CheckAndApply("Damage")
//! runtextmacro CheckAndApply("Armor")
//! runtextmacro CheckAndApply("MaxLife")
//! runtextmacro CheckAndApply("MaxMana")
//! runtextmacro CheckAndApply("LifeRegen")
//! runtextmacro CheckAndApply("ManaRegen")
//! runtextmacro CheckAndApply("AttackSpeed")
//! runtextmacro CheckAndApply("MoveSpeed")

if d.Abilities[0] != 0 then
set i = 0
loop
call UnitAddAbility(U, d.Abilities[i])
call SetUnitAbilityLevel(U, d.Abilities[i], d.AbilityLevels[i])
set i = i+1
exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
endloop
endif
endfunction

function UnitUnapplyItem takes unit U, bonus d returns nothing
local integer i

//! runtextmacro CheckAndUnapply("Str")
//! runtextmacro CheckAndUnapply("Agi")
//! runtextmacro CheckAndUnapply("Int")
//! runtextmacro CheckAndUnapply("Damage")
//! runtextmacro CheckAndUnapply("Armor")
//! runtextmacro CheckAndUnapply("MaxLife")
//! runtextmacro CheckAndUnapply("MaxMana")
//! runtextmacro CheckAndUnapply("LifeRegen")
//! runtextmacro CheckAndUnapply("ManaRegen")
//! runtextmacro CheckAndUnapply("AttackSpeed")
//! runtextmacro CheckAndUnapply("MoveSpeed")

if d.Abilities[0] != 0 then
set i = 0
loop
call UnitRemoveAbility(U, d.Abilities[i])

set i = i+1
exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
endloop
endif
endfunction

function UnitUnapplyAbilities takes unit U, bonus d returns nothing
local integer i
if d.Abilities[0] != 0 then
set i = 0
loop
call UnitRemoveAbility(U, d.Abilities[i])
set i = i+1
exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
endloop
endif
endfunction

function UnitApplyAbilities takes unit U, bonus d returns nothing
local integer i
if d.Abilities[0] != 0 then
set i = 0
loop
call UnitAddAbility(U, d.Abilities[i])
call SetUnitAbilityLevel(U, d.Abilities[i], d.AbilityLevels[i])
set i = i+1
exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
endloop
endif
endfunction

private function onPickup takes nothing returns nothing
local bonus d = GetItemUserData(GetManipulatedItem())
local unit u = GetTriggerUnit()
local integer i = 0
local boolean array b
local item it
if d != 0 then
call UnitApplyItem(u,d)
else
set d = DataTable[GetItemTypeId(GetManipulatedItem())]
endif
if d > 0 then
call UnitApplyItem(u,d)
endif
loop
exitwhen i > 5
set it = UnitItemInSlot(u,i)
set d = SetTable[GetItemTypeId(it)]
if GetItemTypeId(it) == d.ItemIds[0] or GetItemTypeId(it) == d.ItemIds[1] or GetItemTypeId(it) == d.ItemIds[3] or GetItemTypeId(it) == d.ItemIds[4] or GetItemTypeId(it) == d.ItemIds[5] and it != null then
set b[i] = true
endif
set i = i + 1
endloop
set i = 0
loop
exitwhen i > 5
if b[i] then
call BJDebugMsg("Applied")
call UnitApplyItem(u,d)
endif
set i = i + 1
endloop
set u = null
set it = null
endfunction

private function onDrop takes nothing returns nothing
local bonus d = GetItemUserData(GetManipulatedItem())
local unit u = GetTriggerUnit()
local boolean array b
local integer i = 0
local item it
if d != 0 then
call UnitUnapplyItem(u, d)
else
set d = DataTable[GetItemTypeId(GetManipulatedItem())]
endif
if d > 0 then
call UnitUnapplyItem(u, d)
endif
loop
exitwhen i > 5
set it = UnitItemInSlot(u,i)
set d = SetTable[GetItemTypeId(it)]
if GetItemTypeId(it) == d.ItemIds[0] or GetItemTypeId(it) == d.ItemIds[1] or GetItemTypeId(it) == d.ItemIds[3] or GetItemTypeId(it) == d.ItemIds[4] or GetItemTypeId(it) == d.ItemIds[5] and it != null then
set b[i] = true
endif
set i = i + 1
endloop
set i = 0
loop
exitwhen i > 5
if b[i] then
call BJDebugMsg("Applied")
call UnitUnapplyItem(u,d)
endif
set i = i + 1
endloop
set it = null
set u = null
endfunction

struct backpack
integer array ItemType[6]
integer array Charges[6]
integer array UserData[6]

static method create takes unit u returns backpack
local backpack d = backpack.allocate()
local backpack c
local bonus a
local bonus p
local integer i = 0
local item it
loop
set it = UnitItemInSlot(u, i)
if GetSpellAbilityId() == BackpackId then
set a = GetItemUserData(it)
set d.ItemType[i] = GetItemTypeId(it)
set d.Charges[i] = GetItemCharges(it)
set d.UserData[i] = GetItemUserData(it)
%2
05-18-2009, 03:54 AM#2
wraithseeker
Since there's character limit I'll have to make a new post

Collapse JASS:
library CIS initializer Init requires Table, UnitProperties

globals
    private constant integer BackpackId = 'BAG1'
    private constant integer MAX_ABILITIES = 6   
endglobals

globals
    private trigger Pickup = null
    private trigger Drop = null
    Table DataTable = 0
    Table SetTable = 0
endglobals

struct bonus
        integer ItemId = 0
        integer Str = 0
        integer Agi = 0
        integer Int = 0
        real Damage = 0.00
        real Armor = 0.00
        real MaxLife = 0.00
        real MaxMana = 0.00
        real LifeRegen = 0.00
        real ManaRegen = 0.00
        real AttackSpeed = 0.00
        real MoveSpeed = 0.00
        integer array Abilities[MAX_ABILITIES]
        integer array AbilityLevels[MAX_ABILITIES]
        integer array ItemIds[6]
    
       static method create takes integer ItemId returns bonus
            local bonus d = bonus.allocate()
            local integer i = 0

            loop
                set d.Abilities[i] = 0
                set d.AbilityLevels[i] = 1
                set i = i+1
                exitwhen i >= MAX_ABILITIES
            endloop

            set d.ItemId = ItemId
            set DataTable[ItemId] = d

            return d
        endmethod
        
        static method SetItems takes integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns bonus
            local bonus d = bonus.allocate()
            local integer i = 0
                set d.ItemIds[0] = i1
                set d.ItemIds[1] = i2
                set d.ItemIds[2] = i3
                set d.ItemIds[3] = i4
                set d.ItemIds[4] = i5
                set d.ItemIds[5] = i6
            loop
                set d.Abilities[i] = 0
                set d.AbilityLevels[i] = 1
                set i = i+1
                exitwhen i >= MAX_ABILITIES
            endloop
            set i = 0
            loop
                exitwhen i > 6
                set SetTable[d.ItemIds[i]] = d
                set i = i + 1
            endloop
            return d
        endmethod
        
       static method Item takes item whichItem returns bonus
            local bonus d = bonus.allocate()
            local integer i = 0
            loop
                set d.Abilities[i] = 0
                set d.AbilityLevels[i] = 1
                set i = i+1
                exitwhen i >= MAX_ABILITIES
            endloop

            call SetItemUserData(whichItem,d)
            return d
        endmethod
    endstruct

    //! textmacro CheckAndApply takes VALUE
        if d.$VALUE$ != 0 then
           set $VALUE$[u] = $VALUE$[u] + d.$VALUE$
        endif
    //! endtextmacro

    //! textmacro CheckAndUnapply takes VALUE
        if d.$VALUE$ != 0 then
            set $VALUE$[u] = $VALUE$[u] + -1*d.$VALUE$
        endif
    //! endtextmacro
    

    function UnitApplyItem takes unit U, bonus d returns nothing
        local integer i

        //! runtextmacro CheckAndApply("Str")
        //! runtextmacro CheckAndApply("Agi")
        //! runtextmacro CheckAndApply("Int")
        //! runtextmacro CheckAndApply("Damage")
        //! runtextmacro CheckAndApply("Armor")
        //! runtextmacro CheckAndApply("MaxLife")
        //! runtextmacro CheckAndApply("MaxMana")
        //! runtextmacro CheckAndApply("LifeRegen")
        //! runtextmacro CheckAndApply("ManaRegen")
        //! runtextmacro CheckAndApply("AttackSpeed")
        //! runtextmacro CheckAndApply("MoveSpeed")

        if d.Abilities[0] != 0 then
            set i = 0
            loop
                call UnitAddAbility(U, d.Abilities[i])
                call SetUnitAbilityLevel(U, d.Abilities[i], d.AbilityLevels[i])
                set i = i+1
                exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
            endloop
        endif
    endfunction

    function UnitUnapplyItem takes unit U, bonus d returns nothing
        local integer i

        //! runtextmacro CheckAndUnapply("Str")
        //! runtextmacro CheckAndUnapply("Agi")
        //! runtextmacro CheckAndUnapply("Int")
        //! runtextmacro CheckAndUnapply("Damage")
        //! runtextmacro CheckAndUnapply("Armor")
        //! runtextmacro CheckAndUnapply("MaxLife")
        //! runtextmacro CheckAndUnapply("MaxMana")
        //! runtextmacro CheckAndUnapply("LifeRegen")
        //! runtextmacro CheckAndUnapply("ManaRegen")
        //! runtextmacro CheckAndUnapply("AttackSpeed")
        //! runtextmacro CheckAndUnapply("MoveSpeed")

        if d.Abilities[0] != 0 then
            set i = 0
            loop
                call UnitRemoveAbility(U, d.Abilities[i])

                set i = i+1
                exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
            endloop
        endif
    endfunction
    
    function UnitUnapplyAbilities takes unit U, bonus d returns nothing
        local integer i 
        if d.Abilities[0] != 0 then
            set i = 0
            loop
                call UnitRemoveAbility(U, d.Abilities[i])
                set i = i+1
                exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
            endloop
        endif
        endfunction
        
    function UnitApplyAbilities takes unit U, bonus d returns nothing
        local integer i
        if d.Abilities[0] != 0 then
            set i = 0
            loop
                call UnitAddAbility(U, d.Abilities[i])
                call SetUnitAbilityLevel(U, d.Abilities[i], d.AbilityLevels[i])
                set i = i+1
                exitwhen d.Abilities[i] == 0 or i >= MAX_ABILITIES
            endloop
        endif
        endfunction

    private function onPickup takes nothing returns nothing
        local bonus d = GetItemUserData(GetManipulatedItem())
        local unit u = GetTriggerUnit()
        local integer i = 0
        local boolean array b
        local item it
        local integer array items
        
        if d != 0 then
            call UnitApplyItem(u,d)
        endif
        set d = DataTable[GetItemTypeId(GetManipulatedItem())]
        if d > 0 then
            call UnitApplyItem(u,d)
        endif
             set d = SetTable[GetItemTypeId(GetManipulatedItem())]
            loop
                exitwhen i > 6
                    call BJDebugMsg("creatin")
                    call CreateItem(d.ItemIds[i],GetUnitX(u),GetUnitY(u))
                   
                set i = i + 1
            endloop
        set u = null
        set it = null
    endfunction

    private function onDrop takes nothing returns nothing
        local bonus d = GetItemUserData(GetManipulatedItem())
        local unit u = GetTriggerUnit()
        local boolean array b
        local integer i = 0
        local item it
        if d != 0 then
            call UnitUnapplyItem(u, d)
        else
            set d = DataTable[GetItemTypeId(GetManipulatedItem())]
        endif
        if d > 0 then
            call UnitUnapplyItem(u, d)
        endif
            loop
                set it = UnitItemInSlot(u,i)
                set d = SetTable[GetItemTypeId(it)]
                loop
                    if GetItemTypeId(it) == d.ItemIds[0] or GetItemTypeId(it) == d.ItemIds[1] or GetItemTypeId(it) == d.ItemIds[3] or GetItemTypeId(it) == d.ItemIds[4] or GetItemTypeId(it) == d.ItemIds[5] and it != null then
                    set b[i] = true
                    exitwhen true
                    endif
                endloop
                set i = i + 1
                exitwhen i > 5
            endloop
            set i = 0
            loop
                exitwhen i > 5
                if b[i] then
                call BJDebugMsg("Unapplied")
                call UnitUnapplyItem(u,d)
                endif
                set i = i + 1
            endloop
        set it = null
        set u = null
    endfunction
    
struct backpack
    integer array ItemType[6]
    integer array Charges[6]
    integer array UserData[6]    
    
    static method create takes unit u returns backpack
        local backpack d = backpack.allocate()
        local backpack c
        local bonus a
        local bonus p
        local integer i = 0
        local item it
        loop
            set it = UnitItemInSlot(u, i)
            if GetSpellAbilityId() == BackpackId then
                set a = GetItemUserData(it)
                set d.ItemType[i] = GetItemTypeId(it)
                set d.Charges[i] = GetItemCharges(it)
                set d.UserData[i] = GetItemUserData(it)
                set p = SetTable[d.ItemType[i]]
                call BJDebugMsg(I2S(p))
                call UnitApplyItem(u,p)
                call UnitApplyAbilities(u,p)
                if a == 0 then
                    set c = DataTable[d.ItemType[i]]
                    call UnitApplyItem(u,c)
                    call RemoveItem(it)
                    call UnitApplyAbilities(u,c)
                else
                    call UnitApplyItem(u,a)
                    call RemoveItem(it)
                    call UnitApplyAbilities(u,a)
                endif
           
            else
                set d.ItemType[i] = 0
                set d.Charges[i] = 0
                set d.UserData[i] = 0
            endif
            set i = i + 1
            exitwhen i > 5
        endloop
        set it = null
        return d
    endmethod
    implement AutoData
endstruct

function ActivateBackpack takes unit target returns nothing
    local backpack d1
    local backpack d2 = backpack[target]
    local backpack d
    local integer i = 0
    local item it
    local bonus a
    local bonus p
    if GetSpellAbilityId() == BackpackId then
        set d1 = backpack.create(target)
        if d2 != 0 then
            loop
                if d2.ItemType[i] != 0  then
                    set it = UnitAddItemById(target, d2.ItemType[i])
                    call SetItemCharges(it, d2.Charges[i])
                    call SetItemUserData(it, d2.UserData[i])
                    set d = DataTable[d2.ItemType[i]]
                    call UnitUnapplyItem(target,d)
                    call UnitApplyAbilities(target,d)
                    set p = SetTable[d2.ItemType[i]]
                    if p == 0 then
                        call BJDebugMsg("started")
                        call UnitApplyItem(target,p)
                        call UnitApplyAbilities(target,p)
                    endif
                endif
                set i = i + 1
                exitwhen i > 5
            endloop
        endif
        if d2 != 0 then
            call d2.destroy()
        endif
        set backpack[target] = d1
    endif
    set it = null
endfunction

private function Init takes nothing returns nothing
    set Pickup = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(Pickup, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(Pickup, function onPickup)
    set Drop = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(Drop, EVENT_PLAYER_UNIT_DROP_ITEM)
    call TriggerAddAction(Drop, function onDrop)
endfunction

endlibrary


This is just another trial and error by me.
The second one is a newer verison which doesn't work too

Right now when the items get switched to the backpack, the bonuses disappear, problem above also exists.

I think the problem might be
Collapse JASS:
     if GetItemTypeId(it) == d.ItemIds[0] or GetItemTypeId(it) == d.ItemIds[1] or GetItemTypeId(it) == d.ItemIds[3] or GetItemTypeId(it) == d.ItemIds[4] or GetItemTypeId(it) == d.ItemIds[5] and it != null then[/JASS But I don't know how to do it another way.