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

[Trigger] How to Combine Charged Items?

Status
Not open for further replies.
Level 11
Joined
Nov 15, 2007
Messages
781
  • Item charges
    • Events
      • Unit - A unit Acquires an item
    • Conditions
      • (Item-class of (Item being manipulated)) Equal to Charged
    • Actions
      • Trigger - Turn off (This trigger)
      • Hero - Drop (Item being manipulated) from (Triggering unit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Triggering unit) has an item of type (Item-type of (Item being manipulated))) Equal to True
        • Then - Actions
          • Set charges = (Charges remaining in (Item being manipulated))
          • Item - Set charges remaining in (Item carried by (Triggering unit) of type (Item-type of (Item being manipulated))) to ((Charges remaining in (Item carried by (Triggering unit) of type (Item-type of (Item being manipulated)))) + charges)
          • Item - Remove (Item being manipulated)
        • Else - Actions
          • Hero - Give (Item being manipulated) to (Triggering unit)
      • Trigger - Turn on (This trigger)
edit: Disregard that last trigger, I derped hard. I need to think before posting just because something looks inherently simple. It's fixed now.
 
Level 11
Joined
Nov 15, 2007
Messages
781
That trigger completely doesnt work ;o.
Your turning the trigger off at the very start forcing the trigger to not finish the remaining actions, as in it wont turn on again.

Turning the trigger off just means it won't be executed again. It does not end any instances of the trigger that are already running. So, since it turns on again at the end of the trigger, and has no waits...

I tested the trigger myself, I think I'd have noticed if it didn't work.
 
Level 12
Joined
Aug 12, 2008
Messages
349
Perhaps it will help you. I found this in a DotA Template map last 2 years ago. lolx
JASS:
function Trig_Combine_Items_Conditions takes nothing returns boolean
    if ( not ( GetItemCharges(GetManipulatedItem()) > 0 ) ) then
        return false
    endif
    if ( not ( udg_Combine_Charged_Items == true ) ) then
        return false
    endif
    return true
endfunction

function Trig_Combine_Items_Actions takes nothing returns nothing
    local integer ITEMCOUNT
    local integer ITEMLOOP
    local integer CHARGES
    local integer MAXIMUM
    local item NEWITEM
    local unit OURUNIT

    set MAXIMUM = udg_Combine_Charges_Max
    set ITEMCOUNT = 0
    set ITEMLOOP = 0
    set CHARGES = 0
    set NEWITEM = GetManipulatedItem()
    set OURUNIT = GetManipulatingUnit()
    
    loop
        exitwhen ITEMLOOP > 6
        if ((GetItemTypeId(NEWITEM)) == (GetItemTypeId(UnitItemInSlotBJ(OURUNIT, ITEMLOOP)))) then
            if ((GetItemCharges(UnitItemInSlotBJ(OURUNIT, ITEMLOOP)) + GetItemCharges(NEWITEM)) <= MAXIMUM) then
                if not ( (UnitItemInSlotBJ(OURUNIT, ITEMLOOP)) == (NEWITEM)) then                
                    set CHARGES = (GetItemCharges(UnitItemInSlotBJ(OURUNIT, ITEMLOOP))) + GetItemCharges(NEWITEM)
                    call SetItemCharges( UnitItemInSlotBJ(OURUNIT, ITEMLOOP), CHARGES )
                    call RemoveItem( NEWITEM )
                    set ITEMLOOP=7
                endif
            endif
        endif
        if ( ITEMLOOP < 7 ) then
            set ITEMLOOP = ITEMLOOP + 1
        endif
    endloop
endfunction

//===========================================================================
function InitTrig_Combine_Charged_Items takes nothing returns nothing
    set gg_trg_Combine_Charged_Items = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Combine_Charged_Items, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerAddCondition( gg_trg_Combine_Charged_Items, Condition( function Trig_Combine_Items_Conditions ) )
    call TriggerAddAction( gg_trg_Combine_Charged_Items, function Trig_Combine_Items_Actions )
endfunction
 
What does that do?

Items leak when you remove them with less than 0.405 HP :p (You just gotta love Blizzard)
To stay on the safe side, we usually set their HP to 0.406 and remove them.

edit

This is the system I use:

JASS:
library Charges requires RegisterPlayerUnitEvent

    globals
        private constant integer MAX = 30
    endglobals
    
    private module Init
        private static method onInit takes nothing returns nothing
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_PICKUP_ITEM, function thistype.run)
        endmethod
    endmodule
    
    struct CombineCharges extends array
    
        static boolean enabled = true
        
        static method enable takes nothing returns nothing
            set thistype.enabled = true
        endmethod
        
        static method disable takes nothing returns nothing
            set thistype.enabled = false
        endmethod
        
        private static method run takes nothing returns nothing
            local integer il = 0
            local item in = GetManipulatedItem()
            local integer cnew = GetItemCharges(in)
            local item i
            local unit u
            local integer id
            local integer ic
            if enabled and cnew > 0 then
                set u = GetManipulatingUnit()
                set id = GetItemTypeId(in)
                loop
                    exitwhen il > 6
                    set i = UnitItemInSlot(u,il)
                    set ic = GetItemCharges(i)
                    if id == GetItemTypeId(i) and ic + cnew <= MAX and i != in then
                        call SetItemCharges(i,ic + cnew)
                        call SetWidgetLife(in, 0.406)
                        call RemoveItem(in)
                        exitwhen true
                    endif
                    set il = il + 1
                endloop
            endif
            set in=null
            set i=null
            set u=null
        endmethod
        
        implement Init
    endstruct
endlibrary

It requires NewGen though since it's in vJass.
It also requires RegisterPlayerUnitEvent: http://www.hiveworkshop.com/forums/submissions-414/commonevent-203338/
 
Status
Not open for further replies.
Top