From c06cdf603ef0e685a5c86baf950ab68757798682 Mon Sep 17 00:00:00 2001 From: Novatux Date: Sun, 13 Oct 2013 10:26:18 +0200 Subject: [PATCH] Add templates --- technic/machines/other/frames.lua | 346 +++++++++++++++++++++++------- 1 file changed, 267 insertions(+), 79 deletions(-) diff --git a/technic/machines/other/frames.lua b/technic/machines/other/frames.lua index 0c17885..6fd7b98 100644 --- a/technic/machines/other/frames.lua +++ b/technic/machines/other/frames.lua @@ -1,6 +1,9 @@ -frames={} +frames = {} -function get_face(pos,ppos,pvect) +-- Helpers + +local function get_face(pos,ppos,pvect) + -- Raytracer to get which face has been clicked ppos={x=ppos.x-pos.x,y=ppos.y-pos.y+1.5,z=ppos.z-pos.z} if pvect.x>0 then local t=(-0.5-ppos.x)/pvect.x @@ -37,7 +40,74 @@ function get_face(pos,ppos,pvect) end end +local function pos_in_list(l,pos) + 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 + end + return false +end +local function add_table(table,toadd) + local i=1 + while true do + o=table[i] + if o==toadd then return end + if o==nil then break end + i=i+1 + end + table[i]=toadd +end + +local function move_nodes_vect(poslist,vect,must_not_move) + for _,pos in ipairs(poslist) do + local npos=vector.add(pos,vect) + local name = minetest.env:get_node(npos).name + if (name~="air" and minetest.registered_nodes[name].liquidtype=="none") and not(pos_in_list(poslist,npos)) then + return + end + if pos.x==must_not_move.x and pos.y==must_not_move.y and pos.z==must_not_move.z then + return + end + end + nodelist={} + for _,pos in ipairs(poslist) do + local node=minetest.env:get_node(pos) + local meta=minetest.env:get_meta(pos):to_table() + nodelist[#(nodelist)+1]={pos=pos,node=node,meta=meta} + end + objects={} + for _,pos in ipairs(poslist) do + for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do + add_table(objects,object) + end + end + for _,obj in ipairs(objects) do + obj:setpos(vector.add(obj:getpos(),vect)) + le=obj:get_luaentity() + if le and le.name == "pipeworks:tubed_item" then + le.start_pos=vector.add(le.start_pos,vect) + end + end + for _,n in ipairs(nodelist) do + local npos=vector.add(n.pos,vect) + minetest.env:set_node(npos,n.node) + local meta=minetest.env:get_meta(npos) + meta:from_table(n.meta) + for __,pos in ipairs(poslist) do + if npos.x==pos.x and npos.y==pos.y and npos.z==pos.z then + table.remove(poslist, __) + break + end + end + end + for __,pos in ipairs(poslist) do + minetest.env:remove_node(pos) + end +end + + + +-- Frames for xm=0,1 do for xp=0,1 do for ym=0,1 do @@ -141,9 +211,36 @@ end end end -function frame_motor_on(pos, node) + + + +-- Frame motor +local function connected(pos,c,adj) + for _,vect in ipairs(adj) do + local pos1=vector.add(pos,vect) + local nodename=minetest.env:get_node(pos1).name + if not(pos_in_list(c,pos1)) and nodename~="air" and + (minetest.registered_nodes[nodename].frames_can_connect==nil or + minetest.registered_nodes[nodename].frames_can_connect(pos1,vect)) then + c[#(c)+1]=pos1 + if minetest.registered_nodes[nodename].frame==1 then + local adj=minetest.registered_nodes[nodename].frame_connect_all(pos1) + connected(pos1,c,adj) + end + end + end +end + +local function get_connected_nodes(pos) + c={pos} + local nodename=minetest.env:get_node(pos).name + connected(pos,c,minetest.registered_nodes[nodename].frame_connect_all(pos)) + return c +end + +local function frame_motor_on(pos, node) local dirs = {{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}} - local nnodepos = frames.addVect(pos, dirs[math.floor(node.param2/4)+1]) + local nnodepos = vector.add(pos, dirs[math.floor(node.param2/4)+1]) local dir = minetest.facedir_to_dir(node.param2) local nnode=minetest.get_node(nnodepos) local meta = minetest.get_meta(pos) @@ -173,103 +270,194 @@ minetest.register_node("technic:frame_motor",{ end }) -function add_table(table,toadd) - local i=1 - while true do - o=table[i] - if o==toadd then return end - if o==nil then break end - i=i+1 + + +-- Templates +local function template_connected(pos,c) + 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 nodename=minetest.get_node(pos1).name + if not(pos_in_list(c,pos1)) and nodename=="technic:template" then + c[#(c)+1]=pos1 + template_connected(pos1,c) + end end - table[i]=toadd end -function move_nodes_vect(poslist,vect,must_not_move,owner) - if minetest.is_protected then - for _,pos in ipairs(poslist) do - local npos=frames.addVect(pos,vect) - if minetest.is_protected(pos, owner) or minetest.is_protected(npos, owner) then - return - end +local function get_templates(pos) + c={pos} + template_connected(pos,c) + return c +end + +local function save_node(pos) + local node = minetest.get_node(pos) + if node.name == "air" or node.name == "technic:template" then return end + local meta = minetest.get_meta(pos) + local meta0 = meta:to_table() + for _, list in pairs(meta0.inventory) do + for key, stack in pairs(list) do + list[key] = stack:to_string() end end - for _,pos in ipairs(poslist) do - local npos=frames.addVect(pos,vect) - local name = minetest.env:get_node(npos).name - if (name~="air" and minetest.registered_nodes[name].liquidtype=="none") and not(pos_in_list(poslist,npos)) then - return + node.meta = meta0 + return node +end + +local function restore_node(pos, node) + minetest.set_node(pos, node) + local meta = minetest.get_meta(pos) + for _, list in pairs(node.meta.inventory) do + for key, stack in pairs(list) do + list[key] = ItemStack(stack) end - if pos.x==must_not_move.x and pos.y==must_not_move.y and pos.z==must_not_move.z then - return - end end - nodelist={} - for _,pos in ipairs(poslist) do - local node=minetest.env:get_node(pos) - local meta=minetest.env:get_meta(pos):to_table() - nodelist[#(nodelist)+1]={pos=pos,node=node,meta=meta} - end - objects={} - for _,pos in ipairs(poslist) do - for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do - add_table(objects,object) + meta:from_table(node.meta) +end + +local function expand_template(pos) + local meta = minetest.get_meta(pos) + local c = meta:get_string("connected") + if c == "" then return end + meta:set_string("connected", "") + c = minetest.deserialize(c) + 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 saved_node = save_node(pos1) + minetest.set_node(pos1, {name="technic:template"}) + local meta1 = minetest.get_meta(pos1) + if saved_node ~= nil then + meta1:set_string("saved_node", minetest.serialize(saved_node)) + else + meta1:set_string("saved_node", "") + end end end - for _,obj in ipairs(objects) do - obj:setpos(frames.addVect(obj:getpos(),vect)) - le=obj:get_luaentity() - if le and le.name == "pipeworks:tubed_item" then - le.start_pos=frames.addVect(le.start_pos,vect) +end + +minetest.register_node("technic:template",{ + description = "Template", + tiles = {"technic_mv_cable.png"}, + groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, + on_destruct = function(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, + on_punch = function(pos,node,puncher) + hacky_swap_node(pos, "technic:template_disabled") end - for _,n in ipairs(nodelist) do - local npos=frames.addVect(n.pos,vect) - minetest.env:set_node(npos,n.node) - local meta=minetest.env:get_meta(npos) - meta:from_table(n.meta) - for __,pos in ipairs(poslist) do - if npos.x==pos.x and npos.y==pos.y and npos.z==pos.z then - table.remove(poslist, __) - break - end +}) + +minetest.register_node("technic:template_disabled",{ + description = "Template", + tiles = {"technic_hv_cable.png"}, + groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, + on_destruct = function(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, + on_punch = function(pos,node,puncher) + hacky_swap_node(pos, "technic:template") end - for __,pos in ipairs(poslist) do - minetest.env:remove_node(pos) +}) + +minetest.register_craftitem("technic:template_replacer",{ + description = "Template (replacing)", + inventory_image = "technic_template_replacer.png", + on_place = function(itemstack, placer, pointed_thing) + local p = pointed_thing.under + if minetest.is_protected and minetest.is_protected(p, placer:get_player_name()) then + return nil + end + local node = minetest.get_node(p) + if node.name == "technic:template" then return end + local saved_node = save_node(p) + minetest.set_node(p, {name="technic:template"}) + itemstack:take_item() + if saved_node ~= nil then + local meta = minetest.get_meta(p) + meta:set_string("saved_node", minetest.serialize(saved_node)) + end + return itemstack end -end +}) -function get_connected_nodes(pos) - c={pos} - local nodename=minetest.env:get_node(pos).name - connected(pos,c,minetest.registered_nodes[nodename].frame_connect_all(pos)) - return c -end +minetest.register_tool("technic:template_tool",{ + description = "Template tool", + inventory_image = "technic_template_tool.png", + on_use = function(itemstack, puncher, pointed_thing) + local pos = pointed_thing.under + if minetest.is_protected and minetest.is_protected(pos, placer:get_player_name()) then + return nil + end + local node = minetest.get_node(pos) + if node.name ~= "technic:template" then return end + local meta = minetest.get_meta(pos) + local c2 = meta:get_string("connected") + if c2 ~= "" then + expand_template(pos) + return + 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 +}) -function frames.addVect(pos,vect) - return {x=pos.x+vect.x,y=pos.y+vect.y,z=pos.z+vect.z} -end -function pos_in_list(l,pos) - 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 - end - return false -end -function connected(pos,c,adj) +-- Template motor +local function get_template_nodes(pos) + local meta = minetest.get_meta(pos) + local connected = meta:get_string("connected") + if connected == "" then return {} end + local adj = minetest.deserialize(connected) + local c = {} for _,vect in ipairs(adj) do - local pos1=frames.addVect(pos,vect) + local pos1=vector.add(pos,vect) local nodename=minetest.env:get_node(pos1).name - if not(pos_in_list(c,pos1)) and nodename~="air" and - (minetest.registered_nodes[nodename].frames_can_connect==nil or - minetest.registered_nodes[nodename].frames_can_connect(pos1,vect)) then + if not(pos_in_list(c,pos1)) and nodename~="air" then c[#(c)+1]=pos1 - if minetest.registered_nodes[nodename].frame==1 then - local adj=minetest.registered_nodes[nodename].frame_connect_all(pos1) - connected(pos1,c,adj) - end end end + return c +end + +local function template_motor_on(pos, node) + local dirs = {{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}} + local nnodepos = vector.add(pos, dirs[math.floor(node.param2/4)+1]) + local dir = minetest.facedir_to_dir(node.param2) + local nnode=minetest.get_node(nnodepos) + --if minetest.registered_nodes[nnode.name].template==1 then + if nnode.name == "technic:template" then + local connected_nodes=get_template_nodes(nnodepos) + move_nodes_vect(connected_nodes,dir,pos) + end end +minetest.register_node("technic:template_motor",{ + description = "Template motor", + tiles = {"pipeworks_filter_top.png^[transformR90", "technic_lv_cable.png", "technic_lv_cable.png", + "technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png"}, + groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,mesecon=2}, + paramtype2 = "facedir", + mesecons={effector={action_on=template_motor_on}}, +}) -- 2.30.2