Advertisement
thatparadox

Car.2

Aug 27th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. con = "off"
  4.  
  5. function topToBack() -- move turtle from top of controller to back
  6.    turtle.forward()
  7.    turtle.down()
  8.    turtle.turnRight()
  9.    turtle.turnRight()
  10.    pos = "back"
  11.    file = fs.open("pos", "w")
  12.    file.write(pos)
  13.    file:close()
  14.    eng = peripheral.wrap("front")
  15. end
  16.  
  17. function backToTop() -- move turtle from behind engine to top
  18.    turtle.up()
  19.    turtle.forward()
  20.    turtle.turnRight()
  21.    turtle.turnRight()
  22.    pos = "top"
  23.    file = fs.open("pos", "w")
  24.    file.write(pos)
  25.    file:close()
  26.    eng = peripheral.wrap("bottom")
  27. end
  28.  
  29. -- screen display
  30. term.clear()
  31. x,y = term.getSize()
  32. term.setCursorPos(x/2, y/3-1)
  33. print("W")
  34. term.setCursorPos(x/5*4, y/3-1)
  35. print("Auto(R)")
  36. term.setCursorPos(x/2-3, y/3)
  37. print("Forward")
  38. term.setCursorPos(x/5*4, y/3)
  39. print(con)
  40. term.setCursorPos(x/5, y/3*2)
  41. print("A")
  42. term.setCursorPos(x/2, y/3*2)
  43. print("S")
  44. term.setCursorPos(x/5*4, y/3*2)
  45. print("D")
  46. term.setCursorPos(x/5-2, y/3*2+1)
  47. print("Left")
  48. term.setCursorPos(x/2-2, y/3*2+1)
  49. print("Back")
  50. term.setCursorPos(x/5*4-2, y/3*2+1)
  51. print("Right")
  52. term.setCursorPos(1, y-3)
  53. print("[Shift]")
  54. term.setCursorPos(3,y-2)
  55. print("Up")
  56. term.setCursorPos(1, y-1)
  57. print("[Ctrl]")
  58. term.setCursorPos(2, y)
  59. print("Down")
  60.  
  61.  
  62. -- determine turtle position above or behind the engine
  63. result = fs.exists("pos")
  64. if result == true then
  65.   file = io.open("pos", "r")
  66.   pos = file.read()
  67.   file:close()
  68. else
  69.   pos = "back"
  70.   file = fs.open("pos", "w")
  71.   file.write(pos)
  72.   file:close()
  73. end
  74. if pos == "back" then
  75.   eng = peripheral.wrap("front")
  76. else
  77.   eng = peripheral.wrap("bottom")
  78. end
  79.  
  80. while true do
  81.   timeout = os.startTimer(0.5)
  82.   event, param = os.pullEvent()
  83.   if event == "timer" then
  84.     if con == "on" then
  85.       if dir ~= 31 then
  86.         if pos == "top" then
  87.           topToBack()
  88.         end
  89.         if dir == 17 then
  90.           dir = 17
  91.           eng.move(2, false, false)
  92.           os.sleep(1)
  93.           turtle.forward()
  94.         elseif dir == 32 then
  95.           dir = 32
  96.           eng.move(5, false, false)
  97.           turtle.turnRight()
  98.           turtle.forward()
  99.           turtle.turnLeft()
  100.           sleep(.2)
  101.         elseif dir == 30 then
  102.           dir = 30
  103.           eng.move(4, false, false)
  104.           turtle.turnLeft()
  105.           turtle.forward()
  106.           turtle.turnRight()
  107.           sleep(.2)
  108.         elseif dir == 42 then
  109.           dir = 42
  110.           eng.move(1, false, false)
  111.           turtle.up()
  112.           sleep(1)
  113.         elseif dir == 29 then
  114.           dir = 29
  115.           eng.move(0, false, false)
  116.           turtle.down()
  117.           sleep(1)    
  118.         end
  119.       elseif dir == 31 then
  120.         dir = 31
  121.         if pos == "back" then
  122.           backToTop()
  123.         end
  124.         eng.move(3, false, false)
  125.         turtle.forward()
  126.         sleep(1)
  127.       end
  128.     end
  129.   end
  130.   if param ~= 31 and event == "key" then
  131.     if pos == "top" then
  132.       topToBack()
  133.     end
  134.     if param == 17 then
  135.       dir = 17
  136.       eng.move(2, false, false)
  137.       os.sleep(1)
  138.       turtle.forward()
  139.     elseif param == 32 then
  140.       dir = 32
  141.       eng.move(5, false, false)
  142.       turtle.turnRight()
  143.       turtle.forward()
  144.       turtle.turnLeft()
  145.       sleep(.2)
  146.     elseif param == 30 then
  147.       dir = 30
  148.       eng.move(4, false, false)
  149.       turtle.turnLeft()
  150.       turtle.forward()
  151.       turtle.turnRight()
  152.       sleep(.2)
  153.     elseif param == 42 then
  154.       dir = 42
  155.       eng.move(1, false, false)
  156.       turtle.up()
  157.       sleep(1)
  158.     elseif param == 29 then
  159.       dir = 29
  160.       eng.move(0, false, false)
  161.       turtle.down()
  162.       sleep(1)
  163.     elseif param == 19 then
  164.       if con == "on" then
  165.          con = "off"
  166.       else
  167.          con = "on"
  168.       end  
  169.       os.sleep(0.5)
  170.       dir = nil
  171.     term.setCursorPos(x/5*4, y/3-1)
  172.     print(con.." ")  
  173.     end
  174.   elseif param == 31 then
  175.     dir = 31
  176.     if pos == "back" then
  177.       backToTop()
  178.     end
  179.     eng.move(3, false, false)
  180.     turtle.forward()
  181.     sleep(1)
  182.   end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement