Advertisement
BlueMond

Coord based travel on turtle

Sep 9th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. --start: -275 69 351
  2. --farm: -257 72 385
  3. raise = 10
  4. farmLength = 13
  5. lane = 7
  6.  
  7. shell.run("clear")
  8. print("Searching for location...")
  9. --x, y, z = gps.locate(10)
  10. x = nil
  11. shell.run("clear")
  12. if x == nil then
  13.     print("Search failed.. Reverting to manual input.")
  14.     write("X: ")
  15.     x = read()
  16.     write("Y: ")
  17.     y = read()
  18.     write("Z: ")
  19.     z = read()
  20. else
  21.     print("Location retrieved!")
  22. end
  23. write("Direction: ")
  24. direction = read()
  25.  
  26. print("")
  27. print("Specify destination..")
  28. write("X: ")
  29. xGo = read()
  30. write("Y: ")
  31. yGo = read()
  32. write("Z: ")
  33. zGo = read()
  34. write("Direction: ")
  35. dGo = read()
  36.  
  37. function vertical(dir,amount)
  38.     repeat
  39.         if dir == 1 then
  40.             turtle.up()
  41.         else
  42.             turtle.down()
  43.         end
  44.         amount = amount - 1
  45.     until amount == 0
  46. end
  47.  
  48. function switchLane(dir)
  49.     if dir == "right" then
  50.         turtle.turnRight()
  51.         turtle.forward()
  52.         turtle.turnRight()
  53.     elseif dir == "left" then
  54.         turtle.turnLeft()
  55.         turtle.forward()
  56.         turtle.turnLeft()
  57.     end
  58. end
  59.  
  60. function farmIt(forward)
  61.     turtle.select(1)
  62.     turtle.digDown()
  63.     turtle.select(2)
  64.     turtle.placeDown()
  65.    
  66.     if forward then
  67.         turtle.forward()
  68.     end
  69. end
  70.  
  71. function go(amount)
  72.     repeat
  73.         amount = amount - 1
  74.         turtle.forward()
  75.     until amount == 0
  76. end
  77.  
  78. function placeIn(slot)
  79.     turtle.suck()
  80.     amount = turtle.getItemCount(slot)
  81.     turtle.drop(amount-1)
  82. end
  83.  
  84. function face(num)
  85.     while direction ~= num do
  86.         if direction == 3 then
  87.             direction = 0
  88.         else
  89.             direction = direction + 1
  90.         end
  91.         turtle.turnRight()
  92.     end
  93. end
  94.  
  95. function goto(x1,y1,z1,f)
  96.     vertical(1,raise)
  97.  
  98.     if x1 > x then
  99.       face(3)
  100.       go(x1-x)
  101.     end
  102.     if x1 < x then
  103.       face(1)
  104.       go(x-x1)
  105.     end
  106.     if z1 > z then
  107.       face(0)
  108.       go(z1-z)
  109.     end
  110.     if z1 < z then
  111.       face(2)
  112.       go(z-z1)
  113.     end
  114.    
  115.     vertical(0,raise)
  116.     --if y1 > y then
  117.       --vertical(1,y1-y)
  118.     --end
  119.     --if y1 < y then
  120.       --vertical(0,y-y1)
  121.     --end
  122.    
  123.     face(f)
  124.     x = x1
  125.     y = y1
  126.     z = z1
  127.     direction = f
  128. end
  129.  
  130. function farm(direc, last)
  131.     amount = farmLength-1
  132.     repeat
  133.         farmIt(true)
  134.         amount = amount - 1
  135.     until amount == 0
  136.     farmIt(false)
  137.    
  138.     if not last then
  139.         if direc == "right" then
  140.             switchLane("right")
  141.         elseif direc == "left" then
  142.             switchLane("left")
  143.         end
  144.     end
  145. end
  146.  
  147. function completeFarm(lanes)
  148.     turn = "right"
  149.     for x=1,lanes do
  150.         if x == lanes then
  151.             farm(turn, true)
  152.         else
  153.             farm(turn, false)
  154.         end
  155.        
  156.         if turn == "right" then
  157.             turn = "left"
  158.         elseif turn == "left" then
  159.             turn = "right"
  160.         end
  161.     end
  162. end
  163.  
  164. goto(xGo,yGo,zGo,dGo)
  165.  
  166. print("Press any key to launch..")
  167. repeat
  168.     local event = os.pullEvent()
  169. until event == "key"
  170.  
  171. x = -263
  172. y = 72
  173. z = 397
  174. direction = 0
  175.  
  176. xGo = -275
  177. yGo = 69
  178. zGo = 351
  179. dGo = 2
  180.  
  181. goto(xGo,yGo,zGo,dGo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement