• 🏆 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!

Customizable Item System

Level 6
Joined
Nov 10, 2006
Messages
181
attachment.php

Requires PUI, Table, UnitProperties

Document
JASS:
Customizable Item System
    v1.02                                                           Made by wraithseeker
                                            
                                            
                                        Credits
                                        
                                Many thanks to Pyrogasm for helping me.
                                Thanks to Litany for UnitProperties.
                                Thanks to Vexorian for Table.
                                Thanks to Cohadar for PUI.
                                
                            Pros
                        - You do not have to do much OE editing as you only need to set stats in the Initialization function.
                        - Allows 12 inventory for units while carrying bonus in backpack
                        - MUI and leakless
                        - Allow custom item stats.
                        - Allows custom random item stats generating
                        - Configurable and useful
                        
                            Cons
                        - Requires some importing but not as much as InvX
                        - Might take some time to get used to the system
                                
                                
                                Changelog v1.00
                                
                            - Initial release
                            
                                Changelog v1.01
                                
                            - Fixed a stupid bug
                            
                                Changelog v1.02
                                
                            - Ported some stuff and added a new function
                            
                            
                            
                            
How to use it?

    integer ItemId      =   The ItemId of the item. Remember to use '____' for your rawcodes.
    integer Str         =   The amount of Strength.
    integer Agi         =   The amount of Agility.
    integer Int         =   The amount of Intelligence.
    real Damage         =   The amount of Damage.
    real Armor          =   The amount of Armour
    real MaxLife        =   The max life that you want to increase meaning that you increase the base of the unit health.
    real MaxMana        =   The max mana that you want to increase meaning that you increase the base of the unit mana.
    real LifeRegen      =   The amount of life regen you want to give to the unit.
    real ManaRegen      =   The amount of mana regen you want to give to the unit.
    real AttackSpeed    =   The amount of attack speed you want to give to the unit.
    real MoveSpeed      =   The amount of movement speed you want to give to the unit.
    
Abilities Section

    integer array Abilities[MAX_ABILITIES]
    integer array AbilityLevels[MAX_ABILITIES]

    
                        private constant integer MAX_ABILITIES = 6
                    
The max amount of abilities you want to give per unit through one item.

                      integer array AbilityLevels[MAX_ABILITIES]
                      
Whether you want the ability to have levels.

    
                    private constant integer BackpackId = 'BAG1'
    
This is the ID of the dummy spell that will be used to detect whether a unit whats to move it to the backpack.

    static method Item takes item whichItem returns bonus
    
You should only use this if you want to generate a fixed stat for this ITEM.

Example of Usage
    
        call bonus.Item(whichItem)
        set d.Int = 300
        
    It creates properties for the unit in the first line and the second line generates 300 Int whenever you pickup the item
        and removes bonus when you drop it.



System
JASS:
library CIS initializer Init requires PUI, 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
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]

       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
    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()
        if d != 0 then
            call UnitApplyItem(u,d)
        else
            set d = DataTable[GetItemTypeId(GetManipulatedItem())]
            if d > 0 then
                call UnitApplyItem(u,d)
            endif
        endif
        set u = null
    endfunction

    private function onDrop takes nothing returns nothing
        local bonus d = GetItemUserData(GetManipulatedItem())
        local unit u = GetTriggerUnit()
        if d != 0 then
            call UnitUnapplyItem(u, d)
        else
            set d = DataTable[GetItemTypeId(GetManipulatedItem())]
            if d > 0 then
               call UnitUnapplyItem(u, d)
            endif
        endif
        set u = null
    endfunction
    
public struct backpack
    //! runtextmacro PUI()
    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
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.release()
        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

CISInit (Use it if you do not know how to make a Init function of CIS)

JASS:
library CISInit initializer Init requires CIS

private function Init takes nothing returns nothing
    local bonus d
    set DataTable = Table.create()
    set d = bonus.create('I000')
    set d.Damage = 100
    set d.Agi = 200
    set d.Armor = 50
    set d = bonus.create('I001')
    set d.Str = 50
    set d.Abilities[0] = 'Apxf'
    set d.Abilities[1] = 'Ahea'
    set d.Abilities[2] = 'Aivs'
    set d = bonus.Item(gg_item_I000_0026 )
    set d.Int = 200
endfunction

endlibrary
 

Attachments

  • Customizable Item System v1.02.w3x
    98.6 KB · Views: 69
Last edited:
Top