Advertisement
Marum

Computercraft Smart Farm

Jan 31st, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. -- Place a turtle on top of a chest with a redstone input comming from the side.
  2. -- On a straight line from that path, place redstone signals that can catch the turtle from below.
  3. -- When the turtle finds a redstone signal, it will turn right and do the following:
  4. -- > Go forward once, and read a single number in the disk file called "cropstage.txt", to know at which metadata to harvest the crop
  5. -- > Go forward and farm until it hits a wall, setting the depth of the farm. Turn right and come back one tile over.
  6. -- > Go back and forth, each time going one more tile to the right, until it hits another wall.
  7. -- > Go back to the point of origin, pointing the oposite way as from the start
  8. -- If nothing was harvested, the turtle will continue forward until it hits another torch.
  9. -- If something was harvested, the turtle will go back home and drop its cargo in the chest.
  10.  
  11. local maxFarmSize = 20
  12. local startInputSide = "left"
  13. local neededFarmInputSide = "bottom"
  14.  
  15. local forward = 0
  16. local returning = false
  17.  
  18. local function farmTile(cropStage)
  19. local success, detail = turtle.inspectDown()
  20. if (success) then
  21. if (detail.metadata == cropStage) then
  22. turtle.digDown()
  23. turtle.suckDown()
  24. if (turtle.getItemCount() > 1) then
  25. turtle.placeDown()
  26. end
  27. return true
  28. else
  29. turtle.suckDown()
  30. end
  31. else
  32. if (turtle.getItemCount() > 1) then
  33. turtle.placeDown()
  34. end
  35. turtle.suckDown()
  36. end
  37. return false
  38. end
  39.  
  40. local function dropCargo()
  41. for i=1, 16 do
  42. turtle.select(i)
  43. turtle.dropDown()
  44. end
  45. turtle.select(1)
  46. end
  47.  
  48. local function farmPass(cropStage)
  49. local depth = 0
  50. local width = 1
  51. local farmedSomething = false
  52. turtle.select(1)
  53. while true do
  54. if (depth == 0) then
  55. while turtle.forward() do
  56. depth = depth+1
  57. farmTile(cropStage)
  58. end
  59. depth = depth-1
  60. else
  61. for i=1, depth do
  62. turtle.forward()
  63. if (farmTile(cropStage)) then
  64. farmedSomething = true
  65. end
  66. end
  67. end
  68. turtle.turnRight()
  69. turtle.forward()
  70. if (farmTile(cropStage)) then
  71. farmedSomething = true
  72. end
  73. turtle.turnRight()
  74. for i=1, depth do
  75. turtle.forward()
  76. if (farmTile(cropStage)) then
  77. farmedSomething = true
  78. end
  79. end
  80. turtle.turnLeft()
  81. if (turtle.forward()) then
  82. if (farmTile(cropStage)) then
  83. farmedSomething = true
  84. end
  85. width = width+2
  86. turtle.turnLeft()
  87. else
  88. turtle.turnRight()
  89. turtle.turnRight()
  90. for i=1, width do
  91. turtle.forward()
  92. end
  93. turtle.turnLeft()
  94. turtle.forward()
  95. return farmedSomething
  96. end
  97. end
  98. end
  99.  
  100. while true do
  101. forward = 0
  102. returning = false
  103.  
  104. -- Wait for redstone signal to start
  105. while not rs.getInput(startInputSide) do
  106. os.pullEvent("redstone")
  107. print("Redstone update")
  108. end
  109.  
  110. -- Go forward until...
  111. repeat
  112. turtle.forward()
  113. forward = forward+1
  114.  
  115. -- ...it gets a redstone signal
  116. if (rs.getInput(neededFarmInputSide)) then
  117. turtle.turnRight()
  118. turtle.forward()
  119. local file = fs.open("disk/cropstage.txt", "r")
  120. local cropStage = file.readLine()
  121. file.close()
  122. returning = farmPass(tonumber(cropStage))
  123. turtle.forward()
  124. turtle.turnRight()
  125. if (returning) then
  126. break
  127. end
  128. end
  129.  
  130. until turtle.detect() -- ...or you hit a wall
  131.  
  132. turtle.up()
  133. for i=1, forward do
  134. turtle.back()
  135. end
  136. turtle.down()
  137.  
  138. dropCargo()
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement