cc-stuff/augment/main.lua

89 lines
2.1 KiB
Lua

-- 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!")