Better tempaltes

This commit is contained in:
Novatux 2013-10-19 11:27:47 +02:00
parent eac4844175
commit 808d382867

@ -40,6 +40,25 @@ local function get_face(pos,ppos,pvect)
end end
end end
function lines(str)
local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)\r?\n", helper)))
return t
end
local function pos_to_string(pos)
if pos.x == 0 then pos.x = 0 end -- Fix for signed 0
if pos.y == 0 then pos.y = 0 end -- Fix for signed 0
if pos.z == 0 then pos.z = 0 end -- Fix for signed 0
return tostring(pos.x).."\n"..tostring(pos.y).."\n"..tostring(pos.z)
end
local function pos_from_string(str)
local l = lines(str)
return {x = tonumber(l[1]), y = tonumber(l[2]), z = tonumber(l[3])}
end
local function pos_in_list(l,pos) local function pos_in_list(l,pos)
for _,p in ipairs(l) do for _,p in ipairs(l) do
if p.x==pos.x and p.y==pos.y and p.z==pos.z then return true end if p.x==pos.x and p.y==pos.y and p.z==pos.z then return true end
@ -47,6 +66,13 @@ local function pos_in_list(l,pos)
return false return false
end end
local function table_empty(table)
for _, __ in pairs(table) do
return false
end
return true
end
local function add_table(table,toadd) local function add_table(table,toadd)
local i=1 local i=1
while true do while true do
@ -280,29 +306,56 @@ minetest.register_node("technic:frame_motor",{
-- Templates -- Templates
local function template_connected(pos,c) local function template_connected(pos,c,connectors)
for _,vect in ipairs({{x=0,y=1,z=0},{x=0,y=0,z=1},{x=0,y=0,z=-1},{x=1,y=0,z=0},{x=-1,y=0,z=0},{x=0,y=-1,z=0}}) do for _,vect in ipairs({{x=0,y=1,z=0},{x=0,y=0,z=1},{x=0,y=0,z=-1},{x=1,y=0,z=0},{x=-1,y=0,z=0},{x=0,y=-1,z=0}}) do
local pos1=vector.add(pos,vect) local pos1=vector.add(pos,vect)
local nodename=minetest.get_node(pos1).name local nodename=minetest.get_node(pos1).name
if not(pos_in_list(c,pos1)) and nodename=="technic:template" then if not(pos_in_list(c,pos1)) and (nodename=="technic:template" or nodename == "technic:template_connector")then
local meta = minetest.get_meta(pos1) local meta = minetest.get_meta(pos1)
if meta:get_string("connected") == "" then if meta:get_string("connected") == "" then
c[#(c)+1]=pos1 c[#(c)+1]=pos1
template_connected(pos1,c) template_connected(pos1,c,connectors)
if nodename == "technic:template_connector" then
connectors[#connectors+1] = pos1
end
end end
end end
end end
end end
local function get_templates(pos) local function get_templates(pos)
c={pos} local c = {pos}
template_connected(pos,c) local connectors
return c if minetest.get_node(pos).name == "technic:template_connector" then
connectors = {pos}
else
connectors = {}
end
template_connected(pos,c,connectors)
return c, connectors
end
local function swap_template(pos, new)
local meta = minetest.get_meta(pos)
local saved_node = meta:get_string("saved_node")
meta:set_string("saved_node", "")
hacky_swap_node(pos, new)
local meta = minetest.get_meta(pos)
meta:set_string("saved_node", saved_node)
end end
local function save_node(pos) local function save_node(pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.name == "air" or node.name == "technic:template" then return end if node.name == "air" then
minetest.set_node(pos, {name="technic:template"})
return
end
if node.name == "technic:template" then
swap_template(pos, "technic:template_connector")
local meta = minetest.get_meta(pos)
meta:set_string("connected", "")
return
end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local meta0 = meta:to_table() local meta0 = meta:to_table()
for _, list in pairs(meta0.inventory) do for _, list in pairs(meta0.inventory) do
@ -311,6 +364,7 @@ local function save_node(pos)
end end
end end
node.meta = meta0 node.meta = meta0
minetest.set_node(pos, {name="technic:template"})
return node return node
end end
@ -329,28 +383,75 @@ local function expand_template(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local c = meta:get_string("connected") local c = meta:get_string("connected")
if c == "" then return end if c == "" then return end
meta:set_string("connected", "")
c = minetest.deserialize(c) c = minetest.deserialize(c)
for _, vect in ipairs(c) do for _, vect in ipairs(c) do
if vect.x ~= 0 or vect.y ~= 0 or vect.z ~= 0 then
local pos1 = vector.add(pos, vect) local pos1 = vector.add(pos, vect)
local saved_node = save_node(pos1) local saved_node = save_node(pos1)
minetest.set_node(pos1, {name="technic:template"})
local meta1 = minetest.get_meta(pos1) local meta1 = minetest.get_meta(pos1)
if saved_node ~= nil then if saved_node ~= nil then
meta1:set_string("saved_node", minetest.serialize(saved_node)) meta1:set_string("saved_node", minetest.serialize(saved_node))
else else
meta1:set_string("saved_node", "") --meta1:set_string("saved_node", "")
end end
end end
end end
local function compress_templates(pos)
local templates, connectors = get_templates(pos)
if #connectors == 0 then
connectors = {pos}
end
for _, cn in ipairs(connectors) do
local meta = minetest.get_meta(cn)
local c = {}
for _,p in ipairs(templates) do
local np = vector.subtract(p, cn)
if not pos_in_list(c,np) then
c[#c+1] = np
end
end
local cc = {}
for _,p in ipairs(connectors) do
local np = vector.subtract(p, cn)
if (np.x ~= 0 or np.y ~= 0 or np.z ~= 0) then
cc[pos_to_string(np)] = true
end
end
swap_template(cn, "technic:template")
meta:set_string("connected", minetest.serialize(c))
meta:set_string("connectors_connected", minetest.serialize(cc))
end
for _,p in ipairs(templates) do
if not pos_in_list(connectors, p) then
minetest.set_node(p, {name = "air"})
end
end
end end
local function template_drops(pos, node, oldmeta, digger) local function template_drops(pos, node, oldmeta, digger)
local c = oldmeta.fields.connected local c = oldmeta.fields.connected
local cc = oldmeta.fields.connectors_connected
local drops local drops
if c == "" or c == nil then if c == "" or c == nil then
drops = {"technic:template 1"} drops = {"technic:template 1"}
else
if cc == "" or cc == nil then
drops = {"technic:template 1"}
else
local dcc = minetest.deserialize(cc)
if not table_empty(dcc) then
drops = {}
for sp, _ in pairs(dcc) do
local ssp = pos_from_string(sp)
local p = vector.add(ssp, pos)
local meta = minetest.get_meta(p)
local d = minetest.deserialize(meta:get_string("connectors_connected"))
if d ~= nil then
d[pos_to_string({x=-ssp.x, y=-ssp.y, z=-ssp.z})] = nil
meta:set_string("connectors_connected", minetest.serialize(d))
end
end
else else
local stack_max = 99 local stack_max = 99
local num = #(minetest.deserialize(c)) local num = #(minetest.deserialize(c))
@ -361,26 +462,29 @@ local function template_drops(pos, node, oldmeta, digger)
end end
drops[#drops+1] = "technic:template "..num drops[#drops+1] = "technic:template "..num
end end
print(dump(drops)) end
end
minetest.handle_node_drops(pos, drops, digger) minetest.handle_node_drops(pos, drops, digger)
end end
local function template_on_destruct(pos, node)
local meta = minetest.get_meta(pos)
local saved_node = meta:get_string("saved_node")
if saved_node ~= "" then
local nnode = minetest.deserialize(saved_node)
minetest.after(0, restore_node, pos, nnode)
end
end
minetest.register_node("technic:template",{ minetest.register_node("technic:template",{
description = "Template", description = "Template",
tiles = {"technic_mv_cable.png"}, tiles = {"technic_mv_cable.png"},
drop = "", drop = "",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
on_destruct = function(pos, node) on_destruct = template_on_destruct,
local meta = minetest.get_meta(pos)
local saved_node = meta:get_string("saved_node")
if saved_node ~= "" then
local nnode = minetest.deserialize(saved_node)
minetest.after(0, restore_node, pos, nnode)
end
end,
after_dig_node = template_drops, after_dig_node = template_drops,
on_punch = function(pos,node,puncher) on_punch = function(pos,node,puncher)
hacky_swap_node(pos, "technic:template_disabled") swap_template(pos, "technic:template_disabled")
end end
}) })
@ -388,18 +492,24 @@ minetest.register_node("technic:template_disabled",{
description = "Template", description = "Template",
tiles = {"technic_hv_cable.png"}, tiles = {"technic_hv_cable.png"},
drop = "", drop = "",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
on_destruct = function(pos, node) on_destruct = template_on_destruct,
local meta = minetest.get_meta(pos)
local saved_node = meta:get_string("saved_node")
if saved_node ~= "" then
local nnode = minetest.deserialize(saved_node)
minetest.after(0, restore_node, pos, nnode)
end
end,
after_dig_node = template_drops, after_dig_node = template_drops,
on_punch = function(pos,node,puncher) on_punch = function(pos,node,puncher)
hacky_swap_node(pos, "technic:template") local meta = minetest.get_meta(pos)
swap_template(pos, "technic:template_connector")
end
})
minetest.register_node("technic:template_connector",{
description = "Template",
tiles = {"technic_lv_cable.png"},
drop = "",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
on_destruct = template_on_destruct,
after_dig_node = template_drops,
on_punch = function(pos,node,puncher)
swap_template(pos, "technic:template")
end end
}) })
@ -414,7 +524,6 @@ minetest.register_craftitem("technic:template_replacer",{
local node = minetest.get_node(p) local node = minetest.get_node(p)
if node.name == "technic:template" then return end if node.name == "technic:template" then return end
local saved_node = save_node(p) local saved_node = save_node(p)
minetest.set_node(p, {name="technic:template"})
itemstack:take_item() itemstack:take_item()
if saved_node ~= nil then if saved_node ~= nil then
local meta = minetest.get_meta(p) local meta = minetest.get_meta(p)
@ -433,25 +542,15 @@ minetest.register_tool("technic:template_tool",{
return nil return nil
end end
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.name ~= "technic:template" then return end if node.name ~= "technic:template" and node.name ~= "technic:template_connector" then return end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local c2 = meta:get_string("connected") local c2 = meta:get_string("connected")
if c2 ~= "" then if c2 ~= "" then
expand_template(pos) expand_template(pos)
return else
compress_templates(pos)
end end
local templates = get_templates(pos)
local c = {}
for _,p in ipairs(templates) do
local np = {x=p.x-pos.x, y=p.y-pos.y, z=p.z-pos.z}
if not pos_in_list(c,np) then
c[#c+1] = np
end
if p.x ~= pos.x or p.y ~= pos.y or p.z ~= pos.z then
minetest.set_node(p, {name = "air"})
end
end
meta:set_string("connected", minetest.serialize(c))
end end
}) })