Add part builder node, fix formspecs

This commit is contained in:
Evert Prants 2018-04-05 15:40:35 +03:00
parent a6f8bd52fd
commit 6a5c1fe4b2
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
8 changed files with 292 additions and 28 deletions

View File

@ -33,8 +33,8 @@ function metal_caster.get_metal_caster_formspec_default()
"image[6.68,0;1.4,2.8;melter_gui_barbg.png]"..
"image[6.68,0;1.4,2.8;melter_gui_gauge.png]"..
"label[0.08,3.75;No Molten Metal]"..
"list[context;bucket_in;4.75,0.2;2,2;]"..
"list[context;bucket_out;4.75,1.4;2,2;]"..
"list[context;bucket_in;4.75,0.2;1,1;]"..
"list[context;bucket_out;4.75,1.4;1,1;]"..
"image[5.75,0.2;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
"image[5.75,1.4;1,1;gui_furnace_arrow_bg.png^[transformR90]"..
"button[6.68,2.48;1.33,1;dump;Dump]"..
@ -79,8 +79,8 @@ function metal_caster.get_metal_caster_formspec(data)
"image[6.68,"..(2.44 - metal_percent * 2.44)..";1.4,"..(metal_percent * 2.8)..";"..data.metal_texture.."]"..
"image[6.68,0;1.4,2.8;melter_gui_gauge.png]"..
metal_formspec..
"list[context;bucket_in;4.75,0.2;2,2;]"..
"list[context;bucket_out;4.75,1.4;2,2;]"..
"list[context;bucket_in;4.75,0.2;1,1;]"..
"list[context;bucket_out;4.75,1.4;1,1;]"..
"image[5.75,0.2;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
"image[5.75,1.4;1,1;gui_furnace_arrow_bg.png^[transformR90]"..
"button[6.68,2.48;1.33,1;dump;Dump]"..
@ -323,7 +323,7 @@ local function caster_node_timer(pos, elapsed)
local cast = metal_caster.casts[castname]
local result_name = find_castable(metal_type, castname)
if result_name ~= nil then
local result_cost = cast.cost
local result_cost = cast.cost * metal_caster.spec.ingot
local coolant_cost = result_cost / 4
if metal_count >= result_cost and coolant_count >= coolant_cost then
@ -418,10 +418,10 @@ function metal_caster.register_cast(name, data)
local castname = mod..":"..name.."_cast"
minetest.register_craftitem(castname, {
description = data.description.." Cast",
description = data.description.." Cast\n\nMaterial Cost: "..data.cost,
inventory_image = "caster_"..name.."_cast.png",
stack_max = 1,
groups = {cast=1}
groups = {tinker_cast=1}
})
if not metal_caster.casts[name] then

View File

@ -60,8 +60,8 @@ function metal_melter.get_metal_melter_formspec_default()
"image[6.68,0;1.4,2.8;melter_gui_barbg.png]"..
"image[6.68,0;1.4,2.8;melter_gui_gauge.png]"..
"label[0.08,3.75;No Molten Metal]"..
"list[context;bucket_in;4.75,0.2;2,2;]"..
"list[context;bucket_out;4.75,1.4;2,2;]"..
"list[context;bucket_in;4.75,0.2;1,1;]"..
"list[context;bucket_out;4.75,1.4;1,1;]"..
"image[5.75,0.2;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
"image[5.75,1.4;1,1;gui_furnace_arrow_bg.png^[transformR90]"..
"button[6.68,2.48;1.33,1;dump;Dump]"..

View File

