KaoSDlanor

top level edit

Jan 15th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. local function max(tPar)
  2.     local count
  3.     for k,v in pairs(tPar) do
  4.         if (tonumber(k) or 0)>(tonumber(count) or 0) then count=k end
  5.     end
  6.     return count
  7. end
  8.  
  9. local messages=function()
  10.     local timer
  11.     local len
  12.     local function clear()
  13.         if len then
  14.             local size={term.getSize()}
  15.             local pos={term.getCursorPos()}
  16.             term.setCursorPos(size[1]-len,1)
  17.             write(string.rep(" ",len))
  18.             len=nil
  19.             timer=nil
  20.             term.setCursorPos(unpack(pos))
  21.         end
  22.     end
  23.     while true do
  24.         local evts={os.pullEventRaw()}
  25.         if evts[1]=="message" and type(evts[2]=="string") then
  26.             clear()
  27.             local size={term.getSize()}
  28.             local pos={term.getCursorPos()}
  29.             term.setCursorPos(size[1]-#evts[2],1)
  30.             write(evts[2])
  31.             term.setCursorPos(unpack(pos))
  32.             timer=os.startTimer(2)
  33.             len=#evts[2]
  34.         elseif evts[1]=="timer" and timer==evts[2] then
  35.             clear()
  36.         end
  37.     end
  38. end
  39.  
  40. local tO={}
  41. local tCo={coroutine.create(function() os.run({},"rom/programs/shell") end),coroutine.create(messages)}
  42. local pE=printError
  43. local current=0
  44. local pE2=function(...)
  45.     term.clear()
  46.     term.setCursorPos(1,1)
  47.     for k,v in pairs(tO) do
  48.         rawset(k,"printError",pE)
  49.     end
  50.  
  51.     for k,v in pairs(fs.list("rom/apis")) do
  52.         if type(_G[v])=="table" and v~="term" then
  53.             os.unloadAPI(v)
  54.             os.loadAPI("rom/apis/"..v)
  55.         end
  56.     end
  57.     tCo[3]=coroutine.create(rednet.run)
  58.  
  59.     local tEvts={}
  60.     while true do
  61.         local tDone={}
  62.         for k,v in pairs(tCo) do
  63.             current=k
  64.             if coroutine.status(v)=="dead" then
  65.                 table.insert(tDone,k)
  66.                 os.queueEvent("message","coroutine "..k.." has died")
  67.             else
  68.                 pcall(coroutine.resume(v,unpack(tEvts)))
  69.             end
  70.         end
  71.         for k,v in pairs(tDone) do
  72.             tCo[v]=nil
  73.         end
  74.         if #tCo==0 then
  75.             break
  76.         end
  77.         os.startTimer(1)
  78.         tEvts={os.pullEventRaw()}
  79.     end
  80.     print("all coroutines have been terminated, ending now")
  81.     sleep(2)
  82. end
  83.  
  84. for i=1,5 do
  85.     if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
  86.         tO[getfenv(i)]=true
  87.         rawset(getfenv(i),"printError",pE2)
  88.     end
  89. end
  90.  
  91. rawset(_G,"addCo",function(func,ref)
  92.     local t=type(func)
  93.     if not t=="function" then
  94.         print("incorrect param #1 function expected, got "..t)
  95.         return nil, ("incorrect param #1 function expected, got "..t)
  96.     end
  97.     tCo[ref or max(tCo)+1]=coroutine.create(func)
  98.     return ref or max(tCo)
  99. end)
  100.  
  101. rawset(_G,"remCo",function(num,b)
  102.     num=num or 0
  103.     if not tCo[num] then
  104.         print("There is no coroutine under that reference")
  105.         return nil
  106.     elseif num==current then
  107.         print("cannot remove the current coroutine")
  108.         return nil
  109.     end
  110.     tCo[num]=nil
  111.     if b then
  112.         os.queueEvent("message","coroutine "..num.." has died")
  113.     end
  114.     return true
  115. end)
  116.  
  117. os.queueEvent("terminate")
Advertisement
Add Comment
Please, Sign In to add comment