DaGamer12345

Turtle Remote

Jul 21st, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. if turtle then
  2.  function CmdLoop()
  3.   local event, modemSide, senderChannel,
  4.      replyChannel, message, senderDistance = os.pullEvent("modem_message")
  5.   if message == "exit" then
  6.       peripheral.call("right", "transmit", sChan, rChan, "Disconnecting.")
  7.       peripheral.call("right", "closeAll")
  8.       os.reboot()
  9.   end
  10.   if message == "send file" then
  11.    local event, modemSide, senderChannel,
  12.    replyChannel, message, senderDistance = os.pullEvent("modem_message")
  13.    local file = fs.open("file_received", "w")
  14.    file.write(message)
  15.    file.close()
  16.    CmdLoop()
  17.   end
  18.   if message ~= "exit" or "send file" then
  19.       shell.run(message)
  20.       os.sleep(3)
  21.       peripheral.call("right","transmit", sChan, rChan, "Command complete")
  22.       CmdLoop()
  23.  end
  24. end
  25.  print("Turtle Remote - Turtle Version")
  26.  print("What channel to broadcast returns on?")
  27.  sChan = io.read()
  28.  sChan = tonumber(sChan)
  29.  print("What channel should commands be received on?")
  30.  rChan = io.read()
  31.  rChan = tonumber(rChan)
  32.  print("Receiving commands...")
  33.  peripheral.call("right", "open", rChan)
  34.  CmdLoop()
  35. end
  36. if not turtle then
  37.  function sendFile()
  38.   print("What file to send?")
  39.   sFile = io.read()
  40.   --Confirm file exists
  41.   local sPath = shell.resolve(sFile)
  42.   if not fs.exists(sPath) or fs.isDir(sPath) then
  43.    print("No such file")
  44.    return
  45.   end
  46.  
  47.   --Read the entire file
  48.   sName = fs.getName(sPath)
  49.   file = fs.open(sPath, "r")
  50.   message = file.readAll()
  51.   file.close()
  52.  
  53.   --Send file through Modem
  54.   peripheral.call(mSide, "transmit", sChan, rChan, message)
  55.   print("Sent file. File is saved as "..sName.." in the root directory.")
  56.   os.sleep(1)
  57.   peripheral.call(mSide, "transmit", sChan, rChan, "rename file_received "..sName.."")
  58.   sLoop()
  59.  end
  60.  function sLoop()
  61.      print("Type command: (Note: Send files with 'send file')")
  62.      cmd = io.read()
  63.      if cmd == "remote" then
  64.       print("Remote session already open")
  65.      end
  66.      peripheral.call(mSide, "transmit", sChan, rChan, cmd)
  67.      if cmd == "exit" then
  68.       peripheral.call(mSide, "closeAll")
  69.       os.reboot()
  70.       end
  71.      if cmd == "send file" then
  72.       sendFile()
  73.      end
  74.      if cmd ~= "send file" or "exit" then
  75.       print("Sending command...")
  76.       rLoop()
  77.      end
  78.  end
  79.  function rLoop()
  80.      print("Receiving return...")
  81.      local event, modemSide, senderChannel,
  82.         replyChannel, message, senderDistance = os.pullEvent("modem_message")
  83.      print("Turtle on channel "..senderChannel.." says: "..message)
  84.      print("The turtle is: "..senderDistance.." blocks away from me.")
  85.      if message == "Disconnecting." then
  86.          peripheral.call(mSide, "closeAll")
  87.          os.reboot()
  88.      end
  89.      if cmd ~= "Disconnecting." then
  90.          sLoop()
  91.      end
  92.  end
  93.  print("Turtle Remote - Computer Version")
  94.  print("What side is the modem on?")
  95.  mSide = io.read()
  96.  print("What channel to transmit commands?")
  97.  sChan = io.read()
  98.  sChan = tonumber(sChan)
  99.  print("What channel should returns be received on?")
  100.  rChan = io.read()
  101.  rChan = tonumber(rChan)
  102.  peripheral.call(mSide, "open", rChan)
  103.  sLoop()
  104. end
Advertisement
Add Comment
Please, Sign In to add comment