Logikraft/blocks/component.lua

126 lines
3.9 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--[[
0 : x+,z+
1 : TODO finish the comment
--]]
local function vecTo8dir(vec)
local x = vec[1]
local z = vec[3]
return
x>=0 and z>=0 and z<=x and 0 or
x>=0 and z>=0 and z>x and 1 or
x<0 and z>=0 and -z<=x and 2 or
x<0 and z>=0 and -z>x and 3 or
x<0 and z<0 and z>=x and 4 or
x<0 and z<0 and z<x and 5 or
x>=0 and z<0 and -z>=x and 6 or
x>=0 and z<0 and -z<x and 7 or
0 -- Should not happen
end
local function addToPos8dir(dir,pos,w,h)
local x = pos[1]
local y = pos[2]
local z = pos[3]
return
dir == 0 and {x = x+h, y = y, z = z+w} or
dir == 1 and {x = x+w, y = y, z = z+h} or
dir == 2 and {x = x-w, y = y, z = z+h} or
dir == 3 and {x = x-h, y = y, z = z+w} or
dir == 4 and {x = x-h, y = y, z = z-w} or
dir == 5 and {x = x-w, y = y, z = z-h} or
dir == 6 and {x = x+w, y = y, z = z-h} or
dir == 7 and {x = x+h, y = y, z = z-w} or
pos -- Should not happen
end
-- Removes a node without calling on on_destruct()
-- We use this to mess with bed nodes without causing unwanted recursion.
local function remove_no_destruct(pos)
minetest.swap_node(pos, {name = "air"})
minetest.remove_node(pos) -- Now clear the meta
minetest.check_for_falling(pos)
end
minetest.register_node("circuits:component_head", {
drawtype = "block",
tiles = {"component.png"},
groups = {circuitry = 1,dig_immediate = 3},
on_place = function(itemstack, placer, pointed_thing)
local w = 3
local h = 5
local under = pointed_thing.under
local node = minetest.get_node(under)
local udef = minetest.registered_nodes[node.name]
-- Rightclick is prioritary
if udef and udef.on_rightclick and
not (placer and placer:is_player() and
placer:get_player_control().sneak) then
return udef.on_rightclick(under, node, placer, itemstack,
pointed_thing) or itemstack
end
-- Pos will be the head location
local pos
if udef and udef.buildable_to then
pos = under
else
pos = pointed_thing.above
end
local player_name = placer and placer:get_player_name() or ""
local dir = placer and placer:get_look_dir() and
vecTo8dir(placer:get_look_dir()) or 0
for i = 0,w*h-1 do
local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w)
local node_def = minetest.registered_nodes[minetest.get_node(loc).name]
if not node_def or not node_def.buildable_to then
-- Then we cannot build here
return itemstack
end
end
-- If we reached this point, we can build the component
minetest.set_node(pos, {name = "circuits:component_head", param2 = dir})
for i = 1,w*h-1 do
local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w)
minetest.set_node(loc, {name = "circuits:component_tail", param2 = dir})
end
if not minetest.is_creative_enabled(player_name) then
itemstack:take_item()
end
return itemstack
end,
on_destruct = function(pos)
local w = 3
local h = 5
local node = minetest.get_node(pos)
dir = node.param2
remove_no_destruct(pos) -- Should be circuits:component_head
for i = 1,w*h-1 do
local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w)
local onode = minetest.get_node(loc)
if onode.name == "circuits:component_tail" and onode.param2 == dir
then
remove_no_destruct(loc)
end
end
end
})
minetest.register_node("circuits:component_tail", {
drawtype = "block",
paramtype2 = "4dir",
groups = {circuitry = 1},
-- Which direction is the one of the head block
tiles = {"component.png^[colorizehsl:120"}
})