Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- v1.4
- -- This program should be saved to "/VeinMiner/VeinMiner.lua"
- -- Run the following command before running this program:
- -- > pastebin get ggZ3tdXW /API/tAddOn.lua
- dofile("/API/tAddOn.lua")
- function startMine()
- local tasks, whitelist, blacklist
- local function loadData(path)
- if fs.exists(path) then
- local f = fs.open("/VeinMiner/" .. path .. ".txt", "r")
- local data = textutils.unserialize(f.readAll())
- f.close()
- return data
- end
- return {}
- end
- local function saveData(data, path)
- local f = fs.open("/VeinMiner/" .. path .. ".txt", "w")
- f.write(textutils.serialise(data))
- f.close()
- end
- local function checkList(block, list)
- for i = 1, #list do
- if block.name == list[i].name and textutils.serialize(block.state) == textutils.serialize(list[i].state) then
- return true
- end
- end
- return false
- end
- local function queueInspect()
- table.insert(tasks, 1, "inspectDown")
- table.insert(tasks, 1, "inspectUp")
- for i = 1, 4 do
- table.insert(tasks, #tasks+1, "inspectForward")
- table.insert(tasks, #tasks+1, "right")
- end
- end
- local function isDir(dir)
- if dir == "up" then return true
- elseif dir == "down" then return true
- elseif dir == "forward" then return true
- elseif dir == "back" then return true
- elseif dir == "left" then return true
- elseif dir == "right" then return true
- else error("Invalid direction", 3) end
- end
- local function revDir(dir)
- if isDir() then
- if dir == "up" then return "down"
- elseif dir == "down" then return "up"
- elseif dir == "forward" then return "back"
- elseif dir == "back" then return "forward"
- elseif dir == "left" then return "right"
- elseif dir == "right" then return "left"
- else error("Invalid direction", 2) end
- end
- end
- local function mineBlock(block, dir)
- local whiteCheck = checkList(block, whitelist)
- local blackCheck = checkList(block, blacklist)
- if whiteCheck then
- table.remove(tasks, 1)
- table.insert(tasks, 1, revDir(dir))
- queueInspect()
- table.insert(tasks, 1, dir)
- saveData(tasks, "tasks")
- elseif blackCheck then
- table.remove(tasks, 1)
- saveData(tasks, "tasks")
- else
- print("\nAdd "..block.name.." to the whitelist?")
- print('Press "Y" or "N"\n')
- while true do
- local _, key = os.pullEvent("key")
- if key == keys.y then
- table.insert(whitelist,1,{name = block.name, state = block.state})
- saveData(whitelist, "whitelist")
- return
- elseif key == keys.n then
- table.insert(blacklist,1,{name = block.name, state = block.state})
- saveData(blacklist, "blacklist")
- return
- end
- end
- end
- end
- local function inspectBlock(dir)
- if isDir() then
- if dir == "up" then return turtle.inspectUp()
- elseif dir == "down" then return turtle.inspectDown()
- elseif dir == "forward" then return turtle.inspect()
- else return false end
- end
- end
- tasks = loadData("/VeinMiner/tasks.txt")
- whitelist = loadData("/VeinMiner/whitelist.txt")
- blacklist = loadData("/VeinMiner/blacklist.txt")
- queueInpsect()
- repeat
- local invSpace = true
- local xPos, yPos = term.getCursorPos()
- term.setCursorPos(1, 1)
- term.clearLine()
- write("Remaining Tasks: "..#tasks)
- term.setCursorPos(xPos, yPos)
- repeat
- local invCheck = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- invCheck = invCheck + 1
- end
- end
- if invSpace and invCheck == 16 then
- print("\nOut of inventory space.\n")
- invSpace = false
- end
- sleep()
- until invCheck < 16
- if tasks[1]:sub(1, 7) == "inspect" then
- local dir = string.lower(tasks[1]:sub(8))
- local check, block = inspectBlock(dir)
- if check then mineBlock(block, "forward")
- else
- table.remove(tasks, 1)
- saveData(tasks, "tasks")
- end
- elseif isDir(tasks[1]) then
- local dir = tasks[1]
- local check, block = inspectBlock(dir)
- tMove[dir]()
- if check then
- print("Mined " .. block.name .. ".")
- end
- table.remove(tasks, 1)
- saveData(tasks, "tasks")
- end
- until #tasks == 0
- fs.delete("/VeinMiner/tasks.txt")
- end
- --[[ Changelog
- 2023/05/04 - v1.4:
- • Fixed a bug caused by a typo in the isDir function.
- 2023/04/18 - v1.3:
- • Code has been cleaned up a lot. Similar instructions have been grouped into
- functions.
- • 'dofile()' can be used to do the same thing as 'require()' and is backwards
- compatible.
- • Added a requirement for a new API to control turtle movement.
- • Task list, whitelist, and blacklist are now saved to '/VeinMiner/'. This
- program should also be saved to this directory.
- • Reformatted the changelog to comply with the formatting convention used in my
- other programs.
- 2022/06/20 - v1.2:
- • Encapsulated the program into a function that can be called in other programs
- when using os.loadAPI() or require().
- • Blacklist and Whitelist now only saves the blocks' name and state, ignoring
- nbt tags.
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement