Advertisement
thatparadox

WirelessCarriageTurtle

Aug 29th, 2013
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 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. function needFuel()
  30.   if turtle.getFuelLevel() < 1 then
  31.     for i = 1, 16 do
  32.       turtle.select(i)
  33.       turtle.refuel()
  34.       turtle.select(1)
  35.     end
  36.   end
  37. end
  38.  
  39. function goDown()
  40.   dir = 29
  41.   eng.move(0, false, false)
  42.   turtle.down()
  43.   yPos = yPos - 1
  44.   file = fs.open("y", "w")
  45.   file.write(yPos)
  46.   file:close()
  47.   sleep(1)  
  48. end
  49.  
  50. function goUp()
  51.   dir = 42
  52.   eng.move(1, false, false)
  53.   turtle.up()
  54.   yPos = yPos + 1
  55.   file = fs.open("y", "w")
  56.   file.write(yPos)
  57.   file:close()
  58.   sleep(1)
  59. end
  60.  
  61. function goForward()
  62.   dir = 17
  63.   eng.move(2, false, false)
  64.   os.sleep(1)
  65.   zPos = zPos - 1
  66.   file = fs.open("z", "w")
  67.   file.write(zPos)
  68.   file:close()
  69.   turtle.forward()
  70. end
  71.  
  72. function goBack()
  73.   dir = 31
  74.   eng.move(3, false, false)
  75.   turtle.forward()
  76.   zPos = zPos + 1
  77.   file = fs.open("z", "w")
  78.   file.write(zPos)
  79.   file:close()
  80.   sleep(1)
  81. end
  82.  
  83. function goRight()
  84.   dir = 32
  85.   eng.move(5, false, false)
  86.   turtle.turnRight()
  87.   turtle.forward()
  88.   turtle.turnLeft()
  89.   xPos = xPos + 1
  90.   file = fs.open("x", "w")
  91.   file.write(xPos)
  92.   file:close()
  93.   os.sleep(0.2)
  94. end
  95.  
  96. function goLeft()
  97.   dir = 30
  98.   eng.move(4, false, false)
  99.   turtle.turnLeft()
  100.   turtle.forward()
  101.   turtle.turnRight()
  102.   xPos = xPos - 1
  103.   file = fs.open("x", "w")
  104.   file.write(xPos)
  105.   file:close()
  106.   os.sleep(0.2)
  107. end
  108.  
  109. function display() -- screen display
  110. term.clear()
  111. x,y = term.getSize()
  112. term.setCursorPos(x/2, y/3-1)
  113. print("W")
  114. term.setCursorPos((x/5*4) - 2, y/3-1)
  115. print("Auto(R)")
  116. term.setCursorPos(x/2-3, y/3)
  117. print("Forward")
  118. term.setCursorPos(x/5*4, y/3)
  119. print(con)
  120. term.setCursorPos(x/5, y/3*2-1)
  121. print("A")
  122. term.setCursorPos(x/2, y/3*2-1)
  123. print("S")
  124. term.setCursorPos(x/5*4, y/3*2-1)
  125. print("D")
  126. term.setCursorPos(x/5-1, y/3*2)
  127. print("Left")
  128. term.setCursorPos(x/2-1, y/3*2)
  129. print("Back")
  130. term.setCursorPos(x/5*4-2, y/3*2)
  131. print("Right")
  132. term.setCursorPos(1, y-3)
  133. print("[Shift]")
  134. term.setCursorPos(x/5*4, y-3)
  135. print("C")
  136. term.setCursorPos(3,y-2)
  137. print("Up")
  138. term.setCursorPos(x/5*4 - 1,y-2)
  139. print("Set")
  140. term.setCursorPos(1, y-1)
  141. print("[Ctrl]")
  142. term.setCursorPos(x/5*4-5, y-1)
  143. print("Destination")
  144. term.setCursorPos(2, y)
  145. print("Down")
  146. end
  147.  
  148. function waypoint() -- lets user set a waypoint to travel to
  149.   term.clear()
  150.   term.setCursorPos(1,1)
  151.   write("Enter x coordinate of destination: ")
  152.   xDest = io.read()
  153.   xDest = tonumber(xDest)
  154.   term.clear()
  155.   term.setCursorPos(1,1)
  156.   write("Enter y coordinate of destination: ")
  157.   yDest = io.read()  
  158.   yDest = tonumber(yDest)
  159.   term.clear()
  160.   term.setCursorPos(1,1)
  161.   write("Enter z coordinate of destination: ")
  162.   zDest = io.read()
  163.   zDest = tonumber(zDest)
  164.   term.clear()
  165.   term.setCursorPos(1,1)
  166.   print("Traveling to "..xDest.." "..yDest.." "..zDest)
  167.   travel()
  168. end
  169.  
  170. function travel() -- send turtle to user specified waypoint
  171.   if pos == "top" then
  172.     topToBack()
  173.   end
  174.   if yPos < yDest then
  175.     for i = 1, yDest - yPos do
  176.       goUp()
  177.     end
  178.   end
  179.   if xPos < xDest then
  180.     if xDest - xPos < 0 then
  181.       xDis = (xDest - xPos) * -1
  182.     else
  183.       xDis = xDest - xPos
  184.     end
  185.     for i = 1, xDis do
  186.       goRight()
  187.     end
  188.   elseif xPos > xDest then
  189.     if xPos - xDest < 0 then
  190.       xDis = (xPos - xDest) * -1
  191.     else
  192.       xDis = xPos - xDest
  193.     end
  194.     for i = 1, xDis do
  195.       goLeft()
  196.     end
  197.   end
  198.   if zPos < zDest then
  199.     if zPos - zDest < 0 then
  200.       zDis = (zPos - zDest) * -1
  201.     else
  202.       --zDis = zDest - xPos
  203.       zDis = zPos - zDest
  204.     end
  205.     if pos == "back" then
  206.       backToTop()
  207.     end
  208.     for i = 1, zDis do
  209.       goBack()
  210.     end
  211.   elseif zPos > zDest then
  212.     if zDest - zPos < 0 then
  213.       zDis = (zPos - zDest) * -1
  214.     else
  215.       zDis = zPos - zDest
  216.     end
  217.     for i = 1, xDis do
  218.       goForward()
  219.     end
  220.   end
  221.   if yPos > yDest then
  222.     for i = 1, yPos - yDest do
  223.       goDown()
  224.     end
  225.   end
  226.   display()
  227. end
  228.  
  229. -- determine turtle position above or behind the engine
  230. result = fs.exists("pos")
  231. if result == true then
  232.   file = io.open("pos", "r")
  233.   pos = file.read()
  234.   file:close()
  235. else
  236.   pos = "back"
  237.   file = fs.open("pos", "w")
  238.   file.write(pos)
  239.   file:close()
  240. end
  241. if pos == "back" then
  242.   eng = peripheral.wrap("front")
  243. else
  244.   eng = peripheral.wrap("bottom")
  245. end
  246.  
  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. display()
  307. while true do
  308.   needFuel()
  309.   coords()
  310.   timeout = os.startTimer(0.5)
  311.   event, param, param2 = 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.     end
  365.   elseif param == 31 then
  366.     dir = 31
  367.     if pos == "back" then
  368.       backToTop()
  369.     end
  370.     goBack()
  371.   elseif param2 == "waypoint" then
  372.     rednet.send(param, "ready")
  373.     id, xDest = rednet.receive(60)
  374.     id, yDest = rednet.receive(.2)
  375.     id, zDest = rednet.receive(.2)
  376.     sleep(.1)
  377.     rednet.send(param, "complete")
  378.     xDest = tonumber(xDest)
  379.     yDest = tonumber(yDest)
  380.     zDest = tonumber(zDest)
  381.     travel()
  382.   end
  383. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement