Advertisement
visiongaming43

farmLib

Jun 10th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 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 = turtle.turnLeft else farmDirection = turtle.turnRight end
  35.  
  36. function reorient()
  37. posLib.updateStats()
  38. local turtleX, turtleY, turtleZ = posLib.getCoordinatesFromFile()
  39. print(turtleX .. " " .. turtleY .. " " .. turtleZ)
  40. local turtleHeading = posLib.getHeadingFromFile()
  41. local farmX = tonumber(farmSetupTable[1])
  42. local farmY = tonumber(farmSetupTable[2])
  43. local farmZ = tonumber(farmSetupTable[3])
  44. if (turtleX ~= farmSetupTable[1] or turtleY ~= farmSetupTable[2] or turtleZ ~= farmSetupTable[3]) then
  45. posLib.moveTo(farmX, farmY, farmZ)
  46. end
  47. local farmHeading = farmSetupTable[4]
  48. if (turtleHeading ~= farmHeading) then
  49. posLib.faceHeading(farmHeading)
  50. end
  51. end
  52.  
  53. function harvestBlock()
  54. local isBlock, data = turtle.inspectDown()
  55. if (isBlock == true) then
  56. turtle.digDown()
  57. local seedIndex = findSlot(ACCEPTABLE_SEEDS)
  58. if (seedIndex ~= nil) then
  59. turtle.select(seedIndex)
  60. turtle.placeDown()
  61. end
  62. else
  63. local seedIndex = findSlot(ACCEPTABLE_SEEDS)
  64. turtle.select(seedIndex)
  65. turtle.placeDown()
  66. end
  67. end
  68.  
  69. -- RUNS = MATH.MIN(length, width)*2 - 1
  70. -- TURNS = RUNS - 1
  71. -- 0 -1 -1 -2 -2 -3 -3 ...
  72. -- START AT 0, THE NEXT TWO ARE ONE LESS PERMANENTLY
  73. -- ALWAYS ENDS ON LENGTH IF STARTS BY LENGTH
  74.  
  75. function riceFarm()
  76. local l = length
  77. while (l > 0) do
  78. posLib.forward()
  79. harvestBlock()
  80. l = l - 1
  81. end
  82. local w = width - 1
  83. if (w <= 0) then return end
  84. farmDirection()
  85. while (w > 0) do
  86. posLib.forward()
  87. harvestBlock()
  88. w = w - 1
  89. end
  90. farmDirection()
  91. length = length - 1
  92. width = width - 1
  93. return riceFarm()
  94. end
  95.  
  96. return { reorient = reorient, riceFarm = riceFarm }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement