ebrobertson

Mining Toolkit Mk2

May 26th, 2023 (edited)
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.33 KB | Gaming | 0 0
  1. --[[
  2.    Turtle Mining Script Mk2
  3.    Author: Evan Robertson
  4.    Created: 2023-05-26  
  5.    Updated: 2023-06-05
  6.    Todo -
  7.    - Make item find and place more efficient - any time we're looping through
  8.    the turtle's inventory, use the inventory table instead.
  9.    - Ideally a better way to indicate if a previously empty slot has a new item in it
  10.    - more mining patterns, custom patterns.  
  11. ]]
  12.  
  13. --[[
  14.    Define Globals
  15.    Only to be set using get/setter functions
  16. ]]
  17. local pattern -- the mining pattern selected
  18. local steps   -- number of steps taken/blocks moved
  19. local reserve -- amount of fuel to have leftover
  20. local distanceToMine -- the distance to mine set by player
  21. local mostRecentNilIndex -- the most recent index that was nil during checkInventory
  22.  
  23. --[[
  24.    Set Globals
  25. ]]
  26. pattern            = ""
  27. steps              = 0
  28. reserve            = 10
  29. distanceToMine     = 1
  30. mostRecentNilIndex = 1
  31.  
  32. --[[
  33.    Lists
  34.    These are hardcoded lists used for validation
  35.    Example - comparing the inspected block to a list of blocks
  36. ]]
  37. local noFuelList = {
  38.    "minecraft:torch",
  39.    "tconstruct:stone_torch"
  40. }
  41. local buildMaterialList = {
  42.    "minecraft:cobblestone",
  43.    "minecraft:dirt"
  44. }
  45. local noDropList = {
  46.    "minecraft:torch",
  47.    "tconstruct:stone_torch",
  48.    "minecraft:coal"
  49. }
  50. local noBreakList = {
  51.    "minecraft:chest",
  52.    "ironchest:iron_chest"
  53. }
  54. -- Used to track contents of turtle inventory
  55. local inventory = {}
  56.  
  57. --[[
  58.    moveCursorDown
  59.    A simple function to move the cursor down one line and to the start of the line when writing to screen
  60. ]]
  61. local function moveCursorDown(line)
  62.    term.setCursorPos(1,line)
  63.    line = line + 1
  64.    return line
  65. end -- end moveCursorDown
  66.  
  67. --[[
  68.    displayLogs
  69.    displays a sort of menu with stats and info about the run
  70. ]]
  71. local function displayLogs(rows,steps,depth,height,posY,portionOfLoop, goingForward)
  72.    local line = 1
  73.    term.clear()
  74.    line = moveCursorDown(line)
  75.    term.write(" ___________")
  76.    line = moveCursorDown(line)
  77.    term.write("|           |")
  78.    line = moveCursorDown(line)
  79.    term.write("|Loop ")
  80.    term.write(portionOfLoop)
  81.    if portionOfLoop == "start" then
  82.       term.write(" |")
  83.    else
  84.       term.write("   |")
  85.    end
  86.    line = moveCursorDown(line)
  87.    term.write("|Rows:   ")
  88.    term.write(rows)
  89.    term.write("|")
  90.    line = moveCursorDown(line)
  91.    term.write("|Steps:  ")
  92.    term.write(steps)
  93.    term.write("|")
  94.    line = moveCursorDown(line)
  95.    term.write("|Depth:  ")
  96.    term.write(depth)
  97.    term.write("|")
  98.    line = moveCursorDown(line)
  99.    term.write("|Height: ")
  100.    term.write(height)
  101.    term.write("|")
  102.    line = moveCursorDown(line)
  103.    term.write("|Pos Y:  ")
  104.    term.write(posY)
  105.    term.write("|")
  106.    line = moveCursorDown(line)
  107.    term.write("|Direction:")
  108.    if goingForward then
  109.       term.write("f|")
  110.    else
  111.       term.write("b|")
  112.    end
  113.    line = moveCursorDown(line)
  114.    term.write("|___________|")
  115.    line = moveCursorDown(line)
  116. end -- end displayLogs
  117.  
  118. --[[
  119.    setDistanceToMine
  120.    sets the distance the turtle should mine
  121. ]]
  122. local function setDistanceToMine(distance)
  123.    distanceToMine = distance
  124. end -- end setDistanceToMine
  125.  
  126. --[[
  127.    getDistanceToMine
  128.    gets the distance the turtle should mine
  129. ]]
  130. local function getDistanceToMine()
  131.    return distanceToMine
  132. end -- end setDistanceToMine
  133.  
  134. --[[
  135.    setMostRecentNilIndex
  136.    sets the last indexed inventory slot that was empty/nile
  137. ]]
  138. local function setMostRecentNilIndex(newIndex)
  139.    mostRecentNilIndex = newIndex
  140. end -- end setMostRecentNilIndex
  141.  
  142. --[[
  143.    getMostRecentNilIndex
  144.    gets the last indexed inventory slot that was empty/nile
  145. ]]
  146. local function getMostRecentNilIndex()
  147.    return mostRecentNilIndex
  148. end -- end getMostRecentNilIndex
  149.  
  150. --[[
  151.    displayMenu
  152.    Displays a list of menu options
  153. ]]
  154. local function displayMenu()
  155.    term.clear()
  156.    print("Mining Toolkit Mk2")
  157.    print("")
  158.    print("Please choose an option:")
  159.    print("")
  160.    print("1. Strip mining")
  161.    print("2. Custom Height strip mining")
  162.    print("")
  163.    print("Type help to view a help menu or exit to quit")
  164. end -- end displayMenu
  165.  
  166. --[[
  167.    displayHelpMenu
  168.    Displays a list of help menu options
  169. ]]
  170. local function displayHelpMenu()
  171.    term.clear()
  172.    print("Mining Toolkit Mk2 Help Screen")
  173.    print("")
  174.    print("Please choose an option to view help on:")
  175.    print("")
  176.    print("1. Strip mining")
  177.    print("2. Custom Height strip mining")
  178.    print("")
  179.    print("Enter exit to return to main menu")
  180. end -- end displayHelpMenu
  181.  
  182. --[[
  183.    displayPatternHelp
  184.    Displays a help message for the given pattern
  185.    input - a string representing a valid patern
  186. ]]
  187. local function displayPatternHelp(pattern)
  188.    if type(pattern) ~= "string" then
  189.       print("Must enter a valid selection")
  190.       return
  191.    else
  192.       if pattern == "1" then
  193.          term.clear()
  194.          print("Strip Mine Program:")
  195.          print("Mines a 1x2 (wxh) tunnel for the length specified")
  196.          print("")
  197.          print("Tip: Include some torches, fuel, and cobblestone in the turtle's inventory so it can fill gaps and light up the mine as it goes.")
  198.          print("")
  199.          print("Press Enter to return")
  200.          read()
  201.       elseif pattern == "2" then
  202.          term.clear()
  203.          print("Tall strip Mine Program:")
  204.          print("Mines a 1xn (wxh) tunnel for the length specified, where n is a user specified height")
  205.          print("")
  206.          print("Tip: Include some torches, fuel, and cobblestone in the turtle's inventory so it can fill gaps and light up the mine as it goes.")
  207.          print("")
  208.          print("Press Enter to return")
  209.          read()
  210.       end
  211.    end
  212. end -- end displayPatternHelp
  213.  
  214. --[[
  215.    setSteps
  216.    sets the number of steps taken/blocks moved
  217.    input - the number of steps to increment by (1 by default)
  218.    returns the total number of steps moved
  219. ]]
  220. local function setSteps(incrementBy)
  221.  
  222.    if type(incrementBy) == nil then
  223.       steps = steps + 1
  224.    else
  225.       steps = steps + incrementBy
  226.    end
  227.    return steps
  228.  
  229. end -- end setSteps
  230.  
  231. --[[
  232.    setPattern
  233.    input - string, pattern to set
  234.    sets the global pattern to be used for mining  
  235. ]]
  236. local function setPattern(newPattern)
  237.  
  238.    if type(newPattern) ~= "string" then
  239.       print("A valid pattern must be provided (string) not,", type(newPattern))
  240.    elseif newPattern == "stripMine" then      
  241.       pattern = newPattern
  242.    else
  243.       print("Not a valid pattern.")
  244.    end
  245.  
  246. end -- end setPattern
  247.  
  248. --[[
  249.    findItem
  250.    input - string, item to find
  251.    returns the inventory slot the item is in or nil if not found
  252. ]]
  253. local function findItem(itemName)
  254.    local item = {}
  255.    if type(itemName) ~= "string" then
  256.       print("A valid item name must be provided (string) not", type(itemName))
  257.    else
  258.       for i=1,16 do
  259.          turtle.select(i)
  260.          item = turtle.getItemDetail()
  261.          if type(item) == "table" then
  262.             if item.name == itemName then
  263.                turtle.select(1)
  264.                return i
  265.             end
  266.          end  
  267.       end
  268.       -- item not found
  269.       print("Item not found: ", itemName)
  270.       turtle.select(1)
  271.       return nil
  272.    end
  273. end -- end findItem
  274.  
  275. --[[
  276.    placeItem
  277.    input - inventory slot containing item to place, direction to place
  278.    returns true if place was successful and the reason if it failed
  279. ]]
  280. local function placeItem(itemSlot, direction)
  281.  
  282.    local placeSuccess = false
  283.    local reason       = ""
  284.  
  285.    if type(itemSlot) ~= "number" then
  286.       print("A valid inventory slot must be provided (number) not", type(itemSlot))
  287.    elseif type(direction) ~= "string" then
  288.       print("A valid direction must be provided (string)(up,down) not", type(direction))
  289.    else
  290.       turtle.select(itemSlot)
  291.       if direction == "up" then
  292.          placeSuccess, reason = turtle.placeUp()
  293.       elseif direction == "down" then
  294.          placeSuccess, reason = turtle.placeDown()
  295.       elseif direction == "backwards" then
  296.          turtle.turnRight()
  297.          turtle.turnRight()
  298.          placeSuccess, reason = turtle.place()
  299.          turtle.turnRight()
  300.          turtle.turnRight()
  301.       elseif direction == "forwards" then
  302.          placeSuccess, reason = turtle.place()
  303.       else
  304.          print("Invalid direction")
  305.       end
  306.    end
  307.    turtle.select(1)
  308.    return placeSuccess, reason
  309.  
  310. end -- end placeItem
  311.  
  312. --[[
  313.    refuel
  314.    checks inventory for fuel items and refules the turtle with them
  315.    retuns true if refuel was successful
  316. ]]
  317. local function refuel()
  318.    local item = {}
  319.    local refuelSuccess = false
  320.    for i=1,16 do
  321.       turtle.select(i)
  322.       item = turtle.getItemDetail()
  323.       if type(item) == "table" then
  324.          for key, value in pairs(noFuelList) do
  325.             if value ~= item.name then
  326.                refuelSuccess = turtle.refuel()
  327.                if refuelSuccess then
  328.                   turtle.select(1)
  329.                   return refuelSuccess
  330.                end
  331.             end
  332.          end
  333.       end      
  334.    end
  335.    turtle.select(1)
  336.    return refuelSuccess
  337. end -- end refuel
  338.  
  339. --[[
  340.    indexInventory
  341.    Loops through the inventory and builds the inventory table
  342. ]]
  343. local function indexInventory()
  344.    local item = {}
  345.    for i=1,16 do
  346.       turtle.select(i)
  347.       item = turtle.getItemDetail()
  348.       -- if item slot is not empty
  349.       if type(item) == "table" then
  350.          inventory[i] = item.name
  351.       else
  352.          -- else that inventory slot is empty so set to nil
  353.          inventory[i] = nil
  354.       end
  355.    end -- for loop
  356. end -- end indexInventory
  357.  
  358. --[[
  359.    sortInventory
  360.    checks the currently selected item and loops through inventory
  361.    to find matching ones. When matching is found, moves to appropriate slot
  362.    returns the result of transferTo, which is true if some items were moved  
  363. ]]
  364. local function sortInventory()
  365.    indexInventory()
  366.    for i=1,16 do
  367.       -- if slot is not empty, and is not full, compare to other slots
  368.       if inventory[i] ~= nil and turtle.getItemCount(i) < 64 then
  369.          for z=1,16 do
  370.             if i ~= z then              
  371.                -- if slot is not empty, and is not full, do the comparison
  372.                if inventory[z] ~= nil and turtle.getItemCount(z) < 64 then
  373.                   if inventory[i] == inventory[z] then
  374.                      turtle.select(z)
  375.                      turtle.transferTo(i, 64)
  376.                      -- if we managed to move all the items
  377.                      if turtle.getItemCount(z) == 0 then
  378.                         -- reset z to nil now that it's empty
  379.                         inventory[z] = nil
  380.                      end
  381.                   end -- if items match
  382.                end -- if z is not empty or full
  383.             end -- if i ~= zdifferent slots
  384.          end -- for z 1-16
  385.       end -- if i slot not empty or full
  386.    end -- for i 1-16
  387.    turtle.select(1)
  388. end -- end sortInventory
  389.  
  390. --[[
  391.    compareBlockToInventory
  392.    Compare the block in front of the turtle with inventory
  393.    and select matching blocks in inventory
  394.    won't work with dust or gem types so sort inventory is still relevant, unless dust/gems get coordinated list
  395. ]]
  396. local function compareBlockToInventory()
  397. end -- end compareBlockToInventory
  398.  
  399. --[[
  400.    checkFullInventory
  401.    Checks to see if the inventory is full
  402.    returns true if its full
  403. ]]
  404. local function checkFullInventory()
  405.    local item = {}
  406.    local lastIndex =  turtle.getSelectedSlot()
  407.  
  408.    -- if the inventory table has never been indexed, then 1st element will be nil
  409.    -- so index it once, then continue checking inventory.
  410.    if inventory[1] == nil then
  411.       indexInventory()
  412.    else
  413.       -- check the most recent index that was set as nil  
  414.       turtle.select(getMostRecentNilIndex())
  415.       item = turtle.getItemDetail()
  416.       turtle.select(lastIndex)
  417.    end
  418.  
  419.    -- if its still nil, aka doesn't return a table, then index inventory again
  420.    -- ensures hopefull no items are ever missed while mining
  421.    if type(item) == "table" then
  422.       indexInventory()
  423.    end
  424.  
  425.    -- check if inventory table has any nil entries
  426.    for i=1,16 do
  427.       -- if there are any nil slots after indexing, then inventory is not full.
  428.       if inventory[i] == nil then
  429.          setMostRecentNilIndex(i)
  430.          return false      
  431.       end
  432.       -- if no nil slots were found, inventory is "full", meaning all slots have an item.
  433.    end -- end for loop
  434.    return true
  435. end -- end checkFullInventory
  436.  
  437. --[[
  438.    moveForward
  439.    moves the turtle forward one block, tracks steps
  440. ]]
  441. local function moveFoward()
  442.    turtle.forward()
  443.    setSteps(1)
  444. end -- end moveForward
  445.  
  446. --[[
  447.    moveUpward
  448.    moves the turtle up one block
  449. ]]
  450. local function moveUpward()
  451.    turtle.up()
  452. end -- end moveUpward
  453.  
  454. --[[
  455.    moveDownward
  456.    moves the turtle up one block
  457. ]]
  458. local function moveDownward()
  459.    turtle.down()
  460. end -- end moveDownward
  461.  
  462. --[[
  463.    digForwardTillEmpty
  464.    digs until there is no block in front of the turtle
  465. ]]
  466. local function digForwardTillEmpty()
  467.    local block
  468.    local hasBlock
  469.    while turtle.detect() do
  470.       hasBlock, block = turtle.inspect()
  471.       for key, value in pairs(noBreakList) do
  472.          if block.name == value then
  473.             print("Tried braking a block stored on the no break list! Check the block in front. Press enter to resume or hold ctrl+t to terminate the program. WARNING: resuming could result in a chest being broken.")
  474.             read()            
  475.          end
  476.       end
  477.       turtle.dig()      
  478.    end
  479. end -- end digForwardTillEmpty
  480.  
  481. --[[
  482.    digUpTillEmpty
  483.    digs until there is no block above the turtle
  484. ]]
  485. local function digUpTillEmpty()
  486.    while turtle.detectUp() do
  487.       turtle.digUp()      
  488.    end
  489. end -- end digUpTillEmpty
  490.  
  491. --[[
  492.    digDownTillEmpty
  493.    digs until there is no block below the turtle
  494. ]]
  495. local function digDownTillEmpty()
  496.    while turtle.detectDown() do
  497.       turtle.digDown()      
  498.    end
  499. end -- end digDownTillEmpty
  500.  
  501. --[[
  502.    emptyInventory
  503.    Empties the turtles inventory into an inventory in front of the turtle
  504.    Prompts the user to place an inventory by the turtle if none is found
  505.    returns false if no inventory was found
  506. ]]
  507. local function emptyInventory()
  508.    local inv   = turtle.inspect()
  509.    local item  = {}
  510.    local found =  false
  511.    local cobbleKept = false
  512.  
  513.    if type(inv) == nil then
  514.       print("Please put an inventory/chest behind turtle's start position to empty the inventory. Press Enter to resume.")
  515.       read()
  516.       emptyInventory()
  517.       return
  518.    end
  519.    -- Loop through inventory
  520.    for i=1,16 do
  521.       found = false
  522.       turtle.select(i)
  523.       item = turtle.getItemDetail()
  524.       if type(item) == "table" then
  525.          -- first time we find cobble in inventory, keep it. Always have one stack of building material for
  526.          -- filling gaps in floors and ceilings and keeping lava and water out.
  527.          if item.name == "minecraft:cobblestone" and not cobbleKept then
  528.             cobbleKept = true
  529.          else
  530.             -- Loop through the no drop list
  531.             -- If item is found in no drop list, do not drop into inventory
  532.             for key, value in pairs(noDropList) do            
  533.                -- break key/value loop if one is found
  534.                if item.name == value then
  535.                   found = true
  536.                   break
  537.                end -- if item is found in noDropList
  538.                -- only drop coal in chest if fuel is over 2560 (32 coal)
  539.                if item.name == "minecraft:coal" and
  540.                item.name == value and
  541.                (not turtle.getFuelLevel > 2560) then
  542.                   found = true
  543.                   break
  544.                end -- if item is coal
  545.             end -- for key value
  546.          end -- if item is cobble and we haven't stored one stack yet
  547.          if not found then
  548.             turtle.drop()
  549.          end -- if no matching "no drop" item was found
  550.       end -- if slot is not empty
  551.    end -- for loop
  552.    turtle.select(1)
  553. end -- end emptyInventory
  554.  
  555. --[[
  556.    fuelToDistanceCheck
  557.    Uses the turns and steps made to calculate how much fuel is required to get back
  558.    returns true if can return home
  559. ]]
  560. local function fuelToDistanceCheck(currentLoopSteps, rows)
  561.    local totalDistance
  562.    local success
  563.    local enoughFuel = true
  564.    totalDistance = currentLoopSteps + rows
  565.    if turtle.getFuelLevel() <= totalDistance + reserve then
  566.       success = refuel()
  567.       if success then
  568.          if turtle.getFuelLevel() <= totalDistance + reserve then
  569.             enoughFuel = false
  570.          end -- second check
  571.       else
  572.          enoughFuel = false
  573.       end -- if refuel successful
  574.    end -- if fuelLevel is less than or equal to distance + reserve
  575.  
  576.    return enoughFuel
  577.  
  578. end -- end fuelToDistanceCheck
  579.  
  580. --[[
  581.    placeTorch
  582.    places a torch behind the turtle
  583. ]]
  584.  
  585. --[[
  586.    stripMineReturn
  587.    Sends the turtle back to it's starting position
  588.    If there is enough fuel, sends back to where it left off
  589.    returns true if turtle continues where it left off
  590. ]]
  591. local function stripMineReturn(goingForward, currentLoopSteps, rows, height, posY)
  592.    local block
  593.    local isBlock
  594.    local startPosY = posY
  595.    -- first get to Y position 0 aka the ground
  596.    -- if we're above the ground
  597.    if posY > 0 then
  598.  
  599.       for i=1,height do
  600.          -- sanity check, ensure we're not digging below zero
  601.          if posY == 0 then
  602.             break
  603.          end -- end sanity check
  604.          digDownTillEmpty()
  605.          moveDownward()        
  606.          posY = posY - 1
  607.       end -- end for i to height
  608.  
  609.    end -- end if posY > 0
  610.  
  611.    -- position turtle to cross the rows
  612.    -- (ex. if the turtle was traveling forwards, away from start pos,
  613.    --  this should turn to face the 'left' wall)
  614.    if goingForward then
  615.       turtle.turnLeft()
  616.    else
  617.       turtle.turnRight()
  618.    end
  619.  
  620.    -- if we completed any rows to begin with
  621.    if rows > 0 then
  622.       -- cut across all the rows back to row 1
  623.       for i=1,rows do
  624.          digUpTillEmpty()
  625.          digForwardTillEmpty()
  626.          moveFoward()
  627.       end
  628.    end
  629.  
  630.    -- face towards chest/start position
  631.    turtle.turnLeft()
  632.  
  633.    -- if going away from home, current loop steps will represent how far we are from the chest
  634.    -- but going towards home, steps represent how close we are. i.e. if steps are 3 and depth is 5, we are 2 away
  635.    --, so only loop twice, or depth - steps
  636.    if goingForward then
  637.       for i=1,currentLoopSteps do
  638.          digUpTillEmpty()
  639.          digForwardTillEmpty()
  640.          moveFoward()
  641.       end  
  642.    else
  643.       for i=1, currentLoopSteps do
  644.          digUpTillEmpty()
  645.          digForwardTillEmpty()
  646.          moveFoward()
  647.       end  
  648.    end
  649.  
  650.    -- check if the block in front of turtle is a chest
  651.    isBlock, block = turtle.inspect()
  652.    if type(block) ~= "table" then
  653.       print("No block found at start position. There should be a chest to empty the turtle's inventory. Press enter to empty inventory.")
  654.    else
  655.       if (block.name ~= "minecraft:chest")      and
  656.          (block.name ~= "ironchest:iron_chest") then
  657.          print("no chest found at start position/in front of turtle. If there is a chest, press enter to empty inventory.")
  658.          print("block found:", block.name)
  659.          read()
  660.       end
  661.    end
  662.  
  663.    -- empty the contents of the inventory into a chest
  664.    emptyInventory()
  665.  
  666.    -- face forward
  667.    turtle.turnRight()
  668.    turtle.turnRight()
  669.  
  670.    --check fuel
  671.    if fuelToDistanceCheck(currentLoopSteps, rows) then
  672.       -- if fuel level is good, resume mining
  673.      
  674.       -- return to previous depth of row
  675.       -- should this be 0 or 1?
  676.       for i=1,currentLoopSteps do
  677.          read()
  678.          digUpTillEmpty()
  679.          digForwardTillEmpty()
  680.          moveFoward()
  681.       end
  682.  
  683.       -- if we dug a full row or more
  684.       if rows > 0 then
  685.          turtle.turnRight()
  686.  
  687.          -- return to the current row
  688.          for i=1,rows do
  689.             digUpTillEmpty()
  690.             digForwardTillEmpty()
  691.             moveFoward()
  692.          end
  693.  
  694.          -- face the direction the turtle was originally going
  695.          if goingForward then
  696.             turtle.turnLeft()
  697.          else
  698.             turtle.turnRight()
  699.          end
  700.       end      
  701.  
  702.       -- return to starting Y position
  703.       if startPosY > 0 then
  704.  
  705.          for i=1,height do
  706.             -- sanity check, ensure we're not digging abouve height
  707.             if posY == height then
  708.                break
  709.             end -- end sanity check
  710.             digUpTillEmpty()
  711.             moveUpward()        
  712.             posY = posY + 1
  713.          end -- end for i to height
  714.    
  715.       end -- end if startPosY > 0
  716.  
  717.       return true, posY
  718.    else
  719.       return false, posY
  720.    end -- if enough fuel to keep going
  721.  
  722. end -- end stripMineReturn
  723.  
  724. --[[
  725.    stripMineUpDown
  726.    Digs and moves the turtle up or down depending on height
  727. ]]
  728. local function stripMineUpDown(posY, height)
  729.    -- if less than height, we're not at the cieling, so dig and move up
  730.    if posY < height then
  731.          
  732.       for i=1,height do            
  733.          digUpTillEmpty()
  734.          moveUpward()
  735.          posY = posY + 1
  736.          -- if, for whatever reason, the math is wrong or recursion breaks,
  737.          -- just leave the loop once posY == max height
  738.          if posY == height then
  739.             break
  740.          end
  741.       end -- for i to height
  742.  
  743.       -- once at the cieling, check if ceiling is empty (i.e. - falling lava or water)
  744.       if not turtle.detectUp() and posY == height then
  745.          local itemSlot
  746.          for key, value in pairs(buildMaterialList) do
  747.             itemSlot = findItem(value)
  748.             if itemSlot ~= nil then
  749.                placeItem(itemSlot,"up")
  750.                -- leave for loop
  751.                break
  752.             end -- end if slot not empty
  753.          end -- end for pairs in buildingMaterialList
  754.       end -- end if turtle doesn't detect solid block and posY is == height
  755.  
  756.    elseif posY == height then
  757.  
  758.       for i=1, height do
  759.          digDownTillEmpty()
  760.          moveDownward()
  761.          posY = posY - 1
  762.          -- same redundancy
  763.          if posY == 0 then
  764.             break
  765.          end
  766.       end -- end for i to height
  767.          -- check if floor is empty
  768.       if not turtle.detectDown() and posY < height then
  769.          local itemSlot
  770.          for key, value in pairs(buildMaterialList) do
  771.             itemSlot = findItem(value)
  772.             if itemSlot ~= nil then
  773.                placeItem(itemSlot,"down")
  774.                -- leave for loop
  775.                break
  776.             end -- end if slot not empty
  777.          end -- end for pairs in buildMaterialList
  778.       end -- end if turtle does not detect solid block below and posY is < height
  779.    end
  780.    return posY
  781. end -- end stripMineUpDown
  782.  
  783. --[[
  784.    stripMining
  785.    Makes the turtle dig in a strip mine pattern
  786.    inputs - number, how deep the tunnel goes, boolean if we're facing forward or not,
  787.    number the number of rows dug, rowsSinceTorch, the number of rows since placing torchs
  788. ]]
  789. local function stripMining(depth, goingForward, rows, rowsSinceTorch, height, posY)
  790.    local currentLoopSteps = 0
  791.    local isInventoryFull  = false
  792.    local continue         = false
  793.    local stepsSinceTorch  = 5 -- set to 5 so we always place a torch one block away from start and every wall of each new row
  794.    local stepsToTorch     = 6 -- number of steps to take PER torch
  795.    local placeSuccess     = false
  796.    local torchCount       = math.floor(depth / stepsToTorch )
  797.    local torchesPlaced    = 0  
  798.  
  799.    -- if you're going backwards, then you are 'depth' away from starting position
  800.    -- so instead, we'll start there and count backwards to 0 to keep track of how
  801.    -- far away we are
  802.    -- ... subtract 1 so we end on 0?
  803.    if not goingForward then
  804.       currentLoopSteps = depth - 1
  805.    end
  806.  
  807.    -- torch count is how many times we place a torch during a row/loop
  808.    -- if less than 1, set to 1 so we always place at least one.
  809.    if torchCount < 1 then
  810.       torchCount = 1
  811.    end
  812.  
  813.    -- loop for dig depth (plus one to account for final loop where we ensure column is cleared)
  814.    for i=1,depth+1 do
  815.    
  816.       -- print stats
  817.       displayLogs(rows,currentLoopSteps,depth,height,posY,"start",goingForward)
  818.  
  819.       -- if fuel level is less than remaining distance + reserve
  820.       if turtle.getFuelLevel() < (depth - i + reserve) then
  821.          local refuelSuccess
  822.          refuelSuccess = refuel()
  823.          if not refuelSuccess then
  824.             continue, posY = stripMineReturn(goingForward, currentLoopSteps, rows, height, posY)
  825.             if not continue then
  826.                return
  827.             end
  828.          end
  829.       end                
  830.  
  831.       -- check if ceiling is empty (i.e. - falling lava or water) if you're at the cieling (posY equals height)
  832.       if not turtle.detectUp() and posY == height then
  833.          local itemSlot
  834.          for key, value in pairs(buildMaterialList) do
  835.             itemSlot = findItem(value)
  836.             if itemSlot ~= nil then
  837.                placeItem(itemSlot,"up")
  838.                -- leave for loop
  839.                break
  840.             end
  841.          end
  842.       end      
  843.  
  844.       -- check if floor is empty if you're on the floor (posY less than max height)
  845.       if not turtle.detectDown() and posY < height then
  846.          local itemSlot
  847.          for key, value in pairs(buildMaterialList) do
  848.             itemSlot = findItem(value)
  849.             if itemSlot ~= nil then
  850.                placeItem(itemSlot,"down")
  851.                -- leave for loop
  852.                break
  853.             end
  854.          end        
  855.       end
  856.        
  857.       -- handles moving the turtle up or down before moving forward
  858.       posY = stripMineUpDown(posY, height)
  859.  
  860.       -- if we have reached max depth, don't move forward.
  861.       -- reminder, we do 1 extra loop to ensure we clear the whole column before starting a new row
  862.       if i == depth  then
  863.          -- leave the dig up/down/forward loop and move onto starting a new row
  864.          break
  865.       end -- if reached depth
  866.  
  867.       -- after digging and moving up/down, move forward
  868.       -- if Y position is < height, you're not at the cieling, so dig below  
  869.       digForwardTillEmpty()
  870.       moveFoward()
  871.  
  872.       -- if going backwards, decrement, otherwise increment.
  873.       if not goingForward then
  874.          currentLoopSteps = currentLoopSteps - 1
  875.       else
  876.          currentLoopSteps = currentLoopSteps + 1
  877.       end
  878.       displayLogs(rows,currentLoopSteps,depth,height,posY,"mid",goingForward)
  879.  
  880.       -- Place a torch every four rows.
  881.       if rowsSinceTorch >= 4 then
  882.          -- Place a torch every 6 blocks or the depth if its smaller
  883.          if stepsSinceTorch >= stepsToTorch or (stepsToTorch > depth and stepsSinceTorch >= depth - 2) then      
  884.             -- if we are on top block when placing torch, move to bottom, place torch, then move to top
  885.             if posY == height then
  886.                for i=1,height do
  887.                   digDownTillEmpty()
  888.                   moveDownward()                  
  889.                end
  890.                placeSuccess = placeItem(findItem("tconstruct:stone_torch"),"backwards")
  891.                if not placeSuccess then
  892.                   placeSuccess = placeItem(findItem("minecraft:torch"),"backwards")
  893.                end
  894.                for i = 1,height do
  895.                   digUpTillEmpty()
  896.                   moveUpward()
  897.                end
  898.             else -- else just place like normal
  899.                placeSuccess = placeItem(findItem("tconstruct:stone_torch"),"backwards")
  900.                if not placeSuccess then
  901.                   placeSuccess = placeItem(findItem("minecraft:torch"),"backwards")
  902.                end
  903.             end  
  904.             stepsSinceTorch = 0
  905.             torchesPlaced = torchesPlaced + 1
  906.             -- if we've placed the allotted number of torches for this row,
  907.             -- set rowsSinceTorch to 0 so we can place torches in another 4 rows
  908.             if torchesPlaced >= torchCount then
  909.                torchesPlaced = 0
  910.                rowsSinceTorch = 0
  911.             end
  912.          end -- end if stepsSinceTorch eq 8
  913.       end -- end if rowsSinceTorch eq 4      
  914.      
  915.       isInventoryFull = checkFullInventory()
  916.       if isInventoryFull then
  917.          sortInventory()      
  918.          isInventoryFull = checkFullInventory()
  919.             if isInventoryFull then
  920.             continue, posY = stripMineReturn(goingForward, currentLoopSteps, rows, height, posY)
  921.             if not continue then
  922.                return
  923.             end
  924.          end
  925.       end
  926.  
  927.       stepsSinceTorch = stepsSinceTorch + 1
  928.       -- print stats
  929.       displayLogs(rows,steps,depth,height,posY,"end",goingForward)      
  930.      
  931.    end -- end for loop
  932.  
  933.    rows =  rows + 1
  934.    rowsSinceTorch = rowsSinceTorch + 1  
  935.    
  936.    -- turn to start new shaft
  937.  
  938.    if goingForward then
  939.       turtle.turnRight()  
  940.    else
  941.       turtle.turnLeft()  
  942.    end
  943.  
  944.    digForwardTillEmpty()
  945.    moveFoward()
  946.  
  947.    if goingForward then
  948.       turtle.turnRight()  
  949.    else
  950.       turtle.turnLeft()  
  951.    end
  952.  
  953.    if goingForward then
  954.       goingForward = false
  955.    else
  956.       goingForward = true
  957.    end
  958.  
  959.    stripMining(depth, goingForward, rows, rowsSinceTorch, height, posY)
  960.  
  961. end -- end stripMining
  962.  
  963. local input
  964. local heightToUse
  965. input = ""
  966. heightToUse = 1
  967. while input ~= "exit" do
  968.    displayMenu()
  969.    input = read()
  970.    if input == "1" or input == "2" then
  971.       -- set the height of the strip mine
  972.       -- where the heightToUse is the number of blocks we go up
  973.       -- (so going up 2 blocks means a 3 tall mine shaft)
  974.       if input == "2" then
  975.          print("Enter a value for the height of the strip mine")
  976.          input = read()
  977.          -- subtract 1 from the input. This is just for ease of use for the user.
  978.          heightToUse = input - 1
  979.       else
  980.          heightToUse = 1
  981.       end
  982.       term.clear()
  983.       print("Enter a value for the length of the strip mine")
  984.       input = tonumber(read())
  985.       if type(input) ~= "number" then
  986.          print("depth must be a valid number")
  987.          return
  988.       end
  989.       setDistanceToMine(depth)
  990.      
  991.       -- pass rowsSinceTorch as 4 to place torch on first row
  992.       stripMining(tonumber(input), true, 0,4, heightToUse, 0)
  993.    elseif input == "help" then
  994.       input = ""
  995.       while input ~= "exit" do
  996.          displayHelpMenu()  
  997.          input = read()
  998.          displayPatternHelp(input)
  999.       end      
  1000.       input = ""
  1001.    end
  1002. end
  1003.  
  1004. print("Program exit. Thank you for using Mining Toolkit Mk2")
  1005.  
  1006.  
Add Comment
Please, Sign In to add comment