Added components
This commit is contained in:
parent
5873ebdd77
commit
3d633619c4
@ -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()
|
-- Removes a node without calling on on_destruct()
|
||||||
-- We use this to mess with bed nodes without causing unwanted recursion.
|
-- We use this to mess with bed nodes without causing unwanted recursion.
|
||||||
local function remove_no_destruct(pos)
|
local function remove_no_destruct(pos)
|
||||||
@ -55,97 +7,111 @@ local function remove_no_destruct(pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Everything is described as if dir=0
|
-- Everything is described as if dir=0
|
||||||
|
-- z+
|
||||||
|
-- /|\ w
|
||||||
|
-- | XXX h
|
||||||
|
-- +−−−−−−−−−−−> x+
|
||||||
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
|
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
|
||||||
components = {
|
components = {}
|
||||||
"and" = {2,3,{{"in_1","in_2"},{"out"},{},{}}},
|
components["and"] = {2,3,{{"in_2",nil,"in_1"},{nil,"out",nil},nil,nil}}
|
||||||
"switch" = {2,1,{{"in"},{"out"},{"activate"},{}}},
|
components["switch"] = {2,1,{{"in"},{"out"},{"activate",nil},nil}}
|
||||||
"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["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["block"] = {name,x,y}
|
||||||
componentsblocks = {}
|
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)
|
||||||
drawtype = "block",
|
minetest.register_node(nodename, {
|
||||||
tiles = {"component.png"},
|
drawtype = "block",
|
||||||
groups = {circuitry = 1,dig_immediate = 3},
|
tiles = {
|
||||||
on_place = function(itemstack, placer, pointed_thing)
|
"component_"..name..".png",
|
||||||
local w = 3
|
"component.png",
|
||||||
local h = 5
|
((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},
|
||||||
|
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 under = pointed_thing.under
|
||||||
local node = minetest.get_node(under)
|
local node = minetest.get_node(under)
|
||||||
local udef = minetest.registered_nodes[node.name]
|
local udef = minetest.registered_nodes[node.name]
|
||||||
|
|
||||||
-- Rightclick is prioritary
|
-- Rightclick is prioritary
|
||||||
if udef and udef.on_rightclick and
|
if udef and udef.on_rightclick and
|
||||||
not (placer and placer:is_player() and
|
not (placer and placer:is_player() and
|
||||||
placer:get_player_control().sneak) then
|
placer:get_player_control().sneak) then
|
||||||
return udef.on_rightclick(under, node, placer, itemstack,
|
return udef.on_rightclick(under, node, placer, itemstack,
|
||||||
pointed_thing) or itemstack
|
pointed_thing) or itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Pos will be the head location
|
-- Pos will be the head location
|
||||||
local pos
|
local pos
|
||||||
if udef and udef.buildable_to then
|
if udef and udef.buildable_to then
|
||||||
pos = under
|
pos = under
|
||||||
else
|
else
|
||||||
pos = pointed_thing.above
|
pos = pointed_thing.above
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local dir8 = logikraft.vecTo8dir(placer:get_look_dir())
|
||||||
|
|
||||||
local player_name = placer and placer:get_player_name() or ""
|
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
|
||||||
|
end
|
||||||
|
|
||||||
local dir = placer and placer:get_look_dir() and
|
local dir4 = logikraft.dir8to4(dir8)
|
||||||
vecTo8dir(placer:get_look_dir()) or 0
|
local invw = dir8 % 2 == 0
|
||||||
|
|
||||||
for i = 0,w*h-1 do
|
-- If we reached this point, we can build the component
|
||||||
local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w)
|
for i = 1,w do
|
||||||
local node_def = minetest.registered_nodes[minetest.get_node(loc).name]
|
for j = 1,h do
|
||||||
if not node_def or not node_def.buildable_to then
|
local loc = logikraft.addToPos8dir(dir8,pos,invw and (w-i) or (i-1),j-1)
|
||||||
-- Then we cannot build here
|
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
|
return itemstack
|
||||||
|
end or nil,
|
||||||
|
on_destruct = function(pos)
|
||||||
|
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
dir4 = node.param2
|
||||||
|
|
||||||
|
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
|
end
|
||||||
end
|
})
|
||||||
-- If we reached this point, we can build the component
|
componentsblocks["nodename"] = {name,x,y}
|
||||||
minetest.set_node(pos, {name = "logikraft:component_head", param2 = dir})
|
end
|
||||||
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
|
end
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_node("logikraft:component_tail", {
|
minetest.register_alias("logikraft:component_"..name, "logikraft:component_"..name .. "_1_1")
|
||||||
drawtype = "block",
|
end
|
||||||
paramtype2 = "4dir",
|
|
||||||
groups = {circuitry = 1},
|
|
||||||
-- Which direction is the one of the head block
|
|
||||||
tiles = {"component.png^[colorizehsl:120"}
|
|
||||||
})
|
|
||||||
BIN
textures/component_and.png
Normal file
BIN
textures/component_and.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.4 KiB |
BIN
textures/component_demux.png
Normal file
BIN
textures/component_demux.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
textures/component_port.png
Normal file
BIN
textures/component_port.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
textures/component_switch.png
Normal file
BIN
textures/component_switch.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
39
utils.lua
39
utils.lua
@ -80,3 +80,42 @@ function logikraft.lookingAtNbIndex(user,under)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user