• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] ItemCombo System

Status
Not open for further replies.
Level 6
Joined
Feb 18, 2005
Messages
263
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:

JASS:
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:

JASS:
//--------------------------------------------------
// 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:
JASS:
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...

JASS:
//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
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
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)

JASS:
//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...
 
Level 6
Joined
Feb 18, 2005
Messages
263
^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
 
Level 2
Joined
Sep 10, 2005
Messages
20
Im having trouble with these code...
Im newby to the JASS codes , basically to all the codes >.<
Well, the problem is that i dont know how to implement these code into my map.. i readed the instructions of the old system but i still cannot do it :(
If anyone can help my please post a reply with step by stem instructions (please) or a premade map with the code inside, so i can lear about it too, for see how it is done...

THX a lot for everone who replyes...

See ya

PD: if the code works, and i think it does, Great Job :D
 
Status
Not open for further replies.
Top