BrunoZockt

RobustTurtleAPI

Nov 17th, 2020 (edited)
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. ----------------------------------------------
  2. --------------Robust Turtle API---------------
  3. ----------------------------------------------
  4.  
  5. --[[Same as turtle.dig() but guarantees
  6.     that there will be no Block in front
  7.     of the turtle afterwards.
  8. ]]--
  9. function dig()
  10.   success = false
  11.   while turtle.dig() do
  12.     success = true
  13.     os.sleep(0.05)
  14.   end
  15.   return success
  16. end
  17.  
  18. --[[Same as turtle.digUp() but guarantees
  19.     that there will be no Block above the
  20.     turtle afterwards.
  21. ]]--
  22. function digUp()
  23.   success = false
  24.   while turtle.digUp() do
  25.     success = true
  26.     os.sleep(0.05)
  27.   end
  28.   return success
  29. end
  30.  
  31. --[[Same as turtle.digDown().
  32.     Included for user-friendliness
  33. ]]--
  34. function digDown()
  35.   return turtle.digDown()
  36. end
  37.  
  38. --[[Robust version of turtle.forward().
  39.     Refuels if it needs to, removes the
  40.     obstructing block if there is one
  41.     and kills mobs or players that prevent
  42.     it from moving.
  43. ]]--
  44. function _forward()
  45.   while not turtle.forward() do
  46.     if turtle.getFuelLevel() == 0 then
  47.       if not refuel() then
  48.         term.write("Please insert fuel into my inventory.\n Sleeping...")
  49.         os.pullEvent()
  50.       end
  51.     elseif not dig() then
  52.       turtle.attack()
  53.     end
  54.   end
  55. end
  56.  
  57. --[[See _forward().
  58.     @param distance
  59.       Number of Blocks to move.
  60. ]]--
  61. function forward(distance)
  62.   distance = distance or 1
  63.   for i = 1, distance do
  64.     _forward()
  65.   end
  66. end
  67.  
  68. --[[Robust version of turtle.up().
  69.     Refuels if it needs to, removes the
  70.     obstructing block if there is one
  71.     and kills mobs or players that prevent
  72.     it from moving.
  73. ]]--
  74. function _up()
  75.   while not turtle.up() do
  76.     if turtle.getFuelLevel() == 0 then
  77.       refuel()
  78.     elseif not digUp() then
  79.       turtle.attackUp()
  80.     end
  81.   end
  82. end
  83.  
  84. --[[See _up().
  85.     @param distance
  86.       Number of Blocks to move.
  87. ]]--
  88. function up(distance)
  89.   distance = distance or 1
  90.   for i = 1, distance do
  91.     _up()
  92.   end
  93. end
  94.  
  95. --[[Robust version of turtle.down().
  96.     Refuels if it needs to, removes the
  97.     obstructing block if there is one
  98.     and kills mobs or players that prevent
  99.     it from moving.
  100. ]]--
  101. function _down()
  102.   while not turtle.down() do
  103.     if turtle.getFuelLevel() == 0 then
  104.       refuel()
  105.     elseif not digDown() then
  106.       turtle.attackDown()
  107.     end
  108.   end
  109. end
  110.  
  111. --[[See _down().
  112.     @param distance
  113.       Number of Blocks to move.
  114. ]]--
  115. function down(distance)
  116.   distance = distance or 1
  117.   for i = 1, distance do
  118.     _down()
  119.   end
  120. end
  121.  
  122. --[[Robust version of turtle.back().
  123.     Refuels if it needs to, removes the
  124.     obstructing block if there is one
  125.     and kills mobs or players that prevent
  126.     it from moving.
  127. ]]--
  128. function _back()
  129.   if not turtle.back() then
  130.     turnAround()
  131.     forward()
  132.     turnAround()
  133.   end
  134. end
  135.  
  136. --[[See _back().
  137.     @param distance
  138.       Number of Blocks to move.
  139. ]]--
  140. function back(distance)
  141.   distance = distance or 1
  142.   for i = 1, distance do
  143.     _back()
  144.   end
  145. end
  146.  
  147. --[[Type safe version of turtle.getFuelLevel().
  148.     Always returns a number.
  149.     Returns 1,000,000,000 if 'turtlesNeedFuel'
  150.     is set to false in the config.
  151. ]]--
  152. function getFuelLevel()
  153.   local fuelLevel = turtle.getFuelLevel()
  154.   if fuelLevel == "unlimited" then
  155.     return 1000000000
  156.   else
  157.     return fuelLevel
  158.   end
  159. end
  160.  
  161. ----------------------------------------------
  162. -----------Direction track-keeping------------
  163. ----------------------------------------------
  164. local DIRECTIONS = {"north", "east", "south", "west", ["north"] = 1, ["east"] = 2, ["south"] = 3, ["west"] = 4}
  165. local _direction = DIRECTIONS["north"]
  166.  
  167. local function remainder(dividend, divisor)
  168.   return dividend - floor(dividend/divisor+0.5)*divisor
  169. end
  170.  
  171. --[[Equivalent to turtle.turnRight() but
  172.     updates the _direction variable and
  173.     doesn't return success as it would
  174.     always be true anyways.
  175. ]]
  176. function right()
  177.   turtle.turnRight()
  178.   _direction = _direction % 4 + 1
  179. end
  180.  
  181. --[[Analog to right()
  182. ]]
  183. function left()
  184.   turtle.turnLeft()
  185.   _direction = (_direction - 2) % 4 + 1
  186. end
  187.  
  188. --[[Turns the turtle in order to face
  189.     a given direction.
  190. ]]
  191. function face(direction)
  192.   assert(type(direction) == string, "Expected string got "..type(direction))
  193.   direction = DIRECTIONS[direction]
  194.   local action = remainder(direction - _direction, 4)
  195.   if action == 0 then
  196.     return
  197.   elseif action == 1 then
  198.     return right()
  199.   elseif action == -1 then
  200.     return left()
  201.   else
  202.     return turn()
  203.   end
  204. end
  205.  
  206. --[[Sets the direction the turtle is
  207.     currently facing.
  208. ]]
  209. function setDirection(direction)
  210.   assert(type(direction) == string, "Expected string got "..type(direction))
  211.   _direction = DIRECTIONS[direction]
  212. end
  213.  
  214. --[[Gets the direction the turtle is
  215.     currently facing.
  216. ]]
  217. function getDirection()
  218.   return DIRECTIONS[_direction]
  219. end
  220.  
  221. ----------------------------------------------
  222. ---------------QoL Improvements---------------
  223. ----------------------------------------------
  224.  
  225. --[[The turtle turns by 180°.
  226. ]]--
  227. function turnAround()
  228.   left()
  229.   left()
  230. end
  231.  
  232. --[[QoL improvement for turtle.place().
  233.     Tries placing a block from the given slot.
  234. ]]--
  235. function place(slot)
  236.   slot = slot or 1
  237.   turtle.select(slot)
  238.   return turtle.place()
  239. end
  240.  
  241. --[[Analog to place().
  242. ]]--
  243. function placeUp(slot)
  244.   slot = slot or 1
  245.   turtle.select(slot)
  246.   return turtle.placeUp()
  247. end
  248.  
  249. --[[Analog to place().
  250. ]]--
  251. function placeDown(slot)
  252.   slot = slot or 1
  253.   turtle.select(slot)
  254.   return turtle.placeDown()
  255. end
  256.  
  257. --[[Helper function for the drop functions
  258.     to reduce redundancy.]]
  259. local function _drop(slot, dropFunc)
  260.   local selectedSlot = turtle.getSelectedSlot()
  261.   slot = slot or selectedSlot
  262.   turtle.select(slot)
  263.   local success = dropFunc() or turtle.getItemCount() == 0
  264.   turtle.select(selectedSlot)
  265.   return success
  266. end
  267.  
  268. --[[Alternative to turtle.drop().
  269.     Takes a slot to drop items from as
  270.     argument instead of a quantity.
  271.     Will always try to drop the entire stack.
  272.     Only returns false when it can't drop items
  273.     not when there are no items to drop to begin
  274.     with.
  275. ]]--
  276. function drop(slot)
  277.   return _drop(slot, turtle.drop)
  278. end
  279.  
  280. --[[Analog to drop().
  281. ]]--
  282. function dropUp(slot)
  283.   return _drop(slot, turtle.dropUp)
  284. end
  285.  
  286. --[[Analog to drop().
  287. ]]--
  288. function dropDown(slot)
  289.   return _drop(slot, turtle.dropDown)
  290. end
  291.  
  292.  
  293. --[[Automatically chooses a slot to refuel from.
  294. ]]--
  295. function refuel(toFuel)
  296.   for slot = 1, 16 do
  297.     turtle.select(slot)
  298.     if turtle.refuel(toFuel or turtle.getItemCount()) then
  299.       return true
  300.     end
  301.   end
  302.   return false
  303. end
  304.  
Add Comment
Please, Sign In to add comment