HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

How i can optimize this function??

08-24-2006, 01:14 PM#1
GALLED
Hello

That it:

Collapse JASS:
function combina_3items takes unit u, integer item1,integer item2, integer item3, integer result returns nothing
local integer array items
local integer index
local integer cont = 0
local integer i
local item indexItem
local item result1
local effect efecto
local real x = GetUnitX(u)
local real y = GetUnitY(u)

set items[0] = item1
set items[1] = item2
set items[2] = item3

set i = 0
loop
    exitwhen items[i]==null
    set index = 0
    loop
        set indexItem = UnitItemInSlot(u,index)
        if(indexItem!=null) and (GetItemTypeId(indexItem) == items[i]) then
        set cont=cont+1
        endif
        set index = index+1
        exitwhen index>=6
    endloop
    set i=i+1
endloop

if(cont==3)then
set i = 0
loop
    exitwhen items[i]==null
    set index = 0
    loop
        set indexItem = UnitItemInSlot(u,index)
        if(indexItem!=null) and (GetItemTypeId(indexItem) == items[i]) then
        call RemoveItem(indexItem)
        endif
        set index = index+1
        exitwhen index>=6
    endloop
    set i=i+1
endloop

    set result1 = CreateItem(result,x,y)
    set efecto = AddSpecialEffect("Abilities\\Spells\\Items\\AIem\\AIemTarget.mdl",x,y)
    call UnitAddItem(u,result1)

endif

    set result1 = null
    call DestroyEffect(efecto)
    set u = null
    set indexItem = null
endfunction

Is a function to mix items, this is for 3 items into one. And how i can make a similar function to mix N items into one???
08-24-2006, 01:55 PM#2
UnMi
Collapse JASS:
function combina_3items takes unit u, integer item1,integer item2, integer item3, integer result returns nothing
local integer array items
local integer index = 0
local integer cont = 0
local integer i = 0
local item array possession
local item indexItem
local real x = GetUnitX(u)
local real y = GetUnitY(u)

set items[0] = item1
set items[1] = item2
set items[2] = item3
loop
    exitwhen items[i]<=0
    loop
        set indexItem = UnitItemInSlot(u,index)
        if(indexItem!=null) and (GetItemTypeId(indexItem) == items[i]) then
            set possession[cont]=UnitItemInSlot(u,index)
            set cont = cont + 1
        endif
        set index = index+1
        exitwhen index==6
    endloop
    set i=i+1
endloop
if (cont==3) then
    loop
        set cont=cont-1
        exitwhen cont==-1
        call RemoveItem(possession[cont])    
        set posession[cont] = null        
    endloop
    call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Items\\AIem\\AIemTarget.mdl",x,y))// Not sure if this works, if not, use a variable
    call UnitAddItem(u,CreateItem(result,x,y))
endif
set indexItem = null
endfunction
Quote:
And how i can make a similar function to mix N items into one??
Same procedure.
08-24-2006, 08:05 PM#3
GALLED
Thanks, is one loop less.

So, i see the StoreInteger o something, how can be in this manner??
08-24-2006, 08:54 PM#4
UnMi
Oh...yes...gamecache...
It could be made that way, but well...it would look like this
Collapse JASS:
//Header
function game_cache takes nothing returns gamecache   
    if (udg_gamecache==null) then
        call FlushGameCache(InitGameCache("hax.w3v"))
        set udg_gamecache=InitGameCache("hax.w3v")
    endif
    return udg_gamecache
endfunction
//header

// MAP-INITIALIZATION
...
call StoreInteger(game_cache(),"Super Sword","Requirement1",'I000') // Sword Blade
call StoreInteger(game_cache(),"Super Sword","Requirement2",'I001') // Sword Sheath
call StoreInteger(game_cache(),"Super Sword","Requirement3",'I002') // Sword Hilt
call StoreString(game_cache(),"Sword Blade","Recipe1","Super Sword")
call StoreString(game_cache(),"Sword Blade","Recipe2","Ultra Sword")
...
// MAP-INITIALIZATION

function mix_items takes nothing returns nothing          
    local integer slot_index = 0
    local string part = GetItemName(GetManipulatedItem()) //Bought Item
    local unit mixer = GetTriggerUnit()
    local integer recipe_index = 1
    local integer requirement_index = 0
    local integer slot_index = 0
    local integer possession_index = 0
    local boolean mixed = false
    local boolean lacking_part = false
    local integer requirement
    local string recipe
    local item possessed
    local item array possession
    loop
        set recipe = GetStoredString(part,"Recipe"+I2S(recipe_index))
        exitwhen (recipe == null) or (mixed)
        loop
            set requirement = GetStoredInteger(game_cache,recipe,"Requirement"+I2S(requirement_index))
            exitwhen (requirement <= 0) or (no_have)
            loop
                exitwhen (slot_index==6)
                set possessed = UnitItemInSlot(mixer,slot_index)
                if(possessed!=null) and (GetItemTypeId(possessed) == requirement) then
                    set possession_index = possession_index + 1
                    set possession[possession_index]=UnitItemInSlot(mixer,slot_index)                   
                endif
                set slot_index = slot_index+1  
            endloop
            if (possession_index < recipe_index) then
                set lacking_part = true
            endif
            set requirement_index = requirement_index + 1
        endloop
        if (not lacking_part) then
            set mixed = true
        endif
        set recipe_index = recipe_index + 1
    endloop
    if (mixed) then 
        loop
            exitwhen (possession_index == 0)
            call RemoveItem(possession[possession_index])
            set possession_index = possession_index - 1
        endloop
        call UnitAddItemById(mixer,recipe)
    endif
    // Anti leak yourself