cc-stuff/augment/main.lua

81 lines
2.1 KiB
Lua

_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.nearbyEntities = {}
local function run_wrapped(func, filename)
return function()
print("Starting module "..filename)
local ok, res = pcall(func)
if ok then
print("Module "..filename.." exited: " .. tostring(res))
else
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
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...")
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,
function() -- Neural Interface coroutine
print("NI routine started")
while _G._running do
_G.player = safeset(NI.getMetaOwner, "getMetaOwner()", _G.player)
_G.nearbyEntities = safeset(NI.sense, "sense()", _G.entities)
_G.canvas3d.recenter()
os.sleep(0.05)
end
_G.canvas3d_src.clear()
end, table.unpack(modules))
print("Goodbye!")