Advertisement
downwind

Standalone Turtle Controller in ComputerCraft

Oct 12th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- This is a standalone 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. function clear() term.clear() term.setCursorPos(1, 1) end
  7. exiting = false
  8.  
  9. goTo = function()
  10.  print("Enter goto coordinates")
  11.   print("Enter X")
  12.   local gox = tonumber(io.read()) --Get the coordinates from the player, they could be acquired by another means if needed
  13.   clear()
  14.   print("Enter Y")
  15.   local goy = tonumber(io.read())
  16.   clear()
  17.   print("Enter Z")
  18.   local goz = tonumber(io.read())
  19.   clear()
  20.   print("Coordinates are: ")
  21.   print("X: ",gox)
  22.   print("Y: ",goy)
  23.   print("Z: ",goz)
  24.   print("Sending coordinates")
  25.   shell.run("goto", gox, goy, goz) --passes the coordinates to and runs the goto program from http://www.computercraft.info/forums2/index.php?/topic/5665-wireless-goto-program-for-gps-turtles/page__fromsearch__1
  26.   end
  27.  
  28. goHome = function() -- send command to start gohome.lua
  29. shell.run("gohome.lua")
  30. end
  31.  
  32.  
  33. goComp = function() -- send command to start gotopc.lua
  34. shell.run("gotopc.lua")
  35. end
  36.  
  37. exit = function() -- exits back to shell
  38. clear()
  39. exiting = true
  40.  
  41. end
  42.  
  43. while not exiting do
  44. clear()
  45. textutils.slowPrint("Richys Turtle Controller!", 10)
  46. sleep(1)
  47. term.clear()
  48.  
  49. 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.
  50.   {"Goto a position", goTo},
  51.   {"Go Home", goHome},
  52.   {"Go to Main Computer", goComp},
  53.   --More can be added then add functions above
  54.   {"Exit", exit}
  55.  
  56. }
  57. local selected = 1
  58.  
  59.  
  60.  
  61. function draw()
  62.   --Set the position for the first option. Change the second number if you want a headline or so...
  63.  
  64.   print("")
  65.   term.setCursorPos(8, 2)
  66.   print("Richys Turtle Controller")
  67.   print("")
  68.   print("What you would like your turtle to do")
  69.   print("")
  70.  
  71.   --Iterate over any option and print them on the screen
  72.   for ind, param in ipairs(options) do
  73.         --Clear line
  74.         term.clearLine()
  75.  
  76.         --Print with or without marker
  77.         if selected == ind then
  78.           print(">"..param[1])
  79.         else
  80.           print(" "..param[1])
  81.         end
  82.   end
  83. end
  84.  
  85. function run()
  86.   while not exiting do
  87.         --Pull the event
  88.         local _, key = os.pullEvent("key")
  89.  
  90.         if key == keys.up and selected > 1 then
  91.           --Decrement selected.
  92.           selected = selected - 1
  93.         elseif key == keys.down and selected < #options then
  94.           --Increment selected.
  95.           selected = selected + 1
  96.         elseif key == keys.enter then
  97.           --Run the function saved in the second slot of the currently selected option.
  98.           options[selected][2]()
  99.           break
  100.         end
  101.  
  102.         --Redraw the screen
  103.         draw()
  104.   end
  105. end
  106.  
  107. draw()
  108. run()
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement