cc-stuff/augment/main.lua

87 lines
2.3 KiB
Lua
Raw Normal View History

_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
2023-10-12 21:25:20 +03:00
_G.nearbyEntities = {}
2023-10-12 21:36:32 +03:00
_G.nearbyEntitiesByUUID = {}
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 21:26:43 +03:00
printError("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)
2023-10-12 21:36:32 +03:00
_G.nearbyEntities = safeset(NI.sense, "sense()", _G.nearbyEntities or {})
_G.nearbyEntitiesByUUID = {}
for i = 1, #_G.nearbyEntities do
_G.nearbyEntitiesByUUID[_G.nearbyEntities[i].id] = _G.nearbyEntities[i]
end
_G.canvas3d.recenter()
os.sleep(0.05)
end
_G.canvas3d_src.clear()
2023-10-12 21:58:51 +03:00
_G.canvas2d.clear()
2023-10-12 20:42:25 +03:00
end, table.unpack(modules))
print("Goodbye!")