Compare commits

...

3 Commits

Author SHA1 Message Date
98303dd4d1
Fixed rotated circuits ports 2024-05-20 15:24:07 +02:00
a5cbf1d7b2
Added more components 2024-05-20 14:18:52 +02:00
a1177efa69
The circuitry now really computes the circuit 2024-05-20 13:58:26 +02:00
11 changed files with 262 additions and 99 deletions

View File

@ -6,59 +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"}
}
},
["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}
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
@ -78,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",

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

View File

@ -85,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)
@ -123,50 +167,9 @@ function logikraft.compileCircuit(poz)
then
local component = logikraft.components[cpblock.name]
local cp = {type = cpblock.name, ports = {}, pos = pos}
-- 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
end
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") ~= ""
@ -178,6 +181,7 @@ function logikraft.compileCircuit(poz)
-- else we just ignore
end
end
print(dump(components))
logikraft.collapseConnections(connections)
@ -226,8 +230,10 @@ function logikraft.compileCircuit(poz)
end
end
-- TODO sort components in inferred components order, for faster computing
--
print("Discovered:")
--[[
print(dump(cleanedComponents))
print(dump(inputs))
print(dump(outputs))
@ -237,7 +243,63 @@ function logikraft.compileCircuit(poz)
end
function logikraft.compute(circuit,inz)
local out = {outX = inz["in1"]}
return out
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

View File

@ -6,6 +6,7 @@ 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")

BIN
textures/component_nand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_nor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_not.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_off.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_on.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
textures/component_or.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
textures/component_xor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB