Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. local VERSION = 6
  2. local muffy = {}
  3. local directionTable = {}
  4. local facingDirection = -1
  5.  
  6. directionTable[0] = "east"
  7. directionTable[1] = "north"
  8. directionTable[2] = "west"
  9. directionTable[3] = "south"
  10.  
  11. function muffy.getVersion()
  12.     return VERSION
  13. end
  14.  
  15. function muffy.getFacing()
  16.     if facingDirection >= 0 then
  17.         return directionTable[facingDirection]
  18.     else
  19.         return nil
  20.     end
  21. end
  22.  
  23. function muffy.turnRight()
  24.     facingDirection = facingDirection - 1
  25.     if facingDirection < 0 then
  26.         facingDirection = 3
  27.     end
  28.     print("facingDirection = "..facingDirection)
  29.     turtle.turnRight()
  30. end
  31.  
  32. function muffy.turnLeft()
  33.     facingDirection = (facingDirection + 1) % 4
  34.     turtle.turnLeft()
  35. end
  36.  
  37. function muffy.calibrate()
  38.  
  39.     -- First get our location
  40.     local x, y, z = gps.locate(5)
  41.     if not x then
  42.         return false
  43.     end
  44.    
  45.     -- Now figure out which way we're
  46.     --    facing
  47.     turtle.forward()
  48.     local x1, y1, z1 = gps.locate(5)
  49.    
  50.     -- See which variable changed
  51.     if x1 < x then
  52.         print("X decreased: ("..x..")->("..x1..")")
  53.         facingDirection = 2
  54.     elseif x1 > x then
  55.         print("X increased: ("..x..")->("..x1..")")
  56.         facingDirection = 0
  57.     elseif z1 < z then
  58.         print("Z decreased: ("..z..")->("..z1..")")
  59.         facingDirection = 1
  60.     elseif z1 > z then
  61.         print("Z increased: ("..z..")->("..z1..")")
  62.         facingDirection = 3
  63.     else
  64.         print("WTF DID YOU DO")
  65.         return false    
  66.     end
  67.     print("calibrate = "..facingDirection)
  68.     turtle.back()
  69.     return true    
  70. end
  71.  
  72. local function moveXAxis(dX, avoid)
  73.     -- Move some direction along the X axis
  74.     if dX > 0 then
  75.         while facingDirection ~= 0 do
  76.             muffy.turnRight()
  77.         end
  78.         for i=dX, 1, -1 do
  79.             if not turtle.forward() then
  80.                 avoid()
  81.             end
  82.             print("dX = "..i)
  83.         end
  84.     elseif dX < 0 then
  85.         while facingDirection ~= 2 do
  86.             muffy.turnRight()
  87.         end
  88.         for i=dX, -1, 1 do
  89.             if not turtle.forward() then
  90.                 avoid()
  91.             end
  92.             print("dX = "..i)
  93.         end
  94.     end
  95. end
  96.  
  97. local function moveZAxis(dZ, avoid)
  98.     if dZ > 0 then
  99.         while facingDirection ~= 3 do
  100.             muffy.turnRight()
  101.         end
  102.         for i=dZ, 1, -1 do
  103.             if not turtle.forward() then
  104.                 avoid()
  105.             end
  106.             print("dZ = "..i)
  107.         end
  108.     elseif dZ < 0 then
  109.         while facingDirection ~= 1 do
  110.             muffy.turnRight()
  111.         end
  112.         for i=dZ, -1, 1 do
  113.             if not turtle.forward() then
  114.                 avoid()
  115.             end
  116.             print("dZ = "..i)
  117.         end
  118.     end
  119. end
  120.  
  121. local function go(dX, dY, dZ, avoid)
  122.    
  123.     -- Always move Y first
  124.     if dY > 0 then
  125.         for i=dY, 1, -1 do
  126.             if not turtle.up() then
  127.                 avoid()
  128.             end
  129.             print("dY = "..i)
  130.         end
  131.     elseif dY < 0 then
  132.         for i=dY, -1, 1 do
  133.             if not turtle.down() then
  134.                 avoid()
  135.             end
  136.             print("dY = "..i)
  137.         end
  138.     end
  139.    
  140.     -- See if our facingDirection matches a direction we
  141.     --    need to go
  142.     if dX ~= 0 and (facingDirection == 0 or facingDirection == 3) then
  143.         moveXAxis(dX, avoid)
  144.        
  145.     elseif dZ ~= 0 and (facingDirection == 1 or facingDirection == 2) then
  146.         moveZAxis(dZ, avoid)
  147.        
  148.     else
  149.         -- We're not facing a direction we need to go, do
  150.         --  x first.
  151.         moveXAxis(dX, avoid)
  152.         moveZAxis(dZ, avoid)
  153.     end
  154. end    
  155.        
  156.        
  157. function muffy.navigate(destX, destY, destZ, avoid)
  158.    
  159.     -- Start by locating our current position
  160.     local cX, cY, cZ = gps.locate(5)
  161.    
  162.     -- Make sure we're calibrated
  163.     if facingDirection == -1 then
  164.         rc = muffy.calibrate()
  165.        
  166.         -- If calibration failed, return false
  167.         if not rc then
  168.             print("Calibration failed!")
  169.             return false
  170.         end
  171.     end
  172.    
  173.     -- Now - for the tricky part. We want to optimize
  174.     --  our movements so that we start moving in the
  175.     --  direction we're facing. So we need to translate
  176.     --  the destination coordinates in (moveX, moveZ, moveY)
  177.  
  178.     while cX ~= destX or cY ~= destY or cZ ~= destZ do
  179.         moveX = destX - cX
  180.         moveZ = destZ - cZ
  181.         moveY = destY - cY
  182.  
  183.         print("moveX = "..moveX)
  184.         print("moveY = "..moveY)
  185.         print("moveZ = "..moveZ)
  186.         go(moveX, moveY, moveZ, avoid)
  187.         cX, cY, cZ = gps.locate(5)
  188.     end
  189.    
  190.     print("We got there!")
  191. end
  192.    
  193.    
  194. return muffy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement