Added Plethora Neural interface thingie

This commit is contained in:
Casey 2023-10-12 18:14:13 +03:00
parent e61fa4023b
commit a1f3b22598
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
5 changed files with 171 additions and 0 deletions

21
augment/files.json Normal file
View File

@ -0,0 +1,21 @@
{
"repository": "TODO",
"files": [
{
"path": "startup",
"src": "startup.lua"
},
{
"path": "main.lua",
"src": "main.lua"
},
{
"path": "modules/event.lua",
"src": "modules/event.lua"
},
{
"path": "modules/wh.lua",
"src": "modules/wh.lua"
}
]
}

88
augment/main.lua Normal file
View File

@ -0,0 +1,88 @@
-- 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!")

16
augment/modules/event.lua Normal file
View File

@ -0,0 +1,16 @@
-- x-run: cd .. && scp -r ./* pneumatic-pump:htdocs/cc/augment/
return function()
while _G._running do
local ev = { os.pullEvent() }
if ev[1] == "key" and ev[2] == keys.q then
os.queueEvent("exit")
break
elseif ev[1] == "timer" or ev[1] == "plethora_task" then
-- do nothing, just ignore
else
print(table.unpack(ev))
end
os.sleep(0.05)
end
end

25
augment/modules/wh.lua Normal file
View File

@ -0,0 +1,25 @@
-- x-run: cd .. && scp -r ./* pneumatic-pump:htdocs/cc/augment/
return function()
local cubes = {}
while _G._running do
local el = entities
print("found "..#entities.."entities")
for i, cube in ipairs(cubes) do
if el[i] == nil then
cube.setSize(0.0, 0.0, 0.0)
print("entity box "..tostring(i).." unused")
end
end
for i, entity in ipairs(entities) do
if cubes[i] == nil then
cubes[i] = canvas3d.addBox(0, 0, 0)
cubes[i].setDepthTested(false)
print("Created cube for entity "..entity.name)
end
cubes[i].setPosition(entity.x, entity.y, entity.z)
cubes[i].setSize(0.25, 0.25, 0.25)
end
os.sleep(0.05)
end
end

21
augment/startup.lua Normal file
View File

@ -0,0 +1,21 @@
-- x-run: scp ./* pneumatic-pump:htdocs/cc/augment/
local repository = "TODO/files.json"
local files = textutils.unserializeJSON(http.get(repository).readAll())
local function getFile(url, path)
local r, err = http.get(url)
io.write("GET " .. path .. " ... ")
if not r then
print("FAIL: " .. err)
return false, err
end
io.open(path, "w"):write(r.readAll()):close()
io.write("OK\n")
end
for i, file in ipairs(files.files) do
getFile(files.repository .. "/" .. file.src, file.path)
end
shell.run("main.lua")