Compare commits

...

2 Commits

Author SHA1 Message Date
b907e06c5f
Editor now can read circuits 2024-05-20 03:39:54 +02:00
0298b85323
Upgraded circuit generation 2024-05-20 01:02:42 +02:00
8 changed files with 195 additions and 26 deletions

View File

@ -37,6 +37,20 @@ logikraft.components = {
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"}
}
}
}

View File

@ -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}
})

View File

@ -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
@ -89,7 +93,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 +110,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,7 +122,7 @@ 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 = {}}
local cp = {type = cpblock.name, ports = {}, pos = pos}
-- Left
if component.ports.left
then
@ -162,14 +167,77 @@ function logikraft.compileCircuit(poz)
end
end
end
-- 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
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
print("Discovered:")
print(dump(components))
--[[
print(dump(cleanedComponents))
print(dump(inputs))
print(dump(outputs))
print(dump(connections))
--]]
return {components = cleanedComponents, inputs=inputs, outputs=outputs, connections = connections}
end
function logikraft.compute(circuit,inz)
local out = {outX = inz["in1"]}
return out
end

View File

@ -37,4 +37,85 @@ 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
print("ploup")
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)

View File

@ -140,15 +140,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

View File

Before

Width:  |  Height:  |  Size: 606 B

After

Width:  |  Height:  |  Size: 606 B

View File

Before

Width:  |  Height:  |  Size: 636 B

After

Width:  |  Height:  |  Size: 636 B

View File

@ -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