le_Fish

Pumpkin farm v1

Feb 6th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.94 KB | None | 0 0
  1. -- Load different API's
  2. if fs.exists("extraFuncsApi") then
  3.     os.loadAPI("extraFuncsApi")
  4. else
  5.     shell.run("pastebin", "get", "5MwHEZxB", "extraFuncsApi")
  6.     os.loadAPI("extraFuncsApi")
  7. end
  8. if fs.exists("serverApi") then
  9.     os.loadAPI("serverApi")
  10. else
  11.     shell.run("pastebin", "get", "dazENzF5", "serverApi")
  12.     os.loadAPI("serverApi")
  13. end
  14. if fs.exists("facilityApi") then
  15.     os.loadAPI("facilityApi")
  16. else
  17.     shell.run("pastebin", "get", "Eyrmtw0m", "facilityApi")
  18.     os.loadAPI("facilityApi")
  19. end
  20.  
  21. local feederUpgrade = peripheral.wrap("left")
  22.  
  23. -- Set data table
  24. local farm_positions = {
  25.     home = vector.new(-68, 321, 50),
  26.     home_dir = "S",
  27.     start_dir = "W",
  28.     sectors = {
  29.             {vector.new(-69, 322, 50 ), vector.new(-96, 323, 50)},
  30.             {vector.new(-69, 327, 50 ), vector.new(-96, 328, 50)},
  31.             {vector.new(-69, 332, 50 ), vector.new(-96, 333, 50)},
  32.             {vector.new(-69, 337, 50 ), vector.new(-96, 338, 50)},
  33.             {vector.new(-69, 342, 50 ), vector.new(-96, 343, 50)},
  34.     }
  35. }
  36.  
  37. local current_task = ""
  38. local current_farm = 1
  39. local harvested = 0
  40.  
  41. function pumpkinFarm()
  42.     turtle.select(2)
  43.     while true do
  44.         for i=current_farm,5 do
  45.             changeTask("Go to sector " .. i)
  46.             goToY(farm_positions.sectors[i][1])
  47.             goToX(farm_positions.sectors[i][1])
  48.             changeTask("Starting sector " .. i)
  49.             runStrip(farm_positions.sectors[i])
  50.             changeTask("done with sector" .. i)
  51.             current_farm = current_farm + 1
  52.         end
  53.         current_farm = 1
  54.         iddle()
  55.     end
  56. end
  57.  
  58.  
  59. function runStrip(place)
  60.     face(farm_positions.start_dir)
  61.     local bad_counter = 0
  62.     farm()
  63.     while this_turtle.pos.x ~= place[2].x do
  64.         if not this_turtle:forward() then
  65.             bad_counter = bad_counter + 1
  66.         else
  67.             printInfo()
  68.         end
  69.         if bad_counter == 10 then
  70.             changeTask("Something went wrong with farming... trying to fix this")
  71.             for i=1,4 do
  72.                 this_turtle:back()
  73.             end
  74.             getGps()
  75.             pumpkinFarm()
  76.         end
  77.         farm()
  78.     end
  79.     this_turtle:left()
  80.     printInfo()
  81.     this_turtle:forward()
  82.     printInfo()
  83.     this_turtle:left()
  84.     printInfo()
  85.     local bad_counter = 0
  86.     farm()
  87.     while this_turtle.pos.x ~= place[1].x do
  88.         if not this_turtle:forward() then
  89.             bad_counter = bad_counter + 1
  90.         else
  91.             printInfo()
  92.         end
  93.         if bad_counter == 10 then
  94.             changeTask("Something went wrong with farming... trying to fix this")
  95.             for i=1,4 do
  96.                 this_turtle:back()
  97.             end
  98.             getGps()
  99.             pumpkinFarm()
  100.         end
  101.         farm()
  102.     end
  103. end
  104.  
  105. function farm()
  106.     changeTask("Farming sector " .. current_farm)
  107.     local success, t = turtle.inspectDown()
  108.     if success then
  109.         if string.find(t.name, "pumpkin") ~= nil and string.find(t.name, "stem") == nil then
  110.                         turtle.digDown()
  111.                         harvested = harvested + 1
  112.         end
  113.         end
  114.         checkInv()
  115. end
  116.  
  117. function checkInv()
  118.     if turtle.getItemCount(16) > 50 then
  119.         changeTask("Going to empty inventory")
  120.         local back_pos = this_turtle.pos
  121.         local direction = this_turtle.dir
  122.         while this_turtle.pos:tostring() ~= farm_positions.home:tostring() do
  123.             getGps()
  124.             goToX(farm_positions.home)
  125.             goToY(farm_positions.home)
  126.         end
  127.         emptyInventory()
  128.         changeTask("Going back to original place before emptying inventory")
  129.         while this_turtle.pos:tostring() ~= back_pos:tostring() do
  130.             getGps()
  131.             goToX(back_pos)
  132.             goToY(back_pos)
  133.         end
  134.         face(direction)
  135.     end
  136. end
  137.  
  138. function emptyInventory()
  139.     changeTask("Empty inventory")
  140.     turtle.select(1)
  141.     if peripheral.getType("right") ~= "workbench" and peripheral.getType("left") ~= "workbench" then
  142.         turtle.equipLeft()
  143.     end
  144.     if peripheral.getType("right") ~= "workbench" and peripheral.getType("left") ~= "workbench" then
  145.         print("NO WORKBENCH FOUND")
  146.         do return end
  147.     end
  148.  
  149.     for i=2,16 do
  150.         if turtle.getItemCount(i) ~= 0 then
  151.             if turtle.getItemDetail(i).name == "minecraft:pumpkin_seeds" then
  152.                 turtle.dropDown()
  153.             elseif turtle.getItemDetail(i).name ~= "minecraft:pumpkin" then
  154.                 face("E")
  155.                 turtle.drop()
  156.             end
  157.         end
  158.     end
  159.     face("N")
  160.     turtle.select(1) turtle.drop()
  161.     for x = 2, 16 do turtle.select(x) turtle.dropUp() end
  162.     turtle.select(2)
  163.     for i = 2, 16 do
  164.         turtle.suckUp()
  165.         while turtle.getItemCount(2) ~= 0 and turtle.getItemDetail(2).name == "minecraft:pumpkin" do
  166.             turtle.craft()
  167.             turtle.select(3) turtle.dropDown()
  168.         end
  169.         turtle.select(2)
  170.     end
  171.     turtle.select(1) turtle.suck()
  172.     if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
  173.         turtle.equipLeft()
  174.     end
  175.     if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
  176.         print("NO MODEM FOUND")
  177.         do return end
  178.     end
  179.     turtle.select(2)
  180. end
  181.  
  182. -- go to pos
  183. function goToY(place)
  184.     local place = place:round()
  185.  
  186.     if this_turtle.pos.y ~= place.y then
  187.             if this_turtle.pos.y < place.y then
  188.                     while this_turtle.dir ~= "S" do
  189.                             this_turtle:left()
  190.                     end
  191.             elseif this_turtle.pos.y > place.y then
  192.                     while this_turtle.dir ~= "N" do
  193.                             this_turtle:right()
  194.                     end
  195.             end
  196.             local bad_counter = 0
  197.             while this_turtle.pos.y ~= place.y do
  198.                     if not this_turtle:forward() then
  199.                         bad_counter = bad_counter + 1
  200.                     else
  201.                         printInfo()
  202.                     end
  203.                     if bad_counter == 10 then
  204.                         changeTask("Something went wrong with farming... trying to fix this")
  205.                         for i=1,4 do
  206.                             this_turtle:back()
  207.                         end
  208.                         getGps()
  209.                         goToY(place)
  210.                     end
  211.             end
  212.     end
  213. end
  214. function goToX(place)
  215.     local place = place:round()
  216.  
  217.     if this_turtle.pos.x ~= place.x then
  218.             if this_turtle.pos.x > place.x then
  219.                     while this_turtle.dir ~= "W" do
  220.                             this_turtle:left()
  221.                     end
  222.             elseif this_turtle.pos.x < place.x then
  223.                     while this_turtle.dir ~= "E" do
  224.                             this_turtle:right()
  225.                     end
  226.             end
  227.             local bad_counter = 0
  228.             while this_turtle.pos.x ~= place.x do
  229.                 if not this_turtle:forward() then
  230.                     bad_counter = bad_counter + 1
  231.                 else
  232.                     printInfo()
  233.                 end
  234.                 if bad_counter == 10 then
  235.                     changeTask("Something went wrong with farming... trying to fix this")
  236.                     for i=1,4 do
  237.                         this_turtle:back()
  238.                     end
  239.                     getGps()
  240.                     goToX(place)
  241.                 end
  242.             end
  243.     end
  244. end
  245. function goToZ(place)
  246.     if this_turtle.pos.z ~= place.z then
  247.             if this_turtle.pos.z > place.z then
  248.                     while this_turtle.pos.z ~= place.z do
  249.                             this_turtle:down()
  250.                     end
  251.             elseif this_turtle.pos.z < place.z then
  252.                     while this_turtle.pos.z ~= place.z do
  253.                             this_turtle:up()
  254.                     end
  255.             end
  256.     end
  257. end
  258. function face(direction)
  259.     while this_turtle.dir ~= direction do
  260.         this_turtle:right()
  261.         printInfo()
  262.     end
  263. end
  264.  
  265. function getGps()
  266.     if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
  267.         turtle.equipLeft()
  268.     end
  269.     if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
  270.         print("NO MODEM FOUND")
  271.         do return end
  272.     else
  273.         this_turtle:get_dir()
  274.         this_turtle:get_gps()
  275.     end
  276.     printInfo()
  277. end
  278.  
  279. -- Init and start function
  280. function start()
  281.     changeTask("Starting")
  282.     turtle.select(1)
  283.     getGps()
  284.    
  285.     if not fs.exists("startup") then
  286.         changeTask("Making startup file")
  287.         local h = fs.open("startup", "w")
  288.         h.writeLine("sleep(1)")
  289.         h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  290.         h.close()
  291.         current_farm = 1
  292.        
  293.         changeTask("Making folder 'pumpkinFolder'")
  294.         fs.makeDir("pumpkinFolder")
  295.         changeTask("Saving counter variables in 'pumpkinFolder/harvested'")
  296.     local h = fs.open("pumpkinFolder/harvested", "w")
  297.     h.writeLine(harvested)
  298.         h.close()
  299.         pumpkinFarm()
  300.     else
  301.         changeTask("Seems I crashed, try to fix this.")
  302.         crashFunction()
  303.         changeTask("Going to farm " .. current_farm)
  304.         pumpkinFarm()
  305.     end
  306. end
  307.  
  308. -- function init()
  309. --  this_facility = facilityApi.facility:new("PumpkinFarm")
  310. --  if this_facility:is_registered() == false then
  311. --          error("Facility doesn't exists, make before running this script")
  312. --  else
  313. --          slave_turtle = serverApi.slave:new("PumpkinFarmSlave", "PumpkinFarm")
  314. --          if this_computer:is_registered() == false then
  315. --                  if this_computer:register() == true then
  316. --                          print("Slave has been registered")
  317. --                          this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
  318. --                          farm_poistions.home = this_turtle:get_gps()
  319. --                          farm_positions.home_dir = this_turtle:get_dir()
  320. --                          print("Init function okay")
  321. --                          sleep(1)
  322. --                  else
  323. --                          error("Couldn't register slave")
  324. --                  end
  325. --          else
  326. --                  error("Turtle seems to already be registered?")
  327. --          end
  328. --  end
  329. -- end
  330.  
  331. function crashFunction()
  332.     getGps()
  333.     checkInv()
  334.     current_farm = 1
  335.     for key,val in pairs(farm_positions.sectors) do
  336.         if this_turtle.pos.y == val[1].y or this_turtle.pos.y == val[2].y then
  337.             current_farm  = key
  338.         end
  339.     end
  340.     changeTask("Reading counter variables")
  341.     local h = fs.open("pumpkinFolder/harvested", "r")
  342.     harvested = tonumber(h.readLine())
  343.     h.close()
  344. end
  345.  
  346. function printInfo()
  347.     clearScreen()
  348.     print("---------- PUMPKIN BOY ---------")
  349.     print("X: " .. this_turtle.pos.x )
  350.     print("Y: " .. this_turtle.pos.y)
  351.     print("Z: " .. this_turtle.pos.z)
  352.     print("Facing: " .. this_turtle.dir)
  353.     print("--------------------------------")
  354.     print(current_task)
  355.     print("--------------------------------")
  356.     print("Home X: " .. farm_positions.home.x)
  357.     print("Home Y: " .. farm_positions.home.y)
  358.     print("Home Z: " .. farm_positions.home.z)
  359.     print("Home direction: " .. farm_positions.home_dir)
  360. end
  361. function clearScreen()
  362.     term.clear()
  363.     term.setCursorPos(1,1)
  364. end
  365. function changeTask(task)
  366.     current_task = task
  367.     printInfo()
  368. end
  369. function printIddleInfo(sleep_timer)
  370.     clearScreen()
  371.     print("Waiting " .. sleep_timer .. " seconds to start farming again")
  372.     print("---------------------------------------")
  373.     print("Pumpkins harvested: " .. harvested)
  374. end
  375. function iddle()
  376.     local timer = 60
  377.     while timer ~= 0 do
  378.         printIddleInfo(timer)
  379.         timer = timer - 1
  380.         sleep(1)
  381.     end
  382. end
  383.  
  384. function saveCounters()
  385.     local h = fs.open("pumpkinFolder/harvested", "w")
  386.     h.writeLine(harvested)
  387.     h.close()
  388. end
  389.  
  390. this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
  391. start()
Advertisement
Add Comment
Please, Sign In to add comment