Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbb30f0634
|
||
|
|
2c8f51f8ba
|
||
|
|
6a2c44da91
|
||
|
|
98303dd4d1
|
||
|
|
a5cbf1d7b2
|
||
|
|
a1177efa69
|
||
|
|
b907e06c5f
|
||
|
|
0298b85323
|
@@ -63,7 +63,7 @@ function logikraft.rotateCable(name,r)
|
||||
local newconn = {}
|
||||
for i,v in ipairs(conn)
|
||||
do
|
||||
newconn[i] = rotate4dir(v,r)
|
||||
newconn[i] = logikraft.rotate4dir(v,r)
|
||||
end
|
||||
table.sort(newconn)
|
||||
return logikraft.cableFromConns(newconn)
|
||||
@@ -101,8 +101,11 @@ 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
|
||||
@@ -112,21 +115,64 @@ function logikraft.connectContiguousCable(namea,posa,a,nameb,posb,b)
|
||||
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 cia = logikraft.cables[namea].nbToConn[a]
|
||||
local cib = logikraft.cables[nameb].nbToConn[b]
|
||||
--print(dump(connsa))
|
||||
--print(dump(connsb))
|
||||
local cia2 = logikraft.connFromDir(namea,dira)
|
||||
local cib2 = logikraft.connFromDir(nameb,math.fmod(dira+2,4))
|
||||
print(dira)
|
||||
print(cia)
|
||||
print(cia2)
|
||||
print(cib)
|
||||
print(cib2)
|
||||
|
||||
|
||||
-- (nba == nil) -> Need to add dira to the a
|
||||
-- (a == nba) -> Already connected
|
||||
|
||||
@@ -6,45 +6,10 @@ local function remove_no_destruct(pos)
|
||||
minetest.check_for_falling(pos)
|
||||
end
|
||||
|
||||
-- Everything is described as if dir=0
|
||||
-- z+
|
||||
-- /|\ w
|
||||
-- | XXX h
|
||||
-- +−−−−−−−−−−−> x+
|
||||
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
|
||||
logikraft.components = {
|
||||
["and"] = {
|
||||
width = 2,
|
||||
height = 3,
|
||||
ports = {
|
||||
left = {"in_2",nil,"in_1"},
|
||||
right = {nil,"out",nil}
|
||||
}
|
||||
},
|
||||
["switch"] = {
|
||||
width = 2,
|
||||
height = 1,
|
||||
ports = {
|
||||
left = {"in"},
|
||||
right = {"out"},
|
||||
top = {"activate",nil}
|
||||
}
|
||||
},
|
||||
["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"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-- componentsblocks["block"] = {name = Name of the component,x,y}
|
||||
logikraft.componentnodes = {}
|
||||
|
||||
for name,cmp in pairs(logikraft.components)
|
||||
do
|
||||
function logikraft.registerComponent(name,cmp)
|
||||
local w = cmp.width
|
||||
local h = cmp.height
|
||||
local cables = cmp.ports
|
||||
@@ -64,7 +29,7 @@ do
|
||||
((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},
|
||||
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",
|
||||
|
||||
@@ -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
|
||||
@@ -1,11 +0,0 @@
|
||||
minetest.register_node("logikraft:inputBlock", {
|
||||
description = "Input Block",
|
||||
tiles = {"input_block.png"},
|
||||
groups = {circuitry = 1,dig_immediate = 3}
|
||||
})
|
||||
|
||||
minetest.register_node("logikraft:outputBlock", {
|
||||
description = "Output Block",
|
||||
tiles = {"output_block.png"},
|
||||
groups = {circuitry = 1,dig_immediate = 3}
|
||||
})
|
||||
@@ -60,13 +60,17 @@ local function collapseConnectionsStep(connz)
|
||||
do
|
||||
if k<l
|
||||
then
|
||||
local match = table.findMatch1(conna,connb)
|
||||
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,match[1])
|
||||
for i,x in pairs(connb) do
|
||||
if i ~= match[2] then table.insert(conna,x) end
|
||||
-- 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
|
||||
@@ -81,6 +85,50 @@ function logikraft.collapseConnections(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)
|
||||
|
||||
@@ -89,7 +137,6 @@ function logikraft.compileCircuit(poz)
|
||||
for i,pos in ipairs(poz)
|
||||
do
|
||||
local node = minetest.get_node(pos)
|
||||
print("Analyzing "..node.name)
|
||||
if logikraft.cablenodes[node.name]
|
||||
then
|
||||
-- It is a cable
|
||||
@@ -107,7 +154,9 @@ function logikraft.compileCircuit(poz)
|
||||
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
|
||||
table.insert(connections,newconn)
|
||||
local pos2 = table.copy(pos)
|
||||
pos2.conn = j
|
||||
table.insert(connections,{nodes = {pos2},conn = newconn})
|
||||
end
|
||||
end
|
||||
elseif logikraft.componentnodes[node.name]
|
||||
@@ -117,50 +166,14 @@ function logikraft.compileCircuit(poz)
|
||||
if cpblock.x == 1 and cpblock.y == 1
|
||||
then
|
||||
local component = logikraft.components[cpblock.name]
|
||||
local cp = {type = cpblock.name, ports = {}}
|
||||
-- Left
|
||||
if component.ports.left
|
||||
then
|
||||
for j,n in pairs(component.ports.left)
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x-1,y=pos.y,z=pos.z+j-1,d=0}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Right
|
||||
if component.ports.right
|
||||
then
|
||||
for j,n in pairs(component.ports.right)
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+component.width-1,y=pos.y,z=pos.z+j-1,d=0}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Top
|
||||
if component.ports.top
|
||||
then
|
||||
for j,n in pairs(component.ports.top)
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+j-1,y=pos.y,z=pos.z+component.height-1,d=1}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Bottom
|
||||
if component.ports.bottom
|
||||
then
|
||||
for j,n in pairs(component.ports.bottom)
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+j-1,y=pos.y,z=pos.z-1,d=1}
|
||||
end
|
||||
end
|
||||
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
|
||||
@@ -168,8 +181,125 @@ function logikraft.compileCircuit(poz)
|
||||
-- else we just ignore
|
||||
end
|
||||
end
|
||||
logikraft.collapseConnections(connections)
|
||||
print("Discovered:")
|
||||
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
|
||||
|
||||
@@ -37,4 +37,84 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
minetest.chat_send_player(pname, "Could not get pos data from formspec")
|
||||
end
|
||||
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)
|
||||
|
||||
@@ -6,9 +6,9 @@ dofile(minetest.get_modpath("logikraft") .. "/formspecs.lua")
|
||||
dofile(minetest.get_modpath("logikraft") .. "/circuits.lua")
|
||||
dofile(minetest.get_modpath("logikraft") .. "/blocks/circuit.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/cables_register.lua")
|
||||
dofile(minetest.get_modpath("logikraft") .. "/blocks/iocomponent.lua")
|
||||
dofile(minetest.get_modpath("logikraft") .. "/items.lua")
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
|
||||
@@ -17,7 +17,6 @@ minetest.register_craftitem("logikraft:linker", {
|
||||
inventory_image = "linker.png",
|
||||
on_place = function(itemstack, user, pointed_thing)
|
||||
local meta = itemstack:get_meta()
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
if meta:get_int("logikraft:selected_nb") ~= 0
|
||||
then
|
||||
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 selectedz = meta:get_int("logikraft:selected_z")
|
||||
local selectedpos = {x = selectedx, y = selectedy, z = selectedz}
|
||||
if logikraft.cablenodes[node.name]
|
||||
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
|
||||
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
|
||||
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
if(selectednb ~= thisnb)
|
||||
then
|
||||
local new = logikraft.connectInCable(logikraft.cablenodes[node.name],selectednb,thisnb)
|
||||
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||
end
|
||||
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
|
||||
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
local new = logikraft.connectContiguousCable(
|
||||
logikraft.cablenodes[minetest.get_node(selectedpos).name],selectedpos,selectednb,
|
||||
logikraft.cablenodes[node.name],pointed_thing.under,thisnb)
|
||||
if new
|
||||
then
|
||||
minetest.swap_node(selectedpos, {name = "logikraft:cable_" .. new[1]})
|
||||
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new[2]})
|
||||
meta:set_int("logikraft:selected_nb",0)
|
||||
meta:set_string("inventory_image","linker.png")
|
||||
else
|
||||
minetest.chat_send_all("Les cables sont trop loins l'un de l'autre :/")
|
||||
end
|
||||
minetest.chat_send_player(user:get_player_name(),"Les cables sont trop loins l'un de l'autre :/")
|
||||
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
|
||||
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")
|
||||
return itemstack
|
||||
end
|
||||
else
|
||||
|
||||
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
|
||||
meta:set_int("logikraft:selected_nb",logikraft.lookingAtNbIndex(user,pointed_thing.under))
|
||||
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_z",pointed_thing.under.z)
|
||||
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[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
|
||||
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(pointed_thing.above, {name = "logikraft:cable_NSEW1"})
|
||||
minetest.add_node(pos, {name = "logikraft:cable_" .. (new or "NSEW1")})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -140,15 +152,18 @@ minetest.register_craftitem("logikraft:editor", {
|
||||
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:inputBlock"
|
||||
elseif node.name == "logikraft:circuitBlock"
|
||||
then
|
||||
logikraft.showNameFormspec(user:get_player_name(), pointed_thing.under)
|
||||
elseif node.name == "logikraft:outputBlock"
|
||||
then
|
||||
logikraft.showNameFormspec(user:get_player_name(), pointed_thing.under)
|
||||
logikraft.showCircuitFormspec(user:get_player_name(), pointed_thing.under)
|
||||
end
|
||||
|
||||
return itemstack
|
||||
|
||||
|
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 |
@@ -30,12 +30,26 @@ function table.getkey1(arr,x)
|
||||
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)
|
||||
return string.sub(str,1,string.len(ex)) == ex
|
||||
end
|
||||
function string.without(str,ex)
|
||||
return string.sub(str,string.len(ex)+1,-1)
|
||||
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)
|
||||
for i,v in pairs(arr1)
|
||||
do
|
||||
|
||||