Advertisement
BruceWplays

Farm V10 Experimental

Sep 20th, 2022 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function startfarm(funfarmargs)
  2.     while _G.andy_farm_program_running == 1 do
  3.         local setmenu = _G.setmenu
  4.         --print(table.concat(setmenu,", "))
  5.         --print(tostring(setmenu))
  6.         version = "5"
  7.         --term.clear()
  8.         --term.setCursorPos(1, 1)
  9.         --print(version)
  10.         done = 0
  11.         local farm_prog_progress = "init"
  12.         farm_prog_progress = 0
  13.         farm_prog_progress = farm_prog_progress + 1
  14.         farmapiloadedguilib = 0
  15.         --term.clear()
  16.         --term.setCursorPos(1, 1)                          
  17.        
  18.         os.loadAPI("AndysPrograms/api/gt")  
  19.         while farmapiloadedguilib ~= 1 do
  20.             if os.loadAPI("AndysPrograms/api/gui/gui") then
  21.                 --print("loaded gui farmapi lib")
  22.                 farmapiloadedguilib = 1
  23.             else
  24.                 print(" NOTloaded gui lib")
  25.             end
  26.         end
  27.  
  28.         local setmenu = gui.get_setmenu()
  29.         --andy_farm_program_running = 0
  30.  
  31.         if fs.exists("AndysPrograms/Farm/settings") then
  32.             settings.load("AndysPrograms/Farm/settings")
  33.         end
  34.         local maxW, maxH = term.getSize()
  35.  
  36.         farmexit = 0
  37.         while farmexit ~= 1 do
  38.  
  39.         local setmenu = _G.setmenu
  40.  
  41.  
  42.  
  43.  
  44.         -- setmenu = {
  45.         --     {options = "Finish editing", handler = mainmenu_farm},
  46.         --     {text = "Farm Lenth", setname = "Farm Lenth", options = settings.get("Farm Lenth"), type = "num", handler = chngset},
  47.         --     {text = "Farm Width", setname = "Farm Width", options = settings.get("Farm Width"), type = "num", handler = chngset},
  48.         --     {text = "Mode, 1 = Nrml, 2 = Pump/Mel/SgrCne", setname = "Mode", options = settings.get("Mode"), type = "num", handler = chngset},
  49.         --     {text = "Start Location", setname = "Start Location", options = settings.get("Start Location"), type = deftab, handler = chngset},
  50.         --     {text = "Chest Location", setname = "Chest Location", options = settings.get("Chest Location"), type = deftab, handler = chngset},
  51.         --     {text = "Chest Direction 0=fwd 2=up 1=dn", setname = "Chest Direction", options = settings.get("Chest Direction"), type = "num", handler = chngset},
  52.         --     {text = "Sort Blocks", setname = "Sort Blocks", options = settings.get("Sort Blocks"), type = "num", handler = chngset},
  53.         --     {text = "Sort Block Names", setname = "Sort Block Names", options = settings.get("Sort Block Names"), type = deftab, handler = chngset}
  54.         -- }
  55.         ---------------------
  56.         ---------------------
  57.         ---------------------
  58.         --CHANGE HERE START--
  59.         ---------------------
  60.         ---------------------
  61.         ---------------------
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.         --"1" is Normal, "2" is Pumpkin/Melon/Probably Sugar Cane
  70.         farm_prog_progress = farm_prog_progress + 1
  71.         Mode = tonumber(settings.get(setmenu[4].setname))
  72.        
  73.         if Mode > 2 or Mode < 1 then
  74.             Mode = 2
  75.         end
  76.  
  77.  
  78.         Farm_Lenth = tonumber(settings.get(setmenu[2].setname))
  79.         Farm_Width = tonumber(settings.get(setmenu[3].setname))
  80.  
  81.  
  82.  
  83.  
  84.         -- sort for specific crop
  85.  
  86.         --whether or not to sort, 1 is yes, 0 is no
  87.         sortblock = tonumber(settings.get(setmenu[8].setname))
  88.         if sortblock > 1 or sortblock < 0 then
  89.             sortblock = 1
  90.         end
  91.  
  92.         --name of the crop
  93.         crop_name = settings.get(setmenu[9].setname)
  94.  
  95.         --max age of crop should probably be left at 7
  96.         crop_max_age = 7
  97.  
  98.  
  99.  
  100.         --setup location
  101.             setuploc = settings.get(setmenu[5].setname)
  102.  
  103.  
  104.  
  105.         --chest location
  106.             chestloc = settings.get(setmenu[6].setname)
  107.  
  108.         farmsleeptime = 0
  109.        
  110.         --chest 2 = "up", 1 = "down", or 0 = "forward"
  111.         chestdirection = tonumber(settings.get(setmenu[7].setname))
  112.            
  113.         if chestdirection > 2 or chestdirection < 0 then
  114.             chestdirection = 2
  115.         end
  116.             --misc
  117.                                
  118.                             --ignore if a crop has no age value should be set at 1 for Pumpkin/Melon and 0 for Normal
  119.                                 Override_ignore_nil_age = 0
  120.                                 ignore_nil_age = 1
  121.  
  122.                                 override_stages = 0
  123.                                 turnleftchest = 0
  124.                                 totalstages = 1
  125.         -------------------
  126.         -------------------
  127.         --CHANGE HERE END--
  128.         -------------------
  129.         -------------------
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.         farm_prog_progress = farm_prog_progress + 1
  147.  
  148.  
  149.  
  150.         function scrlnum(a,b,st,cl)
  151.         if a == b then
  152.             return a, 1
  153.         else
  154.             local clend = cl + st
  155.             local cldiv = cl - clend
  156.             local clenddiv = clend - cl
  157.             scrlnumper = (os.clock() - cl) / clenddiv
  158.             --local scrlnumperold = scrlnumper
  159.             scrlnumper = scrlnumper ^ 2
  160.             --scrlnumper = ((scrlnumper - 1) ^ 2) - 1
  161.             --if scrlnumper < scrlnumperold then
  162.             --    scrlnumper = scrlnumperold
  163.             --end
  164.             --if scrlnumper < 0 then
  165.             --    scrlnumper = -scrlnumper
  166.             --end
  167.             --if scrlnumper > 1.02 and scrlnumper < 0.98 then
  168.             --    scrlnumper = 1
  169.             --end
  170.  
  171.             return math.floor(((scrlnumper * (b - a)) + a) + 0.1), scrlnumper
  172.             --return ((scrlnumper * (b - a)) + a) + 0.1, scrlnumper
  173.         end
  174.         end
  175.  
  176.  
  177.  
  178.  
  179.  
  180.         --unused!!!! Recenter Values
  181.         turn_right_recenter_times = 0
  182.         forward_recenter_times = 0
  183.  
  184.         if Override_ignore_nil_age == 0 then
  185.         if Mode == 2 then
  186.                 ignore_nil_age = 1
  187.         elseif Mode == 1 then
  188.                 ignore_nil_age = 0
  189.         end
  190.         end
  191.  
  192.         st_width = Farm_Width
  193.         lenth = Farm_Lenth
  194.         if override_stages == 1 then
  195.             totalsteps = totalstages
  196.             if Mode == 2 then
  197.                 grabstage = 5
  198.             elseif Mode == 1 then
  199.                 grabstage = 2
  200.             end
  201.         elseif Mode == 2 then
  202.         if override_stages == 0 then
  203.                 grabstage = 5
  204.                 totalstages = 1
  205.                 totalsteps = 1
  206.             end
  207.         elseif  Mode == 1 then
  208.             if override_stages == 0 then
  209.                 grabstage = 2
  210.                 totalstages = 2
  211.                 totalsteps = 2
  212.             end
  213.         end
  214.  
  215.         --term.clear()
  216.         --term.setCursorPos(1, 1)
  217.         --print("Recentering")
  218.         totalsteps = totalstages
  219.         endsteps = totalsteps + 1
  220.         sub_lenth = lenth - 1
  221.         function chest()
  222.             --term.clear()
  223.             --term.setCursorPos(1, 1)
  224.             --print("Going to Chest")
  225.             gt.goto(chestloc)
  226.             if Mode == 2 then
  227.                 chest = 1
  228.             elseif  Mode == 1 then
  229.                 chest = 2
  230.             end
  231.             --term.clear()
  232.             --term.setCursorPos(1, 1)
  233.             --print("Putting in Chest")
  234.            
  235.                
  236.             while chest <= 16 do
  237.                 turtle.select(chest)
  238.                 if chestdirection == 0 then
  239.                     turtle.drop()
  240.                 elseif chestdirection == 1 then
  241.                     turtle.dropDown()
  242.                 elseif chestdirection == 2 then
  243.                     turtle.dropUp()
  244.                 end                    
  245.                 chest = chest + 1
  246.             end
  247.             if turnleftchest == 1 then
  248.                 turtle.turnLeft()
  249.             end
  250.             --term.clear()
  251.             --term.setCursorPos(1, 1)
  252.             --print("Resetting")
  253.             -- sleep(farmsleeptime)
  254.             farmexit = 1
  255.         end
  256.  
  257.  
  258.         function turn()
  259.             if width >= 2 then
  260.                 if right == 1 then
  261.                     turtle.turnRight()
  262.                     turtle.forward()
  263.                     turtle.turnRight()
  264.                     dist = sub_lenth
  265.                     width = width - 1
  266.                     right = 0
  267.                 else
  268.                     turtle.turnLeft()
  269.                     turtle.forward()
  270.                     turtle.turnLeft()
  271.                     dist = sub_lenth
  272.                     width = width - 1
  273.                     right = 1
  274.                 end
  275.             elseif width < 2 then
  276.                 dist = sub_lenth
  277.                 width = width - 1
  278.                 if width <= 0 then
  279.                     turtle.turnLeft()
  280.                     turtle.turnLeft()
  281.                 end
  282.             end
  283.         end
  284.  
  285.  
  286.         function recenter()                            
  287.             working_turn_right_recenter_times = turn_right_recenter_times
  288.  
  289.             working_forward_recenter_times = forward_recenter_times
  290.             gt.goto(setuploc)
  291.                 if turn_right_recenter_times ~= 0 then
  292.                     while working_turn_right_recenter_times ~= 0 do
  293.                         turtle.turnRight()
  294.                         working_turn_right_recenter_times = working_turn_right_recenter_times - 1
  295.                     end
  296.                 end
  297.  
  298.             right = 1
  299.             turtle.select(1)
  300.             dist = lenth - 1
  301.             if forward_recenter_times ~= 0 then
  302.                 while working_forward_recenter_times ~= 0 do
  303.                     turtle.forward()
  304.                     working_forward_recenter_times = working_forward_recenter_times - 1
  305.                 end
  306.             end
  307.         end
  308.  
  309.         if percentageold == nil then
  310.             percentageold = 0
  311.         end
  312.         if dopnt == nil then
  313.             dopnt = 1
  314.         end
  315.         if tnstm == nil then
  316.             tnstm = 0.01
  317.         end
  318.         if scagtm == nil then
  319.             scagtm = 0.01
  320.         end
  321.         function stage1()
  322.             ttmst = os.clock()
  323.             --term.setCursorPos(1, 1)
  324.             --term.clearLine()
  325.             if totalstages <= 1 then
  326.                 --print ("Stage: " .. done .. "/" .. totalsteps.. " Harvest")
  327.             elseif totalstages >= 2 then
  328.                 --print ("Stage: " .. done .. "/" .. totalsteps.. " Harvest and Plant")
  329.             end
  330.             for i=1,#crop_name do
  331.                 success, data = turtle.inspectDown()
  332.                 if success then
  333.                     if dopnt == 6 then
  334.                     if data.state.age ~= nil then
  335.                         scnmpnt = 0
  336.                         dlprns1 = 0
  337.                         percentagefloat = (data.state.age - 0) / (7 - 0) * 100
  338.                         percentage = math.floor(percentagefloat+0.5)
  339.                         scnmtmst = os.clock()
  340.  
  341.                         scagl = 0
  342.                         while dlprns1 < 1 do
  343.                             scagtmst = os.clock()
  344.                             scnmpnt, dlprns1 = scrlnum(percentageold,percentage,tnstm,scnmtmst)
  345.                             if dlprns1 == nil then
  346.                                 dlprns1 = 0
  347.                             end
  348.                             -- term.setCursorPos(1, 2)
  349.                             -- term.clearLine()
  350.                             --print("Crop Age: " .. scnmpnt .. "%")
  351.                             --term.setCursorPos(1, 4)
  352.                             --term.clearLine()
  353.                             --if dlprns1 ~= nil then
  354.                             --print("" .. dlprns1 .. "")
  355.                             --end
  356.                             --term.setCursorPos(1, 6)
  357.                             --term.clearLine()
  358.                             --print("" .. percentageold .. "")
  359.                             if scagl > 25 then
  360.                                 sleep()
  361.                                 scagl = 0
  362.                             end
  363.                             scagl = scagl + 1
  364.                             scagtmen = os.clock()
  365.                             scagtm = scagtmen - scagtmst
  366.                             end
  367.                         percentageold = percentage
  368.                        
  369.                        
  370.                     end
  371.                     end
  372.                     if (data.name == crop_name[i]) or (sortblock == 0) then
  373.                         if data.state.age == crop_max_age or ignore_nil_age == 1 then
  374.                             --print("Age Check 2: " .. data.state.age)
  375.                             turtle.digDown()
  376.                             --term.setCursorPos(1, 3)
  377.                             --term.clearLine()
  378.                             --print("dig")  
  379.  
  380.                         end
  381.                     end
  382.                     turtle.suckDown()
  383.                     if Mode == 1 then
  384.                         turtle.placeDown()
  385.                     end
  386.                 end
  387.             end
  388.             ttmen = os.clock()
  389.             tnstm = ttmen - ttmst
  390.             tnstm = tnstm - scagtm
  391.             tnstm = tnstm / 2
  392.             if tnstm < 0 then
  393.                 tnstm = 0.02
  394.             end
  395.         end
  396.  
  397.         function scrlage()
  398.             for i=1,#crop_name do
  399.                 success, data = turtle.inspectDown()
  400.                 if success then
  401.                     if dopnt == 1 then
  402.                     if data.state.age ~= nil then
  403.                         scnmpnt = 0
  404.                         dlprns1 = 0
  405.                         percentagefloat = (data.state.age - 0) / (7 - 0) * 100
  406.                         percentage = math.floor(percentagefloat+0.5)
  407.                         scnmtmst = os.clock()
  408.  
  409.                        
  410.                         while dlprns1 < 1 do
  411.                             scnmpnt, dlprns1 = scrlnum(percentageold,percentage,0.5,scnmtmst)
  412.                             if dlprns1 == nil then
  413.                                 dlprns1 = 0
  414.                             end
  415.                             -- term.setCursorPos(1, 2)
  416.                             -- term.clearLine()
  417.                             --print("Crop Age: " .. scnmpnt .. "%")
  418.                             --term.setCursorPos(1, 4)
  419.                             --term.clearLine()
  420.                             --if dlprns1 ~= nil then
  421.                             --print("" .. dlprns1 .. "")
  422.                             --end
  423.                             --term.setCursorPos(1, 6)
  424.                             --term.clearLine()
  425.                             --print("" .. percentageold .. "")
  426.                                 sleep()
  427.                             end
  428.                         percentageold = percentage
  429.                        
  430.                        
  431.                     end
  432.                     end
  433.                 end
  434.             end
  435.         end
  436.  
  437.  
  438.  
  439.                
  440.         function stage2()
  441.             -- term.setCursorPos(1, 1)
  442.             -- term.clearLine()
  443.             --print ("Stage: " .. done .. "/" .. totalsteps .. " Cleanup")
  444.             turtle.suckDown()
  445.             --print("grab")
  446.         end
  447.  
  448.         function stg1tg()
  449.         if dist ~= 0 then
  450.             if done == 1 then
  451.                 -- term.clear()
  452.                 while width ~= 0 do
  453.                     while dist ~= 0 do
  454.                         dopnt = 1
  455.                         parallel.waitForAll(stage1,scrlage)
  456.                         --stage1()
  457.                         dopnt = 1
  458.                         turtle.forward()
  459.                         --parallel.waitForAll(stage1,scrlage)
  460.                         stage1()
  461.                         --print(dist)
  462.                         dist = dist - 1
  463.                     end
  464.                     turn()
  465.                 end
  466.             end
  467.         end
  468.         end
  469.  
  470.  
  471.         if width == 0 or width == nil  then
  472.             done = done + 1
  473.             --print ("add")
  474.             --print ("Steps: " .. done)
  475.             if done == endsteps then
  476.                 chest()
  477.                 done = 0
  478.                 farmexit = 1
  479.             end
  480.             width = st_width
  481.             --print("width: " .. width)
  482.             if farmexit ~= 1 then
  483.                 recenter()
  484.             end
  485.  
  486.         elseif width ~= 0 then
  487.  
  488.             if dist ~= 0 then
  489.                 if done == 1 then
  490.                     -- term.clear()
  491.                     while width ~= 0 do
  492.                         while dist ~= 0 do
  493.                             dopnt = 1
  494.                             parallel.waitForAll(stage1,scrlage)
  495.                             --stage1()
  496.                             dopnt = 1
  497.                             turtle.forward()
  498.                             parallel.waitForAll(stage1,scrlage)
  499.                             --stage1()
  500.                             --print(dist)
  501.                             dist = dist - 1
  502.                         end
  503.                         turn()
  504.                     end
  505.  
  506.                 elseif done == grabstage then
  507.                     -- term.clear()
  508.                     while width ~= 0 do
  509.                         while dist ~= 0 do
  510.                             stage2()
  511.                             turtle.forward()
  512.                             stage2()
  513.                             dist = dist - 1
  514.                         end
  515.                         turn()
  516.                     end
  517.             end
  518.  
  519.         end
  520.         end
  521.         end
  522.     end
  523. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement