Advertisement
Guest User

mazeBot2

a guest
Nov 29th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. t = {}
  2.  
  3. t.x = 0
  4. t.z = 0
  5. t.y = 0
  6. t.dir = 0
  7. t.cal = false
  8.  
  9. --In Minecraft axis work like this
  10. --0 = SOUTH, z axis goes up
  11. --1 = WEST, x axis goes down
  12. --2 = NORTH, z axis goes down
  13. --3 = EAST, x axis goes up
  14.  
  15. --math functions to determine length and distance
  16. function math.len(x,y)
  17.     return math.sqrt(x*x + y*y)
  18. end
  19.  
  20. function math.dist(x1,y1, x2,y2)
  21.     return math.len(x1-x2, y1-y2)
  22. end
  23.  
  24.  
  25. function t.forward()
  26.     if t.cal then
  27.         if turtle.forward() then
  28.             if t.dir == 0 then
  29.                 t.z = t.z + 1
  30.             elseif t.dir == 1 then
  31.                 t.x = t.x - 1
  32.             elseif t.dir == 2 then
  33.                 t.z = t.z - 1
  34.             elseif t.dir == 3 then
  35.                 t.x = t.x + 1
  36.             end
  37.         end
  38.     else
  39.         print("not calibrated")
  40.     end
  41. end
  42.  
  43. function t.back()
  44.     if t.cal then
  45.         if turtle.back() then
  46.             if t.dir == 0 then
  47.                 t.z = t.z - 1
  48.             elseif t.dir == 1 then
  49.                 t.x = t.x + 1
  50.             elseif t.dir == 2 then
  51.                 t.z = t.z + 1
  52.             elseif t.dir == 3 then
  53.                 t.x = t.x - 1
  54.             end
  55.  
  56.         end
  57.     else
  58.         print("not calibrated")
  59.     end
  60. end
  61.  
  62. function t.turnLeft()
  63.     if t.cal then
  64.         turtle.turnLeft()
  65.         t.dir = t.dir + 1
  66.         if t.dir > 3 then t.dir = 0 end
  67.     else
  68.         print("not calibrated")
  69.     end
  70. end
  71.  
  72. function t.turnRight()
  73.     if t.cal then
  74.         turtle.turnRight()
  75.         t.dir = t.dir - 1
  76.         if t.dir < 0 then t.dir = 3 end
  77.     else
  78.         print("not calibrated")
  79.     end
  80. end
  81.  
  82. function t.up()
  83.     if t.cal then
  84.         if turtle.up() then
  85.             t.y=t.y+1
  86.  
  87.         end
  88.     else
  89.         print("not calibrated")
  90.     end
  91. end
  92.  
  93. function t.down()
  94.     if t.cal then
  95.         if turtle.down() then
  96.             t.y=t.y-1
  97.  
  98.         end
  99.     else
  100.         print("not calibrated")
  101.     end
  102. end
  103.  
  104. function t.west()
  105.     while t.dir ~= 1 do
  106.         t.turnLeft()
  107.     end
  108.     t.forward()
  109. end
  110.  
  111. function t.east()
  112.     while t.dir ~= 3 do
  113.         t.turnLeft()
  114.     end
  115.     t.forward()
  116. end
  117.  
  118. function t.north()
  119.     while t.dir ~= 2 do
  120.         t.turnLeft()
  121.     end
  122.     t.forward()
  123. end
  124.  
  125. function t.south()
  126.     while t.dir ~= 0 do
  127.         t.turnLeft()
  128.     end
  129.     t.forward()
  130. end
  131.  
  132. function t.calibrate()
  133.     local result = true
  134.     local calibrationStones = {}
  135.     t.dir = -1
  136.     while t.dir < 0 do
  137.         turtle.turnLeft()
  138.         if not turtle.detect() then
  139.             --start position is correct
  140.             t.x=0
  141.             t.y=0
  142.             t.z=0
  143.             t.dir=0
  144.         end
  145.     end
  146.    
  147.    
  148.     local fuel = turtle.getFuelLevel()
  149.     while fuel<8000 and turtle.refuel(10) do
  150.         sleep(0.2)
  151.     end
  152.     if turtle.getFuelLevel() == 0 then
  153.         result = false
  154.         print("ERROR: No Energy or Fuel")
  155.     end
  156.    
  157.     if result then
  158.         print("Calibration Successful")
  159.     else
  160.         print("Calibration Failed")
  161.     end
  162.    
  163.     while t.dir ~= 0 do
  164.         t.turnLeft()
  165.         sleep(0.2)
  166.     end
  167.    
  168.     t.cal = result
  169.    
  170. end
  171.  
  172. function t.flyHome()
  173.     print("returning home from "..t.x..","..t.z)
  174.     local returnHeight = math.random(12,18)
  175.    
  176.     if t.x+t.y+t.z ~= 0 then
  177.         while t.y < returnHeight do
  178.             t.up()
  179.             sleep(0.2)
  180.         end
  181.        
  182.         while t.x > 0 do
  183.             t.west()
  184.             sleep(0.2)
  185.         end
  186.        
  187.         while t.x < 0 do
  188.             t.east()
  189.             sleep(0.2)
  190.         end
  191.        
  192.         while t.z > 0 do
  193.             t.north()
  194.             sleep(0.2)
  195.         end
  196.        
  197.         while t.z < 0 do
  198.             t.south()
  199.             sleep(0.2)
  200.         end
  201.        
  202.         while t.y > 0 do
  203.             t.down()
  204.             sleep(0.2)
  205.         end
  206.        
  207.         while t.dir ~= 0 do
  208.             t.turnLeft()
  209.             sleep(0.2)
  210.         end
  211.    
  212.     end
  213. end
  214.  
  215. function t.sayPlease()
  216.     redstone.setOutput("front",true)
  217.     sleep(2)
  218.     turtle.suckDown(1)
  219.     redstone.setOutput("front",false)
  220.     t.flyHome()
  221. end
  222.  
  223. function t.checkDistance()
  224.     local xDist
  225.     local zDist
  226.     local yDist = 35
  227.     if t.x > 0 then
  228.         xDist = t.x
  229.     else
  230.         xDist = t.x * -1
  231.     end
  232.    
  233.     if t.z > 0 then
  234.         zDist = t.z
  235.     else
  236.         zDist = t.z * -1
  237.     end    
  238.     local distanceHome = xDist+zDist+yDist
  239.     local fuelLevel = turtle.getFuelLevel()
  240.     print(distanceHome.." "..fuelLevel)
  241.     if fuelLevel < distanceHome then
  242.         print("almost out of fuel, returning home")
  243.         return false
  244.     end
  245.     return true
  246. end
  247.  
  248. --[[t.calibrate()
  249. if t.cal then
  250. t.forward()
  251. t.forward()
  252. t.forward()
  253. t.forward()
  254. t.turnLeft()
  255. t.forward()
  256. t.forward()
  257. t.forward()
  258. t.forward()
  259. t.forward()
  260. t.turnRight()
  261. t.turnRight()
  262. t.back()
  263. t.back()
  264. t.back()
  265. t.back()
  266. t.flyHome()
  267. end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement