Logikraft/blocks/component.lua

151 lines
4.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--[[
Component 2x3:
z+
/|\
\ 22 | 11 /
\ 22 | 11 /
\ 22 | 11 /
333 \ | / 000
333 \|/ 000
+> x+
444 /|\ 777
444 / | \ 777
/ 55 | 66 \
/ 55 | 66 \
/ 55 | 66 \
--]]
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
-- Everything is described as if dir=0
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
components = {
"and" = {2,3,{{"in_1","in_2"},{"out"},{},{}}},
"switch" = {2,1,{{"in"},{"out"},{"activate"},{}}},
"demux" = {2,8,{{"in_1","in_2","in_3"},{"out_1","out_2","out_3","out_4","out_5","out_6","out_7","out_8"},{},{}}},
}
-- componentsblocks["block"] = {name,x,y}
componentsblocks = {}
minetest.register_node("logikraft: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 = "logikraft: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 = "logikraft: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 logikraft: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 == "logikraft:component_tail" and onode.param2 == dir
then
remove_no_destruct(loc)
end
end
end
})
minetest.register_node("logikraft:component_tail", {
drawtype = "block",
paramtype2 = "4dir",
groups = {circuitry = 1},
-- Which direction is the one of the head block
tiles = {"component.png^[colorizehsl:120"}
})