Advertisement
Xylem_Gaming

Turtle Controller

Feb 7th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. x,y = term.getSize()
  2. running = true
  3. routine = {}
  4.  
  5. --[[
  6. W forward
  7. A left
  8. S back
  9. D right
  10.  
  11. Q up
  12. Z down
  13.  
  14. R dig up
  15. F dig
  16. C dig down
  17.  
  18. T place up
  19. G place
  20. V place down
  21. [ select left slot (navigation)
  22. ] select right slot (navigation)
  23.  
  24. X execute
  25. --]]
  26.  
  27. function drawGUI()
  28.  
  29.   term.clear()
  30.   for i = 1,y do
  31.     term.setCursorPos(1,i)
  32.     if i == 1 then
  33.       fuel=
  34.       print("Fuel: "..turtle.getFuelLevel().." Xylem's TController")
  35.     elseif i == 2 then
  36.       for i = 1,x do
  37.         term.write("_")
  38.       end
  39.     elseif i == y-2 then
  40.       for i = 1,x do
  41.         term.write("_")
  42.       end
  43.     elseif i == y then
  44.       term.write("> ")
  45.     end
  46.   end
  47.  
  48. end
  49.  
  50. function appendRoutine(object,state)
  51.  
  52.   if routine == nil and state == nil then
  53.     routine = {}
  54.     routine[1] = object
  55.   elseif routine~= nil and state == nil then
  56.     index = table.getn(routine) + 1
  57.     routine[index] = object
  58.   elseif object == nil and state == "remove" then
  59.     index2 = table.getn(routine)
  60.     routine[index2] = nil
  61.   end
  62.  
  63.   for i = 1,y do
  64.     term.setCursorPos(1,i)
  65.     if i > 2 and i < y-2 then
  66.       term.clearLine()
  67.     end
  68.   end
  69.  
  70.   term.setCursorPos(1,3)
  71.   cx,cy = term.getCursorPos()
  72.   temp = 3
  73.   temp2 = 0
  74.   for i in ipairs(routine) do
  75.     if temp2 <= x-3 then
  76.       term.write(routine[i]..",")
  77.       temp2 = temp2+3
  78.     else
  79.       temp = temp+1
  80.       temp2 = 0
  81.       term.setCursorPos(1,temp)
  82.       term.write(routine[i]..",")
  83.     end
  84.   end
  85. end
  86.  
  87. drawGUI()
  88.  
  89. function run()
  90.   number = table.getn(routine)
  91.   if number <= 0 then
  92.     term.setCursorPos(1,3)
  93.     term.setTextColour(colours.red)
  94.     print("Nothing to execute")
  95.     term.setTextColour(colours.white)
  96.     sleep(2)
  97.     term.setCursorPos(1,3)
  98.     term.clearLine()
  99.   elseif number > 0 then
  100.     for i in ipairs(routine) do
  101.       if routine[i] == "fw" then
  102.         turtle.forward()
  103.       elseif routine[i] == "lf" then
  104.         turtle.turnLeft()
  105.       elseif routine[i] == "bk" then
  106.         turtle.backward()
  107.       elseif routine[i] == "ri" then
  108.         turtle.turnRight()
  109.       elseif routine[i] == "up" then
  110.         turtle.up()
  111.       elseif routine[i] == "dw" then
  112.         turtle.down()
  113.       elseif routine[i] == "du" then
  114.         turtle.digUp()
  115.       elseif routine[i] == "di" then
  116.         turtle.dig()
  117.       elseif routine[i] == "dd" then
  118.         turtle.digDown()
  119.       elseif routine[i] == "pu" then
  120.         turtle.placeUp()
  121.       elseif routine[i] == "pl" then
  122.         turtle.place()
  123.       elseif routine[i] == "pd" then
  124.         turtle.placeDown()
  125.       end
  126.     end
  127.   end
  128. end
  129.  
  130. while running do
  131.   event, param1, param2 = os.pullEvent()
  132.   term.setCursorPos(1,y)
  133.   if event == "char" or event == "key" then
  134.     term.clearLine()
  135.     term.write("> "..event..": "..param1.."       Credit to AngleMalus")
  136.   end
  137.   if event == "char" then
  138.     if param1 == 'w' then -- w
  139.       appendRoutine("fw")
  140.     elseif param1 == 'a' then -- a
  141.       appendRoutine("lf")
  142.     elseif param1 == 's' then -- s
  143.       appendRoutine("bk")
  144.     elseif param1 == 'd' then -- d
  145.       appendRoutine("ri")
  146.     elseif param1 == 'q' then -- q
  147.       appendRoutine("up")
  148.     elseif param1 == 'z' then -- z
  149.       appendRoutine("dw")
  150.     elseif param1 == 'r' then -- r
  151.       appendRoutine("du")
  152.     elseif param1 == 'f' then -- f
  153.       appendRoutine("di")
  154.     elseif param1 == 'c' then -- c
  155.       appendRoutine("dd")
  156.     elseif param1 == 't' then -- t
  157.       appendRoutine("pu")
  158.     elseif param1 == 'g' then -- g
  159.       appendRoutine("pl")
  160.     elseif param1 == 'v' then -- v
  161.       appendRoutine("pd")
  162.     elseif param1 == 'x' then -- x
  163.       run()
  164.     elseif param1 == '[' then
  165.       cpos = turtle.getSelectedSlot()
  166.       if cpos >= 2 then
  167.         turtle.select(cpos-1)
  168.       end
  169.     elseif param1 == ']' then
  170.       cpos = turtle.getSelectedSlot()
  171.       if cpos <= 15 then
  172.         turtle.select(cpos+1)
  173.       end
  174.     end
  175.   end
  176.   if event == "key" then
  177.     if param1 == 14 then -- backspace
  178.       appendRoutine(nil,"remove")
  179.     end
  180.   end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement