Guest User

Untitled

a guest
Jan 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local tArgs = {...}
  2. function dirHelper1(x,z)
  3.   local x2, y2, z2 = gps.locate(3)
  4.   turtle.back()
  5.   local dx = x - x2
  6.   local dz = z - z2
  7.   if (dx == 0) then
  8.     if (dz > 0) then
  9.   print("Dir: 0")
  10.   return 0
  11. else
  12.   print("Dir: 2")
  13.   return 2
  14. end
  15.   else
  16.     if (dx > 0) then
  17.   print("Dir: 3")
  18.   return 3
  19. else
  20.   print("Dir: 1")
  21.   return 1
  22. end
  23.   end
  24. end
  25.  
  26. function left()
  27.   turtle.turnLeft()
  28.   turtleDIR = math.abs(turtleDIR -1) % 4
  29. end
  30. function right()
  31.   turtle.turnRight()
  32.   turtleDIR = math.abs(turtleDIR +1) %4
  33. end
  34. function getDir()
  35.   if (turtleDIR == nil) then
  36.     print("attempting to determine direction")
  37.     local x,y,z = gps.locate(3)
  38.   if (turtle.forward()) then
  39.     turtleDIR = dirHelper1(x,z)
  40.   else
  41.     turtle.turnLeft()
  42.     if (turtle.forward()) then
  43.       turtleDIR = dirHelper1(x,z)
  44.     else
  45.       turtle.turnLeft()
  46.       if (turtle.forward()) then
  47.         turtleDIR = dirHelper1(x,z)
  48.       else
  49.         turtle.turnLeft()    
  50.         if (turtle.forward()) then
  51.           turtleDIR = dirHelper1(x,z)
  52.         else
  53.           print("I can't figure out where i'm going... craaaash")
  54.           return
  55.         end
  56.       end
  57.     end
  58.   end
  59.   return turtleDIR
  60.   else
  61.   return turtleDIR
  62.   end
  63. end
  64.  
  65. function setDir(d)
  66.   local dir = getDir()
  67.   while (not (d == dir)) do
  68.     right()
  69.     dir = getDir()
  70.   end
  71.  
  72. end
  73.  
  74. -- 0 = -z, 1 = +x, 2 = +z, 3 = -x
  75.  
  76. function deltaMove(dx,dy,dz)
  77.    print("Checking if X direction")
  78.    if (not (dx == 0)) then
  79.     print("There is X")
  80.     if (dx > 0) then
  81.       print("Moving direction 1, +x")
  82.       move(1,math.abs(dx))
  83.     else
  84.       print("Direction 3, -x")
  85.       move(3,math.abs(dx))
  86.     end
  87.   end
  88.   print("Checking if Z direction")
  89.   if (not ( dz == 0)) then
  90.     if (dz > 0) then
  91.       print("Direction 2, +z")
  92.       move(2,math.abs(dz))
  93.     else
  94.       print("Direction 0, -z")
  95.       move(0,math.abs(dz))
  96.     end
  97.   end
  98.   changeHeight(dy)
  99. end
  100. function move(dir, dist)
  101.   if (dist == 0) then
  102.      print("Exiting move(dir, dist), dist is 0")
  103.      return
  104.   end
  105.  print("Setting dir to:")
  106.  print(dir)
  107.    setDir(dir)
  108.   for i = 1, dist do
  109.     while (not turtle.forward()) do
  110.       turtle.dig()
  111.     end
  112.   end
  113. end
  114.  
  115. function changeHeight(delta)
  116.   if (delta == 0) then
  117.     return
  118.   end
  119.   local up = true
  120.   if (delta < 0) then
  121.     up = false
  122.   end
  123.   for i = 1, math.abs(delta) do
  124.     if (up) then
  125.       while (not turtle.up()) do
  126.         turtle.digUp()
  127.       end
  128.     else
  129.       while (not turtle.down()) do
  130.         turtle.digDown()
  131.       end
  132.     end
  133.   end
  134. end
  135. local nx = tonumber(tArgs[1])
  136. local ny = tonumber(tArgs[2])
  137. local nz = tonumber(tArgs[3])
  138. rednet.open("right")
  139. local cx, cy, cz = gps.locate(3)
  140. local dx = nx - cx
  141. local dy = ny - cy
  142. local dz = nz - cz
  143. local fuelCost = math.abs(dx) + math.abs(dy) + math.abs(dz)
  144. local turtleDIR
  145. print("FuelCost: " .. fuelCost)
  146. print("delta's xyz")
  147. print(dx)
  148. print(dy)
  149. print(dz)
  150.  
  151.  
  152.  
  153.  
  154. if (turtle.getFuelLevel() < fuelCost) then
  155.   print("Refueling")
  156.   turtle.refuel(fuelCost)
  157.   if (turtle.getFuelLevel() < fuelCost) then
  158.     print("Not enough fuel")
  159.   else
  160.     print("Starting Movements")
  161.     deltaMove(dx,dy,dz)
  162.   end
  163. else
  164.   print("Starting Movements")
  165.   deltaMove(dx,dy,dz)
  166. end
Add Comment
Please, Sign In to add comment