SHARE
TWEET

CC: BTA

-informer- Aug 11th, 2018 (edited) 192 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. A robust turtle API for computercraft that improves upon functions and adds new ones.
  3. Pastebin: MdD051uF
  4.     Functions:
  5.         calibrate() to the cardinal directions
  6.         boxTo(destinationVector[, fn]) moves to every location between two coordinates
  7.         goTo(vector[, fn]) moves the turtle to a specific vector
  8.         move(direction[, fn][, count]) multiple times, foward-back-up-down
  9.         refuel() checks inventory for fuel
  10.         xMovement(distance[, fn]) moves the turtle along the x-axis
  11.         yMovement(distance[, fn]) moves the turtle along the y-axis
  12.         zMovement(distance[, fn]) moves the turtle along the z-axis
  13.     Packages:
  14.         turn.
  15.             north() (etc..) face the four cardinal directions
  16.             spin(direction[, count]) multiple times
  17.             toCardinal(str) face cardinal direction by user input
  18.             getDirection() get current direction as a string value
  19. *It is assumed you calibrate() or face the turtle north, or the program won't function as expected. This is currently
  20. done when the API is loaded and shouldn't be edited out.
  21.  
  22. This API should be used with lua's require method
  23.  
  24. @author informer
  25. @date February 14th 2019
  26. @version 0.1.2
  27. ]]--
  28. local BTA = {}
  29. ---Local Variables---
  30. local currentDirection = 0
  31.  
  32. ---Functions--
  33. --[[
  34. Attempts to calibrate the the turtle using the gps.
  35. ]]--
  36. function BTA.calibrate()
  37.     if gps.locate(5) == nil or turtle.getFuelLevel() < 1 then
  38.         error("Please supply your turtle with fuel and a gps for calibration")
  39.     end
  40.  
  41.     local location1 = vector.new(gps.locate(5))
  42.     turtle.forward()
  43.     local location2 = vector.new(gps.locate(5))
  44.     turtle.back()
  45.     local location3 = location2 - location1
  46.     if location3.z == -1 then
  47.         currentDirection = 0
  48.     elseif location3.x == 1 then
  49.         currentDirection = 1
  50.     elseif location3.z == 1 then
  51.         currentDirection = 2
  52.     elseif location3.x == -1 then
  53.         currentDirection = 3
  54.     end
  55. end
  56.  
  57. --[[
  58. Will move the turtle to the specifed vector, hitting every cooridnate in between the two points.
  59. @parameter destinationVector the coordinates to travel to
  60. @optionalParameter fn a function to apply at each step
  61. @return true if the turtle is able to travel to the vector
  62. ]]--
  63. function BTA.boxTo(destinationVector, fn)
  64.     local location = vector.new(gps.locate(5))
  65.     if location.x == nil then
  66.         error("This function needs a gps work.")
  67.     end
  68.     local distance = destinationVector - location
  69.     local coordinates = {}
  70.  
  71.     --[[
  72.     Helper function to move along a layer. Width and length should be found by subtracting the turtle's
  73.     current location from it's next destination location.
  74.     @oarameter y the y value to add
  75.     @parameter to the vector we are heading to
  76.     @parameter from the vector we are coming from
  77.     ]]--
  78.     local function addLayer(y, to, from)
  79.         local length = to - from
  80.         local switch, x, z = 0
  81.         for i=0, math.abs(length.x) do
  82.             x = (length.x > 0) and (from.x + i) or (from.x - i)
  83.             z = (switch > 1) and from.z or to.z
  84.             switch = (switch > 1) and 0 or 2
  85.             coordinates[#coordinates+1] = vector.new(x, y, z)
  86.         end
  87.     end
  88.  
  89.     --Find coordinates to traverse
  90.     if distance.y == 0 then
  91.         addLayer(location.y, destinationVector, location)
  92.     else
  93.         local ySpaces = (distance.y > 0) and 1 or -1
  94.         local locY, to, from, y, temp = location.y, destinationVector, location
  95.         for i=0, math.abs(distance.y) do
  96.             y = (distance.y > 0) and (locY + i) or (locY - i)
  97.             addLayer(y, to, from)
  98.             temp = from
  99.             from = to
  100.             to = temp
  101.         end
  102.     end
  103.  
  104.     --go to said coordinates
  105.     for i=1, #coordinates do
  106.         BTA.goTo(coordinates[i], fn)
  107.     end
  108.  
  109.     --odd fix (ends up on oppisite side)
  110.     if location:dot(destinationVector)%2 == 1 then
  111.         return BTA.goTo(destinationVector)
  112.     end
  113.  
  114.     --check for truth
  115.     local location = vector.new(gps.locate(5))
  116.     --cross product of same vector is 0 vector, dot product of anything else will result in 0
  117.     if location:cross(destinationVector):dot(vector.new(1,1,1)) == 0 then
  118.         return true
  119.     else
  120.         return false, destinationVector
  121.     end
  122. end
  123.  
  124. --[[
  125. Will move the turtle to the specifed vector.
  126. @parameter destinationVector the coordinates to travel to
  127. @optionalParameter fn a function to apply at each step
  128. @return true if the turtle is able to travel to the vector
  129. ]]--
  130. function BTA.goTo(destinationVector, fn)
  131.     local location = vector.new(gps.locate(5))
  132.     if location.x == nil then
  133.         error("This function needs a gps to work.")
  134.     end
  135.     local distance = destinationVector - location
  136.        
  137.     BTA.yMovement(distance.y, fn)
  138.     BTA.xMovement(distance.x, fn)
  139.     BTA.zMovement(distance.z, fn)
  140.  
  141.     local location = vector.new(gps.locate(5))
  142.     --see boxTo for explanation
  143.     if location:cross(destinationVector):dot(vector.new(1,1,1)) == 0 then
  144.         return true
  145.     else
  146.         return false, destinationVector
  147.     end
  148. end
  149.  
  150. --[[
  151. Moves the turtle in n number of blocks in one direction. Optionally applying a function
  152. during each movement.
  153.  
  154. @parameter direction the direction to move, 1:forward, 2:back, 3:up, 4:down
  155. @optionalParameter fn a function to apply at each step
  156. @optionalParameter count the amount of blocks to traverse
  157. @return true if unimpeded; if false returns the remaining count
  158. ]]--
  159. function BTA.move(direction, fn, count)
  160.     local count = (count ~= nil) and count or 1
  161.     if fn ~= nil then
  162.         fn()
  163.     end
  164.  
  165.     --help function
  166.     local function counter()
  167.         count = count - 1
  168.         if fn ~= nil then
  169.             fn()
  170.         end
  171.     end
  172.  
  173.     while count > 0 do
  174.         if direction == 1 or string.lower(direction) == "forward" then
  175.             if turtle.forward() then
  176.                 counter()
  177.             else
  178.                 return false, count
  179.             end
  180.         elseif direction == 2 or string.lower(direction) == "back" then
  181.             if turtle.back() then
  182.                 counter()
  183.             else
  184.                 return false, count
  185.             end
  186.         elseif direction == 3 or string.lower(direction) == "up" then
  187.             if turtle.up() then
  188.                 counter()
  189.             else
  190.                 return false, count
  191.             end
  192.         elseif direction == 4 or string.lower(direction) == "down" then
  193.             if turtle.down() then
  194.                 counter()
  195.             else
  196.                 return false, count
  197.             end
  198.         end
  199.     end
  200.     return true
  201. end
  202.  
  203. ---Turn package---
  204. turn = {}
  205.  
  206. --[[
  207. Gets the direction the turtle is currently facing.
  208. @return number representing the direction the turtle is facing
  209. ]]--
  210. function turn.getDirection()
  211.     local direction = nil
  212.     local currDir = currentDirection % 4
  213.     if currDir == 0 then
  214.         return "north"
  215.     elseif currDir == 1 then
  216.         return "east"
  217.     elseif currDir == 2 then
  218.         return "south"
  219.     elseif currDir == 3 then
  220.         return "west"
  221.     end
  222. end
  223.  
  224. --[[
  225. Turns the turtle to face a cardinal direction.
  226. @parameter str a string representing the cardinal direciton to turn the turtle towards
  227. ]]--
  228. function turn.toCardinal(str)
  229.     if string.lower(str) == "north" then
  230.         turn.north()
  231.     elseif string.lower(str) == "south" then
  232.         turn.south()
  233.     elseif string.lower(str) == "east" then
  234.         turn.east()
  235.     elseif string.lower(str) == "west" then
  236.         turn.west()
  237.     end
  238. end
  239.  
  240. --[[
  241. Turns the turtle in a set direction n times
  242. @parameter direction the direction to turn the turtle in, -1:left, 1:right
  243. @optionalParameter count the amount of times to turn the turtle
  244. ]]--
  245. function turn.spin(direction, count)
  246.     local count = (count ~= nil) and count or 1
  247.    
  248.     if direction == -1 or string.lower(direction) == "left" then
  249.         while (count > 0) do
  250.             turtle.turnLeft()
  251.             currentDirection = currentDirection - 1
  252.             count = count - 1
  253.         end
  254.     elseif direction == 1 or string.lower(direction) == "right" then
  255.         while (count > 0) do
  256.             turtle.turnRight()
  257.             currentDirection = currentDirection + 1
  258.             count = count - 1
  259.         end
  260.     end
  261. end
  262.  
  263. function turn.north()
  264.     local n = currentDirection % 4
  265.     if n ~= 3 then
  266.         turn.spin(-1, n)
  267.     else
  268.         turn.spin(1)
  269.     end
  270. end
  271.  
  272. function turn.east()
  273.     local n = (currentDirection % 4) - 1
  274.     if n ~= -1 then
  275.         turn.spin(-1, n)
  276.     else
  277.         turn.spin(1)
  278.     end
  279. end
  280.  
  281. function turn.west()
  282.     local n = currentDirection % 4
  283.     if (n-1) ~= -1 then
  284.         turn.spin(1, math.abs(n-3))
  285.     else
  286.         turn.spin(-1)
  287.     end
  288. end
  289.  
  290. function turn.south()
  291.     local n = currentDirection % 4
  292.     if n ~= 3 then
  293.         turn.spin(1, math.abs(n-2))
  294.     else
  295.         turn.spin(-1)
  296.     end
  297. end
  298.  
  299. BTA.turn = turn
  300. --Turn package end---
  301.  
  302. --[[
  303. Steps through each slot in your turtle's inventory looking for fuel. If fuel is found, the turtle
  304. will fuel up to the limit of fuel available or the configured cap set on it. If no fuel is found
  305. the turtle attempts to go back to the vector, refueling station, given to it as a parameter.
  306. @return true if the turtle is able to refuel and the current fuel level
  307. ]]--
  308. function BTA.refuel()
  309.     local fuel = turtle.getFuelLevel()
  310.     local limit = turtle.getFuelLimit()
  311.     if fuel == "unlimited" then
  312.         return true, turtle.getFuelLevel()
  313.     end
  314.  
  315.     for i=1,16 do
  316.         turtle.select(i)
  317.         if turtle.refuel(0) then
  318.             while turtle.getFuelLevel() < turtle.getFuelLimit() and turtle.getItemCount(i) > 0 do
  319.                 turtle.refuel(1)
  320.             end
  321.         else
  322.             return true, turtle.getFuelLevel()
  323.         end
  324.     end
  325.  
  326.     return false, turtle.getFuelLevel()
  327. end
  328.  
  329. --[[
  330. oves the turtle along the X axis.
  331.     *distance can be found by subtracting your destiation vector from your
  332.     location vector
  333. @parameter distance the amount of blocks to move the turtle
  334. @optionalParameter fn a function to apply at each step
  335. @return true if the turtle is unimpeded
  336. ]]--
  337. function BTA.xMovement(distance, fn)
  338.     if distance < 0 then
  339.         BTA.turn.west()
  340.         if not BTA.move("forward", fn,  math.abs(distance)) then
  341.             return false
  342.         end
  343.     elseif distance > 0 then
  344.         BTA.turn.east()
  345.         if not BTA.move("forward", fn,  math.abs(distance)) then
  346.             return false
  347.         end
  348.     end
  349.     return true
  350. end
  351.  
  352. --[[
  353. Moves the turtle along the Y axis.
  354.     *distance can be found by subtracting your destiation vector from your
  355.     location vector
  356. @parameter distance the amount of blocks to move the turtle
  357. @optionalParameter fn a function to apply at each step
  358. @return true if the turtle is unimpeded
  359. ]]--
  360. function BTA.yMovement(distance, fn)
  361.     if distance < 0 then
  362.         if not BTA.move("down", fn,  math.abs(distance)) then
  363.             return false
  364.         end
  365.     elseif distance > 0 then
  366.         if not BTA.move("up", fn,  math.abs(distance)) then
  367.             return false
  368.         end
  369.     end
  370.     return true
  371. end
  372.  
  373. --[[
  374. Moves the turtle along the Z axis.
  375.     *distance can be found by subtracting your destiation vector from your
  376.     location vector
  377. @parameter distance the amount of blocks to move the turtle
  378. @optionalParameter fn a function to apply at each step
  379. @return true if the turtle is unimpeded
  380. ]]--
  381. function BTA.zMovement(distance, fn)
  382.     if distance < 0 then
  383.         BTA.turn.north()
  384.         if not BTA.move("forward", fn,  math.abs(distance)) then
  385.             return false
  386.         end
  387.     elseif distance > 0 then
  388.         BTA.turn.south()
  389.         if not BTA.move("forward", fn,  math.abs(distance)) then
  390.             return false
  391.         end
  392.     end
  393.     return true
  394. end
  395.  
  396. BTA.calibrate()
  397.  
  398. return BTA
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top