Vaerys_Dawn

new_farming.lua

Nov 25th, 2020 (edited)
692
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. base = require "turtle_base"
  2.  
  3. base.init(true, {left = base.validTools.pickaxe, right = base.validTools.modem})
  4.  
  5. seeds = {
  6.   {block = "minecraft:wheat", item = "minecraft:wheat_seeds", age = 7},
  7.   {block = "minecraft:beetroots", item = "minecraft:beetroot_seeds", age = 3},
  8.   {block = "minecraft:carrots", item = "minecraft:carrot", age = 7},
  9.   {block = "minecraft:sweet_berry_bush", item = "minecraft:sweet_berries", age = 3},
  10.   {block = "minecraft:potatoes", item = "minecraft:potato", age = 7}
  11. }
  12.  
  13. settings.load("settings")
  14. rows = settings.get("turtleRows", 0)
  15. columns = settings.get("turtleColumns", 0)
  16.  
  17. function initSettings()
  18.   -- init rows
  19.   if rows == 0 then
  20.     showInfo("Requesting Rows")
  21.     term.clear()
  22.     term.setCursorPos(1,1)
  23.     io.write("Rows: ")
  24.     rows = tonumber(io.read())
  25.     settings.set("turtleRows", rows)
  26.   end
  27.  
  28.   -- init columns
  29.   if columns == 0 then
  30.     showInfo("Requesting Columns")
  31.     term.clear()
  32.     term.setCursorPos(1,1)
  33.     io.write("Columns: ")
  34.     columns = tonumber(io.read())
  35.     settings.set("turtleColumns", columns)
  36.   end
  37.  
  38.   settings.save("settings")
  39. end
  40.  
  41. function farm()
  42.   while true do
  43.     returnHome()
  44.     base.refuel()
  45.     doFarming()
  46.     sleep(60)
  47.   end
  48. end
  49.  
  50. function noSpaceLeft()
  51.   showInfo("No Space Left")
  52.   returnHome()
  53.   base.returnToWork()
  54. end
  55.  
  56. function returnHome()
  57.   showInfo("Returning Home")
  58.   base.returnHome()
  59.   dumpInv()
  60. end
  61.  
  62. function dumpInv()
  63.   showInfo("Dumping Inventory")
  64.   base.minifyStacks()
  65.   base.rotateTo(2)
  66.   for k, v in pairs(seeds) do
  67.     base.dumpExtraStacks(v.item)
  68.   end
  69.   for k, v in pairs(base.fuel) do
  70.     base.dumpExtraStacks(v)
  71.   end
  72.   base.rotateTo(0)
  73. end
  74.  
  75. function moveNorth()
  76.   finalLoc = base.convertToLocal(base.home.x, base.home.z).z + rows -1
  77.   while base.convertToLocal().z < finalLoc do
  78.     showInfo("Moving North")
  79.     print("finalLoc:    ".. finalLoc)
  80.     print("current Loc: ".. base.convertToLocal().z)
  81.     if not base.checkSpace() then
  82.       noSpaceLeft()
  83.     end
  84.     handleCrop()
  85.     base.moveForward()
  86.   end
  87. end
  88.  
  89. function moveSouth()
  90.   finalLoc = base.convertToLocal(base.home.x, base.home.z).z
  91.   while base.convertToLocal().z > finalLoc do
  92.     showInfo("Moving South")
  93.     print("finalLoc:    ".. finalLoc)
  94.     print("current Loc: ".. base.convertToLocal().z)
  95.     if not base.checkSpace() then
  96.       noSpaceLeft()
  97.     end
  98.     handleCrop()
  99.     base.moveForward()
  100.   end
  101. end
  102.  
  103. function doFarming()
  104.   for xPos = base.offsetFromHome().x, columns-1, 1 do
  105.     if xPos % 2 == 0 then
  106.       moveNorth()
  107.     else
  108.       moveSouth()
  109.     end
  110.     if not base.checkSpace() then
  111.       returnHome()
  112.     end
  113.     base.refuel()
  114.  
  115.     if x ~= columns-1 then nextColumn(xPos) end
  116.   end
  117. end
  118.  
  119. function nextColumn(xPos)
  120.   showInfo("Moving to next Column")
  121.   if xPos % 2 == 0 then
  122.     base.rotateTo(1)
  123.     handleCrop()
  124.     base.moveForward()
  125.     base.rotateTo(2)
  126.   else
  127.     base.rotateTo(1)
  128.     handleCrop()
  129.     base.moveForward()
  130.     base.rotateTo(0)
  131.   end
  132. end
  133.  
  134. function getSeedBlock(name)
  135.   for k, v in pairs(seeds) do
  136.     if v.block == name then return v end
  137.   end
  138.   return nil
  139. end
  140.  
  141. function handleCrop()
  142.   local success, data = turtle.inspectDown()
  143.   seed = getSeedBlock(data.name)
  144.   if seed and seed.age == data.state.age then
  145.     showInfo("Harvesting ".. seed.item)
  146.     turtle.digDown()
  147.     local foundItem = true
  148.     while foundItem do
  149.       foundItem = turtle.suckDown()
  150.     end
  151.     item = base.findItem(seed.item)
  152.     if #item > 0 then
  153.       turtle.select(item[1])
  154.       turtle.placeDown()
  155.     end
  156.   end
  157. end
  158.  
  159. function showInfo(status)
  160.   term.clear()
  161.   term.setCursorPos(1,1)
  162.   print(base.currentPosition())
  163.   print("status: ", status)
  164.   base.sendStatus(status)
  165. end
  166.  
  167. initSettings()
  168. farm()
  169.  
RAW Paste Data