Warcraft III resources & community, 2003–2006 · archived

ItemCombo System

4 posts
#1

ItemCombo System

Hi

i posted an itemcombo system a few days ago. Now i have enhanced it and came over some optimizing ideas.

here the link to the old system

Now i would like you, to go through the code and see what parts you think can be optimized (and how)

in adition to that, i need a (maybe rekrusive) function or part of a function that goes through my Items[1..6] and checks whether there is any combination of 2-6 of those items (none twice, starting with the combo that needs most items). It shall return the "result" item.

i stored the several combos in the gamecache using this format:


call StoreInteger(GetItemComboGameCache(), "ItemCombo", "Combo:" + I2S(the summ of all itemtypes of the combo), ResultItem)


this is seen in the last function.


Plase help me improving the code.

I left a blank space in the "HasItemCombo" function - in there i need either the function call, or better the code for checking whether a function exists.

I wrote this code for the check previously:


//--------------------------------------------------
// Check for combo
//--------------------------------------------------
  set a = 0
  loop
    set b = a +1

    loop  //b-loop
      set c = b +1

      set tempS = CheckItemCombo(Items[a],Items[b],0,0,0,0)
      set ComboItems[0] = Items[a]
      set ComboItems[1] = Items[b]
      set ComboItems[2] = 0
      set ComboItems[3] = 0
      set ComboItems[4] = 0
      set ComboItems[5] = 0
      
      exitwhen StringLength(tempS)>1

      if StringLength(tempS)<=1 then
        loop //c-loop
          set d = c +1

          set tempS = CheckItemCombo(Items[a],Items[b],Items[c],0,0,0)
          set ComboItems[0] = Items[a]
          set ComboItems[1] = Items[b]
          set ComboItems[2] = Items[c]
          set ComboItems[3] = 0
          set ComboItems[4] = 0
          set ComboItems[5] = 0

          exitwhen StringLength(tempS)>1

          if StringLength(tempS)<=1 then
            loop //d-loop
              set e = d +1
 
              set tempS = CheckItemCombo(Items[a],Items[b],Items[c],Items[d],0,0)
              set ComboItems[0] = Items[a]
              set ComboItems[1] = Items[b]
              set ComboItems[2] = Items[c]
              set ComboItems[3] = Items[d]
              set ComboItems[4] = 0
              set ComboItems[5] = 0

              exitwhen StringLength(tempS)>1

              if StringLength(tempS)<=1 then
                loop //e-loop
                  set f = e +1
      
                  set tempS = CheckItemCombo(Items[a],Items[b],Items[c],Items[d],Items[e],0)
                  set ComboItems[0] = Items[a]
                  set ComboItems[1] = Items[b]
                  set ComboItems[2] = Items[c]
                  set ComboItems[3] = Items[d]
                  set ComboItems[4] = Items[e]
                  set ComboItems[5] = 0

                  exitwhen StringLength(tempS)>1

                  if StringLength(tempS)<=1 then
                    loop //f-loop
                      set tempS = CheckItemCombo(Items[a],Items[b],Items[c],Items[d],Items[e],Items[f])
                      set ComboItems[0] = Items[a]
                      set ComboItems[1] = Items[b]
                      set ComboItems[2] = Items[c]
                      set ComboItems[3] = Items[d]
                      set ComboItems[4] = Items[e]
                      set ComboItems[5] = Items[f]

                      exitwhen (StringLength(tempS)>1) or (f > cnt)
                      set f = f + 1
                    endloop
                  endif
  
                  exitwhen (e > cnt) or (StringLength(tempS)>1)
                  set e = e + 1
                endloop
              endif
  
              exitwhen (d > cnt) or (StringLength(tempS)>1)
              set d = d + 1
            endloop
          endif

          exitwhen (c > cnt) or (StringLength(tempS)>1)
          set c = c + 1
        endloop
      endif

      exitwhen (b > cnt) or (StringLength(tempS)>1)
      set b = b + 1
    endloop

    set a = a + 1
    exitwhen (a > cnt) or (StringLength(tempS)>1)
  endloop


as you can see, it's quite inefficient. That time i had the itemcombos stored like this:

set ComboString = (the I2S() itemtypes of the combo items seperated by a "|" - no leading "|" and no ending)
call StoreInteger(udg_gc_Cache, "ItemCombo", "Combo:" + ComboString, ResultItem)


Now here is the code, as i changed it until now...


//globals
//  gamecache udg_gc_Cache
//  trigger udg_tg_Trigger
//endglobals

//=======================================================================================================
// Item-Combo-Script
//=======================================================================================================
// uses
//   gamecache udg_gc_Cache
//   trigger   udg_tg_Trigger
//--------------------------------------------------

//--------------------------------------------------
// Return-Bug functions
//--------------------------------------------------

function Unit2String takes unit u returns string
  return u
  return null
endfunction

function Item2String takes item i returns string
  return i
  return null
endfunction

function String2Unit takes string s returns unit
  return s
  return null
endfunction

function String2Item takes string s returns item
  return s
  return null
endfunction

//--------------------------------------------------
// system constants
//--------------------------------------------------

function GetItemComboEffect takes nothing returns string
  return "Abilities\\Spells\\Items\\Also\\SoulGem.wav"
endfunction

function GetItemComboEffectDuration takes nothing returns real
  return 1.00
endfunction

function GetItemComboXP takes nothing returns integer
  return 5
endfunction

function GetItemComboGameCache takes nothing returns gamecache
  return udg_gc_Cache
endfunction

function GetItemCombinationTrigger takes nothing returns trigger
  return udg_tg_Trigger
endfunction

//--------------------------------------------------
// functions for better compatibility with other scripts
//--------------------------------------------------

function GetLastCombinedItem takes nothing returns item
  return String2Item(GetStoredString(GetItemComboGameCache(), "ItemCombo", "LastCombinedItem"))
endfunction

function GetLastCombiningHero takes nothing returns unit
  return String2Unit(GetStoredString(GetItemComboGameCache(), "ItemCombo", "LastCombinatingHero"))
endfunction

function SetLastCombinedItem takes item i returns nothing
  call StoreString(GetItemComboGameCache(), "ItemCombo", "LastCombinedItem", Item2String(i))
endfunction

function SetLastCombiningHero takes unit u returns nothing
  call StoreString(GetItemComboGameCache(), "ItemCombo", "LastCombinatingHero", Unit2String(u))
endfunction

function RegisterItemCombinationEvent takes code f returns triggeraction
  return TriggerAddAction(GetItemCombinationTrigger(), f)
endfunction

function UnregisterItemCombinationEvent takes triggeraction t returns nothing
  call TriggerRemoveAction(GetItemCombinationTrigger(), t)
endfunction


//--------------------------------------------------
// this function adds some eye-candy and xp to the hero/unit that combines the item
//--------------------------------------------------
function ItemComboEffects takes unit u returns nothing
  local effect e
  call AddHeroXP(u, GetItemComboXP(), true)
  set e = AddSpecialEffectTargetUnitBJ("origin", u, GetItemComboEffect())
  call TriggerSleepAction(GetItemComboEffectDuration())
  call DestroyEffect(e)
  set e = null
endfunction

//--------------------------------------------------
//--------------------------------------------------
function HasItemCombo takes unit Hero returns boolean
  local integer array CarriedItems
  local integer Counter
  local integer Counter2
  local integer NumberOfItems
  local integer int_temp
  local string  ComboString
  
  //--------------------------------------------------
  //Get Items
  //--------------------------------------------------
  set Counter  = 0
  set Counter2 = 0
  loop
    set Counter  = Counter + 1
    set int_temp = GetItemTypeId(UnitItemInSlotBJ(Hero, Counter))

    if (int_temp > 0) then
      set Counter2 = Counter2 + 1
      set CarriedItems[Counter2] = int_temp
    endif
   
    exitwhen Counter >= 6
  endloop

  set NumberOfItems = Counter2

  //--------------------------------------------------
  // Check whether a combination exists
  //--------------------------------------------------
  

  
  //--------------------------------------------------
  // Add the combination and return whether one was found
  //--------------------------------------------------
  if ((int_temp > 0) and (int_temp != null)) then
    set int_temp = GetStoredInteger(GetItemComboGameCache(), "ItemCombo", "ComboResultItem")
    call UnitAddItemByIdSwapped(int_temp, Hero)
    
    call SetLastCombinedItem(GetLastCreatedItem())
    call SetLastCombiningHero(Hero)
    call ItemComboEffects(Hero)
    
    return true
  endif
  return false
endfunction

function AddItemCombo takes integer FirstItem, integer SecondItem, integer ThirdItem, integer FourthItem, integer FifthItem, integer SixthItem, integer ResultItem returns nothing
  local integer Combo

  set Combo = FirstItem + SecondItem + ThirdItem + FourthItem + FifthItem + SixthItem
  call StoreInteger(GetItemComboGameCache(), "ItemCombo", "Combo:" + Combo, ResultItem)
endfunction
#2
did some small tweaks and optimizations, and made the wait on the destroying of the effect 10, cuz no effects you would use for this would last more than that long, and this prevents calling another function.

(im assuming that you dont use that junk at the top anymore, so i left it out)

//globals
//    gamecache udg_gc_Cache
//    trigger udg_tg_Trigger
//endglobals
 
