• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Recipe and Ingredients Error

Status
Not open for further replies.
Level 6
Joined
Nov 10, 2006
Messages
181
JASS:
library CIS initializer Init requires Table, UnitProperties, AutoIndex

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

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

function UnitHasItemOfType takes unit whichUnit, integer itemId returns boolean
    local integer index = 0
    local item indexItem
    loop
        set indexItem = UnitItemInSlot(whichUnit, index)
        if indexItem != null and GetItemTypeId(indexItem) == itemId then
            return true
        endif
        set index = index + 1
        exitwhen index > 5
    endloop
    return false
endfunction

function UnitHasItemOfTypeEnd takes bonus d returns boolean
    local integer i = 0
    loop
        exitwhen i > 5
        if d.Items[i] != 0 then
            return false
        endif
        set i = i + 1
    endloop
    return true
endfunction

struct bonus
        static integer Count = 0
        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 Items[6]
        integer array Abilities[MAX_ABILITIES]
        integer array AbilityLevels[MAX_ABILITIES]


       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 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
        
        static method Recipe 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.Items[0] = i1
            set d.Items[1] = i2
            set d.Items[2] = i3
            set d.Items[3] = i4
            set d.Items[4] = i5
            set d.Items[5] = i6
            loop
                set d.Abilities[i] = 0
                set d.AbilityLevels[i] = 1
                set i = i+1
                exitwhen i >= MAX_ABILITIES
            endloop

            set SetTable[d.Count] = d
            set d.Count = d.Count + 1
            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 boolean
        local bonus d = GetItemUserData(GetManipulatedItem())
        local unit u = GetTriggerUnit()
        local integer i = 0
        local integer r = 0
        local integer array Items
        if d != 0 then
            call UnitApplyItem(u,d)
        else
            set d = DataTable[GetItemTypeId(GetManipulatedItem())]
            if d > 0 then
                call UnitApplyItem(u,d)
            else
                loop
                    exitwhen r >= d.Count
                    set d = SetTable[r]
                    loop
                        exitwhen i > 5
                        set Items[i] = d.Items[i]
                        set i = i + 1
                    endloop
                    set i = 0
                    loop
                        exitwhen i > 5
                        if UnitHasItemOfType(u,Items[i]) then
                            set Items[i] = 0
                        endif
                        set i = i + 1
                    endloop
                    set i = 0
                    loop
                        exitwhen i > 5
                        if Items[i] != 0 then
                            return false
                        endif
                        set i = i + 1
                    endloop
                    if COMBINED[GetUnitId(u)] == false then
                        call UnitApplyItem(u,d)
                        set COMBINED[GetUnitId(u)] = true
                    endif
                    set r = r + 1
                endloop
            endif
        endif
        set u = null
        return true
    endfunction

    private function onDrop takes nothing returns boolean
        local bonus d = GetItemUserData(GetManipulatedItem())
        local unit u = GetTriggerUnit()
        local integer i = 0
        local integer r = 0
        local integer array Items
        if d != 0 then
            call UnitUnapplyItem(u, d)
        else
            set d = DataTable[GetItemTypeId(GetManipulatedItem())]
            if d > 0 then
               call UnitUnapplyItem(u, d)
             else
              loop
                    exitwhen r >= d.Count
                    set d = SetTable[r]
                    loop
                        exitwhen i > 5
                        set Items[i] = d.Items[i]
                        set i = i + 1
                    endloop
                    set i = 0
                    loop
                        exitwhen i > 5
                        if UnitHasItemOfType(u,Items[i]) then
                            set Items[i] = 0
                        endif
                        set i = i + 1
                    endloop
                    set i = 0
                    loop
                        exitwhen i > 5
                        if Items[i] != 0 then
                            return false
                        endif
                        set i = i + 1
                    endloop
                    if COMBINED[GetUnitId(u)] then
                        call UnitUnapplyItem(u,d)
                        set COMBINED[GetUnitId(u)] = false
                    endif
                    set r = r + 1
                endloop
            endif
        endif
        set u = null
        return true
endfunction
    
public 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 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)
                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
    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)
                    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 TriggerAddCondition(Pickup,Condition(function onPickup))
    set Drop = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(Drop, EVENT_PLAYER_UNIT_DROP_ITEM)
    call TriggerAddCondition(Drop, Condition(function onDrop))
endfunction

endlibrary

Right now everything works fine but Recipe making isn't MRI (Multiple Recipe Instanceable)

Initialization of Recipes

JASS:
 set d = bonus.Recipe('I002','I003','I004',0,0,0)
    set d.Damage = 500
    set d = bonus.Recipe('I002','I003',0,0,0,0)
    set d.Str = 1000

Basically the first one will run but the second one will not run, do you all have any clues?
 
Last edited:
Status
Not open for further replies.
Top