The circuitry now really computes the circuit
This commit is contained in:
parent
b907e06c5f
commit
a1177efa69
@ -6,59 +6,10 @@ 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
|
|
||||||
-- 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"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
["input"] = {
|
|
||||||
width = 1,
|
|
||||||
height = 1,
|
|
||||||
ports = {
|
|
||||||
right = {"out"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
["output"] = {
|
|
||||||
width = 1,
|
|
||||||
height = 1,
|
|
||||||
ports = {
|
|
||||||
left = {"in"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-- componentsblocks["block"] = {name = Name of the component,x,y}
|
-- componentsblocks["block"] = {name = Name of the component,x,y}
|
||||||
logikraft.componentnodes = {}
|
logikraft.componentnodes = {}
|
||||||
|
|
||||||
for name,cmp in pairs(logikraft.components)
|
function logikraft.registerComponent(name,cmp)
|
||||||
do
|
|
||||||
local w = cmp.width
|
local w = cmp.width
|
||||||
local h = cmp.height
|
local h = cmp.height
|
||||||
local cables = cmp.ports
|
local cables = cmp.ports
|
||||||
|
|||||||
64
blocks/components_register.lua
Normal file
64
blocks/components_register.lua
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
-- 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
|
||||||
|
},
|
||||||
|
["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"}
|
||||||
|
},
|
||||||
|
["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
|
||||||
65
circuits.lua
65
circuits.lua
@ -226,8 +226,10 @@ function logikraft.compileCircuit(poz)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Discovered:")
|
-- TODO sort components in inferred components order, for faster computing
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
print("Discovered:")
|
||||||
print(dump(cleanedComponents))
|
print(dump(cleanedComponents))
|
||||||
print(dump(inputs))
|
print(dump(inputs))
|
||||||
print(dump(outputs))
|
print(dump(outputs))
|
||||||
@ -237,7 +239,62 @@ function logikraft.compileCircuit(poz)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function logikraft.compute(circuit,inz)
|
local function computeStep(circuit,connvals,computed)
|
||||||
local out = {outX = inz["in1"]}
|
local effective = false
|
||||||
return out
|
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)
|
||||||
|
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
|
||||||
|
|||||||
1
init.lua
1
init.lua
@ -6,6 +6,7 @@ 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") .. "/blocks/cables_register.lua")
|
||||||
dofile(minetest.get_modpath("logikraft") .. "/blocks/iocomponent.lua")
|
dofile(minetest.get_modpath("logikraft") .. "/blocks/iocomponent.lua")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user