Advertisement
rhn

FarmBot9x9

rhn
Mar 23rd, 2020
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. local croplist={"Sugarcane",    "Industrial Hemp",  "Wheat",    "Beetroot",     "Netherwart",   "Carrot",   "Potato",   "Botania flowers"}
  2. local croplistTall={true,       true,               false,      false,          false,          false,      false,      false}
  3. local croplistSeed={false,      false,              true,       true,           true,           true,       true,       false}
  4. local croplistTimer={32,        32,                 32,         32,             32,             32,         32,         32}--minutes
  5.  
  6. ---------------------Functions-----------------------------------
  7. --easy move function
  8. local function move(steps)
  9.     for i=1,steps do
  10.         while not turtle.forward() do
  11.             local success,data=turtle.inspect()
  12.             if success and data.name=="minecraft:reeds" then
  13.                 turtle.dig()
  14.             else
  15.                 sleep(1)
  16.             end
  17.         end
  18.     end
  19. end
  20.  
  21. local function unload(seeds)
  22.     local emptyslot=0
  23.     local maxcount=0
  24.     for i=1,15 do
  25.         turtle.select(i)
  26.         itemdata=turtle.getItemDetail()
  27.         if itemdata==nil then
  28.             emptyslot=emptyslot+1
  29.         else
  30.             if itemdata.count>maxcount then
  31.                 maxcount=itemdata.count
  32.             end
  33.         end
  34.     end
  35.     if emptyslot<3 and maxcount>50 then
  36.         for i=1,15 do
  37.             turtle.select(i)
  38.             turtle.dropUp()
  39.         end
  40.         if seeds==false then
  41.             turtle.select(16)
  42.             turtle.dropUp()
  43.         end
  44.         turtle.select(1)
  45.     end
  46. end
  47.  
  48. --Reload fuel
  49. local function refuel()
  50.     while turtle.getFuelLevel() < 120 do
  51.         for i=1,16 do
  52.             turtle.select(i)
  53.             itemdata=turtle.getItemDetail()
  54.             if itemdata==nil then
  55.                 turtle.suckDown(8)
  56.                 if turtle.refuel() then
  57.                     print("Refueled to "..turtle.getFuelLevel())
  58.                     return
  59.                 end
  60.             end
  61.         end
  62.         turtle.select(1)
  63.         turtle.dropUp()
  64.         turtle.suckDown(8)
  65.         if turtle.refuel() then
  66.             print("Refueled to "..turtle.getFuelLevel())
  67.             return
  68.         else
  69.             print("Refuel failed, no fuel!")
  70.             print("Add fuel and hold down X to continue")
  71.             repeat
  72.             event,key= os.pullEvent("key")
  73.             sleep(0.5)
  74.             until key==keys.x
  75.         end
  76.     end
  77. end
  78.  
  79. --Find home location in case of server restart
  80. local function home()
  81.     turtle.up()
  82.     local homecount=0
  83.     while homecount<9 do
  84.         homecount=0
  85.         for i=1,9 do
  86.             if turtle.forward()==true then
  87.                 homecount=homecount+1
  88.                 --print("Homecounter: "..homecount)
  89.             else
  90.                 local success,data=turtle.inspect()
  91.                 if success and data.name=="minecraft:reeds" then
  92.                     turtle.dig()
  93.                     if turtle.forward()==true then
  94.                         homecount=homecount+1
  95.                     end
  96.                 else
  97.                     break
  98.                 end
  99.                
  100.             end
  101.         end
  102.         turtle.turnLeft()
  103.     end
  104.     turtle.turnLeft()
  105. end
  106.  
  107. local function harvest(tall,seed)
  108.     local spiral={8,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1}
  109.     if tall==true then
  110.         turtle.dig()
  111.     end
  112.     move(1)
  113.     if tall==false then
  114.         turtle.down()
  115.     end
  116.     for i=1,#spiral do
  117.         --print(spiral[i])
  118.         for j=1,spiral[i] do
  119.             turtle.digDown()
  120.             if tall==true then
  121.                 turtle.dig()
  122.             end
  123.             if tall==true and seed==true then
  124.                 turtle.down()
  125.                 turtle.select(16)
  126.                 turtle.placeDown()
  127.                 turtle.up()
  128.             elseif seed==true then
  129.                 turtle.select(16)
  130.                 turtle.placeDown()
  131.             end
  132.             move(1)
  133.         end
  134.         turtle.turnRight()
  135.     end
  136.     turtle.digDown()
  137.     if tall==false then
  138.         turtle.up()
  139.     end
  140.     turtle.turnLeft()
  141.     move(4)
  142.     turtle.turnLeft()
  143.     move(4)
  144.     turtle.turnLeft()
  145.     turtle.turnLeft()
  146. end
  147.  
  148. -----------------------------------------
  149.  
  150.  
  151. if (fs.exists("choices") == false) then --Check if this is first time run, no saved choices on file
  152. while true do --Asks for type of crops
  153.         print("Please pick the intended crops:")
  154.         for i=1, #croplist do
  155.             print(i..". "..croplist[i])
  156.         end
  157.         local answer = read()
  158.         if tonumber(answer)==nill then
  159.             print("-Wrong answer, input not a number-")
  160.         else
  161.             local answerv = tonumber(string.format("%." .. 0 .. "f", answer))
  162.             if (0 < answerv and answerv <= #croplist) then
  163.                 pick=answerv
  164.                 --print("Crop ", croplist[answerv], " chosen")
  165.                 break
  166.             else
  167.                 print("-Wrong answer, input does not match range of choices-")
  168.             end
  169.         end
  170. end
  171. tallpick=croplistTall[pick]
  172. seedpick=croplistSeed[pick]
  173. timerpick=croplistTimer[pick]
  174. refuel()
  175.  
  176. --Write choices to file
  177. local file = fs.open("choices","w")
  178. file.writeLine("tallpick="..tostring(tallpick))
  179. file.writeLine("seedpick="..tostring(seedpick))
  180. file.writeLine("timerpick="..tostring(timerpick))
  181. file.close()
  182. print("-------------------------------")
  183. print("     / )")
  184. print("    ' /")
  185. print("---' (_____")
  186. print("       ((__)")
  187. print("    _ ((___)")
  188. print("     -'((__)")
  189. print("--.___((_)")
  190. print("-------------------------------")
  191.  
  192. end
  193.  
  194. os.loadAPI("choices") -- Reads choices from file
  195. local tallpick= choices.tallpick
  196. local seedpick= choices.seedpick
  197. local timerpick=choices.timerpick
  198.  
  199.  
  200. print("Finding home base")
  201. move(1)
  202. home()
  203. unload(seedpick)
  204. refuel()
  205. while true do
  206.     while redstone.getInput("left") do
  207.         print("Disabled by redstone signal (5 min)")
  208.         sleep(300)
  209.     end
  210.     print("Sleeping until crop growth")
  211.     for i=timerpick,1,-1 do
  212.         print(i.." minute(s) to go")
  213.         sleep(60)
  214.     end
  215.     harvest(tallpick,seedpick)
  216.     unload(seedpick)
  217.     refuel()
  218. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement