Skip to content

Commit

Permalink
luacontroller programming support
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 23, 2023
1 parent 3a4acec commit 1b30c8b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ read_globals = {
-- deps
"monitoring",
"mtt",
"mesecon"
"mesecon",
"mooncontroller"
}
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if minetest.get_modpath("mesecons_switch") and minetest.get_modpath("mesecons_li
dofile(MP.."/mesecons/common.lua")
dofile(MP.."/mesecons/lightstones.lua")
dofile(MP.."/mesecons/switch.lua")
dofile(MP.."/mesecons/luacontroller.lua")
dofile(MP.."/mesecons/register_tool.lua")

if minetest.get_modpath("digilines") then
Expand Down
93 changes: 93 additions & 0 deletions mesecons/luacontroller.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

local function register_all(prefix)
for a = 0, 1 do -- 0 = off 1 = on
for b = 0, 1 do
for c = 0, 1 do
for d = 0, 1 do
local cid = tostring(d)..tostring(c)..tostring(b)..tostring(a)
local node_name = prefix..cid
mtui.mesecons.allowed_nodes[node_name] = true
end
end
end
end
end

if minetest.get_modpath("mesecons_luacontroller") then
register_all("mesecons_luacontroller:luacontroller")
end

if minetest.get_modpath("mooncontroller") then
register_all(mooncontroller.BASENAME)
end

-- ui -> game
mtui.register_on_command("luacontroller_set_program", function(data)
minetest.load_area(data.pos)
local node = minetest.get_node_or_nil(data.pos)
if node == nil then
-- not loaded
return { success = false }
end

if not mtui.mesecons.allowed_nodes[node.name] then
-- unexpected node
return { success = false }
end

local def = minetest.registered_nodes[node.name]
if not def or type(def.on_receive_fields) ~= "function" then
-- node definition doesn't make sense
return { success = false }
end

-- shim player (the formspec callback only uses the `get_player_name` field)
-- TODO: replace with proper fakeplayer someday
local player = {
get_player_name = function()
return data.playername
end
}

local fields = {
code = data.code,
program = true
}

def.on_receive_fields(data.pos, nil, fields, player)

-- this only works with the mooncontroller
local meta = minetest.get_meta(data.pos)
local errmsg = meta:get_string("errmsg")

return {
success = errmsg == "",
errmsg = errmsg
}
end)

mtui.register_on_command("luacontroller_get_program", function(data)
minetest.load_area(data.pos)
local node = minetest.get_node_or_nil(data.pos)
if node == nil then
-- not loaded
return { success = false }
end

if not mtui.mesecons.allowed_nodes[node.name] then
-- unexpected node
return { success = false }
end

if minetest.is_protected(data.pos, data.playername) then
-- protected
return { success = false }
end

local meta = minetest.get_meta(data.pos)
return {
success = true,
code = meta:get_string("code"),
errmsg = meta:get_string("errmsg")
}
end)
4 changes: 3 additions & 1 deletion mod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ technic_chests,
beerchat,
mesecons_switch,
mesecons_lightstone,
digilines
mesecons_luacontroller,
digilines,
mooncontroller
"""

0 comments on commit 1b30c8b

Please sign in to comment.