Advertisement
downwind

Computer transmit program for remote turtle in ComputerCraft

Oct 12th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. -- This is the computer transmitter turtle controler
  2. -- Modified movement code from PhyscoKillerMonkey post at http://www.computercraft.info/forums2/index.php?/topic/5665-wireless-goto-program-for-gps-turtles/page__fromsearch__1
  3. -- Modified menu code from JokerRH post at http://www.computercraft.info/forums2/index.php?/topic/11696-press-any-key-to-continue-scroll-enter/
  4. -- Modified by Richard Reigens 10/12/2013
  5.  
  6.  
  7. rednet.open("bottom") --Open rednet, make clearing the screen a single function
  8. function clear() term.clear() term.setCursorPos(1, 1) end
  9.  
  10. exiting = false
  11. goTo = function()
  12.   rednet.send(id, "goto") --When a waiting turtle is found send the command (goto) to its ID so it can ready for the transmission of coordinates
  13.   clear()
  14.   print("Enter goto coordinates")
  15.   print("Enter X")
  16.   local gox = tonumber(io.read()) --Get the coordinates from the player, they could be acquired by another means if needed
  17.   clear()
  18.   print("Enter Y")
  19.   local goy = tonumber(io.read())
  20.   clear()
  21.   print("Enter Z")
  22.   local goz = tonumber(io.read())
  23.   clear()
  24.   print("Coordinates are: ")
  25.   print("X: ",gox)
  26.   print("Y: ",goy)
  27.   print("Z: ",goz)
  28.   print("Sending coordinates")
  29.   rednet.send(id, tostring(gox)) --Convert the coordinates to strings (as rednet only sends strings) and send them to the waiting turtle
  30.   sleep(0.5)
  31.   rednet.send(id, tostring(goy))
  32.   sleep(0.5)
  33.   rednet.send(id, tostring(goz))
  34.   end
  35.  
  36. goHome = function() -- send command to start gohome.lua
  37. rednet.send(id, "gohome")
  38. end
  39.  
  40.  
  41. goComp = function() -- send command to start gotopc.lua
  42. rednet.send(id, "gocomp")
  43. end
  44.  
  45. exit = function() -- exits back to shell
  46. clear()
  47. exiting = true
  48.  
  49. end
  50.  
  51.  
  52. while not exiting do
  53. clear()
  54. textutils.slowPrint("Richys Remote Turtle Controller!", 10)
  55. print("")
  56. print("Connected, ID: ", os.computerID())
  57. textutils.slowPrint("Listening for turtles....")
  58. id, msg = rednet.receive() --Wait for a turtle to say it is "Listening"
  59. if msg == "Listening" then
  60.  
  61.  
  62.  
  63. print("Turtle found!!!")
  64. sleep(2)
  65. clear()
  66.  
  67. local options = { -- Menu options First part is text to be shown on screen, second part is name of the above function to be called if that text is selected
  68.   {"Goto a position", goTo},
  69.   {"Go Home", goHome},
  70.   {"Go to Main Computer", goComp},
  71.  
  72.   {"Exit", exit}
  73.  
  74. }
  75. local selected = 1
  76.  
  77.  
  78.  
  79. function draw()
  80.   --Set the position for the first option. Change the second number if you want a headline or so...
  81.  
  82.   print("")
  83.   term.setCursorPos(12, 2)
  84.   print("Richys Remote Turtle Controller")
  85.   print("")
  86.   print("What you would like your turtle to do")
  87.   print("")
  88.  
  89.   --Iterate over any option and print them on the screen
  90.   for ind, param in ipairs(options) do
  91.         --Clear line
  92.         term.clearLine()
  93.  
  94.         --Print with or without marker
  95.         if selected == ind then
  96.           print(">"..param[1])
  97.         else
  98.           print(" "..param[1])
  99.         end
  100.   end
  101. end
  102.  
  103. function run()
  104.   while not exiting do
  105.         --Pull the event
  106.         local _, key = os.pullEvent("key")
  107.  
  108.         if key == keys.up and selected > 1 then
  109.           --Decrement selected.
  110.           selected = selected - 1
  111.         elseif key == keys.down and selected < #options then
  112.           --Increment selected.
  113.           selected = selected + 1
  114.         elseif key == keys.enter then
  115.           --Run the function saved in the second slot of the currently selected option.
  116.           options[selected][2]()
  117.           break
  118.         end
  119.  
  120.         --Redraw the screen
  121.         draw()
  122.   end
  123. end
  124.  
  125. draw()
  126. run()
  127.  
  128. end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement