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

Recipe System

Recipe System

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


System code:

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

Moderator

M

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 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
 
Level 1
Joined
May 6, 2007
Messages
4
What is map header (custom script section) im new and this will be very usefull in my map, plz help
 
Level 1
Joined
May 6, 2007
Messages
4
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
 

Tan

Tan

Level 3
Joined
Sep 25, 2007
Messages
34
Sorry I was sitting like 5 hours with your &quot;simple recipe&quot; system, coulden't get it to work! You need to explain better how to get it working...
I use this simple recipe system right now: http://www.hiveworkshop.com/forums/showthread.php?t=35788
The only thing that's bad with it that you can't do a recipe that needs two of the same things for. If you can explain how to get yours working I will use it Diablo-dk...
 

Tan

Tan

Level 3
Joined
Sep 25, 2007
Messages
34
I got it working now, and it's great.. :) You should put in where you can find the map header to..
 

CyberDuelX16

C

CyberDuelX16

Ive tried it , its ok but ive seen better. Keep up the good work!
 
Level 2
Joined
Oct 14, 2008
Messages
5
What... It was working fine before and I made like 3 recipes + tested. I just woke up the next morning and I couldn't save ~ it says the functions are undeclared although i copied everything. How to fix this anyone? XP
EDIT: Oh nvm! It's working again. Good work diablo-dk +rep
 
Last edited:
Level 1
Joined
Sep 12, 2008
Messages
5
How can i implement custom items ? and especially items with a colour code. because the item id says something like: l015:fmld (e.g.). but it doesn't work .

I solved it now, cause only the first 4 letters counts. so it's:

I015
 
Level 5
Joined
Dec 18, 2007
Messages
205
hey diablo-dk
i tried your system and it works very good except for the small lags appearing when first mixing an item, but i think that cant be fixed.
and i got one question:
the item disassemble works with an integer i that is the number of items needed for a recipe (correct?). so if i want to change the disassemble to work even when the unit has not enough slots, do i have to replace

JASS:
if i <= 6-UnitInventoryCount(u)+1 and i > 0 then
with
JASS:
if i > 0 then
?

greetings
 
Level 6
Joined
Feb 26, 2008
Messages
171
Doh! Doesn't work for me... Do I have to set RT to the recipe trigger, ic[0] to the number of items and RN to the number of recipes??

Here's what I've did: I've cop/pasted the Recipe Script in the Custom Script Code section, then I've copy/pasted the trigger with all variables (the variables where all created), then I've set the variable RC to true (in variable window) and then I've copy/pasted the ReadMe (just in case) and after the Demo trigger (which I've modified to put my recipes)...

Anything wrong in my list of things that I did?
+HELP ME PLSSS!

EDIT: It was my fault... The raw code was starting by I but in warcraft, the I ressemble like the l (minus-L). Sorry for this :D
 
Last edited:
Level 6
Joined
Feb 12, 2008
Messages
207
Ok, i'm using your UBER-Handy system since last year on my map (i dunno why my map is taking so long, i abandoned it several times) so the system works just fine (Thank you! You're on the credits!). I even customized the system to my needings and its just perfect now. The only problem i have is that sometimes there's an item i want to be disassembled, but the item was never "assembled" so the system doesn't recognizes it as an assembled item and never disassembles it.
I've been trying to check the code but i still lack of JASS knowledge and i couldn't find the way to set the "item user data" you use for disassembling so i could set the item i'm creating by trigger to be "disassemble-able"? (lol)

Just in case this info is needed:
This is the recipe:
  • -------- Thick Leather Tent --------
  • Custom script: call CreateRecipe3('I000','I000','I00V','I01F')
And this is the trigger that creates the craftable item:
  • PackTent
    • Events
      • Unit - A unit Finishes casting an ability
    • Conditions
      • (Ability being cast) Equal to Pack Tent
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Unit-type of (Casting unit)) Equal to Soft Leather Tent
        • Then - Actions
          • Set Temp_Point = (Position of (Casting unit))
          • For each (Integer A) from 1 to (Size of inventory for (Casting unit)), do (Actions)
            • Loop - Actions
              • Unit - Order (Casting unit) to drop (Item carried by (Casting unit) in slot (Integer A)) at Temp_Point
          • Unit - Remove (Casting unit) from the game
          • Item - Create Soft Leather Tent-Kit at Temp_Point
          • Custom script: call RemoveLocation (udg_Temp_Point)
        • Else - Actions
The item is "Soft Leather Tent-Kit" (I01F) just in case you didn't guessed it.
 
Level 5
Joined
Dec 18, 2007
Messages
205
the system cannot recognize not-forged items.
so why don't you make it a recipe with a dummy?;)
take a dummy with slots, add the items to him, boom recipe, then drop the item and give it to the unit that should get it.
not that hard i guess.
 
Level 6
Joined
Feb 12, 2008
Messages
207
nice idea you gave me, i will try but i've seen the system uses itemdata to recognize forged items. Thanks in advance i'll give it a try as soon as i can.
 
Level 5
Joined
Jun 4, 2010
Messages
41
How to get itemid working?

Claw of shit (combine1)
claw of fuck (combine2)
claw of ass (output)

cos1
cof2
coa3

it doesnt work this way? I checked if everything was correctly... but it simply doesn't work.

Please help =)
 
Top