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

This bundle is marked as useful / simple. Simplicity is bliss, low effort and/or may contain minor bugs.
A simple API for combining multiple items into one.

Check the demo code for simple API usage.

Wurst:
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")
Contents

Recipe System (Map)

Reviews
MyPad
Remarks: Due to another resource being recently approved, specifically ItemRecipe, ... this has been moved to Substandard. However, by being moved here, the moderator does not wish to insinuate that the resource is buggy, or done with minimal...
This is what I got from the preview triggers. Please note that this is subject to changes, and may soon be overridden.

NOTES:

  • Function unit.getInventroy() is a typo; but a trivial one. Suggest renaming it to getInventory?
  • In lambda expression found in addAction, () -> begin can be converted to ->.
  • The end keyword in the lambda expression can be omitted.
  • Documentation could be added.
From my understanding of the script, the system does not automatically combine items when picking them up as there is no function exposing these. A hotdoc of the class is probably in order here for clarity.

Overall, I think that this could be a good system for Wursters.
 

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,516
This looks pretty great to me - I like the simplicity.

For a demo I would like to see the API usage script separate from the implementation - not clear to me which parts are built-in and which parts are convenience functions.

Would also be good to see some documentation about the high-level API and some explanation about intended uses/limitations.

Finally, I guess applying generic constraints over recipes like unit type of crafter, time of day, etc could somehow be added to the API (using some kind of constraing function that accepts a closure) - what do you think?

4 stars from me
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
There are plenty of things which I could have done, and possibly would have done.
But another system was made, and while I possibly could compete with it.. I do not really see the need for doing so right now.

If I am a bit naive, I might hope to create something even better but I do not think it is worth the time investment for a very minor improvement.
 
Top