Thunder7102

Portable Miner

Aug 30th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.37 KB | None | 0 0
  1. --[[
  2.     Programmer: Noiro/Thunder7102
  3.    
  4.     Purpose:
  5.         This miner will server 2 purposes. It is primarily meant to act as a module in WorldEater.
  6.         It can be used as a standalone with multiple modes to be a nice miner.
  7. ]]
  8.  
  9. version = 0.1
  10. local debug = true
  11. --Config vars
  12. --Chunksize mined, if maxRadius = 16, he will mine a 16x16 cube
  13. local maxRadius = 16
  14. --The y level the turtle will mine up to. Bigger means there will be a bigger gap between bedrock and the ceiling
  15. local mineToY = 40
  16. --Will this turtle use a chest it dumps into or will it dump into a enderchest?
  17. local hasEnderChest = false
  18. local mode = "fast"
  19. --Static Vars
  20. local chestLoc = {0,0,0,"north"}
  21. local status = {"starting"}
  22.  
  23. --Temporary Variables
  24. local floorY = 0
  25. local tempLoc
  26. --Override determines whether the program will skip the player intro and digging and assume to start digging THERE.
  27. local override = false
  28.  
  29. --Now, if this program gets moduled, things will change
  30. --This takes parameters and changes up the variables before it gets down to runtime so reactions will be...different.
  31. if #({...}) > 0 and #({...}) < 3 then
  32.     error("Either enter 0 parameters or 3. Improper amount. You entered: "..tostring(#({...})))
  33. else
  34.     maxRadius, mineToY, hasEnderChest, floorY = ...
  35.     override = true
  36. end
  37. function debug(message)
  38.     if debug then print(message) end
  39. end
  40.  
  41. function emptyInv()
  42.   status = "emptyInv"
  43.   tempLoc = turtle.getCoordsTbl()
  44.   saveData()
  45.   if hasEnderChest then
  46.         turtle.select(1)
  47.         turtle.digUp()
  48.         turtle.placeUp()
  49.   else
  50.         turtle.fMoveTo(chestLoc,"x","z","y")
  51.   end
  52.  
  53.   for i=2, 16 do
  54.     turtle.select(i)
  55.     if turtle.refuel(1) and turtle.getItemCount(i) > 0 and turtle.getFuelLevel() < 5000 then
  56.         turtle.refuel(turtle.getItemCount(i))
  57.     else
  58.         while not turtle.dropUp(turtle.getItemCount(i)) do sleep(5) end
  59.     end
  60.   end
  61.  
  62.   --If the turtle has an enderchest, pick it back up again, if not, go to original position
  63.   if hasEnderChest then
  64.         turtle.select(1)
  65.         turtle.digUp()
  66.   else
  67.         turtle.fMoveTo(tempLoc,"y","z","x")
  68.   end
  69. end
  70.  
  71. function findBottom()
  72.   --Go down until you can't (you hit bedrock) and go 3 blocks above that
  73.   while turtle.fDown() do end
  74.  
  75.   --Go up and set that as the 'floor' you will mine at (so bedrock doesn't get in the way)
  76.   turtle.fUp(3)
  77.   floorY = turtle.getY()
  78. end
  79.  
  80.  
  81. --[[
  82.     TODO: Redo the logic for mineRadius and forward...it's clunky and overcomplicated.
  83.     Make sure persistence across reboots is available. ^^ It's more fun that way.
  84.         Port to OC when you get a chance...a port without persistence would be fun
  85. ]]
  86.  
  87. --[[
  88.     PRIMARY LOGIC - Handles most of the outline
  89. ]]
  90.  
  91. local blackListedItems = {
  92.     "minecraft:dirt",
  93.     "minecraft:gravel",
  94.     "minecraft:stone"
  95. }
  96.  
  97. function badBlock(direction)
  98.     --This ultimately is in case fastmode is enabled and blacklisted blocks are to be ignored
  99.    
  100.     --Have to include this part in case there is air
  101.     if direction == "up" and not turtle.detectUp() then
  102.         debug("Nothing above me.")
  103.         return true
  104.     elseif direction == "down" and not turtle.detectDown() then
  105.         debug("Nothing below me.")
  106.         return true
  107.     end
  108.    
  109.     if mode == "fast" then
  110.         local succ, block
  111.         --Uses dir to know which side to check
  112.         if direction == "up" then  succ, block = turtle.inspectUp()
  113.         elseif direction == "down" then succ,block = turtle.inspectDown() end
  114.        
  115.         --This is where the eval is run on if the name of the block matches the blacklist
  116.         local name = block["name"]
  117.        
  118.         for i=1, #blackListedItems do
  119.             if name == blackListedItems[i] then
  120.                 debug(block["name"].." in blacklist. Looking "..direction..".")
  121.                 return true  
  122.             end
  123.         end
  124.         return false
  125.     else
  126.         --This means the mode isn't set to fast and they want to mine out everything
  127.         return false
  128.     end
  129. end
  130.  
  131. function column(distance)
  132.   saveData()
  133.   debug("Starting forward...")
  134.   for i=1, distance do
  135.     --Checks firstly if he has room. Also, if set on fastMode, will ignore blacklisted blocks
  136.     debug("On movement "..i.."/"..distance)
  137.     if turtle.getItemCount(16) > 0 then emptyInv() end
  138.     turtle.fForward()
  139.     if turtle.getItemCount(16) > 0 then emptyInv() end
  140.     if not badBlock("up") then turtle.digUp() end
  141.     if turtle.getItemCount(16) > 0 then emptyInv() end
  142.     if not badBlock("down") then turtle.digDown() end
  143.   end
  144. end
  145.  
  146. function mineChunk(radius)
  147.     --Will mine the area with the maxRadius
  148.     status = {"chunk", "start"}
  149.     saveData()
  150.    
  151.     --Here is the big loop that manages most of the mining
  152.     local turn = "right"
  153.     for i=1,radius-1 do
  154.         debug("Starting column "..i.."/"..tostring(radius-1))
  155.         column(radius-1)
  156.        
  157.         --This part is for turning around corners and making sure to stay within bounds
  158.         if i<radius-1 then
  159.             if turn == "right" then
  160.                 turtle.turnRight()
  161.                 column(1)
  162.                 turtle.turnRight()
  163.                 turn = "left"
  164.             elseif turn == "left" then
  165.                 turtle.turnLeft()
  166.                 column(1)
  167.                 turtle.turnLeft()
  168.                 turn = "right"
  169.             end
  170.         end
  171.         --Whoo, he turned around and is about to restart...well, assuming he's supposed to
  172.     end
  173.     debug("Finished mineChunk!")
  174. end
  175.  
  176. function master()
  177.     --Ultimately manages what the turtle should be doing
  178.     if status == "emptyInv" then
  179.         --Persistence stuffs
  180.         emptyInv()
  181.     end
  182.    
  183.     --The ONLY reason this works is because emptyInv has to run first and return to the original location
  184.     while turtle.getY()+1 < mineToY do
  185.         --The turtle mines the block above him as well so that gets counted as 'done' after mineChunk()
  186.         --mineToY is how far up he is supposed to loop the minechunk
  187.         mineChunk(maxRadius)
  188.         --Go to starting point on that y-level before moving up 3.
  189.         turtle.fMoveTo(0,turtle.getY(),0,"north")
  190.         turtle.fUp(3)
  191.     end
  192.  
  193.     turtle.fMoveTo(chestLoc, "x", "z", "y")
  194.     fs.delete("startup")
  195.     if fs.exists("realStartup") then
  196.         fs.move("realStartup", "startup")
  197.     end
  198.     print("Done!")
  199. end
  200.  
  201. --[[
  202.     DATA OPERATIONS
  203. ]]
  204. function saveData()
  205.     local f = fs.open("minerTempStats", "w")
  206.     local variables = {chestLoc, mineToY, status, floorY}
  207.     f.write(textutils.serialize(variables))
  208.     f.close()
  209. end
  210.  
  211. function loadData()
  212.     local f = fs.open("minerTempStats", "r")
  213.     local variables = textutils.unserialize(f.readAll())
  214.     chestLoc, mineToY, status, floorY = unpack(variables)
  215.     f.close()
  216. end
  217.  
  218.  
  219. --[[
  220.     STARTUP
  221. ]]
  222. function loadMove()
  223.     --Loads moveAPI and puts file in appropriate folder for..subroutines if needed.
  224.     if fs.exists("move") then
  225.         fs.move("move", "api/move")
  226.  
  227.     elseif fs.exists("api/move") then
  228.     else
  229.         error("MoveAPI cannot be found. Please install it before executing this program.")
  230.     end
  231.     os.loadAPI("api/move")
  232. end
  233.  
  234. function init()
  235.     loadMove()
  236.     if args[1] == "resume" then
  237.         master()
  238.     else
  239.         setup()
  240.     end
  241. end
  242.  
  243. function setup()
  244.     --Gets all data it needs to start
  245.     while turtle.getItemCount(1) == 0 do
  246.         term.clear()
  247.         term.setCursorPos(1,1)
  248.  
  249.         while turtle.getItemCount(1) == 0 do
  250.             print("No chest found in slot one. Please try again.")
  251.             sleep(2)
  252.         end
  253.     end
  254.  
  255.     print("Starting operation...")
  256.     turtle.select(1)
  257.    
  258.     --This part is to create the chest the turtle will dump items into.
  259.     if not hasEnderChest then
  260.         turtle.placeUp()
  261.     end
  262.  
  263.     if fs.exists("startup") then fs.move("startup", "realStartup") end
  264.     f = fs.open("startup", "w")
  265.     f.writeLine("shell.run(\"miner\", \"resume\")")
  266.     findBottom()
  267.     master()
  268. end
  269.  
  270. print("THIS IS A DEVELOPMENTAL TESTING VERSION....DO NOT USE UNLESS YOU'RE HELPING DEVELOP.")
  271. --Load this when you're ready
  272. --init()
Advertisement
Add Comment
Please, Sign In to add comment