Compare commits

..
13 Commits
Author SHA1 Message Date
Mysaa dbb30f0634 Second Linker Upgrade 2024-05-20 19:56:30 +02:00
Mysaa 2c8f51f8ba Upgraded the linker 2024-05-20 19:06:14 +02:00
Mysaa 6a2c44da91 Removed empty file 2024-05-20 15:26:32 +02:00
Mysaa 98303dd4d1 Fixed rotated circuits ports 2024-05-20 15:24:07 +02:00
Mysaa a5cbf1d7b2 Added more components 2024-05-20 14:18:52 +02:00
Mysaa a1177efa69 The circuitry now really computes the circuit 2024-05-20 13:58:26 +02:00
Mysaa b907e06c5f Editor now can read circuits 2024-05-20 03:39:54 +02:00
Mysaa 0298b85323 Upgraded circuit generation 2024-05-20 01:02:42 +02:00
Mysaa ef8b8910b0 Completed the Editor 2024-05-20 00:14:34 +02:00
Mysaa abbc9b3d07 Rework codebase - Added Editor 2024-05-19 23:29:32 +02:00
Mysaa 9d3ab2f0e0 Added a lot of circuits code 2024-05-19 00:16:20 +02:00
Mysaa 3ea8517ff9 Added the resizer 2024-05-18 20:43:35 +02:00
Mysaa 6e4c9f1b02 Fixed the linker 2024-05-18 20:35:10 +02:00
21 changed files with 1080 additions and 465 deletions
+107 -324
View File
@@ -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
--]] --]]
logikraft.cablesnb = {} logikraft.cablenodes = {}
logikraft.cablesconn = {} logikraft.cables = {}
logikraft.cablesnbconn = {} function logikraft.registerCable(name,nodeboxes,connections,nbconn)
local function createCables(name,nodeboxes,connections,nbconn) local nodename = "logikraft:cable_"..name
minetest.register_node("logikraft: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,208 +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
logikraft.cablesnb[name] = nodeboxes logikraft.cablenodes[nodename] = name
logikraft.cablesconn[name] = connections2 logikraft.cables[name] = {
logikraft.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
end end
return nil
-- 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
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
-- "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, 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
-- Else there is no connection, we don't have to create a cable
createCables(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 = {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
--]]
logikraft.cablesfromconn = table.invert(logikraft.cablesconn)
function logikraft.rotateCable(name,r) function logikraft.rotateCable(name,r)
local conn = logikraft.cablesconn[name] local conn = logikraft.cables[name].conns
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(logikraft.cablesconn,newconn) return logikraft.cableFromConns(newconn)
end end
function logikraft.connectInCable(name,a,b) function logikraft.connectInCable(name,a,b)
local conns = table.copy(logikraft.cablesconn[name]) local conns = table.copy(logikraft.cables[name].conns)
local cia = logikraft.cablesnbconn[name][a] local cia = logikraft.cables[name].nbToConn[a]
local cib = logikraft.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]
@@ -358,16 +82,17 @@ function logikraft.connectInCable(name,a,b)
table.remove(conns,cib) table.remove(conns,cib)
table.sort(conns) table.sort(conns)
return table.getkey1(logikraft.cablesconn,conns) return logikraft.cableFromConns(conns)
end end
function logikraft.connFromDir(name,dir) function logikraft.connFromDir(name,dir)
local conns = logikraft.cablesconn[name] local conns = logikraft.cables[name].conns
local dir8 = math.pow(2,dir) local dir8 = math.pow(2,dir)
for i,c in ipairs(conns) for i,c in ipairs(conns)
do do
if (math.fmod(c,2*dir8) - dir8 >= 0) if (math.fmod(c,2*dir8) - dir8 >= 0)
then return i then
return i
end end
end end
return nil return nil
@@ -376,8 +101,11 @@ end
--[[ --[[
Returns {newcablea,newcableb} if the two cables are contiguous Returns {newcablea,newcableb} if the two cables are contiguous
Returns nil otherwise 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) 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 ( local dira = (posa.y == posb.y) and (
(posa.z + 1 == posb.z and posa.x == posb.x and 3) or (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.x + 1 == posb.x and posa.z == posb.z and 2) or
@@ -388,18 +116,63 @@ function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
then return nil then return nil
end end
local connsa = table.copy(logikraft.cablesconn[namea]) if not namea
local connsb = table.copy(logikraft.cablesconn[nameb]) then
local cia = logikraft.cablesnbconn[namea][a] namea = logikraft.cableFromConns({logikraft.rotate4dir(4,6-dira)})
local cib = logikraft.cablesnbconn[nameb][b] a = 1 -- There is only one nodebox
local cia2 = logikraft.connFromDir(namea,dira) end
local cib2 = logikraft.connFromDir(nameb,3 - dira) if not nameb
print(dira) then
print(cia) nameb = logikraft.cableFromConns({logikraft.rotate4dir(4,4-dira)})
print(cia2) b = 1 -- There is only one nodebox
print(cib) end
print(cib2)
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 -- (nba == nil) -> Need to add dira to the a
-- (a == nba) -> Already connected -- (a == nba) -> Already connected
@@ -407,7 +180,7 @@ function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
if (cia2 == nil) if (cia2 == nil)
then then
connsa[cia] = connsa[cia] + math.pow(2,dira) connsa[cia] = connsa[cia] + math.pow(2,dira)
elseif (a ~= cia2) elseif (cia ~= cia2)
then then
-- We take the v size of selected cable -- We take the v size of selected cable
local v = math.fmod(math.floor(connsa[cia]/16),2) local v = math.fmod(math.floor(connsa[cia]/16),2)
@@ -417,8 +190,8 @@ function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
if (cib2 == nil) if (cib2 == nil)
then then
connsb[cib] = connsb[cib] + math.pow(2,3-dira) connsb[cib] = connsb[cib] + math.pow(2,math.fmod(dira+2,4))
elseif (b ~= cib2) elseif (cib ~= cib2)
then then
-- We take the v size of selected cable -- We take the v size of selected cable
local v = math.fmod(math.floor(connsb[cib]/16),2) local v = math.fmod(math.floor(connsb[cib]/16),2)
@@ -429,13 +202,13 @@ function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
table.sort(connsa) table.sort(connsa)
table.sort(connsb) table.sort(connsb)
return {table.getkey1(logikraft.cablesconn,connsa),table.getkey1(logikraft.cablesconn,connsb)} return {logikraft.cableFromConns(connsa),logikraft.cableFromConns(connsb)}
end end
function logikraft.disconnectInCable(name,a) function logikraft.disconnectInCable(name,a)
local conns = table.copy(logikraft.cablesconn[name]) local conns = table.copy(logikraft.cables[name].conns)
local cindex = logikraft.cablesnbconn[name][a] local cindex = logikraft.cables[name].nbToConn[a]
local conn = conns[cindex] local conn = conns[cindex]
table.remove(conns,cindex) table.remove(conns,cindex)
@@ -453,5 +226,15 @@ function logikraft.disconnectInCable(name,a)
end end
table.sort(conns) table.sort(conns)
return table.getkey1(logikraft.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
+253
View File
@@ -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
+2 -32
View File
@@ -1,39 +1,9 @@
local _contexts = {}
function logikraft.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 logikraft.showSetNameFormspec(playerName, targetName)
minetest.show_formspec(playerName, "logikraft:setNameFormSpec", logikraft.setNameFormspec(targetName))
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "logikraft: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("logikraft:circuitBlock", { minetest.register_node("logikraft: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)
logikraft.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}
}) })
+13 -25
View File
@@ -6,26 +6,13 @@ local function remove_no_destruct(pos)
minetest.check_for_falling(pos) minetest.check_for_falling(pos)
end end
-- Everything is described as if dir=0 -- componentsblocks["block"] = {name = Name of the component,x,y}
-- z+ logikraft.componentnodes = {}
-- /|\ w
-- | XXX h
-- +−−−−−−−−−−−> x+
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
components = {}
components["and"] = {2,3,{{"in_2",nil,"in_1"},{nil,"out",nil},nil,nil}}
components["switch"] = {2,1,{{"in"},{"out"},{"activate",nil},nil}}
components["demux"] = {2,8,{{nil,nil,nil,nil,nil,"in_3","in_2","in_1"},{"out_8","out_7","out_6","out_5","out_4","out_3","out_2","out_1"},nil,nil}}
function logikraft.registerComponent(name,cmp)
-- componentsblocks["block"] = {name,x,y} local w = cmp.width
componentsblocks = {} local h = cmp.height
local cables = cmp.ports
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 x=1,w do
for y=1,h do for y=1,h do
@@ -33,15 +20,16 @@ do
local nodename = "logikraft:component_"..name.."_"..tostring(x).."_"..tostring(y) local nodename = "logikraft:component_"..name.."_"..tostring(x).."_"..tostring(y)
minetest.register_node(nodename, { minetest.register_node(nodename, {
drawtype = "block", drawtype = "block",
description = name.." component",
tiles = { tiles = {
"component_"..name..".png", "component_"..name..".png",
"component.png", "component.png",
((x==w) and (cables[2] and cables[2][y] and "component_port.png" or "component.png") or "component.png"), -- x+ ((x==w) and (cables.right and cables.right[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- ((x==1) and (cables.left and cables.left[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==h) and (cables.top and cables.top[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- ((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}, groups = {circuitry = 1,dig_immediate = 3, not_in_creative_inventory = (x==1 and y==1 and 0) or 1},
paramtype2 = "4dir", paramtype2 = "4dir",
inventory_image = (x == 1 and y == 1 and "component_"..name..".png") or nil, inventory_image = (x == 1 and y == 1 and "component_"..name..".png") or nil,
drop = "logikraft:component_"..name.."_1_1", drop = "logikraft:component_"..name.."_1_1",
@@ -109,7 +97,7 @@ do
end end
end end
}) })
componentsblocks["nodename"] = {name,x,y} logikraft.componentnodes[nodename] = {name = name, x = x, y = y}
end end
end end
+149
View File
@@ -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
+252 -4
View File
@@ -31,9 +31,6 @@ function logikraft.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 logikraft.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
+120
View File
@@ -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)
+4 -30
View File
@@ -2,46 +2,20 @@
logikraft = {} logikraft = {}
dofile(minetest.get_modpath("logikraft") .. "/utils.lua") dofile(minetest.get_modpath("logikraft") .. "/utils.lua")
dofile(minetest.get_modpath("logikraft") .. "/formspecs.lua")
dofile(minetest.get_modpath("logikraft") .. "/circuits.lua") dofile(minetest.get_modpath("logikraft") .. "/circuits.lua")
dofile(minetest.get_modpath("logikraft") .. "/blocks/circuit.lua") dofile(minetest.get_modpath("logikraft") .. "/blocks/circuit.lua")
dofile(minetest.get_modpath("logikraft") .. "/blocks/component.lua") dofile(minetest.get_modpath("logikraft") .. "/blocks/component.lua")
dofile(minetest.get_modpath("logikraft") .. "/blocks/components_register.lua")
dofile(minetest.get_modpath("logikraft") .. "/blocks/cable.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") 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("logikraft:inputBlock", {
description = "Input Block",
tiles = {"input_block.png"},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",logikraft.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("logikraft:outputBlock", {
description = "Output Block",
tiles = {"output_block.png"},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",logikraft.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}
})
+94 -37
View File
@@ -3,11 +3,11 @@ minetest.register_craftitem("logikraft: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,"logikraft:cable_") if not logikraft.cablenodes[node.name]
then return itemstack then return itemstack
end end
local new = logikraft.rotateCable(string.without(node.name,"logikraft:cable_"),1) local new = logikraft.rotateCable(logikraft.cablenodes[node.name],1)
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new}) minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
return itemstack return itemstack
end end
@@ -17,7 +17,6 @@ minetest.register_craftitem("logikraft:linker", {
inventory_image = "linker.png", inventory_image = "linker.png",
on_place = function(itemstack, user, pointed_thing) on_place = function(itemstack, user, pointed_thing)
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local node = minetest.get_node(pointed_thing.under)
if meta:get_int("logikraft:selected_nb") ~= 0 if meta:get_int("logikraft:selected_nb") ~= 0
then then
local selectednb = meta:get_int("logikraft:selected_nb") local selectednb = meta:get_int("logikraft:selected_nb")
@@ -25,60 +24,73 @@ minetest.register_craftitem("logikraft:linker", {
local selectedy = meta:get_int("logikraft:selected_y") local selectedy = meta:get_int("logikraft:selected_y")
local selectedz = meta:get_int("logikraft:selected_z") local selectedz = meta:get_int("logikraft:selected_z")
local selectedpos = {x = selectedx, y = selectedy, z = selectedz} local selectedpos = {x = selectedx, y = selectedy, z = selectedz}
if string.startsWith(node.name,"logikraft:cable_") 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)
local thisnb = logikraft.cablenodes[minetest.get_node(pointed_thing.under).name] and logikraft.lookingAtNbIndex(user,pointed_thing.under) or -1
if selectedx == pos.x and selectedy == pos.y and selectedz == pos.z
then then
if selectedx == pointed_thing.under.x and selectedy == pointed_thing.under.y and selectedz == pointed_thing.under.z if(selectednb ~= thisnb and thisnb ~= -1 and selectednb ~= -1)
then then
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under) local new = logikraft.connectInCable(logikraft.cablenodes[node.name],selectednb,thisnb)
if(selectednb ~= thisnb) minetest.swap_node(pos, {name = "logikraft:cable_" .. new})
then
local new = logikraft.connectInCable(string.without(node.name,"logikraft:cable_"),selectednb,thisnb)
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
end end
meta:set_int("logikraft:selected_nb",0) meta:set_int("logikraft:selected_nb",0)
meta:set_string("inventory_image","linker.png") meta:set_string("inventory_image","linker.png")
return itemstack
else else
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under)
local new = logikraft.connectContiguousCable( local new = logikraft.connectContiguousCable(
string.without(node.name,"logikraft:cable_"),selectedpos,selectednb, logikraft.cablenodes[minetest.get_node(selectedpos).name],selectedpos,(selectednb ~= -1 and selectednb) or nil,
string.without(minetest.get_node(pointed_thing.under).name,"logikraft:cable_"),pointed_thing.under,thisnb) logikraft.cablenodes[node.name],pos,(thisnb ~= -1 and thisnb) or nil)
if new if new
then then
minetest.swap_node(selectedpos, {name = "logikraft:cable_" .. new[1]}) minetest.swap_node(selectedpos, {name = "logikraft:cable_" .. new[1]})
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new[2]}) minetest.swap_node(pos, {name = "logikraft:cable_" .. new[2]})
meta:set_int("logikraft:selected_nb",0) meta:set_int("logikraft:selected_nb",0)
meta:set_string("inventory_image","linker.png") meta:set_string("inventory_image","linker.png")
else else
minetest.chat_send_all("Les cables sont trop loins l'un de l'autre :/") minetest.chat_send_player(user:get_player_name(),"Les cables sont trop loins l'un de l'autre :/")
end end
end
else
local node_def = minetest.registered_nodes[minetest.get_node(pointed_thing.above).name]
if not node_def or not node_def.buildable_to then
-- Then we cannot build here
return itemstack return itemstack
end end
minetest.add_node(pointed_thing.above, {name = "logikraft:cable_NSEW1"})
meta:set_int("logikraft:selected_nb",0)
meta:set_string("inventory_image","linker.png")
end
else else
if string.startsWith(node.name,"logikraft:cable_")
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 then
-- If we are looking at a cable we activate and store data -- If we are looking at a cable we activate and store data
meta:set_int("logikraft:selected_nb",logikraft.lookingAtNbIndex(user,pointed_thing.under)) 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_x",pointed_thing.under.x)
meta:set_int("logikraft:selected_y",pointed_thing.under.y) meta:set_int("logikraft:selected_nb",thisnb)
meta:set_int("logikraft:selected_z",pointed_thing.under.z) 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") meta:set_string("inventory_image","linker_activated.png")
else else
-- Else we place a cable -- Else we place a cable
local node_def = minetest.registered_nodes[minetest.get_node(pointed_thing.above).name] local node_def = minetest.registered_nodes[node.name]
if not node_def or not node_def.buildable_to then if not (node_def and node_def.buildable_to)
-- Then we cannot build here then return itemstack end
return itemstack 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(pointed_thing.above, {name = "logikraft:cable_NSEW1"}) end
minetest.add_node(pos, {name = "logikraft:cable_" .. (new or "NSEW1")})
end end
end end
@@ -97,13 +109,14 @@ minetest.register_craftitem("logikraft: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,"logikraft:cable_") if not logikraft.cablenodes[node.name]
then return itemstack then return itemstack
end end
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under) local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
print(logikraft.cablenodes[node.name])
local new = logikraft.disconnectInCable(string.without(node.name,"logikraft:cable_"),index) print(dump(logikraft.cables[logikraft.cablenodes[node.name]]))
local new = logikraft.disconnectInCable(logikraft.cablenodes[node.name],index)
if new if new
then minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new}) then minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
else minetest.remove_node(pointed_thing.under) else minetest.remove_node(pointed_thing.under)
@@ -112,3 +125,47 @@ minetest.register_craftitem("logikraft:unlinker", {
return itemstack return itemstack
end 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
end
})

Before

Width:  |  Height:  |  Size: 606 B

After

Width:  |  Height:  |  Size: 606 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Before

Width:  |  Height:  |  Size: 636 B

After

Width:  |  Height:  |  Size: 636 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

+75 -2
View File
@@ -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
@@ -68,7 +82,7 @@ function logikraft.nearestNodeboxIndex(nodeboxes,point)
end end
function logikraft.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)
@@ -119,3 +133,62 @@ end
function logikraft.addToPos4dir(dir,pos,w,h) function logikraft.addToPos4dir(dir,pos,w,h)
return logikraft.addToPos8dir(dir*2+1,pos,w,h) return logikraft.addToPos8dir(dir*2+1,pos,w,h)
end 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