Advertisement
visiongaming43

farmLib

Jun 10th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. if (not fs.exists("posLib")) then shell.run("pastebin get https://pastebin.com/1DQsNF8k posLib") end
  2. if (not fs.exists("invLib")) then shell.run("pastebin get https://pastebin.com/019TAz4k invLib") end
  3. if (not fs.exists("FARM_STATS")) then
  4. shell.run("pastebin get https://pastebin.com/CRsg1kwR farmSetup")
  5. error("PLEASE RUN FARM SETUP!")
  6. end
  7.  
  8. -- List of keywords that the turtle will use to figure out what to break & what to plant
  9. local ACCEPTABLE_SEEDS = {
  10. "croptopia:",
  11. "minecraft:carrot",
  12. "minecraft:potato",
  13. "minecraft:wheat",
  14. "minecraft:beetroot",
  15. "minecraft:nether_wart"
  16. }
  17.  
  18. local posLib = require("posLib")
  19. local invLib = require("invLib")
  20.  
  21. -- farmSetupTable refers to the (organized) listing of data that the turtle will use to figure out its base conditions before starting
  22. -- startX, startY, startZ, startHeading, length, width, direction)
  23. local farmStats = fs.open("FARM_STATS", "r")
  24. local farmSetupTable = {}
  25. local index = 1
  26. for line in io.lines("FARM_STATS") do
  27. farmSetupTable[index] = line
  28. index = index + 1
  29. end
  30. local length = tonumber(farmSetupTable[5])
  31. local width = tonumber(farmSetupTable[6])
  32.  
  33. local farmDirection
  34. if (farmSetupTable[7] == "LEFT") then farmDirection = posLib.turnLeft else farmDirection = posLib.turnRight end
  35.  
  36. function reorient()
  37. posLib.updateStats()
  38. local turtleX, turtleY, turtleZ = posLib.getCoordinates()
  39. print(turtleX .. " " .. turtleY .. " " .. turtleZ)
  40. local farmX = tonumber(farmSetupTable[1])
  41. local farmY = tonumber(farmSetupTable[2])
  42. local farmZ = tonumber(farmSetupTable[3])
  43. if (turtleX ~= farmSetupTable[1] or turtleY ~= farmSetupTable[2] or turtleZ ~= farmSetupTable[3]) then
  44. posLib.moveTo(farmX, farmY, farmZ)
  45. end
  46. local farmHeading = tonumber(farmSetupTable[4])
  47. posLib.faceHeading(farmHeading)
  48. end
  49.  
  50. function harvestBlock()
  51. local isBlock, data = turtle.inspectDown()
  52. if (isBlock == true) then
  53. turtle.digDown()
  54. local seedIndex = findSlot(ACCEPTABLE_SEEDS)
  55. if (seedIndex ~= nil) then
  56. turtle.select(seedIndex)
  57. turtle.placeDown()
  58. end
  59. else
  60. local seedIndex = findSlot(ACCEPTABLE_SEEDS)
  61. turtle.select(seedIndex)
  62. turtle.placeDown()
  63. end
  64. end
  65.  
  66. -- RUNS = MATH.MIN(length, width)*2 - 1
  67. -- TURNS = RUNS - 1
  68. -- 0 -1 -1 -2 -2 -3 -3 ...
  69. -- START AT 0, THE NEXT TWO ARE ONE LESS PERMANENTLY
  70. -- ALWAYS ENDS ON LENGTH IF STARTS BY LENGTH
  71.  
  72. function riceFarm()
  73. local l = length
  74. while (l > 0) do
  75. posLib.forward()
  76. harvestBlock()
  77. l = l - 1
  78. end
  79. local w = width - 1
  80. if (w <= 0) then return end
  81. farmDirection()
  82. while (w > 0) do
  83. posLib.forward()
  84. harvestBlock()
  85. w = w - 1
  86. end
  87. farmDirection()
  88. length = length - 1
  89. width = width - 1
  90. return riceFarm()
  91. end
  92.  
  93. return { reorient = reorient, riceFarm = riceFarm }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement