Matix-Media

ComputerCraft Farming

Mar 20th, 2022 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. shell.run("clear")
  2. print("== MATIX'S FARMING SCRIPT ==\nThanks for using Matix's farming script.\n\nLogs:")
  3.  
  4. Directions = {
  5.     LEFT = 0,
  6.     RIGHT = 1
  7. }
  8.  
  9. -- Checks the fuel and refills the turtle
  10. function CheckFuel()
  11.     if turtle.getFuelLevel() < 2 then
  12.         if turtle.getItemCount(1) > 0 then
  13.             print("INFO: Refilling")
  14.             turtle.select(1)
  15.             turtle.refuel(1)
  16.         else
  17.             print("WARN: OUT OF FUEL! Need fuel refill in first slot.")
  18.             -- loop and wait for refuel
  19.             repeat
  20.                 sleep(1)
  21.             until turtle.getItemCount(1) > 0
  22.         end
  23.     end
  24. end
  25.  
  26. -- Till the block below the turtle
  27. function TillBelow()
  28.     if not turtle.detectDown() then
  29.         turtle.digDown()
  30.     end
  31. end
  32.  
  33. function PickupItems()
  34.     turtle.select(2)
  35.     turtle.suckDown()
  36. end
  37.  
  38. local notifiedAboutEmptyCrops = false
  39. -- Place and farm crops
  40. function PlaceAndFarm()
  41.     BlockDetected, BlockData = turtle.inspectDown()
  42.     if BlockDetected then
  43.         if BlockData.state.age == 7 then
  44.             turtle.select(2)
  45.             turtle.digDown()
  46.             sleep(0.2)
  47.         end
  48.     end
  49.     if not turtle.detectDown() then
  50.         if turtle.getItemCount(2) > 0 then
  51.             notifiedAboutEmptyCrops = false
  52.             turtle.select(2)
  53.             turtle.placeDown()
  54.         else
  55.             if not notifiedAboutEmptyCrops then
  56.                 notifiedAboutEmptyCrops = true
  57.                 print("WARN: NO SEEDS OR CROPS in slot 2 of turtle inventory.")
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. -- Check if block is above turtle. If a block is above the turtle, then it assumes it is a chest.
  64. function PlaceItemsInChest()
  65.     if turtle.detectUp() then
  66.         print("INFO: Placing items in chest")
  67.         for slot = 3, 16 do
  68.             turtle.select(slot)
  69.             turtle.dropUp()
  70.         end
  71.     end
  72. end
  73.  
  74. local lastDirection = Directions.RIGHT
  75. -- Checks the turtles position and moves it
  76. function PerformMovement()
  77.     if turtle.detect() then
  78.         if lastDirection == Directions.RIGHT then
  79.             lastDirection = Directions.LEFT
  80.             turtle.turnLeft()
  81.             if not turtle.detect() then
  82.                 turtle.forward()
  83.                 turtle.turnLeft()
  84.             else
  85.                 turtle.turnLeft()
  86.                 turtle.turnLeft()
  87.                 turtle.forward()
  88.                 turtle.turnRight()
  89.                 lastDirection = Directions.RIGHT
  90.             end
  91.         else
  92.             lastDirection = Directions.RIGHT
  93.             turtle.turnRight()
  94.             if not turtle.detect() then
  95.                 turtle.forward()
  96.                 turtle.turnRight()
  97.             else
  98.                 turtle.turnRight()
  99.                 turtle.turnRight()
  100.                 turtle.forward()
  101.                 turtle.turnLeft()
  102.                 lastDirection = Directions.LEFT
  103.             end
  104.         end
  105.     else
  106.         turtle.forward()
  107.     end
  108. end
  109.  
  110. print("INFO: Starting main loop...")
  111. -- Main loop
  112. repeat
  113.     CheckFuel()
  114.     TillBelow()
  115.     PlaceAndFarm()
  116.     PickupItems()
  117.     PlaceItemsInChest()
  118.     PerformMovement()
  119. until false
  120.  
  121. print("ERROR: Farming script ended natrually. THIS SHOULD NOT HAPPEN!")
Add Comment
Please, Sign In to add comment