BruceWplays

Farm V9.6 old

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