Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbb30f0634
|
||
|
|
2c8f51f8ba
|
||
|
|
6a2c44da91
|
||
|
|
98303dd4d1
|
||
|
|
a5cbf1d7b2
|
||
|
|
a1177efa69
|
||
|
|
b907e06c5f
|
||
|
|
0298b85323
|
||
|
|
ef8b8910b0
|
||
|
|
abbc9b3d07
|
||
|
|
9d3ab2f0e0
|
||
|
|
3ea8517ff9
|
||
|
|
6e4c9f1b02
|
||
|
|
3d9944921f | ||
|
|
3d633619c4 | ||
|
|
5873ebdd77 | ||
|
|
55f9fa3367 |
@@ -1,110 +1,3 @@
|
|||||||
--[[
|
|
||||||
cable_NS1 -> One connection from north to south
|
|
||||||
cable_NW1_ES1 -> One connection from north to west, and another from east to south
|
|
||||||
cable_NWE8_S8 -> One connection between north, west and east, 8bit single cable from south
|
|
||||||
cable_0 -> The one cable without connection
|
|
||||||
--]]
|
|
||||||
|
|
||||||
local nodebox_single = {
|
|
||||||
{-0.0625, -0.5, 0.25, 0.0625, -0.375, 0.5},
|
|
||||||
{-0.125, -0.5, 0.25, 0.125, -0.25, 0.5}
|
|
||||||
}
|
|
||||||
local nodebox_straight = {
|
|
||||||
{-0.0625, -0.5, -0.5, 0.0625, -0.375, 0.5},
|
|
||||||
{-0.125, -0.5, -0.5, 0.125, -0.25, 0.5}
|
|
||||||
}
|
|
||||||
local nodeboxes_corner = {
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.0625, 0.0625, -0.375, 0.0625}, -- NodeBox1
|
|
||||||
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.125, 0.125, -0.25, 0.125}, -- NodeBox1
|
|
||||||
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
local nodeboxes_jump = {
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.0625, -0.1875, -0.375, 0.0625}, -- NodeBox2
|
|
||||||
{0.1875, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox3
|
|
||||||
{-0.3125, -0.1875, -0.0625, 0.3125, -0.0625, 0.0625}, -- NodeBox4
|
|
||||||
{-0.3125, -0.375, -0.0625, -0.1875, -0.1875, 0.0625}, -- NodeBox5
|
|
||||||
{0.1875, -0.375, -0.0625, 0.3125, -0.1875, 0.0625}, -- NodeBox6
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.125, -0.1875, -0.25, 0.125}, -- NodeBox2
|
|
||||||
{0.1875, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox3
|
|
||||||
{-0.4375, -0.1875, -0.125, 0.4375, 0.0625, 0.125}, -- NodeBox4
|
|
||||||
{-0.4375, -0.25, -0.125, -0.1875, -0.1875, 0.125}, -- NodeBox5
|
|
||||||
{0.1875, -0.25, -0.125, 0.4375, -0.1875, 0.125}, -- NodeBox6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
local nodeboxes_ccorner = {
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.0625, -0.1875, -0.375, 0.0625}, -- NodeBox1
|
|
||||||
{-0.0625, -0.5, 0.1875, 0.0625, -0.375, 0.5}, -- NodeBox2
|
|
||||||
{-0.3125, -0.5, 0.0625, -0.1875, -0.375, 0.3125}, -- NodeBox3
|
|
||||||
{-0.1875, -0.5, 0.1875, -0.0625, -0.375, 0.3125}, -- NodeBox4
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.125, -0.125, -0.25, 0.125}, -- NodeBox1
|
|
||||||
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
|
||||||
{-0.375, -0.5, 0.125, -0.125, -0.25, 0.375}, -- NodeBox4
|
|
||||||
}
|
|
||||||
}
|
|
||||||
local nodeboxes_tri = {
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox1
|
|
||||||
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox1
|
|
||||||
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
local nodeboxes_cross = {
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox1
|
|
||||||
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
|
||||||
{-0.0625, -0.5, -0.5, 0.0625, -0.375, -0.0625}, -- NodeBox3
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{-0.5, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox1
|
|
||||||
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
|
||||||
{-0.125, -0.5, -0.5, 0.125, -0.25, -0.125}, -- NodeBox3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
local nodebox_disconnected = {-0.125, -0.5, -0.125, 0.125, -0.25, 0.125}
|
|
||||||
|
|
||||||
local function rotateNodebox(nb,rot)
|
|
||||||
return
|
|
||||||
(math.fmod(rot,4)==0) and {nb[1],nb[2],nb[3],nb[4],nb[5],nb[6]} or
|
|
||||||
(math.fmod(rot,4)==1) and {nb[3],nb[2],-nb[1],nb[6],nb[5],-nb[4]} or
|
|
||||||
(math.fmod(rot,4)==2) and {-nb[1],nb[2],-nb[3],-nb[4],nb[5],-nb[6]} or
|
|
||||||
(math.fmod(rot,4)==3) and {-nb[3],nb[2],nb[1],-nb[6],nb[5],nb[4]} or
|
|
||||||
nb -- Should not happen
|
|
||||||
end
|
|
||||||
local function rotate4dir(conn,r)
|
|
||||||
local function rotate4dirOnce(conn)
|
|
||||||
return math.floor(conn/2) + 8*math.fmod(conn,2)
|
|
||||||
end
|
|
||||||
local x = math.fmod(conn,16)
|
|
||||||
for i=1,r do
|
|
||||||
x = rotate4dirOnce(x)
|
|
||||||
end
|
|
||||||
return (conn - math.fmod(conn,16) + x)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function constArray(val,size)
|
|
||||||
local t = {}
|
|
||||||
for i=1,size
|
|
||||||
do
|
|
||||||
table.insert(t,val)
|
|
||||||
end
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
name: the name of the cable
|
name: the name of the cable
|
||||||
nodeboxes: All the rendered nodeboxes
|
nodeboxes: All the rendered nodeboxes
|
||||||
@@ -113,11 +6,12 @@ end
|
|||||||
nbconn: Which correspond each nodebox correspond to
|
nbconn: Which correspond each nodebox correspond to
|
||||||
ex: {1,1,2,1} -> nodeboxes 1,2,3 correspond to connection ES, and nodebox 2 correspond to connection W
|
ex: {1,1,2,1} -> nodeboxes 1,2,3 correspond to connection ES, and nodebox 2 correspond to connection W
|
||||||
--]]
|
--]]
|
||||||
circuits.cablesnb = {}
|
logikraft.cablenodes = {}
|
||||||
circuits.cablesconn = {}
|
logikraft.cables = {}
|
||||||
circuits.cablesnbconn = {}
|
function logikraft.registerCable(name,nodeboxes,connections,nbconn)
|
||||||
local function createCables(name,nodeboxes,connections,nbconn)
|
local nodename = "logikraft:cable_"..name
|
||||||
minetest.register_node("circuits:cable_"..name, {
|
|
||||||
|
minetest.register_node(nodename, {
|
||||||
tiles = {
|
tiles = {
|
||||||
"cable_gray.png",
|
"cable_gray.png",
|
||||||
"cable_gray.png",
|
"cable_gray.png",
|
||||||
@@ -126,18 +20,16 @@ local function createCables(name,nodeboxes,connections,nbconn)
|
|||||||
"cable_gray.png",
|
"cable_gray.png",
|
||||||
"cable_gray.png",
|
"cable_gray.png",
|
||||||
},
|
},
|
||||||
drawtype = "nodebox",
|
|
||||||
paramtype = "light",
|
paramtype = "light",
|
||||||
|
drawtype = "nodebox",
|
||||||
node_box = {
|
node_box = {
|
||||||
type = "fixed",
|
type = "fixed",
|
||||||
fixed = nodeboxes
|
fixed = nodeboxes
|
||||||
},
|
},
|
||||||
groups = {circuitry = 1,dig_immediate = 3}
|
groups = {circuitry = 1,dig_immediate = 3},
|
||||||
|
drop = nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if name == "EW8_N1_S1"
|
|
||||||
then print(dump(connections)) end
|
|
||||||
|
|
||||||
-- We sort the connections and nbconn
|
-- We sort the connections and nbconn
|
||||||
local connections2 = table.copy(connections)
|
local connections2 = table.copy(connections)
|
||||||
table.sort(connections2)
|
table.sort(connections2)
|
||||||
@@ -147,211 +39,40 @@ local function createCables(name,nodeboxes,connections,nbconn)
|
|||||||
do
|
do
|
||||||
nbconn2[i] = perm[v]
|
nbconn2[i] = perm[v]
|
||||||
end
|
end
|
||||||
if name == "EW8_N1_S1"
|
|
||||||
then print(dump(connections2)) end
|
|
||||||
|
|
||||||
circuits.cablesnb[name] = nodeboxes
|
logikraft.cablenodes[nodename] = name
|
||||||
circuits.cablesconn[name] = connections2
|
logikraft.cables[name] = {
|
||||||
circuits.cablesnbconn[name] = nbconn2
|
nbs = nodeboxes,
|
||||||
|
conns = connections2,
|
||||||
|
nbToConn = nbconn2
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- cable_NSEW0
|
function logikraft.cableFromConns(conns)
|
||||||
for v=1,2 do
|
for name,cable in pairs(logikraft.cables)
|
||||||
createCables("NSEW"..tostring((v-1)*7+1),nodeboxes_cross[v],{(v-1)*16+8+4+2+1},constArray(1,#nodeboxes_cross[v]))
|
do
|
||||||
end
|
if table.equals1(conns,cable.conns)
|
||||||
|
then return name
|
||||||
--[
|
|
||||||
-- cable_NW0_SE0
|
|
||||||
-- cable_NE0_SW0
|
|
||||||
-- cable_NS0_EW0
|
|
||||||
for v0=1,2 do
|
|
||||||
for v1=1,2 do
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
for _,nb in ipairs(nodeboxes_ccorner[v0]) do
|
|
||||||
table.insert(nodeboxes, nb)
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
end
|
|
||||||
for _,nb in ipairs(nodeboxes_ccorner[v1]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,2))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
end
|
|
||||||
createCables("NW"..tostring((v0-1)*7+1).."_SE"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+1,(v1-1)*16+4+2},nbconn)
|
|
||||||
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
for _,nb in ipairs(nodeboxes_ccorner[v0]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,1))
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
end
|
|
||||||
for _,nb in ipairs(nodeboxes_ccorner[v1]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,3))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
end
|
|
||||||
createCables("NE"..tostring((v0-1)*7+1).."_SW"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+4,(v1-1)*16+2+1},nbconn)
|
|
||||||
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
table.insert(nodeboxes, nodebox_straight[v0])
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
for _,nb in ipairs(nodeboxes_jump[v1]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,2))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
end
|
|
||||||
createCables("NS"..tostring((v0-1)*7+1).."_EW"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+2,(v1-1)*16+4+1},nbconn)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- cable_AB0_C0_D0 -- ("NW","NE","SE","SW") * (/ + C0 + D0 + C0D0)
|
|
||||||
-- cable_AB0_C0_D0 -- ("NS" "EW") * (/ + C0 + D0 + C0D0)
|
|
||||||
for v=1,2 do
|
|
||||||
for v1=0,2 do
|
|
||||||
for v2=0,2 do
|
|
||||||
for r,rn in ipairs({{"NW","E","S"},{"NE","S","W"},{"SE","W","N"},{"SW","N","E"}}) do
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
local connz = {rotate4dir((v-1)*16+8+1,r-1)}
|
|
||||||
for _,nb in ipairs(nodeboxes_corner[v]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,r-1))
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
end
|
end
|
||||||
if v1 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v1],r))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
table.insert(connz,rotate4dir((v1-1)*16+4,r-1))
|
|
||||||
end
|
|
||||||
if v2 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v2],r+1))
|
|
||||||
table.insert(nbconn, v1==0 and 2 or 3)
|
|
||||||
table.insert(connz,rotate4dir((v2-1)*16+2,r-1))
|
|
||||||
end
|
|
||||||
createCables(
|
|
||||||
rn[1]..tostring((v-1)*7+1)..
|
|
||||||
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or "")..
|
|
||||||
(v2~=0 and ("_"..rn[3]..tostring((v2-1)*7+1)) or "")
|
|
||||||
,nodeboxes,connz,nbconn
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
for r,rn in ipairs({{"NS","E","W"},{"EW","N","S"}}) do
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
local connz = {rotate4dir((v-1)*16+8+2,r-1)}
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_straight[v],r-1))
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
if v1 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v1],r))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
table.insert(connz,rotate4dir((v1-1)*16+4,r-1))
|
|
||||||
end
|
|
||||||
if v2 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v2],r+2))
|
|
||||||
table.insert(nbconn, v1==0 and 2 or 3)
|
|
||||||
table.insert(connz,rotate4dir((v2-1)*16+1,r-1))
|
|
||||||
end
|
|
||||||
createCables(
|
|
||||||
rn[1]..tostring((v-1)*7+1)..
|
|
||||||
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or "")..
|
|
||||||
(v2~=0 and ("_"..rn[3]..tostring((v2-1)*7+1)) or ""),
|
|
||||||
nodeboxes,connz,nbconn
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- "cable_A0_B0_C0_D0" -- (n s e w ns ...)
|
function logikraft.rotateCable(name,r)
|
||||||
for v1=0,2 do
|
local conn = logikraft.cables[name].conns
|
||||||
for v2=0,2 do
|
|
||||||
for v3=0,2 do
|
|
||||||
for v4=0,2 do
|
|
||||||
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
local name = ""
|
|
||||||
local connz = {}
|
|
||||||
if v1 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v1],0))
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
table.insert(connz,(v1-1)*16+8)
|
|
||||||
name = name.."_N"..tostring((v1-1)*7+1)
|
|
||||||
end
|
|
||||||
if v2 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v2],1))
|
|
||||||
table.insert(nbconn, #nbconn+1)
|
|
||||||
table.insert(connz,(v2-1)*16+4)
|
|
||||||
name = name.."_E"..tostring((v2-1)*7+1)
|
|
||||||
end
|
|
||||||
if v3 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v3],2))
|
|
||||||
table.insert(nbconn, #nbconn+1)
|
|
||||||
table.insert(connz,(v3-1)*16+2)
|
|
||||||
name = name.."_S"..tostring((v3-1)*7+1)
|
|
||||||
end
|
|
||||||
if v4 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v4],3))
|
|
||||||
table.insert(nbconn, #nbconn+1)
|
|
||||||
table.insert(connz,(v4-1)*16+1)
|
|
||||||
name = name.."_W"..tostring((v4-1)*7+1)
|
|
||||||
end
|
|
||||||
if name == "" then
|
|
||||||
table.insert(nodeboxes, nodebox_disconnected)
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
table.insert(connz,(v1-1)*16+0) -- special case of no connection
|
|
||||||
name = "_zero"
|
|
||||||
end
|
|
||||||
|
|
||||||
createCables(string.sub(name,2,-1),nodeboxes,connz,nbconn)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- "cable_ABC0_D0" -- ("NEW","NES","ESW","SWN") * (/ + D0)
|
|
||||||
for v=1,2 do
|
|
||||||
for v1=0,2 do
|
|
||||||
for r,rn in ipairs({{"NEW","S"},{"NES","W"},{"ESW","N"},{"SWN","E"}}) do
|
|
||||||
local nodeboxes = {}
|
|
||||||
local nbconn = {}
|
|
||||||
local connz = {rotate4dir((v-1)*16+8+4+1,r-1)}
|
|
||||||
for _,nb in ipairs(nodeboxes_tri[v]) do
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nb,r-1))
|
|
||||||
table.insert(nbconn, 1)
|
|
||||||
end
|
|
||||||
if v1 ~= 0 then
|
|
||||||
table.insert(nodeboxes, rotateNodebox(nodebox_single[v1],r+1))
|
|
||||||
table.insert(nbconn, 2)
|
|
||||||
table.insert(connz,rotate4dir((v1-1)*16+2,r-1))
|
|
||||||
end
|
|
||||||
createCables(
|
|
||||||
rn[1]..tostring((v-1)*7+1)..
|
|
||||||
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or ""),
|
|
||||||
nodeboxes,connz,nbconn
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
--]]
|
|
||||||
|
|
||||||
circuits.cablesfromconn = table.invert(circuits.cablesconn)
|
|
||||||
|
|
||||||
|
|
||||||
function circuits.rotateCable(name,r)
|
|
||||||
local conn = circuits.cablesconn[name]
|
|
||||||
local newconn = {}
|
local newconn = {}
|
||||||
for i,v in ipairs(conn)
|
for i,v in ipairs(conn)
|
||||||
do
|
do
|
||||||
newconn[i] = rotate4dir(v,r)
|
newconn[i] = logikraft.rotate4dir(v,r)
|
||||||
end
|
end
|
||||||
table.sort(newconn)
|
table.sort(newconn)
|
||||||
return table.getkey1(circuits.cablesconn,newconn)
|
return logikraft.cableFromConns(newconn)
|
||||||
end
|
end
|
||||||
|
|
||||||
function circuits.connectInCable(name,a,b)
|
function logikraft.connectInCable(name,a,b)
|
||||||
local conns = table.copy(circuits.cablesconn[name])
|
local conns = table.copy(logikraft.cables[name].conns)
|
||||||
local cia = circuits.cablesnbconn[name][a]
|
local cia = logikraft.cables[name].nbToConn[a]
|
||||||
local cib = circuits.cablesnbconn[name][b]
|
local cib = logikraft.cables[name].nbToConn[b]
|
||||||
local conna = conns[cia]
|
local conna = conns[cia]
|
||||||
local connb = conns[cib]
|
local connb = conns[cib]
|
||||||
|
|
||||||
@@ -361,12 +82,133 @@ function circuits.connectInCable(name,a,b)
|
|||||||
table.remove(conns,cib)
|
table.remove(conns,cib)
|
||||||
|
|
||||||
table.sort(conns)
|
table.sort(conns)
|
||||||
return table.getkey1(circuits.cablesconn,conns)
|
return logikraft.cableFromConns(conns)
|
||||||
end
|
end
|
||||||
|
|
||||||
function circuits.disconnectInCable(name,a)
|
function logikraft.connFromDir(name,dir)
|
||||||
local conns = table.copy(circuits.cablesconn[name])
|
local conns = logikraft.cables[name].conns
|
||||||
local cindex = circuits.cablesnbconn[name][a]
|
local dir8 = math.pow(2,dir)
|
||||||
|
for i,c in ipairs(conns)
|
||||||
|
do
|
||||||
|
if (math.fmod(c,2*dir8) - dir8 >= 0)
|
||||||
|
then
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Returns {newcablea,newcableb} if the two cables are contiguous
|
||||||
|
Returns nil otherwise
|
||||||
|
If namea or nameb is nil, no cable is present, and we create a single pin, or we connect to the conn in the direction
|
||||||
|
If a or b is nil, we consider that
|
||||||
|
--]]
|
||||||
|
function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
|
||||||
|
print("Connecting "..tostring(namea).."("..tostring(a)..") with "..tostring(nameb).."("..tostring(b)..")")
|
||||||
|
local dira = (posa.y == posb.y) and (
|
||||||
|
(posa.z + 1 == posb.z and posa.x == posb.x and 3) or
|
||||||
|
(posa.x + 1 == posb.x and posa.z == posb.z and 2) or
|
||||||
|
(posa.z - 1 == posb.z and posa.x == posb.x and 1) or
|
||||||
|
(posa.x - 1 == posb.x and posa.z == posb.z and 0) or
|
||||||
|
nil) or nil
|
||||||
|
if dira == nil
|
||||||
|
then return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if not namea
|
||||||
|
then
|
||||||
|
namea = logikraft.cableFromConns({logikraft.rotate4dir(4,6-dira)})
|
||||||
|
a = 1 -- There is only one nodebox
|
||||||
|
end
|
||||||
|
if not nameb
|
||||||
|
then
|
||||||
|
nameb = logikraft.cableFromConns({logikraft.rotate4dir(4,4-dira)})
|
||||||
|
b = 1 -- There is only one nodebox
|
||||||
|
end
|
||||||
|
|
||||||
|
local cia
|
||||||
|
if not a
|
||||||
|
then
|
||||||
|
cia = logikraft.connFromDir(namea,dira)
|
||||||
|
if not cia
|
||||||
|
then
|
||||||
|
-- We need to add a tip to a
|
||||||
|
local connsa = table.copy(logikraft.cables[namea].conns)
|
||||||
|
table.insert(connsa,logikraft.rotate4dir(4,6-dira))
|
||||||
|
table.sort(connsa)
|
||||||
|
namea = logikraft.cableFromConns(connsa)
|
||||||
|
cia = logikraft.connFromDir(namea,dira)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cia = logikraft.cables[namea].nbToConn[a]
|
||||||
|
end
|
||||||
|
|
||||||
|
local cib
|
||||||
|
if not b
|
||||||
|
then
|
||||||
|
cib = logikraft.connFromDir(nameb,math.fmod(dira+2,4))
|
||||||
|
if not cib
|
||||||
|
then
|
||||||
|
-- We need to add a tip to b
|
||||||
|
local connsb = table.copy(logikraft.cables[nameb].conns)
|
||||||
|
table.insert(connsb,logikraft.rotate4dir(4,4-dira))
|
||||||
|
table.sort(connsb)
|
||||||
|
nameb = logikraft.cableFromConns(connsb)
|
||||||
|
cib = logikraft.connFromDir(nameb,math.fmod(dira+2,4))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cib = logikraft.cables[nameb].nbToConn[b]
|
||||||
|
end
|
||||||
|
|
||||||
|
print(dira)
|
||||||
|
print(namea)
|
||||||
|
print(cia)
|
||||||
|
print(nameb)
|
||||||
|
print(cib)
|
||||||
|
|
||||||
|
local connsa = table.copy(logikraft.cables[namea].conns)
|
||||||
|
local connsb = table.copy(logikraft.cables[nameb].conns)
|
||||||
|
--print(dump(connsa))
|
||||||
|
--print(dump(connsb))
|
||||||
|
local cia2 = logikraft.connFromDir(namea,dira)
|
||||||
|
local cib2 = logikraft.connFromDir(nameb,math.fmod(dira+2,4))
|
||||||
|
|
||||||
|
-- (nba == nil) -> Need to add dira to the a
|
||||||
|
-- (a == nba) -> Already connected
|
||||||
|
-- (a ~= nba) -> Need to connect a and nbas
|
||||||
|
if (cia2 == nil)
|
||||||
|
then
|
||||||
|
connsa[cia] = connsa[cia] + math.pow(2,dira)
|
||||||
|
elseif (cia ~= cia2)
|
||||||
|
then
|
||||||
|
-- We take the v size of selected cable
|
||||||
|
local v = math.fmod(math.floor(connsa[cia]/16),2)
|
||||||
|
connsa[cia] = math.fmod(connsa[cia],16) + math.fmod(connsa[cia2],16) + v*16
|
||||||
|
table.remove(connsa,cia2)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (cib2 == nil)
|
||||||
|
then
|
||||||
|
connsb[cib] = connsb[cib] + math.pow(2,math.fmod(dira+2,4))
|
||||||
|
elseif (cib ~= cib2)
|
||||||
|
then
|
||||||
|
-- We take the v size of selected cable
|
||||||
|
local v = math.fmod(math.floor(connsb[cib]/16),2)
|
||||||
|
connsb[cib] = math.fmod(connsb[cib],16) + math.fmod(connsb[cib2],16) + v*16
|
||||||
|
table.remove(connsb,cib2)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
table.sort(connsa)
|
||||||
|
table.sort(connsb)
|
||||||
|
return {logikraft.cableFromConns(connsa),logikraft.cableFromConns(connsb)}
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function logikraft.disconnectInCable(name,a)
|
||||||
|
local conns = table.copy(logikraft.cables[name].conns)
|
||||||
|
local cindex = logikraft.cables[name].nbToConn[a]
|
||||||
local conn = conns[cindex]
|
local conn = conns[cindex]
|
||||||
|
|
||||||
table.remove(conns,cindex)
|
table.remove(conns,cindex)
|
||||||
@@ -384,5 +226,15 @@ function circuits.disconnectInCable(name,a)
|
|||||||
end
|
end
|
||||||
|
|
||||||
table.sort(conns)
|
table.sort(conns)
|
||||||
return table.getkey1(circuits.cablesconn,conns)
|
return logikraft.cableFromConns(conns)
|
||||||
|
end
|
||||||
|
|
||||||
|
function logikraft.resizeInCable(name,a)
|
||||||
|
local conns = table.copy(logikraft.cables[name].conns)
|
||||||
|
local cindex = logikraft.cables[name].nbToConn[a]
|
||||||
|
|
||||||
|
conns[cindex] = (conns[cindex] >= 16) and (conns[cindex] - 16) or (conns[cindex] + 16)
|
||||||
|
|
||||||
|
table.sort(conns)
|
||||||
|
return logikraft.cableFromConns(conns)
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,253 @@
|
|||||||
|
--[[
|
||||||
|
cable_NS1 -> One connection from north to south
|
||||||
|
cable_NW1_ES1 -> One connection from north to west, and another from east to south
|
||||||
|
cable_NWE8_S8 -> One connection between north, west and east, 8bit single cable from south
|
||||||
|
cable_0 -> The one cable without connection
|
||||||
|
--]]
|
||||||
|
|
||||||
|
local nodebox_single = {
|
||||||
|
{-0.0625, -0.5, 0.25, 0.0625, -0.375, 0.5},
|
||||||
|
{-0.125, -0.5, 0.25, 0.125, -0.25, 0.5}
|
||||||
|
}
|
||||||
|
local nodebox_straight = {
|
||||||
|
{-0.0625, -0.5, -0.5, 0.0625, -0.375, 0.5},
|
||||||
|
{-0.125, -0.5, -0.5, 0.125, -0.25, 0.5}
|
||||||
|
}
|
||||||
|
local nodeboxes_corner = {
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.0625, 0.0625, -0.375, 0.0625}, -- NodeBox1
|
||||||
|
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.125, 0.125, -0.25, 0.125}, -- NodeBox1
|
||||||
|
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local nodeboxes_jump = {
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.0625, -0.1875, -0.375, 0.0625}, -- NodeBox2
|
||||||
|
{0.1875, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox3
|
||||||
|
{-0.3125, -0.1875, -0.0625, 0.3125, -0.0625, 0.0625}, -- NodeBox4
|
||||||
|
{-0.3125, -0.375, -0.0625, -0.1875, -0.1875, 0.0625}, -- NodeBox5
|
||||||
|
{0.1875, -0.375, -0.0625, 0.3125, -0.1875, 0.0625}, -- NodeBox6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.125, -0.1875, -0.25, 0.125}, -- NodeBox2
|
||||||
|
{0.1875, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox3
|
||||||
|
{-0.4375, -0.1875, -0.125, 0.4375, 0.0625, 0.125}, -- NodeBox4
|
||||||
|
{-0.4375, -0.25, -0.125, -0.1875, -0.1875, 0.125}, -- NodeBox5
|
||||||
|
{0.1875, -0.25, -0.125, 0.4375, -0.1875, 0.125}, -- NodeBox6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local nodeboxes_ccorner = {
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.0625, -0.1875, -0.375, 0.0625}, -- NodeBox1
|
||||||
|
{-0.0625, -0.5, 0.1875, 0.0625, -0.375, 0.5}, -- NodeBox2
|
||||||
|
{-0.3125, -0.5, 0.0625, -0.1875, -0.375, 0.3125}, -- NodeBox3
|
||||||
|
{-0.1875, -0.5, 0.1875, -0.0625, -0.375, 0.3125}, -- NodeBox4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.125, -0.125, -0.25, 0.125}, -- NodeBox1
|
||||||
|
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
||||||
|
{-0.375, -0.5, 0.125, -0.125, -0.25, 0.375}, -- NodeBox4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local nodeboxes_tri = {
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox1
|
||||||
|
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox1
|
||||||
|
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local nodeboxes_cross = {
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox1
|
||||||
|
{-0.0625, -0.5, 0.0625, 0.0625, -0.375, 0.5}, -- NodeBox2
|
||||||
|
{-0.0625, -0.5, -0.5, 0.0625, -0.375, -0.0625}, -- NodeBox3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{-0.5, -0.5, -0.125, 0.5, -0.25, 0.125}, -- NodeBox1
|
||||||
|
{-0.125, -0.5, 0.125, 0.125, -0.25, 0.5}, -- NodeBox2
|
||||||
|
{-0.125, -0.5, -0.5, 0.125, -0.25, -0.125}, -- NodeBox3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local nodebox_disconnected = {-0.125, -0.5, -0.125, 0.125, -0.25, 0.125}
|
||||||
|
|
||||||
|
-- cable_NSEW0
|
||||||
|
for v=1,2 do
|
||||||
|
logikraft.registerCable("NSEW"..tostring((v-1)*7+1),nodeboxes_cross[v],{(v-1)*16+8+4+2+1},table.constTable(1,#nodeboxes_cross[v]))
|
||||||
|
end
|
||||||
|
|
||||||
|
--[
|
||||||
|
-- cable_NW0_SE0
|
||||||
|
-- cable_NE0_SW0
|
||||||
|
-- cable_NS0_EW0
|
||||||
|
for v0=1,2 do
|
||||||
|
for v1=1,2 do
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
for _,nb in ipairs(nodeboxes_ccorner[v0]) do
|
||||||
|
table.insert(nodeboxes, nb)
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
end
|
||||||
|
for _,nb in ipairs(nodeboxes_ccorner[v1]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,2))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
end
|
||||||
|
logikraft.registerCable("NW"..tostring((v0-1)*7+1).."_SE"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+1,(v1-1)*16+4+2},nbconn)
|
||||||
|
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
for _,nb in ipairs(nodeboxes_ccorner[v0]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,1))
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
end
|
||||||
|
for _,nb in ipairs(nodeboxes_ccorner[v1]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,3))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
end
|
||||||
|
logikraft.registerCable("NE"..tostring((v0-1)*7+1).."_SW"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+4,(v1-1)*16+2+1},nbconn)
|
||||||
|
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
table.insert(nodeboxes, nodebox_straight[v0])
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
for _,nb in ipairs(nodeboxes_jump[v1]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,2))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
end
|
||||||
|
logikraft.registerCable("NS"..tostring((v0-1)*7+1).."_EW"..tostring((v1-1)*7+1),nodeboxes,{(v0-1)*16+8+2,(v1-1)*16+4+1},nbconn)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- cable_AB0_C0_D0 -- ("NW","NE","SE","SW") * (/ + C0 + D0 + C0D0)
|
||||||
|
-- cable_AB0_C0_D0 -- ("NS" "EW") * (/ + C0 + D0 + C0D0)
|
||||||
|
for v=1,2 do
|
||||||
|
for v1=0,2 do
|
||||||
|
for v2=0,2 do
|
||||||
|
for r,rn in ipairs({{"NW","E","S"},{"NE","S","W"},{"SE","W","N"},{"SW","N","E"}}) do
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
local connz = {logikraft.rotate4dir((v-1)*16+8+1,r-1)}
|
||||||
|
for _,nb in ipairs(nodeboxes_corner[v]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,r-1))
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
end
|
||||||
|
if v1 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v1],r))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
table.insert(connz,logikraft.rotate4dir((v1-1)*16+4,r-1))
|
||||||
|
end
|
||||||
|
if v2 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v2],r+1))
|
||||||
|
table.insert(nbconn, v1==0 and 2 or 3)
|
||||||
|
table.insert(connz,logikraft.rotate4dir((v2-1)*16+2,r-1))
|
||||||
|
end
|
||||||
|
logikraft.registerCable(
|
||||||
|
rn[1]..tostring((v-1)*7+1)..
|
||||||
|
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or "")..
|
||||||
|
(v2~=0 and ("_"..rn[3]..tostring((v2-1)*7+1)) or "")
|
||||||
|
,nodeboxes,connz,nbconn
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
for r,rn in ipairs({{"NS","E","W"},{"EW","N","S"}}) do
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
local connz = {logikraft.rotate4dir((v-1)*16+8+2,r-1)}
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_straight[v],r-1))
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
if v1 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v1],r))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
table.insert(connz,logikraft.rotate4dir((v1-1)*16+4,r-1))
|
||||||
|
end
|
||||||
|
if v2 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v2],r+2))
|
||||||
|
table.insert(nbconn, v1==0 and 2 or 3)
|
||||||
|
table.insert(connz,logikraft.rotate4dir((v2-1)*16+1,r-1))
|
||||||
|
end
|
||||||
|
logikraft.registerCable(
|
||||||
|
rn[1]..tostring((v-1)*7+1)..
|
||||||
|
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or "")..
|
||||||
|
(v2~=0 and ("_"..rn[3]..tostring((v2-1)*7+1)) or ""),
|
||||||
|
nodeboxes,connz,nbconn
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- "cable_A0_B0_C0_D0" -- (n s e w ns ...)
|
||||||
|
for v1=0,2 do
|
||||||
|
for v2=0,2 do
|
||||||
|
for v3=0,2 do
|
||||||
|
for v4=0,2 do
|
||||||
|
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
local name = ""
|
||||||
|
local connz = {}
|
||||||
|
if v1 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v1],0))
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
table.insert(connz,(v1-1)*16+8)
|
||||||
|
name = name.."_N"..tostring((v1-1)*7+1)
|
||||||
|
end
|
||||||
|
if v2 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v2],1))
|
||||||
|
table.insert(nbconn, #nbconn+1)
|
||||||
|
table.insert(connz,(v2-1)*16+4)
|
||||||
|
name = name.."_E"..tostring((v2-1)*7+1)
|
||||||
|
end
|
||||||
|
if v3 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v3],2))
|
||||||
|
table.insert(nbconn, #nbconn+1)
|
||||||
|
table.insert(connz,(v3-1)*16+2)
|
||||||
|
name = name.."_S"..tostring((v3-1)*7+1)
|
||||||
|
end
|
||||||
|
if v4 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v4],3))
|
||||||
|
table.insert(nbconn, #nbconn+1)
|
||||||
|
table.insert(connz,(v4-1)*16+1)
|
||||||
|
name = name.."_W"..tostring((v4-1)*7+1)
|
||||||
|
end
|
||||||
|
if name ~= "" then
|
||||||
|
-- Else there is no connection, we don't have to create a cable
|
||||||
|
logikraft.registerCable(string.sub(name,2,-1),nodeboxes,connz,nbconn)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- "cable_ABC0_D0" -- ("NEW","NES","ESW","SWN") * (/ + D0)
|
||||||
|
for v=1,2 do
|
||||||
|
for v1=0,2 do
|
||||||
|
for r,rn in ipairs({{"NEW","S"},{"NES","W"},{"ESW","N"},{"SWN","E"}}) do
|
||||||
|
local nodeboxes = {}
|
||||||
|
local nbconn = {}
|
||||||
|
local connz = {logikraft.rotate4dir((v-1)*16+8+4+1,r-1)}
|
||||||
|
for _,nb in ipairs(nodeboxes_tri[v]) do
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nb,r-1))
|
||||||
|
table.insert(nbconn, 1)
|
||||||
|
end
|
||||||
|
if v1 ~= 0 then
|
||||||
|
table.insert(nodeboxes, logikraft.rotateNodebox(nodebox_single[v1],r+1))
|
||||||
|
table.insert(nbconn, 2)
|
||||||
|
table.insert(connz,logikraft.rotate4dir((v1-1)*16+2,r-1))
|
||||||
|
end
|
||||||
|
logikraft.registerCable(
|
||||||
|
rn[1]..tostring((v-1)*7+1)..
|
||||||
|
(v1~=0 and ("_"..rn[2]..tostring((v1-1)*7+1)) or ""),
|
||||||
|
nodeboxes,connz,nbconn
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,39 +1,9 @@
|
|||||||
local _contexts = {}
|
minetest.register_node("logikraft:circuitBlock", {
|
||||||
|
|
||||||
function circuits.setNameFormspec(targetType, defaultName)
|
|
||||||
|
|
||||||
local text = "Please enter the name of this " .. targetType
|
|
||||||
|
|
||||||
local formspec = {
|
|
||||||
"formspec_version[4]",
|
|
||||||
"size[6,3.476]",
|
|
||||||
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
|
||||||
"field[0.375,1.25;5.25,0.8;name;".. minetest.formspec_escape(targetType) .. " name;"..(defaultName or "").."]",
|
|
||||||
"button[1.5,2.3;3,0.8;commit;OK]"
|
|
||||||
}
|
|
||||||
|
|
||||||
-- table.concat is faster than string concatenation - `..`
|
|
||||||
return table.concat(formspec, "")
|
|
||||||
end
|
|
||||||
|
|
||||||
function circuits.showSetNameFormspec(playerName, targetName)
|
|
||||||
minetest.show_formspec(playerName, "circuits:setNameFormSpec", circuits.setNameFormspec(targetName))
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
||||||
if formname ~= "circuits:setNameFormSpec" then return end
|
|
||||||
|
|
||||||
if fields.commit then
|
|
||||||
local pname = player:get_player_name()
|
|
||||||
minetest.chat_send_all(pname .. " guessed " .. fields.name)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
minetest.register_node("circuits:circuitBlock", {
|
|
||||||
description = "Circuit Block",
|
description = "Circuit Block",
|
||||||
tiles = {"circuit_block.png"},
|
tiles = {"circuit_block.png"},
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
circuits.discoverBlocks(pos)
|
local poz = logikraft.discoverBlocks(pos)
|
||||||
|
local circuit = logikraft.compileCircuit(poz)
|
||||||
end,
|
end,
|
||||||
groups = {circuitry = 1,dig_immediate = 3}
|
groups = {circuitry = 1,dig_immediate = 3}
|
||||||
})
|
})
|
||||||
@@ -1,39 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
--[[
|
|
||||||
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()
|
-- 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)
|
||||||
@@ -42,85 +6,100 @@ local function remove_no_destruct(pos)
|
|||||||
minetest.check_for_falling(pos)
|
minetest.check_for_falling(pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_node("circuits:component_head", {
|
-- componentsblocks["block"] = {name = Name of the component,x,y}
|
||||||
drawtype = "block",
|
logikraft.componentnodes = {}
|
||||||
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
|
function logikraft.registerComponent(name,cmp)
|
||||||
local node = minetest.get_node(under)
|
local w = cmp.width
|
||||||
local udef = minetest.registered_nodes[node.name]
|
local h = cmp.height
|
||||||
|
local cables = cmp.ports
|
||||||
|
|
||||||
-- Rightclick is prioritary
|
for x=1,w do
|
||||||
if udef and udef.on_rightclick and
|
for y=1,h do
|
||||||
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 nodename = "logikraft:component_"..name.."_"..tostring(x).."_"..tostring(y)
|
||||||
local pos
|
minetest.register_node(nodename, {
|
||||||
if udef and udef.buildable_to then
|
drawtype = "block",
|
||||||
pos = under
|
description = name.." component",
|
||||||
else
|
tiles = {
|
||||||
pos = pointed_thing.above
|
"component_"..name..".png",
|
||||||
end
|
"component.png",
|
||||||
|
((x==w) and (cables.right and cables.right[y] and "component_port.png" or "component.png") or "component.png"), -- x+
|
||||||
|
((x==1) and (cables.left and cables.left[y] and "component_port.png" or "component.png") or "component.png"), -- x-
|
||||||
|
((y==h) and (cables.top and cables.top[x] and "component_port.png" or "component.png") or "component.png"), -- z+
|
||||||
|
((y==1) and (cables.bottom and cables.bottom[x] and "component_port.png" or "component.png") or "component.png"), -- z-
|
||||||
|
},
|
||||||
|
groups = {circuitry = 1,dig_immediate = 3, not_in_creative_inventory = (x==1 and y==1 and 0) or 1},
|
||||||
|
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)
|
||||||
|
local udef = minetest.registered_nodes[node.name]
|
||||||
|
|
||||||
local player_name = placer and placer:get_player_name() or ""
|
-- 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
|
||||||
|
|
||||||
local dir = placer and placer:get_look_dir() and
|
-- Pos will be the head location
|
||||||
vecTo8dir(placer:get_look_dir()) or 0
|
local pos
|
||||||
|
if udef and udef.buildable_to then
|
||||||
|
pos = under
|
||||||
|
else
|
||||||
|
pos = pointed_thing.above
|
||||||
|
end
|
||||||
|
|
||||||
for i = 0,w*h-1 do
|
local dir8 = logikraft.vecTo8dir(placer:get_look_dir())
|
||||||
local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w)
|
|
||||||
local node_def = minetest.registered_nodes[minetest.get_node(loc).name]
|
for i = 1,w do
|
||||||
if not node_def or not node_def.buildable_to then
|
for j = 1,h do
|
||||||
-- Then we cannot build here
|
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 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
|
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
|
logikraft.componentnodes[nodename] = {name = name, x = x, y = y}
|
||||||
minetest.set_node(pos, {name = "circuits: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 = "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
|
end
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_node("circuits: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"}
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
-- Everything is described as if dir=0
|
||||||
|
-- z+
|
||||||
|
-- /|\ w
|
||||||
|
-- | XXX h
|
||||||
|
-- +−−−−−−−−−−−> x+
|
||||||
|
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
|
||||||
|
logikraft.components = {
|
||||||
|
["and"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 3,
|
||||||
|
ports = {
|
||||||
|
left = {"in_2",nil,"in_1"},
|
||||||
|
right = {nil,"out",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = (inz["in_1"] * inz["in_2"] == 0) and 0 or 1} end
|
||||||
|
},
|
||||||
|
["nand"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 3,
|
||||||
|
ports = {
|
||||||
|
left = {"in_2",nil,"in_1"},
|
||||||
|
right = {nil,"out",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = (inz["in_1"] * inz["in_2"] == 0) and 1 or 0} end
|
||||||
|
},
|
||||||
|
["or"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 3,
|
||||||
|
ports = {
|
||||||
|
left = {"in_2",nil,"in_1"},
|
||||||
|
right = {nil,"out",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = (inz["in_1"] + inz["in_2"] == 0) and 0 or 1} end
|
||||||
|
},
|
||||||
|
["nor"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 3,
|
||||||
|
ports = {
|
||||||
|
left = {"in_2",nil,"in_1"},
|
||||||
|
right = {nil,"out",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = (inz["in_1"] + inz["in_2"] == 0) and 1 or 0} end
|
||||||
|
},
|
||||||
|
["xor"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 3,
|
||||||
|
ports = {
|
||||||
|
left = {"in_2",nil,"in_1"},
|
||||||
|
right = {nil,"out",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = (inz["in_1"] + inz["in_2"] == 1) and 1 or 0} end
|
||||||
|
},
|
||||||
|
["not"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
left = {"in"},
|
||||||
|
right = {"out"}
|
||||||
|
},
|
||||||
|
inputs = {"in"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = 1 - inz["in"]} end
|
||||||
|
},
|
||||||
|
["on"] = {
|
||||||
|
width = 1,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
right = {"out"}
|
||||||
|
},
|
||||||
|
inputs = {},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = 1} end
|
||||||
|
},
|
||||||
|
["off"] = {
|
||||||
|
width = 1,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
right = {"out"}
|
||||||
|
},
|
||||||
|
inputs = {},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) return {out = 0} end
|
||||||
|
},
|
||||||
|
["switch"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
left = {"in"},
|
||||||
|
right = {"out"},
|
||||||
|
top = {"activate",nil}
|
||||||
|
},
|
||||||
|
inputs = {"in","activate"},
|
||||||
|
outputs = {"out"},
|
||||||
|
compute = function(inz) if inz["activate"]==1 then return {out = inz["in"]} else return {} end end
|
||||||
|
},
|
||||||
|
["demux"] = {
|
||||||
|
width = 2,
|
||||||
|
height = 8,
|
||||||
|
ports = {
|
||||||
|
left = {nil,nil,nil,nil,nil,"in_3","in_2","in_1"},
|
||||||
|
right = {"out_8","out_7","out_6","out_5","out_4","out_3","out_2","out_1"}
|
||||||
|
},
|
||||||
|
inputs = {"in_1","in_2","in_3"},
|
||||||
|
outputs = {"out_8","out_7","out_6","out_5","out_4","out_3","out_2","out_1"},
|
||||||
|
compute = function(inz) return {
|
||||||
|
out_1 = (inz.in_1 == 0) and (inz.in_2 == 0) and (inz.in_3 == 0) and 1 or 0,
|
||||||
|
out_2 = (inz.in_1 == 1) and (inz.in_2 == 0) and (inz.in_3 == 0) and 1 or 0,
|
||||||
|
out_3 = (inz.in_1 == 0) and (inz.in_2 == 1) and (inz.in_3 == 0) and 1 or 0,
|
||||||
|
out_4 = (inz.in_1 == 1) and (inz.in_2 == 1) and (inz.in_3 == 0) and 1 or 0,
|
||||||
|
out_5 = (inz.in_1 == 0) and (inz.in_2 == 0) and (inz.in_3 == 1) and 1 or 0,
|
||||||
|
out_6 = (inz.in_1 == 1) and (inz.in_2 == 0) and (inz.in_3 == 1) and 1 or 0,
|
||||||
|
out_7 = (inz.in_1 == 0) and (inz.in_2 == 1) and (inz.in_3 == 1) and 1 or 0,
|
||||||
|
out_8 = (inz.in_1 == 1) and (inz.in_2 == 1) and (inz.in_3 == 1) and 1 or 0,
|
||||||
|
} end
|
||||||
|
},
|
||||||
|
["input"] = {
|
||||||
|
width = 1,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
right = {"out"}
|
||||||
|
},
|
||||||
|
inputs = {},
|
||||||
|
outputs = {"out"}
|
||||||
|
},
|
||||||
|
["output"] = {
|
||||||
|
width = 1,
|
||||||
|
height = 1,
|
||||||
|
ports = {
|
||||||
|
left = {"in"}
|
||||||
|
},
|
||||||
|
inputs = {"in"},
|
||||||
|
outputs = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for name,cmp in pairs(logikraft.components)
|
||||||
|
do
|
||||||
|
logikraft.registerComponent(name,cmp)
|
||||||
|
end
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
-- }
|
-- }
|
||||||
-- }
|
-- }
|
||||||
|
|
||||||
function circuits.computeCircuit(circuit, input)
|
function logikraft.computeCircuit(circuit, input)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ local function has_value (tab, len, val)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function circuits.discoverBlocks(pos)
|
function logikraft.discoverBlocks(pos)
|
||||||
local i = 2
|
local i = 2
|
||||||
local j = 1
|
local j = 1
|
||||||
local poz = {}
|
local poz = {}
|
||||||
@@ -31,9 +31,6 @@ function circuits.discoverBlocks(pos)
|
|||||||
do
|
do
|
||||||
local p = poz[j]
|
local p = poz[j]
|
||||||
j = j+1
|
j = j+1
|
||||||
print(dump(poz))
|
|
||||||
print(i)
|
|
||||||
print(j)
|
|
||||||
local neighbourhood = {
|
local neighbourhood = {
|
||||||
{x = p.x+1, y = p.y, z = p.z},
|
{x = p.x+1, y = p.y, z = p.z},
|
||||||
{x = p.x-1, y = p.y, z = p.z},
|
{x = p.x-1, y = p.y, z = p.z},
|
||||||
@@ -52,6 +49,257 @@ function circuits.discoverBlocks(pos)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.chat_send_all("Discovered ".. (i-1) .. " blocks")
|
minetest.chat_send_all("Discovered ".. (i-1) .. " blocks")
|
||||||
|
return poz
|
||||||
|
end
|
||||||
|
|
||||||
|
local function collapseConnectionsStep(connz)
|
||||||
|
for k,conna in ipairs(connz)
|
||||||
|
do
|
||||||
|
for l,connb in ipairs(connz)
|
||||||
|
do
|
||||||
|
if k<l
|
||||||
|
then
|
||||||
|
local match = table.findMatch1(conna.conn,connb.conn)
|
||||||
|
if match
|
||||||
|
then
|
||||||
|
-- we add connb\m[2] to conna\m[1], and then we remove connb
|
||||||
|
table.remove(conna.conn,match[1])
|
||||||
|
for i,x in pairs(connb.conn) do
|
||||||
|
if i ~= match[2] then table.insert(conna.conn,x) end
|
||||||
|
end
|
||||||
|
-- And we merge the node lists
|
||||||
|
for i,x in ipairs(connb.nodes) do
|
||||||
|
table.insert(conna.nodes,x)
|
||||||
|
end
|
||||||
|
table.remove(connz,l)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
function logikraft.collapseConnections(connz)
|
||||||
|
while collapseConnectionsStep(connz)
|
||||||
|
do end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- side: left = 3, top = 0, right = 1, bottom = 2
|
||||||
|
local function registerPort(pos,side,j,dir,h,w)
|
||||||
|
local a =
|
||||||
|
(side == 0 and (j-1)) or
|
||||||
|
(side == 1 and (w-1)) or
|
||||||
|
(side == 2 and (j-1)) or
|
||||||
|
(side == 3 and 0) or
|
||||||
|
nil
|
||||||
|
local b =
|
||||||
|
(side == 0 and (h-1)) or
|
||||||
|
(side == 1 and (j-1)) or
|
||||||
|
(side == 2 and 0) or
|
||||||
|
(side == 3 and (j-1)) or
|
||||||
|
nil
|
||||||
|
local d = math.fmod(side + dir + 1,2)
|
||||||
|
local out =
|
||||||
|
(dir == 0 and {x=pos.x+a,y=pos.y,z=pos.z+b,d=d}) or
|
||||||
|
(dir == 1 and {x=pos.x+b,y=pos.y,z=pos.z-a,d=d}) or
|
||||||
|
(dir == 2 and {x=pos.x-a,y=pos.y,z=pos.z-b,d=d}) or
|
||||||
|
(dir == 3 and {x=pos.x-b,y=pos.y,z=pos.z+a,d=d}) or
|
||||||
|
nil
|
||||||
|
out.x = (math.fmod(side + dir,4)==3 and out.x-1) or out.x
|
||||||
|
out.z = (math.fmod(side + dir,4)==2 and out.z-1) or out.z
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
end
|
||||||
|
local function registerComponentPorts(pos,component,ports,dir)
|
||||||
|
-- Left
|
||||||
|
local dirz = {
|
||||||
|
[0] = component.ports.top,
|
||||||
|
[1] = component.ports.right,
|
||||||
|
[2] = component.ports.bottom,
|
||||||
|
[3] = component.ports.left
|
||||||
|
}
|
||||||
|
for side,prts in pairs(dirz)
|
||||||
|
do
|
||||||
|
for j,n in pairs(prts)
|
||||||
|
do
|
||||||
|
ports[n] = registerPort(pos,side,j,dir,component.height,component.width)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- {x,y,z,d=0 if x+, 1 if z+}
|
||||||
|
function logikraft.compileCircuit(poz)
|
||||||
|
|
||||||
|
local components = {}
|
||||||
|
local connections = {}
|
||||||
|
for i,pos in ipairs(poz)
|
||||||
|
do
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
if logikraft.cablenodes[node.name]
|
||||||
|
then
|
||||||
|
-- It is a cable
|
||||||
|
local conns = logikraft.cables[logikraft.cablenodes[node.name]].conns
|
||||||
|
for j,c in ipairs(conns)
|
||||||
|
do
|
||||||
|
if not (c == 1 or c == 2 or c == 4 or c == 8 or c == 17 or c == 18 or c == 20 or c == 24)
|
||||||
|
then
|
||||||
|
local newconn = {}
|
||||||
|
if math.fmod(math.floor(c/1),2)==1
|
||||||
|
then table.insert(newconn,{x=pos.x-1,y=pos.y,z=pos.z,d=0}) end
|
||||||
|
if math.fmod(math.floor(c/2),2)==1
|
||||||
|
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z-1,d=1}) end
|
||||||
|
if math.fmod(math.floor(c/4),2)==1
|
||||||
|
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z,d=0}) end
|
||||||
|
if math.fmod(math.floor(c/8),2)==1
|
||||||
|
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z,d=1}) end
|
||||||
|
local pos2 = table.copy(pos)
|
||||||
|
pos2.conn = j
|
||||||
|
table.insert(connections,{nodes = {pos2},conn = newconn})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif logikraft.componentnodes[node.name]
|
||||||
|
then
|
||||||
|
-- It is a component
|
||||||
|
local cpblock = logikraft.componentnodes[node.name]
|
||||||
|
if cpblock.x == 1 and cpblock.y == 1
|
||||||
|
then
|
||||||
|
local component = logikraft.components[cpblock.name]
|
||||||
|
local cp = {type = cpblock.name, ports = {}, pos = pos}
|
||||||
|
|
||||||
|
registerComponentPorts(pos,component,cp.ports,node.param2)
|
||||||
|
|
||||||
|
-- If we have a meta name, we save it
|
||||||
|
local meta = minetest.get_meta(pos)
|
||||||
|
if meta:get_string("logikraft:name") ~= ""
|
||||||
|
then cp.name = meta:get_string("logikraft:name")
|
||||||
|
end
|
||||||
|
table.insert(components,cp)
|
||||||
|
-- We do everything in the 0 0 block, we ignore the others
|
||||||
|
end
|
||||||
|
-- else we just ignore
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print(dump(components))
|
||||||
|
|
||||||
|
logikraft.collapseConnections(connections)
|
||||||
|
|
||||||
|
-- We replace the index of the connection in the components
|
||||||
|
for k,cmp in pairs(components)
|
||||||
|
do
|
||||||
|
for pn,x in pairs(cmp.ports)
|
||||||
|
do
|
||||||
|
-- We find the connection with this port
|
||||||
|
local theconn = 0
|
||||||
|
for i,connz in ipairs(connections)
|
||||||
|
do
|
||||||
|
if table.getkey1(connz.conn,x) -- if x in connz
|
||||||
|
then theconn = i end
|
||||||
|
end
|
||||||
|
cmp.ports[pn] = theconn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- We remove conn data from the connections, the index in now enough
|
||||||
|
for k,conn in ipairs(connections)
|
||||||
|
do
|
||||||
|
connections[k] = connections[k].nodes
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We remove inputs and outputs, as we only store ports
|
||||||
|
local inputs = {}
|
||||||
|
local outputs = {}
|
||||||
|
local cleanedComponents = {}
|
||||||
|
for i,cmp in ipairs(components)
|
||||||
|
do
|
||||||
|
local name = cmp.name or cmp.type.."_unknown_"..tostring(i)
|
||||||
|
if cmp.type == "input"
|
||||||
|
then
|
||||||
|
inputs[name] = {
|
||||||
|
conn = cmp.ports["out"],
|
||||||
|
pos = cmp.pos
|
||||||
|
}
|
||||||
|
elseif cmp.type == "output"
|
||||||
|
then
|
||||||
|
outputs[name] = {
|
||||||
|
conn = cmp.ports["in"],
|
||||||
|
pos = cmp.pos
|
||||||
|
}
|
||||||
|
else
|
||||||
|
table.insert(cleanedComponents,cmp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO sort components in inferred components order, for faster computing
|
||||||
|
|
||||||
|
--
|
||||||
|
print("Discovered:")
|
||||||
|
print(dump(cleanedComponents))
|
||||||
|
print(dump(inputs))
|
||||||
|
print(dump(outputs))
|
||||||
|
print(dump(connections))
|
||||||
|
--]]
|
||||||
|
return {components = cleanedComponents, inputs=inputs, outputs=outputs, connections = connections}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function computeStep(circuit,connvals,computed)
|
||||||
|
local effective = false
|
||||||
|
for i,cmp in pairs(circuit.components)
|
||||||
|
do
|
||||||
|
if not computed[i]
|
||||||
|
then
|
||||||
|
-- We check that all the inputs have a value
|
||||||
|
local allvalue = true
|
||||||
|
local inputs = {}
|
||||||
|
for k,name in pairs(logikraft.components[cmp.type].inputs)
|
||||||
|
do
|
||||||
|
allvalue = allvalue and (cmp.ports[name] and connvals[cmp.ports[name]] and true)
|
||||||
|
inputs[name] = allvalue and connvals[cmp.ports[name]]
|
||||||
|
end
|
||||||
|
|
||||||
|
if allvalue
|
||||||
|
then
|
||||||
|
local output = logikraft.components[cmp.type].compute(inputs)
|
||||||
|
print("Component "..cmp.type)
|
||||||
|
for name,v in pairs(output)
|
||||||
|
do
|
||||||
|
if cmp.ports[name] and connvals[cmp.ports[name]]
|
||||||
|
then
|
||||||
|
-- Already set
|
||||||
|
minetest.chat_send_all("Le composant "..cmp.type.." ne peut pas écrire")
|
||||||
|
else
|
||||||
|
connvals[cmp.ports[name]] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
computed[i] = true
|
||||||
|
effective = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return effective
|
||||||
|
end
|
||||||
|
function logikraft.compute(circuit,inz)
|
||||||
|
local connvals = {}
|
||||||
|
local computed = {}
|
||||||
|
-- We read the inputs components
|
||||||
|
for name,x in pairs(circuit.inputs)
|
||||||
|
do
|
||||||
|
if x.conn and inz[name]
|
||||||
|
then connvals[x.conn] = inz[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while computeStep(circuit,connvals,computed)
|
||||||
|
do end
|
||||||
|
|
||||||
|
-- We read the output components
|
||||||
|
local outz = {}
|
||||||
|
for name,x in pairs(circuit.outputs)
|
||||||
|
do
|
||||||
|
if x.conn
|
||||||
|
then outz[name] = connvals[x.conn]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return outz
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
local _contexts = {}
|
||||||
|
|
||||||
|
|
||||||
|
function logikraft.showNameFormspec(playername,pos)
|
||||||
|
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
local nodedesc = minetest.registered_nodes[node.name].description
|
||||||
|
|
||||||
|
local text = "Please enter the name of the " .. nodedesc .. " at ("..tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z)..")"
|
||||||
|
|
||||||
|
local meta = minetest.get_meta(pos)
|
||||||
|
local oldname = meta:get_string("logikraft:name") or ""
|
||||||
|
|
||||||
|
|
||||||
|
local formspec = {
|
||||||
|
"formspec_version[4]",
|
||||||
|
"size[6,3.476]",
|
||||||
|
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
||||||
|
"field[0.375,1.25;5.25,0.8;name;".. minetest.formspec_escape(nodedesc) .. " name;"..oldname.."]",
|
||||||
|
"button_exit[1.5,2.3;3,0.8;commit;OK]"
|
||||||
|
}
|
||||||
|
_contexts[playername] = pos
|
||||||
|
minetest.show_formspec(playername, "logikraft:nameFormSpec", table.concat(formspec, ""))
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
if formname ~= "logikraft:nameFormSpec" then return end
|
||||||
|
|
||||||
|
if fields.commit then
|
||||||
|
local pname = player:get_player_name()
|
||||||
|
if _contexts[pname]
|
||||||
|
then
|
||||||
|
local meta = minetest.get_meta(_contexts[pname])
|
||||||
|
meta:set_string("logikraft:name",fields.name)
|
||||||
|
minetest.chat_send_player(pname, "Set to "..fields.name)
|
||||||
|
else
|
||||||
|
minetest.chat_send_player(pname, "Could not get pos data from formspec")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
local function posToStr(pos)
|
||||||
|
return pos.x..";"..pos.y..";"..pos.z
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getCircuitFormspec(inz,outz)
|
||||||
|
local formspec = {
|
||||||
|
"formspec_version[4]",
|
||||||
|
"size[9,",tostring(2.625+(table.length(inz)*0.5)+(table.length(outz)*0.5)),"]",
|
||||||
|
"label[0.375,0.5;", minetest.formspec_escape("Inputs"), "]"
|
||||||
|
}
|
||||||
|
local y = 1.0
|
||||||
|
for name,x in pairs(inz)
|
||||||
|
do
|
||||||
|
if x == 0
|
||||||
|
then table.insert(formspec,"button[0.375,"..tostring(y-0.25)..";0.5,0.5;input_"..name..";OFF]")
|
||||||
|
else table.insert(formspec,"button[0.375,"..tostring(y-0.25)..";0.5,0.5;input_"..name..";ON]")
|
||||||
|
end
|
||||||
|
table.insert(formspec,"label[1.0,"..tostring(y)..";"..minetest.formspec_escape(name).."]")
|
||||||
|
y = y + 0.5
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(formspec,"label[0.375,"..tostring(y)..";".. minetest.formspec_escape("Outputs").. "]")
|
||||||
|
y = y + 0.5
|
||||||
|
|
||||||
|
for name,x in pairs(outz)
|
||||||
|
do
|
||||||
|
table.insert(formspec,"style_type[label;bold,font_size=12px]")
|
||||||
|
if x == 0
|
||||||
|
then table.insert(formspec,"label[0.375,"..tostring(y)..";OFF]")
|
||||||
|
else table.insert(formspec,"label[0.375,"..tostring(y)..";ON]")
|
||||||
|
end
|
||||||
|
table.insert(formspec,"style_type[label;normal,font_size=10px]")
|
||||||
|
table.insert(formspec,"label[1.0,"..tostring(y)..";"..minetest.formspec_escape(name).."]")
|
||||||
|
y = y + 0.5
|
||||||
|
end
|
||||||
|
table.insert(formspec,"button_exit[3,"..tostring(y-0.15)..";3,1;commit;OK]")
|
||||||
|
|
||||||
|
return table.concat(formspec,"")
|
||||||
|
end
|
||||||
|
|
||||||
|
function logikraft.showCircuitFormspec(playername,pos)
|
||||||
|
local poz = logikraft.discoverBlocks(pos)
|
||||||
|
local circuit = logikraft.compileCircuit(poz)
|
||||||
|
|
||||||
|
local cn = posToStr(pos).."_"..playername
|
||||||
|
if not _contexts[cn]
|
||||||
|
then
|
||||||
|
_contexts[cn] = {}
|
||||||
|
for name,x in pairs(circuit.inputs)
|
||||||
|
do _contexts[cn][name] = 0 end
|
||||||
|
end
|
||||||
|
print(dump(_contexts[cn]))
|
||||||
|
local formspec = getCircuitFormspec(_contexts[cn],logikraft.compute(circuit,_contexts[cn]))
|
||||||
|
_contexts[playername] = pos
|
||||||
|
minetest.show_formspec(playername, "logikraft:circuitFormSpec", formspec)
|
||||||
|
end
|
||||||
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
if formname ~= "logikraft:circuitFormSpec" then return end
|
||||||
|
|
||||||
|
local pname = player:get_player_name()
|
||||||
|
if _contexts[pname]
|
||||||
|
then
|
||||||
|
local cn = posToStr(_contexts[pname]).."_"..pname
|
||||||
|
-- We update the clicked field
|
||||||
|
for f,x in pairs(fields)
|
||||||
|
do
|
||||||
|
if string.startsWith(f,"input_")
|
||||||
|
then
|
||||||
|
local v = (x == "OFF") and 1 or 0
|
||||||
|
_contexts[cn][string.without(f,"input_")] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not fields.commit
|
||||||
|
then logikraft.showCircuitFormspec(pname,_contexts[pname])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
minetest.chat_send_player(pname, "Could not get pos data from formspec")
|
||||||
|
end
|
||||||
|
end)
|
||||||
@@ -1,47 +1,21 @@
|
|||||||
|
|
||||||
circuits = {}
|
logikraft = {}
|
||||||
|
|
||||||
dofile(minetest.get_modpath("circuits") .. "/utils.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/utils.lua")
|
||||||
dofile(minetest.get_modpath("circuits") .. "/circuits.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/formspecs.lua")
|
||||||
dofile(minetest.get_modpath("circuits") .. "/blocks/circuit.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/circuits.lua")
|
||||||
dofile(minetest.get_modpath("circuits") .. "/blocks/component.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/circuit.lua")
|
||||||
dofile(minetest.get_modpath("circuits") .. "/blocks/cable.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/component.lua")
|
||||||
dofile(minetest.get_modpath("circuits") .. "/items.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/components_register.lua")
|
||||||
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/cable.lua")
|
||||||
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/cables_register.lua")
|
||||||
|
dofile(minetest.get_modpath("logikraft") .. "/items.lua")
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
player:hud_set_hotbar_itemcount(24)
|
player:hud_set_hotbar_itemcount(24)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_node("circuits:inputBlock", {
|
|
||||||
description = "Input Block",
|
|
||||||
tiles = {"input_block.png"},
|
|
||||||
after_place_node = function(pos, placer)
|
|
||||||
local meta = minetest.get_meta(pos)
|
|
||||||
meta:set_string("formspec",circuits.setNameFormspec("Input Block"),meta:get_string("name"))
|
|
||||||
end,
|
|
||||||
on_receive_fields = function(pos, formname, fields, player)
|
|
||||||
if fields.quit then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
minetest.get_meta(pos):set_string("name",fields.name)
|
|
||||||
end,
|
|
||||||
groups = {circuitry = 1,dig_immediate = 3}
|
|
||||||
})
|
|
||||||
minetest.register_node("circuits:outputBlock", {
|
|
||||||
description = "Output Block",
|
|
||||||
tiles = {"output_block.png"},
|
|
||||||
after_place_node = function(pos, placer)
|
|
||||||
local meta = minetest.get_meta(pos)
|
|
||||||
meta:set_string("formspec",circuits.setNameFormspec("Output Block"),meta:get_string("name"))
|
|
||||||
end,
|
|
||||||
on_receive_fields = function(pos, formname, fields, player)
|
|
||||||
if fields.quit then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
minetest.get_meta(pos):set_string("name",fields.name)
|
|
||||||
end,
|
|
||||||
groups = {circuitry = 1,dig_immediate = 3}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +1,170 @@
|
|||||||
minetest.register_craftitem("circuits:rotator", {
|
minetest.register_craftitem("logikraft:rotator", {
|
||||||
description = "The rotator",
|
description = "The rotator",
|
||||||
inventory_image = "rotator.png",
|
inventory_image = "rotator.png",
|
||||||
on_place = function(itemstack, user, pointed_thing)
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
local node = minetest.get_node(pointed_thing.under)
|
local node = minetest.get_node(pointed_thing.under)
|
||||||
if not string.startsWith(node.name,"circuits:cable_")
|
if not logikraft.cablenodes[node.name]
|
||||||
then return itemstack
|
then return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
local new = circuits.rotateCable(string.without(node.name,"circuits:cable_"),1)
|
local new = logikraft.rotateCable(logikraft.cablenodes[node.name],1)
|
||||||
minetest.swap_node(pointed_thing.under, {name = "circuits:cable_" .. new})
|
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
minetest.register_craftitem("circuits:linker", {
|
minetest.register_craftitem("logikraft:linker", {
|
||||||
description = "The linker",
|
description = "The linker",
|
||||||
inventory_image = "linker.png",
|
inventory_image = "linker.png",
|
||||||
on_place = function(itemstack, user, pointed_thing)
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
local node = minetest.get_node(pointed_thing.under)
|
local meta = itemstack:get_meta()
|
||||||
if not string.startsWith(node.name,"circuits:cable_")
|
if meta:get_int("logikraft:selected_nb") ~= 0
|
||||||
then return itemstack
|
|
||||||
end
|
|
||||||
|
|
||||||
local index = circuits.lookingAtNbIndex(user,pointed_thing.under)
|
|
||||||
|
|
||||||
-- We check if we clicked the last selected node
|
|
||||||
local meta = minetest.get_meta(pointed_thing.under)
|
|
||||||
if meta:get_int("circuits:last_selected_nb") ~= 0
|
|
||||||
then
|
then
|
||||||
-- We connect
|
local selectednb = meta:get_int("logikraft:selected_nb")
|
||||||
local firsti = meta:get_int("circuits:last_selected_nb")
|
local selectedx = meta:get_int("logikraft:selected_x")
|
||||||
local new = circuits.connectInCable(string.without(node.name,"circuits:cable_"),firsti,index)
|
local selectedy = meta:get_int("logikraft:selected_y")
|
||||||
minetest.swap_node(pointed_thing.under, {name = "circuits:cable_" .. new})
|
local selectedz = meta:get_int("logikraft:selected_z")
|
||||||
|
local selectedpos = {x = selectedx, y = selectedy, z = selectedz}
|
||||||
|
local pos = logikraft.cablenodes[minetest.get_node(pointed_thing.under).name] and pointed_thing.under or pointed_thing.above
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
|
||||||
-- We destroy the saved selected nb
|
local thisnb = logikraft.cablenodes[minetest.get_node(pointed_thing.under).name] and logikraft.lookingAtNbIndex(user,pointed_thing.under) or -1
|
||||||
meta:set_int("circuits:last_selected_nb",0)
|
if selectedx == pos.x and selectedy == pos.y and selectedz == pos.z
|
||||||
|
then
|
||||||
|
if(selectednb ~= thisnb and thisnb ~= -1 and selectednb ~= -1)
|
||||||
|
then
|
||||||
|
local new = logikraft.connectInCable(logikraft.cablenodes[node.name],selectednb,thisnb)
|
||||||
|
minetest.swap_node(pos, {name = "logikraft:cable_" .. new})
|
||||||
|
end
|
||||||
|
meta:set_int("logikraft:selected_nb",0)
|
||||||
|
meta:set_string("inventory_image","linker.png")
|
||||||
|
return itemstack
|
||||||
|
else
|
||||||
|
local new = logikraft.connectContiguousCable(
|
||||||
|
logikraft.cablenodes[minetest.get_node(selectedpos).name],selectedpos,(selectednb ~= -1 and selectednb) or nil,
|
||||||
|
logikraft.cablenodes[node.name],pos,(thisnb ~= -1 and thisnb) or nil)
|
||||||
|
if new
|
||||||
|
then
|
||||||
|
minetest.swap_node(selectedpos, {name = "logikraft:cable_" .. new[1]})
|
||||||
|
minetest.swap_node(pos, {name = "logikraft:cable_" .. new[2]})
|
||||||
|
meta:set_int("logikraft:selected_nb",0)
|
||||||
|
meta:set_string("inventory_image","linker.png")
|
||||||
|
else
|
||||||
|
minetest.chat_send_player(user:get_player_name(),"Les cables sont trop loins l'un de l'autre :/")
|
||||||
|
end
|
||||||
|
return itemstack
|
||||||
|
end
|
||||||
else
|
else
|
||||||
-- We save the selected nb
|
|
||||||
meta:set_int("circuits:last_selected_nb",index)
|
local pos = logikraft.cablenodes[minetest.get_node(pointed_thing.under).name] and pointed_thing.under or pointed_thing.above
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
|
||||||
|
if logikraft.cablenodes[node.name]
|
||||||
|
then
|
||||||
|
-- If we are looking at a cable we activate and store data
|
||||||
|
local thisnb = logikraft.cablenodes[minetest.get_node(pointed_thing.under).name] and logikraft.lookingAtNbIndex(user,pointed_thing.under) or -1
|
||||||
|
|
||||||
|
meta:set_int("logikraft:selected_nb",thisnb)
|
||||||
|
meta:set_int("logikraft:selected_x",pos.x)
|
||||||
|
meta:set_int("logikraft:selected_y",pos.y)
|
||||||
|
meta:set_int("logikraft:selected_z",pos.z)
|
||||||
|
meta:set_string("inventory_image","linker_activated.png")
|
||||||
|
else
|
||||||
|
-- Else we place a cable
|
||||||
|
local node_def = minetest.registered_nodes[node.name]
|
||||||
|
if not (node_def and node_def.buildable_to)
|
||||||
|
then return itemstack end
|
||||||
|
local new = nil
|
||||||
|
local poz = {
|
||||||
|
{x = pos.x+1, y = pos.y, z = pos.z},
|
||||||
|
{x = pos.x, y = pos.y, z = pos.z+1},
|
||||||
|
{x = pos.x-1, y = pos.y, z = pos.z},
|
||||||
|
{x = pos.x, y = pos.y, z = pos.z-1}
|
||||||
|
}
|
||||||
|
for k,pos2 in pairs(poz)
|
||||||
|
do
|
||||||
|
local cable2 = minetest.get_node(pos2)
|
||||||
|
if logikraft.cablenodes[cable2.name]
|
||||||
|
then
|
||||||
|
local contiguous = logikraft.connectContiguousCable(new, pos, 1, logikraft.cablenodes[cable2.name], pos2, nil)
|
||||||
|
new = contiguous[1]
|
||||||
|
minetest.swap_node(pos2, {name = "logikraft:cable_" .. contiguous[2]})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
minetest.add_node(pos, {name = "logikraft:cable_" .. (new or "NSEW1")})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return itemstack
|
||||||
|
end,
|
||||||
|
on_secondary_use = function(itemstack, user, pointed_thing)
|
||||||
|
-- Looking at nothing -> we dispose of the data
|
||||||
|
local meta = itemstack:get_meta()
|
||||||
|
meta:set_int("logikraft:selected_nb",0)
|
||||||
|
meta:set_string("inventory_image","linker.png")
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
minetest.register_craftitem("circuits:unlinker", {
|
minetest.register_craftitem("logikraft:unlinker", {
|
||||||
description = "The unlinker",
|
description = "The unlinker",
|
||||||
inventory_image = "unlinker.png",
|
inventory_image = "unlinker.png",
|
||||||
on_place = function(itemstack, user, pointed_thing)
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
local node = minetest.get_node(pointed_thing.under)
|
local node = minetest.get_node(pointed_thing.under)
|
||||||
if not string.startsWith(node.name,"circuits:cable_")
|
if not logikraft.cablenodes[node.name]
|
||||||
then return itemstack
|
then return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
local index = circuits.lookingAtNbIndex(user,pointed_thing.under)
|
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||||
|
print(logikraft.cablenodes[node.name])
|
||||||
|
print(dump(logikraft.cables[logikraft.cablenodes[node.name]]))
|
||||||
|
local new = logikraft.disconnectInCable(logikraft.cablenodes[node.name],index)
|
||||||
|
if new
|
||||||
|
then minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||||
|
else minetest.remove_node(pointed_thing.under)
|
||||||
|
end
|
||||||
|
|
||||||
local new = circuits.disconnectInCable(string.without(node.name,"circuits:cable_"),index)
|
return itemstack
|
||||||
minetest.swap_node(pointed_thing.under, {name = "circuits:cable_" .. new})
|
end
|
||||||
|
})
|
||||||
|
minetest.register_craftitem("logikraft:resizer", {
|
||||||
|
description = "The Resizer",
|
||||||
|
inventory_image = "resizer.png",
|
||||||
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
|
local node = minetest.get_node(pointed_thing.under)
|
||||||
|
if not logikraft.cablenodes[node.name]
|
||||||
|
then return itemstack
|
||||||
|
end
|
||||||
|
|
||||||
|
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||||
|
|
||||||
|
local new = logikraft.resizeInCable(logikraft.cablenodes[node.name],index)
|
||||||
|
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||||
|
|
||||||
|
return itemstack
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
minetest.register_craftitem("logikraft:editor", {
|
||||||
|
description = "The Editor",
|
||||||
|
inventory_image = "editor.png",
|
||||||
|
on_place = function(itemstack, user, pointed_thing)
|
||||||
|
local node = minetest.get_node(pointed_thing.under)
|
||||||
|
if logikraft.cablenodes[node.name]
|
||||||
|
then
|
||||||
|
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||||
|
minetest.chat_send_all("Looking at nodeblock "..tostring(index))
|
||||||
|
elseif node.name == "logikraft:component_input_1_1"
|
||||||
|
then
|
||||||
|
logikraft.showNameFormspec(user:get_player_name(), pointed_thing.under)
|
||||||
|
elseif node.name == "logikraft:component_output_1_1"
|
||||||
|
then
|
||||||
|
logikraft.showNameFormspec(user:get_player_name(), pointed_thing.under)
|
||||||
|
elseif logikraft.componentnodes[node.name]
|
||||||
|
then
|
||||||
|
minetest.chat_send_all("Looking at component "..logikraft.componentnodes[node.name].name)
|
||||||
|
elseif node.name == "logikraft:circuitBlock"
|
||||||
|
then
|
||||||
|
logikraft.showCircuitFormspec(user:get_player_name(), pointed_thing.under)
|
||||||
|
end
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|||||||
|
After Width: | Height: | Size: 4.4 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 606 B After Width: | Height: | Size: 606 B |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 636 B After Width: | Height: | Size: 636 B |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.5 KiB |
|
After Width: | Height: | Size: 4.5 KiB |
|
After Width: | Height: | Size: 4.5 KiB |
@@ -30,14 +30,28 @@ function table.getkey1(arr,x)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function table.length(arr)
|
||||||
|
local c = 0
|
||||||
|
for k,v in pairs(arr)
|
||||||
|
do
|
||||||
|
c = c + 1
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
function string.startsWith(str,ex)
|
function string.startsWith(str,ex)
|
||||||
return string.sub(str,1,string.len(ex)) == ex
|
return string.sub(str,1,string.len(ex)) == ex
|
||||||
end
|
end
|
||||||
function string.without(str,ex)
|
function string.without(str,ex)
|
||||||
return string.sub(str,string.len(ex)+1,-1)
|
return string.sub(str,string.len(ex)+1,-1)
|
||||||
end
|
end
|
||||||
|
function string.endsWith(str,ex)
|
||||||
|
return string.sub(str,-1-string.len(ex),-1) == ex
|
||||||
|
end
|
||||||
|
function string.without2(str,ex,ex2)
|
||||||
|
return string.sub(str,string.len(ex)+1,-1-string.len(ex2))
|
||||||
|
end
|
||||||
function table.equals1(arr1,arr2)
|
function table.equals1(arr1,arr2)
|
||||||
for i,v in ipairs(arr1)
|
for i,v in pairs(arr1)
|
||||||
do
|
do
|
||||||
if arr2[i] ~= v
|
if arr2[i] ~= v
|
||||||
then return false
|
then return false
|
||||||
@@ -52,7 +66,7 @@ local function nodeboxDistance(nb,p)
|
|||||||
(((nb[2] < p[2]) and (p[2] - nb[2])) or ((p[2] < nb[5]) and (nb[5] - p[2])) or 0) +
|
(((nb[2] < p[2]) and (p[2] - nb[2])) or ((p[2] < nb[5]) and (nb[5] - p[2])) or 0) +
|
||||||
(((nb[3] < p[3]) and (p[3] - nb[3])) or ((p[3] < nb[6]) and (nb[6] - p[3])) or 0)
|
(((nb[3] < p[3]) and (p[3] - nb[3])) or ((p[3] < nb[6]) and (nb[6] - p[3])) or 0)
|
||||||
end
|
end
|
||||||
function circuits.nearestNodeboxIndex(nodeboxes,point)
|
function logikraft.nearestNodeboxIndex(nodeboxes,point)
|
||||||
local minindex=-1
|
local minindex=-1
|
||||||
local minvalue = 9999
|
local minvalue = 9999
|
||||||
for i,nb in ipairs(nodeboxes)
|
for i,nb in ipairs(nodeboxes)
|
||||||
@@ -67,8 +81,8 @@ function circuits.nearestNodeboxIndex(nodeboxes,point)
|
|||||||
return minindex
|
return minindex
|
||||||
end
|
end
|
||||||
|
|
||||||
function circuits.lookingAtNbIndex(user,under)
|
function logikraft.lookingAtNbIndex(user,under)
|
||||||
local range=5
|
local range=15
|
||||||
local eye = vector.add(user:get_pos(),{x=0,y=1.625,z=0})
|
local eye = vector.add(user:get_pos(),{x=0,y=1.625,z=0})
|
||||||
local objective = vector.add(eye, vector.multiply(user:get_look_dir(),range))
|
local objective = vector.add(eye, vector.multiply(user:get_look_dir(),range))
|
||||||
local ray = minetest.raycast(eye,objective,false,false)
|
local ray = minetest.raycast(eye,objective,false,false)
|
||||||
@@ -80,3 +94,101 @@ function circuits.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
|
||||||
|
|
||||||
|
function table.findMatch(ta,tb)
|
||||||
|
do
|
||||||
|
for i,x in ipairs(ta)
|
||||||
|
do
|
||||||
|
for j,y in ipairs(tb)
|
||||||
|
do
|
||||||
|
if x == y
|
||||||
|
then return {i,j}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function table.findMatch1(ta,tb)
|
||||||
|
do
|
||||||
|
for i,x in ipairs(ta)
|
||||||
|
do
|
||||||
|
for j,y in ipairs(tb)
|
||||||
|
do
|
||||||
|
if table.equals1(x,y)
|
||||||
|
then return {i,j}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function logikraft.rotateNodebox(nb,rot)
|
||||||
|
return
|
||||||
|
(math.fmod(rot,4)==0) and {nb[1],nb[2],nb[3],nb[4],nb[5],nb[6]} or
|
||||||
|
(math.fmod(rot,4)==1) and {nb[3],nb[2],-nb[1],nb[6],nb[5],-nb[4]} or
|
||||||
|
(math.fmod(rot,4)==2) and {-nb[1],nb[2],-nb[3],-nb[4],nb[5],-nb[6]} or
|
||||||
|
(math.fmod(rot,4)==3) and {-nb[3],nb[2],nb[1],-nb[6],nb[5],nb[4]} or
|
||||||
|
nb -- Should not happen
|
||||||
|
end
|
||||||
|
function logikraft.rotate4dir(conn,r)
|
||||||
|
local function rotate4dirOnce(conn)
|
||||||
|
return math.floor(conn/2) + 8*math.fmod(conn,2)
|
||||||
|
end
|
||||||
|
local x = math.fmod(conn,16)
|
||||||
|
for i=1,r do
|
||||||
|
x = rotate4dirOnce(x)
|
||||||
|
end
|
||||||
|
return (conn - math.fmod(conn,16) + x)
|
||||||
|
end
|
||||||
|
|
||||||
|
function table.constTable(val,size)
|
||||||
|
local t = {}
|
||||||
|
for i=1,size
|
||||||
|
do
|
||||||
|
table.insert(t,val)
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||