Editor now can read circuits

This commit is contained in:
MysaaJava 2024-05-20 03:39:54 +02:00
parent 0298b85323
commit b907e06c5f
Signed by: Mysaa
GPG Key ID: DBA23608F23F5A10
4 changed files with 135 additions and 3 deletions

View File

@ -202,8 +202,42 @@ function logikraft.compileCircuit(poz)
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 = components, connections = 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

@ -149,6 +149,9 @@ minetest.register_craftitem("logikraft:editor", {
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

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