Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LOGGING --
- local fs = require("filesystem")
- local dir_main = "/floppy/OREQUARRY" if fs.exists(dir_main) ~= true then fs.makeDirectory(dir_main) end
- local dir_logs = dir_main.."/LOGS" if fs.exists(dir_logs) ~= true then fs.makeDirectory(dir_logs) end
- local coord_log = dir_logs.."/coord" if fs.exists(coord_log) then fs.remove(coord_log) end
- local prog_log = dir_logs.."/prog" if fs.exists(prog_log) then fs.remove(prog_log) end
- local error_log = dir_logs.."/error"
- do
- log_add = function(...)
- local params = {...}
- local log_loc, log_text, log_error, error_lvl = nil, nil, nil, 0
- if params[1] == "coord" then
- log_text, log_loc = tostring("x: "..params[2]..". y: "..params[3]..". z: "..params[4]..". a: "..params[5]..". mining level: "..params[6]), coord_log
- elseif params[1] == "prog" then
- log_text, log_loc, log_error, error_lvl = tostring(params[2]), prog_log, params[3] or nil, params[4] or 0
- end
- if params[1] == "error" then
- log_text, log_loc = tostring("Error: "..params[2].."-"..(params[4] or 0), error_log
- local fh = io.open(log_loc, 'a')
- fh:write(log_text:upper()) fh:write("\n") fh:close()
- end
- if tostring(log_error) ~= "nil" or error_lvl ~= 0 then
- log_text = tostring("--Error: "..log_text.."-"..error_lvl)
- end
- local fh = io.open(log_loc, 'a')
- fh:write(log_text:upper())
- fh:write("\n")
- fh:close()
- end
- end
- -- SAVING & LOADING --
- local serial = require("serialization")
- local settings_file = "CONFIG"
- do
- load_one = function(command)
- log_add("prog", "loading: "..command)
- if fs.exists(tostring(dir_main.."/"..command)) then
- local fh = io.open(dir_main.."/"..command, 'r')
- return serial.unserialize(fh.read())
- end
- end
- save_one = function(command1, command2)
- local fh = io.open(tostring(dir_main.."/"..command1), 'w')
- fh:write(serial.serialize(command2))
- fh:close()
- log_add("prog", "Saving: "..command1)
- end
- end
- -- CONFIGS --
- local term, comp, rob, gen, solar, pwrsource, inv, cos = require("term"), require("component"), nil, nil, nil, false, nil, require("computer")
- local config = {["home_coords"] = {["x"] = 0,
- ["y"] = 0,
- ["z"] = 0,
- ["a"] = 0
- ["direction"] = "north"},
- ["quarry1_coords"] = {["x"] = 0, ["z"] = 0},
- ["quarry2_coords"] = {["x"] = 0, ["z"] = 0},
- ["quarry3_coords"] = {["x"] = 0, ["z"] = 0},
- ["curr_coords"] = {["x"] = 0,
- ["y"] = 0,
- ["z"] = 0,
- ["a"] = 0,
- ["direction"] = "north",
- ["mining_level"] = 0},
- ["bedrock"] = {["flat"] = false,
- ["y"] = -100},
- ["noise_bocks"] = 0,
- ["clearing"] = false,
- ["fuel"] = {["slot"] = 1,
- ["lower"] = 10,
- ["upper"] = 90},
- ["enderchest"] = {["output"] = false,
- ["fuel"] = false}
- ["tool"] = {["auto_repair"] = false,
- ["upper"] = 30,
- ["lower"] = 10,
- ["slot"] = 1},
- ["solar"] = false,
- ["slots"] = {["lower"] = 2,
- ["upper"] = 16},
- ["area"] = 14,
- ["dumping"] = false,
- ["maxslots"] = 16,
- ["gps"] = false,
- }
- local initial_setup = nil
- if fs.exists(dir_main.."/"..settings_file) ~= true then
- initial_setup = {["questions"] = "area to mine? (recommend 14 with robot 1 block in from the chunk borders)"
- "is the bedrock flat? y/n",
- "am i to gather ores or clear all blocks? y/n",
- "will i be ouputting into an enderchest? (slot 2) y/n",
- "will i be taking fuel from an enderchest? (slot 3) y/n",
- "do i have solar generator? y/n",
- "does my pick have auto-repair? y/n",
- "dispose of extra noise blocks? (if not silk touch: place cobble in the noise block section) y/n"},
- ["answers"] = nil}
- for num=1,table.maxn(initial_setup["questions"]) do
- term.clear()
- print(initial_setup["questions"][num])
- log_add("prog", "initial setup q:-"..num)
- initial_setup["answers"][num] = term.read()
- log_add("prog", "initial setup a:-"..initial_setup["answers"][num])
- end
- for num=2,table.maxn(initial_setup["answers"]) do
- if initial_setup["answers"][num] == "y" then
- initial_setup["answers"][num] = true
- else
- initial_setup["answers"][num] = false
- end
- log_add("prog", "initial setup boolean a:-"..initial_setup["answers"][num])
- end
- config["area"] = tonumber(initial_setup["answers"][1]) or 14 log_add("prog", tostring("mining area = "..config["area"]))
- config["bedrock"]["flat"] = initial_setup["answers"][2] or false log_add("prog", tostring("flat bedrock = "..config["bedrock"]["flat"]))
- config["clearing"] = not initial_setup["answers"][3] or false log_add("prog", tostring("clearing = "..config["clearing"]))
- config["enderchest"]["output"] = initial_setup["answers"][4] or false log_add("prog", tostring("enderchest 1 = "..config["enderchest"]["output"]))
- config["enderchest"]["fuel"] = initial_setup["answers"][5] or false log_add("prog", tostring("enderchest 2 = "..config["enderchest"]["fuel"]))
- config["solar"] = tonumber(initial_setup["answers"][6]) or false log_add("prog", tostring("solar = ".. config["solar"]))
- config["tool"]["auto_repair"] = initial_setup["answers"][7] or false log_add("prog", tostring("tool = "..config["tool"]["auto_repair"]))
- config["dumping"] = initial_setup["answers"][8] or false log_add("prog", tostring("dumping = "..config["dumping"]))
- local noise, num2, num3 = 0, 1, 4
- for num=1,64 do config["maxslots"] = rob.select(num) end
- while num2 ~= 0 or tostring(num2) ~= "nil" do num3 = num3 + 1 num2 = rob.count(num3) end
- config["noise_bocks"] = num3 log_add("prog", tostring("noise blocks = "..config["noise_bocks"]))
- if config["noise_bocks"] < 1 then log_add("error", "no noise blocks", true, 2) end
- save_one(settings_file, config)
- else
- config = load_one("CONFIG")
- end
- if comp.isAvailable("robot") then rob = require("robot") else log_add("error", "computer isn't a robot", true, 3) end
- if rob.durability() == nil then log_add("error", "no pick", true, 2) end
- if comp.isAvailable("generator") then gen = comp.generator pwrsource = true else log_add("error", "no generator", true, 1) end
- if config["solar"] == false and pwrsource == false then log_add("error", "no powersources", true, 3) end
- if tostring(gen) ~= "nil" then
- 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
- end
- 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
- 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
- -- BASIC FUNCTIONS --
- local direction = {}
- if config["gps"] then
- direction = {[1] = "north", [2] = "west", [3] = "south", [4] = "east"}
- else
- direction = {[3] = "north", [5] = "west", [2] = "south", [4] = "east"}
- end
- do
- dig = function(...)
- local params = {...}
- local dura = {rob.durability()}
- if dura[1] <= config["tool"]["lower"]
- while dura[1] <= config["tool"]["upper"] do
- rob.select(config["tool"]["slot"])
- inv.equip()
- dura = {rob.durability()}
- rob.select(1)
- end
- end
- if params[1] == "u" then
- if rob.detectUp() then
- if params[2] or config["clearing"] then
- rob.swingUp()
- else
- local noise = false
- for num=4,config["noise_bocks"] do
- if noise ~= true then
- rob.select(num)
- noise = rob.compareUp()
- rob.select(1)
- end
- end
- if noise ~= true then rob.swingUp() end
- end
- end
- elseif params[1] == "f" then
- if rob.detect() then
- if params[2] or config["clearing"] then
- rob.swing()
- else
- local noise = false
- for num=4,config["noise_bocks"] do
- if noise ~= true then
- rob.select(num)
- noise = rob.compare()
- rob.select(1)
- end
- end
- if noise ~= true then rob.swing() end
- end
- end
- elseif params[1] == "d" then
- if rob.detectDown() then
- if params[2] or config["clearing"] then
- rob.swingDown()
- else
- local noise = false
- for num=4,config["noise_bocks"] do
- if noise ~= true then
- rob.select(num)
- noise = rob.compareDown()
- rob.select(1)
- end
- end
- if noise ~= true then rob.swingDown() end
- end
- end
- end
- end
- fuel = function()
- local powerlvl = math.floor((cos.energy() / cos.maxEnergy()) * 100)
- if tonumber(powerlvl) <= tonumber(config["fuel"]["lower"]) then
- while powerlvl <= tonumber(config["fuel"]["upper"]) then
- if tonumber(gen.count()) ~= 1 then
- if rob.count(config["fuel"]["slot"]) < 2 then
- rob.select(config["enderchest"]["fuel"])
- local chest_place = rob.placeUp()
- while chest_place ~= true do
- chest_place = rob.placeUp()
- dig("u", true)
- end
- rob.select(config["fuel"]["slot"])
- rob.suckUp(63)
- rob.select(config["enderchest"]["fuel"])
- rob.swingUp()
- end
- rob.select(config["fuel"]["slot"])
- if rob.count ~= 1 then gen.insert(1) end
- rob.select(1)
- end
- powerlvl = math.floor((cos.energy() / cos.maxEnergy()) * 100)
- end
- end
- end
- gps_update = function()
- config["curr_coords"]["x"], config["curr_coords"]["y"], config["curr_coords"]["z"] = gps.getPosition()
- config["curr_coords"]["a"] = direction[gps.getFacing()]
- save_one(settings_file, config)
- end
- move = function(...)
- local params, num = {...}, 270
- fuel()
- if params[1] == "up" then
- local moved = rob.up()
- while moved ~= true and num ~= 0 do num = num - 1 moved = rob.up() dig("u", true) end
- if num ~= 0 then
- if config["gps"] then gps_update() else config["curr_coords"]["y"] = config["curr_coords"]["y"] + 1 end
- else
- log_add("error", "move break up", true, 1)
- end
- elseif params[1] == "down" then
- local moved = rob.down()
- while moved ~= true and num ~= 0 do num = num - 1 moved = rob.down() dig("d", true) end
- if num ~= 0 then
- if config["gps"] then gps_update() else config["curr_coords"]["y"] = config["curr_coords"]["y"] - 1 end
- else
- log_add("error", "move break down", true, 1)
- end
- elseif params[1] == "forward" then
- local moved = rob.forward()
- while moved ~= true and num ~= 0 do num = num - 1 moved = rob.forward() dig("f", true) end
- if num ~= 0 then
- if config["gps"] then
- gps_update()
- else
- if config["curr_coords"]["a"] == "north" then config["curr_coords"]["z"] = config["curr_coords"]["z"] + 1
- elseif config["curr_coords"]["a"] == "west" then config["curr_coords"]["x"] = config["curr_coords"]["x"] - 1
- elseif config["curr_coords"]["a"] == "south" then config["curr_coords"]["z"] = config["curr_coords"]["z"] - 1
- elseif config["curr_coords"]["a"] == "east" then config["curr_coords"]["x"] = config["curr_coords"]["x"] + 1
- end
- end
- else
- log_add("error", "move break forward", true, 1)
- end
- elseif params[1] == "tl" then
- rob.turnLeft()
- if config["gps"] then
- gps_update()
- else
- config["curr_coords"]["a"] = config["curr_coords"]["a"] + 1
- if config["curr_coords"]["a"] == 5 then config["curr_coords"]["a"] = 1 end
- config["curr_coords"]["direction"] = direction[config["curr_coords"]["a"]]
- end
- elseif params[1] == "tr" then
- rob.turnRight()
- if config["gps"] then
- gps_update()
- else
- config["curr_coords"]["a"] = config["curr_coords"]["a"] - 1
- if config["curr_coords"]["a"] == 0 then config["curr_coords"]["a"] = 4 end
- config["curr_coords"]["direction"] = direction[config["curr_coords"]["a"]]
- end
- end
- end
- find_bedrock = function(...)
- local params, bedrock = {...}, true
- params[1] = params[1] or false
- if params[1] then log_add("prog", "finding bedrock") else log_add("prog", "digging to bedrock") end
- while bedrock do
- if rob.detectDown() then bedrock = rob.swingDown() end
- move("down")
- end
- if params[1] then
- if config["gps"] then gps_update() end
- config["bedrock"]["y"] = config["curr_coords"]["y"]
- log_add("prog", tostring("bedrock located @: "..config["curr_coords"]["y"]))
- end
- end
- -- scan_move = function(...)
- -- local params = {...}
- -- params[1], params[2] = params[1] or 1, params[2] or false
- --
- -- for num=1,params[1] do
- -- move("forward")
- -- dig("u")
- -- if params[2] then find_bedrock() else dig("d") end
- -- end
- -- end
- even = function(...)
- local params = {...}
- params[4] = false
- if type(params[1]) == "number" then
- params[2] = params[1] / 2
- params[3] = params[2] - math.floor(params[2])
- if params[3] ~= 0 and tostring(params[3]) ~= "nil" then params[4] = false else params[4] = true end
- end
- return params[4]
- end
- goto = function(scan, x, y, z, a)
- log_add("prog", "going to: "..tostring(x.." "..y.." "..z.." "..a))
- local angle = config["curr_coords"]["direction"]
- if tostring(x) ~= "nil" then
- while config["curr_coords"]["x"] > x do
- while config["curr_coords"]["direction"] ~= "west" do move("tl") end
- -- if scan == 0 then
- -- scan_move(1, false)
- -- elseif scan == 1 then
- -- scan_move(1, true)
- -- elseif scan ~= 0 and scan ~= 1 then
- move("forward")
- -- end
- end
- while config["curr_coords"]["x"] < x do
- while config["curr_coords"]["direction"] ~= "east" do move("tl") end
- -- if scan == 0 then
- -- scan_move(1, false)
- -- elseif scan == 1 then
- -- scan_move(1, true)
- -- elseif scan ~= 0 and scan ~= 1 then
- move("forward")
- -- end
- end
- end
- if tostring(z) ~= "nil" then
- while config["curr_coords"][" z"] > z do
- while config["curr_coords"]["direction"] ~= "south" do move("tl") end
- -- if scan == 0 then
- -- scan_move(1, false)
- -- elseif scan == 1 then
- -- scan_move(1, true)
- -- elseif scan ~= 0 and scan ~= 1 then
- move("forward")
- -- end
- end
- while config["curr_coords"]["z"] < z do
- while config["curr_coords"]["direction"] ~= "north" do move("tl") end
- -- if scan == 0 then
- -- scan_move(1, false)
- -- elseif scan == 1 then
- -- scan_move(1, true)
- -- elseif scan ~= 0 and scan ~= 1 then
- move("forward")
- -- end
- end
- end
- if tostring(y) ~= "nil" then
- while config["curr_coords"]["y"] > y do
- move("down")
- end
- while config["curr_coords"]["y"] < y do
- move("up")
- end
- end
- if a == "north" or a == "west" or a == "south" or a == "east" then
- while config["curr_coords"]["direction"] ~= a do move("tl") end
- else
- while config["curr_coords"]["direction"] ~= a do move("tl") end
- end
- log_add("prog", "goto completed")
- end
- check_full = function()
- if rob.count(
- scan_move = function(...)
- local params = {...}
- params[1], params[2] = params[1] or 1, params[2] or false
- for num=1,params[1] do
- move("forward")
- dig("u")
- if params[2] then find_bedrock() else dig("d") end
- end
- end
- setup = function()
- if config["home_coords"]["direction"] == "north" then
- config["quarry1_coords"]["z"] = config["home_coords"]["z"] - config["area"]
- config["quarry2_coords"]["z"], config["quarry3_coords"]["z"] = config["quarry1_coords"]["z"], config["home_coords"]["z"]
- config["quarry2_coords"]["x"] = config["home_coords"]["x"] - config["area"]
- config["quarry3_coords"]["x"], config["quarry1_coords"]["x"] = config["quarry2_coords"]["x"], config["home_coords"]["x"]
- elseif config["home_coords"]["direction"] == "west" then
- config["quarry1_coords"]["x"] = config["home_coords"]["x"] + config["area"]
- config["quarry2_coords"]["x"], config["quarry3_coords"]["x"] = config["quarry1_coords"]["x"], config["home_coords"]["x"]
- config["quarry2_coords"]["z"] = config["home_coords"]["z"] - config["area"]
- config["quarry3_coords"]["z"], config["quarry1_coords"]["z"] = config["quarry2_coords"]["z"], config["home_coords"]["z"]
- elseif config["home_coords"]["direction"] == "south" then
- config["quarry1_coords"]["z"] = config["home_coords"]["z"] + config["area"]
- config["quarry2_coords"]["z"], config["quarry3_coords"]["z"] = config["quarry1_coords"]["z"], config["home_coords"]["z"]
- config["quarry2_coords"]["x"] = config["home_coords"]["x"] + config["area"]
- config["quarry3_coords"]["x"], config["quarry1_coords"]["x"] = config["quarry2_coords"]["x"], config["home_coords"]["x"]
- elseif config["home_coords"]["direction"] == "east" then
- config["quarry1_coords"]["x"] = config["home_coords"]["x"] - config["area"]
- config["quarry2_coords"]["x"], config["quarry3_coords"]["x"] = config["quarry1_coords"]["x"], config["home_coords"]["x"]
- config["quarry2_coords"]["z"] = config["home_coords"]["z"] + config["area"]
- config["quarry3_coords"]["z"], config["quarry1_coords"]["z"] = config["quarry2_coords"]["z"], config["home_coords"]["z"]
- end
- end
- combine = function(equation, variable1, variable2)
- local variable2, answer = variable2 or 1, nil
- if equation = "+" then answer = variable1 + variable2
- elseif equation = "-" then answer = variable1 - variable2
- elseif equation = "*" then answer = variable1 * variable2
- elseif equation = "/" then answer = variable1 / variable2
- elseif equation = "^" then answer = variable1 ^ variable2
- end
- return answer
- end
- level = function()
- goto(2, config["home_coords"]["x"], config["home_coords"]["z"], nil, config["home_coords"]["a"])
- local bedr = false
- if config["curr_coords"]["mining_level"] == 1 then bedr = true end
- local num1, turn = 1, nil
- while num1 < config["area"] do
- scan_move(config["area"], bedr)
- if even(num1) then turn = "tr" else turn = "tl" end
- move(turn) move("forward") move(turn)
- num1 = num1 + 1
- end
- goto(2, config["home_coords"]["x"], config["home_coords"]["z"], nil, config["home_coords"]["a"])
- end
Advertisement
Add Comment
Please, Sign In to add comment