From 07101b99fee40092c1738907d660185f06a32092 Mon Sep 17 00:00:00 2001 From: Vanessa Ezekowitz Date: Tue, 20 Jun 2017 05:02:53 -0400 Subject: [PATCH] add helper function for crafting param2-colorized items One call registers the full set of 32, 89, or 256 colors. Pass it a recipe of the following general form ```lua unifieddyes.register_color_craft({ output = "mymod:colorized_node", type = palette = , neutral_node = "some_mod:neutral_node", recipe = { } }) ``` palette may be either "wallmounted" (32 colors), false or omitted entirely (89 colors), or "extended" (256 colors). The recipe section is either a shapeless list, or a standard three-line shaped recipe, same as the regular register_craft() function. The key difference is two placeholder keys that are now supported: * if an item in the recipe is "MAIN_DYE", then Unified Dyes will, with each pass of its registration loop, substitute the actual "dye:foo" craft item in its place which corresponds with the current loop's color. * if an item in the recipe list is "NEUTRAL_NODE", then the value of the "neutral_node" field will be substituted in its place. The expectation here is that the modder probably has some base recipe in mind that, given no dyes, would result in a neutral, uncolored node. This call creates all the recipes that would be needed to craft colored versions of that neutral node either using that base recipe with dye added, or by crafting the neutral node with some dye after the fact. --- init.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index ff2fd41..5ae1b0c 100644 --- a/init.lua +++ b/init.lua @@ -30,8 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc., --===================================================================== unifieddyes = {} -unifieddyes.last_used_dye = {} -unifieddyes.last_dyed_node = {} local creative_mode = minetest.settings:get_bool("creative_mode") @@ -89,6 +87,17 @@ unifieddyes.HUES_EXTENDED = { { "crimson", 0xff, 0x00, 0x40 } } +unifieddyes.HUES_WALLMOUNTED = { + "red", + "orange", + "yellow", + "green", + "cyan", + "blue", + "violet", + "magenta" +} + unifieddyes.SATS = { "", "_s50" @@ -136,6 +145,65 @@ local default_dyes = { "yellow" } +-- this helper function registers all of the recipes needed to create colored +-- blocks with any of the dyes supported by that block's palette. + +function unifieddyes.register_color_craft(craft) + if not craft or not craft.recipe or not craft.output or not craft.neutral_node then return end + + local hues_table = unifieddyes.HUES_EXTENDED + local sats_table = unifieddyes.SATS + local vals_table = unifieddyes.VALS_EXTENDED + + if not craft.palette then + hues_table = unifieddyes.HUES + sats_table = unifieddyes.SATS + vals_table = unifieddyes.VALS + elseif craft.palette == "wallmounted" then + hues_table = unifieddyes.HUES_WALLMOUNTED + sats_table = {""} + vals_table = unifieddyes.VALS + end + + for _,hue in ipairs(hues_table) do + for _,sat in ipairs(sats_table) do + for _,val in ipairs(vals_table) do + + local color = "dye:"..val..hue[1]..sat + local paletteidx = unifieddyes.getpaletteidx(color, craft.palette) + local newrecipe = table.copy(craft.recipe) + + for k, item in ipairs(newrecipe) do + if item == "MAIN_DYE" then newrecipe[k] = color end + if item == "NEUTRAL_NODE" then newrecipe[k] = craft.neutral_node end + end + + local stack = ItemStack(craft.output) + stack:get_meta():set_int("palette_index", paletteidx) + stack:get_meta():set_string("dye", color) + local colorized_itemstack = stack:to_string() + + minetest.register_craft({ + output = colorized_itemstack, + type = craft.type, + recipe = newrecipe + }) + + if craft.neutral_node ~= string.split(craft.output, " ")[1] then + minetest.register_craft( { + output = colorized_itemstack, + type = "shapeless", + recipe = { + craft.neutral_node, + color + } + }) + end + end + end + end +end + -- automatically recolor a placed node to match the last-used dye -- should be called in the node's `after_place_node` callback.