cc-stuff/augment/main.lua

82 lines
2.1 KiB
Lua
Raw Normal View History

-- 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()
2023-10-12 20:42:25 +03:00
print("Starting module "..filename)
local ok, res = pcall(func)
if ok then
2023-10-12 20:42:25 +03:00
print("Module "..filename.." exited: " .. tostring(res))
else
2023-10-12 20:42:25 +03:00
printErr("Module "..filename.." crashed: " .. res)
end
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
2023-10-12 19:15:26 +03:00
table.insert(modules, run_wrapped(callback, name))
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...")
2023-10-12 20:42:25 +03:00
parallel.waitForAll(function()
print("Exit handler started")
while _G._running 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,
2023-10-12 20:42:25 +03:00
function() -- Neural Interface coroutine
print("NI routine started")
while _G._running do
_G.player = safeset(NI.getMetaOwner, "getMetaOwner()", _G.player)
_G.entities = safeset(NI.sense, "sense()", _G.entities)
_G.canvas3d.recenter()
os.sleep(0.05)
end
_G.canvas3d_src.clear()
2023-10-12 20:42:25 +03:00
end, table.unpack(modules))
print("Goodbye!")