Added components

This commit is contained in:
Samy Avrillon 2024-05-18 14:47:59 +02:00
parent 5873ebdd77
commit 3d633619c4
No known key found for this signature in database
GPG Key ID: 3E163E7603E330FE
6 changed files with 133 additions and 128 deletions

View File

@ -1,51 +1,3 @@
--[[
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)
@ -55,25 +7,45 @@ local function remove_no_destruct(pos)
end
-- Everything is described as if dir=0
-- z+
-- /|\ w
-- | XXX h
-- +> x+
-- {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"},{},{}}},
}
components = {}
components["and"] = {2,3,{{"in_2",nil,"in_1"},{nil,"out",nil},nil,nil}}
components["switch"] = {2,1,{{"in"},{"out"},{"activate",nil},nil}}
components["demux"] = {2,8,{{nil,nil,nil,nil,nil,"in_3","in_2","in_1"},{"out_8","out_7","out_6","out_5","out_4","out_3","out_2","out_1"},nil,nil}}
-- componentsblocks["block"] = {name,x,y}
componentsblocks = {}
for name,cmp in pairs(components)
do
local w = cmp[1]
local h = cmp[2]
local cables = cmp[3]
for x=1,w do
for y=1,h do
minetest.register_node("logikraft:component_head", {
local nodename = "logikraft:component_"..name.."_"..tostring(x).."_"..tostring(y)
minetest.register_node(nodename, {
drawtype = "block",
tiles = {"component.png"},
tiles = {
"component_"..name..".png",
"component.png",
((x==w) and (cables[2] and cables[2][y] and "component_port.png" or "component.png") or "component.png"), -- x+
((x==1) and (cables[1] and cables[1][y] and "component_port.png" or "component.png") or "component.png"), -- x-
((y==h) and (cables[3] and cables[3][x] and "component_port.png" or "component.png") or "component.png"), -- z+
((y==1) and (cables[4] and cables[4][x] and "component_port.png" or "component.png") or "component.png"), -- z-
},
groups = {circuitry = 1,dig_immediate = 3},
on_place = function(itemstack, placer, pointed_thing)
local w = 3
local h = 5
paramtype2 = "4dir",
inventory_image = (x == 1 and y == 1 and "component_"..name..".png") or nil,
drop = "logikraft:component_"..name.."_1_1",
on_place = (x == 1 and y == 1) and function(itemstack, placer, pointed_thing)
local under = pointed_thing.under
local node = minetest.get_node(under)
@ -95,57 +67,51 @@ minetest.register_node("logikraft:component_head", {
pos = pointed_thing.above
end
local dir8 = logikraft.vecTo8dir(placer:get_look_dir())
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)
for i = 1,w do
for j = 1,h do
local loc = logikraft.addToPos8dir(dir8,pos,i-1,j-1)
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
local dir4 = logikraft.dir8to4(dir8)
local invw = dir8 % 2 == 0
-- If we reached this point, we can build the component
for i = 1,w do
for j = 1,h do
local loc = logikraft.addToPos8dir(dir8,pos,invw and (w-i) or (i-1),j-1)
minetest.set_node(loc, {name = "logikraft:component_"..name.."_"..tostring(i).."_"..tostring(j), param2 = dir4})
end
end
if not minetest.is_creative_enabled(placer) then
itemstack:take_item()
end
return itemstack
end,
end or nil,
on_destruct = function(pos)
local w = 3
local h = 5
local node = minetest.get_node(pos)
dir4 = node.param2
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
for i = 1,w do
for j = 1,h do
local loc = logikraft.addToPos4dir(dir4,pos,i-x,j-y)
remove_no_destruct(loc)
end
end
end
})
componentsblocks["nodename"] = {name,x,y}
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"}
})
minetest.register_alias("logikraft:component_"..name, "logikraft:component_"..name .. "_1_1")
end

BIN
textures/component_and.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_port.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@ -80,3 +80,42 @@ function logikraft.lookingAtNbIndex(user,under)
end
return 0
end
function logikraft.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
function logikraft.dir8to4(dir)
return math.floor(dir / 2)
end
function logikraft.addToPos8dir(dir,pos,w,h)
local x = pos[1]
local y = pos[2]
local z = pos[3]
return
dir == 0 and {x = x-w, y = y, z = z+h} or
dir == 1 and {x = x+w, y = y, z = z+h} or
dir == 2 and {x = x+h, y = y, z = z+w} or
dir == 3 and {x = x+h, y = y, z = z-w} or
dir == 4 and {x = x+w, y = y, z = z-h} or
dir == 5 and {x = x-w, y = y, z = z-h} or
dir == 6 and {x = x-h, y = y, z = z-w} or
dir == 7 and {x = x-h, y = y, z = z+w} or
pos -- Should not happen
end
function logikraft.addToPos4dir(dir,pos,w,h)
return logikraft.addToPos8dir(dir*2+1,pos,w,h)
end