Advertisement
SirBaconBitz

RiM Turtle Controller

May 25th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.01 KB | None | 0 0
  1. rednet.open("left")
  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.       sleep(0.2)
  177.       goUp()
  178.     end
  179.   end
  180.   if xPos < xDest then
  181.     if xDest - xPos < 0 then
  182.       xDis = (xDest - xPos) * -1
  183.     else
  184.       xDis = xDest - xPos
  185.     end
  186.     for i = 1, xDis do
  187.       sleep(0.2)
  188.       goRight()
  189.     end
  190.   elseif xPos > xDest then
  191.     if xPos - xDest < 0 then
  192.       xDis = (xPos - xDest) * -1
  193.     else
  194.       xDis = xPos - xDest
  195.     end
  196.     for i = 1, xDis do
  197.       sleep(0.2)
  198.       goLeft()
  199.     end
  200.   end
  201.   if zPos < zDest then
  202.     if zPos - zDest < 0 then
  203.       zDis = (zPos - zDest) * -1
  204.     else
  205.       --zDis = zDest - xPos
  206.       zDis = zPos - zDest
  207.     end
  208.     if pos == "back" then
  209.       backToTop()
  210.     end
  211.     for i = 1, zDis do
  212.       sleep(0.2)
  213.       goBack()
  214.     end
  215.   elseif zPos > zDest then
  216.     if zDest - zPos < 0 then
  217.       zDis = (zPos - zDest) * -1
  218.     else
  219.       zDis = zPos - zDest
  220.     end
  221.     for i = 1, xDis do
  222.       sleep(0.2)
  223.       goForward()
  224.     end
  225.   end
  226.   if yPos > yDest then
  227.     for i = 1, yPos - yDest do
  228.       sleep(0.2)
  229.       goDown()
  230.     end
  231.   end
  232.   display()
  233.   redstone.setOutput("right", true)
  234.   sleep(0.2)
  235.   redstone.setOutput("right", false)
  236. end
  237.  
  238. -- determine turtle position above or behind the engine
  239. result = fs.exists("pos")
  240. if result == true then
  241.   file = io.open("pos", "r")
  242.   pos = file.read()
  243.   file:close()
  244. else
  245.   pos = "back"
  246.   file = fs.open("pos", "w")
  247.   file.write(pos)
  248.   file:close()
  249. end
  250. if pos == "back" then
  251.   eng = peripheral.wrap("front")
  252. else
  253.   eng = peripheral.wrap("bottom")
  254. end
  255.  
  256.  
  257. function coords()
  258.   file = io.open("x", "r")
  259.   xPos = file.read()
  260.   xPos = tonumber(xPos)
  261.   file:close()
  262.   file = io.open("y", "r")
  263.   yPos = file.read()
  264.   yPos = tonumber(yPos)
  265.   file:close()
  266.   file = io.open("z", "r")
  267.   zPos = file.read()
  268.   zPos = tonumber(zPos)
  269.   file:close()
  270. end
  271.  
  272. result = fs.exists("x") --determine if turtle knows it's position
  273. if result == true then
  274.   file = io.open("x", "r")
  275.   xPos = file.read()
  276.   xPos = tonumber(xPos)
  277.   file:close()
  278.   file = io.open("y", "r")
  279.   yPos = file.read()
  280.   yPos = tonumber(yPos)
  281.   file:close()
  282.   file = io.open("z", "r")
  283.   zPos = file.read()
  284.   zPos = tonumber(zPos)
  285.   file:close()
  286. else
  287.   term.clear()
  288.   term.setCursorPos(1,1)
  289.   write("Enter the Turtle's x coordinate: ")
  290.   xPos = io.read()
  291.   file = fs.open("x", "w")
  292.   file.write(xPos)
  293.   file:close()
  294.   term.clear()
  295.   xPos = tonumber(xPos)
  296.   term.setCursorPos(1,1)
  297.   write("Enter the Turtle's y coordinate: ")
  298.   yPos = io.read()
  299.   file = fs.open("y", "w")
  300.   file.write(yPos)
  301.   file:close()    
  302.   term.clear()
  303.   yPos = tonumber(yPos)
  304.   term.setCursorPos(1,1)
  305.   write("Enter the Turtle's z coordinate: ")
  306.   zPos = io.read()
  307.   file = fs.open("z", "w")
  308.   file.write(zPos)
  309.   file:close()
  310.   zPos = tonumber(zPos)
  311.   term.clear()
  312.   term.setCursorPos(1,1)
  313. end
  314.  
  315. display()
  316. while true do
  317.   needFuel()
  318.   coords()
  319.   timeout = os.startTimer(0.5)
  320.   event, param, param2 = os.pullEvent()
  321.   if event == "timer" then
  322.     if con == "on" then
  323.       if dir ~= 31 then
  324.         if pos == "top" then
  325.           topToBack()
  326.         end
  327.         if dir == 17 then
  328.           goForward()
  329.         elseif dir == 32 then
  330.           goRight()
  331.         elseif dir == 30 then
  332.           goLeft()
  333.         elseif dir == 42 then
  334.           goUp()
  335.         elseif dir == 29 then
  336.           goDown()    
  337.         end
  338.       elseif dir == 31 then
  339.         dir = 31
  340.         if pos == "back" then
  341.           backToTop()
  342.         end
  343.         goBack()
  344.       end
  345.     end
  346.   end
  347.   if param ~= 31 and event == "key" then
  348.     if pos == "top" then
  349.       topToBack()
  350.     end
  351.     if param == 17 then
  352.       goForward()
  353.     elseif param == 32 then
  354.       goRight()
  355.     elseif param == 30 then
  356.       goLeft()
  357.     elseif param == 42 then
  358.       goUp()
  359.     elseif param == 29 then
  360.       goDown()
  361.     elseif param == 19 then
  362.       if con == "on" then
  363.          con = "off"
  364.       else
  365.          con = "on"
  366.       end  
  367.       os.sleep(0.2)
  368.       dir = nil
  369.       term.setCursorPos(x/5*4, y/3-1)
  370.       print(con.." ")  
  371.     elseif param == 46 then
  372.       waypoint()
  373.     end
  374.   elseif param == 31 then
  375.     dir = 31
  376.     if pos == "back" then
  377.       backToTop()
  378.     end
  379.     goBack()
  380.   elseif param2 == "incoming" then
  381.     rednet.send(param, "ready")
  382.     id, xDest = rednet.receive(60)
  383.     id, yDest = rednet.receive(1)
  384.     id, zDest = rednet.receive(1)
  385.     sleep(.1)
  386.     rednet.send(param, "complete")
  387.     xDest = tonumber(xDest)
  388.     yDest = tonumber(yDest)
  389.     zDest = tonumber(zDest)
  390.     travel()
  391.   end
  392. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement