Advertisement
gjheaton

CC Turtle Farmer 2.0

Apr 16th, 2022
1,624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. --[[
  2. #######################################################
  3. # Author:  Fat_Cobra                                  #
  4. # Version: 2.0                                        #
  5. # Date:    4/16/2022                                   #
  6. #######################################################
  7.  
  8.  
  9. USAGE:
  10.     1) Seeds to be planted should be placed in top left slot (1).
  11.     2) Place a single seed in the bottom right slot (16) for identification.
  12. --]]
  13.  
  14. function init ()
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17.    
  18. -- check what the turtle is planting
  19.     local data = turtle.getItemDetail(16)
  20.     if data then
  21.         planting = data.name
  22.     else
  23.         print("-F- I don't know what to plant.")
  24.         error()
  25.     end
  26.    
  27. -- Make sure the turtle has items to plant
  28.     local data = turtle.getItemDetail(1)
  29.     if data and data.name == planting then
  30.     else
  31.         print("-F- I have nothing to plant.")
  32.         error()
  33.     end
  34.  
  35. -- Set up global variables
  36.     direction = 1
  37.     title     = os.getComputerLabel()
  38.     farming   = false
  39.     sTime     = 300
  40.     ageCheck  = 7
  41.    
  42.     if string.match(data.name, "finallyfarmabledyes") then
  43.         ageCheck = 3
  44.     end
  45.    
  46.     if not title then
  47.         title = planting
  48.         os.setComputerLabel(title)
  49.     end
  50.  
  51.     print("Hello,")
  52.     print("My name is '", title, "'.")
  53.     print("I am planting:")
  54.     print("     ", planting)
  55.    
  56. end
  57.  
  58. function rTurn ()
  59.     turtle.turnRight()
  60.     direction = direction + 1
  61.     if direction > 4 then
  62.         direction = 1
  63.     end
  64. end
  65.  
  66. function lTurn ()
  67.     turtle.turnLeft()
  68.     direction = direction - 1
  69.     if direction < 1 then
  70.         direction = 4
  71.     end
  72. end
  73.  
  74. function findHome()
  75.     notHome = true
  76.    
  77.     while notHome do
  78.         if not turtle.forward() then
  79.             local success,data = turtle.inspect()
  80.             if success and string.match(string.lower(data.name), "chest") then
  81.                 notHome = false
  82.             else
  83.                 turtle.turnLeft()
  84.             end
  85.         end
  86.     end
  87.    
  88.     turtle.turnLeft()
  89.     turtle.turnLeft()
  90.     isHome()
  91. end
  92. function isHome ()
  93.     rTurn()
  94.     rTurn()
  95.     local success, data = turtle.inspect()
  96.     if success and string.match(string.lower(data.name), "chest") then
  97.         lTurn()
  98.         lTurn()
  99.         direction = 1
  100.         return true
  101.     else
  102.         findHome()
  103.     end
  104. end
  105.  
  106. function sortInventory ()
  107.     for i=15,2,-1 do
  108.         turtle.select(i)
  109.         if turtle.compareTo(16) then
  110.             turtle.transferTo(1)
  111.         end
  112.     end
  113. end
  114.  
  115. function unloadInventory ()
  116.     if isHome() then
  117.         rTurn()
  118.         rTurn()
  119.         for i=15,2,-1 do
  120.             turtle.select(i)
  121.             turtle.drop()
  122.         end
  123.         rTurn()
  124.         rTurn()
  125.     end
  126. end
  127.  
  128. function goF ()
  129.     if turtle.forward() then
  130.         local success, data = turtle.inspectDown()
  131.         if success then
  132.             if data.state["age"] >= ageCheck then
  133.                 turtle.select(1)
  134.                 turtle.digDown()
  135.                 turtle.placeDown()
  136.             end
  137.         else
  138.             turtle.digDown()
  139.             turtle.placeDown()
  140.         end
  141.         turtle.suckDown()
  142.         return true
  143.     else
  144.         return false
  145.     end
  146. end
  147.  
  148. function farm ()
  149.     if isHome() then
  150.         farming = true
  151.         turtle.select(1)
  152.     else
  153.         error()
  154.     end
  155.     while farming do
  156.         if not goF() then
  157.             if     direction == 1 then
  158.                 rTurn()
  159.                 if goF() then
  160.                     rTurn()
  161.                 else
  162.                     farming = false
  163.                 end
  164.             elseif direction == 3 then
  165.                 lTurn()
  166.                 if goF() then
  167.                     lTurn()
  168.                 else
  169.                     farming = false
  170.                 end
  171.                
  172.             else
  173.                 error()
  174.             end
  175.         end
  176.     end
  177.     lTurn()
  178.     lTurn()
  179.     findHome()    
  180. end
  181.  
  182. --[[
  183.     Main program
  184. ]]--
  185.  
  186. init()
  187. isHome()
  188. sortInventory()
  189. unloadInventory()
  190.  
  191. while true do
  192.     farm()
  193.     sortInventory()
  194.     unloadInventory()
  195.     sleep(sTime)
  196. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement