package Hello
import LinkedListModule
import LinkedList
function unit.craft() returns LinkedList<int>
LinkedList<ItemRecipe> recipes = getRecipes()
LinkedList<int> craftables = new LinkedList<int>()
for ItemRecipe ir in recipes
if(this.canCraft(ir))
craftables.push(ir.craftable)
return craftables
function getRecipes() returns LinkedList<ItemRecipe>
return ItemRecipe.getRecipes()
function unit.canCraft(ItemRecipe recipe) returns boolean
return recipe.canCraft(this)
function unit.getInventory() returns LinkedList<item>
LinkedList<item> list = new LinkedList<item>()
for int i = 0 to 5
item tempItem = this.itemInSlot(i)
if(tempItem != null)
list.push(tempItem)
return list
function unit.hasItem(int id, int quantity) returns boolean
LinkedList<item> inventory = this.getInventory()
int count = 0
for item i in inventory
if(i.getTypeId() == id)
count ++
return count >= quantity
class ItemComponent
int component
int quantity
construct(int id, int count)
component = id
quantity = count
class ItemRecipe
use LinkedListModule
LinkedList<ItemComponent> components = new LinkedList<ItemComponent>()
int craftable
construct(int id)
craftable = id
function addComponent(int id, int quantity)
components.push(new ItemComponent(id, quantity))
static function getRecipes() returns LinkedList<thistype>
LinkedList<thistype> recipes = new LinkedList<thistype>()
let iterator = iterator()
for segment from iterator
recipes.push(segment)
return recipes
function canCraft(unit u) returns boolean
bool craftable = true
for ItemComponent ic in components
if(u.hasItem(ic.component, ic.quantity) == false)
craftable = false
break
return craftable
unit u
init
// Create recipe and add components
new ItemRecipe('shar')
..addComponent('whwd', 2)
..addComponent('pghe', 1)
..addComponent('phlt', 1)
// Crafting Demo
u = createUnit(players[0], 'Hpal', vec2(0, 0), angle(0))
// try to craft on esc press
CreateTrigger()
..registerPlayerEvent(players[0], EVENT_PLAYER_END_CINEMATIC)
..addAction() ->
print(u.getName() + " is crafting!")
let ids = u.craft()
if(ids.size() > 0)
print("The following items can be crafted: ")
for id in ids
let i = createItem(id, vec2(0, 0))
print(i.getName())
i.remove()
else
print("No items can be crafted")