theTANCO

VeinMiner.lua

Nov 7th, 2019 (edited)
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. -- v1.7
  2. -- This program should be saved to "/VeinMiner/VeinMiner.lua"
  3. -- Run the following commands before running this program:
  4. -- > pastebin get ggZ3tdXW /API/tAddOn.lua
  5. -- > pastebin get 0C6LyNES /VeinMiner/VeinMiner.lua
  6. dofile("/API/tAddOn.lua")
  7.  
  8. function startMine()
  9.     local function loadData(path)
  10.         path = "/VeinMiner/" .. path .. ".txt"
  11.         if fs.exists(path) then
  12.             local f = fs.open(path, "r")
  13.             local data = textutils.unserialize(f.readAll())
  14.             f.close()
  15.             return data
  16.         end
  17.         return {}
  18.     end
  19.    
  20.     local tasks = loadData("tasks")
  21.     local whitelist = loadData("whitelist")
  22.     local blacklist = loadData("blacklist")
  23.  
  24.     local function saveData(data, path)
  25.         local f = fs.open("/VeinMiner/" .. path .. ".txt", "w")
  26.         f.write(textutils.serialise(data))
  27.         f.close()
  28.     end
  29.  
  30.     local function checkList(block, list)
  31.         for i = 1, #list do
  32.             if block.name == list[i].name and textutils.serialize(block.state) == textutils.serialize(list[i].state) then
  33.                 return true
  34.             end
  35.         end
  36.         return false
  37.     end
  38.  
  39.     local function queueInspect()
  40.         table.insert(tasks, 1, "inspectDown")
  41.         table.insert(tasks, 1, "inspectUp")
  42.         for i = 1, 4 do
  43.             table.insert(tasks, 1, "right")
  44.             table.insert(tasks, 1, "inspectForward")
  45.         end
  46.     end
  47.  
  48.     local function isDir(dir)
  49.         if dir == "up" then return true
  50.         elseif dir == "down" then return true
  51.         elseif dir == "forward" then return true
  52.         elseif dir == "back" then return true
  53.         elseif dir == "left" then return true
  54.         elseif dir == "right" then return true
  55.         else error("Invalid direction", 3) end
  56.     end
  57.  
  58.     local function revDir(dir)
  59.         if isDir(dir) then
  60.             if dir == "up" then return "down"
  61.             elseif dir == "down" then return "up"
  62.             elseif dir == "forward" then return "back"
  63.             elseif dir == "back" then return "forward"
  64.             elseif dir == "left" then return "right"
  65.             elseif dir == "right" then return "left"
  66.             else error("Invalid direction", 2) end
  67.         end
  68.     end
  69.  
  70.     local function mineBlock(block, dir)
  71.         local whiteCheck = checkList(block, whitelist)
  72.         local blackCheck = checkList(block, blacklist)
  73.  
  74.         if whiteCheck then
  75.             table.remove(tasks, 1)
  76.             table.insert(tasks, 1, revDir(dir))
  77.             queueInspect()
  78.             table.insert(tasks, 1, dir)
  79.             saveData(tasks, "tasks")
  80.         elseif blackCheck then
  81.             table.remove(tasks, 1)
  82.             saveData(tasks, "tasks")
  83.         else
  84.             print("\nAdd "..block.name.." to the whitelist?")
  85.             print('Press "Y" or "N"\n')
  86.             while true do
  87.                 local _, key = os.pullEvent("key")
  88.                 if key == keys.y then
  89.                     table.insert(whitelist,1,{name = block.name, state = block.state})
  90.                     saveData(whitelist, "whitelist")
  91.                     return
  92.                 elseif key == keys.n then
  93.                     table.insert(blacklist,1,{name = block.name, state = block.state})
  94.                     saveData(blacklist, "blacklist")
  95.                     return
  96.                 end
  97.             end
  98.         end
  99.     end
  100.  
  101.     local function inspectBlock(dir)
  102.         if isDir(dir) then
  103.             if dir == "up" then return turtle.inspectUp()
  104.             elseif dir == "down" then return turtle.inspectDown()
  105.             elseif dir == "forward" then return turtle.inspect()
  106.             else return false end
  107.         end
  108.     end
  109.  
  110.     queueInspect()
  111.  
  112.     repeat
  113.         local invSpace = true
  114.         local xPos, yPos = term.getCursorPos()
  115.         term.setCursorPos(1, 1)
  116.         term.clearLine()
  117.         write("Remaining Tasks: "..#tasks)
  118.         term.setCursorPos(xPos, yPos)
  119.  
  120.         repeat
  121.             local invCheck = 0
  122.             for i = 1, 16 do
  123.                 if turtle.getItemCount(i) > 0 then
  124.                     invCheck = invCheck + 1
  125.                 end
  126.             end
  127.             if invSpace and invCheck == 16 then
  128.                 print("\nOut of inventory space.\n")
  129.                 invSpace = false
  130.             end
  131.             sleep()
  132.         until invCheck < 16
  133.  
  134.         if tasks[1]:sub(1, 7) == "inspect" then
  135.             local dir = string.lower(tasks[1]:sub(8))
  136.             local check, block = inspectBlock(dir)
  137.             if check then mineBlock(block, dir)
  138.             else
  139.                 table.remove(tasks, 1)
  140.                 saveData(tasks, "tasks")
  141.             end
  142.         elseif isDir(tasks[1]) then
  143.             local dir = tasks[1]
  144.             local check, block = inspectBlock(dir)
  145.             tMove[dir]()
  146.             if check then
  147.                 print("Mined " .. block.name .. ".")
  148.             end
  149.             table.remove(tasks, 1)
  150.             saveData(tasks, "tasks")
  151.         end
  152.     until #tasks == 0
  153.     fs.delete("/VeinMiner/tasks.txt")
  154. end
  155.  
  156. if arg[1] == "startMine" then
  157.     startMine()
  158. elseif arg[1] == "clearTasks" and fs.exists("/VeinMiner/tasks.txt") then
  159.     fs.delete("/VeinMiner/tasks.txt")
  160. end
  161.  
  162. --[[ Changelog
  163. 2025/12/13 - v1.7:
  164. • Swapped task order so inspecting forward is done first.
  165.  
  166. 2025/12/12 - v1.6:
  167. • Added arguments that can be passed to the program without needing to run
  168.   another program.
  169. • "startMine" will start the vein mining program.
  170. • "clearTasks" will delete the save file for queued tasks.
  171. • Added comment with command that can be copy pasted to download this
  172.   program.
  173.  
  174. 2025/12/08 - v1.5:
  175. I don't know what the hell happened since the last time I updated this program.
  176. This hasn't been edited since the last time I updated it but it feels like
  177. someone is messing with me with how messed up this is. This was all working fine
  178. last time I used it, but now all of a sudden all of this code was wrong.
  179. • Fixed typo at line 109: 'queueInpsect()' → 'queueInspect()'.
  180. • Fixed missing argument for 'isDir(dir)' in 'inspectBlock(dir)'.
  181. • Fixed missing argument for 'isDir(dir)' in 'revDir(dir)'.
  182. • Fixed file path string being wrong when loading data.
  183. • Fixed turtle only mining forward when not inspecting forward at line 136.
  184. • Fixed queueInspect adding tasks to the end of the tasks list instead of the
  185.   start.
  186. Maybe I trolled myself somehow.
  187.  
  188. 2023/05/04 - v1.4:
  189. • Fixed a bug caused by a typo in the isDir function.
  190.  
  191. 2023/04/18 - v1.3:
  192. • Code has been cleaned up a lot. Similar instructions have been grouped into
  193.   functions.
  194. • 'dofile()' can be used to do the same thing as 'require()' and is backwards
  195.   compatible.
  196. • Added a requirement for a new API to control turtle movement.
  197. • Task list, whitelist, and blacklist are now saved to '/VeinMiner/'. This
  198.   program should also be saved to this directory.
  199. • Reformatted the changelog to comply with the formatting convention used in my
  200.   other programs.
  201.  
  202. 2022/06/20 - v1.2:
  203. • Encapsulated the program into a function that can be called in other programs
  204.   when using os.loadAPI() or require().
  205. • Blacklist and Whitelist now only saves the blocks' name and state, ignoring
  206.   nbt tags.
  207. ]]
  208.  
Advertisement
Add Comment
Please, Sign In to add comment