Advertisement
MigukNamja

Peat Farm

Oct 9th, 2013
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.46 KB | None | 0 0
  1. --
  2. -- Copyright 2013 MigukNamja
  3. --
  4. -- Last Updated : 2013-10-14
  5. --
  6. -- MigukNamja's Peat Farm Turtle Program
  7. -- Requires a digging (diamond pickaxe-equipped) turtle
  8. --
  9. -- The turtle will do the following:
  10. -- 1. Drop off any all items
  11. --    It expects this inventory (ex. chest) on its left side
  12. --    It expects a Bog Earth chest above it
  13. --
  14. -- 2. Re-fill its Bog Earth inventory
  15. --
  16. -- 3. Start digging peat and replacing bog earth starting at the tile directly beneath it.
  17. --    The turtle stays on top of the peat farm and dig/replaces down. It will also dig/remove anything on its
  18. --    plane.
  19. --
  20. -- 4. It will not dig nor replace the water, which should be at positions 3, 8, 13, etc.,.
  21. --    In other words, water can convert Bog Earth up to 2 tiles away. This means you must initially place
  22. --    the water. The first time this program runs, the turtle should leave these spots empty for you
  23. --
  24. -- 5. Refuel with peat before returning and dropping it off
  25. --
  26. -- Recommended configurations are a mulitple of 5 in each dimension, ex:
  27. ---   5x5, 5x10, 10x10, 5x15, 15x15, 20x20, 10x20, etc.,.
  28. --
  29. -- Maximum recommended configuration is 20x20 due to turtle inventory space constraints
  30. --
  31. --
  32.  
  33. -- Globals
  34. gForward = 0 -- Set by program arg
  35. gRight = 0   -- Set by program arg
  36.  
  37. --
  38. -- Function to answer whether turtle should dig here or not.
  39. -- This function calculates where water is or should be and skips it by
  40. -- returning false at locations, (3,3), (3,8), (8,8), (8,3), etc.
  41. --
  42. -- Bog Earth must have water up to 2 blocks away and that water can be diagonal
  43. --
  44. function shouldDigHere( x, y )
  45.   if( ((x - 3) % 5 == 0) and ((y - 3) % 5 == 0) ) then
  46.     return false
  47.   end
  48.   return true
  49. end
  50.  
  51. --
  52. -- Drop-off everything in inventory
  53. -- Preconditions : turtle is already facing the drop-off inventory (chest)
  54. -- This should probably be done before replinishing Bog Earth
  55. --
  56. -- Returns false if it could not drop off
  57. --
  58. function dropoff()
  59.   local slot = 16
  60.   while slot > 0 do
  61.  
  62.     if turtle.getItemCount(slot) > 0 then    
  63.       turtle.select(slot)
  64.  
  65.       -- drop-off Bog Earth in the chest above
  66.       if turtle.compareTo(1) then
  67.         if not turtle.dropUp() then
  68.           print( "Could not place Bog Earth into container above" )
  69.           return false
  70.         end
  71.       elseif not turtle.drop() then
  72.         print( "Could not drop off peat and dirt into drop-off container on left side" )
  73.         return false
  74.       end
  75.     end
  76.  
  77.     slot = slot - 1
  78.   end
  79.  
  80.   return true
  81. end
  82.  
  83. --
  84. -- Check for minimum refuel value. Return true if enough fuel
  85. --
  86. function enoughFuel()
  87.   if turtle.getFuelLevel() >= ((gForward+1) * (gRight+1)) then
  88.     return true
  89.   end
  90.  
  91.   return false
  92. end
  93.  
  94. --
  95. -- Check for minimum refuel value and refuel if necessary
  96. -- Preconditions : turtle is already facing the refuel inventory (chest)
  97. --
  98. -- Returns false if it could not refuel to minimum level
  99. --
  100. function refuel()
  101.   for i=1,16 do
  102.     if enoughFuel() then
  103.       return true
  104.     end
  105.     turtle.select(i)
  106.     turtle.refuel()
  107.   end
  108.  
  109.   if enoughFuel() then
  110.     return true
  111.   end
  112.  
  113.   return false
  114. end
  115.  
  116. --
  117. -- Pick-up Bog Earth items
  118. -- Preconditions : turtle is beneath the Bog Earth inventory (chest)
  119. --
  120. function pickup()
  121.   -- Full rectangle with Bog Earth
  122.   -- except water spots
  123.   -- ...and pickup 1 extra to leave in slot 1
  124.   local peatNeed = (gForward * gRight) - ((gForward / 5) * (gRight / 5)) + 1
  125.   print( "peatNeed is ", peatNeed )
  126.  
  127.   -- Verify we can actually do this, i.e. Bog Earth farm is not too big !
  128.   if peatNeed > (7*64) then -- need 2 free slots (peat+dirt) for every bog earth slot
  129.     print( "[ERROR] Bog Earth farm ", gForward, "x", gRight, " too big !"  )
  130.     print( "Maximum recommended farm size is 20x20" )
  131.     return false
  132.   end
  133.  
  134.   local peatCollected = 0
  135.   local slot = 1
  136.   while peatCollected < peatNeed do
  137.  
  138.     turtle.select(16) -- use last slot to hold and count
  139.     if not turtle.suckUp() then
  140.       print( "[ERROR] Could not pickup Bog Earth." )
  141.       print( "Please make sure there is an inventory" )
  142.       print( "  (chest) above turtle with at least" )
  143.       print( peatNeed, " pieces Bog Earth." )
  144.     end
  145.  
  146.     peatCollected = peatCollected + turtle.getItemCount(16)
  147.     print( "Collected ", peatCollected, " peat so far." )
  148.    
  149.     turtle.transferTo(slot)
  150.     if( turtle.getItemCount(16) > 0 ) then
  151.       assert( turtle.getItemCount(slot) == 64 )
  152.       slot = slot + 1
  153.       turtle.transferTo(slot)
  154.     end
  155.  
  156.     if( turtle.getItemCount(slot) == 64 ) then
  157.       slot = slot + 1
  158.     end
  159.  
  160.   end
  161.  
  162.   print( "Collected ", peatCollected, " peat." )
  163.   return true
  164. end
  165.  
  166.  
  167. --
  168. -- Place a Bog Earth beneath the turtle
  169. gSlot = 1
  170. gTotalPlaced = 0
  171. function placeBogEarth()
  172.      
  173.   turtle.select(gSlot)
  174.   if not turtle.compareDown() then -- don't bother digging and placing if Bog Earth already there
  175.     turtle.digDown()
  176.     turtle.placeDown()
  177.     gTotalPlaced = gTotalPlaced + 1
  178.   end
  179.  
  180.   -- increment to next slot, if applicable
  181.   local count = turtle.getItemCount(gSlot)  
  182.   if ((count == 1 and gSlot == 1) or -- leave at least 1 Bog Earth in slot 1 to use to compare later
  183.       (count == 0)) then -- leave 0 elsewhere to get filled when the turtle next digs
  184.     print( "Placed ", gTotalPlaced, " Bog Earth so far." )
  185.     gSlot = gSlot + 1
  186.   end
  187.  
  188. end
  189.  
  190. --
  191. -- Return to the starting position and orientation
  192. --
  193. function returnToStart()  
  194.   -- odd number of columns will have turtle at the far end (top) of the farm
  195.   -- so, turtle should return to bottom row
  196.   if gRight % 2 == 1 then
  197.     local y = gForward - 1
  198.     turtle.turnLeft()
  199.     turtle.turnLeft()
  200.     assert( moveForward( y ) == y )
  201.   end
  202.  
  203.   local x = gRight - 1
  204.   turtle.turnRight()
  205.   assert( moveForward( x ) == x )
  206.   assert( dropoff() )
  207.   turtle.turnRight()
  208. end
  209.  
  210. -- Move forward 'blocks' number of blocks, digging when necessary
  211. -- Hitting bedrock or something undiggable like an entity (creature) will
  212. --   prevent forward movement
  213. --
  214. -- Return actual blocks moved forward
  215. function moveForward(blocks)
  216.   if blocks < 0 then return moveBack(0-blocks) end
  217.   if blocks == 0 then return 0 end
  218.  
  219.   local moved = 0
  220.  
  221.   while moved < blocks do
  222.     if turtle.forward() then -- air, nothing to dig, but go down
  223.       moved = moved + 1
  224.     elseif turtle.dig() then -- had to dig to go forwards
  225.       if not turtle.forward() then
  226.         local attempts = 0
  227.         while turtle.dig() and attempts < 20 do
  228.           attempts = attempts + 1 -- take care water won't cause infinite digging
  229.           sleep(0.25) -- might have to chew through a stack of sand or gravel
  230.         end
  231.        
  232.         if turtle.forward() then
  233.           moved = moved + 1
  234.         end
  235.       else
  236.         moved = moved + 1
  237.       end
  238.     else -- undiggable, give up (for now)
  239.       return moved
  240.     end
  241.    
  242.     sleep(0.25) -- sleep to avoid yielding error
  243.   end
  244.  
  245.   return moved
  246. end
  247.  
  248. -- Move backwards, digging when necessary
  249. function moveBack(blocks)
  250.   if blocks < 0 then return moveForward(0-blocks) end
  251.   if blocks == 0 then return 0 end
  252.  
  253.   local moved = 0
  254.  
  255.   turtle.turnRight()
  256.   turtle.turnRight()
  257.   moved = moveForward(blocks)
  258.   turtle.turnRight()
  259.   turtle.turnRight()
  260.  
  261.   return moved
  262. end
  263.  
  264. function usage()
  265.   print( "Usage: peat <blocks_forward> <blocks_right>" )
  266.   print( " Both values must be a least 1" )
  267.   print( " Recommended values are a multiple of 5" )
  268. end
  269.  
  270. function parseArgs(args)
  271.   local validSyntax = false
  272.  
  273.   if #args ~= 2 then
  274.     return false
  275.   elseif args[1] == "help" then
  276.     return false
  277.   elseif args[1] == nil or args[2] == nil then
  278.     return false
  279.   else
  280.     gForward = tonumber( args[1] )
  281.     gRight   = tonumber( args[2] )
  282.        
  283.     if gForward <= 0 or gRight <= 0 then
  284.       validSyntax = false
  285.     else
  286.       validSyntax = true
  287.     end
  288.   end
  289.  
  290.   return validSyntax
  291. end
  292.  
  293. local tArgs = { ... }
  294. validSyntax = parseArgs(tArgs)
  295. if not validSyntax then
  296.   usage()
  297.   return
  298. end
  299.  
  300. print( "Making peat farm ", gForward, " blocks forward and ", gRight, " blocks rightwards." )
  301.  
  302. -- Step 1 : Drop off all inventory
  303. -- Step 2 : Replinish Bog Earth
  304. -- Step 3 : Dig up peat and re-plant with fresh Bog Earth
  305. -- Step 4 : Refuel
  306. -- Step 5 : Return to start
  307.  
  308. -- Step 1
  309. turtle.turnLeft()
  310. if not dropoff() then
  311.   turtle.turnRight()
  312.   print( "[ERROR] Could not drop off inventory." )
  313.   print( "Please make sure there is an inventory" )
  314.   print( "  (chest) on the turtle's left side" )
  315.   print( "  with enough free space." )
  316.   error( "" )
  317. end
  318.    
  319. -- Step 2
  320. turtle.turnRight()
  321. if not pickup() then
  322.   error( "" )
  323. end
  324.  
  325. -- Step 3
  326. local x = 1
  327. local y = 1
  328. turtle.select(1)
  329. for x=1,gRight do
  330.   for y=1,gForward do
  331.     if shouldDigHere(x,y) then
  332.       placeBogEarth()
  333.     end
  334.     if y < gForward then
  335.       assert( moveForward(1) == 1 )
  336.     end
  337.   end
  338.  
  339.   if x < gRight then
  340.     if (x % 2) == 1 then -- odd numbered row, turn rightwards and get lined up for next row
  341.       turtle.turnRight()
  342.       assert( moveForward(1) == 1 )
  343.       turtle.turnRight()
  344.     else -- even numbered row, turn leftwards and get lined up for next row
  345.       turtle.turnLeft()
  346.       assert( moveForward(1) == 1 )
  347.       turtle.turnLeft()
  348.     end
  349.   end
  350. end
  351.  
  352. -- Step 4
  353. if not refuel() then
  354.   print( "[ERROR] Could not refuel to minimum" )
  355.   print( " level of ", (gRight+1) * (gForward+1), "." )
  356. end
  357.  
  358. -- Step 5
  359. returnToStart()
  360.  
  361. print( "I have finished the assigned task, my master !" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement