Advertisement
phupqpr

CC miner Create Astral

Sep 6th, 2023 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.00 KB | Source Code | 0 0
  1. -- Mining turtle program for Create: Astral modpack
  2.  
  3. -- Constants
  4. local FUEL_SLOT = 1 -- The slot where the turtle stores fuel (coal)
  5. local CHEST_SLOT = 2 -- The slot where the turtle stores chests
  6. local TRASH_SLOT = 3 -- The slot where the turtle stores trash blocks (cobblestone, dirt, etc.)
  7. local FUEL_LIMIT = 240 -- The minimum fuel level to start mining
  8. local FUEL_MAX = 2000 -- The maximum fuel level to refuel
  9. local COAL_LEVEL = 64 -- The y-coordinate of the coal level
  10. local ORE_LEVELS = { -- The y-coordinates of different ore levels
  11.     ["iron"] = 64,
  12.     ["gold"] = 32,
  13.     ["redstone"] = 16,
  14.     ["diamond"] = 12,
  15.     ["emerald"] = 8,
  16.     ["lapis"] = 8,
  17.     ["quartz"] = 8,
  18.     ["copper"] = 8,
  19.     ["tin"] = 8,
  20.     ["silver"] = 8,
  21.     ["lead"] = 8,
  22.     ["nickel"] = 8,
  23.     ["uranium"] = 8,
  24.     ["zinc"] = 8,
  25.     ["osmium"] = 8
  26. }
  27.  
  28. -- Variables
  29. local x, y, z -- The current coordinates of the turtle
  30. local dx, dy, dz -- The current direction of the turtle (unit vector)
  31. local ores -- The table of ores to mine at each level
  32. local trash -- The table of trash blocks to dump
  33.  
  34. -- Functions
  35.  
  36. -- Refuel the turtle from the fuel slot
  37. local function refuel()
  38.     if turtle.getFuelLevel() < FUEL_MAX then
  39.         turtle.select(FUEL_SLOT)
  40.         while turtle.getFuelLevel() < FUEL_MAX and turtle.getItemCount() > 0 do
  41.             turtle.refuel(1)
  42.         end
  43.         print("Refueled to " .. turtle.getFuelLevel())
  44.     end
  45. end
  46.  
  47. -- Check if the turtle needs to refuel and go to the coal level if necessary
  48. local function checkFuel()
  49.     if turtle.getFuelLevel() < FUEL_LIMIT then
  50.         print("Low on fuel, going to coal level")
  51.         goTo(0, COAL_LEVEL, 0) -- Go to the origin at coal level
  52.         local found = false
  53.         for i = 1, 4 do -- Try four directions
  54.             turnRight()
  55.             if turtle.detect() then -- If there is a block in front
  56.                 turtle.dig() -- Dig it
  57.                 if turtle.inspect().name == "minecraft:coal_ore" then -- If it is coal ore
  58.                     found = true
  59.                     break
  60.                 else
  61.                     turtle.place() -- Place it back
  62.                 end
  63.             end
  64.         end
  65.         if found then -- If coal ore is found
  66.             print("Found coal ore, mining it")
  67.             while turtle.inspect().name == "minecraft:coal_ore" do -- While there is coal ore in front
  68.                 turtle.dig() -- Dig it
  69.                 forward() -- Move forward
  70.             end
  71.             turnBack() -- Turn back
  72.             while not turtle.detect() do -- While there is no block behind
  73.                 back() -- Move back
  74.             end
  75.             turnBack() -- Turn back again
  76.             refuel() -- Refuel from the mined coal
  77.         else -- If coal ore is not found
  78.             print("No coal ore found, aborting")
  79.             os.exit() -- Exit the program
  80.         end
  81.     end  
  82. end
  83.  
  84. -- Move the turtle forward and update its coordinates
  85. local function forward()
  86.     checkFuel() -- Check fuel before moving
  87.     while not turtle.forward() do -- While the move is unsuccessful
  88.         if turtle.detect() then -- If there is a block in front
  89.             turtle.dig() -- Dig it
  90.         elseif turtle.attack() then -- If there is a mob in front
  91.             print("Attacked a mob")
  92.         else -- If there is some other obstacle
  93.             print("Cannot move forward")
  94.             os.sleep(1) -- Wait for a second and try again
  95.         end
  96.     end
  97.     x = x + dx -- Update x-coordinate
  98.     y = y + dy -- Update y-coordinate
  99.     z = z + dz -- Update z-coordinate
  100. end
  101.  
  102. -- Move the turtle backward and update its coordinates
  103. local function back()
  104.     checkFuel() -- Check fuel before moving
  105.     while not turtle.back() do -- While the move is unsuccessful
  106.         if turtle.detectDown() then -- If there is a block below
  107.             turtle.digDown() -- Dig it
  108.         elseif turtle.attackDown() then -- If there is a mob below
  109.             print("Attacked a mob")
  110.         else -- If there is some other obstacle
  111.             print("Cannot move backward")
  112.             os.sleep(1) -- Wait for a second and try again
  113.         end
  114.     end
  115.     x = x - dx -- Update x-coordinate
  116.     y = y - dy -- Update y-coordinate
  117.     z = z - dz -- Update z-coordinate
  118. end
  119.  
  120. -- Move the turtle up and update its coordinates
  121. local function up()
  122.     checkFuel() -- Check fuel before moving
  123.     while not turtle.up() do -- While the move is unsuccessful
  124.         if turtle.detectUp() then -- If there is a block above
  125.             turtle.digUp() -- Dig it
  126.         elseif turtle.attackUp() then -- If there is a mob above
  127.             print("Attacked a mob")
  128.         else -- If there is some other obstacle
  129.             print("Cannot move up")
  130.             os.sleep(1) -- Wait for a second and try again
  131.         end
  132.     end
  133.     x = x + dx -- Update x-coordinate
  134.     y = y + dy -- Update y-coordinate
  135.     z = z + dz -- Update z-coordinate
  136. end
  137.  
  138. -- Move the turtle down and update its coordinates
  139. local function down()
  140.     checkFuel() -- Check fuel before moving
  141.     while not turtle.down() do -- While the move is unsuccessful
  142.         if turtle.detectDown() then -- If there is a block below
  143.             turtle.digDown() -- Dig it
  144.         elseif turtle.attackDown() then -- If there is a mob below
  145.             print("Attacked a mob")
  146.         else -- If there is some other obstacle
  147.             print("Cannot move down")
  148.             os.sleep(1) -- Wait for a second and try again
  149.         end
  150.     end
  151.     x = x - dx -- Update x-coordinate
  152.     y = y - dy -- Update y-coordinate
  153.     z = z - dz -- Update z-coordinate
  154. end
  155.  
  156. -- Turn the turtle right and update its direction
  157. local function turnRight()
  158.     checkFuel() -- Check fuel before turning
  159.     turtle.turnRight()
  160.     dx, dy, dz = -dz, dy, dx -- Update direction vector
  161. end
  162.  
  163. -- Turn the turtle left and update its direction
  164. local function turnLeft()
  165.     checkFuel() -- Check fuel before turning
  166.     turtle.turnLeft()
  167.     dx, dy, dz = dz, dy, -dx -- Update direction vector
  168. end
  169.  
  170. -- Turn the turtle back and update its direction
  171. local function turnBack()
  172.     checkFuel() -- Check fuel before turning
  173.     turtle.turnRight()
  174.     turtle.turnRight()
  175.     dx, dy, dz = -dx, dy, -dz -- Update direction vector
  176. end
  177.  
  178. -- Go to a specific coordinate by the shortest path
  179. local function goTo(targetX, targetY, targetZ)
  180.     while x ~= targetX do -- While not at the target x-coordinate
  181.         if x < targetX then -- If need to go positive x-direction
  182.             while dx ~= 1 do turnRight() end -- Turn to face positive x-direction
  183.             forward() -- Move forward
  184.         else -- If need to go negative x-direction
  185.             while dx ~= -1 do turnRight() end -- Turn to face negative x-direction
  186.             forward() -- Move forward
  187.         end
  188.     end
  189.  
  190.     while y ~= targetY do -- While not at the target y-coordinate
  191.         if y < targetY then -- If need to go positive y-direction
  192.             while dy ~= 1 do turnRight() end -- Turn to face positive y-direction
  193.             forward() -- Move forward
  194.         else -- If need to go negative y-direction
  195.             while dy ~= -1 do turnRight() end -- Turn to face negative y-direction
  196.             forward() -- Move forward
  197.         end
  198.     end
  199.  
  200.     while z ~= targetZ do -- While not at the target z-coordinate
  201.         if z < targetZ then -- If need to go positive z-direction
  202.             while dz ~= 0 do turnRight() end -- Turn to face positive z-direction
  203.             forward() -- Move forward
  204.         else -- If need to go negative z-direction
  205.             while dz ~= 0 do turnLeft() end -- Turn to face negative z-direction
  206.             forward() -- Move forward
  207.         end
  208.     end
  209.  
  210.     print("Reached " .. targetX .. ", " .. targetY .. ", " .. targetZ) -- Print the current coordinates
  211. end
  212.  
  213. -- Place a chest behind the turtle and dump the trash blocks into it
  214. local function dumpTrash()
  215.     print("Dumping trash blocks")
  216.     turnBack() -- Turn back
  217.     turtle.select(CHEST_SLOT) -- Select the chest slot
  218.     if turtle.getItemCount() > 0 then -- If there is a chest in the slot
  219.         turtle.place() -- Place the chest behind
  220.         for i = 4, 16 do -- For each slot from 4 to 16
  221.             turtle.select(i) -- Select the slot
  222.             local name = turtle.getItemDetail().name -- Get the name of the item
  223.             if trash[name] then -- If the item is in the trash table
  224.                 turtle.drop() -- Drop it into the chest
  225.             end
  226.         end
  227.         turtle.select(CHEST_SLOT) -- Select the chest slot again
  228.         turtle.dig() -- Break the chest and collect it
  229.     else -- If there is no chest in the slot
  230.         print("No chest available, aborting")
  231.         os.exit() -- Exit the program
  232.     end
  233.     turnBack() -- Turn back again
  234. end
  235.  
  236. -- Check if the block in front is an ore and mine it if necessary
  237. local function checkOre()
  238.     if turtle.detect() then -- If there is a block in front
  239.         local name = turtle.inspect().name -- Get the name of the block
  240.         local level = y + dy -- Get the level of the block
  241.         if ores[level] and ores[level][name] then -- If the block is an ore at this level
  242.             print("Found " .. name .. " at level " .. level)
  243.             turtle.dig() -- Dig it
  244.             forward() -- Move forward
  245.             checkOre() -- Check again recursively
  246.             back() -- Move back
  247.         end
  248.     end
  249. end
  250.  
  251. -- Scan and mine ores around the turtle at a given level
  252. local function scanOres(level)
  253.     print("Scanning ores at level " .. level)
  254.     for i = 1, 4 do -- For each direction
  255.         checkOre() -- Check and mine ore in front
  256.         turnRight() -- Turn right
  257.     end
  258.  
  259.     checkOreUp = false -- Flag for checking ore above
  260.     checkOreDown = false -- Flag for checking ore below
  261.  
  262.     if level < ORE_LEVELS["diamond"] then -- If below diamond level
  263.         checkOreUp = true -- Check ore above
  264.     elseif level > ORE_LEVELS["iron"] then -- If above iron level
  265.         checkOreDown = true -- Check ore below
  266.     end
  267.  
  268.     if checkOreUp then -- If need to check ore above
  269.         up() -- Move up
  270.         scanOres(level + 1) -- Scan ores at the next level recursively
  271.         down() -- Move down
  272.     end
  273.  
  274.     if checkOreDown then -- If need to check ore below
  275.         down() -- Move down
  276.         scanOres(level - 1) -- Scan ores at the next level recursively
  277.         up() -- Move up
  278.     end
  279. end
  280.  
  281. -- Main program
  282.  
  283. -- Initialize variables
  284. x, y, z = 0, 0, 0 -- Start at the origin
  285. dx, dy, dz = 0, 0, 1 -- Start facing positive z-direction
  286. ores = {} -- Create an empty table for ores
  287. trash = {} -- Create an empty table for trash
  288.  
  289. -- Print a setup terminal
  290. print("Welcome to the mining turtle program")
  291. print("Please enter the ores you want to mine at each level, separated by commas")
  292. print("For example: diamond,emerald,lapis")
  293. print("Enter 'none' if you don't want to mine any ore at that level")
  294. print("Enter 'all' if you want to mine all ores at that level")
  295. print("Press enter to confirm your choice")
  296.  
  297. -- Ask the user for the ores to mine at each level
  298. for k, v in pairs(ORE_LEVELS) do -- For each ore and its level
  299.     print("Level " .. v .. ": " .. k) -- Print the level and the ore name
  300.     local input = io.read() -- Read the user input
  301.     local list = {} -- Create an empty list for the ores
  302.     for word in input:gmatch("%w+") do -- For each word in the input
  303.         word = word:lower() -- Convert the word to lower case
  304.         if word == "none" then -- If the word is 'none'
  305.             list = {} -- Set the list to empty
  306.             break -- Break the loop
  307.         elseif word == "all" then -- If the word is 'all'
  308.             list = ORE_LEVELS -- Set the list to all ore levels
  309.             break -- Break the loop
  310.         elseif ORE_LEVELS[word] then -- If the word is a valid ore name
  311.             list[word] = true -- Add the word to the list as a key
  312.         else -- If the word is invalid
  313.             print("Invalid ore name: " .. word) -- Print an error message
  314.             os.exit() -- Exit the program
  315.         end
  316.     end
  317.     ores[v] = list -- Add the list to the ores table with the level as a key
  318. end
  319.  
  320. -- Ask the user for the trash blocks to dump
  321. print("Please enter the trash blocks you want to dump, separated by commas")
  322. print("For example: cobblestone,dirt,gravel")
  323. print("Enter 'none' if you don't want to dump any block")
  324. print("Enter 'all' if you want to dump all blocks except ores and chests")
  325. local input = io.read() -- Read the user input
  326. for word in input:gmatch("%w+") do -- For each word in the input
  327.     word = word:lower() -- Convert the word to lower case
  328.     if word == "none" then -- If the word is 'none'
  329.         trash = {} -- Set the trash table to empty
  330.         break -- Break the loop
  331.     elseif word == "all" then -- If the word is 'all'
  332.         trash = {["minecraft:cobblestone"] = true, ["minecraft:dirt"] = true, ["minecraft:gravel"] = true} -- Set the trash table to common blocks
  333.         break -- Break the loop
  334.     else -- If the word is a block name
  335.         trash[word] = true -- Add the word to the trash table as a key
  336.     end
  337. end
  338.  
  339. -- Start mining from diamond level
  340. goTo(0, ORE_LEVELS["diamond"], 0) -- Go to the origin at diamond level
  341. scanOres(ORE_LEVELS["diamond"]) -- Scan and mine ores at diamond level
  342.  
  343. -- Go back to the origin and dump the ores into a chest
  344. goTo(0, 0, 0) -- Go back to the origin
  345. turnBack() -- Turn back
  346. turtle.select(CHEST_SLOT) -- Select the chest slot
  347. if turtle.getItemCount() > 0 then -- If there is a chest in the slot
  348.     turtle.place() -- Place the chest behind
  349.     for i = 4, 16 do -- For each slot from 4 to 16
  350.         turtle.select(i) -- Select the slot
  351.         turtle.drop() -- Drop the item into the chest
  352.     end
  353.     print("Dumped all ores into the chest")
  354. else -- If there is no chest in the slot
  355.     print("No chest available, aborting")
  356.     os.exit() -- Exit the program
  357. end
  358.  
  359. -- End of program
  360. print("Mining completed, thank you for using the program")
  361.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement