imposiblaa

turtleUtils

Mar 5th, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.50 KB | None | 0 0
  1. local turtleutils = { } do
  2. local currentDirection = 1
  3.  
  4.  
  5.  
  6. function turtleutils.dirInit()
  7.     trying = true
  8.     tryingNum = 1
  9.     while(trying)do
  10.         startLocation = {gps.locate()}
  11.         if(tryingNum == 1)then
  12.             turtle.forward()
  13.             endLocation = {gps.locate()}
  14.             if(endLocation[1] > startLocation[1])then
  15.                 currentDirection = 2
  16.                 trying = false
  17.             elseif(endLocation[1] < startLocation[1])then
  18.                 currentDirection = 4
  19.                 trying = false
  20.             elseif(endLocation[3] > startLocation[3])then
  21.                 currentDirection = 3
  22.                 trying = false
  23.             elseif(endLocation[3] < startLocation[3])then
  24.                 currentDirection = 1
  25.                 trying = false
  26.             end
  27.         elseif(tryingNum == 2)then
  28.             turtle.back()
  29.             endLocation = {gps.locate()}
  30.             if(endLocation[1] > startLocation[1])then
  31.                 currentDirection = 4
  32.                 trying = false
  33.             elseif(endLocation[1] < startLocation[1])then
  34.                 currentDirection = 2
  35.                 trying = false
  36.             elseif(endLocation[3] > startLocation[3])then
  37.                 currentDirection = 1
  38.                 trying = false
  39.             elseif(endLocation[3] < startLocation[3])then
  40.                 currentDirection = 3
  41.                 trying = false
  42.             end
  43.         end
  44.         tryingNum = tryingNum + 1
  45.     end
  46. end
  47.  
  48. --gets and stores the direction of the turtle
  49. function direction(change, strict)
  50.     if change == "left" then
  51.         if currentDirection == 1 then
  52.             currentDirection = 4
  53.         else
  54.             currentDirection = currentDirection - 1
  55.         end
  56.         return currentDirection
  57.     elseif change == "right" then
  58.         if currentDirection == 4 then
  59.             currentDirection = 1
  60.         else
  61.             currentDirection = currentDirection + 1
  62.         end
  63.         return currentDirection
  64.     else
  65.         if strict then
  66.             print("the function \"direction\" recieved and invalid direction while sctrict was set to true")
  67.         else
  68.             return currentDirection
  69.         end
  70.     end
  71. end
  72.  
  73. function turtleutils.turn(idirection)
  74.     if idirection == "left" then
  75.         turtle.turnLeft()
  76.     elseif idirection == "right" then
  77.         turtle.turnRight()
  78.     end
  79.     return direction(idirection,  true)
  80. end
  81.  
  82. -- moves and mines at the same time to ensure there is no block where it is trying to move
  83. function turtleutils.moveMine(idirection)
  84.     local waitingCount = 0
  85.     if idirection == "up" then
  86.         location = {gps.locate()}
  87.         moving = true
  88.         while moving do
  89.             bool, block = turtle.inspectUp()
  90.             if block.name ~= "computercraft:turtle_expanded" then
  91.                 turtle.digUp()
  92.             else
  93.                 waitingCount = waitingCount + 1
  94.                 sleep(1)
  95.             end
  96.             turtle.up()
  97.             local newLocation = {gps.locate()}
  98.             if newLocation ~= location then
  99.                 moving = false
  100.             end
  101.             if waitingCount > 5 then
  102.                 turtle.dig()
  103.                 turtle.forward()
  104.                 return
  105.             end
  106.         end
  107.     elseif idirection == "forward" then
  108.         location = {gps.locate()}
  109.         moving = true
  110.         while moving do
  111.             bool, block = turtle.inspect()
  112.             if block.name ~= "computercraft:turtle_expanded" then
  113.                 turtle.dig()
  114.             else
  115.                 waitingCount = waitingCount + 1
  116.                 sleep(1)
  117.             end
  118.             turtle.forward()
  119.             local newLocation = {gps.locate()}
  120.             if newLocation ~= location then
  121.                 moving = false
  122.             end
  123.             if waitingCount > 5 then
  124.                 turtle.digDown()
  125.                 turtle.down()
  126.                 turtle.digDown()
  127.                 turtle.down()
  128.                 sleep(3)
  129.                 return
  130.             end
  131.         end
  132.     elseif idirection == "down" then
  133.         location = {gps.locate()}
  134.         moving = true
  135.         while moving do
  136.             bool, block = turtle.inspectDown()
  137.             if block.name ~= "computercraft:turtle_expanded" then
  138.                 turtle.digDown()
  139.             else
  140.                 waitingCount = waitingCount + 1
  141.             end
  142.             turtle.down()
  143.             local newLocation = {gps.locate()}
  144.             if newLocation ~= location then
  145.                 moving = false
  146.             end
  147.             if waitingCount > 5 then
  148.                 turtle.dig()
  149.                 turtle.forward()
  150.                 return
  151.             end
  152.         end
  153.     else
  154.         print("function \"moveMine\" recieved invalid direction")
  155.     end
  156. end
  157.  
  158. -- goes to a coordinate position
  159. function turtleutils.turtleGoto(x, y, z, doMoveMine, reverse, gettingValues)
  160.     if gettingValues then
  161.         location = {gps.locate()}
  162.         for part = 1, #location do
  163.             location[part] = math.floor(location[part]+0.5)
  164.         end
  165.         distances = {x - location[1], y - location[2], z - location[3]}
  166.         return distances
  167.     end
  168.     pathing = true
  169.     if reverse then
  170.         while pathing do
  171.             location = {gps.locate()}
  172.             for part = 1, #location do
  173.                 location[part] = math.floor(location[part]+0.5)
  174.             end
  175.             distances = {x - location[1], y - location[2], z - location[3]}
  176.             print(textutils.serialize(distances))
  177.             if distances[3] > 0  then
  178.                 while direction() ~= 3 do
  179.                     turtleutils.turn("left")
  180.                 end
  181.                 if(domoveMine)then
  182.                     moveMine("forward")
  183.                 else
  184.                     turtle.forward()
  185.                 end
  186.             elseif distances[3] < 0 then
  187.                 while direction() ~= 1 do
  188.                     turtleutils.turn("left")
  189.                 end
  190.                 if(domoveMine)then
  191.                     moveMine("forward")
  192.                 else
  193.                     turtle.forward()
  194.                 end
  195.             elseif distances[1] > 0 then
  196.                 while direction() ~= 2 do
  197.                     turtleutils.turn("right")
  198.                 end
  199.                 if(domoveMine)then
  200.                     moveMine("forward")
  201.                 else
  202.                     turtle.forward()
  203.                 end
  204.             elseif distances[1] < 0 then
  205.                 while direction() ~= 4 do
  206.                     turtleutils.turn("left")
  207.                 end
  208.                 if(domoveMine)then
  209.                     moveMine("forward")
  210.                 else
  211.                     turtle.forward()
  212.                 end
  213.             elseif distances[2] > 0 then
  214.                 if(domoveMine)then
  215.                     moveMine("up")
  216.                 else
  217.                     turtle.up()
  218.                 end
  219.             elseif distances[2] < 0 then
  220.                 if(domoveMine)then
  221.                     moveMine("down")
  222.                 else
  223.                     turtle.down()
  224.                 end
  225.             else
  226.                 print("at location")
  227.                 pathing = false
  228.                 return
  229.             end
  230.         end
  231.     else
  232.         while pathing do
  233.             location = {gps.locate()}
  234.             for part = 1, #location do
  235.                 location[part] = math.floor(location[part]+0.5)
  236.             end
  237.             distances = {x - location[1], y - location[2], z - location[3]}
  238.             print(textutils.serialize(distances))
  239.             if distances[2] > 0 then
  240.                 if(domoveMine)then
  241.                     moveMine("up")
  242.                 else
  243.                     turtle.up()
  244.                 end
  245.             elseif distances[2] < 0 then
  246.                 if(domoveMine)then
  247.                     moveMine("down")
  248.                 else
  249.                     turtle.down()
  250.                 end
  251.             elseif distances[1] > 0 then
  252.                 while direction() ~= 2 do
  253.                     turtleutils.turn("right")
  254.                 end
  255.                 if(domoveMine)then
  256.                     moveMine("forward")
  257.                 else
  258.                     turtle.forward()
  259.                 end
  260.             elseif distances[1] < 0 then
  261.                 while direction() ~= 4 do
  262.                     turtleutils.turn("left")
  263.                 end
  264.                 if(domoveMine)then
  265.                     moveMine("forward")
  266.                 else
  267.                     turtle.forward()
  268.                 end
  269.             elseif distances[3] > 0  then
  270.                 while direction() ~= 3 do
  271.                     turtleutils.turn("left")
  272.                 end
  273.                 if(domoveMine)then
  274.                     moveMine("forward")
  275.                 else
  276.                     turtle.forward()
  277.                 end
  278.             elseif distances[3] < 0 then
  279.                 while direction() ~= 1 do
  280.                     turtleutils.turn("left")
  281.                 end
  282.                 if(domoveMine)then
  283.                     moveMine("forward")
  284.                 else
  285.                     turtle.forward()
  286.                 end
  287.             else
  288.                 print("at location")
  289.                 pathing = false
  290.                 return
  291.             end
  292.         end
  293.     end
  294. end
  295.  
  296. function turtleutils.getCoalCount(x, y, z)
  297.     local distance = ((math.abs(x) + math.abs(y) + math.abs(z)) * 2)
  298.     print(distance)
  299.     return distance / 80 + 3
  300. end
  301.  
  302.  
  303. end return turtleutils
  304.  
Add Comment
Please, Sign In to add comment