Clean file frames.lua to make more readable

- "#(array)" replaced with "#array"
- "(a and b) or c" replaced with "a and b or c"
- Cleaned some other places with unnecessary parenthesis
- "a,b" replaced with "a, b"
- "a+b" replaced with "a + b" (and for all other binops)
- "{a, b, c}" replaced with "{ a, b, c }"
- "-n + a" replaced with "a - n"
- Removed trailing whitespace
- Blank lines added in some very dense places
- Very long lines broken into shorter lines
- Use modern functions like vector.new and vector.round
- Align with spaces instead of tabs
This commit is contained in:
Elias Åström 2018-08-16 13:47:43 +02:00 committed by SmallJoker
parent d74c250a40
commit 488f80d950
1 changed files with 365 additions and 276 deletions

View File

@ -1,9 +1,9 @@
local S = technic.getter local S = technic.getter
frames = {} frames = {}
local infinite_stacks = minetest.settings:get_bool("creative_mode") and minetest.get_modpath("unified_inventory") == nil local infinite_stacks = minetest.settings:get_bool("creative_mode")
and minetest.get_modpath("unified_inventory") == nil
local frames_pos = {} local frames_pos = {}
@ -12,45 +12,60 @@ local frames_pos = {}
local function get_face(pos, ppos, pvect) local function get_face(pos, ppos, pvect)
-- Raytracer to get which face has been clicked -- 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 } ppos = { x = ppos.x - pos.x, y = ppos.y - pos.y + 1.5, z = ppos.z - pos.z }
if pvect.x > 0 then if pvect.x > 0 then
local t = (-0.5 - ppos.x) / pvect.x local t = (-0.5 - ppos.x) / pvect.x
local y_int = ppos.y + t * pvect.y local y_int = ppos.y + t * pvect.y
local z_int = ppos.z + t * pvect.z 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 elseif pvect.x < 0 then
local t = (0.5 - ppos.x) / pvect.x local t = (0.5 - ppos.x) / pvect.x
local y_int = ppos.y + t * pvect.y local y_int = ppos.y + t * pvect.y
local z_int = ppos.z + t * pvect.z 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
end
if pvect.y > 0 then if pvect.y > 0 then
local t = (-0.5 - ppos.y) / pvect.y local t = (-0.5 - ppos.y) / pvect.y
local x_int = ppos.x + t * pvect.x local x_int = ppos.x + t * pvect.x
local z_int = ppos.z + t * pvect.z 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 elseif pvect.y < 0 then
local t = (0.5 - ppos.y) / pvect.y local t = (0.5 - ppos.y) / pvect.y
local x_int = ppos.x + t * pvect.x local x_int = ppos.x + t * pvect.x
local z_int = ppos.z + t * pvect.z 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
end
if pvect.z > 0 then if pvect.z > 0 then
local t = (-0.5 - ppos.z) / pvect.z local t = (-0.5 - ppos.z) / pvect.z
local x_int = ppos.x + t * pvect.x local x_int = ppos.x + t * pvect.x
local y_int = ppos.y + t * pvect.y 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 elseif pvect.z < 0 then
local t = (0.5 - ppos.z) / pvect.z local t = (0.5 - ppos.z) / pvect.z
local x_int = ppos.x + t * pvect.x local x_int = ppos.x + t * pvect.x
local y_int = ppos.y + t * pvect.y 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
end end
local function lines(str) local function lines(str)
local t = {} local t = {}
local function helper(line) table.insert(t, line) return "" end local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)\r?\n", helper))) helper(str:gsub("(.-)\r?\n", helper))
return t return t
end end
@ -68,7 +83,9 @@ 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
end end
return false return false
end end
@ -100,16 +117,16 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner)
end end
end end
end end
for _, pos in ipairs(poslist) do 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 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
frames_pos[pos_to_string(npos)]) and not pos_in_list(poslist, npos) then
return return
end 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 end
local nodelist = {} local nodelist = {}
for _, pos in ipairs(poslist) do for _, pos in ipairs(poslist) do
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -126,6 +143,7 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner)
} }
} }
end end
local objects = {} local objects = {}
for _, pos in ipairs(poslist) do for _, pos in ipairs(poslist) do
for _, object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do for _, object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
@ -135,9 +153,11 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner)
end end
end end
end end
for _, obj in ipairs(objects) do for _, obj in ipairs(objects) do
obj:setpos(vector.add(obj:getpos(), vect)) obj:setpos(vector.add(obj:getpos(), vect))
end end
for _, n in ipairs(nodelist) do for _, n in ipairs(nodelist) do
local npos = n.pos local npos = n.pos
minetest.set_node(npos, n.node) minetest.set_node(npos, n.node)
@ -154,19 +174,20 @@ local function move_nodes_vect(poslist,vect,must_not_move,owner)
end end
end end
end end
for __, pos in ipairs(poslist) do for __, pos in ipairs(poslist) do
minetest.remove_node(pos) minetest.remove_node(pos)
end end
for _, callback in ipairs(mesecon.on_mvps_move) do for _, callback in ipairs(mesecon.on_mvps_move) do
callback(nodelist) callback(nodelist)
end end
end end
local function is_supported_node(name) local function is_supported_node(name)
return ((string.find(name, "tube") ~= nil) and (string.find(name, "pipeworks") ~= nil)) return string.find(name, "tube") and string.find(name, "pipeworks")
end end
-- Frames -- Frames
for xm = 0, 1 do for xm = 0, 1 do
for xp = 0, 1 do for xp = 0, 1 do
@ -180,6 +201,7 @@ local b=7/16
local nodeboxes = { local nodeboxes = {
{ -a, -a, -a, -b, a, -b }, { -a, -a, -a, -b, a, -b },
{ -a, -a, b, -b, a, a }, { -a, -a, b, -b, a, a },
{ b, -a, b, a, a, a }, { b, -a, b, a, a, a },
{ b, -a, -a, a, a, -b }, { b, -a, -a, a, a, -b },
@ -215,7 +237,7 @@ local nodeboxes= {
table.insert(nodeboxes, { -b, -b, -a, b, b, -b }) table.insert(nodeboxes, { -b, -b, -a, b, b, -b })
end end
local nameext=tostring(xm)..tostring(xp)..tostring(ym)..tostring(yp)..tostring(zm)..tostring(zp) local nameext = string.format("%d%d%d%d%d%d", xm, xp, ym, yp, zm, zp)
local groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2 } 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
@ -237,33 +259,44 @@ local nodeboxes= {
frame = 1, frame = 1,
drop = "technic:frame_111111", drop = "technic:frame_111111",
sunlight_propagates = true, sunlight_propagates = true,
frame_connect_all = function(nodename) frame_connect_all = function(nodename)
l2 = {} 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}} 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 for i, dir in ipairs(l1) do
if string.sub(nodename, -7 + i, -7 + i) == "1" then if string.sub(nodename, -7 + i, -7 + i) == "1" then
l2[#(l2)+1]=dir l2[#l2 + 1] = dir
end end
end end
return l2 return l2
end, end,
on_punch = function(pos, node, puncher) on_punch = function(pos, node, puncher)
local ppos = puncher:getpos() local ppos = puncher:getpos()
local pvect = puncher:get_look_dir() local pvect = puncher:get_look_dir()
local pface = get_face(pos, ppos, pvect) local pface = get_face(pos, ppos, pvect)
if pface == nil then return end if pface == nil then return end
local nodename = node.name local nodename = node.name
local newstate=tostring(1-tonumber(string.sub(nodename,-7+pface,-7+pface))) local newstate = tostring(1 - tonumber(string.sub(nodename, pface - 7, pface - 7)))
if pface <= 5 then if pface <= 5 then
nodename=string.sub(nodename,1,-7+pface-1)..newstate..string.sub(nodename,-7+pface+1) nodename = string.sub(nodename, 1, pface - 7 - 1)..newstate..string.sub(nodename, pface - 7 + 1)
else else
nodename = string.sub(nodename, 1, -2)..newstate nodename = string.sub(nodename, 1, -2)..newstate
end end
node.name = nodename node.name = nodename
minetest.set_node(pos, node) minetest.set_node(pos, node)
end, end,
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.above local pos = pointed_thing.above
if minetest.is_protected(pos, placer:get_player_name()) then if minetest.is_protected(pos, placer:get_player_name()) then
minetest.log("action", placer:get_player_name() minetest.log("action", placer:get_player_name()
.. " tried to place " .. itemstack:get_name() .. " tried to place " .. itemstack:get_name()
@ -272,7 +305,9 @@ local nodeboxes= {
minetest.record_protection_violation(pos, placer:get_player_name()) minetest.record_protection_violation(pos, placer:get_player_name())
return itemstack return itemstack
end end
if pos == nil then return end if pos == nil then return end
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.name ~= "air" then if node.name ~= "air" then
if is_supported_node(node.name) then if is_supported_node(node.name) then
@ -282,11 +317,13 @@ local nodeboxes= {
else else
minetest.set_node(pos, { name = itemstack:get_name() }) minetest.set_node(pos, { name = itemstack:get_name() })
end end
if not infinite_stacks then if not infinite_stacks then
itemstack:take_item() itemstack:take_item()
end end
return itemstack return itemstack
end, end,
on_rightclick = function(pos, node, placer, itemstack, pointed_thing) on_rightclick = function(pos, node, placer, itemstack, pointed_thing)
if is_supported_node(itemstack:get_name()) then if is_supported_node(itemstack:get_name()) then
if minetest.is_protected(pos, placer:get_player_name()) then if minetest.is_protected(pos, placer:get_player_name()) then
@ -305,7 +342,7 @@ local nodeboxes= {
-- Run callback -- Run callback
if def.after_place_node then if def.after_place_node then
-- Copy place_to because callback can modify it -- Copy place_to because callback can modify it
local pos_copy = {x=pos.x, y=pos.y, z=pos.z} local pos_copy = vector.new(pos)
if def.after_place_node(pos_copy, placer, itemstack) then if def.after_place_node(pos_copy, placer, itemstack) then
take_item = false take_item = false
end end
@ -360,15 +397,17 @@ minetest.register_entity("technic:frame_entity", {
set_node = function(self, node) set_node = function(self, node)
self.node = node self.node = node
local pos = self.object:getpos() local pos = vector.round(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 frames_pos[pos_to_string(pos)] = node.name
local stack = ItemStack(node.name) local stack = ItemStack(node.name)
local itemtable = stack:to_table() local itemtable = stack:to_table()
local itemname = nil local itemname = nil
if itemtable then if itemtable then
itemname = stack:to_table().name itemname = stack:to_table().name
end end
local item_texture = nil local item_texture = nil
local item_type = "" local item_type = ""
if minetest.registered_items[itemname] then if minetest.registered_items[itemname] then
@ -393,8 +432,7 @@ minetest.register_entity("technic:frame_entity", {
dig = function(self) dig = function(self)
minetest.handle_node_drops(self.object:getpos(), { ItemStack("technic:frame_111111") }, self.last_puncher) minetest.handle_node_drops(self.object:getpos(), { ItemStack("technic:frame_111111") }, self.last_puncher)
local pos = self.object:getpos() local pos = vector.round(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 frames_pos[pos_to_string(pos)] = nil
self.object:remove() self.object:remove()
end, end,
@ -410,18 +448,21 @@ minetest.register_entity("technic:frame_entity", {
else else
self.damage_object:get_luaentity().remaining_time = 0.25 self.damage_object:get_luaentity().remaining_time = 0.25
end end
self.last_puncher = puncher self.last_puncher = puncher
local ppos = puncher:getpos() local ppos = puncher:getpos()
local pvect = puncher:get_look_dir() local pvect = puncher:get_look_dir()
local pface = get_face(pos, ppos, pvect) local pface = get_face(pos, ppos, pvect)
if pface == nil then return end if pface == nil then return end
local nodename = self.node.name local nodename = self.node.name
local newstate = tostring(1-tonumber(string.sub(nodename, -7+pface, -7+pface))) local newstate = tostring(1 - tonumber(string.sub(nodename, pface - 7, pface - 7)))
if pface <= 5 then if pface <= 5 then
nodename = string.sub(nodename, 1, -7+pface-1)..newstate..string.sub(nodename, -7+pface+1) nodename = string.sub(nodename, 1, pface - 7 - 1)..newstate..string.sub(nodename, pface - 7 + 1)
else else
nodename = string.sub(nodename, 1, -2)..newstate nodename = string.sub(nodename, 1, -2)..newstate
end end
self.node.name = nodename self.node.name = nodename
self:set_node(self.node) self:set_node(self.node)
end, end,
@ -431,14 +472,19 @@ minetest.register_entity("technic:frame_entity", {
local ppos = clicker:getpos() local ppos = clicker:getpos()
local pvect = clicker:get_look_dir() local pvect = clicker:get_look_dir()
local pface = get_face(pos, ppos, pvect) 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)} if pface == nil then
return
end
local pos_under = vector.round(pos)
local pos_above = { x = pos_under.x, y = pos_under.y, z = pos_under.z } local pos_above = { x = pos_under.x, y = pos_under.y, z = pos_under.z }
local index = ({ "x", "y", "z" })[math.floor((pface + 1) / 2)] local index = ({ "x", "y", "z" })[math.floor((pface + 1) / 2)]
pos_above[index] = pos_above[index] + 2 * ((pface + 1)%2) - 1 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_under, above = pos_above }
local itemstack = clicker:get_wielded_item() local itemstack = clicker:get_wielded_item()
local itemdef = minetest.registered_items[itemstack:get_name()] local itemdef = minetest.registered_items[itemstack:get_name()]
if itemdef ~= nil then if itemdef ~= nil then
itemdef.on_place(itemstack, clicker, pointed_thing) itemdef.on_place(itemstack, clicker, pointed_thing)
end end
@ -485,7 +531,11 @@ mesecon.register_on_mvps_move(function(moved_nodes)
local to_move = {} local to_move = {}
for _, n in ipairs(moved_nodes) do for _, n in ipairs(moved_nodes) do
if frames_pos[pos_to_string(n.oldpos)] ~= nil then 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)]} 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 frames_pos[pos_to_string(n.oldpos)] = nil
end end
end end
@ -495,7 +545,8 @@ mesecon.register_on_mvps_move(function(moved_nodes)
local objects = minetest.get_objects_inside_radius(t.oldpos, 0.1) local objects = minetest.get_objects_inside_radius(t.oldpos, 0.1)
for _, obj in ipairs(objects) do for _, obj in ipairs(objects) do
local entity = obj:get_luaentity() local entity = obj:get_luaentity()
if entity and (entity.name == "technic:frame_entity" or entity.name == "technic:damage_entity") then if entity and (entity.name == "technic:frame_entity" or
entity.name == "technic:damage_entity") then
obj:setpos(t.pos) obj:setpos(t.pos)
end end
end end
@ -525,10 +576,10 @@ local function connected(pos,c,adj)
if frames_pos[pos_to_string(pos1)] then if frames_pos[pos_to_string(pos1)] then
nodename = frames_pos[pos_to_string(pos1)] nodename = frames_pos[pos_to_string(pos1)]
end end
if not(pos_in_list(c,pos1)) and nodename~="air" and 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 == nil or
minetest.registered_nodes[nodename].frames_can_connect(pos1, vect)) then minetest.registered_nodes[nodename].frames_can_connect(pos1, vect)) then
c[#(c)+1]=pos1 c[#c + 1] = pos1
if minetest.registered_nodes[nodename].frame == 1 then if minetest.registered_nodes[nodename].frame == 1 then
local adj = minetest.registered_nodes[nodename].frame_connect_all(nodename) local adj = minetest.registered_nodes[nodename].frame_connect_all(nodename)
connected(pos1, c, adj) connected(pos1, c, adj)
@ -548,39 +599,55 @@ local function get_connected_nodes(pos)
end end
local function frame_motor_on(pos, node) 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 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 nnodepos = vector.add(pos, dirs[math.floor(node.param2 / 4) + 1])
local dir = minetest.facedir_to_dir(node.param2) local dir = minetest.facedir_to_dir(node.param2)
local nnode = minetest.get_node(nnodepos) local nnode = minetest.get_node(nnodepos)
if frames_pos[pos_to_string(nnodepos)] then if frames_pos[pos_to_string(nnodepos)] then
nnode.name = frames_pos[pos_to_string(nnodepos)] nnode.name = frames_pos[pos_to_string(nnodepos)]
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if meta:get_int("last_moved") == minetest.get_gametime() then if meta:get_int("last_moved") == minetest.get_gametime() then
return return
end end
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
if minetest.registered_nodes[nnode.name].frame == 1 then if minetest.registered_nodes[nnode.name].frame == 1 then
local connected_nodes = get_connected_nodes(nnodepos) local connected_nodes = get_connected_nodes(nnodepos)
move_nodes_vect(connected_nodes, dir, pos, owner) move_nodes_vect(connected_nodes, dir, pos, owner)
end end
minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime()) minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime())
end end
minetest.register_node("technic:frame_motor", { minetest.register_node("technic:frame_motor", {
description = S("Frame Motor"), description = S("Frame Motor"),
tiles = {"pipeworks_filter_top.png^[transformR90", "technic_lv_cable.png", "technic_lv_cable.png", tiles = {
"technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png"}, "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 }, groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, mesecon = 2 },
paramtype2 = "facedir", paramtype2 = "facedir",
mesecons = { effector = { action_on = frame_motor_on } }, mesecons = { effector = { action_on = frame_motor_on } },
after_place_node = function(pos, placer, itemstack) after_place_node = function(pos, placer, itemstack)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name()) meta:set_string("owner", placer:get_player_name())
end, end,
frames_can_connect = function(pos, dir) frames_can_connect = function(pos, dir)
local node = minetest.get_node(pos) 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] 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]
return dir2.x ~= -dir.x or dir2.y ~= -dir.y or dir2.z ~= -dir.z return dir2.x ~= -dir.x or dir2.y ~= -dir.y or dir2.z ~= -dir.z
end end
}) })
@ -589,13 +656,19 @@ minetest.register_node("technic:frame_motor",{
-- Templates -- Templates
local function template_connected(pos, c, connectors) 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 vects = {
{ 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 }
}
for _, vect in ipairs(vects) 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" 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) 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, connectors) template_connected(pos1, c, connectors)
if nodename == "technic:template_connector" then if nodename == "technic:template_connector" then
connectors[#connectors + 1] = pos1 connectors[#connectors + 1] = pos1
@ -638,6 +711,7 @@ local function save_node(pos)
meta:set_string("connected", "") meta:set_string("connected", "")
return return
end 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
@ -645,6 +719,7 @@ local function save_node(pos)
list[key] = stack:to_string() list[key] = stack:to_string()
end end
end end
node.meta = meta0 node.meta = meta0
minetest.set_node(pos, { name = "technic:template" }) minetest.set_node(pos, { name = "technic:template" })
return node return node
@ -664,25 +739,27 @@ end
local function expand_template(pos) 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
c = minetest.deserialize(c) c = minetest.deserialize(c)
for _, vect in ipairs(c) do for _, vect in ipairs(c) do
local pos1 = vector.add(pos, vect) local pos1 = vector.add(pos, vect)
local saved_node = save_node(pos1) local saved_node = save_node(pos1)
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
--meta1:set_string("saved_node", "")
end end
end end
end end
local function compress_templates(pos) local function compress_templates(pos)
local templates, connectors = get_templates(pos) local templates, connectors = get_templates(pos)
if #connectors == 0 then if #connectors == 0 then
connectors = { pos } connectors = { pos }
end end
for _, cn in ipairs(connectors) do for _, cn in ipairs(connectors) do
local meta = minetest.get_meta(cn) local meta = minetest.get_meta(cn)
local c = {} local c = {}
@ -695,7 +772,7 @@ local function compress_templates(pos)
local cc = {} local cc = {}
for _, p in ipairs(connectors) do for _, p in ipairs(connectors) do
local np = vector.subtract(p, cn) local np = vector.subtract(p, cn)
if (np.x ~= 0 or np.y ~= 0 or np.z ~= 0) then if np.x ~= 0 or np.y ~= 0 or np.z ~= 0 then
cc[pos_to_string(np)] = true cc[pos_to_string(np)] = true
end end
end end
@ -715,6 +792,7 @@ 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 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 else
@ -736,7 +814,7 @@ local function template_drops(pos, node, oldmeta, digger)
end end
else else
local stack_max = 99 local stack_max = 99
local num = #(minetest.deserialize(c)) local num = #minetest.deserialize(c)
drops = {} drops = {}
while num > stack_max do while num > stack_max do
drops[#drops + 1] = "technic:template "..stack_max drops[#drops + 1] = "technic:template "..stack_max
@ -746,6 +824,7 @@ local function template_drops(pos, node, oldmeta, digger)
end end
end end
end end
minetest.handle_node_drops(pos, drops, digger) minetest.handle_node_drops(pos, drops, digger)
end end
@ -820,7 +899,7 @@ minetest.register_tool("technic:template_tool",{
inventory_image = "technic_template_tool.png", inventory_image = "technic_template_tool.png",
on_use = function(itemstack, puncher, pointed_thing) on_use = function(itemstack, puncher, pointed_thing)
local pos = pointed_thing.under local pos = pointed_thing.under
if pos == nil or (minetest.is_protected and minetest.is_protected(pos, puncher:get_player_name())) then if pos == nil or minetest.is_protected and minetest.is_protected(pos, puncher:get_player_name()) then
return nil return nil
end end
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -849,14 +928,18 @@ local function get_template_nodes(pos)
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 ~= "air" then if not(pos_in_list(c, pos1)) and nodename ~= "air" then
c[#(c)+1]=pos1 c[#c + 1] = pos1
end end
end end
return c return c
end end
local function template_motor_on(pos, node) 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 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 nnodepos = vector.add(pos, dirs[math.floor(node.param2 / 4) + 1])
local dir = minetest.facedir_to_dir(node.param2) local dir = minetest.facedir_to_dir(node.param2)
local nnode = minetest.get_node(nnodepos) local nnode = minetest.get_node(nnodepos)
@ -874,8 +957,14 @@ end
minetest.register_node("technic:template_motor", { minetest.register_node("technic:template_motor", {
description = S("Template Motor"), description = S("Template Motor"),
tiles = {"pipeworks_filter_top.png^[transformR90", "technic_lv_cable.png", "technic_lv_cable.png", tiles = {
"technic_lv_cable.png", "technic_lv_cable.png", "technic_lv_cable.png"}, "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 }, groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, mesecon = 2 },
paramtype2 = "facedir", paramtype2 = "facedir",
mesecons = { effector = { action_on = template_motor_on } }, mesecons = { effector = { action_on = template_motor_on } },