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
|
2018-11-24 20:36:02 +08:00
|
|
|
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)
|
2018-11-30 03:05:33 +08:00
|
|
|
end
|
|
|
|
end
|
2018-12-03 21:46:54 +08:00
|
|
|
for i,tb in pairs(H["_anything"]) do
|
|
|
|
coR(tb.co,i,e)
|
2018-11-30 03:05:33 +08:00
|
|
|
end
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
|
|
|
sleep=function(sec)
|
2018-12-03 21:46:54 +08:00
|
|
|
tbI(W,{id=0,tm=upt()+sec,co=coT()})
|
|
|
|
tbR(W,coY())
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
2018-11-30 03:05:33 +08:00
|
|
|
cancel=function(id,wid)
|
|
|
|
if(wid) then
|
2018-12-03 21:46:54 +08:00
|
|
|
tbR(W,wid)
|
2018-11-30 03:05:33 +08:00
|
|
|
else
|
2018-12-03 21:46:54 +08:00
|
|
|
for i,t in ipairs(W) do
|
2018-11-30 03:05:33 +08:00
|
|
|
if(t.id==id) then
|
2018-12-03 21:46:54 +08:00
|
|
|
tbR(W,i)
|
2018-11-30 03:05:33 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-03 21:46:54 +08:00
|
|
|
if(T[id]) then
|
|
|
|
T[id]=nil
|
2018-11-30 03:05:33 +08:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2018-11-24 20:36:02 +08:00
|
|
|
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)
|
2018-11-24 20:36:02 +08:00
|
|
|
while true do
|
2018-11-30 03:05:33 +08:00
|
|
|
pcall(fn)
|
2018-12-03 21:46:54 +08:00
|
|
|
local tb=T[id]
|
2018-11-30 03:05:33 +08:00
|
|
|
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()
|
2018-11-30 03:05:33 +08:00
|
|
|
else
|
|
|
|
cancel(id,wid)
|
|
|
|
return
|
2018-11-24 20:36:02 +08:00
|
|
|
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
|
2018-11-30 03:05:33 +08:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
|
|
|
listen=function(name,cb)
|
2018-12-03 21:46:54 +08:00
|
|
|
if(H[name]==nil) then
|
|
|
|
H[name]={}
|
2018-11-30 03:05:33 +08:00
|
|
|
end
|
2018-12-03 21:46:54 +08:00
|
|
|
tbI(H[name],{co=coC(function(i,e)
|
2018-11-24 20:36:02 +08:00
|
|
|
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()
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
|
|
|
end)})
|
2018-12-03 21:46:54 +08:00
|
|
|
return #H[name]
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
|
|
|
wait=function(sec,name)
|
2018-11-30 03:05:33 +08:00
|
|
|
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})
|
2018-11-30 03:05:33 +08:00
|
|
|
name=name or "_anything"
|
|
|
|
local id=listen(name,function(e)
|
2018-12-03 21:46:54 +08:00
|
|
|
coR(this,-1,e)
|
2018-11-30 03:05:33 +08:00
|
|
|
return false
|
|
|
|
end)
|
2018-12-03 21:46:54 +08:00
|
|
|
local wid,e=coY()
|
2018-11-30 03:05:33 +08:00
|
|
|
ignore(name,id)
|
|
|
|
if(wid==-1) then
|
2018-12-03 21:46:54 +08:00
|
|
|
for i,t in ipairs(W) do
|
2018-11-30 03:05:33 +08:00
|
|
|
if(t.co==this) then
|
2018-12-03 21:46:54 +08:00
|
|
|
tbR(W,i)
|
2018-11-30 03:05:33 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2018-11-24 20:36:02 +08:00
|
|
|
return e
|
|
|
|
else
|
2018-12-03 21:46:54 +08:00
|
|
|
tbR(W,wid)
|
2018-11-24 20:36:02 +08:00
|
|
|
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])
|
2018-11-30 03:05:33 +08:00
|
|
|
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
|
2018-11-30 03:05:33 +08:00
|
|
|
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)
|
2018-11-24 20:36:02 +08:00
|
|
|
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
|
2018-11-30 03:05:33 +08:00
|
|
|
if(v.tm<max_wait) then
|
|
|
|
max_wait,max_id=v.tm,i
|
|
|
|
end
|
2018-11-17 16:12:06 +08:00
|
|
|
end
|
2018-11-24 20:36:02 +08:00
|
|
|
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()))
|
2018-11-24 20:36:02 +08:00
|
|
|
end
|
|
|
|
if(e[1]==nil) then
|
2018-12-03 21:46:54 +08:00
|
|
|
coR(W[max_id].co,max_id)
|
2018-11-24 20:36:02 +08:00
|
|
|
else
|
|
|
|
handle_event(e)
|
2018-11-17 16:12:06 +08:00
|
|
|
end
|
|
|
|
end
|