121 lines
4.2 KiB
Lua
121 lines
4.2 KiB
Lua
local _contexts = {}
|
|
|
|
|
|
function logikraft.showNameFormspec(playername,pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
local nodedesc = minetest.registered_nodes[node.name].description
|
|
|
|
local text = "Please enter the name of the " .. nodedesc .. " at ("..tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z)..")"
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
local oldname = meta:get_string("logikraft:name") or ""
|
|
|
|
|
|
local formspec = {
|
|
"formspec_version[4]",
|
|
"size[6,3.476]",
|
|
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
|
|
"field[0.375,1.25;5.25,0.8;name;".. minetest.formspec_escape(nodedesc) .. " name;"..oldname.."]",
|
|
"button_exit[1.5,2.3;3,0.8;commit;OK]"
|
|
}
|
|
_contexts[playername] = pos
|
|
minetest.show_formspec(playername, "logikraft:nameFormSpec", table.concat(formspec, ""))
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "logikraft:nameFormSpec" then return end
|
|
|
|
if fields.commit then
|
|
local pname = player:get_player_name()
|
|
if _contexts[pname]
|
|
then
|
|
local meta = minetest.get_meta(_contexts[pname])
|
|
meta:set_string("logikraft:name",fields.name)
|
|
minetest.chat_send_player(pname, "Set to "..fields.name)
|
|
else
|
|
minetest.chat_send_player(pname, "Could not get pos data from formspec")
|
|
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)
|