Advertisement
LaniusFNV

CC Hilbert Potato Farm

Jun 8th, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. --
  2. -- This script farms potatoes (other crops if you modify singleHarvest()) in the pattern of
  3. -- a Hilbert curve of the second order.
  4. --
  5. -- It looks like this:
  6. -- x: air with water for the farm undearneath
  7. -- <: the starting position of the turtle (facing into the cobblestone)
  8. -- C: a block of cobblestone as a marker for when the turtle is done with one round
  9. --  : farmblock
  10. --
  11. -- xxxx  xxxx
  12. -- x  x  x  x
  13. -- x  x  x  x
  14. -- x  xxxx  x
  15. -- x        x
  16. -- x        x
  17. -- xxxx  xxxx
  18. --    x  x
  19. --    x  x
  20. --C<xxx  xxxx
  21. --
  22.  
  23. local function isBlock(block)
  24.     local found_block, block_data = turtle.inspect();
  25.     if found_block then
  26.         if block_data["name"] == block then
  27.             return true;
  28.         end
  29.     end
  30.  
  31.     return false;
  32. end
  33.  
  34. local function tableContains(table, needle)
  35.     for key, value in pairs(table) do
  36.         if key == needle then
  37.             return true;
  38.         end
  39.     end
  40.  
  41.     return false;
  42. end
  43.  
  44. local function isCrop()
  45.     local found_block, block_data = turtle.inspect();
  46.     if found_block then
  47.         if tableContains(block_data["tags"], "minecraft:crops") then
  48.             return true;
  49.         end
  50.     end
  51.  
  52.     return false;
  53. end
  54.  
  55. local function isGrown()
  56.     local found_block, block_data = turtle.inspect();
  57.     if found_block then
  58.         if tableContains(block_data["tags"], "minecraft:crops") then
  59.             if block_data["state"]["age"] == 7 then
  60.                 return true;
  61.             end
  62.         end
  63.     end
  64.  
  65.     return false;
  66. end
  67.  
  68. local function itemIndex(item_name)
  69.     for i = 1, 16, 1 do
  70.         local details = turtle.getItemDetail(i)
  71.         if details ~= nil then
  72.             if details["name"] == item_name then
  73.                 return i
  74.             end
  75.         end
  76.     end
  77.  
  78.     return 1 -- FIXME not great, since this is an invalid return
  79. end
  80.  
  81. local function harvestSingle()
  82.     turtle.suck()
  83.     if isGrown() then
  84.         turtle.dig()
  85.         turtle.select(itemIndex("minecraft:potato"))
  86.         turtle.place();
  87.     end
  88. end
  89.  
  90. local function moveRight()
  91.     harvestSingle();
  92.     turtle.turnRight()
  93.     turtle.forward()
  94.     turtle.turnLeft()
  95. end
  96.  
  97. local function forwardTurnLeft()
  98.     turtle.forward()
  99.     turtle.turnLeft()
  100. end
  101.  
  102. local function firstHalfSmallLoop()
  103.     for i = 1, 3, 1 do
  104.         moveRight()
  105.     end
  106.     for i = 1, 2, 1 do
  107.         forwardTurnLeft()
  108.         for j = 1, 2, 1 do
  109.             moveRight()
  110.         end
  111.     end
  112.     turtle.turnRight()
  113. end
  114.  
  115. local function firstHalflongStretchA()
  116.     for i = 1, 6, 1 do
  117.         moveRight()
  118.     end
  119.     turtle.turnRight()
  120.     for i = 1, 3, 1 do
  121.         moveRight()
  122.     end
  123.     turtle.turnRight()
  124. end
  125.  
  126. local function firstHalflongStretchB()
  127.     for i = 1, 3, 1 do
  128.         moveRight()
  129.     end
  130.     turtle.turnRight()
  131.     for i = 1, 6, 1 do
  132.         moveRight()
  133.     end
  134.     turtle.turnRight()
  135. end
  136.  
  137. local function firstHalf()
  138.     firstHalfSmallLoop()
  139.     firstHalflongStretchA()
  140.     firstHalfSmallLoop()
  141.     firstHalflongStretchB()
  142.     firstHalfSmallLoop()
  143. end
  144.  
  145. local function secondHalfShortLoop()
  146.     for i = 1, 2, 1 do
  147.         turtle.turnRight()
  148.         for j = 1, 3, 1 do
  149.             moveRight()
  150.         end
  151.     end
  152. end
  153.  
  154. local function secondHalfLongStretchA()
  155.     forwardTurnLeft()
  156.     for i = 1, 5, 1 do
  157.         moveRight()
  158.     end
  159.     for i = 1, 2, 1 do
  160.         forwardTurnLeft()
  161.         for j = 1, 2, 1 do
  162.             moveRight()
  163.         end
  164.     end
  165. end
  166.  
  167. local function secondHalfLongStretchB()
  168.     forwardTurnLeft()
  169.     for i = 1, 2, 1 do
  170.         moveRight()
  171.     end
  172.     forwardTurnLeft()
  173.     for i = 1, 5, 1 do
  174.         moveRight()
  175.     end
  176.     forwardTurnLeft()
  177.     for i = 1, 2, 1 do
  178.         moveRight()
  179.     end
  180. end
  181.  
  182. local function secondHalf()
  183.     secondHalfShortLoop()
  184.     turtle.turnRight()
  185.     for i = 1, 3, 1 do
  186.         moveRight()
  187.     end
  188.     secondHalfLongStretchA()
  189.     secondHalfShortLoop()
  190.     secondHalfLongStretchB()
  191.     secondHalfShortLoop()
  192. end
  193.  
  194. local function harvest()
  195.     term.setCursorPos(1, 4)
  196.     term.write("Harvesting...")
  197.  
  198.     firstHalf()
  199.     secondHalf()
  200.     turtle.turnRight() -- Turn back to the starting block
  201. end
  202.  
  203. local function printSleep(number)
  204.     local seconds_remaining = number
  205.  
  206.     while seconds_remaining > 0 do
  207.         term.setCursorPos(1, 1)
  208.         term.clearLine()
  209.         term.write("Sleeping for " .. seconds_remaining .. "...")
  210.  
  211.         sleep(5)
  212.         seconds_remaining = seconds_remaining - 5
  213.     end
  214.     term.setCursorPos(1, 2)
  215.     term.write("Done sleeping.")
  216. end
  217.  
  218. local function main()
  219.     while true do
  220.         term.clear()
  221.         if isBlock("minecraft:cobblestone") then
  222.             turtle.turnRight();
  223.             harvest();
  224.         end
  225.         printSleep(60)
  226.     end
  227. end
  228.  
  229. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement