code:
-- NOTE: The code is takes directly from VanessaE's homedecor mod.
-- I just made it the lights into indictive appliances for this mod.

-- This file supplies electric powered glowlights

-- Boilerplate to support localized strings if intllib mod is installed.
local S
if (minetest.get_modpath("intllib")) then
	dofile(minetest.get_modpath("intllib").."/intllib.lua")
	S = intllib.Getter(minetest.get_current_modname())
else
	S = function (s) return s end
end

local function technic_homedecor_node_is_owned(pos, placer)
        local ownername = false
        if type(isprotect) == "function" then -- glomie's protection mod
                if not isprotect(5, pos, placer) then
                        ownername = S("someone")
                end
        elseif type(protector) == "table" and type(protector.can_dig) == "function" then -- Zeg9's protection mod
                if not protector.can_dig(5, pos, placer) then
                        ownername = S("someone")
                end
        end

        if ownername ~= false then
                minetest.chat_send_player(placer:get_player_name(), S("Sorry, %s owns that spot."):format(ownername) )
                return true
        else
                return false
        end
end

local dirs2 = {9,  18,  7, 12}

local technic_homedecor_rotate_and_place = function(itemstack, placer, pointed_thing)
	if not technic_homedecor_node_is_owned(pointed_thing.under, placer)
	   and not technic_homedecor_node_is_owned(pointed_thing.above, placer) then
		local node = minetest.get_node(pointed_thing.under)
		if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].on_rightclick then

			local above = pointed_thing.above
			local under = pointed_thing.under
			local pitch = placer:get_look_pitch()
			local pname = minetest.get_node(under).name
			local fdir = minetest.dir_to_facedir(placer:get_look_dir())
			local wield_name = itemstack:get_name()

			if not minetest.registered_nodes[pname]
			    or not minetest.registered_nodes[pname].on_rightclick then

				local iswall = (above.x ~= under.x) or (above.z ~= under.z)
				local isceiling = (above.x == under.x) and (above.z == under.z) and (pitch > 0)
				local pos1 = above

				if minetest.registered_nodes[pname]["buildable_to"] then
					pos1 = under
					iswall = false
				end

				if not minetest.registered_nodes[minetest.get_node(pos1).name]["buildable_to"] then return end

				if iswall then
					minetest.add_node(pos1, {name = wield_name, param2 = dirs2[fdir+1] }) -- place wall variant
				elseif isceiling then
					minetest.add_node(pos1, {name = wield_name, param2 = 20 }) -- place upside down variant
				else
					minetest.add_node(pos1, {name = wield_name, param2 = 0 }) -- place right side up
				end

				if not homedecor_expect_infinite_stacks then
					itemstack:take_item()
					return itemstack
				end
			end
		else
			minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack)
		end
	end
end

-- Yellow -- Half node
minetest.register_node('technic:homedecor_glowlight_half_yellow', {
	description = S("Yellow Glowlight (thick)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "Yellow Glowlight (thick)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 100, "technic:homedecor_glowlight_half_yellow_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_half_yellow_active', {
	description = S("Yellow Glowlight (thick)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png',
		'technic_homedecor_glowlight_thick_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_half_yellow",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "Yellow Glowlight (thick)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_half_yellow")
		   end
})

-- Yellow -- Quarter node
minetest.register_node('technic:homedecor_glowlight_quarter_yellow', {
	description = S("Yellow Glowlight (thin)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "Yellow Glowlight (thin)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 100, "technic:homedecor_glowlight_quarter_yellow_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_quarter_yellow_active', {
	description = S("Yellow Glowlight (thin)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_yellow_tb.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png',
		'technic_homedecor_glowlight_thin_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX-1,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_quarter_yellow",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "Yellow Glowlight (thin)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_quarter_yellow")
		   end
})

-- White -- half node
minetest.register_node('technic:homedecor_glowlight_half_white', {
	description = S("White Glowlight (thick)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "White Glowlight (thick)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 100, "technic:homedecor_glowlight_half_white_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_half_white_active', {
	description = S("White Glowlight (thick)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png',
		'technic_homedecor_glowlight_thick_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, 0, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_half_white",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "White Glowlight (thick)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_half_white")
		   end
})

-- White -- Quarter node
minetest.register_node('technic:homedecor_glowlight_quarter_white', {
	description = S("White Glowlight (thin)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "White Glowlight (thin)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 100, "technic:homedecor_glowlight_quarter_white_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_quarter_white_active', {
	description = S("White Glowlight (thin)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_white_tb.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png',
		'technic_homedecor_glowlight_thin_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.5, -0.5, -0.5, 0.5, -0.25, 0.5 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX-1,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_quarter_white",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 100, "White Glowlight (thin)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_quarter_white")
		   end
})

-- Glowlight "cubes" - yellow
minetest.register_node('technic:homedecor_glowlight_small_cube_yellow', {
	description = S("Yellow Glowlight (small cube)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_cube_yellow_tb.png',
		'technic_homedecor_glowlight_cube_yellow_tb.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 50, "Yellow Glowlight (small cube)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 50, "technic:homedecor_glowlight_small_cube_yellow_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_small_cube_yellow_active', {
	description = S("Yellow Glowlight (small cube)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_cube_yellow_tb.png',
		'technic_homedecor_glowlight_cube_yellow_tb.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png',
		'technic_homedecor_glowlight_cube_yellow_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX-1,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_small_cube_yellow",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 50, "Yellow Glowlight (small cube)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_small_cube_yellow")
		   end
})

-- Glowlight "cubes" - white
minetest.register_node('technic:homedecor_glowlight_small_cube_white', {
	description = S("White Glowlight (small cube)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_cube_white_tb.png',
		'technic_homedecor_glowlight_cube_white_tb.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3 },
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 50, "White Glowlight (small cube)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_off(pos, 50, "technic:homedecor_glowlight_small_cube_white_active")
		   end
})

minetest.register_node('technic:homedecor_glowlight_small_cube_white_active', {
	description = S("White Glowlight (small cube)"),
	drawtype = "nodebox",
	tiles = {
		'technic_homedecor_glowlight_cube_white_tb.png',
		'technic_homedecor_glowlight_cube_white_tb.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png',
		'technic_homedecor_glowlight_cube_white_sides.png'
	},
        selection_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },
        node_box = {
                type = "fixed",
                fixed = { -0.25, -0.5, -0.25, 0.25, 0, 0.25 }
        },

	sunlight_propagates = false,
	paramtype = "light",
	paramtype2 = "facedir",
	walkable = true,
	light_source = minetest.LIGHT_MAX-1,
	sounds = default.node_sound_wood_defaults(),

	groups = { snappy = 3, not_in_creative_inventory=1},
	drop="technic:homedecor_glowlight_small_cube_white",
	on_place = function(itemstack, placer, pointed_thing)
		technic_homedecor_rotate_and_place(itemstack, placer, pointed_thing)
		return itemstack
	     end,
	on_construct = function(pos)
			  technic.inductive_on_construct(pos, 50, "White Glowlight (small cube)")
		       end,
	on_punch = function(pos, node, puncher)
		      technic.inductive_on_punch_on(pos, 0, "technic:homedecor_glowlight_small_cube_white")
		   end
})

technic.register_inductive_machine("technic:homedecor_glowlight_half_yellow")
technic.register_inductive_machine("technic:homedecor_glowlight_half_white")
technic.register_inductive_machine("technic:homedecor_glowlight_quarter_yellow")
technic.register_inductive_machine("technic:homedecor_glowlight_quarter_white")
technic.register_inductive_machine("technic:homedecor_glowlight_small_cube_yellow")
technic.register_inductive_machine("technic:homedecor_glowlight_small_cube_white")


code:
-- The power radiator fuctions like an inductive charger
-- only better in the game setting.
-- The purpose is to allow small appliances to receive power
-- without the overhead of the wiring needed for larger machines.
--
-- The power radiator will consume power corresponding to the
-- sum(power rating of the attached appliances)/0.06
-- Using inductive power transfer is very inefficient so this is
-- set to the factor 0.06.
--
-- Punching the radiator will toggle the power state of all attached appliances.

local power_radius = 12

minetest.register_craft({
	output = 'technic:power_radiator 1',
	recipe = {
		{'technic:stainless_steel_ingot', 'technic:mv_transformer', 'technic:stainless_steel_ingot'},
		{'technic:copper_coil',           'technic:machine_casing', 'technic:copper_coil'},
		{'technic:rubber',                'technic:mv_cable',       'technic:rubber'},
	}
})

------------------------------------------------------------------
-- API for inductive powered nodes:
-- Use the functions below to set the corresponding callbacks
-- Also two nodes are needed: The inactive and the active one. The active must be called <name>_active .
------------------------------------------------------------------
-- Register a new appliance using this function

technic.inductive_nodes = {}
technic.register_inductive_machine = function(name)
	table.insert(technic.inductive_nodes, name)
	table.insert(technic.inductive_nodes, name.."_active")
end

-- Appliances:
--  has_supply: pos of supply node if the appliance has a power radiator near with sufficient power for the demand else ""
--  EU_demand: The power demand of the device.
--  EU_charge: Actual use. set to EU_demand if active==1
--  active: set to 1 if the device is on
technic.inductive_on_construct = function(pos, eu_demand, infotext)
	local meta = minetest.get_meta(pos)
	meta:set_string("infotext", infotext)
	meta:set_int("technic_inductive_power_machine", 1)
	meta:set_int("EU_demand", eu_demand)     -- The power demand of this appliance
	meta:set_int("EU_charge", 0)       -- The actual power draw of this appliance
	meta:set_string("has_supply", "") -- Register whether we are powered or not. For use with several radiators.
	meta:set_int("active", 0)    -- If the appliance can be turned on and off by using it use this.
end

technic.inductive_on_punch_off = function(pos, eu_charge, swapnode)
	local meta = minetest.get_meta(pos)
	if meta:get_string("has_supply") ~= "" then
		technic.swap_node(pos, swapnode)
		meta:set_int("active", 1)
		meta:set_int("EU_charge",eu_charge)
		--print("-----------")
		--print("Turn on:")
		--print("EU_charge: "..meta:get_int("EU_charge"))
		--print("has_supply: "..meta:get_string("has_supply"))
		--print("<----------->")
	end
end

technic.inductive_on_punch_on = function(pos, eu_charge, swapnode)
	local meta = minetest.get_meta(pos)
	technic.swap_node(pos, swapnode)
	meta:set_int("active", 0)
	meta:set_int("EU_charge",eu_charge)
	--print("-----------")
	--print("Turn off:")
	--print("EU_charge: "..meta:get_int("EU_charge"))
	--print("has_supply: "..meta:get_string("has_supply"))
	--print("<---------->")
end

local shutdown_inductive_appliances = function(pos)
	-- The supply radius
	local rad = power_radius
	-- If the radiator is removed. turn off all appliances in region
	-- If another radiator is near it will turn on the appliances again
	local positions = minetest.find_nodes_in_area(
		{x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
		{x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
		technic.inductive_nodes)
	for _, pos1 in pairs(positions) do
		local meta1 = minetest.get_meta(pos1)
		-- If the appliance is belonging to this node
		if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
			local nodename = minetest.get_node(pos1).name
			-- Swap the node and make sure it is off and unpowered
			if string.sub(nodename, -7) == "_active" then
				technic.swap_node(pos1, string.sub(nodename, 1, -8))
				meta1:set_int("active", 0)
				meta1:set_int("EU_charge", 0)
			end
			meta1:set_string("has_supply", "")
		end
	end
end

local toggle_on_off_inductive_appliances = function(pos, node, puncher)
	if pos == nil then return end
	-- The supply radius
	local rad = power_radius
	local positions = minetest.find_nodes_in_area(
		{x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
		{x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
		technic.inductive_nodes)
	for _, pos1 in pairs(positions) do
		local meta1 = minetest.get_meta(pos1)
		if meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
			minetest.punch_node(pos1)
		end
	end
end

minetest.register_node("technic:power_radiator", {
	description = "MV Power Radiator",
	tiles  = {"technic_lv_cable.png", "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},
	sounds = default.node_sound_wood_defaults(),
	drawtype = "nodebox",
	paramtype = "light",
	is_ground_content = true,
	node_box = {
		type = "fixed",
		fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
	},
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_int("MV_EU_demand",1)               -- Demand on the primary side when idle
		meta:set_int("connected_EU_demand",0)        -- Potential demand of connected appliances
		meta:set_string("infotext", "MV Power Radiator")
	end,
	on_dig = function(pos, node, digger)
		shutdown_inductive_appliances(pos)
		return minetest.node_dig(pos, node, digger)
	end,
	on_punch = function(pos, node, puncher)
		toggle_on_off_inductive_appliances(pos, node, puncher)
	end
})

minetest.register_abm({
	label = "Machines: run power radiator",
	nodenames = {"technic:power_radiator"},
	interval   = 1,
	chance     = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local meta             = minetest.get_meta(pos)
		local eu_input  = meta:get_int("MV_EU_input")
		local eu_demand = meta:get_int("MV_EU_demand")

		-- Power off automatically if no longer connected to a switching station
		technic.switching_station_timeout_count(pos, "MV")

		if eu_input == 0 then
			-- No power
			meta:set_string("infotext", "MV Power Radiator is unpowered");
			-- meta:set_int("active", 1) -- used for setting textures someday maybe
			shutdown_inductive_appliances(pos)
			meta:set_int("connected_EU_demand", 0)
			meta:set_int("MV_EU_demand",1)
		elseif eu_input == eu_demand then
			-- Powered and ready

			-- The maximum EU sourcing a single radiator can provide.
			local max_charge          = 30000 -- == the max EU demand of the radiator
			local connected_EU_demand = meta:get_int("connected_EU_demand")

			-- Efficiency factor
			local eff_factor = 0.06
			-- The supply radius
			local rad = power_radius

			local used_charge      = 0

			-- Index all nodes within supply range
			local positions = minetest.find_nodes_in_area(
				{x=pos.x-rad, y=pos.y-rad, z=pos.z-rad},
				{x=pos.x+rad, y=pos.y+rad, z=pos.z+rad},
				technic.inductive_nodes)
			for _, pos1 in pairs(positions) do
				local meta1 = minetest.get_meta(pos1)
				-- If not supplied see if this node can handle it.
				if meta1:get_string("has_supply") == "" then
					-- if demand surpasses the capacity of this node, don't bother adding it.
					local app_eu_demand = math.floor(meta1:get_int("EU_demand") / eff_factor)
					if connected_EU_demand + app_eu_demand <= max_charge then
						-- We can power the appliance. Register, and spend power if it is on.
						connected_EU_demand = connected_EU_demand + app_eu_demand

						meta1:set_string("has_supply", pos.x..pos.y..pos.z)
						--Always 0: used_charge = math.floor(used_charge + meta1:get_int("EU_charge") / eff_factor)
					end
				elseif meta1:get_string("has_supply") == pos.x..pos.y..pos.z then
					-- The appliance has power from this node. Spend power if it is on.
					used_charge = used_charge + math.floor(meta1:get_int("EU_charge") / eff_factor)
				end
				meta:set_string("infotext", "MV Power Radiator is powered ("
					..math.floor(used_charge / max_charge * 100)
					.."% of maximum power)");
				if used_charge == 0 then
					meta:set_int("MV_EU_demand", 1) -- Still idle
				else
					meta:set_int("MV_EU_demand", used_charge)
				end
			end
			-- Save state
			meta:set_int("connected_EU_demand", connected_EU_demand)
		end
	end,
})

technic.register_machine("MV", "technic:power_radiator", technic.receiver)

repo_name: Desour/PlayMinetestServer

license: mit


code:
minetest.register_craft({
	output = 'technic:solar_array_mv 1',
	recipe = {
		{'technic:solar_array_lv',     'technic:solar_array_lv', 'technic:solar_array_lv'},
		{'technic:carbon_steel_ingot', 'technic:mv_transformer', 'technic:carbon_steel_ingot'},
		{'',                           'technic:mv_cable',       ''},
	}
})

technic.register_solar_array({tier="MV", power=30})

-- compatibility alias for upgrading from old versions of technic
minetest.register_alias("technic:solar_panel_mv", "technic:solar_array_mv")


code:
-- Tool workshop
-- This machine repairs tools.

minetest.register_alias("tool_workshop", "technic:tool_workshop")

local S = technic.getter

local tube_entry = "^pipeworks_tube_connection_wooden.png"

minetest.register_craft({
	output = 'technic:tool_workshop',
	recipe = {
		{'group:wood',                         'default:diamond',        'group:wood'},
		{'mesecons_pistons:piston_sticky_off', 'technic:machine_casing', 'technic:carbon_cloth'},
		{'default:obsidian',                   'technic:mv_cable',       'default:obsidian'},
	}
})

local workshop_demand = {5000, 3500, 2000}

local workshop_formspec =
	"size[8,9;]"..
	"list[current_name;src;3,1;1,1;]"..
	"label[0,0;"..S("%s Tool Workshop"):format("MV").."]"..
	"list[current_name;upgrade1;1,3;1,1;]"..
	"list[current_name;upgrade2;2,3;1,1;]"..
	"label[1,4;"..S("Upgrade Slots").."]"..
	"list[current_player;main;0,5;8,4;]"..
	"listring[current_player;main]"..
	"listring[current_name;src]"..
	"listring[current_player;main]"..
	"listring[current_name;upgrade1]"..
	"listring[current_player;main]"..
	"listring[current_name;upgrade2]"..
	"listring[current_player;main]"

local run = function(pos, node)
	local meta         = minetest.get_meta(pos)
	local inv          = meta:get_inventory()
	local eu_input     = meta:get_int("MV_EU_input")
	local machine_name = S("%s Tool Workshop"):format("MV")

	-- Setup meta data if it does not exist.
	if not eu_input then
		meta:set_int("MV_EU_demand", workshop_demand[1])
		meta:set_int("MV_EU_input", 0)
		return
	end

	local EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)

	local repairable = false
	local srcstack = inv:get_stack("src", 1)
	if not srcstack:is_empty() then
		local itemdef = minetest.registered_items[srcstack:get_name()]
		if itemdef and
				(not itemdef.wear_represents or
				itemdef.wear_represents == "mechanical_wear") and
				srcstack:get_wear() ~= 0 then
			repairable = true
		end
	end
	technic.handle_machine_pipeworks(pos, tube_upgrade, function (pos, x_velocity, z_velocity)
		if not repairable then
			technic.send_items(pos, x_velocity, z_velocity, "src")
		end
	end)
	if not repairable then
		meta:set_string("infotext", S("%s Idle"):format(machine_name))
		meta:set_int("MV_EU_demand", 0)
		return
	end

	if eu_input < workshop_demand[EU_upgrade+1] then
		meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
	elseif eu_input >= workshop_demand[EU_upgrade+1] then
		meta:set_string("infotext", S("%s Active"):format(machine_name))
		srcstack:add_wear(-1000)
		inv:set_stack("src", 1, srcstack)
	end
	meta:set_int("MV_EU_demand", workshop_demand[EU_upgrade+1])
end

minetest.register_node("technic:tool_workshop", {
	description = S("%s Tool Workshop"):format("MV"),
	paramtype2 = "facedir",
	tiles = {
		"technic_workshop_top.png"..tube_entry,
		"technic_machine_bottom.png"..tube_entry,
		"technic_workshop_side.png"..tube_entry,
		"technic_workshop_side.png"..tube_entry,
		"technic_workshop_side.png"..tube_entry,
		"technic_workshop_side.png"
	},
	groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
		technic_machine=1, technic_mv=1, tubedevice=1, tubedevice_receiver=1},
	connect_sides = {"bottom", "back", "left", "right"},
	sounds = default.node_sound_wood_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("infotext", S("%s Tool Workshop"):format("MV"))
		meta:set_string("formspec", workshop_formspec)
		local inv = meta:get_inventory()
		inv:set_size("src", 1)
		inv:set_size("upgrade1", 1)
		inv:set_size("upgrade2", 1)
	end,
	can_dig = technic.machine_can_dig,
	allow_metadata_inventory_put = technic.machine_inventory_put,
	allow_metadata_inventory_take = technic.machine_inventory_take,
	tube = {
		can_insert = function (pos, node, stack, direction)
			return minetest.get_meta(pos):get_inventory():room_for_item("src", stack)
		end,
		insert_object = function (pos, node, stack, direction)
			return minetest.get_meta(pos):get_inventory():add_item("src", stack)
		end,
		connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
	},
	technic_run = run,
	after_place_node = pipeworks.after_place,
	after_dig_node = technic.machine_after_dig_node
})

technic.register_machine("MV", "technic:tool_workshop", technic.receiver)


code:
local S = technic.getter

minetest.register_craft({
	output = 'technic:wind_mill_frame 5',
	recipe = {
		{'technic:carbon_steel_ingot', '',                           'technic:carbon_steel_ingot'},
		{'',                           'technic:carbon_steel_ingot', ''},
		{'technic:carbon_steel_ingot', '',                           'technic:carbon_steel_ingot'},
	}
})

minetest.register_craft({
	output = 'technic:wind_mill',
	recipe = {
		{'',                           'basic_materials:motor',              ''},
		{'technic:carbon_steel_ingot', 'technic:carbon_steel_block', 'technic:carbon_steel_ingot'},
		{'',                           'technic:mv_cable',           ''},
	}
})

minetest.register_node("technic:wind_mill_frame", {
	description = S("Wind Mill Frame"),
	drawtype = "glasslike_framed",
	tiles = {"technic_carbon_steel_block.png", "default_glass.png"},
	sunlight_propagates = true,
	groups = {cracky=3},
	sounds = default.node_sound_stone_defaults(),
	paramtype = "light",
})

local function check_wind_mill(pos)
	if pos.y < 30 then
		return false
	end
	pos = {x=pos.x, y=pos.y, z=pos.z}
	for i = 1, 20 do
		pos.y = pos.y - 1
		local node = minetest.get_node_or_nil(pos)
		if not node then
			-- we reached CONTENT_IGNORE, we can assume, that nothing changed
			-- as the user will have to load the block to change it
			return
		end
		if node.name ~= "technic:wind_mill_frame" then
			return false
		end
	end
	return true
end

local run = function(pos, node)
	local meta = minetest.get_meta(pos)
	local machine_name = S("Wind %s Generator"):format("MV")

	local check = check_wind_mill(pos)
	if check == false then
		meta:set_int("MV_EU_supply", 0)
		meta:set_string("infotext", S("%s Improperly Placed"):format(machine_name))
	elseif check == true then
		local power = math.min(pos.y * 100, 5000)
		meta:set_int("MV_EU_supply", power)
		meta:set_string("infotext", S("@1 (@2)", machine_name,
			technic.EU_string(power)))
	end
	-- check == nil: assume nothing has changed
end

minetest.register_node("technic:wind_mill", {
	description = S("Wind %s Generator"):format("MV"),
	tiles = {"technic_carbon_steel_block.png"},
	paramtype2 = "facedir",
	groups = {cracky=1, technic_machine=1, technic_mv=1},
	connect_sides = {"top", "bottom", "back", "left", "right"},
	sounds = default.node_sound_stone_defaults(),
	drawtype = "nodebox",
	paramtype = "light",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}, -- Main box
			{-0.1, -0.1, -0.5,  0.1, 0.1, -0.6}, -- Shaft
			{-0.1, -1,   -0.6,  0.1, 1,   -0.7}, -- Vertical blades
			{-1,   -0.1, -0.6,  1,   0.1, -0.7}, -- Horizontal blades
		}
	},
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("infotext", S("Wind %s Generator"):format("MV"))
		meta:set_int("MV_EU_supply", 0)
	end,
	technic_run = run,
})

technic.register_machine("MV", "technic:wind_mill", technic.producer)


code:
local path = technic.modpath.."/machines"

technic.digilines = {
	rules = {
		-- digilines.rules.default
		{x= 1,y= 0,z= 0},{x=-1,y= 0,z= 0}, -- along x beside
		{x= 0,y= 0,z= 1},{x= 0,y= 0,z=-1}, -- along z beside
		{x= 1,y= 1,z= 0},{x=-1,y= 1,z= 0}, -- 1 node above along x diagonal
		{x= 0,y= 1,z= 1},{x= 0,y= 1,z=-1}, -- 1 node above along z diagonal
		{x= 1,y=-1,z= 0},{x=-1,y=-1,z= 0}, -- 1 node below along x diagonal
		{x= 0,y=-1,z= 1},{x= 0,y=-1,z=-1}, -- 1 node below along z diagonal
		-- added rules for digi cable
		{x =  0, y = -1, z = 0}, -- along y below
	}
}

dofile(path.."/register/init.lua")

-- Tiers
dofile(path.."/LV/init.lua")
dofile(path.."/MV/init.lua")
dofile(path.."/HV/init.lua")

dofile(path.."/switching_station.lua")
dofile(path.."/switching_station_globalstep.lua")

dofile(path.."/power_monitor.lua")
dofile(path.."/supply_converter.lua")

dofile(path.."/other/init.lua")


code:
local S = technic.getter

local desc = S("Administrative World Anchor")

local function compute_forceload_positions(pos, meta)
	local radius = meta:get_int("radius")
	local minpos = vector.subtract(pos, vector.new(radius, radius, radius))
	local maxpos = vector.add(pos, vector.new(radius, radius, radius))
	local minbpos = {}
	local maxbpos = {}
	for _, coord in ipairs({"x","y","z"}) do
		minbpos[coord] = math.floor(minpos[coord] / 16) * 16
		maxbpos[coord] = math.floor(maxpos[coord] / 16) * 16
	end
	local flposes = {}
	for x = minbpos.x, maxbpos.x, 16 do
		for y = minbpos.y, maxbpos.y, 16 do
			for z = minbpos.z, maxbpos.z, 16 do
				table.insert(flposes, vector.new(x, y, z))
			end
		end
	end
	return flposes
end

local function currently_forceloaded_positions(meta)
	local ser = meta:get_string("forceloaded")
	return ser == "" and {} or minetest.deserialize(ser)
end

local function forceload_off(meta)
	local flposes = currently_forceloaded_positions(meta)
	meta:set_string("forceloaded", "")
	for _, p in ipairs(flposes) do
		minetest.forceload_free_block(p)
	end
end

local function forceload_on(pos, meta)
	local want_flposes = compute_forceload_positions(pos, meta)
	local have_flposes = {}
	for _, p in ipairs(want_flposes) do
		if minetest.forceload_block(p) then
			table.insert(have_flposes, p)
		end
	end
	meta:set_string("forceloaded", #have_flposes == 0 and "" or minetest.serialize(have_flposes))
end

local function set_display(pos, meta)
	meta:set_string("infotext", S(meta:get_int("enabled") ~= 0 and "%s Enabled" or "%s Disabled"):format(desc))
	meta:set_string("formspec",
		"size[5,3.5]"..
		"item_image[0,0;1,1;technic:admin_anchor]"..
		"label[1,0;"..minetest.formspec_escape(desc).."]"..
		"label[0,1;"..minetest.formspec_escape(S("Owner:").." "..meta:get_string("owner")).."]"..
		(meta:get_int("locked") == 0 and
			"button[3,1;2,1;lock;"..minetest.formspec_escape(S("Unlocked")).."]" or
			"button[3,1;2,1;unlock;"..minetest.formspec_escape(S("Locked")).."]")..
		"field[0.25,2.3;1,1;radius;"..minetest.formspec_escape(S("Radius:"))..";"..meta:get_int("radius").."]"..
		(meta:get_int("enabled") == 0 and
			"button[3,2;2,1;enable;"..minetest.formspec_escape(S("Disabled")).."]" or
			"button[3,2;2,1;disable;"..minetest.formspec_escape(S("Enabled")).."]")..
		"label[0,3;"..minetest.formspec_escape(S("Keeping %d/%d map blocks loaded"):format(#currently_forceloaded_positions(meta), #compute_forceload_positions(pos, meta))).."]")
end

minetest.register_node("technic:admin_anchor", {
	description = desc,
	drawtype = "normal",
	tiles = {"technic_admin_anchor.png"},
	is_ground_content = true,
	groups = {cracky=3, not_in_creative_inventory=1},
	sounds = default.node_sound_stone_defaults(),
	after_place_node = function (pos, placer)
		local meta = minetest.get_meta(pos)
		if placer and placer:is_player() then
			local player_name = placer:get_player_name()
			meta:set_string("owner", player_name)
			logging.log("technic:admin_anchor place", string.format("anchor placed at %s by: %s",
					minetest.pos_to_string(pos), player_name))
		else
			logging.log("technic:admin_anchor place", string.format("anchor placed at %s by non-player",
					minetest.pos_to_string(pos)))
		end
		set_display(pos, meta)
	end,
	can_dig = function (pos, player)
		local meta = minetest.get_meta(pos)
		return meta:get_int("locked") == 0 or (player and player:is_player() and player:get_player_name() == meta:get_string("owner"))
	end,
	on_destruct = function (pos)
		local meta = minetest.get_meta(pos)
		forceload_off(meta)
		logging.log("technic:admin_anchor remove", string.format("anchor removed at %s",
				minetest.pos_to_string(pos)))
	end,
	on_receive_fields = function (pos, formname, fields, sender)
		local meta = minetest.get_meta(pos)
		if (meta:get_int("locked") ~= 0 or fields.lock) and
				not (sender and sender:is_player() and
					sender:get_player_name() == meta:get_string("owner")) then
			return
		end
		if fields.unlock then meta:set_int("locked", 0) end
		if fields.lock then meta:set_int("locked", 1) end
		if fields.disable or fields.enable or fields.radius then
			forceload_off(meta)
			if fields.disable then meta:set_int("enabled", 0) end
			if fields.enable then meta:set_int("enabled", 1) end
			if fields.radius and string.find(fields.radius, "^[0-9]+$") and tonumber(fields.radius) < 256 then meta:set_int("radius", fields.radius) end
			if meta:get_int("enabled") ~= 0 then
				forceload_on(pos, meta)
			end
		end
		set_display(pos, meta)
	end,
	mesecons = {
		effector = {
			action_on = function(pos)
				local meta = minetest.get_meta(pos)
				forceload_on(pos, meta)
			end,
			action_off = function(pos)
				local meta = minetest.get_meta(pos)
				forceload_off(meta)
			end
		}
	}
})


code:
-- Fuel driven alloy furnace. This uses no EUs:

local S = technic.getter

minetest.register_craft({
	output = 'technic:coal_alloy_furnace',
	recipe = {
		{'default:brick', 'default:brick', 'default:brick'},
		{'default:brick', '',              'default:brick'},
		{'default:brick', 'default:brick', 'default:brick'},
	}
})

local machine_name = S("Fuel-Fired Alloy Furnace")
local formspec =
	"size[8,9]"..
	"label[0,0;"..machine_name.."]"..
	"image[2,2;1,1;default_furnace_fire_bg.png]"..
	"list[current_name;fuel;2,3;1,1;]"..
	"list[current_name;src;2,1;2,1;]"..
	"list[current_name;dst;5,1;2,2;]"..
	"list[current_player;main;0,5;8,4;]"..
	"listring[current_name;dst]"..
	"listring[current_player;main]"..
	"listring[current_name;src]"..
	"listring[current_player;main]"..
	"listring[current_name;fuel]"..
	"listring[current_player;main]"

minetest.register_node("technic:coal_alloy_furnace", {
	description = machine_name,
	tiles = {"technic_coal_alloy_furnace_top.png",  "technic_coal_alloy_furnace_bottom.png",
	         "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png",
	         "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front.png"},
	paramtype2 = "facedir",
	groups = {cracky=2},
	legacy_facedir_simple = true,
	sounds = default.node_sound_stone_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("formspec", formspec)
		meta:set_string("infotext", machine_name)
		local inv = meta:get_inventory()
		inv:set_size("fuel", 1)
		inv:set_size("src", 2)
		inv:set_size("dst", 4)
	end,
	can_dig = technic.machine_can_dig,
	allow_metadata_inventory_put = technic.machine_inventory_put,
	allow_metadata_inventory_take = technic.machine_inventory_take,
	allow_metadata_inventory_move = technic.machine_inventory_move,
})

minetest.register_node("technic:coal_alloy_furnace_active", {
	description = machine_name,
	tiles = {"technic_coal_alloy_furnace_top.png",  "technic_coal_alloy_furnace_bottom.png",
	         "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png",
	         "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front_active.png"},
	paramtype2 = "facedir",
	light_source = 8,
	drop = "technic:coal_alloy_furnace",
	groups = {cracky=2, not_in_creative_inventory=1},
	legacy_facedir_simple = true,
	sounds = default.node_sound_stone_defaults(),
	can_dig = technic.machine_can_dig,
	allow_metadata_inventory_put = technic.machine_inventory_put,
	allow_metadata_inventory_take = technic.machine_inventory_take,
	allow_metadata_inventory_move = technic.machine_inventory_move,
})

minetest.register_abm({
	label = "Machines: run coal alloy furnace",
	nodenames = {"technic:coal_alloy_furnace", "technic:coal_alloy_furnace_active"},
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local meta = minetest.get_meta(pos)
		local inv    = meta:get_inventory()

		if inv:get_size("src") == 1 then -- Old furnace -> convert it
			inv:set_size("src", 2)
			inv:set_stack("src", 2, inv:get_stack("src2", 1))
			inv:set_size("src2", 0)
		end

		for i, name in pairs({
				"fuel_totaltime",
				"fuel_time",
				"src_totaltime",
				"src_time"}) do
			if not meta:get_float(name) then
				meta:set_float(name, 0.0)
			end
		end

		-- Get what to cook if anything
		local result = technic.get_recipe("alloy", inv:get_list("src"))

		local was_active = false

		if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
			was_active = true
			meta:set_int("fuel_time", meta:get_int("fuel_time") + 1)
			if result then
				meta:set_int("src_time", meta:get_int("src_time") + 1)
				if meta:get_int("src_time") >= result.time then
					meta:set_int("src_time", 0)
					local result_stack = ItemStack(result.output)
					if inv:room_for_item("dst", result_stack) then
						inv:set_list("src", result.new_input)
						inv:add_item("dst", result_stack)
					end
				end
			else
				meta:set_int("src_time", 0)
			end
		end

		if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
			local percent = math.floor(meta:get_float("fuel_time") /
					meta:get_float("fuel_totaltime") * 100)
			meta:set_string("infotext", S("%s Active"):format(machine_name).." ("..percent.."%)")
			technic.swap_node(pos, "technic:coal_alloy_furnace_active")
			meta:set_string("formspec",
					"size[8,9]"..
					"label[0,0;"..machine_name.."]"..
					"image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:"..
					(100 - percent)..":default_furnace_fire_fg.png]"..
					"list[current_name;fuel;2,3;1,1;]"..
					"list[current_name;src;2,1;2,1;]"..
					"list[current_name;dst;5,1;2,2;]"..
					"list[current_player;main;0,5;8,4;]"..
					"listring[current_name;dst]"..
					"listring[current_player;main]"..
					"listring[current_name;src]"..
					"listring[current_player;main]"..
					"listring[current_name;fuel]"..
					"listring[current_player;main]")
			return
		end

		local recipe = technic.get_recipe("alloy", inv:get_list("src"))

		if not recipe then
			if was_active then
				meta:set_string("infotext", S("%s is empty"):format(machine_name))
				technic.swap_node(pos, "technic:coal_alloy_furnace")
				meta:set_string("formspec", formspec)
			end
			return
		end

		-- Next take a hard look at the fuel situation
		local fuel = nil
		local afterfuel
		local fuellist = inv:get_list("fuel")

		if fuellist then
			fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
		end

		if fuel.time <= 0 then
			meta:set_string("infotext", S("%s Out Of Fuel"):format(machine_name))
			technic.swap_node(pos, "technic:coal_alloy_furnace")
			meta:set_string("formspec", formspec)
			return
		end

		meta:set_string("fuel_totaltime", fuel.time)
		meta:set_string("fuel_time", 0)

		inv:set_stack("fuel", 1, afterfuel.items[1])
	end,
})


code:
local S = technic.getter

if minetest.registered_nodes["default:furnace"].description == "Furnace" then
	minetest.override_item("default:furnace", { description = S("Fuel-Fired Furnace") })
end


code:
local S = technic.getter

local function deploy_node(inv, slot_name, pos, node, machine_node)
	if node.param2 > 3 then return end
	if node.name ~= "air" then
		if node.name == "ignore" or
		   node.name == "default:lava_source" or
		   node.name == "default:lava_flowing" or
		   node.name == "default:water_source" or
		   node.name == "default:water_flowing" then
			return
		end
		local drops = minetest.get_node_drops(node.name, "")
		local remove_to = false
		for i, item in ipairs(drops) do
			if not inv:room_for_item(slot_name, item) then
				remove_to = i - 1
				break
			end
			inv:add_item(slot_name, item)
		end
		if remove_to then
			for i = 1, remove_to do
				inv:remove_item(slot_name, drops[i])
			end
		else
			minetest.remove_node(pos)
		end
		return
	end
	if not inv:is_empty(slot_name) then
		local stack = inv:get_list(slot_name)[1]
		local def = stack:get_definition()
		if def.type == "node" then
			minetest.set_node(pos, {
				name = stack:get_name(),
				param2 = machine_node.param2
			})
			stack:take_item()
			inv:set_stack(slot_name, 1, stack)
		elseif def.type == "craft" then
			if def.on_place then
				-- Use pcall to avoid nil placer errors.
				-- TODO: Do without pcall.
				local ok, stk = pcall(def.on_place, stack, nil, {
					-- Fake pointed_thing
					type = "node",
					above = pos,
					under = {x=pos.x, y=pos.y-1, z=pos.z},
				})
				if ok then
					inv:set_stack(slot_name, 1, stk or stack)
					return
				end
			end
			minetest.item_place_object(stack, nil, {
				-- Fake pointed_thing
				type = "node",
				above = pos,
				under = pos,
			})
			inv:set_stack(slot_name, 1, nil)
		end
	end
end

minetest.register_craft({
	type = "shapeless",
	output = 'technic:constructor_mk1_off 1',
	recipe = {'technic:nodebreaker_off', 'technic:deployer_off'},

})
minetest.register_craft({
	type = "shapeless",
	output = 'technic:constructor_mk2_off 1',
	recipe = {'technic:constructor_mk1_off', 'technic:constructor_mk1_off'},

})

minetest.register_craft({
	type = "shapeless",
	output = 'technic:constructor_mk3_off 1',
	recipe = {'technic:constructor_mk2_off', 'technic:constructor_mk2_off'},

})

local function make_on(mark, length)
	return function(pos, node)
		local meta = minetest.get_meta(pos)
		local owner = meta:get_string("owner")
		local inv = meta:get_inventory()
		local dir = vector.new()
		if node.param2 == 3 then dir.x = 1 end
		if node.param2 == 2 then dir.z = 1 end
		if node.param2 == 1 then dir.x = -1 end
		if node.param2 == 0 then dir.z = -1 end

		local place_pos = vector.new(pos)

		if node.name == "technic:constructor_mk"..mark.."_off" then
			technic.swap_node(pos, "technic:constructor_mk"..mark.."_on")
			minetest.check_for_falling(pos)
			for i = 1, length do
				place_pos = vector.add(place_pos, dir)
				if not minetest.is_protected(place_pos, owner) then
					local place_node = minetest.get_node(place_pos)
					deploy_node(inv, "slot"..i, place_pos, place_node, node)
				end
			end
		end
	end
end

local function make_off(mark)
	return function(pos, node)
		if node.name == "technic:constructor_mk"..mark.."_on" then
			technic.swap_node(pos,"technic:constructor_mk"..mark.."_off")
			minetest.check_for_falling(pos)
		end
	end
end

local function allow_inventory_put(pos, listname, index, stack, player)
	if stack and minetest.get_item_group(stack:get_name(), "technic_constructor") == 1 then
		return 0
	end
	return technic.machine_inventory_put(pos, listname, index, stack, player)
end

local function make_constructor(mark, length)
	minetest.register_node("technic:constructor_mk"..mark.."_off", {
		description = S("Constructor Mk%d"):format(mark),
		tiles = {"technic_constructor_mk"..mark.."_top_off.png",
			"technic_constructor_mk"..mark.."_bottom_off.png",
			"technic_constructor_mk"..mark.."_side2_off.png",
			"technic_constructor_mk"..mark.."_side1_off.png",
			"technic_constructor_back.png",
			"technic_constructor_front_off.png"},
		paramtype2 = "facedir",
		groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
			mesecon = 2, technic_constructor = 1},
		mesecons = {effector = {action_on = make_on(mark, length)}},
		sounds = default.node_sound_stone_defaults(),
		after_place_node = function (pos, placer)
			minetest.get_meta(pos):set_string("owner", placer:get_player_name())
		end,
		on_construct = function(pos)
			local meta = minetest.get_meta(pos)
			local formspec = "size[8,9;]"..
				"label[0,0;"..S("Constructor Mk%d"):format(mark).."]"..
				"list[current_player;main;0,5;8,4;]"
			for i = 1, length do
				formspec = formspec
					.."label[5,"..(i - 1)..";"..S("Slot %d"):format(i).."]"
					.."list[current_name;slot"..i
						..";6,"..(i - 1)..";1,1;]"
			end
			meta:set_string("formspec", formspec)
			meta:set_string("infotext", S("Constructor Mk%d"):format(mark))
			local inv = meta:get_inventory()
			for i = 1, length do
				inv:set_size("slot"..i, 1)
			end
		end,
		can_dig = function(pos, player)
			local meta = minetest.get_meta(pos)
			local inv = meta:get_inventory()
			for i = 1, length do
				if not inv:is_empty("slot"..i) then
					return false
				end
			end
			return true
		end,
		allow_metadata_inventory_put = allow_inventory_put,
		allow_metadata_inventory_take = technic.machine_inventory_take,
		allow_metadata_inventory_move = technic.machine_inventory_move,
		on_rotate = screwdriver.rotate_simple
	})

	minetest.register_node("technic:constructor_mk"..mark.."_on", {
		tiles = {"technic_constructor_mk"..mark.."_top_on.png",
			"technic_constructor_mk"..mark.."_bottom_on.png",
			"technic_constructor_mk"..mark.."_side2_on.png",
			"technic_constructor_mk"..mark.."_side1_on.png",
			"technic_constructor_back.png",
			"technic_constructor_front_on.png"},
		paramtype2 = "facedir",
		drop = "technic:constructor_mk"..mark.."_off",
		groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
			mesecon=2, not_in_creative_inventory=1, technic_constructor=1},
		mesecons= {effector = {action_off = make_off(mark)}},
		sounds = default.node_sound_stone_defaults(),
		allow_metadata_inventory_put = allow_inventory_put,
		allow_metadata_inventory_take = technic.machine_inventory_take,
		allow_metadata_inventory_move = technic.machine_inventory_move,
		on_rotate = false
	})
end

make_constructor(1, 1)
make_constructor(2, 2)
make_constructor(3, 4)


code:
local S = technic.getter

local infinite_stacks = minetest.settings:get_bool("creative_mode")
	and minetest.get_modpath("unified_inventory") == nil

local frames_pos = {}

-- 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
		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
	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
	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
	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
	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
	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
	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
	end
	return false
end

local function table_empty(table)
	for _, __ in pairs(table) do
		return false
	end
	return true
end

local function add_table(table, toadd)
	local i = 1
	while true do
		local 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, 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
				return
			end
		end
	end

	for _, pos in ipairs(poslist) do
		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
			return
		end
	end

	local nodelist = {}
	for _, pos in ipairs(poslist) do
		local node = minetest.get_node(pos)
		local meta = minetest.get_meta(pos):to_table()
		local timer = minetest.get_node_timer(pos)
		nodelist[#nodelist + 1] = {
			oldpos = pos,
			pos = vector.add(pos, vect),
			node = node,
			meta = meta,
			timer = {
				timeout = timer:get_timeout(),
				elapsed = timer:get_elapsed()
			}
		}
	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
				add_table(objects, object)
			end
		end
	end

	for _, obj in ipairs(objects) do
		obj:set_pos(vector.add(obj:get_pos(), vect))
	end

	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)
		local timer = minetest.get_node_timer(npos)
		if n.timer.timeout ~= 0 or n.timer.elapsed ~= 0 then
			timer:set(n.timer.timeout, n.timer.elapsed)
		end
		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.remove_node(pos)
	end

	for _, callback in ipairs(mesecon.on_mvps_move) do
		callback(nodelist)
	end
end

local function is_supported_node(name)
	return string.find(name, "tube") and string.find(name, "pipeworks")
end

-- Frames
for xm = 0, 1 do
for xp = 0, 1 do
for ym = 0, 1 do
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 },

		{ -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, -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
	if ym == 0 then
		table.insert(nodeboxes, { -b, -a, -b, b, -b, b })
	end
	if xp == 0 then
		table.insert(nodeboxes, { b, b, b, a, -b, -b })
	end
	if xm == 0 then
		table.insert(nodeboxes, { -a, -b, -b, -b, b, b })
	end
	if zp == 0 then
		table.insert(nodeboxes, { -b, -b, b, b, b, a })
	end
	if zm == 0 then
		table.insert(nodeboxes, { -b, -b, -a, b, b, -b })
	end

	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 }
	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,
		drawtype = "nodebox",
		node_box = {
			type = "fixed",
			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)
			local l2 = {}
			local 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,

		on_punch = function(pos, node, puncher)
			local ppos = puncher:get_pos()
			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, pface - 7, pface - 7)))
			if pface <= 5 then
				nodename = string.sub(nodename, 1, pface - 7 - 1)..newstate..string.sub(nodename, pface - 7 + 1)
			else
				nodename = string.sub(nodename, 1, -2)..newstate
			end

			node.name = nodename
			minetest.set_node(pos, node)
		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
					local 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
		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 = vector.new(pos)
					if def.after_place_node(pos_copy, placer, itemstack) then
						take_item = false
					end
				end

				-- Run script hook
				local callback = nil
				for _, _ 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

				local 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
					return minetest.item_place_node(itemstack, placer, pointed_thing)
				end
			end
		end,
	})

end
end
end
end
end
end

minetest.register_entity("technic:frame_entity", {
	initial_properties = {
		physical = true,
		collisionbox = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
		visual = "wielditem",
		textures = {},
		visual_size = { x = 0.667, y = 0.667 },
	},

	node = {},

	set_node = function(self, node)
		self.node = node
		local pos = vector.round(self.object:getpos())
		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
		local prop = {
			is_visible = true,
			textures = { node.name },
		}
		self.object:set_properties(prop)
	end,

	get_staticdata = function(self)
		return self.node.name
	end,

	on_activate = function(self, staticdata)
		self.object:set_armor_groups({ immortal = 1 })
		self:set_node({ name = staticdata })
	end,

	dig = function(self)
		minetest.handle_node_drops(self.object:get_pos(), { ItemStack("technic:frame_111111") }, self.last_puncher)
		local pos = vector.round(self.object:get_pos())
		frames_pos[pos_to_string(pos)] = nil
		self.object:remove()
	end,

	on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
		local pos = self.object:get_pos()
		if self.damage_object == nil then
			self.damage_object = minetest.add_entity(pos, "technic:damage_entity")
			self.damage_object:get_luaentity().remaining_time = 0.25
			self.damage_object:get_luaentity().frame_object = self
			self.damage_object:get_luaentity().texture_index = 0
			self.damage_object:get_luaentity().texture_change_time = 0.15
		else
			self.damage_object:get_luaentity().remaining_time = 0.25
		end

		self.last_puncher = puncher
		local ppos = puncher:get_pos()
		local pvect = puncher:get_look_dir()
		local pface = get_face(pos, ppos, pvect)
		if pface == nil then return end
		local nodename = self.node.name
		local newstate = tostring(1 - tonumber(string.sub(nodename, pface - 7, pface - 7)))

		if pface <= 5 then
			nodename = string.sub(nodename, 1, pface - 7 - 1)..newstate..string.sub(nodename, pface - 7 + 1)
		else
			nodename = string.sub(nodename, 1, -2)..newstate
		end

		self.node.name = nodename
		self:set_node(self.node)
	end,

	on_rightclick = function(self, clicker)
		local pos = self.object:get_pos()
		local ppos = clicker:get_pos()
		local pvect = clicker:get_look_dir()
		local pface = get_face(pos, ppos, pvect)

		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 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 itemstack = clicker:get_wielded_item()
		local itemdef = minetest.registered_items[itemstack:get_name()]

		if itemdef ~= nil then
			itemdef.on_place(itemstack, clicker, pointed_thing)
		end
	end,
})

local crack = "crack_anylength.png^[verticalframe:5:0"
minetest.register_entity("technic:damage_entity", {
	initial_properties = {
		visual = "cube",
		visual_size = { x = 1.01, y = 1.01 },
		textures = { crack, crack, crack, crack, crack, crack },
		collisionbox = { 0, 0, 0, 0, 0, 0 },
		physical = false,
	},
	on_step = function(self, dtime)
		if self.remaining_time == nil then
			self.object:remove()
			self.frame_object.damage_object = nil
		end
		self.remaining_time = self.remaining_time - dtime
		if self.remaining_time < 0 then
			self.object:remove()
			self.frame_object.damage_object = nil
		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 } })
		end
	end,
})

mesecon.register_mvps_unmov("technic:frame_entity")
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
		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:set_pos(t.pos)
				end
			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
		end
	end
end)

-- Frame motor
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)
				connected(pos1, c, adj)
			end
		end
	end
end

local function get_connected_nodes(pos)
	local 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
	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)
	end

	minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime())
end

minetest.register_node("technic:frame_motor", {
	description = S("Frame 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 = frame_motor_on } },

	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.get_meta(pos)
		meta: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]
		return dir2.x ~= -dir.x or dir2.y ~= -dir.y or dir2.z ~= -dir.z
	end
})

-- Templates
local function template_connected(pos, c, connectors)
	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 nodename = minetest.get_node(pos1).name
		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
				template_connected(pos1, c, connectors)
				if nodename == "technic:template_connector" then
					connectors[#connectors + 1] = pos1
				end
			end
		end
	end
end

local function get_templates(pos)
	local c = { pos }
	local connectors
	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", "")
	technic.swap_node(pos, new)
	meta = minetest.get_meta(pos)
	meta:set_string("saved_node", saved_node)
end

local function save_node(pos)
	local node = minetest.get_node(pos)
	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 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

	node.meta = meta0
	minetest.set_node(pos, { name = "technic:template" })
	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
	end
	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
	c = minetest.deserialize(c)

	for _, vect in ipairs(c) do
		local pos1 = vector.add(pos, vect)
		local saved_node = save_node(pos1)
		local meta1 = minetest.get_meta(pos1)
		if saved_node ~= nil then
			meta1:set_string("saved_node", minetest.serialize(saved_node))
		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

local function template_drops(pos, node, oldmeta, digger)
	local c = oldmeta.fields.connected
	local cc = oldmeta.fields.connectors_connected
	local drops

	if c == "" or c == nil then
		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
				local stack_max = 99
				local num = #minetest.deserialize(c)
				drops = {}
				while num > stack_max do
					drops[#drops + 1] = "technic:template "..stack_max
					num = num - stack_max
				end
				drops[#drops + 1] = "technic:template "..num
			end
		end
	end

	minetest.handle_node_drops(pos, drops, digger)
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", {
	description = S("Template"),
	tiles = { "technic_mv_cable.png" },
	drop = "",
	groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2 },
	on_destruct = template_on_destruct,
	after_dig_node = template_drops,
	on_punch = function(pos, node, puncher)
		swap_template(pos, "technic:template_disabled")
	end
})

minetest.register_node("technic:template_disabled", {
	description = S("Template"),
	tiles = { "technic_hv_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)
	local _ = minetest.get_meta(pos)
		swap_template(pos, "technic:template_connector")
	end
})

minetest.register_node("technic:template_connector", {
	description = S("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
})

minetest.register_craftitem("technic:template_replacer", {
	description = S("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)
		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
})

minetest.register_tool("technic:template_tool", {
	description = S("Template Tool"),
	inventory_image = "technic_template_tool.png",
	on_use = function(itemstack, puncher, pointed_thing)
		local pos = pointed_thing.under
		if pos == nil or minetest.is_protected and minetest.is_protected(pos, puncher:get_player_name()) then
			return nil
		end
		local node = minetest.get_node(pos)
		if node.name ~= "technic:template" and node.name ~= "technic:template_connector" then return end
		local meta = minetest.get_meta(pos)
		local c2 = meta:get_string("connected")
		if c2 ~= "" then
			expand_template(pos)
		else
			compress_templates(pos)
		end

	end
})

-- 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 = vector.add(pos, vect)
		local nodename = minetest.get_node(pos1).name
		if not(pos_in_list(c, pos1)) and nodename ~= "air" then
			c[#c + 1] = pos1
		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)
	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 nnode.name == "technic:template" then
		local connected_nodes = get_template_nodes(nnodepos)
		move_nodes_vect(connected_nodes, dir, pos, owner)
	end
	minetest.get_meta(vector.add(pos, dir)):set_int("last_moved", minetest.get_gametime())
end

minetest.register_node("technic:template_motor", {
	description = S("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 } },
	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", placer:get_player_name())
	end,
})

-- Crafts
minetest.register_craft({
	output = 'technic:frame_111111',
	recipe = {
		{ '',              'default:stick',       '' },
		{ 'default:stick', 'basic_materials:brass_ingot', 'default:stick' },
		{ '',              'default:stick',       '' },
	}
})

minetest.register_craft({
	output = 'technic:frame_motor',
	recipe = {
		{ '',                                  'technic:frame_111111', '' },
		{ 'group:mesecon_conductor_craftable', 'basic_materials:motor',        'group:mesecon_conductor_craftable' },
		{ '',                                  'technic:frame_111111', '' },
	}
})

minetest.register_craft({
	output = 'technic:template 10',
	recipe = {
		{ '',                    'basic_materials:brass_ingot',  '' },
		{ 'basic_materials:brass_ingot', 'default:mese_crystal', 'basic_materials:brass_ingot' },
		{ '',                    'basic_materials:brass_ingot',  '' },
	}
})

minetest.register_craft({
	output = 'technic:template_replacer',
	recipe = { { 'technic:template' } }
})

minetest.register_craft({
	output = 'technic:template',
	recipe = { { 'technic:template_replacer' } }
})

minetest.register_craft({
	output = 'technic:template_motor',
	recipe = {
		{ '',                                  'technic:template', '' },
		{ 'group:mesecon_conductor_craftable', 'basic_materials:motor',    'group:mesecon_conductor_craftable' },
		{ '',                                  'technic:template', '' },
	}
})

minetest.register_craft({
	output = 'technic:template_tool',
	recipe = {
		{ '',                     'technic:template', '' },
		{ 'default:mese_crystal', 'default:stick',    'default:mese_crystal' },
		{ '',                     'default:stick',    '' },
	}
})


code:
local path = technic.modpath.."/machines/other"

-- mesecons and tubes related
dofile(path.."/injector.lua")
dofile(path.."/constructor.lua")

if technic.config:get_bool("enable_frames") and minetest.get_modpath("mesecons_mvps") ~= nil then
	dofile(path.."/frames.lua")
end

-- Coal-powered machines
dofile(path.."/coal_alloy_furnace.lua")
dofile(path.."/coal_furnace.lua")

dofile(path.."/anchor.lua")


code:
local S = technic.getter

local fs_helpers = pipeworks.fs_helpers

local tube_entry = "^pipeworks_tube_connection_metallic.png"

local param2_to_under = {
	[0] = {x= 0,y=-1,z= 0}, [1] = {x= 0,y= 0,z=-1},
	[2] = {x= 0,y= 0,z= 1}, [3] = {x=-1,y= 0,z= 0},
	[4] = {x= 1,y= 0,z= 0}, [5] = {x= 0,y= 1,z= 0}
}

local function inject_items(pos, dir)
		local meta=minetest.get_meta(pos)
		local inv = meta:get_inventory()
		local mode=meta:get_string("mode")
		if mode=="single items" then
			local i=0
			for _,stack in ipairs(inv:get_list("main")) do
			i=i+1
				if stack then
				local item0=stack:to_table()
				if item0 then
					item0["count"] = 1
					technic.tube_inject_item(pos, pos, dir, item0)
					stack:take_item(1)
					inv:set_stack("main", i, stack)
					return
					end
				end
			end
		end
		if mode=="whole stacks" then
			local i=0
			for _,stack in ipairs(inv:get_list("main")) do
			i=i+1
				if stack then
				local item0=stack:to_table()
				if item0 then
					technic.tube_inject_item(pos, pos, dir, item0)
					stack:clear()
					inv:set_stack("main", i, stack)
					return
					end
				end
			end
		end

end

minetest.register_craft({
	output = 'technic:injector 1',
	recipe = {
		{'', 'technic:control_logic_unit',''},
		{'', 'default:chest',''},
		{'', 'pipeworks:tube_1',''},
	}
})

local function set_injector_formspec(meta)
	local is_stack = meta:get_string("mode") == "whole stacks"
	meta:set_string("formspec",
		"size[8,9;]"..
		"item_image[0,0;1,1;technic:injector]"..
		"label[1,0;"..S("Self-Contained Injector").."]"..
		(is_stack and
			"button[0,1;2,1;mode_item;"..S("Stackwise").."]" or
			"button[0,1;2,1;mode_stack;"..S("Itemwise").."]")..
		"list[current_name;main;0,2;8,2;]"..
		"list[current_player;main;0,5;8,4;]"..
		"listring[]"..
		fs_helpers.cycling_button(
			meta,
			pipeworks.button_base,
			"splitstacks",
			{
				pipeworks.button_off,
				pipeworks.button_on
			}
		)..pipeworks.button_label
	)
end

minetest.register_node("technic:injector", {
	description = S("Self-Contained Injector"),
	tiles = {
		"technic_injector_top.png"..tube_entry,
		"technic_injector_bottom.png",
		"technic_injector_side.png"..tube_entry,
		"technic_injector_side.png"..tube_entry,
		"technic_injector_side.png"..tube_entry,
		"technic_injector_side.png"
	},
	paramtype2 = "facedir",
	groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1},
	tube = {
		can_insert = function(pos, node, stack, direction)
			local meta = minetest.get_meta(pos)
			local inv = meta:get_inventory()
			if meta:get_int("splitstacks") == 1 then
				stack = stack:peek_item(1)
			end
			return inv:room_for_item("main", stack)
		end,
		insert_object = function(pos, node, stack, direction)
			return minetest.get_meta(pos):get_inventory():add_item("main", stack)
		end,
		connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
	},
	sounds = default.node_sound_wood_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("infotext", S("Self-Contained Injector"))
		local inv = meta:get_inventory()
		inv:set_size("main", 8*2)
		meta:set_string("mode","single items")
		set_injector_formspec(meta)
	end,
	can_dig = function(pos,player)
		local meta = minetest.get_meta(pos);
		local inv = meta:get_inventory()
		return inv:is_empty("main")
	end,
	on_receive_fields = function(pos, formanme, fields, sender)
		local meta = minetest.get_meta(pos)
		if fields.mode_item then meta:set_string("mode", "single items") end
		if fields.mode_stack then meta:set_string("mode", "whole stacks") end

		if fields["fs_helpers_cycling:0:splitstacks"]
		  or fields["fs_helpers_cycling:1:splitstacks"] then
			if not pipeworks.may_configure(pos, sender) then return end
			fs_helpers.on_receive_fields(pos, fields)
		end
		set_injector_formspec(meta)
	end,
	allow_metadata_inventory_put = technic.machine_inventory_put,
	allow_metadata_inventory_take = technic.machine_inventory_take,
	allow_metadata_inventory_move = technic.machine_inventory_move,
	after_place_node = pipeworks.after_place,
	after_dig_node = pipeworks.after_dig
})

minetest.register_abm({
	label = "Machines: run injector",
	nodenames = {"technic:injector"},
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local dir = param2_to_under[math.floor(node.param2/4)]
		local pos1 = vector.add(pos, dir)
		local node1 = minetest.get_node(pos1)
		if minetest.get_item_group(node1.name, "tubedevice") > 0 then
			inject_items(pos, dir)
		end
	end,
})


code:
-- POWER MONITOR
-- The power monitor can be used to monitor how much power is available on a network,
-- similarly to the old "slave" switching stations.

local S = technic.getter

local cable_entry = "^technic_cable_connection_overlay.png"

-- return the position of the associated switching station or nil
local function get_swpos(pos)
	local network_hash = technic.cables[minetest.hash_node_position(pos)]
	local network = network_hash and minetest.get_position_from_hash(network_hash)
	local swpos = network and {x=network.x,y=network.y+1,z=network.z}
	local is_powermonitor = swpos and minetest.get_node(swpos).name == "technic:switching_station"
	return is_powermonitor and swpos or nil
end

minetest.register_craft({
	output = "technic:power_monitor",
	recipe = {
		{"",                 "",                       ""},
		{"",                 "technic:machine_casing", "default:copper_ingot"},
		{"technic:lv_cable", "technic:lv_cable",       "technic:lv_cable"}
	}
})

minetest.register_node("technic:power_monitor",{
	description = S("Power Monitor"),
	tiles  = {
		"technic_power_monitor_sides.png",
		"technic_power_monitor_sides.png"..cable_entry,
		"technic_power_monitor_sides.png",
		"technic_power_monitor_sides.png",
		"technic_power_monitor_sides.png"..cable_entry,
		"technic_power_monitor_front.png"
	},
	paramtype2 = "facedir",
	groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_all_tiers=1, technic_machine=1},
	connect_sides = {"bottom", "back"},
	sounds = default.node_sound_wood_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("infotext", S("Power Monitor"))
		meta:set_string("channel", "power_monitor"..minetest.pos_to_string(pos))
		meta:set_string("formspec", "field[channel;Channel;${channel}]")
	end,
	on_receive_fields = function(pos, formname, fields, sender)
		if not fields.channel then
			return
		end
		local plname = sender:get_player_name()
    if minetest.is_protected(pos, plname) and not minetest.check_player_privs(sender, "protection_bypass") then
			minetest.record_protection_violation(pos, plname)
			return
		end
		local meta = minetest.get_meta(pos)
		meta:set_string("channel", fields.channel)
	end,
	digiline = {
		receptor = {
			rules = technic.digilines.rules,
			action = function() end
		},
		effector = {
			rules = technic.digilines.rules,
			action = function(pos, node, channel, msg)
				if msg ~= "GET" and msg ~= "get" then
					return
				end
				local meta = minetest.get_meta(pos)
				if channel ~= meta:get_string("channel") then
					return
				end

				local sw_pos = get_swpos(pos)
				if not sw_pos then
					return
				end

				local sw_meta = minetest.get_meta(sw_pos)
				digilines.receptor_send(pos, technic.digilines.rules, channel, {
					supply = sw_meta:get_int("supply"),
					demand = sw_meta:get_int("demand"),
					lag = sw_meta:get_int("lag"),
					battery_count = sw_meta:get_int("battery_count"),
					battery_charge = sw_meta:get_int("battery_charge"),
					battery_charge_max = sw_meta:get_int("battery_charge_max"),
				})
			end
		},
	},
})

minetest.register_abm({
	nodenames = {"technic:power_monitor"},
	label = "Machines: run power monitor",
	interval   = 1,
	chance     = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local meta = minetest.get_meta(pos)
		local sw_pos = get_swpos(pos)
		local timeout = 0
		for tier in pairs(technic.machines) do
			timeout = math.max(meta:get_int(tier.."_EU_timeout"),timeout)
		end
		if timeout > 0 and sw_pos then
			local sw_meta = minetest.get_meta(sw_pos)
			local supply = sw_meta:get_int("supply")
			local demand = sw_meta:get_int("demand")
			meta:set_string("infotext",
					S("Power Monitor. Supply: @1 Demand: @2",
					technic.EU_string(supply), technic.EU_string(demand)))
		else
			meta:set_string("infotext",S("Power Monitor Has No Network"))
		end
	end,
})

for tier in pairs(technic.machines) do
	-- RE in order to use the "timeout" functions, although it consumes 0 power
	technic.register_machine(tier, "technic:power_monitor", "RE")
end


code:
local S = technic.getter

function technic.insert_object_unique_stack(pos, node, incoming_stack, direction)
	local meta = minetest.get_meta(pos)
	local inv = meta:get_inventory()
	local incoming_name = incoming_stack:get_name()
	local stack_index = nil
	for inv_index, inv_stack in pairs(inv:get_list("src")) do
		if inv_stack:get_name() == incoming_name then
			stack_index = inv_index
			break
		end
	end
	if stack_index == nil then
		return inv:add_item("src", incoming_stack)
	end
	local present_stack = inv:get_stack("src", stack_index)
	local leftover = present_stack:add_item(incoming_stack)
	inv:set_stack("src", stack_index, present_stack)
	return leftover
end

function technic.can_insert_unique_stack(pos, node, incoming_stack, direction)
	local meta = minetest.get_meta(pos)
	local inv = meta:get_inventory()
	local incoming_name = incoming_stack:get_name()
	if meta:get_int("splitstacks") == 0 then
		-- avoid looping second time with inv:contains_item("src", incoming_stack)
		for _, inv_stack in pairs(inv:get_list("src")) do
			if inv_stack:get_name() == incoming_name then
				return inv_stack:item_fits(incoming_stack)
			end
		end
	end

	return technic.default_can_insert(pos, node, incoming_stack, direction)
end

function technic.register_alloy_furnace(data)
	data.typename = "alloy"
	data.machine_name = "alloy_furnace"
	data.machine_desc = S("%s Alloy Furnace")

	data.insert_object = technic.insert_object_unique_stack
	data.can_insert = technic.can_insert_unique_stack

	technic.register_base_machine(data)
end


code:
local S = technic.getter

technic.register_recipe_type("alloy", {
	description = S("Alloying"),
	input_size = 2,
})

function technic.register_alloy_recipe(data)
	data.time = data.time or 6
	technic.register_recipe("alloy", data)
end

local recipes = {
	{"technic:copper_dust 7",         "technic:tin_dust",           "technic:bronze_dust 8", 12},
	{"default:copper_ingot 7",        "default:tin_ingot",          "default:bronze_ingot 8", 12},
	{"technic:wrought_iron_dust 2",   "technic:coal_dust",          "technic:carbon_steel_dust 2", 6},
	{"technic:wrought_iron_ingot 2",  "technic:coal_dust",          "technic:carbon_steel_ingot 2", 6},
	{"technic:carbon_steel_dust 2",   "technic:coal_dust",          "technic:cast_iron_dust 2", 6},
	{"technic:carbon_steel_ingot 2",  "technic:coal_dust",          "technic:cast_iron_ingot 2", 6},
	{"technic:carbon_steel_dust 4",   "technic:chromium_dust",      "technic:stainless_steel_dust 5", 7.5},
	{"technic:carbon_steel_ingot 4",  "technic:chromium_ingot",     "technic:stainless_steel_ingot 5", 7.5},
	{"technic:copper_dust 2",         "technic:zinc_dust",          "technic:brass_dust 3"},
	{"default:copper_ingot 2",        "technic:zinc_ingot",         "basic_materials:brass_ingot 3"},
	{"default:sand 2",                "technic:coal_dust 2",        "technic:silicon_wafer"},
	{"technic:silicon_wafer",         "technic:gold_dust",          "technic:doped_silicon_wafer"},
	-- from https://en.wikipedia.org/wiki/Carbon_black
	-- The highest volume use of carbon black is as a reinforcing filler in rubber products, especially tires.
	-- "[Compounding a] pure gum vulcanizate … with 50% of its weight of carbon black improves its tensile strength and wear resistance …"
	{"technic:raw_latex 4",           "technic:coal_dust 2",        "technic:rubber 6", 2},
	{"default:ice", 		  	      "bucket:bucket_empty",        "bucket:bucket_water", 1 },
	{"default:obsidian",			  "bucket:bucket_empty",        "bucket:bucket_lava", 1 },
}

if minetest.get_modpath("ethereal") then
	table.insert(recipes, {"default:clay", "dye:red", "bakedclay:red"})
	table.insert(recipes, {"default:clay", "dye:orange", "bakedclay:orange"})
	table.insert(recipes, {"default:clay", "dye:grey", "bakedclay:grey"})
end

for _, data in pairs(recipes) do
	technic.register_alloy_recipe({input = {data[1], data[2]}, output = data[3], time = data[4]})
end