library ItemRecipeSystem /* by deathismyfriend version 1.1.1.0
================================ Requirements =================================
Requires JNGP
*/uses Table, /* [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url] by bribe
* Alloc * [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-alternative-221493/[/url] by nestharus
*************************************************************************************
*
* Used to easily create item recipes.
*
************************************************************************************
*
* Installing
* STEP 1: Install JNGP first if you dont have JNGP.
* STEP 2: Install Table by bribe the link is above.
* STEP 3: Install Alloc by nestharus the link is above. You may be able to use some other alloc systems.
* STEP 4: Copy and paste this library to your map.
*
************************************************************************************
*
* Description
* -----------------------
* This is a very easy recipe system it only requires you to make recipes by using the create function.
* After that it checks for a recipe everytime an item is picked up or bought or when u enter a rect decided by the user.
* Allows you to set a rect if u want to use the enter rect / region event.
* Note: removes the items and adds the new item to the unit when the last item needed for the recipe is picked up by the unit.
*
* Capabilities
* -----------------------
*
* Allows the user to easily create an item recipe.
* - example as done in map - local RecipeList data = RecipeList.create( 'ratc', 'rat6', 'rat9', 0, 0, 0, 'I000', true)
* - this recipe only uses 3 items.
*
******************************************************************************************************************************************************************************************************
/ ******************************************************************************************************************************************************************************************************
/ *
/ * CREATORS / DESTRUCTORS
/ * -----------------------
/ *
/ ******************************************************************************************************************************************************************************************************
/ *
/ * struct Recipe extends array
/ *
/ * static method create takes integer i1, integer i2, integer i3, integer i4, integer i5, integer i6, integer itmToBeMade, boolean createOnPickup returns thistype
/ * - this is all you need to create an itemRecipe. If you do not want a 6 item recipe just put in a 0 for the integers.
/ * - If you want to make a recipe that you need to go in a region to make then set pickupRecipe to false.
/ *
/ * method destroy takes nothing returns nothing
/ * - to destroy an item recipe if wanted. just store the struct into an integer variable to destroy the item Recipe
/ *
/ * static method disAssemble takes item itm, unit u returns nothing
/ * - u can use this to disAssemble an item made through a recipe
/ *
/ *
/ * METHODS
/ * -------
/ *
/ * method RequireCharges takes integer itemId, integer charges returns nothing
/ * - This allows u to make item recipes require charges.
/ * - It takes the item id and the number of charges you want it to have.
/ *
/ * static method forceRecipe takes unit u returns nothing
/ * - allows u to force a recipe for when a unit enters region or some other event of ur choice
/ *
/ * static method forceRecipeByItem takes unit u, integer itemTypeId returns nothing
/ * - this allows for specific recipes to be made in specific rects.
/ * - note: use a special item for the itemTypeId
/ *
/ * static method disable takes nothing returns nothing
/ * - pauses the recipe system
/ *
/ * static method enable takes nothing returns nothing
/ * - resumes the recipe system
/ *
/ *
/ * private static method turnOffOtherSystems takes nothing returns nothing
/ * - add systems to turn off when the recipe system is running
/ *
/ * private static method turnOnOtherSystems takes nothing returns nothing
/ * - the systems u turned off in the above method turn them on here
/ *
/ ********************************************************************************************************************************/
globals
private Table disAssembleTable
private integer array structArray
private integer array regId
endglobals
struct Recipe extends array
implement Alloc
private static rect recipeRect
private static integer tempR
private static method turnOffOtherSystems takes nothing returns nothing
endmethod
private static method turnOnOtherSystems takes nothing returns nothing
endmethod
private static integer index = 0
private static boolean combineItems = true
private integer i1
private integer i2
private integer i3
private integer i4
private integer i5
private integer i6
private integer charges1
private integer charges2
private integer charges3
private integer charges4
private integer charges5
private integer charges6
private integer itmToBeMade
private integer ttlItemsForRecipe
private boolean pickup
private boolean charges
private static method checkingRecipes takes integer id, unit u, boolean pick returns nothing
local thistype this
local region reg
local integer array i
local integer array charges
local integer ttlItemsUnitHas = 0
local boolean b
local item array itm
local boolean array isItm
local integer L = 1
local integer L1 = 1 // used to check through the recipes tht go w that specific item
local integer L2 = 1
local integer r = 1 // used for removing items from the slots
if combineItems then
call thistype.turnOffOtherSystems()
loop
exitwhen L1 > index
set this = structArray[L1]
if this.pickup == pick then
set i[1] = this.i1
set i[2] = this.i2
set i[3] = this.i3
set i[4] = this.i4
set i[5] = this.i5
set i[6] = this.i6
set charges[1] = this.charges1
set charges[2] = this.charges2
set charges[3] = this.charges3
set charges[4] = this.charges4
set charges[5] = this.charges5
set charges[6] = this.charges6
set isItm[1] = false
set isItm[2] = false
set isItm[3] = false
set isItm[4] = false
set isItm[5] = false
set isItm[6] = false
set ttlItemsUnitHas = 0
loop
exitwhen L > 6
set itm[L] = UnitItemInSlot( u, (L - 1))
loop
exitwhen L2 > 6
if isItm[L2] == false then
if itm[L] != null and GetItemTypeId( itm[L]) == i[L2] then
if this.charges then
if GetItemCharges( itm[L]) >= charges[L] then
set isItm[L2] = true
set ttlItemsUnitHas = ttlItemsUnitHas + 1
endif
else
set isItm[L2] = true
set ttlItemsUnitHas = ttlItemsUnitHas + 1
endif
exitwhen isItm[L2]
endif
endif
set L2 = L2 + 1
endloop
set L2 = 1
set L = L + 1
endloop
if ttlItemsUnitHas == this.ttlItemsForRecipe then
loop
exitwhen r > 6
if isItm[r] then
call RemoveItem( itm[r])
endif
set itm[r] = null
set r = r + 1
endloop
call UnitAddItemById( u, this.itmToBeMade)
endif
endif
set L1 = L1 + 1
endloop
call thistype.turnOnOtherSystems()
endif
endmethod
static method forceRecipe takes unit u returns nothing
local integer L = 0
loop
exitwhen L > 5
call thistype.checkingRecipes( GetItemTypeId( UnitItemInSlot( u, L)), u, false)
set L = L + 1
endloop
set u = null
endmethod
static method forceRecipeByItem takes unit u, integer itemTypeId returns nothing
local integer L = 0
loop
exitwhen L > 5
if GetItemTypeId( UnitItemInSlot( u, L)) == itemTypeId then
call thistype.checkingRecipes( itemTypeId, u, false)
endif
set L = L + 1
endloop
set u = null
endmethod
method RequireCharges takes integer itemId, integer charges returns nothing
if this.i1 != 0 and this.i1 == itemId then
set this.charges1 = charges
elseif this.i2 != 0 and this.i2 == itemId then
set this.charges2 = charges
elseif this.i3 != 0 and this.i3 == itemId then
set this.charges3 = charges
elseif this.i4 != 0 and this.i4 == itemId then
set this.charges4 = charges
elseif this.i5 != 0 and this.i5 == itemId then
set this.charges5 = charges
elseif this.i6 != 0 and this.i6 == itemId then
set this.charges6 = charges
endif
set this.charges = true
endmethod
static method disAssemble takes item itm, unit u returns nothing
local thistype this = disAssembleTable[ GetItemTypeId( itm)]
set combineItems = false
call thistype.turnOffOtherSystems()
call RemoveItem( itm)
call UnitAddItemById( u, this.i1)
call UnitAddItemById( u, this.i2)
call UnitAddItemById( u, this.i3)
call UnitAddItemById( u, this.i4)
call UnitAddItemById( u, this.i5)
call UnitAddItemById( u, this.i6)
call thistype.turnOnOtherSystems()
set combineItems = true
endmethod
static method disable takes nothing returns nothing
set combineItems = false
endmethod
static method enable takes nothing returns nothing
set combineItems = true
endmethod
static method create takes integer id1, integer id2, integer id3, integer id4, integer id5, integer id6, integer itmToBeMade, boolean createOnPickup returns thistype
local thistype this = thistype.allocate()
local integer array i
local integer L = 0
set this.i1 = id1
set this.i2 = id2
set this.i3 = id3
set this.i4 = id4
set this.i5 = id5
set this.i6 = id6
set this.itmToBeMade = itmToBeMade
set this.pickup = createOnPickup
set this.charges = false
set i[0] = id1
set i[1] = id2
set i[2] = id3
set i[3] = id4
set i[4] = id5
set i[5] = id6
set this.ttlItemsForRecipe = 0
set disAssembleTable[itmToBeMade] = this
set index = index + 1
set structArray[index] = this
set this.ttlItemsForRecipe = 0
loop
exitwhen L > 5
if i[L] != 0 then
set this.ttlItemsForRecipe = this.ttlItemsForRecipe + 1
endif
set L = L + 1
endloop
return this
endmethod
method destroy takes nothing returns nothing
local integer L = 1
loop
exitwhen L > index
if structArray[ L] == this then
set structArray[ L] = structArray[index]
set index = index - 1
endif
set L = L + 1
endloop
call this.deallocate()
endmethod
private static method recipeCheck takes nothing returns boolean
call thistype.checkingRecipes(GetItemTypeId(GetManipulatedItem()), GetTriggerUnit(), true)
return false
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
set disAssembleTable = Table.create()
call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
call TriggerAddCondition( t, function thistype.recipeCheck)
set t = null
endmethod
endstruct
endlibrary