Advertisement
Sv443

ComputerCraft Turtle Certus Quartz Farm - Block Replacer

Dec 3rd, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.40 KB | Gaming | 0 0
  1. -- How many times the turtle has to turn from its
  2. -- initial facing to face the output inventory
  3. -- Positive number for clockwise, negative for counter-clockwise
  4. OUTPUT_INVENTORY_TURNS = 0
  5. -- How many times the turtle has to turn from its
  6. -- initial facing to face the input inventory
  7. -- Positive number for clockwise, negative for counter-clockwise
  8. INPUT_INVENTORY_TURNS = 2
  9.  
  10. -- Full name including namespace of the non-budding certus block
  11. CERTUS_BLOCK_NAME = "ae2:quartz_block"
  12.  
  13. -- Delay in seconds to wait after the block has been exhausted
  14. -- before trying to replace it
  15. -- Should be around 3-4 times as high as the checking interval
  16. -- of the crystal mining turtles
  17. DELAY_AFTER_EXHAUST = 2
  18.  
  19. -- The file where the turtle stores its current facing
  20. FACING_FILE = "/facing.txt"
  21. -- The file where the mining stats are stored
  22. STATS_FILE = "/stats.txt"
  23.  
  24.  
  25. -- DO NOT EDIT BELOW THIS LINE OR TURTLE GETS ANGY >:(
  26.  
  27. local curFacing = 0
  28. local replacedBlocks = 0
  29.  
  30. -- #SECTION replacing
  31.  
  32. -- Checks if the block below is a regular certus block and returns true if so
  33. function checkBlock()
  34.     local hasBlock, detail = turtle.inspectDown()
  35.     if hasBlock then
  36.         return detail.name == CERTUS_BLOCK_NAME
  37.     end
  38.     return false
  39. end
  40.  
  41. -- Digs the block below the turtle
  42. function digBlock()
  43.     local dug = false
  44.     while not turtle.digDown() do
  45.         if not dug then
  46.             print("! Failed to replace block")
  47.             dug = true
  48.         end
  49.         os.sleep(0.5)
  50.     end
  51.     return true
  52. end
  53.  
  54. -- Places a block below the turtle
  55. function placeBlock()
  56.     local placed = false
  57.     turtle.select(1)
  58.     while not turtle.placeDown() do
  59.         if not placed then
  60.             print("! Failed to place block")
  61.             placed = true
  62.         end
  63.         os.sleep(0.5)
  64.     end
  65.     replacedBlocks = replacedBlocks + 1
  66.     return true
  67. end
  68.  
  69. -- #SECTION turning
  70.  
  71. -- Restores the facing from file
  72. function restoreFacing()
  73.     if not fs.exists(FACING_FILE) then
  74.         local file = fs.open(FACING_FILE, "w")
  75.         file.write("0\n")
  76.         file.close()
  77.     end
  78.     local file = fs.open(FACING_FILE, "r")
  79.     local facing = tonumber(file.readLine())
  80.     file.close()
  81.     if facing ~= 0 then
  82.         print("* Restoring facing...")
  83.     end
  84.     for i = 1, math.abs(facing) do
  85.         if facing > 0 then
  86.             turtle.turnLeft()
  87.         else
  88.             turtle.turnRight()
  89.         end
  90.     end
  91. end
  92.  
  93. -- Turns in the given direction for the given amount and updates the facing file
  94. function turn(direction, amount)
  95.     if amount == 0 then
  96.         return
  97.     end
  98.     if fs.exists(FACING_FILE) then
  99.         fs.delete(FACING_FILE)
  100.     end
  101.  
  102.     for i = 1, math.abs(amount) do
  103.         if direction == "left" then
  104.             curFacing = curFacing - 1
  105.             local file = fs.open(FACING_FILE, "w")
  106.             file.write(tostring(curFacing).."\n")
  107.             file.close()
  108.             turtle.turnLeft()
  109.         elseif direction == "right" then
  110.             curFacing = curFacing + 1
  111.             local file = fs.open(FACING_FILE, "w")
  112.             file.write(tostring(curFacing).."\n")
  113.             file.close()
  114.             turtle.turnRight()
  115.         end
  116.     end
  117. end
  118.  
  119. -- #SECTION inventories
  120.  
  121. -- Grabs a new budding certus block from the input inventory
  122. function grabBlock()
  123.     -- turn to face input inventory
  124.     turn(INPUT_INVENTORY_TURNS > 0 and "right" or "left", INPUT_INVENTORY_TURNS)
  125.  
  126.     -- grab item
  127.     local grabbed = false
  128.     while not turtle.suck(1) do
  129.         if not grabbed then
  130.             print("* Waiting for input block...")
  131.             grabbed = true
  132.         end
  133.         os.sleep(0.5)
  134.     end
  135.  
  136.     -- turn to face initial direction
  137.     turn(INPUT_INVENTORY_TURNS < 0 and "right" or "left", INPUT_INVENTORY_TURNS)
  138.  
  139.     return true
  140. end
  141.  
  142. -- Deposits the used up certus block into the output inventory
  143. function depositBlock()
  144.     -- turn to face output inventory
  145.     turn(OUTPUT_INVENTORY_TURNS > 0 and "right" or "left", OUTPUT_INVENTORY_TURNS)
  146.  
  147.     -- drop item
  148.     local slot = 1
  149.     local dropped = false
  150.     turtle.select(1)
  151.     while not turtle.drop() do
  152.         slot = slot + 1
  153.         if slot > 16 then
  154.             slot = 1
  155.         end
  156.         turtle.select(slot)
  157.         if not dropped then
  158.             print("! Output inventory full!")
  159.             dropped = true
  160.         end
  161.         os.sleep(0.5)
  162.     end
  163.  
  164.     -- turn to face initial direction
  165.     turn(OUTPUT_INVENTORY_TURNS < 0 and "right" or "left", OUTPUT_INVENTORY_TURNS)
  166.  
  167.     return true
  168. end
  169.  
  170. -- #SECTION stats
  171.  
  172. -- Loads the stats from file
  173. function loadStats()
  174.     if not fs.exists(STATS_FILE) then
  175.         updateStats()
  176.         return
  177.     end
  178.     local file = fs.open(STATS_FILE, "r")
  179.     local blocks = file.readLine()
  180.     file.close()
  181.  
  182.     replacedBlocks = tonumber(blocks)
  183. end
  184.  
  185. -- Updates the stats file
  186. function updateStats()
  187.     local file = fs.open(STATS_FILE, "w")
  188.     file.write(tostring(replacedBlocks).."\n")
  189.     file.close()
  190. end
  191.  
  192. -- Prints the stats to the console
  193. function printStats()
  194.     print("\n-> Stats:")
  195.     print("   Total replaced blocks: "..tostring(replacedBlocks))
  196. end
  197.  
  198. -- #SECTION run
  199.  
  200. local waitingCheck = false
  201.  
  202. function run()
  203.     print("\n| CertusCrystalFarm by Sv443\n| https://github.com/Sv443/ComputerCraft-Projects\n")
  204.     loadStats()
  205.     restoreFacing()
  206.     local hasBlock = turtle.inspectDown()
  207.     if not hasBlock then
  208.         local itemDet = turtle.getItemDetail(1)
  209.         if itemDet == nil then
  210.             grabBlock()
  211.         end
  212.         placeBlock()
  213.     end
  214.     while true do
  215.         turtle.select(1)
  216.         local itemDet = turtle.getItemDetail(1)
  217.         if itemDet == nil then
  218.             grabBlock()
  219.         else
  220.             if itemDet.count > 1 then
  221.                 print("! Started with unexpected items")
  222.                 print("* Depositing them in output...\n")
  223.                 depositBlock()
  224.             end
  225.         end
  226.         if not waitingCheck then
  227.             print("* Waiting for block to exhaust...")
  228.             waitingCheck = true
  229.         end
  230.         if checkBlock() then
  231.             waitingCheck = false
  232.             os.sleep(DELAY_AFTER_EXHAUST)
  233.             digBlock()
  234.             placeBlock()
  235.             depositBlock()
  236.             print("* Replaced block")
  237.             updateStats()
  238.             printStats()
  239.         end
  240.         os.sleep(CHECK_INTERVAL)
  241.     end
  242. end
  243.  
  244. run()
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement