/ bjvxlx_engineering / blueprint.lua
blueprint.lua
1 local mod = bjvxlx.engineering 2 local S = mod.translate 3 4 mod.registered_blueprints = {} 5 6 --[[ bjvxlx.engineering.register_blueprint(name, defn) 7 First, assigns "blueprint-themed" defaults to certain fields of `defn` 8 if they are missing. Namely: 9 10 field default value 11 ----- ------------- 12 `description` Makes it clear the item is a blueprint 13 and what it's a blueprint of. 14 `_doc_items_longdesc` Explains how to use blueprints 15 with the engineering table. 16 `inventory_image` Icon that looks like a blueprint. 17 `color` Blue tint, because it's a blueprint. 18 `stack_max` 1 19 `on_place`, `on_secondary_use` Function that displays the blueprint 20 in a formspec. 21 22 Then, registers craftitem `name` with definition `defn`, 23 and sets `name` to `defn` in `bjxvlx.engineering.registered_blueprints`. 24 25 Fields for `defn` which are specific to blueprints, 26 as opposed to other craftitems: 27 { 28 output = ..., -- Only item name, no qty allowed (always 1) 29 recipe = { -- Must have all 3 rows and columns; empty cells ('') ok 30 {..., ..., ...}, 31 {..., ..., ...}, 32 {..., ..., ...} 33 }, 34 tool = ... -- Optional name of tenth ingredient to require but not consume 35 } ]] 36 function mod.register_blueprint(name, defn) 37 if type(name) == 'table' and not defn then 38 defn = name 39 name = defn.output .. "_blueprint" 40 end 41 local output = minetest.registered_items[defn.output] 42 local itemdesc = output and output.description or S("Unknown") 43 defn.description = defn.description or 44 ("%s %s"):format(itemdesc, S("Blueprint")) 45 defn._doc_items_longdesc = defn._doc_items_longdesc or S(table.concat({ 46 "When used, allows you to view a crafting recipe", 47 "for use with the Engineering Table.", 48 "You will also need to put the blueprint into the table", 49 "to craft the recipe it displays." 50 }, ' ')) 51 defn.inventory_image = defn.inventory_image or 52 "bjvxlx_engineering_blueprint.png" 53 defn.color = defn.color or "#8080ffff" 54 defn.stack_max = defn.stack_max or 1 55 defn.on_place = defn.on_place or mod.show_blueprint 56 defn.on_secondary_use = defn.on_secondary_use or mod.show_blueprint 57 mod.registered_blueprints[name] = defn 58 minetest.register_craftitem(name, defn) 59 end 60 61 --[[ bjvxlx.engineering.show_blueprint(blueprint, player) 62 Shows `player` a formspec illustrating the recipe and output of `blueprint`. 63 `blueprint` may be an ItemStack or an item name string, 64 and `player` may be an ObjectRef or a player name string. ]] 65 function mod.show_blueprint(blueprint, player) 66 if type(blueprint) == 'userdata' then 67 blueprint = blueprint:get_name() 68 end 69 if type(player) == 'userdata' then 70 player = player:get_player_name() 71 end 72 blueprint = mod.registered_blueprints[blueprint] 73 local formspec_parts = { 74 "formspec_version[7]", 75 "size[9,5]", 76 "item_image[7,2;1,1;", 77 bjvxlx.util.resolve_alias(blueprint.output), 78 "]" 79 } 80 if blueprint.tool then 81 table.insert(formspec_parts, table.concat({ 82 "item_image[5,1.5;1,1;", 83 bjvxlx.util.resolve_alias(blueprint.tool), 84 "]", 85 "image[5,2.5;1,1;craftguide_arrow.png;]" 86 }, '')) 87 else 88 table.insert(formspec_parts 89 "image[5,2;1,1;craftguide_arrow.png;]") 90 end 91 for y, row in ipairs(blueprint.recipe) do 92 for x, item in ipairs(row) do 93 table.insert(formspec_parts, table.concat({ 94 "item_image[", 95 tostring(x), ",", tostring(y), 96 ";1,1;", bjvxlx.util.resolve_alias(item), "]" 97 }, '')) 98 end 99 end 100 minetest.show_formspec(player, 101 "bjvxlx_engineering:blueprint_ui", 102 table.concat(formspec_parts, "")) 103 end