//=======================================================================================================
// Item-Combo-Script
//=======================================================================================================
// uses
//   gamecache udg_gc_Cache
//   trigger   udg_tg_Trigger
//--------------------------------------------------
 
//--------------------------------------------------
// Return-Bug functions
//--------------------------------------------------
 
function Unit2String takes unit u returns string
    return u
    return null
endfunction
 
function Item2String takes item i returns string
    return i
    return null
endfunction
 
function String2Unit takes string s returns unit
    return s
    return null
endfunction
 
function String2Item takes string s returns item
    return s
    return null
endfunction
 
//--------------------------------------------------
// system constants
//--------------------------------------------------
 
function GetItemComboEffect takes nothing returns string
    return "Abilities\\Spells\\Items\\Also\\SoulGem.wav"
endfunction
 
function GetItemComboXP takes nothing returns integer
    return 5
endfunction
 
function GetItemComboGameCache takes nothing returns gamecache
    return udg_gc_Cache
endfunction
 
function GetItemCombinationTrigger takes nothing returns trigger
    return udg_tg_Trigger
endfunction
 
//--------------------------------------------------
// functions for better compatibility with other scripts
//--------------------------------------------------
 
function GetLastCombinedItem takes nothing returns item
    return String2Item(GetStoredString(GetItemComboGameCache(), "ItemCombo", "LastCombinedItem"))
endfunction
 
function GetLastCombiningHero takes nothing returns unit
    return String2Unit(GetStoredString(GetItemComboGameCache(), "ItemCombo", "LastCombinatingHero"))
endfunction
 
function SetLastCombinedItem takes item i returns nothing
    call StoreString(GetItemComboGameCache(), "ItemCombo", "LastCombinedItem", Item2String(i))
endfunction
 
function SetLastCombiningHero takes unit u returns nothing
    call StoreString(GetItemComboGameCache(), "ItemCombo", "LastCombinatingHero", Unit2String(u))
endfunction
 
function RegisterItemCombinationEvent takes code f returns triggeraction
    return TriggerAddAction(GetItemCombinationTrigger(), f)
endfunction
 
function UnregisterItemCombinationEvent takes triggeraction t returns nothing
    call TriggerRemoveAction(GetItemCombinationTrigger(), t)
endfunction
 
 
//--------------------------------------------------
// this function adds some eye-candy and xp to the hero/unit that combines the item
//--------------------------------------------------
function ItemComboEffects takes unit u returns nothing
    local effect e
    call AddHeroXP(u, GetItemComboXP(), true)
    set e = AddSpecialEffectTarget(GetItemComboEffect(),u,"origin")
    call TriggerSleepAction(10)
    call DestroyEffect(e)
    set e = null
endfunction
 
//--------------------------------------------------
//--------------------------------------------------
function HasItemCombo takes unit Hero returns boolean
    local integer array CarriedItems
    local integer Counter = -1
    local integer int_temp
     
    //--------------------------------------------------
    //Get Items
    //--------------------------------------------------    
loop
        set Counter  = Counter + 1
        set int_temp = GetItemTypeId(UnitItemInSlot(Hero, Counter))
         
        if (int_temp > 0) then
            set CarriedItems[Counter] = int_temp
        endif
         
        exitwhen Counter == 5
    endloop

    //--------------------------------------------------
    // Add the combination and return whether one was found
    //--------------------------------------------------
    if int_temp > 0 then
        set int_temp = GetStoredInteger(GetItemComboGameCache(), "ItemCombo", "ComboResultItem") 
        call SetLastCombinedItem(UnitAddItemById(Hero,int_temp))
        call SetLastCombiningHero(Hero)
        call ItemComboEffects(Hero)
        return true
    endif
    return false
endfunction
 
function AddItemCombo takes integer FirstItem, integer SecondItem, integer ThirdItem, integer FourthItem, integer FifthItem, integer SixthItem, integer ResultItem returns nothing
call StoreInteger(GetItemComboGameCache(), "ItemCombo", "Combo:" + I2S(FirstItem + SecondItem + ThirdItem + FourthItem + FifthItem + SixthItem), ResultItem)
endfunction


EDIT: after criticizing others, i forgot the JASS tags lol...
#3
^thanks very much for ypour helpt - but you also kileld the part in which the function, that checks whether an combo exists should be placed in, as well...

besides that:
very much thx @you
and @all the rest: (maybe including you ^^)
please gimme any kind of help, considering the part of my function that checks whether a combination exists

- Raszul
#4
thats what i was asking if it went in or not :D

will check it out later.
The conversation continued. After wc3sear.ch closed, 2 further replies were posted to this thread on The Hive Workshop. Read them there →