Savage_Me55iah

OC OREQUARRY WIP

Nov 5th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.69 KB | None | 0 0
  1.  
  2.  
  3. -- LOGGING --
  4.  
  5. local fs = require("filesystem")
  6.  
  7.  
  8. local dir_main = "/floppy/OREQUARRY" if fs.exists(dir_main) ~= true then fs.makeDirectory(dir_main) end
  9. local dir_logs = dir_main.."/LOGS" if fs.exists(dir_logs) ~= true then fs.makeDirectory(dir_logs) end
  10. local coord_log = dir_logs.."/coord" if fs.exists(coord_log) then fs.remove(coord_log) end
  11. local prog_log = dir_logs.."/prog" if fs.exists(prog_log) then fs.remove(prog_log) end
  12. local error_log = dir_logs.."/error"
  13.  
  14. do
  15.   log_add = function(...)
  16.     local params = {...}
  17.     local log_loc, log_text, log_error, error_lvl = nil, nil, nil, 0
  18.     if params[1] == "coord" then
  19.       log_text, log_loc = tostring("x: "..params[2]..". y: "..params[3]..". z: "..params[4]..". a: "..params[5]..". mining level: "..params[6]), coord_log
  20.     elseif params[1] == "prog" then
  21.       log_text, log_loc, log_error, error_lvl = tostring(params[2]), prog_log, params[3] or nil, params[4] or 0
  22.     end
  23.    
  24.     if params[1] == "error" then
  25.       log_text, log_loc = tostring("Error: "..params[2].."-"..(params[4] or 0), error_log
  26.       local fh = io.open(log_loc, 'a')
  27.       fh:write(log_text:upper()) fh:write("\n") fh:close()
  28.     end
  29.    
  30.     if tostring(log_error) ~= "nil" or error_lvl ~= 0 then
  31.       log_text = tostring("--Error: "..log_text.."-"..error_lvl)
  32.     end
  33.    
  34.     local fh = io.open(log_loc, 'a')
  35.     fh:write(log_text:upper())
  36.     fh:write("\n")
  37.     fh:close()
  38.   end
  39. end
  40.  
  41. -- SAVING & LOADING --
  42.  
  43. local serial = require("serialization")
  44. local settings_file = "CONFIG"
  45.  
  46. do
  47.   load_one = function(command)
  48.     log_add("prog", "loading: "..command)
  49.     if fs.exists(tostring(dir_main.."/"..command)) then
  50.       local fh = io.open(dir_main.."/"..command, 'r')
  51.       return serial.unserialize(fh.read())
  52.     end
  53.   end
  54.  
  55.   save_one = function(command1, command2)
  56.     local fh = io.open(tostring(dir_main.."/"..command1), 'w')
  57.     fh:write(serial.serialize(command2))
  58.     fh:close()
  59.     log_add("prog", "Saving: "..command1)
  60.   end
  61. end
  62.  
  63. -- CONFIGS --
  64.  
  65. local term, comp, rob, gen, solar, pwrsource, inv, cos = require("term"), require("component"), nil, nil, nil, false, nil, require("computer")
  66.  
  67. local config = {["home_coords"] = {["x"] = 0,
  68.                                    ["y"] = 0,
  69.                                    ["z"] = 0,
  70.                                    ["a"] = 0
  71.                                    ["direction"] = "north"},
  72.                 ["quarry1_coords"] = {["x"] = 0, ["z"] = 0},
  73.                 ["quarry2_coords"] = {["x"] = 0, ["z"] = 0},
  74.                 ["quarry3_coords"] = {["x"] = 0, ["z"] = 0},
  75.                 ["curr_coords"] = {["x"] = 0,
  76.                                    ["y"] = 0,
  77.                                    ["z"] = 0,
  78.                                    ["a"] = 0,
  79.                                    ["direction"] = "north",
  80.                                    ["mining_level"] = 0},
  81.                 ["bedrock"] = {["flat"] = false,
  82.                                ["y"] = -100},
  83.                 ["noise_bocks"] = 0,
  84.                 ["clearing"] = false,
  85.                 ["fuel"] = {["slot"] = 1,
  86.                             ["lower"] = 10,
  87.                             ["upper"] = 90},
  88.                 ["enderchest"] = {["output"] = false,
  89.                                   ["fuel"] = false}
  90.                 ["tool"] = {["auto_repair"] = false,
  91.                             ["upper"] = 30,
  92.                             ["lower"] = 10,
  93.                             ["slot"] = 1},
  94.                 ["solar"] = false,
  95.                 ["slots"] = {["lower"] = 2,
  96.                              ["upper"] = 16},
  97.                 ["area"] = 14,
  98.                 ["dumping"] = false,
  99.                 ["maxslots"] = 16,
  100.                 ["gps"] = false,
  101.                }
  102.  
  103. local initial_setup = nil
  104.  
  105. if fs.exists(dir_main.."/"..settings_file) ~= true then
  106.  
  107.   initial_setup = {["questions"] = "area to mine? (recommend 14 with robot 1 block in from the chunk borders)"
  108.     "is the bedrock flat? y/n",
  109.     "am i to gather ores or clear all blocks? y/n",
  110.     "will i be ouputting into an enderchest? (slot 2) y/n",
  111.     "will i be taking fuel from an enderchest? (slot 3) y/n",
  112.     "do i have solar generator? y/n",
  113.     "does my pick have auto-repair? y/n",
  114.     "dispose of extra noise blocks? (if not silk touch: place cobble in the noise block section) y/n"},
  115.     ["answers"] = nil}
  116.    
  117.   for num=1,table.maxn(initial_setup["questions"]) do
  118.     term.clear()
  119.     print(initial_setup["questions"][num])
  120.     log_add("prog", "initial setup q:-"..num)
  121.     initial_setup["answers"][num] = term.read()
  122.     log_add("prog", "initial setup a:-"..initial_setup["answers"][num])
  123.   end
  124.  
  125.   for num=2,table.maxn(initial_setup["answers"]) do
  126.     if initial_setup["answers"][num] == "y" then
  127.       initial_setup["answers"][num] = true
  128.     else
  129.       initial_setup["answers"][num] = false
  130.     end
  131.     log_add("prog", "initial setup boolean a:-"..initial_setup["answers"][num])
  132.   end
  133.  
  134.   config["area"] = tonumber(initial_setup["answers"][1]) or 14 log_add("prog", tostring("mining area = "..config["area"]))
  135.   config["bedrock"]["flat"] = initial_setup["answers"][2] or false log_add("prog", tostring("flat bedrock = "..config["bedrock"]["flat"]))
  136.   config["clearing"] = not initial_setup["answers"][3] or false log_add("prog", tostring("clearing = "..config["clearing"]))
  137.   config["enderchest"]["output"] = initial_setup["answers"][4] or false log_add("prog", tostring("enderchest 1 = "..config["enderchest"]["output"]))
  138.   config["enderchest"]["fuel"] = initial_setup["answers"][5] or false log_add("prog", tostring("enderchest 2 = "..config["enderchest"]["fuel"]))
  139.   config["solar"] = tonumber(initial_setup["answers"][6]) or false log_add("prog", tostring("solar = ".. config["solar"]))
  140.   config["tool"]["auto_repair"] = initial_setup["answers"][7] or false log_add("prog", tostring("tool = "..config["tool"]["auto_repair"]))
  141.   config["dumping"] = initial_setup["answers"][8] or false log_add("prog", tostring("dumping = "..config["dumping"]))
  142.  
  143.   local noise, num2, num3 = 0, 1, 4
  144.   for num=1,64 do config["maxslots"] = rob.select(num) end
  145.   while num2 ~= 0 or tostring(num2) ~= "nil" do num3 = num3 + 1 num2 = rob.count(num3) end
  146.   config["noise_bocks"] = num3 log_add("prog", tostring("noise blocks = "..config["noise_bocks"]))
  147.   if config["noise_bocks"] < 1 then log_add("error", "no noise blocks", true, 2) end
  148.  
  149.   save_one(settings_file, config)
  150. else
  151.   config = load_one("CONFIG")
  152. end
  153.  
  154. if comp.isAvailable("robot") then rob = require("robot") else log_add("error", "computer isn't a robot", true, 3) end
  155. if rob.durability() == nil then log_add("error", "no pick", true, 2) end
  156. if comp.isAvailable("generator") then gen = comp.generator pwrsource = true else log_add("error", "no generator", true, 1) end
  157. if config["solar"] == false and pwrsource == false then log_add("error", "no powersources", true, 3) end
  158. if tostring(gen) ~= "nil" then
  159.   if (rob.count(config["fuel"]["slot"]) > 10) == true then rob.select(config["fuel"]["slot"]) gen.remove(64) gen.insert(1) else log_add("error", "not enough fuel", true, 2) end
  160. end
  161. if comp.isAvailable("navigation") then gps = comp.navigation log_add("prog", "gps available") config["gps"] = true else log_add("error", "no gps", true, 1) config["gps"] = false end
  162. if comp.isAvailable("inventory_controller") then inv = comp.inventory_controller log_add("prog", "inv controller available") else log_add("error", "no inventory controller", true, 3) end
  163.  
  164. -- BASIC FUNCTIONS --
  165.  
  166. local direction = {}
  167.  
  168. if config["gps"] then  
  169.   direction = {[1] = "north", [2] = "west", [3] = "south", [4] = "east"}
  170. else      
  171.   direction = {[3] = "north", [5] = "west", [2] = "south", [4] = "east"}          
  172. end              
  173.  
  174. do
  175.   dig = function(...)
  176.     local params = {...}
  177.    
  178.     local dura = {rob.durability()}
  179.     if dura[1] <= config["tool"]["lower"]
  180.       while dura[1] <= config["tool"]["upper"] do
  181.         rob.select(config["tool"]["slot"])
  182.         inv.equip()
  183.         dura = {rob.durability()}
  184.         rob.select(1)
  185.       end
  186.     end
  187.    
  188.     if params[1] == "u" then
  189.       if rob.detectUp() then
  190.         if params[2] or config["clearing"] then
  191.           rob.swingUp()
  192.         else
  193.           local noise = false
  194.           for num=4,config["noise_bocks"] do
  195.             if noise ~= true then
  196.               rob.select(num)
  197.               noise = rob.compareUp()
  198.               rob.select(1)
  199.             end
  200.           end
  201.           if noise ~= true then rob.swingUp() end
  202.         end
  203.       end
  204.      
  205.     elseif params[1] == "f" then
  206.       if rob.detect() then
  207.         if params[2] or config["clearing"] then
  208.           rob.swing()
  209.         else
  210.           local noise = false
  211.           for num=4,config["noise_bocks"] do
  212.             if noise ~= true then
  213.               rob.select(num)
  214.               noise = rob.compare()
  215.               rob.select(1)
  216.             end
  217.           end
  218.           if noise ~= true then rob.swing() end
  219.         end
  220.       end
  221.      
  222.     elseif params[1] == "d" then
  223.       if rob.detectDown() then
  224.         if params[2] or config["clearing"] then
  225.           rob.swingDown()
  226.         else
  227.           local noise = false
  228.           for num=4,config["noise_bocks"] do
  229.             if noise ~= true then
  230.               rob.select(num)
  231.               noise = rob.compareDown()
  232.               rob.select(1)
  233.             end
  234.           end
  235.           if noise ~= true then rob.swingDown() end
  236.         end
  237.       end
  238.     end
  239.   end
  240.        
  241.   fuel = function()    
  242.     local powerlvl = math.floor((cos.energy() / cos.maxEnergy()) * 100)
  243.     if tonumber(powerlvl) <= tonumber(config["fuel"]["lower"]) then
  244.       while powerlvl <= tonumber(config["fuel"]["upper"]) then
  245.         if tonumber(gen.count()) ~= 1 then
  246.           if rob.count(config["fuel"]["slot"]) < 2 then
  247.             rob.select(config["enderchest"]["fuel"])
  248.             local chest_place = rob.placeUp()
  249.             while chest_place ~= true do
  250.               chest_place = rob.placeUp()
  251.               dig("u", true)
  252.             end
  253.             rob.select(config["fuel"]["slot"])
  254.             rob.suckUp(63)
  255.             rob.select(config["enderchest"]["fuel"])
  256.             rob.swingUp()
  257.           end
  258.           rob.select(config["fuel"]["slot"])
  259.           if rob.count ~= 1 then gen.insert(1) end
  260.           rob.select(1)
  261.         end
  262.         powerlvl = math.floor((cos.energy() / cos.maxEnergy()) * 100)
  263.       end
  264.     end
  265.   end
  266.  
  267.   gps_update = function()
  268.     config["curr_coords"]["x"], config["curr_coords"]["y"], config["curr_coords"]["z"] = gps.getPosition()
  269.     config["curr_coords"]["a"] = direction[gps.getFacing()]
  270.     save_one(settings_file, config)
  271.   end
  272.  
  273.   move = function(...)
  274.     local params, num = {...}, 270
  275.    
  276.     fuel()
  277.    
  278.     if params[1] == "up" then
  279.       local moved = rob.up()
  280.       while moved ~= true and num ~= 0 do num = num - 1 moved = rob.up() dig("u", true) end
  281.       if num ~= 0 then
  282.         if config["gps"] then gps_update() else config["curr_coords"]["y"] = config["curr_coords"]["y"] + 1 end
  283.       else
  284.         log_add("error", "move break up", true, 1)
  285.       end
  286.     elseif params[1] == "down" then
  287.       local moved = rob.down()
  288.       while moved ~= true and num ~= 0 do num = num - 1 moved = rob.down() dig("d", true) end
  289.       if num ~= 0 then
  290.         if config["gps"] then gps_update() else config["curr_coords"]["y"] = config["curr_coords"]["y"] - 1 end
  291.       else
  292.         log_add("error", "move break down", true, 1)
  293.       end
  294.     elseif params[1] == "forward" then
  295.       local moved = rob.forward()
  296.       while moved ~= true and num ~= 0 do num = num - 1 moved = rob.forward() dig("f", true) end
  297.       if num ~= 0 then
  298.         if config["gps"] then
  299.           gps_update()
  300.         else
  301.           if config["curr_coords"]["a"] == "north" then config["curr_coords"]["z"] = config["curr_coords"]["z"] + 1
  302.           elseif config["curr_coords"]["a"] == "west" then config["curr_coords"]["x"] = config["curr_coords"]["x"] - 1
  303.           elseif config["curr_coords"]["a"] == "south" then config["curr_coords"]["z"] = config["curr_coords"]["z"] - 1
  304.           elseif config["curr_coords"]["a"] == "east" then config["curr_coords"]["x"] = config["curr_coords"]["x"] + 1
  305.           end
  306.         end
  307.       else
  308.         log_add("error", "move break forward", true, 1)
  309.       end
  310.     elseif params[1] == "tl" then
  311.       rob.turnLeft()
  312.       if config["gps"] then
  313.         gps_update()
  314.       else
  315.         config["curr_coords"]["a"] = config["curr_coords"]["a"] + 1
  316.         if config["curr_coords"]["a"] == 5 then config["curr_coords"]["a"] = 1 end
  317.         config["curr_coords"]["direction"] = direction[config["curr_coords"]["a"]]
  318.       end
  319.     elseif params[1] == "tr" then
  320.       rob.turnRight()
  321.       if config["gps"] then
  322.         gps_update()
  323.       else
  324.         config["curr_coords"]["a"] = config["curr_coords"]["a"] - 1
  325.         if config["curr_coords"]["a"] == 0 then config["curr_coords"]["a"] = 4 end
  326.         config["curr_coords"]["direction"] = direction[config["curr_coords"]["a"]]
  327.       end
  328.     end
  329.   end
  330.      
  331.   find_bedrock = function(...)
  332.     local params, bedrock = {...}, true
  333.     params[1] = params[1] or false
  334.    
  335.     if params[1] then log_add("prog", "finding bedrock") else log_add("prog", "digging to bedrock") end
  336.     while bedrock do
  337.       if rob.detectDown() then bedrock = rob.swingDown() end
  338.       move("down")
  339.     end
  340.    
  341.     if params[1] then
  342.       if config["gps"] then gps_update() end
  343.       config["bedrock"]["y"] = config["curr_coords"]["y"]
  344.       log_add("prog", tostring("bedrock located @: "..config["curr_coords"]["y"]))
  345.     end
  346.   end
  347.    
  348. --  scan_move = function(...)
  349. --    local params = {...}
  350. --    params[1], params[2] = params[1] or 1, params[2] or false
  351. --    
  352. --    for num=1,params[1] do
  353. --      move("forward")
  354. --      dig("u")
  355. --      if params[2] then find_bedrock() else dig("d") end
  356. --    end
  357. --  end
  358.  
  359.   even = function(...)
  360.     local params = {...}
  361.    
  362.     params[4] = false
  363.     if type(params[1]) == "number" then
  364.       params[2] = params[1] / 2
  365.       params[3] = params[2] - math.floor(params[2])
  366.       if params[3] ~= 0 and tostring(params[3]) ~= "nil" then params[4] = false else params[4] = true end
  367.     end
  368.    
  369.     return params[4]
  370.   end
  371.  
  372.   goto = function(scan, x, y, z, a)
  373.     log_add("prog", "going to: "..tostring(x.." "..y.." "..z.." "..a))
  374.    
  375.     local angle = config["curr_coords"]["direction"]
  376.    
  377.     if tostring(x) ~= "nil" then
  378.       while config["curr_coords"]["x"] > x do
  379.         while config["curr_coords"]["direction"] ~= "west" do move("tl") end
  380. --        if scan == 0 then
  381. --          scan_move(1, false)
  382. --        elseif scan == 1 then
  383. --          scan_move(1, true)
  384. --        elseif scan ~= 0 and scan ~= 1 then
  385.           move("forward")
  386. --        end
  387.       end
  388.       while config["curr_coords"]["x"] < x do
  389.         while config["curr_coords"]["direction"] ~= "east" do move("tl") end
  390. --        if scan == 0 then
  391. --          scan_move(1, false)
  392. --        elseif scan == 1 then
  393. --          scan_move(1, true)
  394. --        elseif scan ~= 0 and scan ~= 1 then
  395.           move("forward")
  396. --        end
  397.       end
  398.     end
  399.    
  400.     if tostring(z) ~= "nil" then
  401.       while config["curr_coords"]["  z"] > z do
  402.         while config["curr_coords"]["direction"] ~= "south" do move("tl") end
  403. --        if scan == 0 then
  404. --          scan_move(1, false)
  405. --        elseif scan == 1 then
  406. --          scan_move(1, true)
  407. --        elseif scan ~= 0 and scan ~= 1 then
  408.           move("forward")
  409. --        end
  410.       end
  411.       while config["curr_coords"]["z"] < z do
  412.         while config["curr_coords"]["direction"] ~= "north" do move("tl") end
  413. --        if scan == 0 then
  414. --          scan_move(1, false)
  415. --        elseif scan == 1 then
  416. --          scan_move(1, true)
  417. --        elseif scan ~= 0 and scan ~= 1 then
  418.           move("forward")
  419. --        end
  420.       end
  421.     end
  422.    
  423.     if tostring(y) ~= "nil" then
  424.       while config["curr_coords"]["y"] > y do
  425.         move("down")
  426.       end
  427.       while config["curr_coords"]["y"] < y do
  428.         move("up")
  429.       end
  430.     end
  431.    
  432.     if a == "north" or a == "west" or a == "south" or a == "east" then
  433.       while config["curr_coords"]["direction"] ~= a do move("tl") end
  434.     else
  435.       while config["curr_coords"]["direction"] ~= a do move("tl") end
  436.     end
  437.    
  438.     log_add("prog", "goto completed")
  439.   end
  440.  
  441.   check_full = function()
  442.     if rob.count(
  443.  
  444.   scan_move = function(...)
  445.     local params = {...}
  446.     params[1], params[2] = params[1] or 1, params[2] or false
  447.    
  448.     for num=1,params[1] do
  449.       move("forward")
  450.       dig("u")
  451.       if params[2] then find_bedrock() else dig("d") end
  452.     end
  453.   end
  454.  
  455.   setup = function()
  456.     if config["home_coords"]["direction"] == "north" then
  457.       config["quarry1_coords"]["z"] = config["home_coords"]["z"] - config["area"]
  458.       config["quarry2_coords"]["z"], config["quarry3_coords"]["z"] = config["quarry1_coords"]["z"], config["home_coords"]["z"]
  459.      
  460.       config["quarry2_coords"]["x"] = config["home_coords"]["x"] - config["area"]
  461.       config["quarry3_coords"]["x"], config["quarry1_coords"]["x"] = config["quarry2_coords"]["x"], config["home_coords"]["x"]
  462.      
  463.     elseif config["home_coords"]["direction"] == "west" then
  464.       config["quarry1_coords"]["x"] = config["home_coords"]["x"] + config["area"]
  465.       config["quarry2_coords"]["x"], config["quarry3_coords"]["x"] = config["quarry1_coords"]["x"], config["home_coords"]["x"]
  466.      
  467.       config["quarry2_coords"]["z"] = config["home_coords"]["z"] - config["area"]
  468.       config["quarry3_coords"]["z"], config["quarry1_coords"]["z"] = config["quarry2_coords"]["z"], config["home_coords"]["z"]
  469.      
  470.     elseif config["home_coords"]["direction"] == "south" then
  471.       config["quarry1_coords"]["z"] = config["home_coords"]["z"] + config["area"]
  472.       config["quarry2_coords"]["z"], config["quarry3_coords"]["z"] = config["quarry1_coords"]["z"], config["home_coords"]["z"]
  473.      
  474.       config["quarry2_coords"]["x"] = config["home_coords"]["x"] + config["area"]
  475.       config["quarry3_coords"]["x"], config["quarry1_coords"]["x"] = config["quarry2_coords"]["x"], config["home_coords"]["x"]
  476.      
  477.     elseif config["home_coords"]["direction"] == "east" then
  478.       config["quarry1_coords"]["x"] = config["home_coords"]["x"] - config["area"]
  479.       config["quarry2_coords"]["x"], config["quarry3_coords"]["x"] = config["quarry1_coords"]["x"], config["home_coords"]["x"]
  480.      
  481.       config["quarry2_coords"]["z"] = config["home_coords"]["z"] + config["area"]
  482.       config["quarry3_coords"]["z"], config["quarry1_coords"]["z"] = config["quarry2_coords"]["z"], config["home_coords"]["z"]
  483.     end
  484.   end
  485.  
  486.   combine = function(equation, variable1, variable2)
  487.     local variable2, answer = variable2 or 1, nil
  488.     if equation = "+" then answer = variable1 + variable2
  489.     elseif equation = "-" then answer = variable1 - variable2
  490.     elseif equation = "*" then answer = variable1 * variable2
  491.     elseif equation = "/" then answer = variable1 / variable2
  492.     elseif equation = "^" then answer = variable1 ^ variable2
  493.     end
  494.     return answer
  495.   end
  496.  
  497.   level = function()
  498.     goto(2, config["home_coords"]["x"], config["home_coords"]["z"], nil, config["home_coords"]["a"])
  499.     local bedr = false
  500.     if config["curr_coords"]["mining_level"] == 1 then bedr = true end
  501.     local num1, turn = 1, nil
  502.     while num1 < config["area"] do
  503.       scan_move(config["area"], bedr)
  504.       if even(num1) then turn = "tr" else turn = "tl" end
  505.       move(turn) move("forward") move(turn)
  506.       num1 = num1 + 1
  507.     end
  508.     goto(2, config["home_coords"]["x"], config["home_coords"]["z"], nil, config["home_coords"]["a"])
  509.   end
Advertisement
Add Comment
Please, Sign In to add comment