theUpsider

Farmer

Mar 2nd, 2021 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | None | 0 0
  1. -- to come back to original values after one whole loop
  2. local backupX
  3. local backupY
  4.  
  5. local timesx
  6. local timesy
  7. local timesxBackup = 0 -- save value for each row
  8. local timesyBackup = 0
  9. -- if work interrupted by anything, continue on position
  10. local returnPointX = 0
  11. local returnPointY = 0
  12.  
  13. local Totaltimes = 0
  14. local checkpointDigs
  15. local isDown = false
  16.  
  17. -- return to chest and deposit
  18. function Return()
  19.    
  20.     checkpointDigs = 200 -- reset checkpoint counter
  21.     print("Master, im full. Heading back to deposit.")
  22.     returnPointX = timesx
  23.     returnPointY = timesy
  24.     if isDown == false then -- if straight
  25.         print("up")
  26.         turtle.turnLeft()
  27.         if (timesyBackup - timesy) >= 1 then
  28.             for i=0,(timesyBackup - timesy - 1) do
  29.                 turtle.forward()
  30.             end
  31.         end
  32.         turtle.turnLeft()
  33.         local iBackx = (timesxBackup - timesx)
  34.         for i=0,iBackx do
  35.             turtle.forward()
  36.         end
  37.         turtle.turnRight()
  38.         turtle.turnRight()
  39.     else -- if turtle comes down
  40.         -- when not at 0 we need to add 1 step
  41.         local down = timesx
  42.         if not timesx == 0 then
  43.             down = down + 1
  44.         end
  45.         for i=0,down do
  46.             turtle.forward()
  47.         end
  48.         turtle.turnRight()
  49.         local iBacky = (timesyBackup - timesy)
  50.         if iBacky >= 1 then
  51.             for i=0,(timesyBackup - timesy - 1) do
  52.                 turtle.forward()
  53.             end
  54.         else
  55.             for i=0,timesyBackup - 1 do
  56.                 turtle.forward()
  57.             end    
  58.         end
  59.         turtle.turnRight()
  60.     end
  61.         -- back to chest
  62.     --turtle.back()
  63.     turtle.down()
  64.     turtle.down()
  65.     turtle.turnRight()
  66.     turtle.turnRight()
  67.     local invcount = 1
  68.     repeat
  69.         turtle.select(1+invcount)
  70.         turtle.drop()
  71.         invcount = invcount + 1
  72.     until invcount == 16
  73.     turtle.select(1)
  74.     turtle.turnRight()
  75.     turtle.turnRight()
  76.     turtle.up()
  77. end
  78.  
  79. -- continue where it left
  80. function Continue()
  81.     -- used for counting
  82.     local tempX = returnPointX
  83.     local tempY = returnPointY
  84.     print("Going back to work at:",returnPointX, returnPointY)
  85.  
  86.     -- if straight
  87.     if isDown == false then
  88.        for i=0, (timesxBackup - returnPointX) do
  89.         turtle.forward()
  90.       end
  91.  
  92.       turtle.turnRight()
  93.      
  94.         if (timesyBackup - returnPointY) >= 1 then
  95.             for i=0, (timesyBackup - returnPointY) do
  96.                 turtle.forward()
  97.             end
  98.         end
  99.       turtle.turnLeft()
  100.  
  101.  
  102.     -- if turtle comes dow
  103.     else
  104.       turtle.turnRight()
  105.       if (timesyBackup - returnPointY) > 1 then
  106.             for i=0, (timesyBackup - returnPointY - 1) do
  107.                 turtle.forward()
  108.             end
  109.         elseif (timesyBackup - returnPointY) == 1 then
  110.             turtle.forward()
  111.       end
  112.       turtle.turnLeft()
  113.       for i=0, returnPointX do
  114.         turtle.forward()
  115.       end
  116.       turtle.turnRight()
  117.       turtle.turnRight()
  118.     end
  119.     -- restore pos
  120.     timesx = returnPointX
  121.     timesy = returnPointY
  122. end
  123.  
  124. -- place, till and farm
  125. function work(inTransition)
  126.     inTransition = inTransition or false
  127.     -- print("X Y: ", timesx, timesy)
  128.     -- print("Blocks before return: ", checkpointDigs)
  129.     if (checkpointDigs == 0 and inTransition == false)  then
  130.         Return()
  131.         Continue()
  132.         -- print("restored: ", timesx, timesy)
  133.     end
  134.     local success, data = turtle.inspectDown()
  135.     if  data.name == "minecraft:wheat" then -- try to place crop
  136.         turtle.digDown() -- till
  137.         turtle.placeDown() -- place crop
  138.         turtle.suckDown() -- suck it up
  139.     elseif data.name == "minecraft:water" then -- when air just dig
  140.         -- maybe print soemthing?
  141.     else -- if just dirt or sth
  142.         turtle.digDown() -- till
  143.         turtle.placeDown() -- place crop
  144.     end        
  145. end
  146.  
  147. -- do everything
  148. function Start()
  149.     isDown = false
  150.     if timesy == 0 then
  151.         timesy = timesx -- make square
  152.     end
  153.     backupX = timesx
  154.     backupY = timesy
  155.     while true do
  156.         Totaltimes = Totaltimes + 1
  157.         timesx = backupX
  158.         timesy = backupY
  159.         turtle.down() -- try to move down initially
  160.         turtle.up()
  161.         turtle.forward()
  162.         turtle.select(1) -- select crops
  163.         timesxBackup = timesx -- save value for each row
  164.         timesyBackup = timesy
  165.         repeat -- until horizonal line complete          
  166.             repeat -- until straight line complete
  167.                 timesx = timesx -1
  168.                 turtle.forward()  
  169.                 work()          
  170.                 checkpointDigs = checkpointDigs -1 -- count sucks (can only suck 256 times then he might be full: 64x16=1024 / 4 = 256)
  171.             until timesx == 0
  172.             -- turn around <
  173.             if (timesyBackup - timesy)%2 == 0 then -- if EVEN turn right
  174.                 turtle.turnRight()
  175.                 turtle.forward()
  176.                 work(true)
  177.                 turtle.turnRight()
  178.                 isDown = true
  179.             else -- odd turn left
  180.                 turtle.turnLeft()
  181.                 turtle.forward()
  182.                 work(true)
  183.                 turtle.turnLeft()
  184.                 isDown = false
  185.             end
  186.             -- > turn around
  187.             timesx = timesxBackup
  188.             timesy = timesy -1
  189.         until timesy == 0
  190.         Return()
  191.         print("Im done Master. Sleeping for 30 min. Please dont hurt me.")
  192.         print("I already farmed so many times: ", Totaltimes)
  193.         os.sleep(1800)
  194.     end
  195. end
  196.  
  197. print("Please place seeds in the first slot")
  198. print("How long (vertical)?")
  199. local input
  200. input = io.read()
  201. timesx = tonumber(input)
  202. print("How far to the right (horizontal)? 0 for square")
  203. input = io.read()
  204. timesy = tonumber(input)
  205. checkpointDigs = 200
  206. Start()
  207.  
Add Comment
Please, Sign In to add comment