BruceWplays

Farm old V9.0

Sep 13th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function startfarm(funfarmargs)
  2. version = "5"
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. --print(version)
  6. done = 0
  7.  
  8. --for l=1,10 do
  9. --if fs.exists("farmapi/gt") == false then do
  10.     --print("downloading goto api")
  11.     --shell.run("mkdir","farmapi")
  12.     --shell.run("cd","farmapi")
  13.     --shell.run("pastebin","get","a3GBewNp","gt")
  14.     --shell.run("cd","..")
  15.         --if fs.exists("farmapi/gt") == true then
  16.             --print("download complete")
  17.                 --elseif fs.exists("farmapi/gt") == false then
  18.                     --print("download failed")
  19.                 --end
  20. --end
  21.  
  22.  
  23.  
  24. --else do
  25.            
  26.   term.clear()
  27.   term.setCursorPos(1, 1)                          
  28.  
  29.  os.loadAPI("AndysPrograms/api/gt")  
  30.  
  31.  
  32.  
  33.  
  34.  if fs.exists("AndysPrograms/Farm/settings") then
  35.     settings.load("AndysPrograms/Farm/settings")
  36. end
  37. local maxW, maxH = term.getSize()
  38.  
  39.  
  40.  
  41. function exit()
  42.     term.clear()
  43.     term.setCursorPos(1, 1)
  44.     print("Starting")
  45.     openset = 0
  46. end
  47. function chngset()
  48.     chngsel = 1
  49. end
  50.  
  51. function savesets()
  52.     settings.save("AndysPrograms/Farm/settings")
  53. end
  54. gpstab = {}
  55. local stgpsx, stgpsy, stgpsz = gps.locate(5)
  56. table.insert(gpstab, stgpsx)
  57. table.insert(gpstab, stgpsy)
  58. table.insert(gpstab, stgpsz)
  59. table.insert(gpstab, "n")
  60. if fs.exists("AndysPrograms/Farm/settings") == false then
  61.     if fs.exists("AndysPrograms/Farm") == false then
  62.       fs.makeDir("AndysPrograms/Farm")
  63.     end
  64.  
  65.  
  66.  
  67.  
  68.     srtbk = {"minecraft:pumpkin", "minecraft:reeds"}
  69.     settings.set("Farm Lenth", 1)
  70.     settings.set("Farm Width", 1)
  71.     settings.set("Mode", 2)
  72.     settings.set("Start Location", gpstab)
  73.     settings.set("Chest Location", gpstab)
  74.     settings.set("Sort Block Names", srtbk)
  75.     settings.set("Sort Blocks", 1)
  76.     settings.set("Chest Direction", 0)
  77.     savesets()
  78.  
  79.    
  80.    
  81.   end
  82. function resetmenu()
  83. deftab = {}
  84. setmenu = {}
  85. setmenu = {
  86.     {text = "Finish editing:", options = "Finish", handler = exit},
  87.     {text = "Farm Lenth", setname = "Farm Lenth", options = settings.get("Farm Lenth"), type = "num", handler = chngset},
  88.     {text = "Farm Width", setname = "Farm Width", options = settings.get("Farm Width"), type = "num", handler = chngset},
  89.     {text = "Mode, 1 = Nrml, 2 = Pump/Mel/SgrCne", setname = "Mode", options = settings.get("Mode"), type = "num", handler = chngset},
  90.     {text = "Start Location", setname = "Start Location", options = settings.get("Start Location"), type = deftab, handler = chngset},
  91.     {text = "Chest Location", setname = "Chest Location", options = settings.get("Chest Location"), type = deftab, handler = chngset},
  92.     {text = "Chest Direction 0=fwd 2=up 1=dn", setname = "Chest Direction", options = settings.get("Chest Direction"), type = "num", handler = chngset},
  93.     {text = "Sort Blocks", setname = "Sort Blocks", options = settings.get("Sort Blocks"), type = "num", handler = chngset},
  94.     {text = "Sort Block Names", setname = "Sort Block Names", options = settings.get("Sort Block Names"), type = deftab, handler = chngset}
  95. }
  96.  
  97. end
  98. resetmenu()
  99.  
  100. function addsettings(num, value)
  101.     settings.set(setmenu[num].setname, value)
  102.     savesets()
  103. end
  104.  
  105. openset = 1
  106.  
  107. farmargs = funfarmargs
  108. if farmargs[1] == "noset" then
  109.     openset = 0
  110. end
  111.  
  112. while openset == 1 do
  113.  
  114.  
  115.  
  116.  
  117. if openset == 1 then
  118.     chngsel = 0
  119.     term.clear()
  120.     setsoffset = 1
  121.     selitem = 1
  122.     --if (maxH) < (#setmenu * 2) then
  123.     --    selitem = maxH / 2
  124.    -- elseif (maxH) > (#setmenu) then
  125.        --selitem = 1
  126.     --end
  127. function printmenu(menu)
  128.     for sets=1,#setmenu do
  129.        
  130.         setsmul = (sets * 2) - 1
  131.         if (setsmul + setsoffset) <= maxH then
  132.             term.setCursorPos(1, setsmul + setsoffset)
  133.             print(setmenu[sets].text)
  134.             if #setmenu[sets].text / maxW > 1 then
  135.                 --setsoffset = setsoffset + #setmenu[sets].text / maxW
  136.                 --setsoffset = setsoffset - 0.9
  137.             end
  138.             term.setCursorPos(1, (setsmul + 1) + setsoffset)
  139.             if setmenu[sets].options ~= nil then
  140.                 term.clearLine()
  141.                 prntset = setmenu[sets].options
  142.                 if (type(prntset) == "table") then
  143.                     prntset = table.concat(setmenu[sets].options, ", ")
  144.                 end
  145.                 if selitem == sets then
  146.                     if chngsel == 1 then
  147.                         input = read()
  148.                         if input then
  149.                             inptesset = setmenu[sets].type
  150.                             if type(inptesset) == "table" then
  151.                                 settingsinputtable = {}
  152.                                 input2 = string.gsub(input, ", ", ",")
  153.                                 --print(input)
  154.                                 for i in string.gmatch(input2, '([^,]+)') do
  155.                                     table.insert(settingsinputtable, i)
  156.                                 end
  157.                                 --print(input)
  158.                                 addsettings(selitem, settingsinputtable)
  159.                             end
  160.                             if type(inptesset) ~= "table" then
  161.                                 addsettings(selitem, input)
  162.                             end
  163.                             resetmenu()
  164.                             --print(chngsel)
  165.                             chngsel = 0
  166.                         end
  167.  
  168.                     else
  169.                         print(">  "..prntset)
  170.                     end
  171.                 else
  172.                     print("   "..prntset)
  173.                 end
  174.             end
  175.         end
  176.     end
  177. end
  178.     function onsel(menu, selected)
  179.         menu[selected].handler()
  180.     end
  181.  
  182.     function onkeypress(key, menu, selected)
  183.         if key == keys.enter or key == keys.space then
  184.             if chngsel == 0 then
  185.                 onsel(menu, selected)
  186.             end
  187.            -- if chngsel == 1 and (key == keys.enter or key == keys.space) then
  188.              --   chngsel = 0
  189.            -- end
  190.         elseif key == keys.up or key == keys.w then
  191.             if selitem > 1 then
  192.                 setsoffset = setsoffset + 2.1
  193.                 setsoffset = math.floor(setsoffset)
  194.                 selitem = selitem - 1
  195.             end
  196.         elseif key == keys.down or key == keys.s then
  197.             if selitem < (#menu) then
  198.                 setsoffset = setsoffset - 1.9
  199.                 setsoffset = math.floor(setsoffset)
  200.                 selitem = selitem + 1
  201.             end
  202.         elseif key == keys.q then
  203.             exit()
  204.         end
  205.     end
  206.  
  207. while openset == 1 do
  208.     term.setCursorPos(1, 1)
  209.     term.clear()
  210.     printmenu(setmenu)
  211.     event, key = os.pullEvent("key")
  212.     onkeypress(key, setmenu, selitem)
  213. end
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. end
  224.  
  225.  
  226.  
  227. end
  228. farmexit = 0
  229. while farmexit ~= 1 do
  230.  
  231.  
  232.  
  233.                        
  234. ---------------------                        
  235. ---------------------                        
  236. ---------------------
  237. --CHANGE HERE START--
  238. ---------------------
  239. ---------------------                        
  240. ---------------------                        
  241.    
  242.  
  243.  
  244.  
  245.  
  246.                        
  247. --"1" is Normal, "2" is Pumpkin/Melon/Probably Sugar Cane                    
  248. Mode = tonumber(settings.get(setmenu[4].setname))
  249.  
  250. if Mode > 2 or Mode < 1 then
  251.     Mode = 2
  252. end
  253.  
  254.  
  255. Farm_Lenth = tonumber(settings.get(setmenu[2].setname))
  256. Farm_Width = tonumber(settings.get(setmenu[3].setname))
  257.  
  258.  
  259.  
  260.                      
  261. -- sort for specific crop                        
  262.  
  263. --whether or not to sort, 1 is yes, 0 is no                        
  264. sortblock = tonumber(settings.get(setmenu[8].setname))
  265. if sortblock > 1 or sortblock < 0 then
  266.     sortblock = 1
  267. end
  268.  
  269. --name of the crop                        
  270. crop_name = settings.get(setmenu[9].setname)                                        
  271.                        
  272. --max age of crop should probably be left at 7
  273. crop_max_age = 7                      
  274.                        
  275.                        
  276.                        
  277.                        
  278. --setup location
  279.     setuploc = settings.get(setmenu[5].setname)
  280.            
  281.                        
  282.                    
  283. --chest location                      
  284.     chestloc = settings.get(setmenu[6].setname)
  285.    
  286.  
  287.  
  288. --chest 2 = "up", 1 = "down", or 0 = "forward"                        
  289. chestdirection = tonumber(settings.get(setmenu[7].setname))
  290.      
  291. if chestdirection > 2 or chestdirection < 0 then
  292.     chestdirection = 2
  293. end
  294.     --misc
  295.                        
  296.                       --ignore if a crop has no age value should be set at 1 for Pumpkin/Melon and 0 for Normal
  297.                         Override_ignore_nil_age = 0
  298.                         ignore_nil_age = 1  
  299.                        
  300.                         override_stages = 0
  301.                         turnleftchest = 0
  302.                         totalstages = 1
  303.                        
  304.                        
  305. -------------------                        
  306. -------------------
  307. --CHANGE HERE END--
  308. -------------------        
  309. -------------------
  310.                        
  311.                        
  312.        
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342. --unused!!!! Recenter Values
  343. turn_right_recenter_times = 0
  344. forward_recenter_times = 0  
  345.  
  346. if Override_ignore_nil_age == 0 then
  347.    if Mode == 2 then                          
  348.          ignore_nil_age = 1
  349.    elseif Mode == 1 then                            
  350.          ignore_nil_age = 0                        
  351.    end                          
  352. end                    
  353.                        
  354. st_width = Farm_Width
  355. lenth = Farm_Lenth                      
  356. if override_stages == 1 then
  357. totalsteps = totalstages
  358.      if Mode == 2 then
  359.        grabstage = 5
  360.        elseif Mode == 1 then
  361.        grabstage = 2                        
  362.      end                              
  363. elseif Mode == 2 then
  364.    if override_stages == 0 then                        
  365.          grabstage = 5                  
  366.        totalstages = 1                    
  367.  totalsteps = 1  
  368.      end                                                      
  369.  elseif  Mode == 1 then
  370.    if override_stages == 0 then                        
  371.        grabstage = 2                    
  372.        totalstages = 2                    
  373.  totalsteps = 2  
  374.      end                          
  375.   end    
  376.  
  377. term.clear()
  378. term.setCursorPos(1, 1)
  379. print("Recentering")              
  380. totalsteps = totalstages                                              
  381. endsteps = totalsteps + 1                    
  382. sub_lenth = lenth - 1                      
  383. function chest()                        
  384.     term.clear()
  385.            term.setCursorPos(1, 1)                                
  386.           print("going to chest")                        
  387.           gt.goto(chestloc)            
  388.        if Mode == 2 then
  389.         chest = 1
  390.          elseif  Mode == 1 then                        
  391.        chest = 2
  392.                                        end
  393.            term.clear()
  394.            term.setCursorPos(1, 1)                          
  395.        print("putting in chest")
  396.        
  397.          
  398.        while chest <= 16 do
  399.        turtle.select(chest)
  400.      if chestdirection == 0 then  
  401.          turtle.drop()                              
  402.                                      
  403.            elseif chestdirection == 1 then
  404.                        
  405.        turtle.dropDown()
  406.        elseif chestdirection == 2 then                          
  407.          turtle.dropUp()
  408.                end                        
  409.                                        
  410.                                        
  411.        chest = chest + 1
  412.      end
  413.          if turnleftchest == 1 then
  414.              turtle.turnLeft()
  415.                                    end
  416.         term.clear()
  417.            term.setCursorPos(1, 1)                                
  418.           print("Resetting")                        
  419.       sleep(1)
  420.       farmexit = 1                    
  421.     end                    
  422.     --chest()                          
  423.                                
  424.                                
  425. function turn()
  426.  
  427.  
  428. if width >= 2 then                                  
  429.  
  430.   if right == 1 then
  431.  
  432.     turtle.turnRight()
  433.     turtle.forward()
  434.     turtle.turnRight()
  435.  
  436.     dist = sub_lenth
  437.     width = width - 1
  438.     right = 0
  439.   else
  440.  
  441.     turtle.turnLeft()
  442.     turtle.forward()
  443.     turtle.turnLeft()
  444.     dist = sub_lenth
  445.     width = width - 1
  446.     right = 1
  447. end
  448. elseif width < 2 then
  449.   dist = sub_lenth
  450.   width = width - 1
  451.   if width <= 0 then
  452.     turtle.turnLeft()
  453.     turtle.turnLeft()
  454.   end
  455. end
  456. end
  457.  
  458.                        
  459. function recenter()                            
  460. working_turn_right_recenter_times = turn_right_recenter_times
  461.                            
  462. working_forward_recenter_times = forward_recenter_times              
  463. gt.goto(setuploc)
  464. if turn_right_recenter_times ~= 0 then
  465.     while working_turn_right_recenter_times ~= 0 do
  466.         turtle.turnRight()
  467.         working_turn_right_recenter_times = working_turn_right_recenter_times - 1      
  468.     end                                
  469. end                          
  470.                                    
  471. right = 1
  472. turtle.select(1)
  473. dist = lenth - 1
  474.   if forward_recenter_times ~= 0 then  
  475.      while working_forward_recenter_times ~= 0 do                          
  476.       turtle.forward()  
  477.       working_forward_recenter_times = working_forward_recenter_times - 1                              
  478.      end                        
  479.   end                              
  480.                                
  481. end
  482.                        
  483.  
  484.                        
  485.                        
  486. function stage1()    
  487.                            
  488.                          
  489.                                        term.setCursorPos(1, 1)
  490.                    term.clearLine()                        
  491.                  print ("Stage: " .. done .. "/" .. totalsteps.. " Harvest and Plant")    
  492. for i=1,#crop_name do                          
  493.        success, data = turtle.inspectDown()  
  494.          if success then        
  495.                              
  496.                                
  497.                                 if data.state.age ~= nil then
  498.                                    
  499.                                    
  500.                                             term.setCursorPos(1, 2)
  501.                                                 term.clearLine()
  502.                                                percentagefloat = (data.state.age - 0) / (7 - 0) * 100
  503.                                                 percentage = math.floor(percentagefloat+0.5)
  504.                                                
  505.                                                 print("Crop Age: " .. percentage .. "%")
  506.                                     end
  507. --         if ignore_nil_age == 1 then
  508.  --    if data.state.age == nil then
  509.    --         data.state.age = crop_max_age                        
  510.    --  end
  511.  --end                              
  512.                              
  513.  if (data.name == crop_name[i]) or (sortblock == 0) then  
  514.        if data.state.age == crop_max_age or ignore_nil_age == 1 then
  515.            --print("Age Check 2: " .. data.state.age)                                          
  516.         turtle.digDown()
  517.                  turtle.suckDown()                                  
  518.                  --term.setCursorPos(1, 3)
  519.                   --term.clearLine()                                  
  520.                  --print("dig")  
  521.                                        
  522.                                      
  523.               end
  524.                 end
  525.                                            
  526.                   if Mode == 1 then                          
  527.                   turtle.placeDown()                            
  528.                     end                
  529.        end
  530.     end        
  531. end        
  532.  
  533.                        
  534.                        
  535. function stage2()  
  536.                            
  537.                            
  538.                            
  539.                             term.setCursorPos(1, 1)
  540.                                   term.clearLine()
  541.                            
  542.                                             print ("Stage: " .. done .. "/" .. totalsteps .. " Cleanup")                                    
  543.         turtle.suckDown()
  544.        
  545.                                     --print("grab")                      
  546.                        
  547.                        
  548.                        
  549. end                        
  550.                        
  551.                        
  552.  
  553. if width == 0 or width == nil  then
  554.     done = done + 1
  555.     --print ("add")                      
  556.     --print ("Steps: " .. done)
  557.     if done == endsteps then
  558.         chest()
  559.         done = 0
  560.         farmexit = 1
  561.     end
  562.  
  563. width = st_width
  564.                             --print("width: " .. width)
  565.                             if farmexit ~= 1 then
  566.                                 recenter()
  567.                             end
  568.                            
  569.  elseif width ~= 0 then
  570.                                
  571.                            
  572.                            
  573. if dist ~= 0 then                          
  574.            
  575.                          
  576. if done == 1 then
  577. term.clear()
  578.    while width ~= 0 do                                
  579.     while dist ~= 0 do                          
  580. stage1()                                
  581. turtle.forward()                        
  582. stage1()
  583. --print(dist)
  584.        dist = dist - 1
  585.                                                    
  586.              
  587.          end                                
  588.         turn()
  589.        end                            
  590.  
  591. elseif done == grabstage then
  592.      term.clear()                                
  593.    while width ~= 0 do                                  
  594.     while dist ~= 0 do
  595. stage2()                        
  596. turtle.forward()
  597. stage2()
  598.                                                
  599.        dist = dist - 1
  600.        end                                  
  601.         turn()
  602.                            
  603.     end
  604.                                        
  605. --unused planting phase not needed because it can be done after digging
  606. elseif done == 9 then
  607.    
  608.    while width ~= 0 do                                
  609.     while dist ~= 0 do
  610.         turtle.placeDown()
  611.         turtle.forward()
  612.        print("plant")
  613.             print ("Steps: " .. done)                                  
  614.    dist = dist - 1
  615.        end
  616.        turn()                                      
  617.     end
  618.       --end- unused planting phase end                        
  619.  
  620.  end
  621.  
  622.                                     end
  623.  
  624.  
  625.                                
  626.  
  627.  
  628.  
  629.  
  630. end
  631. end
  632. end
Add Comment
Please, Sign In to add comment