From a1f3b22598b1b8beda0acdc542ec8ca8e058c422 Mon Sep 17 00:00:00 2001 From: hkc Date: Thu, 12 Oct 2023 18:14:13 +0300 Subject: [PATCH] Added Plethora Neural interface thingie --- augment/files.json | 21 ++++++++++ augment/main.lua | 88 +++++++++++++++++++++++++++++++++++++++ augment/modules/event.lua | 16 +++++++ augment/modules/wh.lua | 25 +++++++++++ augment/startup.lua | 21 ++++++++++ 5 files changed, 171 insertions(+) create mode 100644 augment/files.json create mode 100644 augment/main.lua create mode 100644 augment/modules/event.lua create mode 100644 augment/modules/wh.lua create mode 100644 augment/startup.lua diff --git a/augment/files.json b/augment/files.json new file mode 100644 index 0000000..ec05838 --- /dev/null +++ b/augment/files.json @@ -0,0 +1,21 @@ +{ + "repository": "TODO", + "files": [ + { + "path": "startup", + "src": "startup.lua" + }, + { + "path": "main.lua", + "src": "main.lua" + }, + { + "path": "modules/event.lua", + "src": "modules/event.lua" + }, + { + "path": "modules/wh.lua", + "src": "modules/wh.lua" + } + ] +} diff --git a/augment/main.lua b/augment/main.lua new file mode 100644 index 0000000..aa25bfd --- /dev/null +++ b/augment/main.lua @@ -0,0 +1,88 @@ +-- x-run: scp -r ./* pneumatic-pump:htdocs/cc/augment/ +_G.NI = peripheral.wrap("back") +_G._running = true + +_G.canvas2d = NI.canvas() +_G.canvas2d.clear() +_G.canvas3d_src = NI.canvas3d() +_G.canvas3d_src.clear() +_G.canvas3d = canvas3d_src.create() + +_G.player = nil +_G.entities = {} + +local function run_wrapped(func, filename) + return function() + local ok, res = pcall(func) + local oldc = term.getTextColor() + if ok then + term.setTextColor(colors.blue) + print("module "..filename.." exited") + else + term.setTextColor(colors.blue) + print("module "..filename.." crashed: " .. res) + end + + term.setTextColor(oldc) + end +end + +local modules = {} + +print("Loading modules...") +for i, name in ipairs(fs.list("modules")) do + io.write(name .. " -> ") + local success, callback = pcall(dofile, "modules/" .. name) + term.setTextColor(success and colors.green or colors.red) + io.write(tostring(callback)) + io.write("\n") + + if success then + table.insert(modules, callback) + end + term.setTextColor(colors.white) +end + +print("Loaded " .. #modules .. " modules") + +local function safeset(func, name, old) + if func then + local s, res = pcall(func) + if not s then + print("ERR: " .. name .. " failed: " .. res) + else + return res + end + end + return old +end + +print("Running...") +parallel.waitForAll(table.unpack(modules), function() + while true do + local ev = { os.pullEvent("exit") } + if ev[1] == "exit" then + _G._running = false + local oldc = term.getTextColor() + term.setTextColor(colors.red) + print("Caught exit event, shutting down...") + term.setTextColor(oldc) + break + end + end +end, +function() + while _G._running do + _G.player = safeset(NI.getMetaOwner, "getMetaOwner()", _G.player) + _G.entities = safeset(NI.sense, "sense()", _G.entities) + os.sleep(0.05) + end +end, +function() + while _G._running do + _G.canvas3d.recenter() + os.sleep(0.05) + end + _G.canvas3d_src.clear() +end) +print("Goodbye!") diff --git a/augment/modules/event.lua b/augment/modules/event.lua new file mode 100644 index 0000000..44e9ad0 --- /dev/null +++ b/augment/modules/event.lua @@ -0,0 +1,16 @@ +-- x-run: cd .. && scp -r ./* pneumatic-pump:htdocs/cc/augment/ + +return function() + while _G._running do + local ev = { os.pullEvent() } + if ev[1] == "key" and ev[2] == keys.q then + os.queueEvent("exit") + break + elseif ev[1] == "timer" or ev[1] == "plethora_task" then + -- do nothing, just ignore + else + print(table.unpack(ev)) + end + os.sleep(0.05) + end +end diff --git a/augment/modules/wh.lua b/augment/modules/wh.lua new file mode 100644 index 0000000..e5817cf --- /dev/null +++ b/augment/modules/wh.lua @@ -0,0 +1,25 @@ +-- x-run: cd .. && scp -r ./* pneumatic-pump:htdocs/cc/augment/ + +return function() + local cubes = {} + while _G._running do + local el = entities + print("found "..#entities.."entities") + for i, cube in ipairs(cubes) do + if el[i] == nil then + cube.setSize(0.0, 0.0, 0.0) + print("entity box "..tostring(i).." unused") + end + end + for i, entity in ipairs(entities) do + if cubes[i] == nil then + cubes[i] = canvas3d.addBox(0, 0, 0) + cubes[i].setDepthTested(false) + print("Created cube for entity "..entity.name) + end + cubes[i].setPosition(entity.x, entity.y, entity.z) + cubes[i].setSize(0.25, 0.25, 0.25) + end + os.sleep(0.05) + end +end diff --git a/augment/startup.lua b/augment/startup.lua new file mode 100644 index 0000000..1a5d805 --- /dev/null +++ b/augment/startup.lua @@ -0,0 +1,21 @@ +-- x-run: scp ./* pneumatic-pump:htdocs/cc/augment/ + +local repository = "TODO/files.json" +local files = textutils.unserializeJSON(http.get(repository).readAll()) + +local function getFile(url, path) + local r, err = http.get(url) + io.write("GET " .. path .. " ... ") + if not r then + print("FAIL: " .. err) + return false, err + end + io.open(path, "w"):write(r.readAll()):close() + io.write("OK\n") +end + +for i, file in ipairs(files.files) do + getFile(files.repository .. "/" .. file.src, file.path) +end + +shell.run("main.lua")