diff --git a/technic/machines/other/frames.lua b/technic/machines/other/frames.lua index c83cceb..c7cb16e 100644 --- a/technic/machines/other/frames.lua +++ b/technic/machines/other/frames.lua @@ -5,79 +5,77 @@ frames = {} local infinite_stacks = minetest.setting_getbool("creative_mode") and minetest.get_modpath("unified_inventory") == nil +-- localize these functions from vector_extras with small names because they work fairly fast +local get = vector.get_data_from_pos +local set = vector.set_data_to_pos +local remove = vector.remove_data_from_pos + local frames_pos = {} +local function get_frame(pos) + return get(frames_pos, pos.z,pos.y,pos.x) +end + +local function set_frame(pos, data) + return set(frames_pos, pos.z,pos.y,pos.x, data) +end + +local function remove_frame(pos) + remove(frames_pos, pos.z,pos.y,pos.x) +end + + -- Helpers -local function get_face(pos,ppos,pvect) +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 local y_int=ppos.y+t*pvect.y local z_int=ppos.z+t*pvect.z - if y_int>-0.45 and y_int<0.45 and z_int>-0.45 and z_int<0.45 then return 1 end + if y_int>-0.45 and y_int<0.45 and z_int>-0.45 and z_int<0.45 then return 1 end elseif pvect.x<0 then local t=(0.5-ppos.x)/pvect.x local y_int=ppos.y+t*pvect.y local z_int=ppos.z+t*pvect.z - if y_int>-0.45 and y_int<0.45 and z_int>-0.45 and z_int<0.45 then return 2 end + if y_int>-0.45 and y_int<0.45 and z_int>-0.45 and z_int<0.45 then return 2 end end if pvect.y>0 then local t=(-0.5-ppos.y)/pvect.y local x_int=ppos.x+t*pvect.x local z_int=ppos.z+t*pvect.z - if x_int>-0.45 and x_int<0.45 and z_int>-0.45 and z_int<0.45 then return 3 end + if x_int>-0.45 and x_int<0.45 and z_int>-0.45 and z_int<0.45 then return 3 end elseif pvect.y<0 then local t=(0.5-ppos.y)/pvect.y local x_int=ppos.x+t*pvect.x local z_int=ppos.z+t*pvect.z - if x_int>-0.45 and x_int<0.45 and z_int>-0.45 and z_int<0.45 then return 4 end + if x_int>-0.45 and x_int<0.45 and z_int>-0.45 and z_int<0.45 then return 4 end end if pvect.z>0 then local t=(-0.5-ppos.z)/pvect.z local x_int=ppos.x+t*pvect.x local y_int=ppos.y+t*pvect.y - if x_int>-0.45 and x_int<0.45 and y_int>-0.45 and y_int<0.45 then return 5 end + if x_int>-0.45 and x_int<0.45 and y_int>-0.45 and y_int<0.45 then return 5 end elseif pvect.z<0 then local t=(0.5-ppos.z)/pvect.z local x_int=ppos.x+t*pvect.x local y_int=ppos.y+t*pvect.y - if x_int>-0.45 and x_int<0.45 and y_int>-0.45 and y_int<0.45 then return 6 end + if x_int>-0.45 and x_int<0.45 and y_int>-0.45 and y_int<0.45 then return 6 end end end -local 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) 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 vector.equals(pos, p) then + return true + end end return false end local function table_empty(table) - for _, __ in pairs(table) do - return false - end - return true + return next(table) == nil end local function add_table(table,toadd) @@ -91,19 +89,31 @@ local function add_table(table,toadd) table[i] = toadd end -local function move_nodes_vect(poslist,vect,must_not_move,owner) - if minetest.is_protected then - for _,pos in ipairs(poslist) do - local npos=vector.add(pos,vect) - if minetest.is_protected(pos, owner) or minetest.is_protected(npos, owner) then +local function move_nodes_vect(poslist, vect, must_not_move, owner) + local dones = {} + for _,pos in ipairs(poslist) do + if not get(dones, pos.z,pos.y,pos.x) then + if minetest.is_protected(pos, owner) then return end + set(dones, pos.z,pos.y,pos.x, true) + end + local pos = vector.add(pos, vect) + if not get(dones, pos.z,pos.y,pos.x) then + if minetest.is_protected(pos, owner) then + return + end + set(dones, pos.z,pos.y,pos.x, true) end end + dones = nil for _,pos in ipairs(poslist) do - local npos=vector.add(pos,vect) + local npos = vector.add(pos,vect) local name = minetest.get_node(npos).name - if ((name~="air" and minetest.registered_nodes[name].liquidtype=="none") or frames_pos[pos_to_string(npos)]) and not(pos_in_list(poslist,npos)) then + if ( + (name ~= "air" and minetest.registered_nodes[name].liquidtype == "none") + or get_frame(npos) + ) 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 @@ -112,15 +122,14 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner) end local nodelist = {} for _, pos in ipairs(poslist) do - local node = minetest.get_node(pos) - local meta = minetest.get_meta(pos):to_table() - nodelist[#(nodelist)+1] = {oldpos = pos, pos = vector.add(pos, vect), node = node, meta = meta} + nodelist[#(nodelist)+1] = {oldpos=pos, pos=vector.add(pos, vect), node=minetest.get_node(pos), meta=minetest.get_meta(pos):to_table()} end local objects = {} for _, pos in ipairs(poslist) do for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do local entity = object:get_luaentity() - if not entity or not mesecon.is_mvps_unmov(entity.name) then + if not entity + or not mesecon.is_mvps_unmov(entity.name) then add_table(objects, object) end end @@ -131,16 +140,15 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner) for _,n in ipairs(nodelist) do local npos = n.pos minetest.set_node(npos, n.node) - local meta = minetest.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, __) + minetest.get_meta(npos):from_table(n.meta) + for i,pos in ipairs(poslist) do + if vector.equals(nops, pos) then + table.remove(poslist, i) break end end end - for __, pos in ipairs(poslist) do + for _, pos in ipairs(poslist) do minetest.remove_node(pos) end for _, callback in ipairs(mesecon.on_mvps_move) do @@ -149,11 +157,117 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner) end local function is_supported_node(name) - return ((string.find(name, "tube") ~= nil) and (string.find(name, "pipeworks") ~= nil)) + return string.find(name, "tube") ~= nil and string.find(name, "pipeworks") ~= nil end -- Frames + +local function connect_frame(nodename) + l2={} + l1={{x=-1,y=0,z=0},{x=1,y=0,z=0},{x=0,y=-1,z=0},{x=0,y=1,z=0},{x=0,y=0,z=-1},{x=0,y=0,z=1}} + for i,dir in ipairs(l1) do + if string.sub(nodename,-7+i,-7+i) == "1" then + l2[#(l2)+1] = dir + end + end + return l2 +end + +local function punch_frame(pos, node, puncher) + local pface = get_face(pos, puncher:getpos(), puncher:get_look_dir()) + if not pface then + return + end + local nodename = node.name + local newstate = tostring(1-tonumber(string.sub(nodename,-7+pface,-7+pface))) + if pface <= 5 then + nodename = string.sub(nodename,1,-7+pface-1)..newstate..string.sub(nodename,-7+pface+1) + else + nodename = string.sub(nodename,1,-2)..newstate + end + node.name = nodename + minetest.set_node(pos,node) +end + +local function place_frame(itemstack, placer, pointed_thing) + local pos = pointed_thing.above + if not pos then + return + end + local pname = placer:get_player_name() + local nodename = itemstack:get_name() + if minetest.is_protected(pos, pname) then + minetest.log("action", pname + .. " tried to place " .. nodename + .. " at protected position " + .. minetest.pos_to_string(pos)) + minetest.record_protection_violation(pos, pname) + return itemstack + end + local node = minetest.get_node(pos) + if node.name == "air" then + minetest.set_node(pos, {name = nodename}) + elseif is_supported_node(node.name) then + obj = minetest.add_entity(pos, "technic:frame_entity") + obj:get_luaentity():set_node({name=nodename}) + end + if not infinite_stacks then + itemstack:take_item() + end + return itemstack +end + +local function rightclick_frame(pos, node, placer, itemstack, pointed_thing) + local nodename = itemstack:get_name() + if is_supported_node(nodename) then + local pname = placer:get_player_name() + if minetest.is_protected(pos, pname) then + minetest.log("action", pname + .. " tried to rightglick with " .. nodename + .. " at protected position " + .. minetest.pos_to_string(pos)) + minetest.record_protection_violation(pos, pname) + return itemstack + end + + minetest.set_node(pos, {name = nodename}) + + local take_item = true + local def = minetest.registered_items[nodename] + -- Run callback + if def.after_place_node then + if def.after_place_node(vector.new(pos), placer, itemstack) then + take_item = false + end + end + + -- Run script hook + for _, callback in ipairs(minetest.registered_on_placenodes) do + local newnode_copy = {name=def.name, param1=0, param2=0} + local oldnode_copy = {name="air", param1=0, param2=0} + if callback(vector.new(pos), newnode_copy, placer, oldnode_copy, itemstack) then + take_item = false + end + end + + if take_item then + itemstack:take_item() + end + + obj = minetest.add_entity(pos, "technic:frame_entity") + obj:get_luaentity():set_node({name=node.name}) + + return itemstack + else + --local pointed_thing = {type = "node", under = pos} + if pointed_thing then + minetest.item_place_node(itemstack, placer, pointed_thing) + end + end +end + + for xm=0,1 do for xp=0,1 do for ym=0,1 do @@ -161,27 +275,27 @@ for yp=0,1 do for zm=0,1 do for zp=0,1 do -local a=8/16 -local b=7/16 -local nodeboxes= { - { -a, -a, -a, -b, a, -b }, - { -a, -a, b, -b, a, a }, - { b, -a, b, a, a, a }, - { b, -a, -a, a, a, -b }, + local a=8/16 + local b=7/16 + local nodeboxes= { + { -a, -a, -a, -b, a, -b }, + { -a, -a, b, -b, a, a }, + { b, -a, b, a, a, a }, + { b, -a, -a, a, a, -b }, - { -b, b, -a, b, a, -b }, - { -b, -a, -a, b, -b, -b }, - - { -b, b, b, b, a, a }, - { -b, -a, b, b, -b, a }, + { -b, b, -a, b, a, -b }, + { -b, -a, -a, b, -b, -b }, - { b, b, -b, a, a, b }, - { b, -a, -b, a, -b, b }, + { -b, b, b, b, a, a }, + { -b, -a, b, b, -b, a }, - { -a, b, -b, -b, a, b }, - { -a, -a, -b, -b, -b, b }, + { b, b, -b, a, a, b }, + { b, -a, -b, a, -b, b }, + + { -a, b, -b, -b, a, b }, + { -a, -a, -b, -b, -b, b }, } - + if yp==0 then table.insert(nodeboxes, {-b,b,-b, b,a,b}) end @@ -200,129 +314,39 @@ local nodeboxes= { if zm==0 then table.insert(nodeboxes, {-b,-b,-a, b,b,-b}) end - - local nameext=tostring(xm)..tostring(xp)..tostring(ym)..tostring(yp)..tostring(zm)..tostring(zp) + + local nameext = tostring(xm)..tostring(xp)..tostring(ym)..tostring(yp)..tostring(zm)..tostring(zp) local groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2} - if nameext~="111111" then groups.not_in_creative_inventory=1 end - + if nameext ~= "111111" then + groups.not_in_creative_inventory = 1 + end + minetest.register_node("technic:frame_"..nameext,{ description = S("Frame"), tiles = {"technic_frame.png"}, - groups=groups, + paramtype = "light", + sunlight_propagates = true, + frame = 1, + drop = "technic:frame_111111", + selection_box = true, drawtype = "nodebox", node_box = { type = "fixed", - fixed=nodeboxes, + fixed = nodeboxes, }, - selection_box = { - type="fixed", - fixed={-0.5,-0.5,-0.5,0.5,0.5,0.5} - }, - paramtype = "light", - frame=1, - drop="technic:frame_111111", - sunlight_propagates = true, - frame_connect_all=function(nodename) - l2={} - l1={{x=-1,y=0,z=0},{x=1,y=0,z=0},{x=0,y=-1,z=0},{x=0,y=1,z=0},{x=0,y=0,z=-1},{x=0,y=0,z=1}} - for i,dir in ipairs(l1) do - if string.sub(nodename,-7+i,-7+i)=="1" then - l2[#(l2)+1]=dir - end - end - return l2 + groups = groups, + frame_connect_all = function(nodename) + return connect_frame(nodename) end, - on_punch=function(pos,node,puncher) - local ppos=puncher:getpos() - local pvect=puncher:get_look_dir() - local pface=get_face(pos,ppos,pvect) - if pface==nil then return end - local nodename=node.name - local newstate=tostring(1-tonumber(string.sub(nodename,-7+pface,-7+pface))) - if pface<=5 then - nodename=string.sub(nodename,1,-7+pface-1)..newstate..string.sub(nodename,-7+pface+1) - else - nodename=string.sub(nodename,1,-2)..newstate - end - node.name=nodename - minetest.set_node(pos,node) + on_punch = function(...) + return punch_frame(...) end, - on_place = function(itemstack, placer, pointed_thing) - local pos = pointed_thing.above - if minetest.is_protected(pos, placer:get_player_name()) then - minetest.log("action", placer:get_player_name() - .. " tried to place " .. itemstack:get_name() - .. " at protected position " - .. minetest.pos_to_string(pos)) - minetest.record_protection_violation(pos, placer:get_player_name()) - return itemstack - end - if pos == nil then return end - local node = minetest.get_node(pos) - if node.name ~= "air" then - if is_supported_node(node.name) then - obj = minetest.add_entity(pos, "technic:frame_entity") - obj:get_luaentity():set_node({name=itemstack:get_name()}) - end - else - minetest.set_node(pos, {name = itemstack:get_name()}) - end - if not infinite_stacks then - itemstack:take_item() - end - return itemstack + on_place = function(...) + return place_frame(...) end, - on_rightclick = function(pos, node, placer, itemstack, pointed_thing) - if is_supported_node(itemstack:get_name()) then - if minetest.is_protected(pos, placer:get_player_name()) then - minetest.log("action", placer:get_player_name() - .. " tried to place " .. itemstack:get_name() - .. " at protected position " - .. minetest.pos_to_string(pos)) - minetest.record_protection_violation(pos, placer:get_player_name()) - return itemstack - end - - minetest.set_node(pos, {name = itemstack:get_name()}) - - local take_item = true - local def = minetest.registered_items[itemstack:get_name()] - -- Run callback - if def.after_place_node then - -- Copy place_to because callback can modify it - local pos_copy = {x=pos.x, y=pos.y, z=pos.z} - if def.after_place_node(pos_copy, placer, itemstack) then - take_item = false - end - end - - -- Run script hook - local _, callback - for _, callback in ipairs(minetest.registered_on_placenodes) do - -- Copy pos and node because callback can modify them - local pos_copy = {x=pos.x, y=pos.y, z=pos.z} - local newnode_copy = {name=def.name, param1=0, param2=0} - local oldnode_copy = {name="air", param1=0, param2=0} - if callback(pos_copy, newnode_copy, placer, oldnode_copy, itemstack) then - take_item = false - end - end - - if take_item then - itemstack:take_item() - end - - obj = minetest.add_entity(pos, "technic:frame_entity") - obj:get_luaentity():set_node({name=node.name}) - - return itemstack - else - --local pointed_thing = {type = "node", under = pos} - if pointed_thing then - minetest.item_place_node(itemstack, placer, pointed_thing) - end - end + on_rightclick = function(...) + return rightclick_frame(...) end, }) @@ -346,26 +370,11 @@ minetest.register_entity("technic:frame_entity", { set_node = function(self, node) self.node = node - local pos = self.object:getpos() - pos = {x = math.floor(pos.x+0.5), y = math.floor(pos.y+0.5), z = math.floor(pos.z+0.5)} - frames_pos[pos_to_string(pos)] = node.name - local stack = ItemStack(node.name) - local itemtable = stack:to_table() - local itemname = nil - if itemtable then - itemname = stack:to_table().name - end - local item_texture = nil - local item_type = "" - if minetest.registered_items[itemname] then - item_texture = minetest.registered_items[itemname].inventory_image - item_type = minetest.registered_items[itemname].type - end - prop = { + set_frame(vector.round(self.object:getpos()), node.name) + self.object:set_properties({ is_visible = true, textures = {node.name}, - } - self.object:set_properties(prop) + }) end, get_staticdata = function(self) @@ -376,15 +385,14 @@ minetest.register_entity("technic:frame_entity", { self.object:set_armor_groups({immortal=1}) self:set_node({name=staticdata}) end, - + dig = function(self) minetest.handle_node_drops(self.object:getpos(), {ItemStack("technic:frame_111111")}, self.last_puncher) - local pos = self.object:getpos() - pos = {x = math.floor(pos.x+0.5), y = math.floor(pos.y+0.5), z = math.floor(pos.z+0.5)} - frames_pos[pos_to_string(pos)] = nil + local pos = vector.round(self.object:getpos()) + remove_frame(pos) self.object:remove() end, - + on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir) local pos = self.object:getpos() if self.damage_object == nil then @@ -397,10 +405,10 @@ minetest.register_entity("technic:frame_entity", { self.damage_object:get_luaentity().remaining_time = 0.25 end self.last_puncher = puncher - local ppos = puncher:getpos() - local pvect = puncher:get_look_dir() - local pface = get_face(pos,ppos,pvect) - if pface == nil then return end + local pface = get_face(pos, puncher:getpos(), puncher:get_look_dir()) + if not pface then + return + end local nodename = self.node.name local newstate = tostring(1-tonumber(string.sub(nodename, -7+pface, -7+pface))) if pface <= 5 then @@ -411,23 +419,25 @@ minetest.register_entity("technic:frame_entity", { self.node.name = nodename self:set_node(self.node) end, - + on_rightclick = function(self, clicker) local pos = self.object:getpos() - local ppos = clicker:getpos() - local pvect = clicker:get_look_dir() - local pface = get_face(pos, ppos, pvect) - if pface == nil then return end - local pos_under = {x = math.floor(pos.x+0.5), y = math.floor(pos.y+0.5), z = math.floor(pos.z+0.5)} - local pos_above = {x = pos_under.x, y = pos_under.y, z = pos_under.z} + local pface = get_face(pos, clicker:getpos(), clicker:get_look_dir()) + if not pface then + return + end + pos = vector.round(pos) + local pos_above = vector.new(pos) local index = ({"x", "y", "z"})[math.floor((pface+1)/2)] pos_above[index] = pos_above[index] + 2*((pface+1)%2) - 1 - local pointed_thing = {type = "node", under = pos_under, above = pos_above} + local pointed_thing = {type = "node", under = pos, above = pos_above} + local itemstack = clicker:get_wielded_item() local itemdef = minetest.registered_items[itemstack:get_name()] - if itemdef ~= nil then + if itemdef then itemdef.on_place(itemstack, clicker, pointed_thing) end + clicker:set_wielded_item(itemstack) end, }) @@ -441,27 +451,34 @@ minetest.register_entity("technic:damage_entity", { physical = false, }, on_step = function(self, dtime) - if self.remaining_time == nil then + if not self.remaining_time then self.object:remove() self.frame_object.damage_object = nil + return end self.remaining_time = self.remaining_time - dtime if self.remaining_time < 0 then self.object:remove() self.frame_object.damage_object = nil + return end self.texture_change_time = self.texture_change_time - dtime - if self.texture_change_time < 0 then - self.texture_change_time = self.texture_change_time + 0.15 - self.texture_index = self.texture_index + 1 - if self.texture_index == 5 then - self.object:remove() - self.frame_object.damage_object = nil - self.frame_object:dig() - end - local ct = "crack_anylength.png^[verticalframe:5:"..self.texture_index - self.object:set_properties({textures = {ct, ct, ct, ct, ct, ct}}) + if self.texture_change_time >= 0 then + return end + local pos = self.object:getpos() + self.texture_index = self.texture_index + 1 + if self.texture_index == 5 then + self.object:remove() + self.frame_object.damage_object = nil + self.frame_object:dig() + minetest.sound_play("default_dug_node", {pos = pos}) + return + end + minetest.sound_play("default_dig_choppy", {pos = pos}) + self.texture_change_time = self.texture_change_time + 0.15 + local ct = "crack_anylength.png^[verticalframe:5:"..self.texture_index + self.object:set_properties({textures = {ct, ct, ct, ct, ct, ct}}) end, }) @@ -470,53 +487,59 @@ mesecon.register_mvps_unmov("technic:damage_entity") mesecon.register_on_mvps_move(function(moved_nodes) local to_move = {} for _, n in ipairs(moved_nodes) do - if frames_pos[pos_to_string(n.oldpos)] ~= nil then - to_move[#to_move+1] = {pos = n.pos, oldpos = n.oldpos, name = frames_pos[pos_to_string(n.oldpos)]} - frames_pos[pos_to_string(n.oldpos)] = nil + local name = get_frame(n.oldpos) + if name then + to_move[#to_move+1] = {pos = n.pos, oldpos = n.oldpos, name = name} + remove_frame(n.oldpos) end end - if #to_move > 0 then - for _, t in ipairs(to_move) do - frames_pos[pos_to_string(t.pos)] = t.name - local objects = minetest.get_objects_inside_radius(t.oldpos, 0.1) - for _, obj in ipairs(objects) do - local entity = obj:get_luaentity() - if entity and (entity.name == "technic:frame_entity" or entity.name == "technic:damage_entity") then - obj:setpos(t.pos) - end + if not next(to_move) then + return + end + for _, t in ipairs(to_move) do + set_frame(t.pos, t.name) + local objects = minetest.get_objects_inside_radius(t.oldpos, 0.1) + for _, obj in ipairs(objects) do + local entity = obj:get_luaentity() + if entity + and (entity.name == "technic:frame_entity" or entity.name == "technic:damage_entity") then + obj:setpos(t.pos) end end end end) minetest.register_on_dignode(function(pos, node) - if frames_pos[pos_to_string(pos)] ~= nil then - minetest.set_node(pos, {name = frames_pos[pos_to_string(pos)]}) - frames_pos[pos_to_string(pos)] = nil - local objects = minetest.get_objects_inside_radius(pos, 0.1) - for _, obj in ipairs(objects) do - local entity = obj:get_luaentity() - if entity and (entity.name == "technic:frame_entity" or entity.name == "technic:damage_entity") then - obj:remove() - end + local name = get_frame(pos) + if not name then + return + end + minetest.set_node(pos, {name = name}) + remove_frame(pos) + local objects = minetest.get_objects_inside_radius(pos, 0.1) + for _, obj in ipairs(objects) do + local entity = obj:get_luaentity() + if entity + and (entity.name == "technic:frame_entity" or entity.name == "technic:damage_entity") then + obj:remove() end end end) -- Frame motor -local function connected(pos,c,adj) +local function connected(pos, c, adj) for _,vect in ipairs(adj) do - local pos1=vector.add(pos,vect) - local nodename=minetest.get_node(pos1).name - if frames_pos[pos_to_string(pos1)] then - nodename = frames_pos[pos_to_string(pos1)] - end - 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(nodename) + local pos1 = vector.add(pos,vect) + local nodename = get_frame(pos1) or minetest.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(nodename) connected(pos1,c,adj) end end @@ -525,30 +548,22 @@ end local function get_connected_nodes(pos) c={pos} - local nodename=minetest.get_node(pos).name - if frames_pos[pos_to_string(pos)] then - nodename = frames_pos[pos_to_string(pos)] - end + local nodename = get_frame(pos) or minetest.get_node(pos).name connected(pos,c,minetest.registered_nodes[nodename].frame_connect_all(nodename)) 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 = 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 frames_pos[pos_to_string(nnodepos)] then - nnode.name = frames_pos[pos_to_string(nnodepos)] - end local meta = minetest.get_meta(pos) if meta:get_int("last_moved") == minetest.get_gametime() then return end - local owner = meta:get_string("owner") - if minetest.registered_nodes[nnode.name].frame==1 then - local connected_nodes=get_connected_nodes(nnodepos) - move_nodes_vect(connected_nodes,dir,pos,owner) + 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 name = get_frame(nnodepos) or minetest.get_node(nnodepos).name + local dir = minetest.facedir_to_dir(node.param2) + if minetest.registered_nodes[name].frame == 1 then + move_nodes_vect(get_connected_nodes(nnodepos), dir, pos, meta:get_string("owner")) end minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime()) end @@ -560,13 +575,11 @@ minetest.register_node("technic:frame_motor",{ groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,mesecon=2}, paramtype2 = "facedir", mesecons={effector={action_on=frame_motor_on}}, - after_place_node = function(pos, placer, itemstack) - local meta = minetest.get_meta(pos) - meta:set_string("owner", placer:get_player_name()) + after_place_node = function(pos, placer) + minetest.get_meta(pos):set_string("owner", placer:get_player_name()) end, - frames_can_connect=function(pos,dir) - local node = minetest.get_node(pos) - local dir2 = ({{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}})[math.floor(node.param2/4)+1] + frames_can_connect = function(pos, dir) + local dir2 = ({{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}})[math.floor(minetest.get_node(pos).param2/4)+1] return dir2.x~=-dir.x or dir2.y~=-dir.y or dir2.z~=-dir.z end }) @@ -578,7 +591,8 @@ 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 local pos1=vector.add(pos,vect) local nodename=minetest.get_node(pos1).name - if not(pos_in_list(c,pos1)) and (nodename=="technic:template" or nodename == "technic:template_connector")then + if not(pos_in_list(c,pos1)) + and (nodename=="technic:template" or nodename == "technic:template_connector")then local meta = minetest.get_meta(pos1) if meta:get_string("connected") == "" then c[#(c)+1]=pos1 @@ -664,6 +678,13 @@ local function expand_template(pos) end 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 compress_templates(pos) local templates, connectors = get_templates(pos) if #connectors == 0 then @@ -689,7 +710,7 @@ local function compress_templates(pos) 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"}) @@ -697,6 +718,18 @@ local function compress_templates(pos) end end +local 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_from_string(str) + local l = lines(str) + return {x = tonumber(l[1]), y = tonumber(l[2]), z = tonumber(l[3])} +end + local function template_drops(pos, node, oldmeta, digger) local c = oldmeta.fields.connected local cc = oldmeta.fields.connectors_connected @@ -818,7 +851,7 @@ minetest.register_tool("technic:template_tool",{ else compress_templates(pos) end - + end }) diff --git a/technic/machines/other/init.lua b/technic/machines/other/init.lua index f4a82f0..95959d0 100644 --- a/technic/machines/other/init.lua +++ b/technic/machines/other/init.lua @@ -4,7 +4,10 @@ local path = technic.modpath.."/machines/other" dofile(path.."/injector.lua") dofile(path.."/constructor.lua") -if technic.config:get_bool("enable_frames") and minetest.get_modpath("mesecons_mvps") ~= nil then +if technic.config:get_bool("enable_frames") +and rawget(_G, "mesecon") +and mesecon.register_mvps_unmov +and vector.set_data_to_pos then dofile(path.."/frames.lua") end