@ -1,5 +1,19 @@
local modifiers = {
flint = {
cracky = {times={[3]=1.20}, uses=5, maxlevel=1},
crumbly = {times={[1]=2.90, [2]=1.50, [3]=0.30}, uses=5, maxlevel=1},
snappy = {times={[2]=1.3, [3]=0.20}, uses=5, maxlevel=1},
choppy = {times={[2]=2.70, [3]=1.20}, uses=5, maxlevel=1},
damagegroups = {fleshy = 1},
explody = nil,
binding = {increase = 0.00, uses = 0},
rod = {increase = 0.00, uses = 0},
tags = {
{name = "cheap", description = "Cheap"}
}
},
wood = {
cracky = {times={[3]=1.60}, uses=10, maxlevel=1},
crumbly = {times={[1]=3.00, [2]=1.60, [3]=0.60}, uses=10, maxlevel=1},
@ -235,8 +249,9 @@ local modifiers = {
tinkering.materials = {
-- Materials
wood = {name = "Wood", default = "group:wood", color = "#634623", base = "group", modifier = modifiers.wood},
stone = {name = "Stone", default = "group:stone", color = "#8D8988", base = "group", modifier = modifiers.stone},
flint = {name = "Flint", default = "default:flint", color = "#514E49", base = "item", modifier = modifiers.flint},
wood = {name = "Wood", default = "wood", color = "#634623", base = "group", modifier = modifiers.wood},
stone = {name = "Stone", default = "stone", color = "#8D8988", base = "group", modifier = modifiers.stone},
obsidian = {name = "Obsidian", default = "default:obsidian", color = "#2C384E", base = "node", cast = true, modifier = modifiers.obsidian},
-- Metals
@ -244,7 +259,7 @@ tinkering.materials = {
copper = {name = "Copper", default = "default:copper_ingot", color = "#E87945", base = "ingot", cast = true, modifier = modifiers.copper},
tin = {name = "Tin", default = "default:tin_ingot", color = "#C1C1C1", base = "ingot", cast = true, modifier = modifiers.tin},
gold = {name = "Gold", default = "default:gold_ingot", color = "#FFFF54", base = "ingot", cast = true, modifier = modifiers.gold},
mese = {name = "Mese", default = "default:mese_crystal", color = "#FFFF02", base = "gem", modifier = modifiers.mese},
mese = {name = "Mese", default = "default:mese_crystal", color = "#FFFF02", base = "gem", cast = true, modifier = modifiers.mese},
-- From technic
lead = {name = "Lead", default = "technic:lead_ingot", color = "#C6C6C6", base = "ingot", cast = true, modifier = modifiers.lead},

View File

@ -0,0 +1,246 @@
part_builder = {}
function part_builder.get_formspec()
return "size[8,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;pattern;1,1.5;1,1;]"..
"list[context;input;2,1;1,2;]"..
"list[context;output;6,1.5;1,1;]"..
"image[4,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
"listring[context;pattern]"..
"listring[context;input]"..
"listring[context;output]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25)
end
local function get_template_group(groups)
if not groups then return nil end
for g,i in pairs(groups) do
if g:find("tc_") == 1 then
return g:gsub("^tc_", "")
end
end
return nil
end
local function find_buildable(material_name, pattern_name)
local types = metal_melter.melts[material_name]
if not types then return nil end
local typeres = types[pattern_name]
if not typeres then return nil end
if #typeres > 0 then
return typeres[1]
end
return nil
end
local function find_material_match(type, stack)
local match = nil
for mat,iv in pairs(tinkering.materials) do
if match then break end
if not iv.cast then
if iv.base == "group" and minetest.get_item_group(stack, iv.default) > 0 then
match = mat
elseif stack == iv.default then
match = mat
end
end
end
return match
end
local function get_output(inv)
local pattern = inv:get_stack("pattern", 1):get_name()
local find_pattern = get_template_group(minetest.registered_items[pattern].groups)
if not find_pattern then return nil end
local list = inv:get_list("input")
local material = nil
local total_count = 0
local output = nil
for _,stack in pairs(list) do
if not stack:is_empty() then
local mat = find_material_match(find_pattern, stack:get_name())
-- Both input slots need to be of the same material, unless one of them is empty
if material and mat ~= material then
material = nil
break
end
material = mat
total_count = total_count + stack:get_count()
end
end
if not material then return nil end
local cost = tinkering.patterns[find_pattern].cost
if total_count < cost then return nil end
local output_stack = find_buildable(material, find_pattern)
return output_stack, cost
end
local function handle_take_output(pos, listname)
local inv = minetest.get_meta(pos):get_inventory()
local output, cost = get_output(inv)
local left = cost
local input = inv:get_list("input")
for _,stack in pairs(input) do
if not stack:is_empty() then
local stack_cnt = stack:get_count()
if stack_cnt > left then
stack:set_count(stack_cnt - left)
break
else
if stack_cnt == left then
stack:clear()
break
else
left = left - stack:get_count()
stack:clear()
end
end
end
end
inv:set_list("input", input)
minetest.get_node_timer(pos):start(0.05)
end
local function on_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local output = get_output(inv)
if output then
inv:set_list("output", {output})
else
inv:set_list("output", {})
end
return false
end
local function allow_metadata_inventory_put (pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "output" then
return 0
end
if listname == "pattern" then
if minetest.get_item_group(stack:get_name(), "tinker_pattern") == 0 then
return 0
end
end
return stack:get_count()
end
local function allow_metadata_inventory_move (pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
local function allow_metadata_inventory_take (pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end
local function on_construct(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", part_builder.get_formspec())
-- Create inventory
local inv = meta:get_inventory()
inv:set_size('pattern', 1)
inv:set_size('input', 2)
inv:set_size('output', 1)
end
local function on_take(pos, listname, index, stack, player)
local inv = minetest.get_meta(pos):get_inventory()
if listname == "output" then
handle_take_output(pos, "input")
end
minetest.get_node_timer(pos):start(0.02)
end
local function can_dig(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("input") and inv:is_empty("output") and inv:is_empty("pattern")
end
local function on_receive_fields(pos, formname, fields, sender)
if sender and minetest.is_protected(pos, sender:get_player_name()) then
return 0
end
minetest.get_node_timer(pos):start(0.02)
end
minetest.register_node("tinkering:part_builder", {
description = "Part Builder",
tiles = {
"tinkering_blank_pattern.png", "tinkering_bench_bottom.png",
"tinkering_bench_side.png", "tinkering_bench_side.png",
"tinkering_bench_side.png", "tinkering_bench_side.png"
},
drawtype = "nodebox",
paramtype = "light",
node_box = tinkering.bench,
on_construct = on_construct,
legacy_facedir_simple = true,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
can_dig = can_dig,
on_timer = on_timer,
on_construct = on_construct,
on_receive_fields = on_receive_fields,
on_metadata_inventory_move = function(pos)
minetest.get_node_timer(pos):start(0.05)
end,
on_metadata_inventory_put = function(pos)
minetest.get_node_timer(pos):start(0.05)
end,
on_metadata_inventory_take = on_take,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
allow_metadata_inventory_move = allow_metadata_inventory_move,
groups = {choppy = 2, oddly_breakable_by_hand = 2}
})

View File

@ -36,7 +36,6 @@ function tool_station.get_formspec()
"list[current_player;main;0,5.5;8,3;8]"..
tool_list..
"listring[context;input]"..
"listring[current_player;main]"..
"listring[context;output]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25)
@ -45,8 +44,8 @@ end
local function get_metalgroup(groups)
if not groups then return nil end
for g,i in pairs(groups) do
if g:find("metal_") == 1 then
return g:gsub("^metal_", "")
if g:find("material_") == 1 then
return g:gsub("^material_", "")
end
end
return nil

View File

@ -13,3 +13,6 @@ tinkering.bench = {
-- Tool Station
dofile(tinkering.modpath.."/nodes/tool_station.lua")
-- Part Builder
dofile(tinkering.modpath.."/nodes/part_builder.lua")

View File

@ -1,16 +1,16 @@
tinkering.pattern = {}
tinkering.patterns = {}
-- Register a new pattern
function tinkering.register_pattern(name, data)
local mod = data.mod_name or minetest.get_current_modname()
local desc = data.description
tinkering.pattern[name] = data
tinkering.patterns[name] = data
minetest.register_craftitem(mod..":"..name.."_pattern", {
description = desc.." Pattern",
description = desc.." Pattern\n\nMaterial Cost: "..data.cost,
inventory_image = "tinkering_"..name.."_pattern.png",
groups = {pattern=1}
groups = {tinker_pattern=1, ["tc_"..name] = 1}
})
end
@ -18,5 +18,5 @@ end
minetest.register_craftitem("tinkering:blank_pattern", {
description = "Blank Pattern",
inventory_image = "tinkering_blank_pattern.png",
groups = {pattern=1}
groups = {tinker_pattern=1}
})

View File

@ -62,12 +62,12 @@ tinkering.tools = {
}
tinkering.components = {
pickaxe_head = {description = "%s Pickaxe Head", materials = 1, image = tinkering.tools.pick.textures.main},
axe_head = {description = "%s Axe Head", materials = 1, image = tinkering.tools.axe.textures.main},
sword_blade = {description = "%s Sword Blade", materials = 1, image = tinkering.tools.sword.textures.main},
shovel_head = {description = "%s Shovel Head", materials = 1, image = tinkering.tools.shovel.textures.main},
tool_rod = {description = "%s Tool Rod", materials = 1, image = "tinkering_tool_rod.png"},
tool_binding = {description = "%s Tool Binding", materials = 1, image = "tinkering_tool_binding.png"}
pickaxe_head = {description = "%s Pickaxe Head", material_cost = 2, image = tinkering.tools.pick.textures.main},
axe_head = {description = "%s Axe Head", material_cost = 2, image = tinkering.tools.axe.textures.main},
sword_blade = {description = "%s Sword Blade", material_cost = 2, image = tinkering.tools.sword.textures.main},
shovel_head = {description = "%s Shovel Head", material_cost = 2, image = tinkering.tools.shovel.textures.main},
tool_rod = {description = "%s Tool Rod", material_cost = 1, image = "tinkering_tool_rod.png"},
tool_binding = {description = "%s Tool Binding", material_cost = 2, image = "tinkering_tool_binding.png"}
}
-- Create component for material
@ -78,7 +78,7 @@ local function create_material_component(data)
local groups = {tinker_component = 1}
groups["tc_"..data.component] = 1
groups["metal_"..data.metal] = 1
groups["material_"..data.metal] = 1
minetest.register_craftitem(mod..":"..name, {
description = desc,
@ -103,13 +103,14 @@ function tinkering.register_component(name, data)
description = comp_desc,
mod_name = mod,
result = name,
cost = metal_caster.spec.cast,
cost = data.material_cost,
typenames = {name}
})
-- Register pattern
tinkering.register_pattern(name, {
description = comp_desc,
cost = data.material_cost,
mod_name = mod
})