paint812

CC Farmbot

Jan 3rd, 2022 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.56 KB | None | 0 0
  1. --vars
  2. local Args = {...}
  3. slotSeed = 1
  4. rowCount = 0
  5. colCount = 0
  6. isFlax = false
  7. turtleHigh = false
  8. vegs ={ ["enhancedfarming:tomato_crop"] =6, ["minecraft:carrots"] =7, ["minecraft:wheat"] =7, ["minecraft:potatoes"] =7, ["farmersdelight:cabbages"] =7, ["minecraft:beetroots"] =3, ["farmersdelight:onions"] =7, ["farmersdelight:tomatoes"] =7, ["enhancedfarming:aubergine_crop"] =6, ["enhancedfarming:corn_crop"] =7, ["enhancedfarming:lettuce_crop"] =5, ["enhancedfarming:garlic_crop"] =5, ["enhancedfarming:cucumber_crop"] =5, ["minecraft:pumpkin"] =0, ["minecraft:melon"] =0, ["supplementaries:flax"] = 7}
  9. seds = {["minecraft:wheat_seeds"] = true,["minecraft:pumpkin_seeds"] = true,["minecraft:melon_seeds"] = true,["minecraft:beetroot_seeds"] = true,["farmersdelight:cabbage_seeds"] = true,["farmersdelight:tomato_seeds"] = true,["enhancedfarming:mint_seeds"] = true,["enhancedfarming:tomato_seeds"] = true,["enhancedfarming:cucumber_seeds"] = true,["enhancedfarming:aubergine_seeds"] = true,["enhancedfarming:grape_seeds"] = true,["enhancedfarming:pineapple_seeds"] = true,["enhancedfarming:farm_seeds"] = true,["enhancedfarming:onion_seeds"] = true,["enhancedfarming:garlic_seeds"] = true,["enhancedfarming:lettuce_seeds"] = true,["farmersdelight:onion"] = true,["minecraft:carrot"] = true,["minecraft:potato"] = true,["enhancedfarming:corn_seeds"] = true, ["supplementaries:flax_seeds"] = true}
  10.  
  11. function emptyToChest()
  12.     turtle.back()
  13.     turtle.down()
  14.       for i = 3, 16 do -- loop through the slots
  15.         turtle.select(i) -- change to the slot
  16.         if turtle.refuel(0) == false then -- if it's not valid fuel
  17.             turtle.dropDown()
  18.         end
  19.       end
  20.    
  21.     turtle.up()
  22.     turtle.forward()
  23. end
  24.  
  25.  
  26.  
  27. function isHarvestReady(itm, age)
  28.     if age == nil then
  29.         age = 0
  30.     end
  31.    
  32.     print("1-Found", itm, "looking up harvest age")
  33.  
  34.     print("2-HaAge:", vegs[itm], "Age:", age)
  35.     if vegs[itm] == age then
  36.         print("Harvest")
  37.         return true
  38.     else
  39.         print("dontHarvest")
  40.         return false
  41.     end
  42. end
  43.  
  44. function hasTag(data, tag)
  45.     if data.tags and data.tags[tag] then
  46.         return true
  47.     else
  48.         return false
  49.     end
  50. end
  51.    
  52. function isFence(data)
  53.         if hasTag(data, "forge:fences") or hasTag(data, "forge:fence_gates") then
  54.             return true
  55.         else
  56.             return false
  57.         end
  58. end
  59.  
  60. function goUp()
  61.     if turtleHigh == false then
  62.     turtleHigh = true
  63.     turtle.up()
  64. end
  65.     end
  66. function goDown()
  67.  if turtleHigh == true then
  68.             turtleHigh= false
  69.             turtle.down()
  70. end
  71.     end
  72.  
  73. function row()
  74.     local fenceCount = 0
  75.     rowCount=rowCount+1
  76.     local cc = 0
  77.     for i = 1,64 do
  78.         cc = cc + 1
  79.         local success, d = turtle.inspectDown()
  80.         if success == false and isFlax == true then --air block
  81.         goDown()
  82.         success, d = turtle.inspectDown()
  83.         end  
  84.            
  85.         if success == true then
  86.             local n = d.name
  87.             local ag = d.state.age
  88.             if isFence(d) then
  89.                 print("fence detected")
  90.                 fenceCount = fenceCount + 1
  91.                  if i > 3 then --garden must be 4 wide to prevent false positive
  92.                     break --hit fence, must be end of row
  93.                  end
  94.             end            
  95.             if hasTag(d, "computercraft:turtle_hoe_harvestable") then
  96.                 if isHarvestReady(n, ag) then
  97.                     print("3- Harvest/Plant needed - Crop age:", ag )
  98.                     print("Using Seeds from Slot #3")
  99.                     --turtle.select(3)
  100.                     selectSeeds()
  101.                     turtle.digDown()
  102.                     if isFlax == true then
  103.                     goDown()
  104.                     end
  105.                     turtle.digDown()
  106.                     turtle.placeDown()
  107.                 end
  108.             end
  109.         else --success false
  110.             print("3b -lookup failed, try planting")        
  111.             --turtle.select(3)
  112.             selectSeeds()
  113.           --  if isFlax then
  114.           --      turtle.down()
  115.         --    end
  116.             turtle.digDown()
  117.             turtle.placeDown()        
  118.         end      
  119.         if isFlax then
  120.         goUp()
  121.         end
  122.        
  123.     turtle.forward()
  124.     checkFuel()
  125.     if fenceCount == 3 then --three fences in a row, assume we are at the end
  126.             resetPos()
  127.             reset=1
  128.          return
  129.     end    
  130.     end
  131.     colCount = cc
  132. end
  133.  
  134.  
  135. function resetPos()
  136.     if isFlax == true then
  137.     goDown()    
  138.     end
  139.     print("rows:", rowCount)
  140.     print("cols:", colCount)
  141.    --We need to reset because we are end of farm.
  142.     -- move back 3 - 3 fence reads
  143.  
  144.     rowCount = rowCount - 1
  145.             print("going back")
  146.     if rowCount % 2 == 0 then --on wrong side, we need to go back to the right side.
  147.             turtle.back()
  148.         turtle.back()
  149.          turtle.back()
  150.         turtle.back()
  151.         turtle.turnLeft()
  152.         for i = 1,rowCount do
  153.             turtle.forward()
  154.         end
  155.         turtle.turnRight()
  156.  
  157.     print("should be home")    
  158.     --turtle.forward()
  159.     else
  160.         for i = 1, colCount-4 do
  161.          turtle.forward()  
  162.         end        
  163.         turtle.turnRight()
  164.          for i = 1,rowCount do
  165.             turtle.forward()
  166.         end
  167.         turtle.turnRight()
  168.         print("should be home")
  169.     end
  170. end    
  171. function checkFuel()
  172.   local fuellevel = turtle.getFuelLevel()
  173.       if fuellevel < 3 then
  174.             print("out of fuel - Add fuel and run start")
  175.         os.shutdown()
  176.       end  
  177. end
  178.  
  179. function leftReset()
  180.   turtle.turnLeft()
  181.   turtle.forward()
  182.   turtle.turnLeft()
  183.   --turtle.forward()
  184.  
  185.   checkFuel()
  186.  
  187. end
  188.  
  189. function rightReset()
  190.   turtle.turnRight()
  191.   turtle.forward()
  192.   turtle.turnRight()
  193.   --turtle.forward()
  194.   checkFuel()
  195. end
  196.  
  197. function resetAE()
  198.   turtle.turnRight()
  199.   turtle.forward()
  200.   turtle.forward()
  201.   turtle.forward()
  202.   turtle.turnRight()
  203.   turtle.forward()
  204.   turtle.forward()
  205. end
  206.  
  207.  
  208. function refuel()
  209.   for i = 1, 16 do -- loop through the slots
  210.     turtle.select(i) -- change to the slot
  211.     if turtle.refuel(0) then -- if it's valid fuel
  212.         local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
  213.         turtle.refuel(halfStack) -- consume half the stack as fuel
  214.     end
  215.   end
  216.   local fuellevel = turtle.getFuelLevel()
  217.   if fuellevel < 20 then
  218.     rednet.broadcast(os.getComputerLabel() .. " is low on fuel!","fuelturtle")
  219.   end
  220. end
  221.  
  222. function selectSeeds()
  223.     local ss = slotSeed
  224.     for i = 1, 16 do
  225.         turtle.select(ss)
  226.          local d2 = turtle.getItemDetail(ss)
  227.        
  228.         if d2 then
  229.             --print(d2.name)
  230.             if seds[d2.name] then --Has seed Tag
  231.                 print("Found Seeds, slot:", ss, d2.name)
  232.                 slotSeed = ss
  233.                 return true
  234.             end
  235.         end
  236.         ss = ss + 1
  237.         if ss == 17 then
  238.             ss = 1
  239.         end
  240.     end
  241.     print("No Seeds")
  242.     rednet.broadcast(os.getComputerLabel() .. " is out of seeds!","fuelturtle")
  243.     return false
  244. end
  245.  
  246. --Main Function
  247. if Args[1] == "flax" then
  248. isFlax = true    
  249. end
  250. while true do
  251.  rednet.broadcast(os.getComputerLabel() .. " running","fuelturtle")    
  252. fenceCount = 0
  253. rowCount = 0
  254. colCount = 0
  255. reset=0    
  256. rednet.open("right")
  257. for i=1,64 do
  258.         if isFlax then
  259.             goUp()
  260.         end
  261.     row()
  262.     if reset == 1 then break end
  263.     rightReset()
  264.     refuel()
  265.                 if isFlax then
  266.             goUp()
  267.         end
  268.     row()
  269.     if reset == 1 then break end    
  270.     leftReset()
  271.     refuel()
  272. end
  273.  
  274. --resetAE()
  275. print("Empty Inv")
  276. emptyToChest()
  277. print("sleep 15 mins")
  278. print("Current Fuel:" ,turtle.getFuelLevel())
  279. refuel()    
  280. os.sleep(900)
  281. end
Add Comment
Please, Sign In to add comment