1
0
Fork 0
cc-stuff/augment/main.lua

124 lines
3.0 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.surroundings = {
entities = {},
entitiesByUUID = {}
}
table.contains = function(tbl, value)
for k, v in pairs(tbl) do
if v == value then return true, k end
end
return false, nil
end
table.keys = function(tbl)
local gen = pairs(tbl)
local k = nil
return function()
k = gen(tbl, k)
return k
end
end
table.values = function(tbl)
local gen = pairs(tbl)
local k, v
return function()
k, v = gen(tbl, k)
return v
end
end
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
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
table.insert(modules, run_wrapped(callback, name))
end
term.setTextColor(colors.white)
end
print("Loaded " .. #modules .. " modules")
local function safeget(func, name, fallback, ...)
if func then
local s, res = pcall(func, ...)
if not s then
printError(name .. " failed: " .. res)
else
return res
end
end
return fallback
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
printError("Caught exit event, shutting down...")
break
end
end
end,
function() -- Neural Interface coroutine
print("NI routine started")
while _G._running do
_G.player = safeget(NI.getMetaOwner, "getMetaOwner()", _G.player)
surroundings.entities = safeget(NI.sense, "sense()", surroundings.entities)
local knownUUIDs = {}
for entity in table.values(surroundings.entities) do
local s, res = pcall(NI.getMetaByID, entity.id)
surroundings.entitiesByUUID[entity.id] = s and res or entity
table.insert(knownUUIDs, entity.id)
end
for uuid in table.keys(surroundings.entitiesByUUID) do
if not table.contains(knownUUIDs, uuid) then
surroundings.entitiesByUUID[uuid] = nil
end
end
_G.canvas3d.recenter()
os.sleep(0.05)
end
_G.canvas3d_src.clear()
_G.canvas2d.clear()
end, table.unpack(modules))
print("Goodbye!")