Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print "functions: mining your inserted specifications, and detecting for fuel levels for the task"
- -- Direction constants
- local DIR_FORWARD = 0
- local DIR_RIGHT = 1
- local DIR_BACKWARD = 2
- local DIR_LEFT = 3
- -- Turtle's position and direction
- local xPos, yPos, dir = 0, 0, DIR_FORWARD
- -- Function to update the turtle's position
- local function updatePosition()
- if dir == DIR_FORWARD then
- xPos = xPos + 1
- elseif dir == DIR_RIGHT then
- yPos = yPos + 1
- elseif dir == DIR_BACKWARD then
- xPos = xPos - 1
- elseif dir == DIR_LEFT then
- yPos = yPos - 1
- end
- end
- -- Function to mine forward and ensure a 2-block tall tunnel
- local function mineForward()
- turtle.dig()
- turtle.forward()
- turtle.digUp()
- updatePosition()
- end
- -- Function to turn the turtle left and update direction
- local function turnLeft()
- turtle.turnLeft()
- dir = (dir - 1) % 4
- end
- -- Function to turn the turtle right and update direction
- local function turnRight()
- turtle.turnRight()
- dir = (dir + 1) % 4
- end
- -- Function to turn around at the end of a row
- local function turnAround(isLeftTurn)
- if isLeftTurn then
- turnLeft()
- mineForward()
- turnLeft()
- else
- turnRight()
- mineForward()
- turnRight()
- end
- end
- -- Function to calculate fuel requirements
- local function calculateFuelRequired(length, width)
- local forwardMovements = length * width
- local turnAroundMovements = (width - 1) * 2
- local returnMovements = width
- local totalMovements = forwardMovements + turnAroundMovements + returnMovements
- return totalMovements
- end
- -- Function to return to starting position
- local function returnToStart()
- if dir == DIR_FORWARD or dir == DIR_BACKWARD then
- if xPos > 0 then
- while xPos > 0 do
- turtle.back()
- xPos = xPos - 1
- end
- else
- turnLeft()
- turnLeft()
- while xPos < 0 do
- turtle.forward()
- xPos = xPos + 1
- end
- turnLeft()
- turnLeft()
- end
- elseif dir == DIR_LEFT or dir == DIR_RIGHT then
- turnLeft()
- while xPos ~= 0 do
- turtle.forward()
- xPos = xPos + (dir == DIR_LEFT and -1 or 1)
- end
- turnRight()
- end
- if yPos > 0 then
- while yPos > 0 do
- turtle.back()
- yPos = yPos - 1
- end
- else
- turnLeft()
- turnLeft()
- while yPos < 0 do
- turtle.forward()
- yPos = yPos + 1
- end
- turnLeft()
- turnLeft()
- end
- while dir ~= DIR_FORWARD do
- turnLeft()
- end
- end
- -- Main strip mining function
- local function stripMine(length, width)
- local isLeftTurn = true
- for w = 1, width do
- for l = 1, length do
- if turtle.getFuelLevel() <= calculateFuelRequired(length - l, width - w) + width then
- print("Low on fuel! Returning to start.")
- returnToStart()
- return
- end
- mineForward()
- end
- if w < width then
- turnAround(isLeftTurn)
- isLeftTurn = not isLeftTurn
- end
- end
- returnToStart()
- end
- -- Main function to initiate strip mining
- local function main()
- print("Enter the length of the strip mine:")
- local length = tonumber(io.read())
- print("Enter the width of the strip mine:")
- local width = tonumber(io.read())
- local fuelRequired = calculateFuelRequired(length, width)
- if turtle.getFuelLevel() < fuelRequired then
- print("Not enough fuel to complete the task. Required: " .. fuelRequired .. ", Available: " .. turtle.getFuelLevel())
- return
- end
- stripMine(length, width)
- print("Mining complete!")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment