xdecor-libre/worktable.lua

227 lines
5.7 KiB
Lua
Raw Normal View History

local material = {
2015-06-25 18:25:36 +03:00
"cloud", -- Only used for the formspec display.
"wood", "junglewood", "pinewood",
"tree", "jungletree", "pinetree",
"cobble", "mossycobble", "desert_cobble",
"stone", "sandstone", "desert_stone", "obsidian",
"stonebrick", "sandstonebrick", "desert_stonebrick", "obsidianbrick",
2015-06-25 18:25:36 +03:00
"coalblock", "copperblock", "bronzeblock",
"goldblock", "steelblock", "diamondblock",
"clay", "ice", "meselamp",
"glass", "obsidian_glass"
}
local def = { -- Node name, yield, nodebox shape.
{ "nanoslab", "16",
2015-06-25 23:46:40 +03:00
{-0.5, -0.5, -0.5, 0, -0.4375, 0} },
2015-06-25 18:25:36 +03:00
{ "micropanel", "16",
2015-06-25 23:46:40 +03:00
{-0.5, -0.5, -0.5, 0.5, -0.4375, 0} },
2015-06-25 18:25:36 +03:00
{ "microslab", "8",
2015-06-25 23:46:40 +03:00
{-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5} },
2015-06-25 18:25:36 +03:00
{ "panel", "4",
2015-06-25 23:46:40 +03:00
{-0.5, -0.5, -0.5, 0.5, 0, 0} },
2015-06-25 18:25:36 +03:00
{ "slab", "2",
2015-06-25 23:46:40 +03:00
{-0.5, -0.5, -0.5, 0.5, 0, 0.5} },
2015-06-25 18:25:36 +03:00
{ "outerstair", "1", {
{-0.5, -0.5, -0.5, 0.5, 0, 0.5},
2015-06-25 23:46:40 +03:00
{-0.5, 0, 0, 0, 0.5, 0.5} } },
2015-06-25 18:25:36 +03:00
{ "stair", "1", {
{-0.5, -0.5, -0.5, 0.5, 0, 0.5},
2015-06-25 23:46:40 +03:00
{-0.5, 0, 0, 0.5, 0.5, 0.5} } },
2015-06-25 18:25:36 +03:00
{ "innerstair", "1", {
{-0.5, -0.5, -0.5, 0.5, 0, 0.5},
{-0.5, 0, 0, 0.5, 0.5, 0.5},
2015-06-25 23:46:40 +03:00
{-0.5, 0, -0.5, 0, 0.5, 0} } }
}
2015-06-14 16:29:30 +03:00
local function xconstruct(pos)
local meta = minetest.get_meta(pos)
2015-06-14 20:55:59 +03:00
local nodebtn = {}
2015-06-14 16:29:30 +03:00
for i=1, #def do
2015-06-15 17:37:11 +03:00
nodebtn[#nodebtn+1] = "item_image_button["..(i-1)..
",0.5;1,1;xdecor:"..def[i][1].."_cloud;"..def[i][1]..";]"
end
2015-06-14 20:55:59 +03:00
nodebtn = table.concat(nodebtn)
2015-06-16 19:47:29 +03:00
meta:set_string("formspec", "size[8,7;]"..xdecor.fancy_gui..
"label[0,0;Cut your material into...]"..
2015-06-14 16:29:30 +03:00
nodebtn..
"label[0,1.5;Input]"..
"list[current_name;input;0,2;1,1;]"..
"image[1,2;1,1;xdecor_saw.png]"..
"label[2,1.5;Output]"..
"list[current_name;output;2,2;1,1;]"..
"label[4.5,1.5;Damaged tool]"..
"list[current_name;src;5,2;1,1;]"..
"image[6.07,2.07;0.85,0.85;xdecor_anvil.png]"..
"label[6.8,1.5;Hammer]]"..
"list[current_name;fuel;7,2;1,1;]"..
"list[current_player;main;0,3.25;8,4;]")
2015-06-14 16:29:30 +03:00
meta:set_string("infotext", "Work Table")
2015-06-25 18:25:36 +03:00
local inv = meta:get_inventory()
inv:set_size("output", 1)
inv:set_size("input", 1)
inv:set_size("src", 1)
inv:set_size("fuel", 1)
end
2015-06-14 16:29:30 +03:00
local function xfields(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local inputstack = inv:get_stack("input", 1)
local outputstack = inv:get_stack("output", 1)
2015-06-15 17:37:11 +03:00
local shape, get = {}, {}
local anz = 0
2015-06-14 20:55:59 +03:00
for m=1, #material do
for n=1, #def do
local v = material[m]
local w = def[n]
2015-06-25 18:25:36 +03:00
if (inputstack:get_name() == "default:"..v) and
(outputstack:get_count() < 99) and fields[w[1]] then
2015-06-15 16:31:18 +03:00
shape = "xdecor:"..w[1].."_"..v
anz = w[2]
get = shape.." "..anz
inv:add_item("output", get)
inputstack:take_item()
inv:set_stack("input", 1, inputstack)
end
end
end
end
2015-06-14 16:29:30 +03:00
local function xdig(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2015-06-25 18:25:36 +03:00
if not inv:is_empty("input") or not inv:is_empty("output") or not
inv:is_empty("fuel") or not inv:is_empty("src") then
2015-06-25 18:25:36 +03:00
return false
end
return true
end
local function xput(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "output" then
return 0
end
2015-07-05 17:40:33 +03:00
if listname == "fuel" then
if stack:get_name() == "xdecor:hammer" then
return stack:get_count()
else
return 0
end
end
return stack:get_count()
end
2015-06-13 20:08:35 +03:00
xdecor.register("worktable", {
2015-06-25 18:25:36 +03:00
description = "Work Table",
groups = {snappy=3},
2015-06-25 23:46:40 +03:00
sounds = xdecor.wood,
2015-06-25 18:25:36 +03:00
tiles = {
"xdecor_worktable_top.png", "xdecor_worktable_top.png",
2015-06-13 20:08:35 +03:00
"xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
2015-06-25 18:25:36 +03:00
"xdecor_worktable_front.png", "xdecor_worktable_front.png"
},
on_construct = xconstruct,
on_receive_fields = xfields,
can_dig = xdig,
allow_metadata_inventory_put = xput
2015-06-25 18:25:36 +03:00
})
2015-06-25 23:46:40 +03:00
local function light(mat)
2015-06-25 18:25:36 +03:00
if (mat == "meselamp") then
return 12
else
return 0
end
end
2015-06-25 23:46:40 +03:00
local function sound(mat)
if string.find(mat, "glass") or string.find(mat, "lamp") or
string.find(mat, "ice") then
return default.node_sound_glass_defaults()
2015-06-15 17:37:11 +03:00
elseif string.find(mat, "wood") or string.find(mat, "tree") then
return default.node_sound_wood_defaults()
else
return default.node_sound_stone_defaults()
end
end
2015-06-25 23:46:40 +03:00
local function name(mat)
2015-06-15 17:37:11 +03:00
if string.find(mat, "block") then
local newname = string.gsub(mat, "(block)", "_%1")
return "default_"..newname..".png"
2015-06-15 17:37:11 +03:00
elseif string.find(mat, "brick") then
local newname = string.gsub(mat, "(brick)", "_%1")
return "default_"..newname..".png"
2015-07-02 16:26:19 +03:00
elseif string.find(mat, "tree") then
local newname = string.gsub(mat, "(tree)", "%1_top")
return "default_"..newname..".png"
2015-06-25 18:25:36 +03:00
else
return "default_"..mat..".png"
end
end
2015-06-14 20:55:59 +03:00
for m=1, #material do
local v = material[m]
2015-06-25 23:46:40 +03:00
local light = light(v)
local sound = sound(v)
local tile = name(v)
2015-06-14 20:55:59 +03:00
for n=1, #def do
2015-06-25 18:25:36 +03:00
local w = def[n]
xdecor.register(w[1].."_"..v, {
description = string.sub(string.upper(w[1]), 0, 1)..
string.sub(w[1], 2),
light_source = light,
sounds = sound,
tiles = {tile},
2015-07-04 12:23:20 +03:00
groups = {cracky=2, not_in_creative_inventory=1},
2015-06-25 18:25:36 +03:00
node_box = {
type = "fixed",
fixed = w[3]
},
on_place = minetest.rotate_node
})
end
end
2015-06-25 23:46:40 +03:00
minetest.register_abm({ -- Repair Tool's code by Krock, modified by kilbith.
2015-06-13 20:08:35 +03:00
nodenames = {"xdecor:worktable"},
interval = 5, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local src = inv:get_stack("src", 1)
local wear = src:get_wear()
local repair = -1400
2015-06-25 18:25:36 +03:00
if (src:is_empty() or wear == 0 or wear == 65535) then
return
end
local fuel = inv:get_stack("fuel", 1)
2015-06-25 18:25:36 +03:00
if (fuel:is_empty() or fuel:get_name() ~= "xdecor:hammer") then
return
end
if (wear + repair < 0) then
src:add_wear(repair + wear)
else
src:add_wear(repair)
end
inv:set_stack("src", 1, src)
inv:remove_item("fuel", "xdecor:hammer 1")
end
})