Blazuno

TurtleFarm

Oct 24th, 2021 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.43 KB | None | 0 0
  1. term.clear()
  2. print("Turtle crop farm! (Before starting, ensure you have seeds in the first slot [it'll tell you where else you need to put them for first time autoplanting] and fuel in the last 2 slots before continuing with this program)")
  3. print("Length? (the way the turtle is facing.)")
  4. Length = tonumber(read())
  5. if Length == 0 then
  6.     print("Invalid")
  7.     else do
  8.         print("Width?")
  9.         Width = tonumber(read())
  10.         SeedSlotAmount = Length*Width/64
  11.         SeedSlotAmount = math.ceil(SeedSlotAmount)
  12.         if SeedSlotAmount>14 then
  13.             SeedSlotAmount = 14
  14.         end
  15.         print("Delay between farming sessions?")
  16.         Delay = tonumber(read())
  17.         print("Do you want to have the turtle plant them for the first run as a first time?")
  18.         FirstRun = read()
  19.         if FirstRun == "yes" then
  20.             FirstRun = true
  21.         print("Okay farming time make sure you have seeds in slots 1- "..SeedSlotAmount.." and fuel in the last 2 slots.")
  22.         elseif FirstRun == "no" then
  23.             FirstRun = false
  24.             print("Okay farming time just have fuel in the last 2 slots.")
  25.             else
  26.                 print("Invalid.")
  27.             end
  28.     end
  29. end
  30.  
  31.  
  32. TurtleXPos = 1
  33. TurtleYPos = 1
  34.  
  35.  
  36. turnLeft = false
  37. changeXPos = false
  38.  
  39. function changeReverseXPos()
  40.     if changeXPos == false then
  41.         changeXPos = true
  42.     else
  43.         changeXPos = false
  44.         print(changeXPos)
  45.     end
  46. end
  47.  
  48.  
  49. SeedCount = Length*Width
  50.  
  51. function checkFirst()
  52. local x
  53.     if turtle.getItemCount(1)==0 then
  54.         print("No seeds in first slot!")
  55.         repeat
  56.             sleep(1)
  57.         until turtle.getItemCount(1)>=1
  58.     end
  59. end
  60.  
  61. function checkSeedCount()
  62.     local SlotNum = 1
  63.     local x
  64.     local y = false
  65.     SeedAmount = 0
  66.     for x=1,SeedSlotAmount do
  67.         if turtle.getItemCount(SlotNum)>1 then
  68.                 SeedAmount = turtle.getItemCount(SlotNum)
  69.         elseif turtle.getItemCount(SlotNum)==0 then
  70.             if turtle.getSelectedSlot()<14 then
  71.                 repeat
  72.                     SlotNum=SlotNum+1
  73.                     turtle.select(SlotNum)
  74.                     sleep(0.5)
  75.                 until turtle.getSelectedSlot()==14
  76.             end
  77.             if turtle.getSelectedSlot()==14 then
  78.                 turtle.select(14)
  79.             end
  80.         end
  81.         if turtle.getItemCount(SlotNum)==0 and turtle.getSelectedSlot()==14 then
  82.             SlotNum = 1
  83.             print("Out of seeds")
  84.             repeat
  85.                 sleep(1)
  86.             until turtle.getItemCount(SlotNum)>0
  87.             turtle.select(SlotNum)
  88.         end
  89.     end
  90.     return SeedAmount
  91. end
  92.            
  93.  
  94. function doRefuel()
  95.     local slotNum = 15
  96.     local TurtleXPoss = TurtleXPos
  97.     local TurtleYPoss = TurtleYPos
  98.     local TurtleXPosss = TurtleXPos
  99.     local TurtleYPosss = TurtleYPos
  100.     local g
  101.     local j
  102.     local l
  103.     local h
  104.     local k
  105.     local v
  106.     local SlotNum2 = 15
  107.     if turtle.getFuelLevel()<100 then
  108.         if turtle.getItemCount(15)>0 then
  109.             turtle.select(15)
  110.             turtle.refuel()
  111.             turtle.select(1)
  112.         elseif turtle.getItemCount(15)==0 then
  113.             if turtle.getItemCount(16)>0 then
  114.                 turtle.select(16)
  115.                 turtle.refuel()
  116.                 turtle.select(1)       
  117.             elseif turtle.getItemCount(16)==0 then
  118.                 turtle.select(15)
  119.                  if bit.band(TurtleYPoss, 1) == 0 then
  120.                         turtle.turnRight()
  121.                     else
  122.                         turtle.turnLeft()
  123.                     end
  124.                         for j=1,(TurtleYPosss-1) do
  125.                             turtle.forward()
  126.                         end
  127.                         turtle.turnLeft()
  128.                         for g=1,(TurtleXPosss-1) do
  129.                             turtle.forward()
  130.                         end
  131.                 turtle.up()
  132.                 turtle.select(15)
  133.                 if turtle.suck() == true then
  134.                     turtle.refuel(32)
  135.                     turtle.select(16)
  136.                     turtle.suck()
  137.                     turtle.refuel(32)
  138.                     else
  139.                     turtle.select(16)
  140.                     if turtle.suck() == true and turtle.refuel(32) == true then
  141.                         turtle.refuel(32)
  142.                         else
  143.                         turtle.select(15)
  144.                         print("Turtle is out of fuel. Please input fuel into the chest/turtle.")
  145.                         repeat
  146.                             sleep(1)
  147.                             until turtle.refuel() == true or turtle.suck() == true and turtle.refuel(32)
  148.                         turtle.refuel(32)
  149.                         turtle.select(1)
  150.                     end
  151.                 end
  152.                 turtle.down()
  153.                 turtle.turnRight()
  154.                 turtle.turnRight()
  155.                 for h=1,TurtleXPos-1 do
  156.                     turtle.forward()
  157.                 end
  158.                 turtle.turnRight()
  159.                 for k=1,TurtleYPos-1 do
  160.                     turtle.forward()
  161.                 end
  162.                 if bit.band(TurtleYPoss, 2) == 0 then
  163.                         turtle.turnLeft()
  164.                     else
  165.                         turtle.turnRight()
  166.                     end
  167.             end
  168.         end
  169.     turtle.select(1)
  170.     end
  171. end
  172.            
  173.  
  174. function doDelay()
  175.     for l=1,Delay do
  176.         sleep(60)
  177.     end
  178. end
  179.  
  180. function checkFull()
  181.     local ResourceSlotCount = SeedSlotAmount + 1
  182.     local q
  183.     local j
  184.     local g
  185.     local p
  186.     local l
  187.     local h
  188.     local k
  189.     local SlotNum = 2
  190.     local SlotNum2 = ResourceSlotCount
  191.     local TurtleXPoss = TurtleXPos
  192.     local TurtleYPoss = TurtleYPos
  193.     local TurtleXPosss = TurtleXPos
  194.     local TurtleYPosss = TurtleYPos
  195.     for q=2,14 do
  196.         if SlotNum2>14 then
  197.             SlotNum2 = 14
  198.         end
  199.         if turtle.getItemCount(SlotNum2)==64 then
  200.             repeat
  201.                 SlotNum2=SlotNum2+1
  202.                 local itemCount=turtle.getItemCount(SlotNum2)
  203.                 until itemCount<64 or SlotNum2 == 15
  204.                 turtle.select(1)
  205.                 elseif SlotNum2==15 and turtle.getItemCount==64 then
  206.                     if bit.band(TurtleYPoss, 2) == 0 then
  207.                         turtle.turnRight()
  208.                     else
  209.                         turtle.turnLeft()
  210.                     end
  211.                         for j=1,TurtleYPoss-1 do
  212.                             turtle.forward()
  213.                         end
  214.                         turtle.turnLeft()
  215.                         for g=1,TurtleXPoss-1 do
  216.                             turtle.forward()
  217.                         end
  218.                     for l=ResourceSlotCount,13 do
  219.                         turtle.select(SlotNum)
  220.                         turtle.drop()
  221.                         SlotNum=SlotNum+1
  222.                     end
  223.                 turtle.turnRight()
  224.                 turtle.turnRight()
  225.                 for h=1,TurtleXPosss-1 do
  226.                     turtle.forward()
  227.                 end
  228.                 turtle.turnRight()
  229.                 for k=1,TurtleYPosss-1 do
  230.                     turtle.forward()
  231.                 end
  232.                 if bit.band(TurtleYPoss, 2) == 0 then
  233.                         turtle.turnLeft()
  234.                     else
  235.                         turtle.turnRight()
  236.                     end
  237.                 ResourceSlotCount=SeedSlotAmount+1
  238.                 turtle.select(1)
  239.             end
  240.        end
  241. end
  242.                
  243.  
  244. function goHome()
  245. local TurtleXPoss = TurtleXPos
  246. local TurtleYPoss = TurtleYPos
  247. local TurtleXPosss = TurtleXPos
  248. local TurtleYPosss = TurtleYPos
  249. local q
  250. local p
  251. local h
  252. local SlotNum = SeedSlotAmount+1
  253. local b
  254. local SlotNum2 = 2
  255.       if bit.band(TurtleYPoss, 1) == 0 then
  256.                         turtle.turnRight()
  257.                     else
  258.                         turtle.turnLeft()
  259.                     end
  260.         for q=1,TurtleYPosss-1 do
  261.             turtle.forward()
  262.         end
  263.         turtle.turnLeft()
  264.         for p=1,TurtleXPosss-1 do
  265.             turtle.forward()
  266.         end
  267.         if FirstRun==true then
  268.             for h=SeedSlotAmount,14 do
  269.                 turtle.select(SlotNum)
  270.                 turtle.drop(64)
  271.                 SlotNum=SlotNum+1
  272.                 if SlotNum==15 then
  273.                     SlotNum=1
  274.                 end
  275.             end
  276.         else
  277.             for b=2,14 do
  278.                 turtle.select(SlotNum2)
  279.                 turtle.drop(64)
  280.                 SlotNum2=SlotNum2+1
  281.                 if SlotNum2==15 then
  282.                     SlotNum2=1
  283.                 end
  284.             end
  285.         end
  286.     turtle.select(15)
  287.     turtle.up()
  288.     turtle.suck(64)
  289.     turtle.refuel()
  290.     turtle.select(16)
  291.     turtle.suck(64)
  292.     turtle.refuel()
  293.     turtle.select(1)
  294.     turtle.down()
  295.     turtle.turnRight()
  296.     turtle.turnRight()
  297. end
  298.        
  299.    
  300.            
  301.        
  302.  
  303. function lengthSpan()
  304.     local p
  305.     for LengthLoop = 1,Length-1 do
  306.         if FirstRun == false then
  307.             checkFull()
  308.         else
  309.             checkSeedCount()
  310.         end
  311.         doRefuel()
  312.         turtle.digDown()
  313.         turtle.placeDown()
  314.         turtle.forward()
  315.         if changeXPos == false then
  316.             TurtleXPos = TurtleXPos+1
  317.             print("Debug: TurtleXPos: "..TurtleXPos)
  318.             elseif changeXPos == true then
  319.                 TurtleXPos = TurtleXPos-1
  320.                 print("Debug: TurtleXPos: "..TurtleXPos)
  321.         end
  322.     end
  323. end
  324.  
  325. function nextRow()
  326.     if turnLeft == false then
  327.         turtle.turnRight()
  328.         turtle.digDown()
  329.         turtle.placeDown()
  330.         turtle.forward()
  331.         turtle.digDown()
  332.         turtle.placeDown()
  333.         turtle.turnRight()
  334.         turnLeft = true
  335.         TurtleYPos = TurtleYPos + 1
  336.         print(TurtleYPos)
  337.         elseif turnLeft == true then
  338.         turtle.turnLeft()
  339.         turtle.digDown()
  340.         turtle.placeDown()
  341.         turtle.forward()
  342.         turtle.digDown()
  343.         turtle.placeDown()
  344.         turtle.turnLeft()
  345.         turnLeft = false
  346.         TurtleYPos = TurtleYPos+1
  347.         print(TurtleYPos)
  348.     end
  349. end
  350.  
  351.    
  352.  
  353.  
  354.  
  355. function harvestLand()
  356. while true do
  357.     local q
  358.     local l
  359.     local slotNum=1
  360.         checkFirst()
  361.         for q=1,Width do
  362.             lengthSpan()
  363.             nextRow()
  364.             changeReverseXPos()
  365.         end
  366.     goHome()
  367.     doDelay()
  368.     if FirstRun==true then
  369.         FirstRun=false
  370.     end
  371. end
  372. end
  373. while true do
  374.     harvestLand()
  375. end
  376.  
Add Comment
Please, Sign In to add comment