OpenComputerScripts/programs/drone/drone_bios.lua

142 lines
3.3 KiB
Lua
Raw Normal View History

2018-12-04 14:32:18 +08:00
drone_version="Drone v3.3d"
2018-11-17 16:12:06 +08:00
drone=component.proxy(component.list("drone")())
modem=component.proxy(component.list("modem")())
2018-11-24 00:45:58 +08:00
drone.setStatusText(drone_version .. '\n' .. modem.address)
2018-11-17 16:12:06 +08:00
modem.open(98)
2018-12-03 21:46:54 +08:00
H={["_anything"]={}}
T={}
W={}
2018-12-04 14:32:18 +08:00
local coC,coT,coY,coR,upt,tbI,tbR=coroutine.create,coroutine.running,coroutine.yield,coroutine.resume,computer.uptime,table.insert,table.remove
handle_event=function(e)
2018-12-03 21:46:54 +08:00
if(H[e[1]]) then
for i,tb in pairs(H[e[1]]) do
coR(tb.co,i,e)
end
end
2018-12-03 21:46:54 +08:00
for i,tb in pairs(H["_anything"]) do
coR(tb.co,i,e)
end
end
sleep=function(sec)
2018-12-03 21:46:54 +08:00
tbI(W,{id=0,tm=upt()+sec,co=coT()})
tbR(W,coY())
end
cancel=function(id,wid)
if(wid) then
2018-12-03 21:46:54 +08:00
tbR(W,wid)
else
2018-12-03 21:46:54 +08:00
for i,t in ipairs(W) do
if(t.id==id) then
2018-12-03 21:46:54 +08:00
tbR(W,i)
break
end
end
end
2018-12-03 21:46:54 +08:00
if(T[id]) then
T[id]=nil
return true
else
return false
end
end
timer=function(sec,fn,times)
2018-12-03 21:46:54 +08:00
tbI(T,{cb=fn,intv=sec,times=times})
local id=#T
tbI(W,{tm=upt()+sec,id=id,co=coC(function(wid)
while true do
pcall(fn)
2018-12-03 21:46:54 +08:00
local tb=T[id]
if(tb.times<0 or tb.times>1) then
2018-12-03 21:46:54 +08:00
tb.times,tb.tm=tb.times-1,upt()+tb.intv
wid=coY()
else
cancel(id,wid)
return
end
end
end)})
return id
end
ignore=function(name,id)
2018-12-03 21:46:54 +08:00
if(H[name][id]) then
H[name][id]=nil
return true
else
return false
end
end
listen=function(name,cb)
2018-12-03 21:46:54 +08:00
if(H[name]==nil) then
H[name]={}
end
2018-12-03 21:46:54 +08:00
tbI(H[name],{co=coC(function(i,e)
while true do
local ok,res=pcall(cb,e)
if(ok and res~=nil and not res) then
ignore(name,i)
return
end
2018-12-03 21:46:54 +08:00
i,e=coY()
end
end)})
2018-12-03 21:46:54 +08:00
return #H[name]
end
wait=function(sec,name)
if(name==nil and type(sec)=="string") then name=sec sec=nil end
2018-12-03 21:46:54 +08:00
sec=sec and upt()+sec or math.huge
local this=coT()
tbI(W,{id=0,tm=sec,co=this})
name=name or "_anything"
local id=listen(name,function(e)
2018-12-03 21:46:54 +08:00
coR(this,-1,e)
return false
end)
2018-12-03 21:46:54 +08:00
local wid,e=coY()
ignore(name,id)
if(wid==-1) then
2018-12-03 21:46:54 +08:00
for i,t in ipairs(W) do
if(t.co==this) then
2018-12-03 21:46:54 +08:00
tbR(W,i)
break
end
end
return e
else
2018-12-03 21:46:54 +08:00
tbR(W,wid)
return nil
end
end
listen("modem_message",function(e)
2018-12-03 21:46:54 +08:00
if(e[6] and e[6]=='execute_command' and e[7]) then
local f,err=load(e[7])
if(f) then
2018-12-03 21:46:54 +08:00
local ok,err=pcall(function()
modem.send(e[3],99,f())
end)
if(not ok) then
modem.send(e[3],99,"CallError: " .. err)
2018-11-17 16:12:06 +08:00
end
else
2018-12-03 21:46:54 +08:00
modem.send(e[3],99,"SyntaxError: " .. err)
2018-11-17 16:12:06 +08:00
end
end
end)
while true do
local max_wait,max_id=math.huge,0
2018-12-03 21:46:54 +08:00
for i,v in ipairs(W) do
if(v.tm<max_wait) then
max_wait,max_id=v.tm,i
end
2018-11-17 16:12:06 +08:00
end
local e
if(max_wait==math.huge) then
e=table.pack(computer.pullSignal())
else
2018-12-03 21:46:54 +08:00
e=table.pack(computer.pullSignal(max_wait-upt()))
end
if(e[1]==nil) then
2018-12-03 21:46:54 +08:00
coR(W[max_id].co,max_id)
else
handle_event(e)
2018-11-17 16:12:06 +08:00
end
end