moo3oo3oo3

Simple Mystical Agriculture Farmer (CC:Restitched)

Jun 18th, 2021 (edited)
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. --Setup:
  2. --Place turtle with hoe on top of the
  3. --crop on the bottom left corner
  4. --of a rectangular field.
  5. --Place fuel in the last slot.
  6.  
  7. --Config
  8. local farmWidth = 5
  9. local farmLength = 5
  10. -- Do not edit past this point
  11. -- unless you know what you are
  12. -- doing!
  13.  
  14. local DIRECTIONS =
  15. {
  16.     ["FORWARD"] = 1,
  17.     ["BACKWARD"] = 2,
  18.     ["LEFT"] = 3,
  19.     ["RIGHT"] = 4
  20. }
  21.  
  22. local posX = 1
  23. local posY = 1
  24. local direction = DIRECTIONS.FORWARD
  25.  
  26.  
  27. function refuel()
  28.     local fuelNeeded = farmWidth * farmLength
  29.     if fuelNeeded > turtle.getFuelLimit() then fuelneeded = turtle.getFuelLimit() end;
  30.    
  31.     while turtle.getFuelLevel() < fuelNeeded do
  32.         turtle.select(16)
  33.         local didRefuel = turtle.refuel()
  34.         if not didRefuel then error("Need more fuel in the last slot!") end;
  35.     end
  36. end
  37.  
  38.  
  39. function forward(int)
  40.     for i=1,int do
  41.         turtle.forward()
  42.     end
  43. end
  44.  
  45.  
  46. function checkStatus()
  47.     local wasSuccess, plantInfo = turtle.inspectDown()
  48.    
  49.     if type(plantInfo) == "string" then return false end;
  50.    
  51.     local plantName = plantInfo.name
  52.     local plantAge = plantInfo.state.age
  53.    
  54.     if plantAge == 7 then return plantName end;
  55.    
  56.     return false
  57. end
  58.  
  59.  
  60. function scanInv(itemName)
  61.     for i=1,15 do
  62.         turtle.select(i)
  63.         local item = turtle.getItemDetail()
  64.         if item ~= nil and string.match(item.name, itemName) then
  65.             return i
  66.         end
  67.     end
  68.    
  69.     return false
  70. end
  71.  
  72.  
  73. function harvest(plantName)
  74.     local plantResource = string.match(plantName, ":(%S+)_")
  75.    
  76.     turtle.select(1)
  77.     turtle.digDown()
  78.     local slot = scanInv(":"..plantResource.."_seeds")
  79.    
  80.     if slot then
  81.         turtle.placeDown()
  82.     end
  83. end
  84.  
  85.  
  86. function reset()
  87.     if direction == DIRECTIONS.FORWARD then turtle.turnLeft() end;
  88.     if direction == DIRECTIONS.BACKWARD then turtle.turnRight() end;
  89.     direction = DIRECTIONS.LEFT
  90.    
  91.     forward(farmWidth - 1)
  92.     posY = 1
  93.    
  94.     if posX ~= 1 then
  95.         turtle.turnLeft()
  96.         forward(farmLength - 1)
  97.         direction = DIRECTIONS.BACKWARD
  98.     end
  99.    
  100.     if direction == DIRECTIONS.BACKWARD then
  101.         turtle.turnLeft()
  102.         turtle.turnLeft()
  103.     elseif direction == DIRECTIONS.LEFT then
  104.         turtle.turnRight()
  105.     end
  106.    
  107.     posX = 1
  108.     direction = DIRECTIONS.FORWARD
  109. end
  110.  
  111.  
  112. function main()
  113.     for i=1, farmWidth do
  114.         for j=1, farmLength do
  115.            
  116.             refuel()
  117.            
  118.             local isRipe = checkStatus()
  119.             if isRipe then
  120.                 harvest(isRipe)
  121.             end
  122.            
  123.             if posX < farmLength and direction == DIRECTIONS.FORWARD then
  124.                 turtle.forward()
  125.                 posX = posX + 1
  126.             elseif posX > 1 and direction == DIRECTIONS.BACKWARD then
  127.                 turtle.forward()
  128.                 posX = posX - 1
  129.             end
  130.         end
  131.        
  132.         if posX == farmLength and posY ~= farmWidth then
  133.             turtle.turnRight()
  134.             turtle.forward()
  135.             posY = posY + 1
  136.             turtle.turnRight()
  137.             direction = DIRECTIONS.BACKWARD
  138.         elseif posX == 1 and posY ~= farmWidth then
  139.             turtle.turnLeft()
  140.             turtle.forward()
  141.             posY = posY + 1
  142.             turtle.turnLeft()
  143.             direction = DIRECTIONS.FORWARD
  144.         end
  145.     end
  146.    
  147.     reset()
  148. end
  149.  
  150. while true do
  151.     main()
  152.     os.sleep(60)
  153. end
Add Comment
Please, Sign In to add comment