Maxwelljonez

MJZ MC LUA Turtle Function Module Hare

Aug 9th, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | Gaming | 0 0
  1. --[[Function Module program by Al Sweigart
  2. Provides useful utility functions.
  3. Modified by MaxwellJoneZ]]
  4.  
  5. -- use pastebin get JeWaZUdk hare
  6.  
  7. -- selectItem() selects the inventory
  8. -- slot with the named item, returns
  9. -- true if found and false if not
  10. function selectItem(name)
  11.   -- check all inventory slots
  12.   local item
  13.   for slot = 1, 16 do
  14.     item = turtle.getItemDetail(slot)
  15.     if item ~= nil and item['name'] == name then
  16.       turtle.select(slot)
  17.       return true
  18.     end
  19.   end
  20.  
  21.   return false  -- couldn't find item
  22. end
  23.  
  24.  
  25. -- selectEmptySlot() selects inventory
  26. -- slot that is empty, returns true if
  27. -- found, false if no empty spaces
  28. function selectEmptySlot()
  29.  
  30.   -- loop through all slots
  31.   for slot = 1, 16 do
  32.     if turtle.getItemCount(slot) == 0 then
  33.       turtle.select(slot)
  34.       return true
  35.     end
  36.   end
  37.   return false -- couldn't find empty space
  38. end
  39.  
  40.  
  41. -- countInventory() returns the total
  42. -- number of items in the inventory
  43. function countInventory()
  44.   local total = 0
  45.  
  46.   for slot = 1, 16 do
  47.     total = total + turtle.getItemCount(slot)
  48.   end
  49.   return total
  50. end
  51.  
  52.  
  53. -- selectAndPlaceDown() selects a nonempty slot
  54. -- and places a block from it under the turtle
  55. function selectAndPlaceDown()
  56.  
  57.   for slot = 1, 16 do
  58.     if turtle.getItemCount(slot) > 0 then
  59.       turtle.select(slot)
  60.       turtle.placeDown()
  61.       return
  62.     end
  63.   end
  64. end
  65.  
  66.  
  67. -- buildWall() creates a wall stretching
  68. -- in front of the turtle
  69. function buildWall(length, height)
  70.   if hare.countInventory() < length * height then
  71.     return false  -- not enough blocks
  72.   end
  73.  
  74.   turtle.up()
  75.  
  76.   local movingForward = true
  77.  
  78.   for currentHeight = 1, height do
  79.     for currentLength = 1, length do
  80.       selectAndPlaceDown() -- place the block
  81.       if movingForward and currentLength ~= length then
  82.         turtle.forward()
  83.       elseif not movingForward and currentLength ~= length then
  84.         turtle.back()
  85.       end
  86.     end
  87.     if currentHeight ~= height then
  88.       turtle.up()
  89.     end
  90.     movingForward = not movingForward
  91.   end
  92.  
  93.   -- done building wall; move to end position
  94.   if movingForward then
  95.     -- turtle is near the start position
  96.     for currentLength = 1, length do
  97.       turtle.forward()
  98.     end
  99.   else
  100.     -- turtle is near the end position
  101.     turtle.forward()
  102.   end
  103.  
  104.   -- move down to the ground
  105.   for currentHeight = 1, height do
  106.     turtle.down()
  107.   end
  108.  
  109.   return true
  110. end
  111.  
  112.  
  113. -- buildRoom() constructs four walls
  114. -- and a ceiling
  115. function buildRoom(length, width, height)
  116.   if hare.countInventory() < (((length - 1) * height * 2) + ((width - 1) * height * 2)) then
  117.     return false  -- not enough blocks
  118.   end
  119.  
  120.   -- build the four walls
  121.   buildWall(length - 1, height)
  122.   turtle.turnRight()
  123.  
  124.   buildWall(width - 1, height)
  125.   turtle.turnRight()
  126.  
  127.   buildWall(length - 1, height)
  128.   turtle.turnRight()
  129.  
  130.   buildWall(width - 1, height)
  131.   turtle.turnRight()
  132.  
  133.   return true
  134. end
  135.  
  136.  
  137. -- sweepField() moves across the rows
  138. -- and columns of an area in front and
  139. -- to the right of the turtle, calling
  140. -- the provided sweepFunc at each space
  141. function sweepField(length, width, sweepFunc)
  142.   local turnRightNext = true
  143.  
  144.   for x = 1, width do
  145.     for y = 1, length do
  146.       sweepFunc()
  147.  
  148.       -- don't move forward on the last row
  149.       if y ~= length then
  150.         turtle.forward()
  151.       end
  152.     end
  153.  
  154.     -- don't turn on the last column
  155.     if x ~= width then
  156.       -- turn to the next column
  157.       if turnRightNext then
  158.         turtle.turnRight()
  159.         turtle.forward()
  160.         turtle.turnRight()
  161.       else
  162.         turtle.turnLeft()
  163.         turtle.forward()
  164.         turtle.turnLeft()
  165.       end
  166.  
  167.       turnRightNext = not turnRightNext
  168.     end
  169.   end
  170.  
  171.   -- move back to the start position
  172.   if width % 2 == 0 then
  173.     turtle.turnRight()
  174.   else
  175.     for y = 1, length - 1 do
  176.       turtle.back()
  177.     end
  178.     turtle.turnLeft()
  179.   end
  180.  
  181.   for x = 1, width - 1 do
  182.     turtle.forward()
  183.   end
  184.   turtle.turnRight()
  185.  
  186.   return true
  187. end
  188.  
  189.  
  190. -- buildFloor() builds a rectangular
  191. -- floor out of the blocks in the
  192. -- inventory
  193. function buildFloor(length, width)
  194.   if countInventory() < length * width then
  195.     return false  -- not enough blocks
  196.   end
  197.  
  198.   turtle.up()
  199.   sweepField(length, width, selectAndPlaceDown)
  200. end
  201.  
  202.  
  203. -- findBlock() spins around searching
  204. -- for the named block next to the turtle
  205. function findBlock(name)
  206.   local result, block
  207.  
  208.   for i = 1, 4 do
  209.     result, block = turtle.inspect()
  210.     if block ~= nil and block['name'] == name then
  211.       return true
  212.     end
  213.     turtle.turnRight()
  214.   end
  215.   return false
  216. end
  217.  
  218.  
  219. -- digUntilClear() keeps digging until
  220. -- there are no more blocks (used when
  221. -- sand or gravel can fall into the path)
  222. function digUntilClear()
  223.   while turtle.detect() do
  224.     if not turtle.dig() then
  225.       return false
  226.     end
  227.   end
  228.   return true
  229. end
  230.  
  231. -- digUpUntilClear() keeps digging up until
  232. -- there are no more blocks (used when
  233. -- sand or gravel can fall into the path)
  234. function digUpUntilClear()
  235.   while turtle.detectUp() do
  236.     if not turtle.digUp() then
  237.       return false
  238.     end
  239.   end
  240.   return true
  241. end
  242.  
Add Comment
Please, Sign In to add comment