1. Are you planning to upload your awesome spell or system to Hive? Please review the rules here.
    Dismiss Notice
  2. The poll for our 11th Music Contest is up! Help us choose the most awesome cinematic tracks by casting a vote!
    Dismiss Notice
  3. Melee Mapping contest #3 - Poll is up! Vote for the best 4v4 melee maps!
    Dismiss Notice
  4. The 30th edition of the Modeling Contest is finally up! The Portable Buildings need your attention, so come along and have a blast!
    Dismiss Notice
  5. The Aftermath has been revealed for the 19th Terraining Contest! Be sure to check out the Results and see what came out of it.
    Dismiss Notice

Recipe System

Submitted by Diablo-dk
This bundle is marked as approved. It works and satisfies the submission rules.
Recipe System

Enables easy item recipe creation, including the option to dissamble recipe items again.


System code:

Code (vJASS):

//=======================================================================================================
// Recipe System
// By Fredbrik(Diablo-dk)
//
//=======================================================================================================
//globals
//integer               RN=0 // Recipe number.
//trigger               RT=CreateTrigger() // Main recipe trigger.
//boolean               RC=true
//integer array         itemid1
//integer array         itemid2
//integer array         itemid3
//integer array         itemid4
//integer array         itemid5
//integer array         itemid6
//integer array         ic //item count
//integer array         output
//endglobals
//=======================================================================================================
// User changeable constants
//=======================================================================================================
constant function Recipe_Effect takes nothing returns string
    return "Abilities\\Spells\\Items\\AIem\\AIemTarget.mdl" //An effect that comes whenever a recipe is made.
endfunction

constant function Recipe_AP takes nothing returns string
    return "origin" //Attachment point for Recipe_Effect()
endfunction
//=======================================================================================================
// This function will return the first item of entered Id in the unit's inventory.
function GetItem takes unit u, integer Id returns item
    local integer i=0
    local item it
    loop
        exitwhen i==6
        if GetItemTypeId(UnitItemInSlot(u,i)) == Id then
            return UnitItemInSlot(u,i)
        set i=5
        endif
        set i=i+1
    endloop
    return null
endfunction
//==============================
function HasItems takes unit u, integer i returns boolean
    local integer index=0
    local integer id=-1
    local integer b1=0
    local integer b2=0
    local integer b3=0
    local integer b4=0
    local integer b5=0
    local integer b6=0
    loop
        exitwhen index==6
        set id=GetItemTypeId(UnitItemInSlot(u,index))
        if id == 0 then
            set id=-1
        endif
        if id == udg_itemid1[i] and b1 == 0 then
            set b1=1
            set id=-1
        endif
        if id == udg_itemid2[i] and b2 == 0 then
            set b2=1
            set id=-1
        endif
        if id == udg_itemid3[i] and b3 == 0 then
            set b3=1
            set id=-1
        endif
        if id == udg_itemid4[i] and b4 == 0 then
            set b4=1
            set id=-1
        endif
        if id == udg_itemid5[i] and b5 == 0 then
            set b5=1
            set id=-1
        endif
        if id == udg_itemid6[i] and b6 == 0 then
            set b6=1
            set id=-1
        endif
        if b1+b2+b3+b4+b5+b6 == udg_ic[i] then
            return true
        endif
        set index=index+1
    endloop
    return false
endfunction
//=======================================================================================================
// User Functions:
// Recipe Creating:
//
//=======================================================================================================
function CreateRecipe takes integer i1,integer i2,integer i3,integer i4,integer i5,integer i6,integer output returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_ic[udg_RN]=2
    if i3 != 0 then
        set udg_itemid3[udg_RN]=i3
        set udg_ic[udg_RN]=udg_ic[udg_RN]+1
    endif
    if i4 != 0 then
        set udg_itemid4[udg_RN]=i4
        set udg_ic[udg_RN]=udg_ic[udg_RN]+1
    endif
    if i5 != 0 then
        set udg_itemid5[udg_RN]=i5
        set udg_ic[udg_RN]=udg_ic[udg_RN]+1
    endif
    if i6 != 0 then
        set udg_itemid6[udg_RN]=i6
        set udg_ic[udg_RN]=udg_ic[udg_RN]+1
    endif
    set udg_output[udg_RN]=output
endfunction
function CreateRecipe2 takes integer i1,integer i2,integer i3 returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_output[udg_RN]=i3
    set udg_ic[udg_RN]=2
endfunction

