2023-10-12 18:14:13 +03:00
|
|
|
_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 = {}
|
2023-10-12 18:14:13 +03:00
|
|
|
|
|
|
|
local function run_wrapped(func, filename)
|
|
|
|
return function()
|
2023-10-12 20:42:25 +03:00
|
|
|
print("Starting module "..filename)
|
2023-10-12 18:14:13 +03:00
|
|
|
local ok, res = pcall(func)
|
|
|
|
if ok then
|
2023-10-12 20:42:25 +03:00
|
|
|
print("Module "..filename.." exited: " .. tostring(res))
|
2023-10-12 18:14:13 +03:00
|
|
|
else
|
2023-10-12 21:26:43 +03:00
|
|
|
printError("Module "..filename.." crashed: " .. res)
|
2023-10-12 18:14:13 +03:00
|
|
|
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))
|
2023-10-12 18:14:13 +03:00
|
|
|
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
|
2023-10-12 18:14:13 +03:00
|
|
|
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")
|
2023-10-12 18:14:13 +03:00
|
|
|
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
|
2023-10-12 18:14:13 +03:00
|
|
|
_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))
|
2023-10-12 18:14:13 +03:00
|
|
|
print("Goodbye!")
|