spdkils

Control

Oct 27th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local bChan = 555
  2. local rChan = 999
  3. local modem = peripheral.wrap("right")
  4. local wrap = "right"
  5. if modem == nil then
  6.   modem = peripheral.wrap("left")
  7.   wrap = "left"
  8. end
  9. if modem == nil then
  10.   modem = peripheral.wrap("back")
  11.   wrap = "back"
  12. end
  13. modem.open(rChan)
  14. modem.open(bChan)
  15. local id=os.getComputerID()
  16. modem.open(id)
  17. local tBuffer = {}
  18. local index = 0
  19. local tArgs = { ... }
  20. local run = 'false'
  21.  
  22. local function pairsByKeys (t, f)
  23.       local a = {}
  24.       for n in pairs(t) do table.insert(a, n) end
  25.       table.sort(a, f)
  26.       local i = 0      -- iterator variable
  27.       local iter = function ()   -- iterator function
  28.         i = i + 1
  29.         if a[i] == nil then return nil
  30.         else return a[i], t[a[i]]
  31.         end
  32.       end
  33.       return iter
  34. end
  35.  
  36. local function fnPullCMD(aTable,nIndex)
  37.  if aTable[nIndex] then
  38.   return aTable[nIndex]
  39.  else
  40.   return ''
  41.  end
  42. end
  43.  
  44. local function myClear(sData)
  45.     term.clear()
  46.     term.setCursorPos(1,1)
  47.     if sData and #sData > 0 then
  48.     term.write(sData)
  49.     end
  50.     local nx,ny = term.getSize()
  51.     term.setCursorPos(nx-7,1)
  52.     term.write('CPU: ' .. id)
  53.     term.setCursorPos(1,2)
  54.     term.setCursorBlink(true)
  55. end
  56.  
  57. myClear(' ')
  58. local sString = ''
  59. local tFuel = {}
  60.  
  61. while true do
  62.   local tAble = {os.pullEvent()}
  63.   if #tAble > 0 then
  64.     if tAble[1] == "modem_message" and tAble[3] ~= rChan then
  65.         local tProg = textutils.unserialize(tAble[5])
  66.       if tProg and #tProg > 0 then
  67.                     if tProg[1] == "fuel" and not pocket then
  68.                      local data = turtle.getFuelLevel()
  69.                       os.queueEvent('modem_message',wrap,rChan,id,data)
  70.                       modem.transmit(rChan,id,data)
  71.                     else
  72.                       local x,y = term.getCursorPos()
  73.                       term.setCursorPos(1,13)
  74.                                       term.clearLine()
  75.                                       term.setCursorPos(1,3)
  76.                       if #tArgs == 0 then
  77.                          run = tostring(shell.run(unpack(tProg)))
  78.                       end
  79.                       modem.transmit(rChan,id,run)
  80.                       os.queueEvent('modem_message',wrap,rChan,id,run)
  81.                     end
  82.       end
  83.           end
  84.  
  85. if tAble[1] == "key" then
  86. if sString == '' then
  87.  term.setCursorPos(1,2)
  88. end
  89.   local key = keys.getName(tAble[2])
  90.   if key == "enter" then
  91.    --myClear(sString)
  92.    if sString and #sString > 0 then
  93.     table.insert(tBuffer,1,sString)
  94.    end
  95.    term.setCursorPos(1,2)
  96.    tString = {}
  97.    tFuel = {}
  98.    if sString == 'exit' then
  99.      return
  100.    end
  101.    for w in string.gmatch(sString,'%w+')do
  102.      table.insert(tString,w)
  103.    end
  104.    sString = ''
  105.    modem.transmit(bChan ,id,textutils.serialize(tString))
  106.    os.queueEvent('modem_message',wrap,bChan ,id,textutils.serialize(tString))
  107.    elseif key == "backspace" then
  108.      sString = string.sub(sString,1,string.len(sString)-1)
  109.      local x,y = term.getCursorPos()
  110.     if x > 1 then
  111.      term.setCursorPos(x-1,y)
  112.      term.write(" ")
  113.      term.setCursorPos(x-1,y)
  114.     end
  115.    elseif tAble[2] == 200 and not pocket then
  116.      turtle.forward()
  117.    elseif tAble[2] == 208 and not pocket  then
  118.      turtle.back()
  119.    elseif tAble[2] == 203 and not pocket  then
  120.      turtle.turnLeft()
  121.    elseif tAble[2] == 205 and not pocket  then
  122.      turtle.turnRight()
  123.    elseif tAble[2] == 199 and not pocket  then
  124.      turtle.up()
  125.    elseif tAble[2] ==207 and not pocket  then
  126.      turtle.down()
  127.    elseif tAble[2] == 210 then
  128.      if index < #tBuffer then
  129.      index = index + 1
  130.     else
  131.     index = 0
  132.     end
  133.      sString = fnPullCMD(tBuffer,index)
  134.      term.setCursorPos(1,2)
  135.      term.clearLine()
  136.      if sString and #sString > 0 then
  137.      term.write(sString)
  138.      end
  139.    end
  140.    elseif tAble[1] == "char" then
  141.      sString = sString .. tAble[2]
  142.      term.write(tAble[2])
  143.    elseif tAble[3] == rChan then
  144.      tFuel[tAble[4]] = tAble[5]
  145.    myClear(tBuffer[1])
  146.     term.setCursorPos(1,3)
  147.     for k,v in pairsByKeys(tFuel) do
  148.      term.write(k .. ' ' .. v)
  149.      local x,y = term.getCursorPos()
  150.      if x <14  then
  151.       term.setCursorPos(15,y)
  152.      else
  153.       term.setCursorPos(1,y+1)
  154.      end
  155.     end
  156.    end
  157.   end
  158.  end
Advertisement
Add Comment
Please, Sign In to add comment