Crafting Demo Twee Code
I'd provide the raw file but free accounts can't upload arbitrary file types :(
:: StoryTitle Crafting Demo :: StoryData { "ifid": "BAC55E3F-FC56-4187-9F6E-3A00E1ED4B1F", "zoom": 1 } :: StoryCaption Functions to exchange two sets of arbitrary items. :: Inventory Scripts [script] setup.hasEnough = function(recipe, pantry) { return Object.keys(recipe).every(x => recipe[x] <= pantry[x]); }; setup.consume = function (recipe, pantry) { Object.keys(recipe).forEach(x => pantry[x]-= recipe[x]); }; setup.produce = function (result, pantry) { Object.keys(result).forEach(x => pantry[x]+= result[x]); }; setup.craft = function (recipe, result, pantry) { if(setup.hasEnough(recipe, pantry)) { setup.consume(recipe, pantry); setup.produce(result, pantry); } }; :: StoryInit <<set $inventory = { "gold": 0, "muffin vouchers": 3, "muffins": 0 }; $standard = {"muffin vouchers": 1}; $greedy = {"muffin vouchers": 2}; >> :: StoryMenu [[Notes]] :: Notes !! Notes This began as a demonstration of a player inventory that could hold multiple items of the same type. It gradually expanded to include some functions for exchanging arbitrary lists of items. The original idea was to make a crafting system, but you could build a conventional shop by limiting your requirements to gold or some other currency. The requirements and rewards are just objects with strings as keys and integers as values: {{{ $inventory = {"muffin vouchers":3}; $muffin_requirement = {"muffin vouchers" : 1}; $muffin_reward = {"muffins": 1}; $setup.craft($muffin_requirement, $muffin_reward, $inventory); }}} <<return>> :: Start <<include Inventory>> <<include "Coupon Exchange">> <<include "Greedy Coupon Exchange">> <<include "Generous Coupon Exchange">> :: Inventory !! Inventory <nobr><table> <<for _item, _qty range $inventory>> <<if _item == "gold" or _qty > 0>><tr><td>_qty</td><td>_item</td></tr><</if>> <</for>></table></nobr> :: Coupon Exchange !! Coupon Exchange Exchange vouchers for muffins here. <<if setup.hasEnough($standard, $inventory);>>\ <<button "Trade 1 to 1">>\ <<run setup.craft({"muffin vouchers": 1}, {"muffins":1}, $inventory); Engine.play(passage());>> <</button>><<else>><button class="macro-button" title="You need a voucher to get a muffin." disabled>Trade</button><</if>> :: Greedy Coupon Exchange !! Greedy Coupon Exchange Exchange vouchers for muffins here. <<if setup.hasEnough($greedy, $inventory);>>\ <<button "Trade 2 to 1">>\ <<run setup.craft({"muffin vouchers": 2}, {"muffins":1}, $inventory); Engine.play(passage());>> <</button>><<else>><button class="macro-button" title="You need TWO vouchers to get a muffin." disabled>Trade</button><</if>> :: Generous Coupon Exchange !! Generous Coupon Exchange Exchange vouchers for muffins here. <<if setup.hasEnough($standard, $inventory);>>\ <<button "Trade 1 to 2">>\ <<run setup.craft({"muffin vouchers": 1}, {"muffins":2}, $inventory); Engine.play(passage());>> <</button>><<else>><button class="macro-button" title="You need a voucher to get two muffins." disabled>Trade</button><</if>>