From eb435e758a352df441a05b3f0864ffd0216896cb Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Sun, 30 Oct 2016 17:05:20 +0100 Subject: [PATCH] Fix Problems found by @Wuzzy2 For details see #2 --- LICENSE | 33 ++++ README.md | 5 +- init.lua | 552 +++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 496 insertions(+), 94 deletions(-) diff --git a/LICENSE b/LICENSE index 7585f72..cd17082 100755 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,36 @@ +License for Textures +-------------------- + CC0 Public Domain See https://creativecommons.org/publicdomain/zero/1.0/ for details. + + + + +License for Code +---------------- + +MIT License + +Copyright (c) 2016 Thomas S. +Copyright (C) 2016 webdesigner97 (for workshop code) +Copyright (C) 2016 cheapie (for workshop code) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 53ce2ba..c7cf198 100755 --- a/README.md +++ b/README.md @@ -3,4 +3,7 @@ ts_doors This mod adds more doors. It was made by Thomas-S. -It is published under the CC0 license. +For license see LICENSE file. + +Special thanks to Wuzzy2, who helped to improve this mod by suggesting many good ideas. +Special thanks to webdesigner97 and cheapie, who contributed some code for the door workshop. \ No newline at end of file diff --git a/init.lua b/init.lua index 9730628..6459aad 100755 --- a/init.lua +++ b/init.lua @@ -1,5 +1,7 @@ ts_doors = {} +ts_doors.registered_doors = {} + local function copytable(orig) local orig_type = type(orig) local copy @@ -16,13 +18,14 @@ local function copytable(orig) end local function register_alias(name, convert_to) - minetest.register_alias(name , convert_to ) + minetest.register_alias(name, convert_to) minetest.register_alias(name .. "_a", convert_to .. "_a") minetest.register_alias(name .. "_b", convert_to .. "_b") end function ts_doors.register_door(item, description, texture, recipe) recipe = recipe or item + ts_doors.registered_doors[item:gsub(":", "_")] = recipe register_alias("doors:ts_door_" .. item:gsub(":", "_"), "ts_doors:door_" .. item:gsub(":", "_")) register_alias("doors:ts_door_full_" .. item:gsub(":", "_"), "ts_doors:door_full_" .. item:gsub(":", "_")) register_alias("doors:ts_door_locked_" .. item:gsub(":", "_"), "ts_doors:door_locked_" .. item:gsub(":", "_")) @@ -30,39 +33,38 @@ function ts_doors.register_door(item, description, texture, recipe) local groups = minetest.registered_nodes[item].groups local door_groups = {} - for k,v in pairs(groups) do + for k, v in pairs(groups) do if k ~= "wood" then door_groups[k] = v end end - trapdoor_groups = copytable(door_groups) + door_groups.not_in_creative_inventory = 1 + + local trapdoor_groups = copytable(door_groups) doors.register("ts_doors:door_" .. item:gsub(":", "_"), { - tiles = {{ name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base.png^[noalpha^[makealpha:0,255,0", backface_culling = true }}, - description = description .. " Door", + tiles = { { name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base.png^[noalpha^[makealpha:0,255,0", backface_culling = true } }, + description = description .. " Windowed Door", inventory_image = "[combine:32x32:0,8=" .. texture .. ":16,8=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_inv.png^[noalpha^[makealpha:0,255,0", groups = door_groups, - recipe = { - {recipe, recipe}, - {recipe, recipe}, - {recipe, recipe}, - } + }) + minetest.override_item("ts_doors:door_" .. item:gsub(":", "_"), { + groups = { not_in_creative_inventory = 1 } }) doors.register("ts_doors:door_full_" .. item:gsub(":", "_"), { - tiles = {{ name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full.png^[noalpha", backface_culling = true }}, - description = description .. " Door", + tiles = { { name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full.png^[noalpha", backface_culling = true } }, + description = "Solid " .. description .. " Door", inventory_image = "[combine:32x32:0,8=" .. texture .. ":16,8=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full_inv.png^[noalpha^[makealpha:0,255,0", groups = door_groups, - recipe = { - {recipe}, - {"ts_doors:door_" .. item:gsub(":", "_")}, - } + }) + minetest.override_item("ts_doors:door_full_" .. item:gsub(":", "_"), { + groups = { not_in_creative_inventory = 1 } }) doors.register_trapdoor("ts_doors:trapdoor_" .. item:gsub(":", "_"), { - description = description .. " Trapdoor", + description = "Windowed " .. description .. " Trapdoor", inventory_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor.png^[noalpha^[makealpha:0,255,0", wield_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor.png^[noalpha^[makealpha:0,255,0", tile_front = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor.png^[noalpha^[makealpha:0,255,0", @@ -71,7 +73,7 @@ function ts_doors.register_door(item, description, texture, recipe) }) doors.register_trapdoor("ts_doors:trapdoor_full_" .. item:gsub(":", "_"), { - description = description .. " Trapdoor", + description = "Solid " .. description .. " Trapdoor", inventory_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full.png^[noalpha", wield_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full.png^[noalpha", tile_front = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full.png^[noalpha", @@ -79,56 +81,37 @@ function ts_doors.register_door(item, description, texture, recipe) groups = trapdoor_groups, }) - minetest.register_craft({ - output = "ts_doors:trapdoor_" .. item:gsub(":", "_"), - recipe = { - {recipe, recipe}, - {recipe, recipe}, - } - }) - - minetest.register_craft({ - output = "ts_doors:trapdoor_full_" .. item:gsub(":", "_"), - recipe = { - {recipe}, - {"ts_doors:trapdoor_" .. item:gsub(":", "_")}, - } - }) - door_groups.level = 2 trapdoor_groups.level = 2 doors.register("ts_doors:door_locked_" .. item:gsub(":", "_"), { - tiles = {{ name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_locked.png^[noalpha^[makealpha:0,255,0", backface_culling = true }}, - description = description .. " Locked Door", + tiles = { { name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_locked.png^[noalpha^[makealpha:0,255,0", backface_culling = true } }, + description = "Windowed Locked " .. description .. " Door", inventory_image = "[combine:32x32:0,8=" .. texture .. ":16,8=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_locked_inv.png^[noalpha^[makealpha:0,255,0", protected = true, groups = door_groups, sound_open = "doors_steel_door_open", sound_close = "doors_steel_door_close", - recipe = { - {recipe, recipe, ""}, - {recipe, recipe, "default:steel_ingot"}, - {recipe, recipe, ""}, - } + }) + minetest.override_item("ts_doors:door_locked_" .. item:gsub(":", "_"), { + groups = { not_in_creative_inventory = 1 } }) doors.register("ts_doors:door_full_locked_" .. item:gsub(":", "_"), { - tiles = {{ name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full_locked.png^[noalpha", backface_culling = true }}, - description = description .. " Locked Door", + tiles = { { name = "[combine:32x38:0,0=" .. texture .. ":0,16=" .. texture .. ":0,32=" .. texture .. ":16,0=" .. texture .. ":16,16=" .. texture .. ":16,32=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full_locked.png^[noalpha", backface_culling = true } }, + description = "Solid Locked " .. description .. " Door", inventory_image = "[combine:32x32:0,8=" .. texture .. ":16,8=" .. texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_full_locked_inv.png^[noalpha^[makealpha:0,255,0", protected = true, groups = door_groups, sound_open = "doors_steel_door_open", sound_close = "doors_steel_door_close", - recipe = { - {recipe}, - {"ts_doors:door_locked_" .. item:gsub(":", "_")}, - } + }) + minetest.override_item("ts_doors:door_full_locked_" .. item:gsub(":", "_"), { + groups = { not_in_creative_inventory = 1 } }) doors.register_trapdoor("ts_doors:trapdoor_locked_" .. item:gsub(":", "_"), { - description = description .. " Locked Trapdoor", + description = "Windowed Locked " .. description .. " Trapdoor", inventory_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_locked.png^[noalpha^[makealpha:0,255,0", wield_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_locked.png^[noalpha^[makealpha:0,255,0", tile_front = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_locked.png^[noalpha^[makealpha:0,255,0", @@ -141,7 +124,7 @@ function ts_doors.register_door(item, description, texture, recipe) }) doors.register_trapdoor("ts_doors:trapdoor_full_locked_" .. item:gsub(":", "_"), { - description = description .. " Locked Trapdoor", + description = "Solid Locked " .. description .. " Trapdoor", inventory_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full_locked.png^[noalpha", wield_image = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full_locked.png^[noalpha", tile_front = texture .. "^[transformR90^[colorize:#fff:30^ts_doors_base_trapdoor_full_locked.png^[noalpha", @@ -152,65 +135,448 @@ function ts_doors.register_door(item, description, texture, recipe) sound_close = "doors_steel_door_close", groups = trapdoor_groups }) - - minetest.register_craft({ - output = "ts_doors:trapdoor_locked_" .. item:gsub(":", "_"), - recipe = { - {"default:steel_ingot"}, - {"ts_doors:trapdoor_" .. item:gsub(":", "_")}, - } - }) - - minetest.register_craft({ - output = "ts_doors:trapdoor_full_locked_" .. item:gsub(":", "_"), - recipe = { - {recipe}, - {"ts_doors:trapdoor_locked_" .. item:gsub(":", "_")}, - } - }) end -ts_doors.register_door("default:aspen_wood" , "Aspen" , "default_aspen_wood.png" ) -ts_doors.register_door("default:pine_wood" , "Pine" , "default_pine_wood.png" ) -ts_doors.register_door("default:acacia_wood", "Acacia" , "default_acacia_wood.png") -ts_doors.register_door("default:wood" , "Wooden" , "default_wood.png" ) -ts_doors.register_door("default:junglewood" , "Jungle Wood", "default_junglewood.png" ) +ts_doors.register_door("default:aspen_wood", "Aspen", "default_aspen_wood.png") +ts_doors.register_door("default:pine_wood", "Pine", "default_pine_wood.png") +ts_doors.register_door("default:acacia_wood", "Acacia", "default_acacia_wood.png") +ts_doors.register_door("default:wood", "Wooden", "default_wood.png") +ts_doors.register_door("default:junglewood", "Jungle Wood", "default_junglewood.png") if minetest.get_modpath("moretrees") then - ts_doors.register_door("moretrees:apple_tree_planks" , "Apple Tree" , "moretrees_apple_tree_wood.png" ) - ts_doors.register_door("moretrees:beech_planks" , "Beech" , "moretrees_beech_wood.png" ) - ts_doors.register_door("moretrees:birch_planks" , "Birch" , "moretrees_birch_wood.png" ) - ts_doors.register_door("moretrees:fir_planks" , "Fir" , "moretrees_fir_wood.png" ) - ts_doors.register_door("moretrees:oak_planks" , "Oak" , "moretrees_oak_wood.png" ) - ts_doors.register_door("moretrees:palm_planks" , "Palm" , "moretrees_palm_wood.png" ) + ts_doors.register_door("moretrees:apple_tree_planks", "Apple Tree", "moretrees_apple_tree_wood.png") + ts_doors.register_door("moretrees:beech_planks", "Beech", "moretrees_beech_wood.png") + ts_doors.register_door("moretrees:birch_planks", "Birch", "moretrees_birch_wood.png") + ts_doors.register_door("moretrees:fir_planks", "Fir", "moretrees_fir_wood.png") + ts_doors.register_door("moretrees:oak_planks", "Oak", "moretrees_oak_wood.png") + ts_doors.register_door("moretrees:palm_planks", "Palm", "moretrees_palm_wood.png") ts_doors.register_door("moretrees:rubber_tree_planks", "Rubber Tree", "moretrees_rubber_tree_wood.png") - ts_doors.register_door("moretrees:sequoia_planks" , "Sequoia" , "moretrees_sequoia_wood.png" ) - ts_doors.register_door("moretrees:spruce_planks" , "Spruce" , "moretrees_spruce_wood.png" ) - ts_doors.register_door("moretrees:willow_planks" , "Willow" , "moretrees_willow_wood.png" ) + ts_doors.register_door("moretrees:sequoia_planks", "Sequoia", "moretrees_sequoia_wood.png") + ts_doors.register_door("moretrees:spruce_planks", "Spruce", "moretrees_spruce_wood.png") + ts_doors.register_door("moretrees:willow_planks", "Willow", "moretrees_willow_wood.png") end -ts_doors.register_door("default:bronzeblock" , "Bronze" , "default_bronze_block.png" , "default:bronze_ingot" ) -ts_doors.register_door("default:copperblock" , "Copper" , "default_copper_block.png" , "default:copper_ingot" ) -ts_doors.register_door("default:diamondblock", "Diamond", "default_diamond_block.png", "default:diamond" ) -ts_doors.register_door("default:goldblock" , "Gold" , "default_gold_block.png" , "default:gold_ingot" ) -ts_doors.register_door("default:steelblock" , "Steel" , minetest.registered_nodes["default:steelblock"].tiles[1], "default:steel_ingot") +ts_doors.register_door("default:bronzeblock", "Bronze", "default_bronze_block.png", "default:bronze_ingot") +ts_doors.register_door("default:copperblock", "Copper", "default_copper_block.png", "default:copper_ingot") +ts_doors.register_door("default:diamondblock", "Diamond", "default_diamond_block.png", "default:diamond") +ts_doors.register_door("default:goldblock", "Gold", "default_gold_block.png", "default:gold_ingot") +ts_doors.register_door("default:steelblock", "Steel", minetest.registered_nodes["default:steelblock"].tiles[1], "default:steel_ingot") if minetest.get_modpath("moreores") then ts_doors.register_door("moreores:mithril_block", "Mithril", "moreores_mithril_block.png", "moreores:mithril_ingot") - ts_doors.register_door("moreores:silver_block" , "Silver" , "moreores_silver_block.png" , "moreores:silver_ingot" ) - ts_doors.register_door("moreores:tin_block" , "Tin" , "moreores_tin_block.png" , "moreores:tin_ingot" ) + ts_doors.register_door("moreores:silver_block", "Silver", "moreores_silver_block.png", "moreores:silver_ingot") + ts_doors.register_door("moreores:tin_block", "Tin", "moreores_tin_block.png", "moreores:tin_ingot") end if minetest.get_modpath("technic") then - ts_doors.register_door("technic:brass_block" , "Brass" , "technic_brass_block.png" , "technic:brass_ingot" ) - ts_doors.register_door("technic:carbon_steel_block" , "Carbon Steel" , "technic_carbon_steel_block.png" , "technic:carbon_steel_ingot" ) - ts_doors.register_door("technic:cast_iron_block" , "Cast Iron" , "technic_cast_iron_block.png" , "technic:cast_iron_ingot" ) - ts_doors.register_door("technic:chromium_block" , "Chromium" , "technic_chromium_block.png" , "technic:chromium_ingot" ) - ts_doors.register_door("technic:lead_block" , "Lead" , "technic_lead_block.png" , "technic:lead_ingot" ) + ts_doors.register_door("technic:brass_block", "Brass", "technic_brass_block.png", "technic:brass_ingot") + ts_doors.register_door("technic:carbon_steel_block", "Carbon Steel", "technic_carbon_steel_block.png", "technic:carbon_steel_ingot") + ts_doors.register_door("technic:cast_iron_block", "Cast Iron", "technic_cast_iron_block.png", "technic:cast_iron_ingot") + ts_doors.register_door("technic:chromium_block", "Chromium", "technic_chromium_block.png", "technic:chromium_ingot") + ts_doors.register_door("technic:lead_block", "Lead", "technic_lead_block.png", "technic:lead_ingot") ts_doors.register_door("technic:stainless_steel_block", "Stainless Steel", "technic_stainless_steel_block.png", "technic:stainless_steel_ingot") - ts_doors.register_door("technic:zinc_block" , "Zinc" , "technic_zinc_block.png" , "technic:zinc_ingot" ) + ts_doors.register_door("technic:zinc_block", "Zinc", "technic_zinc_block.png", "technic:zinc_ingot") - ts_doors.register_door("technic:concrete" , "Concrete" , "technic_concrete_block.png" ) + ts_doors.register_door("technic:concrete", "Concrete", "technic_concrete_block.png") ts_doors.register_door("technic:blast_resistant_concrete", "Blast Resistant Concrete", "technic_blast_resistant_concrete_block.png") end + +minetest.override_item("doors:door_steel", { + description = "Windowed Locked Plain Steel Door", +}) + +minetest.override_item("doors:door_wood", { + description = "Windowed Mixed Wood Door", +}) + +minetest.override_item("doors:trapdoor", { + description = "Windowed Mixed Wood Trapdoor", +}) + +minetest.override_item("doors:trapdoor_steel", { + description = "Windowed Locked Plain Steel Trapdoor", +}) + + + + +ts_doors.workshop = {} + +function ts_doors.workshop.start(pos) + local node = minetest.get_node(pos) + if node.name ~= "ts_doors:workshop" then + return + end + + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local template = inv:get_stack("template", 1):get_name() + local material = inv:get_stack("material", 1):get_name() + local material_needed_name = inv:get_stack("material_needed", 1):get_name() + local material_needed = inv:get_stack("material_needed", 1):get_count() + local material_ok = inv:get_stack("material", 1):get_count() >= material_needed + local steel = inv:get_stack("steel", 1):get_name() + local steel_needed = inv:get_stack("steel_needed", 1):get_count() + local steel_ok = inv:get_stack("steel", 1):get_count() >= steel_needed + + if not (material_ok and steel_ok + and (steel and steel == "default:steel_ingot" or steel_needed == 0) + and template and template ~= "" + and material == material_needed_name) + then + return + end + minetest.chat_send_all("start") + + if not inv:room_for_item("output", { name = template, count = 1 }) then + return + end + + meta:set_string("working_on", template) + meta:set_int("progress", 0) + inv:remove_item("material", { name = material, count = material_needed }) + inv:remove_item("steel", { name = "default:steel_ingot", count = steel_needed }) + ts_doors.workshop.step(pos) +end + +function ts_doors.workshop.step(pos) + minetest.chat_send_all("step") + local node = minetest.get_node(pos) + if node.name ~= "ts_doors:workshop" then + return + end + local meta = minetest.get_meta(pos) + if meta:get_string("working_on") == "" then + return + end + local inv = meta:get_inventory() + local progress = meta:get_int("progress") + progress = progress + 1 + if progress < 10 then + minetest.after(0.2, ts_doors.workshop.step, pos) + else + meta:set_int("progress", 0) + progress = 0 + inv:add_item("output", meta:get_string("working_on")) + meta:set_string("working_on", "") + ts_doors.workshop.start(pos) + end + meta:set_int("progress", progress) + ts_doors.workshop.update_formspec(pos) +end + +function ts_doors.workshop.update_formspec(pos) + local node = minetest.get_node(pos) + if node.name ~= "ts_doors:workshop" then + return + end + local meta = minetest.get_meta(pos) + local page = meta:get_int("page") + local maxpage = meta:get_int("maxpage") + + local int_to_bool = {} + int_to_bool[0] = false + int_to_bool[1] = true + + local trapdoor = int_to_bool[meta:get_int("trapdoor")] or false + local locked = int_to_bool[meta:get_int("locked")] or false + local solid = int_to_bool[meta:get_int("solid")] or false + + if page < 1 then + page = maxpage + elseif page > maxpage then + page = 1 + end + meta:set_int("page", page) + + local fs = "size[9,9;]" + if not locked then + fs = fs .. "button[0,0;2,1;unlocked;" .. minetest.colorize("#ffff00", "Unlocked") .. "]" + fs = fs .. "button[0,0.75;2,1;locked;Locked]" + else + fs = fs .. "button[0,0;2,1;unlocked;Unlocked]" + fs = fs .. "button[0,0.75;2,1;locked;" .. minetest.colorize("#ffff00", "Locked") .. "]" + end + + if not solid then + fs = fs .. "button[2,0;2,1;windowed;" .. minetest.colorize("#ffff00", "Windowed") .. "]" + fs = fs .. "button[2,0.75;2,1;solid;Solid]" + else + fs = fs .. "button[2,0;2,1;windowed;Windowed]" + fs = fs .. "button[2,0.75;2,1;solid;" .. minetest.colorize("#ffff00", "Solid") .. "]" + end + + if not trapdoor then + fs = fs .. "button[4,0;2,1;doors;" .. minetest.colorize("#ffff00", "Doors") .. "]" + fs = fs .. "button[4,0.75;2,1;trapdoors;Trapdoors]" + else + fs = fs .. "button[4,0;2,1;doors;Doors]" + fs = fs .. "button[4,0.75;2,1;trapdoors;" .. minetest.colorize("#ffff00", "Trapdoors") .. "]" + end + fs = fs .. "label[0,1.5;Material]" + fs = fs .. "label[0,1.75;needed]" + fs = fs .. "list[context;material_needed;0,2.1;1,1]" + fs = fs .. "label[0,3.1;Input]" + fs = fs .. "list[context;material;0,3.5;1,1]" + fs = fs .. "label[1,1.5;Steel]" + fs = fs .. "label[1,1.75;needed]" + fs = fs .. "list[context;steel_needed;1,2.1;1,1]" + fs = fs .. "label[1,3.1;Input]" + fs = fs .. "list[context;steel;1,3.5;1,1]" + fs = fs .. "list[context;list;2,1.75;4,3;" .. tostring((page - 1) * 12) .. "]" + fs = fs .. "button[6,2.25;1,1;prevpage;<-]" + fs = fs .. "button[6,3.25;1,1;nextpage;->]" + fs = fs .. "label[6,4;" .. string.format("Page %s of %s", page, maxpage) .. "]" + fs = fs .. "label[7.5,0.5;Template]" + fs = fs .. "list[context;template;7.5,1;1,1]" + fs = fs .. "image[7.5,2;1,1;gui_furnace_arrow_bg.png^[lowpart:" .. meta:get_int("progress") * 10 .. ":gui_furnace_arrow_fg.png^[transformR180]" + fs = fs .. "list[context;output;7.5,3;1,1]" + fs = fs .. "list[current_player;main;0.5,5;8,4]" + meta:set_string("formspec", fs) +end + +local function update_inventory(pos) + local node = minetest.get_node(pos) + if node.name ~= "ts_doors:workshop" then + return + end + local meta = minetest.get_meta(pos) + + local itemcount = 0 + for k, v in pairs(ts_doors.registered_doors) do + itemcount = itemcount + 1 + end + + meta:set_int("maxpage", math.ceil(itemcount / 16)) + local inv = meta:get_inventory() + inv:set_size("material_needed", 0) + inv:set_size("material_needed", 1) + inv:set_size("steel_needed", 0) + inv:set_size("steel_needed", 1) + inv:set_size("material", 1) + inv:set_size("steel", 1) + inv:set_size("list", 0) + inv:set_size("list", math.ceil(itemcount / 12) * 12) -- 4x3 + inv:set_size("template", 1) + inv:set_size("output", 1) + + local int_to_bool = {} + int_to_bool[0] = false + int_to_bool[1] = true + + local trapdoor = int_to_bool[meta:get_int("trapdoor")] or false + local locked = int_to_bool[meta:get_int("locked")] or false + local solid = int_to_bool[meta:get_int("solid")] or false + + for item, recipe in pairs(ts_doors.registered_doors) do + if trapdoor then + if locked then + if solid then + inv:add_item("list", "ts_doors:trapdoor_full_locked_" .. item:gsub(":", "_")) + else + inv:add_item("list", "ts_doors:trapdoor_locked_" .. item:gsub(":", "_")) + end + else + if solid then + inv:add_item("list", "ts_doors:trapdoor_full_" .. item:gsub(":", "_")) + else + inv:add_item("list", "ts_doors:trapdoor_" .. item:gsub(":", "_")) + end + end + else + if locked then + if solid then + inv:add_item("list", "ts_doors:door_full_locked_" .. item:gsub(":", "_")) + else + inv:add_item("list", "ts_doors:door_locked_" .. item:gsub(":", "_")) + end + else + if solid then + inv:add_item("list", "ts_doors:door_full_" .. item:gsub(":", "_")) + else + inv:add_item("list", "ts_doors:door_" .. item:gsub(":", "_")) + end + end + end + end + local templatestack = inv:get_stack("template", 1) + if templatestack and templatestack:to_string() ~= "" then + local door = templatestack:to_table().name:sub(10) + if door:sub(0, 4) == "trap" then + trapdoor = true + door = door:sub(10) + else + trapdoor = false + door = door:sub(6) + end + if door:sub(0, 4) == "full" then + solid = true + door = door:sub(6) + else + solid = false + end + if door:sub(0, 7) == "locked_" then + locked = true + door = door:sub(8) + else + locked = false + end + local material_needed = 1 + if trapdoor then + material_needed = 4 + else + material_needed = 6 + end + if solid then + material_needed = material_needed + 1 + end + local steel_needed = 0 + if locked then + steel_needed = 1 + end + inv:add_item("material_needed", { name = ts_doors.registered_doors[door], count = material_needed }) + inv:add_item("steel_needed", { name = "default:steel_ingot", count = steel_needed }) + end + ts_doors.workshop.update_formspec(pos) + ts_doors.workshop.start(pos) +end + +local function on_receive_fields(pos, formname, fields, sender) + local meta = minetest.get_meta(pos) + if fields.unlocked then + meta:set_int("locked", 0) + elseif fields.locked then + meta:set_int("locked", 1) + elseif fields.windowed then + meta:set_int("solid", 0) + elseif fields.solid then + meta:set_int("solid", 1) + elseif fields.doors then + meta:set_int("trapdoor", 0) + elseif fields.trapdoors then + meta:set_int("trapdoor", 1) + elseif fields.prevpage then + meta:set_int("page", meta:get_int("page") - 1) + elseif fields.nextpage then + meta:set_int("page", meta:get_int("page") + 1) + end + update_inventory(pos) +end + +local function on_construct(pos) + local meta = minetest.get_meta(pos) + meta:set_int("trapdoor", 0) + meta:set_int("locked", 0) + meta:set_int("solid", 0) + meta:set_int("progress", 0) + meta:set_string("working_on", "") + meta:set_int("page", 1) + meta:set_int("maxpage", 1) + update_inventory(pos) +end + +local function allow_metadata_inventory_take(pos, listname, index, stack, player) + if listname == "material" or listname == "steel" or listname == "output" then + return stack:get_count() + else + return 0 + end +end + +local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) + if from_list == "list" and to_list == "template" then + return 1 + elseif from_list == "template" and to_list == "list" then + local inv = minetest.get_meta(pos):get_inventory() + return 1 + else + return 0 + end +end + +local function on_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) + update_inventory(pos) +end + +local function on_metadata_inventory_put(pos, listname, index, stack, player) + update_inventory(pos) +end + +local function on_metadata_inventory_take(pos, listname, index, stack, player) + update_inventory(pos) +end + +local function allow_metadata_inventory_put(pos, listname, index, stack, player) + if listname == "material" then + return stack:get_count() + elseif listname == "steel" and (stack:get_name() == "default:steel_ingot") then + return stack:get_count() + else + return 0 + end +end + +local function can_dig(pos, player) + local inv = minetest.get_meta(pos):get_inventory() + if inv:is_empty("material") and inv:is_empty("steel") and inv:is_empty("output") then + return true + else + return false + end +end + +minetest.register_node("ts_doors:workshop", { + description = "Door Workshop", + tiles = { + "default_wood.png", + "default_wood.png", + "default_wood.png^doors_item_wood.png", + "default_wood.png^doors_item_wood.png", + "default_wood.png", + "default_wood.png" + }, + drawtype = "nodebox", + paramtype = "light", + paramtype2 = "facedir", + groups = { cracky = 1 }, + node_box = { + type = "fixed", + fixed = { + { -0.5, -0.5, -0.5, 0.5, -0.3125, 0.5 }, -- NodeBox1 + { -0.5, -0.5, -0.5, -0.375, 0.5, 0.5 }, -- NodeBox2 + { 0.375, -0.5, -0.5, 0.5, 0.5, 0.5 }, -- NodeBox3 + { -0.5, -0.5, 0.375, 0.5, 0.5, 0.5 }, -- NodeBox4 + { -0.5, 0.3125, -0.4375, 0.5, 0.4375, -0.3125 }, -- NodeBox5 + } + }, + selection_box = { + type = "regular" + }, + on_receive_fields = on_receive_fields, + on_construct = on_construct, + allow_metadata_inventory_take = allow_metadata_inventory_take, + allow_metadata_inventory_move = allow_metadata_inventory_move, + allow_metadata_inventory_put = allow_metadata_inventory_put, + on_metadata_inventory_move = on_metadata_inventory_move, + on_metadata_inventory_put = on_metadata_inventory_put, + on_metadata_inventory_take = on_metadata_inventory_take, + can_dig = can_dig, +}) + +minetest.register_lbm({ + name = "ts_doors:update_door_workshop", + nodenames = { "ts_doors:door_workshop" }, + action = function(pos, node) + update_inventory(pos) + end, +}) + +minetest.register_craft({ + output = "ts_doors:workshop", + recipe = { + { "default:wood", "default:wood", "default:wood" }, + { "default:wood", "doors:door_wood", "default:wood" }, + { "default:wood", "default:wood", "default:wood" }, + } +}) \ No newline at end of file