Advertisement
thatparadox

Car.31

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