MtnMCG

SmartMine

Jul 21st, 2024 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | Gaming | 0 0
  1. print "functions: mining your inserted specifications, and detecting for fuel levels for the task"
  2. -- Direction constants
  3. local DIR_FORWARD = 0
  4. local DIR_RIGHT = 1
  5. local DIR_BACKWARD = 2
  6. local DIR_LEFT = 3
  7.  
  8. -- Turtle's position and direction
  9. local xPos, yPos, dir = 0, 0, DIR_FORWARD
  10.  
  11. -- Function to update the turtle's position
  12. local function updatePosition()
  13.     if dir == DIR_FORWARD then
  14.         xPos = xPos + 1
  15.     elseif dir == DIR_RIGHT then
  16.         yPos = yPos + 1
  17.     elseif dir == DIR_BACKWARD then
  18.         xPos = xPos - 1
  19.     elseif dir == DIR_LEFT then
  20.         yPos = yPos - 1
  21.     end
  22. end
  23.  
  24. -- Function to mine forward and ensure a 2-block tall tunnel
  25. local function mineForward()
  26.     turtle.dig()
  27.     turtle.forward()
  28.     turtle.digUp()
  29.     updatePosition()
  30. end
  31.  
  32. -- Function to turn the turtle left and update direction
  33. local function turnLeft()
  34.     turtle.turnLeft()
  35.     dir = (dir - 1) % 4
  36. end
  37.  
  38. -- Function to turn the turtle right and update direction
  39. local function turnRight()
  40.     turtle.turnRight()
  41.     dir = (dir + 1) % 4
  42. end
  43.  
  44. -- Function to turn around at the end of a row
  45. local function turnAround(isLeftTurn)
  46.     if isLeftTurn then
  47.         turnLeft()
  48.         mineForward()
  49.         turnLeft()
  50.     else
  51.         turnRight()
  52.         mineForward()
  53.         turnRight()
  54.     end
  55. end
  56.  
  57. -- Function to calculate fuel requirements
  58. local function calculateFuelRequired(length, width)
  59.     local forwardMovements = length * width
  60.     local turnAroundMovements = (width - 1) * 2
  61.     local returnMovements = width
  62.     local totalMovements = forwardMovements + turnAroundMovements + returnMovements
  63.     return totalMovements
  64. end
  65.  
  66. -- Function to return to starting position
  67. local function returnToStart()
  68.     if dir == DIR_FORWARD or dir == DIR_BACKWARD then
  69.         if xPos > 0 then
  70.             while xPos > 0 do
  71.                 turtle.back()
  72.                 xPos = xPos - 1
  73.             end
  74.         else
  75.             turnLeft()
  76.             turnLeft()
  77.             while xPos < 0 do
  78.                 turtle.forward()
  79.                 xPos = xPos + 1
  80.             end
  81.             turnLeft()
  82.             turnLeft()
  83.         end
  84.     elseif dir == DIR_LEFT or dir == DIR_RIGHT then
  85.         turnLeft()
  86.         while xPos ~= 0 do
  87.             turtle.forward()
  88.             xPos = xPos + (dir == DIR_LEFT and -1 or 1)
  89.         end
  90.         turnRight()
  91.     end
  92.  
  93.     if yPos > 0 then
  94.         while yPos > 0 do
  95.             turtle.back()
  96.             yPos = yPos - 1
  97.         end
  98.     else
  99.         turnLeft()
  100.         turnLeft()
  101.         while yPos < 0 do
  102.             turtle.forward()
  103.             yPos = yPos + 1
  104.         end
  105.         turnLeft()
  106.         turnLeft()
  107.     end
  108.  
  109.     while dir ~= DIR_FORWARD do
  110.         turnLeft()
  111.     end
  112. end
  113.  
  114. -- Main strip mining function
  115. local function stripMine(length, width)
  116.     local isLeftTurn = true
  117.     for w = 1, width do
  118.         for l = 1, length do
  119.             if turtle.getFuelLevel() <= calculateFuelRequired(length - l, width - w) + width then
  120.                 print("Low on fuel! Returning to start.")
  121.                 returnToStart()
  122.                 return
  123.             end
  124.             mineForward()
  125.         end
  126.         if w < width then
  127.             turnAround(isLeftTurn)
  128.             isLeftTurn = not isLeftTurn
  129.         end
  130.     end
  131.     returnToStart()
  132. end
  133.  
  134. -- Main function to initiate strip mining
  135. local function main()
  136.     print("Enter the length of the strip mine:")
  137.     local length = tonumber(io.read())
  138.     print("Enter the width of the strip mine:")
  139.     local width = tonumber(io.read())
  140.     local fuelRequired = calculateFuelRequired(length, width)
  141.     if turtle.getFuelLevel() < fuelRequired then
  142.         print("Not enough fuel to complete the task. Required: " .. fuelRequired .. ", Available: " .. turtle.getFuelLevel())
  143.         return
  144.     end
  145.     stripMine(length, width)
  146.     print("Mining complete!")
  147. end
  148.  
  149. main()
  150.  
Advertisement
Add Comment
Please, Sign In to add comment