Advertisement
RJ-Bradach

Block Vein API

Jun 28th, 2021 (edited)
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.72 KB | Gaming | 0 0
  1. --[[
  2.     Block Vein API for usage in various block harvesting programs for turtles.
  3.     Examples of different types of use for this API: Tree harvester, Strip Miner, Sand harvester
  4.     How does the API work?
  5.     -   API Users will call blockVein.setBlocksToMine(string | table).
  6.         Calling this function will provide the turtle with (partial) block names or block tags to mine.
  7.         ...More info on this function can be found down below
  8.     -   Users also have access to the blockVein.backIntoVein() and blockVein.backOutOfVein(checkVeinBlocks) functions
  9.         Calling these functions are optional. They are for use in a user's own programs,
  10.         usually for when the turtle needs to refuel or when the turtle's inventory is full.
  11.     -   The core function call for this API is blockVein.scanAdjBlocks(fuelCheck, inventoryCheck)
  12.         Call this function with your own fuelCheck and InventoryCheck function, these are optional.
  13.     -   When the function is called, the turtle inspects the block in front of it, if the block matches the given list of blocks to mine,
  14.         it will mine that block and move into the block-space, and continue to check all blocks around the block-space. If the turtle
  15.         finds a block that matches any block in the list of blocks to mine, it will repeat the previous steps.
  16.     -   1. Inspect block in front of turtle, if successful, continue to step 2, if fail, check adjacent blocks.
  17.         2. Check block's metadata against list of acceptable blocks to mine
  18.         3. Mine block
  19.         4. Move into block-space left behind
  20.         5. Turtle repeats steps 1-4 for all blocks surrounding previously mined block.
  21.         6. Turtle returns to original block
  22. ]]--
  23.  
  24. -- To see block metadata (names, tags), press F3 + H in game to show "Advanced Tooltips".
  25. -- Hover over blocks in inventory or JEI to see metadata. First string listed is the name. e.g. "minecraft:spruce_logs" for Spruce Logs
  26. -- Every other string listed under the name are tags.
  27.  
  28. local blockTagsToMine = {} -- List of metadata to include in your turtles blockvein.
  29. -- e.g. blockTagsToMine = {"minecraft:logs"} will mine all logs for all trees assuming their metadata includes the string "minecraft:logs"
  30. -- as trees should.
  31.  
  32. local blockNamesToMine = {} -- List of names, or substrings of names for blocks to mine.
  33. -- e.g. blockNamesToMine = {"log"} will mine any block with a name containing "log". i.e. Spruce Log, Oak Log, etc...
  34.  
  35. -- Table for turtle movement functions
  36. local moveFunctions = {[1] = turtle.forward, [2] = turtle.back,[3] = turtle.turnLeft, [4] = turtle.turnRight, [5] = turtle.up, [6] = turtle.down}
  37.  
  38. -- Table filled by turtle movement functions ordered to back out of a block-vein
  39. local unwindBlockVein = {}
  40.  
  41. -- Table filled by turtle movement functions ordered to send the turtle back into the block-vein
  42. local windBlockVein = {}
  43.  
  44. -- Default skip function
  45. local function skip()
  46. -- Empty for optional parameter later on
  47. end
  48.  
  49. -- Function to dig, created to be sand/gravel/etc resistant
  50. local function dig(digFunction, detectFunction, inventoryCheck, inspectFunction)
  51.     local refuel = false
  52.     while detectFunction() do
  53.         local _, data = inspectFunction()
  54.         if (data.name == "minecraft:coal_ore") then
  55.             refuel = true
  56.         end
  57.             digFunction()
  58.             sleep(0.4)
  59.     end
  60.     if (refuel) then
  61.         for i = 1,16 do
  62.             turtle.select(i)
  63.             local data = turtle.getItemDetail(i)
  64.             if (data and data.name == "minecraft:coal") then
  65.                 turtle.refuel()
  66.             end
  67.         end
  68.     end
  69.     inventoryCheck()
  70. end
  71.  
  72. -- Function to move the turtle in or out of a vein
  73. local function move(moveType, windMove, unwindMove, fuelCheck)
  74.     windMove = windMove or nil
  75.     unwindMove = unwindMove or nil
  76.     -- moveType will be the argument of the turtles actual movement. This will either match windMove or unwindMove, but not always the same one.
  77.     -- This is why we define what move is the wind move and which unwinds the turtle from the block vein
  78.     moveType()
  79.  
  80.     if (windMove ~= nil) then --Only need to check if one of these are nil since they will both be defined or both be nil
  81.         table.insert(windBlockVein, windMove)
  82.         table.insert(unwindBlockVein, unwindMove)
  83.     end
  84.     fuelCheck()
  85. end
  86.  
  87.  
  88. -- Function to set blockTagsToMine or blockNamesToMine table
  89. -- Required for people using this API. Call this function to set the blocks the turtle should mine.
  90. -- Arguments:
  91. --      blockMetadata - the tags/names/subnames of blocks to mine.
  92. --          Usage: Input a table or string containing block metadata.
  93. --          Accepted Metadata = "minecraft:logs" or {"minecraft:logs"} or "log, leaves" or "dirt" or "spruce" or {"spru", "log", "sand"}
  94. --      metadataTableIndex - The type of metadata you've used to input your blocks to mine.
  95. --          Usage: Input the type of metadata you're setting.
  96. --          Accepted Indexes = 1, 2
  97. --          Metadata Index Definition: 1 - Name metadata
  98. --                                     2 - Tag metadata
  99. function setBlocksToMine(blockMetadata, metadataTableIndex)
  100.     local metadataTable
  101.     if (metadataTableIndex == 1) then
  102.         metadataTable = blockNamesToMine
  103.     elseif (metadataTableIndex == 2) then
  104.         metadataTable = blockTagsToMine
  105.     else
  106.         error("Wrong Table Index Used. Try 1 or 2")
  107.     end
  108.  
  109.     if (type(blockMetadata) == "table") then
  110.         for _,value in pairs(blockMetadata) do
  111.             table.insert(metadataTable, value)
  112.         end
  113.     else
  114.         for value in string.gmatch(blockMetadata, "([^,]+)") do
  115.             table.insert(metadataTable, value )
  116.         end
  117.     end
  118. end
  119.  
  120. -- Function to inspect a block, either above, below, or in front of a turtle.
  121. -- Returns block information
  122. local function scan(inspectFunction)
  123.     local success, blockInfo = inspectFunction()
  124.     if success then
  125.         return blockInfo
  126.     end
  127. end
  128.  
  129. -- Function to compare block in front of turtle to the blockTagsToMine and blockNamesToMine tables
  130. local function blockCheck(blockInfo)
  131.     if (blockInfo ~= nil) then
  132.         for k in pairs(blockInfo["tags"]) do
  133.             for i = 1,#blockTagsToMine do
  134.                 if (k:match(blockTagsToMine[i])) then
  135.                 -- if (k == blockTagsToMine[i]) then
  136.                     return true
  137.                 end
  138.             end
  139.         end
  140.         for i = 1,#blockNamesToMine do
  141.             if (string.find(blockInfo["name"], blockNamesToMine[i])) then
  142.                 return true
  143.             end
  144.         end
  145.     end
  146. end
  147.  
  148. -- Function to return to where the turtle left off inside the vein of blocks it was mining
  149. -- Use this function when you want the turtle to return after refueling or when the turtle emptied it's inventory
  150. -- and needs to return to inside a vein.
  151. -- In the user's own program, write inventory/refuel function(s) that
  152. -- hook into this function to get the turtle back to a position that the user's program is not aware of.
  153. function backIntoVein()
  154.     for i = 1, #windBlockVein do
  155.         move(windBlockVein[i], nil, nil, skip)
  156.     end
  157. end
  158.  
  159. -- Function to remove redundant 360° turns in the move order tables
  160. local function remove360s()
  161.     if (#unwindBlockVein >= 4) then
  162.         local flag = 0
  163.         for i = 1, #unwindBlockVein do
  164.             if (unwindBlockVein[i] == moveFunctions[4]) then
  165.                 flag = flag + 1
  166.             else
  167.                 flag = 0
  168.             end
  169.             if (flag == 4) then
  170.                 for j = 0,3 do
  171.                     table.remove(unwindBlockVein, i-j)
  172.                     table.remove(windBlockVein, i-j)
  173.                 end
  174.                 remove360s()
  175.                 break
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. -- Function to back the turtle out of the vein it's currently in.
  182. -- Use this function when you want the turtle to refuel or when the inventory is full
  183. -- In the user's own program, write inventory/refuel function(s) that
  184. -- hook into this function to get the turtle to a position that the user's program is aware of.
  185. function backOutOfVein(checkVeinBlocks)
  186.     remove360s()
  187.     for i = #unwindBlockVein, 1, -1 do
  188.         -- Save move for break checking
  189.         local move = unwindBlockVein[i]
  190.  
  191.         -- When the turtle exhausts the current path it will continue to look for more blocks on different paths
  192.         move(unwindBlockVein[i])
  193.  
  194.         -- Removes the exhausted path from the wind and unwind block vein tables if not checking veinBlocks as the turtle exits the vein
  195.         if (checkVeinBlocks == false) then
  196.             table.remove(unwindBlockVein)
  197.             table.remove(windBlockVein)
  198.         end
  199.  
  200.          -- If the turtle is about to go back, go back and then break into recursively checking for more blocks
  201.          if ((move == moveFunctions[2]  or move == moveFunctions[5] or move == moveFunctions[6])and checkVeinBlocks == false)  then
  202.             break -- Breaks back somewhere into recursive calls of scanAdjBlocks function
  203.          end
  204.        
  205.     end
  206. end
  207.  
  208. -- Function to scan adjacent blocks
  209. function scanAdjBlocks(fuelCheck, inventoryCheck)
  210.     -- Functions will be skipped if no function is passed in API call
  211.     fuelCheck = fuelCheck or skip
  212.     inventoryCheck = inventoryCheck or skip
  213.     -- Check block in front of turtle. If block matches blocksToMine, mine block, move into block-space. Start scanning again
  214.     -- If block doesn't match, turnLeft and try inspecting again. Repeat for all directions
  215.     for _ = 1, 4 do
  216.         if blockCheck(scan(turtle.inspect)) then
  217.             dig(turtle.dig, turtle.detect, inventoryCheck, turtle.inspect)
  218.             move(moveFunctions[1], moveFunctions[1], moveFunctions[2], fuelCheck )
  219.             scanAdjBlocks(fuelCheck, inventoryCheck)
  220.         end
  221.         move(moveFunctions[3], moveFunctions[3], moveFunctions[4], skip)
  222.     end
  223.     if blockCheck(scan(turtle.inspectDown)) then
  224.         dig(turtle.digDown, turtle.detectDown, inventoryCheck, turtle.inspectDown)
  225.         move(moveFunctions[6], moveFunctions[6], moveFunctions[5], fuelCheck)
  226.         scanAdjBlocks(fuelCheck, inventoryCheck)
  227.     end
  228.     if blockCheck(scan(turtle.inspectUp)) then
  229.         dig(turtle.digUp, turtle.detectUp, inventoryCheck, turtle.inspectUp)
  230.         move(moveFunctions[5], moveFunctions[5], moveFunctions[6], fuelCheck)
  231.         scanAdjBlocks(inventoryCheck, fuelCheck)
  232.     end
  233.     backOutOfVein(false) -- False argument as the turtle is completely done with the vein
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement