Advertisement
Sv443

ComputerCraft Turtle Certus Quartz Farm - Crystal Miner

Dec 3rd, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | Gaming | 0 0
  1. -- Whether to mine only fully grown certus quartz crystals
  2. -- If set to false, the tiny crystals will be harvested instantly
  3. -- thus giving certus dust instead of crystals
  4. MINE_FULLY_GROWN = true
  5.  
  6. -- Delay between mining attempts and turning in seconds
  7. -- Has to be a multiple of 0.05 (1 game tick)
  8. DELAY = 0.5
  9.  
  10. -- The initial direction the turtle should turn after mining
  11. -- the crystal it is already facing
  12. -- 1 = clockwise, -1 = counter-clockwise
  13. INITIAL_TURN_DIRECTION = 1
  14. -- The file where the turtle stores its current facing
  15. FACING_FILE = "/facing.txt"
  16.  
  17. -- After how many times of turning the
  18. -- current stats should be printed
  19. STATS_PRINT_AFTER = 50
  20. -- The file where the mining stats are stored
  21. STATS_FILE = "/stats.txt"
  22.  
  23. -- The *vertical* position where the turtle should drop off
  24. -- all items it has collected (can be "top" or "bottom")
  25. DROPOFF_POS = "top"
  26.  
  27. -- Which blocks are allowed to be mined
  28. -- This only needs to be changed in case you are using an older
  29. -- (or newer for that matter) version of AE2
  30. MINABLE_BLOCK_NAMES = MINE_FULLY_GROWN and { "ae2:quartz_cluster" } or { "ae2:small_quartz_bud", "ae2:medium_quartz_bud", "ae2:large_quartz_bud", "ae2:quartz_cluster" }
  31.  
  32.  
  33. -- DO NOT EDIT BELOW THIS LINE OR TURTLE GETS ANGY >:(
  34.  
  35. local curFacing = 0
  36. local turnCycle = INITIAL_TURN_DIRECTION
  37. local minedBlocks = 0
  38. local itemsDeposited = 0
  39. local loopCount = 0
  40.  
  41. -- #SECTION mining
  42.  
  43. function loop()
  44.     -- mine
  45.     local hasBlock, detail = turtle.inspect()
  46.     if hasBlock then
  47.         for i = 1, #MINABLE_BLOCK_NAMES do
  48.             if detail.name == MINABLE_BLOCK_NAMES[i] then
  49.                 if turtle.dig() then
  50.                     minedBlocks = minedBlocks + 1
  51.                 end
  52.                 break
  53.             end
  54.         end
  55.     end
  56.     -- deposit items
  57.     local itemDet = turtle.getItemDetail(1)
  58.     if itemDet ~= nil then
  59.         if itemDet.count > 0 then
  60.             depositItems()
  61.             itemsDeposited = itemsDeposited + itemDet.count
  62.         end
  63.     end
  64.  
  65.     -- turn
  66.     turn(turnCycle == 1 and "right" or "left")
  67.     turnCycle = turnCycle * -1
  68.  
  69.     -- update & print stats
  70.     updateStats()
  71.     if loopCount % STATS_PRINT_AFTER == 0 then
  72.         printStats()
  73.     end
  74.  
  75.     -- update loop count
  76.     loopCount = loopCount + 1
  77. end
  78.  
  79. -- Drops all items from the first slot
  80. function depositItems()
  81.     turtle.select(1)
  82.     if DROPOFF_POS == "top" then
  83.         turtle.dropUp()
  84.     elseif DROPOFF_POS == "bottom" then
  85.         turtle.dropDown()
  86.     end
  87. end
  88.  
  89. -- #SECTION facing
  90.  
  91. -- Restores the facing from file
  92. function restoreFacing()
  93.     if not fs.exists(FACING_FILE) then
  94.         local file = fs.open(FACING_FILE, "w")
  95.         file.write("0\n")
  96.         file.close()
  97.     end
  98.     local file = fs.open(FACING_FILE, "r")
  99.     local facing = tonumber(file.readLine())
  100.     file.close()
  101.     if facing ~= 0 then
  102.         print("* Restoring facing...")
  103.     end
  104.     for i = 1, math.abs(facing) do
  105.         if facing > 0 then
  106.             turtle.turnLeft()
  107.         else
  108.             turtle.turnRight()
  109.         end
  110.     end
  111. end
  112.  
  113. -- Turns in the given direction and updates the facing file
  114. function turn(direction)
  115.     local file = fs.open(FACING_FILE, "w")
  116.     if direction == "left" then
  117.         curFacing = curFacing - 1
  118.         file.write(tostring(curFacing).."\n")
  119.         turtle.turnLeft()
  120.         file.close()
  121.     elseif direction == "right" then
  122.         curFacing = curFacing + 1
  123.         file.write(tostring(curFacing).."\n")
  124.         turtle.turnRight()
  125.         file.close()
  126.     end
  127. end
  128.  
  129. -- #SECTION stats
  130.  
  131. -- Loads the stats from file
  132. function loadStats()
  133.     if not fs.exists(STATS_FILE) then
  134.         updateStats()
  135.         return
  136.     end
  137.     local file = fs.open(STATS_FILE, "r")
  138.     local blocks = file.readLine()
  139.     local items = file.readLine()
  140.     file.close()
  141.  
  142.     minedBlocks = tonumber(blocks)
  143.     itemsDeposited = tonumber(items)
  144. end
  145.  
  146. -- Updates the stats file
  147. function updateStats()
  148.     local file = fs.open(STATS_FILE, "w")
  149.     file.write(tostring(minedBlocks).."\n"..tostring(itemsDeposited).."\n")
  150.     file.close()
  151. end
  152.  
  153. -- Prints the stats to the console
  154. function printStats()
  155.     print("\n-> Stats:")
  156.     print("   Total mined blocks: "..tostring(minedBlocks))
  157.     print("   Total gained items: "..tostring(itemsDeposited))
  158. end
  159.  
  160. -- #SECTION run
  161.  
  162. function run()
  163.     print("\n| CertusCrystalFarm by Sv443\n| https://github.com/Sv443/ComputerCraft-Projects\n")
  164.     loadStats()
  165.     restoreFacing()
  166.     while true do
  167.         loop()
  168.         os.sleep(DELAY)
  169.     end
  170. end
  171.  
  172. run()
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement