Savage_Me55iah

CC_FARMER_TURTLE

Mar 19th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.10 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local version, pastebinCode = "v1.5", "bCnEcEma"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. --V1.0
  13. --V1.1 - Complete rewrite to run smoother
  14. --V1.2 - PneumaticCraft plants support added
  15. --V1.3 - Rewritten to flexibly support different mods/plants, section under plant data
  16. --v1.4 - Bug fixes
  17. --v1.5 - Restart from pause
  18. --v1.5 - Added Chococraft & Witchery mod compatable
  19.  
  20. -- SETUP --
  21.  
  22. local pause = false
  23. local grown = false
  24.  
  25. label = os.getComputerLabel() or false
  26. if label == false then
  27.     print("Please Type label here: ")
  28.     os.setComputerLabel(tostring(read()))
  29. end
  30.  
  31. function boolean_check(params)
  32.     local answer = nil
  33.     if tostring(params) == "true" then
  34.         answer = true
  35.     else
  36.         answer = false
  37.     end
  38.     return answer
  39. end
  40.  
  41. local settings_file = "config"
  42.  
  43. local settings_template = [[-- adjust these configuration options as necessary.
  44. -- IMPORTANT!, API won't load if there are spaces in the values
  45. COMPUTER_LABEL = "${label}"
  46. COMPUTER_ID = ${id}
  47.  
  48. TYPES = ${types}
  49. USE_ROWS = "${use_rows}"
  50. TIMER_1 = ${timer_1}
  51. TIMER_MAX = ${timer_max}
  52. TIMER_MIN = ${timer_min}
  53. FIRST_RUN = "${first_run}"
  54. ]]
  55.  
  56. local function interpolate(s, params)
  57.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  58. end
  59.  
  60. do  
  61.     save_settings = function()
  62.         local filehandler = fs.open(tostring(settings_file), "w")
  63.        
  64.         if tostring(CONFIG) == "nil" then
  65.             settings = {
  66.             label = os.getComputerLabel(),
  67.             id = os.getComputerID(),
  68.             types = 7,
  69.             use_rows = "true",
  70.             timer_1 = 100,
  71.             timer_max = 400,
  72.             timer_min = 100,
  73.             first_run = "true"
  74.             }
  75.         else
  76.             settings = {
  77.             label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  78.             id = os.getComputerID(),
  79.             types = CONFIG.TYPES or 7,
  80.             use_rows = CONFIG.USE_ROWS or "true",
  81.             timer_1 = CONFIG.TIMER_1 or 100,
  82.             timer_max = CONFIG.TIMER_MAX or 400,
  83.             timer_min = CONFIG.TIMER_MIN or 100,
  84.             first_run = CONFIG.FIRST_RUN or "true"
  85.             }
  86.         end
  87.  
  88.         filehandler.write(interpolate(settings_template, settings))
  89.         filehandler.close()
  90.     end
  91. end
  92.  
  93. if fs.exists(settings_file) == false then
  94.     save_settings()
  95.     return
  96. else
  97.     os.unloadAPI(settings_file)
  98.  
  99.     if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  100.  
  101.     CONFIG = nil
  102.  
  103.     for k, v in pairs(_G) do
  104.         if k == settings_file then CONFIG = v break end
  105.     end
  106.  
  107.     if CONFIG == nil then log_add("CONFIG came up nil", true) end
  108. end
  109.    
  110. local current = 2
  111. local home_param = false
  112. local x = 1
  113.  
  114. local tArgs = {...}
  115. local update = tostring(tArgs[1]) or nil
  116.  
  117. -- PLANT DATA --
  118.  
  119. local plants = {}
  120.  
  121. function plant_add(mod, dir, meta, drop, stk, replant)
  122.     local num = table.maxn(plants) + 1
  123.     plants[num] = {
  124.         ["mod"] = mod,
  125.         ["dir"] = dir,
  126.         ["meta"] = meta,
  127.         ["drop"] = drop,
  128.         ["stk"] = stk,
  129.         ["replant"] = replant
  130.     }
  131. end
  132.    
  133. plant_add("pneumaticcraft:", "down", {[6] = true, [13] = true}, "true", 16, true)
  134. plant_add("pneumaticcraft:heliumplant", "up", {[6] = true, [13] = true}, "true", 16, true)
  135. plant_add("minecraft:", "down", {[7] = true}, "false", 16, true)
  136. plant_add("minecraft:nether_wart", "down", {[3] = true}, "false", 16, true)
  137. plant_add("minecraft:pumpkin", "down", {[0] = true, [1] = true, [2] = true, [3] = true, [4] = true}, "false", 16, false)
  138. plant_add("minecraft:pumpkin_stem", "down", {[7] = false}, "false", 16, false)
  139. plant_add("minecraft:melon", "down", {[0] = true}, "false", 16, false)
  140. plant_add("minecraft:melon_stem", "down", {[7] = false}, "false", 16, false)
  141. --plant_add("minecraft:reeds", "down", {[0] = true}, "false", 16, false)
  142. --plant_add("minecraft:cactus", "down", {[0] = true}, "false", 16, false)
  143. plant_add("magicalcrops:", "down", {[7] = true}, "false", 16, true)
  144. plant_add("thaumcraft:", "down", {[7] = true}, "false", 16, true)
  145. plant_add("thaumcraft:blockmanapod", "up", {[7] = true}, "false", 16, true)
  146. plant_add("thaumcraft:itemmanabean", "up", {[7] = true}, "false", 16, true)
  147. plant_add("harvestcraft:", "down", {[7] = true}, "false", 16, true)
  148. plant_add("extrautilities:", "down", {[7] = true}, "false", 16, true)
  149. plant_add("natura:n crops", "down", {[3] = true, [8] = true}, "false", 16, true)
  150. plant_add("witchery:", "down", {[4] = true}, "false", 16, true)
  151. plant_add("chococraft:", "down", {[4] = true}, "false", 64, true)
  152. plant_add("ExtraUtilities:plant/ender_lilly", "down", {[7] = true}, "false", 16, true)
  153.  
  154. -- COMMANDS --
  155.  
  156. function drop_off(loc, num)
  157.     local param, num = nil, num or 64
  158.     if loc then param = not turtle.dropUp(num) else param = not turtle.dropDown(num) end
  159.     return param
  160. end
  161.  
  162. function mv_f()
  163. --    print("MV_F")
  164.     if turtle.getFuelLevel() < 200 then
  165. --        print(turtle.getFuelLevel())
  166.         turtle.select(1)
  167.         if turtle.getItemCount() ~= 1 then
  168.             print("refueling")
  169.             turtle.refuel(1)
  170.         else
  171.             print("ERROR: NO FUEL")
  172.             os.pullEvent()
  173.         end
  174.     end
  175.     local params = turtle.forward()
  176.     local z = 1
  177.     while params ~= true and z < 20 do
  178.         params = turtle.forward()
  179.         z = z + 1
  180.     end
  181.     if z > 19 then home_param = true print("mv_f() break") end
  182. end
  183.  
  184. function find_home(params)
  185.  
  186.     if params == true then turtle.turnLeft() end
  187.     home_param = false
  188.     print("HOME")
  189.     --LOOKING FOR HOME CHEST
  190.     local home, chest_loc = false, true
  191.     while home == false do
  192.         while turtle.detect() do
  193.             turtle.turnLeft()
  194.         end
  195.         if turtle.detectUp() then
  196.             local chest = {turtle.inspectUp()}
  197.             if tostring(string.find(chest[2]["name"], "chest")) ~= "nil" or tostring(string.find(chest[2]["name"], "chest")) ~= "nil" then home = true chest_loc = true end
  198.         end
  199.         if turtle.detectDown() then
  200.             local chest = {turtle.inspectDown()}
  201.             if tostring(string.find(chest[2]["name"], "chest")) ~= "nil" or tostring(string.find(chest[2]["name"], "chest")) ~= "nil" then home = true chest_loc = false end
  202.         end
  203.         if home == false then
  204.             while turtle.detect() == false and home_param == false do
  205.                 mv_f()
  206.             end
  207.             if home_param then turtle.turnLeft() end
  208.         end
  209.     end
  210.    
  211.     if boolean_check(CONFIG.FIRST_RUN) ~= true then
  212.    
  213.         --ADJUSTING TIMER
  214.         if boolean_check(grown) == false then
  215.             CONFIG.TIMER_1 = tonumber(CONFIG.TIMER_1) + 10
  216.             save_settings()
  217.         else
  218.             CONFIG.TIMER_1 = tonumber(CONFIG.TIMER_1) - 10
  219.             save_settings()
  220.         end
  221.        
  222.         if tonumber(CONFIG.TIMER_1) > tonumber(CONFIG.TIMER_MAX) then
  223.             CONFIG.TIMER_1 = tonumber(CONFIG.TIMER_1) - 10
  224.             save_settings()
  225.         elseif tonumber(CONFIG.TIMER_MIN) > tonumber(CONFIG.TIMER_1) then
  226.             CONFIG.TIMER_1 = tonumber(CONFIG.TIMER_1) + 10
  227.             save_settings()
  228.         end
  229.        
  230.         --EMPTYING
  231.         for num=2,(tonumber(CONFIG.TYPES) + 1) do
  232.             local paramsA, paramsB = turtle.getItemDetail(num), 16
  233.                         if turtle.getItemCount(num) > 0 then
  234.             for a,b in pairs(plants) do
  235.                 if tostring(string.find(paramsA["name"]:lower(), plants[a]["mod"])) ~= "nil" then paramsB = tonumber(plants[a]["stk"]) end
  236.                          end
  237.             end
  238.        
  239.             if turtle.getItemCount(num) > tonumber(paramsB) then
  240.                 turtle.select(num)
  241.                 pause = drop_off(chest_loc, turtle.getItemCount() - tonumber(paramsB))
  242.             end
  243.         end
  244.         for num=(tonumber(CONFIG.TYPES) + 2),16 do
  245.             if turtle.getItemCount(num) > 0 then turtle.select(num) pause = drop_off(chest_loc, 64) end
  246.             turtle.select(1)
  247.         end
  248.     else
  249.         CONFIG.FIRST_RUN = "false"
  250.         save_settings()
  251.     end
  252.     turtle.turnRight()
  253.     turtle.select(1)
  254.     turtle.suck((64 - turtle.getItemCount()))
  255.     turtle.turnLeft()
  256. end
  257.  
  258. function even(params)
  259.     print("EVEN")
  260.     local num1, num2, answer = tonumber(params / 2), math.floor(tonumber(params / 2)), nil
  261.     local num3 = (num1 - num2) or nil
  262.     if num3 == 0.5 then
  263.         answer = false
  264.     else
  265.         answer = true
  266.     end
  267.     return answer or nil
  268. end
  269.  
  270. function column()
  271.  
  272.     local finish = false
  273.     while finish == false and finish2 == false do
  274.         local paramsA, paramsB = nil, nil
  275.  
  276.         --IDENTIFYING AND CHECKING GROWTH
  277.         term.clear() term.setCursorPos(1,1)
  278.         if turtle.detectDown() then
  279.             paramsA = {turtle.inspectDown()}
  280.             for a,b in pairs(paramsA[2]) do print(a..": "..b) end
  281.             for a,b in pairs(plants) do
  282.                 if tostring(string.find(paramsA[2]["name"]:lower(), plants[a]["mod"])) ~= "nil" and plants[a]["meta"][paramsA[2]["metadata"]] then
  283.                     paramsB = a
  284.                 elseif tostring(string.find(paramsA[2]["name"]:lower(), plants[a]["mod"])) ~= "nil" and not plants[a]["meta"][paramsA[2]["metadata"]] then
  285.                     paramsB = nil
  286.                 end
  287.             end
  288.         end
  289.         if turtle.detectUp() and tostring(paramsB) == "nil" then
  290.             paramsA = {turtle.inspectUp()}
  291.             for a,b in pairs(paramsA[2]) do print(a..": "..b) end
  292.             for a,b in pairs(plants) do
  293.                 if tostring(string.find(paramsA[2]["name"]:lower(), plants[a]["mod"])) ~= "nil" and plants[a]["meta"][paramsA[2]["metadata"]] then
  294.                     paramsB = a
  295.                 elseif tostring(string.find(paramsA[2]["name"]:lower(), plants[a]["mod"])) ~= "nil" and not plants[a]["meta"][paramsA[2]["metadata"]] then
  296.                     paramsB = nil
  297.                 end
  298.             end
  299.         end
  300.  
  301.         --MINING IF GROWN
  302.         if tostring(paramsB) ~= "nil" then
  303.             if boolean_check(CONFIG.USE_ROWS) then turtle.select(x + 1) end
  304.             if plants[paramsB]["dir"] == "up" then
  305.                 turtle.digUp() turtle.suckUp()
  306.             else
  307.                 turtle.digDown() turtle.suckDown()
  308.             end
  309.                     grown = true
  310.         end
  311.        
  312.         finish = turtle.detect()
  313.        
  314.         paramsC, paramsD, paramsE = nil, nil, nil
  315.        
  316.         if boolean_check(CONFIG.USE_ROWS) then
  317.            
  318.             turtle.select(x + 1)
  319.            
  320.             if turtle.getItemCount() > 1 then
  321.                 paramsC = turtle.getItemDetail()
  322.                 for a,b in pairs(paramsC) do print(a..": "..b) end
  323.                 for a,b in pairs(plants) do
  324.                     if tostring(string.find(paramsC["name"]:lower(), plants[a]["mod"])) ~= "nil" then paramsD, paramsE = a, plants[a]["replant"] print("plant mod matched: "..plants[a]["mod"]) end
  325.                     if paramsC["name"]:lower() == "pneumaticcraft:plasticplant" and tonumber(paramsC["damage"]) == 11 then paramsD, paramsE = 2, plants[2]["replant"] end
  326.                 end
  327.             end
  328.            
  329.             if paramsE then
  330.                 if turtle.detectUp() == false and tostring(paramsD) ~= "nil" then
  331.                     if plants[paramsD]["dir"] == "up" then
  332.                         if boolean_check(plants[paramsD]["drop"]) then turtle.dropUp(1) else turtle.placeUp() end
  333.                     end
  334.                 end
  335.                 if turtle.detectDown() == false and tostring(paramsD) ~= "nil" then
  336.                     if plants[paramsD]["dir"] == "down" then
  337.                         if boolean_check(plants[paramsD]["drop"]) then turtle.dropDown(1) else turtle.digDown() turtle.placeDown() end
  338.                     end
  339.                 end
  340.             end
  341.            
  342.         else
  343.            
  344.             local y = 2
  345.            
  346.             while turtle.getItemCount(y) < 2 and y < (CONFIG.TYPES + 1) do y = y + 1 end
  347.             turtle.select(y)
  348.            
  349.             if turtle.getItemCount() > 1 then
  350.                 local params = turtle.getItemDetail()
  351.                 for a,b in pairs(plants) do
  352.                     if string.find(params["name"]:lower(), plants[a]["mod"]) ~= "nil" then params2, params3 = a, plants[a]["replant"] end
  353.                     if params["name"]:lower() == "pneumaticcraft:plasticplant" and tonumber(params["damage"]) == 11 then params2, params3 = 2, plants[2]["replant"] end
  354.                 end
  355.             end
  356.             print(params2.." : "..params3)
  357.             if params3 then
  358.                 if turtle.detectUp() == false and tostring(params2) ~= "nil" then
  359.                     if tostring(plants[params2]["dir"]) == "up" then
  360.                         if boolean_check(plants[params2]["drop"]) then turtle.dropUp(1) print("drop") else turtle.placeUp() print("place") end
  361.                     end
  362.                 elseif turtle.detectDown() == false and tostring(params2) ~= "nil" then
  363.                     if tostring(plants[params2]["dir"]) == "down" then
  364.                         if boolean_check(plants[params2]["drop"]) then turtle.dropDown(1) print("drop") else turtle.placeDown() print("place") end
  365.                     end
  366.                 end
  367.             end
  368.        
  369.         end
  370.        
  371.         turtle.select(1)
  372.         mv_f()
  373.         sleep(1)
  374.     end
  375. end
  376.  
  377. function run()
  378.     finish2, x = false, 1
  379.     grown = false
  380.     while finish2 == false do
  381.         term.clear() term.setCursorPos(1,1)
  382.         print("COLUMN: "..x)
  383.         column()
  384.         if even(x) then
  385.             turtle.turnRight()
  386.             mv_f()
  387.             turtle.turnRight()
  388.         else
  389.             turtle.turnLeft()
  390.             mv_f()
  391.             turtle.turnLeft()
  392.         end
  393.         if x >= tonumber(CONFIG.TYPES) then finish2 = true end
  394.         x = x + 1
  395.     end
  396.     local y = 1
  397.     while turtle.detect() ~= true and y ~= 3 do turtle.turnRight() y = y + 1 end
  398.     find_home()
  399. end
  400.  
  401. find_home()
  402.  
  403. if update ~= "update" then
  404.     while true do
  405.         os.startTimer(tonumber(CONFIG.TIMER_1))
  406.         term.clear()
  407.         term.setCursorPos(1,1)
  408.         print("NAME: "..os.getComputerLabel())
  409.         print("ID: "..os.getComputerID())
  410.         print("ROWS: "..tostring(CONFIG.USE_ROWS))
  411.         print("TIMER: "..tonumber(CONFIG.TIMER_1))
  412.         print("PAUSE: "..tostring(pause))
  413.         local event = os.pullEvent()
  414.         if not pause and event ~= "turtle_inventory" then
  415.             run()
  416.         else
  417.             find_home()
  418.         end
  419.     end
  420. else
  421.     print("UPDATING")
  422.     shell.run("pastebin get "..pastebinCode.." update")
  423.     if fs.exists("update") then
  424.         fs.delete(shell.getRunningProgram())
  425.         fs.copy("update", shell.getRunningProgram())
  426.         fs.delete("update")
  427.         print("UPDATE SUCCESS")
  428.     else
  429.         print("UPDATE FAILED")
  430.     end
  431.     sleep(2)
  432.     os.reboot()
  433.  end
Advertisement
Add Comment
Please, Sign In to add comment