// Creates a recipe that requires 2 items to combine into a new item. i3 is the combined item.
// Example: call CreateRecipe2('I000','I001','I002')
function CreateRecipe3 takes integer i1,integer i2,integer i3,integer i4 returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_itemid3[udg_RN]=i3
    set udg_output[udg_RN]=i4
    set udg_ic[udg_RN]=3
endfunction

// The same as CreateRecipe2 except this requires 3 items to combine. i4 is the combined item.
function CreateRecipe4 takes integer i1,integer i2,integer i3,integer i4,integer i5 returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_itemid3[udg_RN]=i3
    set udg_itemid4[udg_RN]=i4
    set udg_output[udg_RN]=i5
    set udg_ic[udg_RN]=4
endfunction

// The same as CreateRecipe2 except this requires 4 items to combine. i5 is the combined item.
function CreateRecipe5 takes integer i1,integer i2,integer i3,integer i4,integer i5,integer i6 returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_itemid3[udg_RN]=i3
    set udg_itemid4[udg_RN]=i4
    set udg_itemid5[udg_RN]=i5
    set udg_output[udg_RN]=i6
    set udg_ic[udg_RN]=5
endfunction

// The same as CreateRecipe2 except this requires 5 items to combine. i6 is the combined item.
function CreateRecipe6 takes integer i1,integer i2,integer i3,integer i4,integer i5,integer i6,integer i7 returns nothing
    set udg_RN=udg_RN+1
    set udg_itemid1[udg_RN]=i1
    set udg_itemid2[udg_RN]=i2
    set udg_itemid3[udg_RN]=i3
    set udg_itemid4[udg_RN]=i4
    set udg_itemid5[udg_RN]=i5
    set udg_itemid6[udg_RN]=i6
    set udg_output[udg_RN]=i7
    set udg_ic[udg_RN]=6
endfunction

// The same as CreateRecipe2 except this requires 6 items to combine. i7 is the combined item.
//===================================================================================================
// This function disassembles a recipe item to its original components.

function DisItem takes unit u,item it returns boolean
    local integer c=GetItemUserData(it)
    local item array newitem
    local integer i=udg_ic[c]
    if it != null then
        if i <= 6-UnitInventoryCount(u)+1 and i > 0 then
            set udg_RC=false
            if udg_itemid2[c] != null then
                set newitem[1]=CreateItem(udg_itemid1[c],GetUnitX(u),GetUnitY(u))
                set newitem[2]=CreateItem(udg_itemid2[c],GetUnitX(u),GetUnitY(u))
                endif
            if udg_itemid3[c] != null then
                set newitem[3]=CreateItem(udg_itemid3[c],GetUnitX(u),GetUnitY(u))
                call UnitAddItem(u,newitem[3])
            endif
            if udg_itemid4[c] != null then
                set newitem[4]=CreateItem(udg_itemid4[c],GetUnitX(u),GetUnitY(u))
                call UnitAddItem(u,newitem[4])
            endif
            if udg_itemid5[c] != null then
                set newitem[5]=CreateItem(udg_itemid5[c],GetUnitX(u),GetUnitY(u))
                call UnitAddItem(u,newitem[5])
            endif
            if udg_itemid6[c] != null then
                set newitem[6]=CreateItem(udg_itemid6[c],GetUnitX(u),GetUnitY(u))
                call UnitAddItem(u,newitem[6])
            endif
            call RemoveItem(it)
            call UnitAddItem(u,newitem[1])
            call UnitAddItem(u,newitem[2])

        set newitem[1]=null
        set newitem[2]=null
        set newitem[3]=null
        set newitem[4]=null
        set newitem[5]=null
        set newitem[6]=null
        set udg_RC=true
        else
            return false
        endif
    endif
    return true
endfunction
//=======================================================================================================
//
//Main Recipe Function: Do not change unless you know what you are doing
//
//=======================================================================================================
function Recipe_Main takes nothing returns nothing
    local item it
    local integer i=0
    local unit u=GetManipulatingUnit()
    loop
        exitwhen i==udg_RN
        set i=i+1
        if udg_RC == true then
            if udg_ic[i] == 6 then
                if HasItems(u,i) then
                    call RemoveItem(GetItem(u,udg_itemid1[i]))
                    call RemoveItem(GetItem(u,udg_itemid2[i]))
                    call RemoveItem(GetItem(u,udg_itemid3[i]))
                    call RemoveItem(GetItem(u,udg_itemid4[i]))
                    call RemoveItem(GetItem(u,udg_itemid5[i]))
                    call RemoveItem(GetItem(u,udg_itemid6[i]))
                    call DestroyEffect(AddSpecialEffectTarget(Recipe_Effect(),u,Recipe_AP()))
                    set it=CreateItem(udg_output[i],GetUnitX(u),GetUnitY(u))
                    call SetItemUserData(it,i) //Used for disassembling.
                    call UnitAddItem(u,it)
                    set i=udg_RN
                endif
            elseif udg_ic[i] == 5 then
                if HasItems(u,i) then
                    call RemoveItem(GetItem(u,udg_itemid1[i]))
                    call RemoveItem(GetItem(u,udg_itemid2[i]))
                    call RemoveItem(GetItem(u,udg_itemid3[i]))
                    call RemoveItem(GetItem(u,udg_itemid4[i]))
                    call RemoveItem(GetItem(u,udg_itemid5[i]))
                    call DestroyEffect(AddSpecialEffectTarget(Recipe_Effect(),u,Recipe_AP()))
                    set it=CreateItem(udg_output[i],GetUnitX(u),GetUnitY(u))
                    call SetItemUserData(it,i) //Used for disassembling.
                    call UnitAddItem(u,it)
                    set i=udg_RN
                endif
            elseif udg_ic[i] == 4 then
                if HasItems(u,i) then
                    call RemoveItem(GetItem(u,udg_itemid1[i]))
                    call RemoveItem(GetItem(u,udg_itemid2[i]))
                    call RemoveItem(GetItem(u,udg_itemid3[i]))
                    call RemoveItem(GetItem(u,udg_itemid4[i]))
                    call DestroyEffect(AddSpecialEffectTarget(Recipe_Effect(),u,Recipe_AP()))
                    set it=CreateItem(udg_output[i],GetUnitX(u),GetUnitY(u))
                    call SetItemUserData(it,i) //Used for disassembling.
                    call UnitAddItem(u,it)
                    set i=udg_RN
                endif
            elseif udg_ic[i] == 3 then
                if HasItems(u,i) then
                    call RemoveItem(GetItem(u,udg_itemid1[i]))
                    call RemoveItem(GetItem(u,udg_itemid2[i]))
                    call RemoveItem(GetItem(u,udg_itemid3[i]))
                    call DestroyEffect(AddSpecialEffectTarget(Recipe_Effect(),u,Recipe_AP()))
                    set it=CreateItem(udg_output[i],GetUnitX(u),GetUnitY(u))
                    call SetItemUserData(it,i) //Used for disassembling.
                    call UnitAddItem(u,it)
                    set i=udg_RN
                endif
            elseif udg_ic[i] == 2 then
                if HasItems(u,i) then
                    call RemoveItem(GetItem(u,udg_itemid1[i]))
                    call RemoveItem(GetItem(u,udg_itemid2[i]))
                    call DestroyEffect(AddSpecialEffectTarget(Recipe_Effect(),u,Recipe_AP()))
                    set it=CreateItem(udg_output[i],GetUnitX(u),GetUnitY(u))
                    call SetItemUserData(it,i) //Used for disassembling.
                    call UnitAddItem(u,it)
                    set i=udg_RN
                endif
            endif
        endif
    endloop
    set it=null
    set u=null
endfunction
function InitRecipe takes nothing returns nothing
    set udg_RT=CreateTrigger()
    set udg_RC=true
    call TriggerRegisterAnyUnitEventBJ( udg_RT, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerAddAction(udg_RT,function Recipe_Main)
endfunction
//call this at map init.


//forced by WE
function InitTrig_Recipe takes nothing returns nothing
endfunction
// End Recipe

Requirements:

Recipe system has no specific requirements. Get quickly started by reading the manual included within the demo map.
Furthermore I made a small sample code to demonstrate how to use the system.

Author's note:

Please give proper credits, when using Recipe System in your map.

Changelog
Changelog:
version 1.22:
Fixed a bug in the implemention readme that would cause the system not to work correctly.

version 1.21:
Shortened a few functions.

version 1.2:
Fixed a bug that caused recipes with 2 or more of the same item to bug.

version 1.1:
Optimized code a bit.


Keywords:
recipe, itemrecipe
Contents

Recipe System (Map)

Reviews
Moderator
19:01, 2ndth Sep 2015 This resource has been re-approved by BPower. From moderator to user: Perfect if you need a simple item recipe system. It's very fail-safe and easy to setup. Keep in mind that the system manipulates ItemUserData. In case...
  1. 19:01, 2ndth Sep 2015

    This resource has been re-approved by BPower.

    From moderator to user:
    Perfect if you need a simple item recipe system. It's very fail-safe and easy to setup.
    Keep in mind that the system manipulates ItemUserData.
    In case you require more functionality than merging/disassembling items,
    than I would recommend to use a different item recipe system.

    For those who prefer vJass, there is a similar resource in our databank
     
  2. paskovich

    paskovich

    Joined:
    Jul 12, 2005
    Messages:
    794
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    I only looked at the script, but haven't tested yet. Does this work when you buy an item? I don't know if EVENT_PLAYER_UNIT_PICKUP_ITEM triggers when purchasing one...

    Anyway, if it works, i'll approve this. It is useful.
     
  3. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    yes, buying an item runs the Event too.
     
  4. paskovich

    paskovich

    Joined:
    Jul 12, 2005
    Messages:
    794
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    [APPROVED]
    Assuming that it works in all situations, this is a very useful system that can be easily implemented and used. Good job!
     
  5. sonkan1

    sonkan1

    Joined:
    Jan 18, 2007
    Messages:
    333
    Resources:
    0
    Resources:
    0
    Oh another nice system for making the games easier! Yaaay =) good
     
  6. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Update: optimized code a bit making it a little easier to read through and should improve performance.
     
  7. polo2005

    polo2005

    Joined:
    May 5, 2007
    Messages:
    9
    Resources:
    0
    Resources:
    0
    plz cant u make it in gull? dont understand how to change recripe items....
     
  8. polo2005

    polo2005

    Joined:
    May 5, 2007
    Messages:
    9
    Resources:
    0
    Resources:
    0
    plz cant u make it in gull? dont understand how to change recripe items....
     
  9. HasToMuchTime

    HasToMuchTime

    Joined:
    May 5, 2007
    Messages:
    7
    Resources:
    1
    Maps:
    1
    Resources:
    1
    What do i do when i disable then enable the trigger with the recipe of an item in it and it says it excepted a function name?
     
  10. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    polo, if you mean that it can be done in GUI, then no.
    I made it as easy as possible for creation of recipes you just need to enter the rawcodes.
     
  11. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    UPDATE:
    Fixed a bug that caused recipes with 2 or more of the same item to bug.
     
  12. arrieta

    arrieta

    Joined:
    May 6, 2007
    Messages:
    4
    Resources:
    0
    Resources:
    0
    What is map header (custom script section) im new and this will be very usefull in my map, plz help
     
  13. arrieta

    arrieta

    Joined:
    May 6, 2007
    Messages:
    4
    Resources:
    0
    Resources:
    0
    i dont understand how to implement the trigger, plz help
     
  14. NearbyHermit

    NearbyHermit

    Joined:
    May 20, 2007
    Messages:
    2,894
    Resources:
    0
    Resources:
    0
    Plz explain to ur good NextDoorHermit how to implement this spell????? Do no
    :I
     
  15. NearbyHermit

    NearbyHermit

    Joined:
    May 20, 2007
    Messages:
    2,894
    Resources:
    0
    Resources:
    0
    Plz explain to ur good NextDoorHermit how to implement this spell????? Do no
    :I
     
  16. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Ok, the map header is located at the top of the trigger editor(it is the one with the map name next to it and with an icon like a w3x map.
    Implemention instructions are in the readme.
     
  17. arrieta

    arrieta

    Joined:
    May 6, 2007
    Messages:
    4
    Resources:
    0
    Resources:
    0
    hey diablo-dk thnks for tellin me where the map header is. I copied the whole recipe trigger in the custom script, then i copied globalvars to my map and erased it. But when i am savin my map an error pops up. I have followed the instructions, i dont know what did i do wrong. what do you mean with unkown variables?? srry to bother u
     
  18. Diablo-dk

    Diablo-dk

    Joined:
    Nov 10, 2004
    Messages:
    369
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Open the world editor first of all, then click on &quot;File&quot;.
    Under File click on preferences.
    When you are under preferences, check the field called Automatically create unknown variables while pasting trigger data.
    when this is done, copy and delete the GlobalVars to your map.
     
  19. HasToMuchTime

    HasToMuchTime

    Joined:
    May 5, 2007
    Messages:
    7
    Resources:
    1
    Maps:
    1
    Resources:
    1
    Great spell you helped me a lot on it
    10/10 :D