Advertisement
theTANCO

VeinMiner.lua

Nov 7th, 2019 (edited)
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1. -- v1.4
  2. -- This program should be saved to "/VeinMiner/VeinMiner.lua"
  3. -- Run the following command before running this program:
  4. -- > pastebin get ggZ3tdXW /API/tAddOn.lua
  5. dofile("/API/tAddOn.lua")
  6.  
  7. function startMine()
  8.     local tasks, whitelist, blacklist
  9.  
  10.     local function loadData(path)
  11.         if fs.exists(path) then
  12.             local f = fs.open("/VeinMiner/" .. path .. ".txt", "r")
  13.             local data = textutils.unserialize(f.readAll())
  14.             f.close()
  15.             return data
  16.         end
  17.         return {}
  18.     end
  19.  
  20.     local function saveData(data, path)
  21.         local f = fs.open("/VeinMiner/" .. path .. ".txt", "w")
  22.         f.write(textutils.serialise(data))
  23.         f.close()
  24.     end
  25.  
  26.     local function checkList(block, list)
  27.         for i = 1, #list do
  28.             if block.name == list[i].name and textutils.serialize(block.state) == textutils.serialize(list[i].state) then
  29.                 return true
  30.             end
  31.         end
  32.         return false
  33.     end
  34.  
  35.     local function queueInspect()
  36.         table.insert(tasks, 1, "inspectDown")
  37.         table.insert(tasks, 1, "inspectUp")
  38.         for i = 1, 4 do
  39.             table.insert(tasks, #tasks+1, "inspectForward")
  40.             table.insert(tasks, #tasks+1, "right")
  41.         end
  42.     end
  43.  
  44.     local function isDir(dir)
  45.         if dir == "up" then return true
  46.         elseif dir == "down" then return true
  47.         elseif dir == "forward" then return true
  48.         elseif dir == "back" then return true
  49.         elseif dir == "left" then return true
  50.         elseif dir == "right" then return true
  51.         else error("Invalid direction", 3) end
  52.     end
  53.  
  54.     local function revDir(dir)
  55.         if isDir() then
  56.             if dir == "up" then return "down"
  57.             elseif dir == "down" then return "up"
  58.             elseif dir == "forward" then return "back"
  59.             elseif dir == "back" then return "forward"
  60.             elseif dir == "left" then return "right"
  61.             elseif dir == "right" then return "left"
  62.             else error("Invalid direction", 2) end
  63.         end
  64.     end
  65.  
  66.     local function mineBlock(block, dir)
  67.         local whiteCheck = checkList(block, whitelist)
  68.         local blackCheck = checkList(block, blacklist)
  69.  
  70.         if whiteCheck then
  71.             table.remove(tasks, 1)
  72.             table.insert(tasks, 1, revDir(dir))
  73.             queueInspect()
  74.             table.insert(tasks, 1, dir)
  75.             saveData(tasks, "tasks")
  76.         elseif blackCheck then
  77.             table.remove(tasks, 1)
  78.             saveData(tasks, "tasks")
  79.         else
  80.             print("\nAdd "..block.name.." to the whitelist?")
  81.             print('Press "Y" or "N"\n')
  82.             while true do
  83.                 local _, key = os.pullEvent("key")
  84.                 if key == keys.y then
  85.                     table.insert(whitelist,1,{name = block.name, state = block.state})
  86.                     saveData(whitelist, "whitelist")
  87.                     return
  88.                 elseif key == keys.n then
  89.                     table.insert(blacklist,1,{name = block.name, state = block.state})
  90.                     saveData(blacklist, "blacklist")
  91.                     return
  92.                 end
  93.             end
  94.         end
  95.     end
  96.  
  97.     local function inspectBlock(dir)
  98.         if isDir() then
  99.             if dir == "up" then return turtle.inspectUp()
  100.             elseif dir == "down" then return turtle.inspectDown()
  101.             elseif dir == "forward" then return turtle.inspect()
  102.             else return false end
  103.         end
  104.     end
  105.  
  106.     tasks = loadData("/VeinMiner/tasks.txt")
  107.     whitelist = loadData("/VeinMiner/whitelist.txt")
  108.     blacklist = loadData("/VeinMiner/blacklist.txt")
  109.     queueInpsect()
  110.  
  111.     repeat
  112.         local invSpace = true
  113.         local xPos, yPos = term.getCursorPos()
  114.         term.setCursorPos(1, 1)
  115.         term.clearLine()
  116.         write("Remaining Tasks: "..#tasks)
  117.         term.setCursorPos(xPos, yPos)
  118.  
  119.         repeat
  120.             local invCheck = 0
  121.             for i = 1, 16 do
  122.                 if turtle.getItemCount(i) > 0 then
  123.                     invCheck = invCheck + 1
  124.                 end
  125.             end
  126.             if invSpace and invCheck == 16 then
  127.                 print("\nOut of inventory space.\n")
  128.                 invSpace = false
  129.             end
  130.             sleep()
  131.         until invCheck < 16
  132.  
  133.         if tasks[1]:sub(1, 7) == "inspect" then
  134.             local dir = string.lower(tasks[1]:sub(8))
  135.             local check, block = inspectBlock(dir)
  136.             if check then mineBlock(block, "forward")
  137.             else
  138.                 table.remove(tasks, 1)
  139.                 saveData(tasks, "tasks")
  140.             end
  141.         elseif isDir(tasks[1]) then
  142.             local dir = tasks[1]
  143.             local check, block = inspectBlock(dir)
  144.             tMove[dir]()
  145.             if check then
  146.                 print("Mined " .. block.name .. ".")
  147.             end
  148.             table.remove(tasks, 1)
  149.             saveData(tasks, "tasks")
  150.         end
  151.     until #tasks == 0
  152.     fs.delete("/VeinMiner/tasks.txt")
  153. end
  154.  
  155. --[[ Changelog
  156. 2023/05/04 - v1.4:
  157. • Fixed a bug caused by a typo in the isDir function.
  158.  
  159. 2023/04/18 - v1.3:
  160. • Code has been cleaned up a lot. Similar instructions have been grouped into
  161.   functions.
  162. • 'dofile()' can be used to do the same thing as 'require()' and is backwards
  163.   compatible.
  164. • Added a requirement for a new API to control turtle movement.
  165. • Task list, whitelist, and blacklist are now saved to '/VeinMiner/'. This
  166.   program should also be saved to this directory.
  167. • Reformatted the changelog to comply with the formatting convention used in my
  168.   other programs.
  169.  
  170. 2022/06/20 - v1.2:
  171. • Encapsulated the program into a function that can be called in other programs
  172.   when using os.loadAPI() or require().
  173. • Blacklist and Whitelist now only saves the blocks' name and state, ignoring
  174.   nbt tags.
  175. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement