UNOBTANIUM

TurtleInteractions

Apr 5th, 2013
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.64 KB | None | 0 0
  1. --VARIABLES
  2. local version = "TURTLE INTERACTIONS 0.9.1"
  3. local w,h = term.getSize()
  4. local select, turtleslot = 1, 1
  5. local running = true
  6. local a, b, c, d, e, f, z = 6,7,8,9,10,11,h-2
  7. turtle.select(turtleslot)
  8.  
  9. --PRINT
  10.  
  11. local function printCentered(str, ypos)
  12.  term.setCursorPos(w/2 - #str/2, ypos)
  13.  term.write(str)
  14. end
  15.  
  16. local function printRight(str, ypos)
  17.  term.setCursorPos(w-#str, ypos)
  18.  term.write(str)
  19. end
  20.  
  21. function clearScreen()
  22.  term.clear()
  23.  term.setCursorPos(1,1)
  24.  term.clear()
  25. end
  26.  
  27. function drawHeader(title, line)
  28.  printCentered(title, line)
  29.  printCentered(string.rep("-", w), line+1)
  30. end
  31.  
  32. function drawCopyright()
  33.  printRight("by UNOBTANIUM", h)
  34. end
  35.  
  36.  
  37. -- MENUS
  38.  
  39. function drawMenuTurtleInteractions()
  40.  drawHeader(version, 1)
  41.  drawCopyright()
  42.  
  43.  if select == 1 then
  44.   printCentered("> Movement <", a)
  45.  else
  46.   printCentered("Movement", a)
  47.  end
  48.  if select == 2 then
  49.   printCentered("> Actions <", b)
  50.  else
  51.   printCentered("Actions", b)
  52.  end
  53.  if select == 3 then
  54.   printCentered("> Control <", c)
  55.  else
  56.   printCentered("Control", c)
  57.  end
  58.  if select == 4 then
  59.   printCentered("> Exit <", z)
  60.  else
  61.   printCentered("Exit", z)
  62.  end
  63. end
  64.  
  65. function drawMenuTurtleMovement()
  66.  drawHeader(version, 1)
  67.  drawHeader("TURTLE MOVEMENT", 3)
  68.  
  69.  if select == 1 then
  70.   printCentered("> Forward <", a)
  71.  else
  72.   printCentered("Forward", a)
  73.  end
  74.  if select == 2 then
  75.   printCentered("> Back <", b)
  76.  else
  77.   printCentered("Back", b)
  78.  end
  79.  if select == 3 then
  80.   printCentered("> Up <", c)
  81.  else
  82.   printCentered("Up", c)
  83.  end
  84.  if select == 4 then
  85.   printCentered("> Down <", d)
  86.  else
  87.   printCentered("Down", d)
  88.  end
  89.  if select == 5 then
  90.   printCentered("> Turn Left <", e)
  91.  else
  92.   printCentered("Turn Left", e)
  93.  end
  94.  if select == 6 then
  95.   printCentered("> Turn Right <", f)
  96.  else
  97.   printCentered("Turn Right", f)
  98.  end
  99.  if select == 7 then
  100.   printCentered("> Back <", 12)
  101.  else
  102.   printCentered("Back", 12)
  103.  end
  104. end
  105.  
  106. function drawMenuTurtleActions()
  107.  drawHeader(version, 1)
  108.  drawHeader("TURTLE ACTIONS", 3)
  109.  
  110.  if select == 1 then
  111.   printCentered("> Refuel <", a)
  112.  else
  113.   printCentered("Refuel", a)
  114.  end
  115.  if select == 2 then
  116.   printCentered("> Dig <", b)
  117.  else
  118.   printCentered("Dig", b)
  119.  end
  120.  if select == 3 then
  121.   printCentered("> Select <", c)
  122.  else
  123.   printCentered("Select", c)
  124.  end
  125.  if select == 4 then
  126.   printCentered("> Back <", z)
  127.  else
  128.   printCentered("Back", z)
  129.  end
  130. end
  131.  
  132. function drawMenuTurtleDig()
  133.  drawHeader(version, 1)
  134.  drawHeader("TURTLE DIG", 3)
  135.  
  136.  if select == 1 then
  137.   printCentered("> Up <", a)
  138.  else
  139.   printCentered("Up", a)
  140.  end
  141.  if select == 2 then
  142.   printCentered("> Front <", b)
  143.  else
  144.   printCentered("Front", b)
  145.  end
  146.  if select == 3 then
  147.   printCentered("> Down <", c)
  148.  else
  149.   printCentered("Down", c)
  150.  end
  151.  if select == 4 then
  152.   printCentered("> Back <", z)
  153.  else
  154.   printCentered("Back", z)
  155.  end
  156. end
  157.  
  158.  
  159. -- MENUSTATE
  160.  
  161. local menustate = "turtleinteractions"
  162.  
  163. local mopt = {
  164.  ["turtleinteractions"] = {
  165.   options = {"turtlemovement", "turtleactions", "control", "quit"},
  166.   draw = drawMenuTurtleInteractions
  167.  },
  168.  ["turtlemovement"] = {
  169.   options = {"forward", "back", "up", "down", "left", "right" , "turtleinteractions"},
  170.   draw = drawMenuTurtleMovement
  171.  },
  172.  ["turtleactions"] = {
  173.   options = {"refuel", "turtledig", "select", "turtleinteractions"},
  174.   draw = drawMenuTurtleActions
  175.  },
  176.  ["turtledig"] = {
  177.   options = {"digup", "digfront", "digdown", "turtleactions"},
  178.   draw = drawMenuTurtleDig
  179.  }
  180. }
  181.  
  182. function runMenu()
  183.  while true do
  184.   clearScreen()
  185.   mopt[menustate].draw()
  186.  
  187.   local id, key = os.pullEvent("key")
  188.   if key == 200 then
  189.    select = select-1
  190.   end
  191.   if key == 208 then
  192.    select = select+1
  193.   end
  194.   if select == 0 then
  195.    select = #mopt[menustate].options
  196.   end
  197.   if select > #mopt[menustate].options then
  198.    select = 1
  199.   end
  200.   clearScreen()
  201.   if key == 28 then
  202.    if mopt[menustate].options[select] == "quit" then
  203.     running = false
  204.     break
  205.    elseif mopt[menustate].options[select] == "control" then
  206.     control()
  207.    elseif mopt[menustate].options[select] == "forward" then
  208.     turtle.forward()
  209.    elseif mopt[menustate].options[select] == "back" then
  210.     turtle.back()
  211.    elseif mopt[menustate].options[select] == "up" then
  212.     turtle.up()
  213.    elseif mopt[menustate].options[select] == "down" then
  214.     turtle.down()
  215.    elseif mopt[menustate].options[select] == "left" then
  216.     turtle.turnLeft()
  217.    elseif mopt[menustate].options[select] == "right" then
  218.     turtle.turnRight()
  219.    elseif mopt[menustate].options[select] == "refuel" then
  220.     turtle.refuel(1)
  221.    elseif mopt[menustate].options[select] == "select" then
  222.     turtleslot = turtleslot + 1
  223.     if turtleslot > 16 then turtleslot = 1 end
  224.     turtle.select(turtleslot)
  225.    elseif mopt[menustate].options[select] == "digup" then
  226.     turtle.digUp()
  227.    elseif mopt[menustate].options[select] == "digfront" then
  228.     turtle.dig()
  229.    elseif mopt[menustate].options[select] == "digdown" then
  230.     turtle.digDown()
  231.    elseif true then
  232.     menustate = mopt[menustate].options[select]
  233.     select = 1
  234.    end
  235.   end
  236.  end
  237. end
  238.  
  239. function control()
  240.  local blockslot = 1
  241.  local mode = "move"
  242.  local working = true
  243.  turtle.select(blockslot)
  244.  
  245.  function info()
  246.   clearScreen()
  247.   print("Navigate your turtle with your keyboard.")
  248.   print("W A S D --- Horizontal")
  249.   print("Space SHIFT --- Vertical")
  250.   print("E --- Switch between dig mode/ place mode/ move mode")
  251.   print("Q --- Select next slot")
  252.   print("Strg --- Leave the conrol interface.")
  253.   print("")
  254.   print("Turtle Mode: " .. mode)
  255.  end
  256.  
  257.  while working do
  258.   info()
  259.   local id, key = os.pullEvent("key")
  260.   if key == 29 then
  261.    working = false
  262.   elseif key == 16 then
  263.    blockslot = blockslot + 1
  264.    if blockslot == 17 then
  265.     blockslot = 1
  266.    end
  267.    turtle.select(blockslot)
  268.   elseif key == 30 then
  269.    turtle.turnLeft()
  270.   elseif key == 31 then
  271.    turtle.back()
  272.   elseif key == 32 then
  273.    turtle.turnRight()
  274.  
  275.   elseif mode == "move" then
  276.    if key == 17 then
  277.     turtle.forward()
  278.    elseif key == 57 then
  279.     turtle.up()
  280.    elseif key == 42 then
  281.     turtle.down()
  282.    elseif key == 18 then
  283.     mode = "dig"
  284.    end
  285.  
  286.   elseif mode == "dig" then
  287.    if key == 17 then
  288.     turtle.dig()
  289.    elseif key == 57 then
  290.     turtle.digUp()
  291.    elseif key == 42 then
  292.     turtle.digDown()
  293.    elseif key == 18 then
  294.     mode = "place"
  295.    end
  296.  
  297.   elseif mode == "place" then
  298.    if key == 17 then
  299.     turtle.place()
  300.    elseif key == 57 then
  301.     turtle.placeUp()
  302.    elseif key == 42 then
  303.     turtle.placeDown()
  304.    elseif key == 18 then
  305.     mode = "move"
  306.    end
  307.   end
  308.  end
  309. end
  310.  
  311. while running do
  312.  runMenu()
  313. end
Advertisement
Add Comment
Please, Sign In to add comment