Advertisement
thatparadox

Car.3

Aug 28th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 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.  
  30. function goDown()
  31.   dir = 29
  32.   eng.move(0, false, false)
  33.   turtle.down()
  34.   yPos = yPos - 1
  35.   file = fs.open("y", "w")
  36.   file.write(yPos)
  37.   file:close()
  38.   sleep(1)  
  39. end
  40.  
  41. function goUp()
  42.   dir = 42
  43.   eng.move(1, false, false)
  44.   turtle.up()
  45.   yPos = yPos + 1
  46.   file = fs.open("y", "w")
  47.   file.write(yPos)
  48.   file:close()
  49.   sleep(1)
  50. end
  51.  
  52. function goForward()
  53.   dir = 17
  54.   eng.move(2, false, false)
  55.   os.sleep(1)
  56.   zPos = zPos - 1
  57.   file = fs.open("z", "w")
  58.   file.write(zPos)
  59.   file:close()
  60.   turtle.forward()
  61. end
  62.  
  63. function goBack()
  64.   dir = 31
  65.   eng.move(3, false, false)
  66.   turtle.forward()
  67.   zPos = zPos + 1
  68.   file = fs.open("z", "w")
  69.   file.write(zPos)
  70.   file:close()
  71.   sleep(1)
  72. end
  73.  
  74. function goRight()
  75.   dir = 32
  76.   eng.move(5, false, false)
  77.   turtle.turnRight()
  78.   turtle.forward()
  79.   turtle.turnLeft()
  80.   xPos = xPos + 1
  81.   file = fs.open("x", "w")
  82.   file.write(xPos)
  83.   file:close()
  84.   os.sleep(0.2)
  85. end
  86.  
  87. function goLeft()
  88.   eng.move(4, false, false)
  89.   turtle.turnLeft()
  90.   turtle.forward()
  91.   turtle.turnRight()
  92.   xPos = xPos - 1
  93.   file = fs.open("x", "w")
  94.   file.write(xPos)
  95.   file:close()
  96.   os.sleep(0.2)
  97. end
  98.  
  99. function display() -- screen display
  100. term.clear()
  101. x,y = term.getSize()
  102. term.setCursorPos(x/2, y/3-1)
  103. print("W")
  104. term.setCursorPos((x/5*4) - 2, y/3-1)
  105. print("Auto(R)")
  106. term.setCursorPos(x/2-3, y/3)
  107. print("Forward")
  108. term.setCursorPos(x/5*4, y/3)
  109. print(con)
  110. term.setCursorPos(x/5, y/3*2)
  111. print("A")
  112. term.setCursorPos(x/2, y/3*2)
  113. print("S")
  114. term.setCursorPos(x/5*4, y/3*2)
  115. print("D")
  116. term.setCursorPos(x/5-2, y/3*2+1)
  117. print("Left")
  118. term.setCursorPos(x/2-2, y/3*2+1)
  119. print("Back")
  120. term.setCursorPos(x/5*4-2, y/3*2+1)
  121. print("Right")
  122. term.setCursorPos(1, y-3)
  123. print("[Shift]")
  124. term.setCursorPos(3,y-2)
  125. print("Up")
  126. term.setCursorPos(1, y-1)
  127. print("[Ctrl]")
  128. term.setCursorPos(2, y)
  129. print("Down")
  130. end
  131.  
  132. function waypoint() -- lets user set a waypoint to travel to
  133.   term.clear()
  134.   term.setCursorPos(1,1)
  135.   write("Enter x coordinate of destination: ")
  136.   xDest = io.read()
  137.   xDest = tonumber(xDest)
  138.   term.clear()
  139.   term.setCursorPos(1,1)
  140.   write("Enter y coordinate of destination: ")
  141.   yDest = io.read()  
  142.   yDest = tonumber(yDest)
  143.   term.clear()
  144.   term.setCursorPos(1,1)
  145.   write("Enter z coordinate of destination: ")
  146.   zDest = io.read()
  147.   zDest = tonumber(zDest)
  148.   term.clear()
  149.   term.setCursorPos(1,1)
  150.   print("Traveling to "..xDest.." "..yDest.." "..zDest)
  151.   if pos == "top" then
  152.     topToBack()
  153.   end
  154.   if yPos < yDest then
  155.     for i = 1, yDest - yPos do
  156.       goUp()
  157.     end
  158.   elseif yPos > yDest then
  159.     for i = 1, yPos - yDest do
  160.       goDown()
  161.     end
  162.   end
  163.   if xPos < xDest then
  164.     if xDest - xPos < 0 then
  165.       xDis = (xDest - xPos) * -1
  166.     else
  167.       xDis = xDest - xPos
  168.     end
  169.     for i = 1, xDis do
  170.       goRight()
  171.     end
  172.   elseif xPos > xDest then
  173.     if xPos - xDest < 0 then
  174.       xDis = (xPos - xDest) * -1
  175.     else
  176.       xDis = xPos - xDest
  177.     end
  178.     for i = 1, xDis do
  179.       goLeft()
  180.     end
  181.   end
  182.   if zPos < zDest then
  183.     if zPos - zDest < 0 then
  184.       zDis = (zPos - zDest) * -1
  185.     else
  186.       --zDis = zDest - xPos
  187.       zDis = zPos - zDest
  188.     end
  189.     if pos == "back" then
  190.       backToTop()
  191.     end
  192.     for i = 1, zDis do
  193.       goBack()
  194.     end
  195.   elseif zPos > zDest then
  196.     if zDest - zPos < 0 then
  197.       zDis = (zPos - zDest) * -1
  198.     else
  199.       zDis = zPos - zDest
  200.     end
  201.     for i = 1, xDis do
  202.       goForward()
  203.     end
  204.   end
  205. end
  206.  
  207.  
  208. -- determine turtle position above or behind the engine
  209. result = fs.exists("pos")
  210. if result == true then
  211.   file = io.open("pos", "r")
  212.   pos = file.read()
  213.   file:close()
  214. else
  215.   pos = "back"
  216.   file = fs.open("pos", "w")
  217.   file.write(pos)
  218.   file:close()
  219. end
  220. if pos == "back" then
  221.   eng = peripheral.wrap("front")
  222. else
  223.   eng = peripheral.wrap("bottom")
  224. end
  225.  
  226. function coords()
  227.   file = io.open("x", "r")
  228.   xPos = file.read()
  229.   xPos = tonumber(xPos)
  230.   file:close()
  231.   file = io.open("y", "r")
  232.   yPos = file.read()
  233.   yPos = tonumber(yPos)
  234.   file:close()
  235.   file = io.open("z", "r")
  236.   zPos = file.read()
  237.   zPos = tonumber(zPos)
  238.   file:close()
  239. end
  240.  
  241. result = fs.exists("x") --determine if turtle knows it's position
  242. if result == true then
  243.   file = io.open("x", "r")
  244.   xPos = file.read()
  245.   xPos = tonumber(xPos)
  246.   file:close()
  247.   file = io.open("y", "r")
  248.   yPos = file.read()
  249.   yPos = tonumber(yPos)
  250.   file:close()
  251.   file = io.open("z", "r")
  252.   zPos = file.read()
  253.   zPos = tonumber(zPos)
  254.   file:close()
  255. else
  256.   term.clear()
  257.   term.setCursorPos(1,1)
  258.   write("Enter the Turtle's x coordinate: ")
  259.   xPos = io.read()
  260.   file = fs.open("x", "w")
  261.   file.write(xPos)
  262.   file:close()
  263.   term.clear()
  264.   xPos = tonumber(xPos)
  265.   term.setCursorPos(1,1)
  266.   write("Enter the Turtle's y coordinate: ")
  267.   yPos = io.read()
  268.   file = fs.open("y", "w")
  269.   file.write(yPos)
  270.   file:close()    
  271.   term.clear()
  272.   yPos = tonumber(yPos)
  273.   term.setCursorPos(1,1)
  274.   write("Enter the Turtle's z coordinate: ")
  275.   zPos = io.read()
  276.   file = fs.open("z", "w")
  277.   file.write(zPos)
  278.   file:close()
  279.   zPos = tonumber(zPos)
  280.   term.clear()
  281.   term.setCursorPos(1,1)
  282. end
  283.  
  284. while true do
  285.   coords()
  286.   display()
  287.   timeout = os.startTimer(0.5)
  288.   event, param = os.pullEvent()
  289.   if event == "timer" then
  290.     if con == "on" then
  291.       if dir ~= 31 then
  292.         if pos == "top" then
  293.           topToBack()
  294.         end
  295.         if dir == 17 then
  296.           goForward()
  297.         elseif dir == 32 then
  298.           goRight()
  299.         elseif dir == 30 then
  300.           goLeft()
  301.         elseif dir == 42 then
  302.           goUp()
  303.         elseif dir == 29 then
  304.           goDown()    
  305.         end
  306.       elseif dir == 31 then
  307.         dir = 31
  308.         if pos == "back" then
  309.           backToTop()
  310.         end
  311.         goBack()
  312.       end
  313.     end
  314.   end
  315.   if param ~= 31 and event == "key" then
  316.     if pos == "top" then
  317.       topToBack()
  318.     end
  319.     if param == 17 then
  320.       goForward()
  321.     elseif param == 32 then
  322.       goRight()
  323.     elseif param == 30 then
  324.       goLeft()
  325.     elseif param == 42 then
  326.       goUp()
  327.     elseif param == 29 then
  328.       goDown()
  329.     elseif param == 19 then
  330.       if con == "on" then
  331.          con = "off"
  332.       else
  333.          con = "on"
  334.       end  
  335.       os.sleep(0.5)
  336.       dir = nil
  337.       term.setCursorPos(x/5*4, y/3-1)
  338.       print(con.." ")  
  339.     elseif param == 46 then
  340.       waypoint()
  341.     end
  342.     --end
  343.   elseif param == 31 then
  344.     dir = 31
  345.     if pos == "back" then
  346.       backToTop()
  347.     end
  348.     goBack()
  349.   end
  350. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement