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

Matrix Recipe System

Level 5
Joined
Jan 18, 2007
Messages
59
My first sytem I've created.
**************************************************************************************
*************************Simple Recipe System v1.01***********************************
**********************************************************************************
******************************By: Matrix****************************************
Pros:
1) Allow same item-types [like Stygian Desolator]
2) Consists of 74 lines
3) Can be used not only for Jassers/vJassers but also for GUI users
4) It is the simpliest system I've ever seen:)
Cons:
The only thing it's bad is if's. Due to them it's not very fast
*******************
**Changelog v1.02**
*******************
1) Added RecipeEx function
2) Now function returns a boolean depending on wether the recipe succeeded or not
3) More Cleaner&Leakless code
*******************
**How to implant:**
*******************
The system is very easy. The only things you'll have to do to implement the system is to
copy the custom script code
*******************
**How to use:******
*******************
To use this system u have to have a trigger with the event unit pick up item and in the actions
just write:
call Recipe(GetTriggerUnit(),'rde3','rde1','rde1',0,0,0,0)
Arguments:
GetTriggerUnit() - Unit that picked up item //I think it is clear
'rde3' - The New Item rawcode (the rawcode of the item that must be created)
'rde1' - The Rawcode of the first ingredient
'rde1' - The Rawcode of the second ingredient
0 - The Recipe hasn't more ingredients so the 3,4,5,6 ingredient are 0

************************
*****Trouble Shooter****
************************
Q: I have some troubles with this system. It doesn't work!!!
A: The reason maybe globals. If u have global integer i, i1,i2,i3,i4,i5,i6,count,j,ni... and all the arguments the system works with.
If u havent JassNewGenPack all must be Ok.

Q: Why did u do this shit?
A: I'm cr8ing an AoS map and I was searching for a simple recipe sytem but I didn't find=(
That's why I decided to cr8 my own =)


Hopefully you like the system!
~Matrix 3.08.08

P.S. I think I found my new home...
It's thehelper.net - really helper
Thx a lot to Earth-Fury for his suggestion
JASS:
function Recipe takes unit hero, integer ni, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6, string Effect, string where returns boolean
local integer id
local integer i = 0
local integer j = 1
local integer count = 0
local item array Items
local item it = null
if i1!=0 then
   set count = count+1
endif
if i2!=0 then
   set count = count+1
endif
if i3!=0 then
   set count = count+1
endif
if i4!=0 then
   set count = count+1
endif
if i5!=0 then
   set count = count+1
endif
if i6!=0 then
   set count = count+1
endif
loop
    exitwhen i>5
    set it = UnitItemInSlot(hero, i)
    set id = GetItemTypeId(it) 
    if id == i1 and i1!=0 then
           set Items[j] = it
           set j = j+1
           set i1=0
    elseif id == i2 and i2!=0 then
           set Items[j] = it
           set j = j+1
           set i2=0
    elseif id == i3 and i3!=0 then
           set Items[j] = it
           set j = j+1
           set i3=0
    elseif id == i4 and i4!=0 then
           set Items[j]= it
           set j=j+1
           set i4=0
    elseif id == i5 and i5!=0 then
           set Items[j] = UnitItemInSlot(hero, i)
           set j=j+1
           set i5=0
    elseif id == i6 and i6!=0 then
           set Items[j] = it
           set j=j+1
           set i6=0
    endif
    set i = i+1
endloop
set i = 1
if j-1!=count then
   return false
endif
loop
    exitwhen i>count
    call RemoveItem(Items[i])
    set Items[i]=null
    set i = i+1
endloop 
set it = null
set bj_lastCreatedItem = UnitAddItemById(hero, ni)
call DestroyEffect(AddSpecialEffectTarget(Effect,hero,where))
return true
endfunction
function RecipeEx takes unit hero, integer ni, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns boolean
    return Recipe(hero,ni,i1,i2,i3,i4,i5,i6,null,null)
endfunction
Download Matrix Recipe System v1.02
 
Last edited:
Level 11
Joined
Feb 18, 2004
Messages
394
  1. Functions are indented. Period.
    JASS:
    function Recipe takes unit hero, integer ni, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6, string Effect, string where returns nothing
        local integer id
        local integer i = 0
        local integer j = 1
        local integer count = 0
        local integer array ItemId
        local item array Item
    	
        if i1!=0 then
           set count = count+1
           set ItemId[count]=i1
        endif
        if i2!=0 then
           set count = count+1
           set ItemId[count]=i2
        endif
        if i3!=0 then
           set count = count+1
           set ItemId[count]=i3
        endif
        if i4!=0 then
           set count = count+1
           set ItemId[count]=i4
        endif
        if i5!=0 then
           set count = count+1
           set ItemId[count]=i5
        endif
        if i6!=0 then
           set count = count+1
           set ItemId[count]=i6
        endif
    	
        loop
            exitwhen i>5
            set id = GetItemTypeId(UnitItemInSlot(hero, i))
            if id == ItemId[1] and ItemId[1]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[1]=0
            elseif id == ItemId[2] and ItemId[2]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[2]=0
            elseif id == ItemId[3] and ItemId[3]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[3]=0
            elseif id == ItemId[4] and ItemId[4]!=0 then
                   set Item[j]= UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[4]=0
            elseif id == ItemId[5] and ItemId[5]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[5]=0
            elseif id == ItemId[6] and ItemId[6]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[6]=0
            endif
            set i = i+1
        endloop
    	
        set i = 1
        loop
            exitwhen i>count
            if Item[i]==null then
                return
            endif
            set i = i+1
        endloop
    	
        set i =1
        loop
            exitwhen i>count
            call RemoveItem(Item[i])
            set Item[i]=null
            set i = i+1
        endloop
    	
        call DestroyEffect(AddSpecialEffectTarget(Effect,hero,where))
    	
        call UnitAddItemById(hero, ni)
    endfunction
  2. The function's API is actually not half bad. A good enough example of procedural programming. However, the function should return a boolean indicating the recipes success or failure.

  3. 1-based arrays are failures:
    JASS:
            if i1!=0 then
           set count = count+1
           set ItemId[count]=i1
        endif

  4. Failing to use nested loops, while theoretically more efficient on an immeasurably small scale, is a failure:
    JASS:
            if id == ItemId[1] and ItemId[1]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[1]=0
            elseif id == ItemId[2] and ItemId[2]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[2]=0
            elseif id == ItemId[3] and ItemId[3]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j = j+1
                   set ItemId[3]=0
            elseif id == ItemId[4] and ItemId[4]!=0 then
                   set Item[j]= UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[4]=0
            elseif id == ItemId[5] and ItemId[5]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[5]=0
            elseif id == ItemId[6] and ItemId[6]!=0 then
                   set Item[j] = UnitItemInSlot(hero, i)
                   set j=j+1
                   set ItemId[6]=0
            endif

  5. Singular instead of plural names on array variables is a failure:
    JASS:
        local integer array ItemId
        local item array Item

  6. Using UnitInventorySize to reduce iteration count in the event of a unit with less than 6 inventory slots would be nice.

  7. JASS:
        set i = 1
        loop
            exitwhen i>count
            if Item[i]==null then
                return
            endif
            set i = i+1
        endloop
    .. Isn't this what j is for?
    JASS:
    if j != count then
        return
    endif

  8. The repeated calls to UnitItemInSlot(hero, i) should be optimized out with an item type local.

  9. As I have mentioned, this function should return a boolean depending on weather the recipe succeeded or not. That would allow removal of special effect code from the function, without removing the possibility of the functionality.
 
Top