Advertisement
M4ND4RiM

Tunneler (Left Turtle)

Dec 9th, 2023 (edited)
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.94 KB | Source Code | 0 0
  1. -- Tunneler code for Create Astral modpack
  2.     -- Code adapated from 'ComputerCraft DFS Mining' from '0XSHAWN' in pastebin
  3.  
  4.  
  5.  
  6. -- 🗺️ Variable declaration
  7. -- =================================
  8.  
  9. -- Block type used to fill the holes after mining
  10. local FILL_BLOCKS = {
  11.   ["minecraft:cobblestone"]=true,
  12.   ["minecraft:granite"]=true,
  13.   ["minecraft:diorite"]=true,
  14.   ["minecraft:andesite"]=true,
  15.   ["minecraft:cobbled_deepslate"]=true,
  16.   ["minecraft:tuff"]=true,
  17.   ["minecraft:dirt"]=true,
  18.   ["ad_astra:moon_cobblestone"]=true,
  19.   ["ad_astra:mars_cobblestone"]=true,
  20.   ["ad_astra:venus_cobblestone"]=true,
  21.   ["ad_astra:mercury_cobblestone"]=true,
  22.   ["ad_astra:glacio_cobblestone"]=true,
  23. }
  24.  
  25. -- Table containing the whitelist of ores to mine
  26. local ORES_WHITELIST = {
  27.   ["minecraft:copper_ore"]=true,
  28.   ["minecraft:deepslate_copper_ore"]=true,
  29.   ["minecraft:gold_ore"]=true,
  30.   ["minecraft:deepslate_gold_ore"]=true,
  31.   ["minecraft:iron_ore"]=true,
  32.   ["minecraft:deepslate_iron_ore"]=true,
  33.   ["minecraft:coal_ore"]=true,
  34.   ["minecraft:deepslate_coal_ore"]=true,
  35.   ["minecraft:redstone_ore"]=true,
  36.   ["minecraft:deepslate_redstone_ore"]=true,
  37.   ["minecraft:emerald_ore"]=true,
  38.   ["minecraft:deepslate_emerald_ore"]=true,
  39.   ["minecraft:diamond_ore"]=true,
  40.   ["minecraft:deepslate_diamond_ore"]=true,
  41.   ["minecraft:lapis_ore"]=true,
  42.   ["minecraft:deepslate_lapis_ore"]=true,
  43.   ["minecraft:nether_gold_ore"]=true,
  44.   ["minecraft:nether_quartz_ore"]=true,
  45.   ["minecraft:ancient_debris"]=true,
  46.   ["ad_astra:moon_iron_ore"]=true,
  47.   ["ad_astra:moon_ice_shard_ore"]=true,
  48.   ["ad_astra:deepslate_ice_shard_ore"]=true,
  49.   ["ad_astra:moon_cheese_ore"]=true,
  50.   ["ad_astra:moon_desh_ore"]=true,
  51.   ["ad_astra:deepslate_desh_ore"]=true,
  52.   ["ad_astra:mars_iron_ore"]=true,
  53.   ["ad_astra:mars_diamond_ore"]=true,
  54.   ["ad_astra:mars_ostrum_ore"]=true,
  55.   ["ad_astra:deepslate_ostrum_ore"]=true,
  56.   ["ad_astra:mars_ice_shard_ore"]=true,
  57.   ["ad_astra:venus_coal_ore"]=true,
  58.   ["ad_astra:venus_gold_ore"]=true,
  59.   ["ad_astra:venus_diamond_ore"]=true,
  60.   ["ad_astra:venus_calorite_ore"]=true,
  61.   ["ad_astra:deepslate_calorite_ore"]=true,
  62.   ["ad_astra:mercury_iron_ore"]=true,
  63.   ["ad_astra:glacio_ice_shard_ore"]=true,
  64.   ["ad_astra:glacio_coal_ore"]=true,
  65.   ["ad_astra:glacio_copper_ore"]=true,
  66.   ["ad_astra:glacio_iron_ore"]=true,
  67.   ["ad_astra:glacio_lapis_ore"]=true,
  68.   ["create:zinc_ore"]=true,
  69.   ["create:deepslate_zinc_ore"]=true,
  70.   ["techreborn:bauxite_ore"]=true,
  71.   ["techreborn:deepslate_bauxite_ore"]=true,
  72.   ["techreborn:ruby_ore"]=true,
  73.   ["techreborn:deepslate_ruby_ore"]=true,
  74.   ["techreborn:sapphire_ore"]=true,
  75.   ["techreborn:deepslate_sapphire_ore"]=true,
  76.   ["techreborn:tin_ore"]=true,
  77.   ["techreborn:deepslate_tin_ore"]=true,
  78.   ["techreborn:cinnabar_ore"]=true,
  79.   ["techreborn:galena_ore"]=true,
  80.   ["techreborn:deepslate_galena_ore"]=true,
  81.   ["techreborn:iridium_ore"]=true,
  82.   ["techreborn:pyrite_ore"]=true,
  83.   ["techreborn:lead_ore"]=true,
  84.   ["techreborn:peridot_ore"]=true,
  85.   ["techreborn:deepslate_peridot_ore"]=true,
  86.   ["techreborn:sheldonite_ore"]=true,
  87.   ["techreborn:deepslate_sheldonite_ore"]=true,
  88.   ["techreborn:silver_ore"]=true,
  89.   ["techreborn:deepslate_silver_ore"]=true,
  90.   ["techreborn:tungsten_ore"]=true,
  91.   ["techreborn:deepslate_tungsten_ore"]=true,
  92.   ["techreborn:sodalite_ore"]=true,
  93.   ["techreborn:deepslate_sodalite_ore"]=true,
  94.   ["techreborn:sphalerite_ore"]=true
  95. }
  96.  
  97. -- Enum containing directions (Front, Back, Left, Right, Up, Down).
  98. local Dir = {
  99.   Front = 0,
  100.   Back = 1,
  101.   Left = 2,
  102.   Right = 3,
  103.   Up = 4,
  104.   Down = 5
  105. }
  106.  
  107. -- Declares the coal slot
  108. local COAL_SLOT = 16
  109.  
  110. -- Declares the redstone position of the transmitter
  111. local REDSTONE_TRANSMITTER = 'Back'
  112.  
  113. -- Declares if the turtle is the right one or the left one
  114. local TURTLE_SIDE = 'Left'
  115.  
  116. -- Declares the amount of chunks to mine
  117. local CHUNKS_TO_MINE = 4
  118.  
  119. --  Number of times the turtle moves forward and checks for ore veins (zero because it will be defined later)
  120. local ITER = 0
  121.  
  122. -- Set redstone output to zero
  123. redstone.setAnalogOutput(REDSTONE_TRANSMITTER,0)
  124.  
  125.  
  126. -- ⚙️ Functions
  127. -- =================================
  128.  
  129. -- Creates a table with depth and direction
  130. local function vector(depth, dir)
  131. return {depth=depth, dir=dir}
  132. end
  133.  
  134. -- Aligns the turtle and adds information to the track table
  135. local function alignDir(dir, depth, track)
  136. if (dir == Dir.Left) then
  137.   turtle.turnLeft()
  138.   table.insert(track, vector(depth, Dir.Left))
  139. elseif (dir == Dir.Right) then
  140.   turtle.turnRight()
  141.   table.insert(track, vector(depth, Dir.Right))
  142. elseif (dir == Dir.Back) then
  143.   turtle.turnRight()
  144.   turtle.turnRight()
  145.   table.insert(track, vector(depth, Dir.Right))
  146.   table.insert(track, vector(depth, Dir.Right))
  147. end
  148. end
  149.  
  150. -- Undoes alignment based on the track table for the specified depth
  151. local function undoAlignDir(depth, track)
  152. while (#track > 0 and track[#track].depth == depth) do
  153.   local vec = table.remove(track)
  154.   if (vec.dir == Dir.Left) then
  155.     turtle.turnRight()
  156.   elseif (vec.dir == Dir.Right) then
  157.     turtle.turnLeft()
  158.   end
  159. end
  160. end
  161.  
  162. -- Checks if the block is an ore based on the whitelist and reports rare ores
  163. local function isMine(hasBlock, blockInfo)
  164. return hasBlock and ORES_WHITELIST[blockInfo.name]
  165. end
  166.  
  167. -- Checks if the block in the given direction is mineable
  168. local function inspectDirIsMine(dir)
  169. local hasBlock, blockInfo
  170. if (dir == Dir.Up) then
  171.   hasBlock, blockInfo = turtle.inspectUp()
  172. elseif (dir == Dir.Down) then
  173.   hasBlock, blockInfo = turtle.inspectDown()
  174. else
  175.   hasBlock, blockInfo = turtle.inspect()
  176. end
  177.  
  178. return isMine(hasBlock, blockInfo)
  179. end
  180.  
  181. -- Mines the block in the given direction, updates the turtle position, and adds information to the track table
  182. local function excavateDir(dir, depth, track)
  183. if (dir == Dir.Up) then
  184.   turtle.digUp()
  185.   turtle.up()
  186.   table.insert(track, vector(depth, Dir.Up))
  187. elseif (dir == Dir.Down) then
  188.   turtle.digDown()
  189.   turtle.down()
  190.   table.insert(track, vector(depth, Dir.Down))
  191. else
  192.   local counter = 0
  193.   repeat
  194.     turtle.dig()
  195.     counter = counter + 1
  196.   until turtle.forward()
  197.   table.insert(track, vector(depth, Dir.Front))
  198. end
  199. end
  200.  
  201. -- Selects the fill block from the turtle inventory
  202. local function selectFillBlock()
  203. for i = 1, 16 do
  204.   local info = turtle.getItemDetail(i)
  205.   if (info ~= null) then
  206.     for key, _ in pairs(FILL_BLOCKS) do
  207.       if (key == info.name) then
  208.         turtle.select(i)
  209.         return
  210.       end
  211.     end
  212.   else
  213.     select(i)
  214.     return
  215.   end
  216. end
  217. end
  218.  
  219. -- Moves the turtle back to the original position, filling the holes
  220. local function backtrack(currentDepth, targetDepth, moveTrack, turnTrack)
  221. while (currentDepth ~= targetDepth) do
  222.   while (#moveTrack > 0 and moveTrack[#moveTrack].depth == currentDepth - 1) do
  223.     local vec = table.remove(moveTrack)
  224.     if (vec.dir == Dir.Front) then
  225.       turtle.back()
  226.       selectFillBlock()
  227.       turtle.place()
  228.     elseif (vec.dir == Dir.Up) then
  229.       turtle.down()
  230.       selectFillBlock()
  231.       turtle.placeUp()
  232.     elseif (vec.dir == Dir.Down) then
  233.       turtle.up()
  234.       selectFillBlock()
  235.       turtle.placeDown()
  236.     end
  237.   end
  238.   undoAlignDir(currentDepth - 1, turnTrack)
  239.   currentDepth = currentDepth - 1
  240. end
  241. end
  242.  
  243. -- Starts the depth-first search for ore veins in the given direction
  244. local function dfs(initDir)
  245. local stack = {}
  246. local turnTrack = {}
  247. local moveTrack = {}
  248. local depth = 0
  249. table.insert(stack, vector(depth, initDir))
  250.  
  251. while (#stack > 0) do
  252.   local vec = table.remove(stack)
  253.   backtrack(depth, vec.depth, moveTrack, turnTrack)
  254.   depth = vec.depth
  255.  
  256.   alignDir(vec.dir, depth, turnTrack)
  257.  
  258.   if (inspectDirIsMine(vec.dir)) then
  259.     excavateDir(vec.dir, depth, moveTrack)
  260.     depth = depth + 1
  261.  
  262.     if isMine(turtle.inspect()) then
  263.       table.insert(stack, vector(depth, Dir.Front))
  264.     end
  265.  
  266.     if isMine(turtle.inspectUp()) then
  267.       table.insert(stack, vector(depth, Dir.Up))
  268.     end
  269.     if isMine(turtle.inspectDown()) then
  270.       table.insert(stack, vector(depth, Dir.Down))
  271.     end
  272.     turtle.turnRight()
  273.     if isMine(turtle.inspect()) then
  274.       table.insert(stack, vector(depth, Dir.Right))
  275.     end
  276.     turtle.turnRight()
  277.     if isMine(turtle.inspect()) then
  278.       table.insert(stack, vector(depth, Dir.Back))
  279.     end
  280.     turtle.turnRight()
  281.     if isMine(turtle.inspect()) then
  282.       table.insert(stack, vector(depth, Dir.Left))
  283.     end
  284.     turtle.turnRight()
  285.   else
  286.     undoAlignDir(depth, turnTrack)
  287.   end
  288. end
  289.  
  290. backtrack(depth, 0, moveTrack, turnTrack)
  291. end
  292.  
  293. -- Triggers the depth-first search if an ore block is found in the given direction
  294. local function triggerDfs(dir)
  295. if inspectDirIsMine(dir) then
  296.   dfs(dir)
  297. end
  298. end
  299.  
  300. -- Checks for ore blocks around the turtle in all directions
  301. local function inspectAround()
  302. turtle.turnRight()
  303. triggerDfs(Dir.Front)
  304.  
  305. turtle.turnLeft()
  306. triggerDfs(Dir.Up)
  307.  
  308. turtle.turnLeft()
  309. triggerDfs(Dir.Front)
  310.  
  311. turtle.down()
  312. triggerDfs(Dir.Front)
  313.  
  314. turtle.turnRight()
  315. triggerDfs(Dir.Down)
  316.  
  317. turtle.turnRight()
  318. triggerDfs(Dir.Front)
  319.  
  320. turtle.turnLeft()
  321. turtle.up()
  322. end
  323.  
  324. -- Refuel when bellow 50 fuel
  325. local function refuel()
  326. fuel_level = turtle.getFuelLevel()
  327. if (fuel_level < 200) then
  328.   turtle.select(COAL_SLOT)
  329.   while (fuel_level < 600) do
  330.     fuel_level = turtle.getFuelLevel()
  331.     turtle.refuel()
  332.   end
  333. end
  334. end
  335.  
  336. -- Digs one block ahead and breaks the block, and digs downwards
  337. local function advance()
  338. repeat
  339.   turtle.dig()
  340. until turtle.forward()
  341. turtle.digDown()
  342. refuel()
  343. end
  344.  
  345. -- Checks if it is in the initial position
  346. local function isItTheInitialPosition()
  347.   hasBlockUp, blockInfoUp = turtle.inspectUp()
  348.   hasBlockDown, blockInfoDown = turtle.inspectDown()
  349.  
  350.   if hasBlockUp and hasBlockDown then
  351.       if blockInfoUp.name == 'reinfchest:gold_chest' and blockInfoDown.name == 'minecraft:chest' then
  352.           return true
  353.       else
  354.           return false
  355.       end
  356.   else
  357.       return false
  358.   end
  359. end
  360.  
  361. -- Checks coal amount in turtle inventory
  362. local function checkCoalAmount()
  363. turtle.select(COAL_SLOT)
  364. coal_amount = turtle.getItemCount()
  365. if (coal_amount < 64) then
  366.   coal_amount_to_get = 64
  367.   return coal_amount_to_get
  368. else
  369.   coal_amount_to_get = 0
  370.   return coal_amount_to_get
  371. end
  372. end
  373.  
  374. -- Grabs coal from chest bellow
  375. local function suckCoalFromChest(coal_amount_to_get)
  376. turtle.select(COAL_SLOT)
  377. turtle.suckDown(coal_amount_to_get)
  378. end
  379.  
  380. -- Deposit the item in the chest above
  381. local function depositsItems()
  382. for i = 1, 16 do
  383.   turtle.select(i)
  384.   item = turtle.getItemDetail()
  385.   if (item == null) then
  386.   else
  387.     if (item.name ~= 'minecraft:coal') then
  388.       didItWork = turtle.dropUp()
  389.       if (didItWork == False) then
  390.         repeat
  391.           print("Error, not able to deposit in chest (maybe it is full)!")
  392.           didItWork = turtle.dropUp()
  393.         until (didItWork == true)
  394.       end
  395.     else
  396.       didItWork = turtle.dropDown()
  397.       if (didItWork == False) then
  398.           didItWork = turtle.dropUp()
  399.           if (didItWork == False) then
  400.             repeat
  401.               print("Error, not able to deposit in chest (maybe it is full)!")
  402.               didItWork = turtle.dropUp()
  403.             until (didItWork == true)
  404.           end
  405.       end
  406.     end
  407.   end
  408. end
  409. end
  410.  
  411. -- Defines ITER value based on the turtle position
  412. local function defineITER()
  413. if (TURTLE_SIDE == 'Left') then
  414.   ITER = (CHUNKS_TO_MINE * 16) - 5
  415. else
  416.   ITER = CHUNKS_TO_MINE * 16
  417. end
  418. end
  419.  
  420.  
  421.  
  422.  
  423. -- ⭐ Main function
  424. -- =================================
  425.  
  426. if (isItTheInitialPosition()) then
  427.  
  428.   -- Get coal amount in the coal slot, if it less than a stack, it grabs more
  429.   coal_amount_to_get = checkCoalAmount()
  430.   while (coal_amount_to_get ~= 0) do
  431.     coal_amount_to_get = checkCoalAmount()
  432.     suckCoalFromChest(coal_amount_to_get)
  433.     refuel()
  434.   end
  435.  
  436.   -- Define ITER amount
  437.   defineITER()
  438.  
  439.   -- Does the movement to get out of the Create machine and start mining
  440.   if (TURTLE_SIDE == 'Left') then
  441.     turtle.turnLeft()
  442.     turtle.forward()
  443.   else
  444.     turtle.turnRight()
  445.     hasBlock = turtle.inspect()
  446.     if (hasBlock == true) then
  447.       turtle.dig()
  448.       turtle.forward()
  449.     end
  450.     turtle.forward()
  451.   turtle.forward()
  452.   end
  453.  
  454.   -- Starts the mining
  455.   for i = 1, ITER do
  456.     advance()
  457.     inspectAround()
  458.     selectFillBlock()
  459.     turtle.placeDown()
  460.   end
  461.  
  462.   -- Ends the mining and return to the starting point before starting mining
  463.   turtle.turnRight()
  464.   turtle.turnRight()
  465.   for i = 1, ITER do
  466.     turtle.forward()
  467.   end
  468.  
  469.   -- Returns to the initial position inside the create machine
  470.   if (TURTLE_SIDE == 'Left') then
  471.     turtle.forward()
  472.     turtle.turnLeft()
  473.   else
  474.     turtle.forward()
  475.     turtle.forward()
  476.     turtle.turnRight()
  477.   end
  478.  
  479.   -- Deposit items
  480.   depositsItems()
  481.  
  482.   -- Sends a redstone signal to the back of the turtle to trigger the create machine movement
  483.   redstone.setAnalogOutput(REDSTONE_TRANSMITTER,15)
  484.  
  485.   -- End of program
  486.  
  487. else
  488.   print("Error, not in the correct initial position, maybe chunk reload happened!")
  489. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement