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
Code
function Recipe takesunit hero, integer ni, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6, string Effect, string where returnsboolean localinteger id localinteger i = 0 localinteger j = 1 localinteger count = 0 localitemarray Items localitem 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 returnfalse 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)) returntrue endfunction function RecipeEx takesunit hero, integer ni, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returnsboolean return Recipe(hero,ni,i1,i2,i3,i4,i5,i6,null,null) endfunction
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]==nullthen 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
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.
1-based arrays are failures:
if i1!=0 then set count = count+1 set ItemId[count]=i1 endif
Failing to use nested loops, while theoretically more efficient on an immeasurably small scale, is a failure:
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
Singular instead of plural names on array variables is a failure:
localintegerarray ItemId localitemarray Item
Using UnitInventorySize to reduce iteration count in the event of a unit with less than 6 inventory slots would be nice.
set i = 1 loop exitwhen i>count if Item[i]==nullthen return endif set i = i+1 endloop
.. Isn't this what j is for?
if j != count then return endif
The repeated calls to UnitItemInSlot(hero, i) should be optimized out with an item type local.
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.
__________________
Giving reputation is a nice way to say thank you to someone that helps you!