Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- StripMineCC.lua
- -- A script for a turtle to strip mine in Minecraft using ComputerCraft
- -- Initialize position and direction
- local position = {x = 0, y = 0, z = 0}
- local direction = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
- -- Initialize log file
- local logFile = fs.open("log.txt", "w")
- local function log(message)
- local timestamp = os.date("%Y-%m-%d %H:%M:%S")
- local logMessage = "[" .. timestamp .. "] " .. message
- print(logMessage)
- logFile.writeLine(logMessage)
- logFile.flush()
- end
- -- Save position to a file
- local function savePosition()
- log("Saving position: " .. textutils.serialize(position))
- local file = fs.open("position.txt", "w")
- file.write(textutils.serialize(position))
- file.close()
- end
- -- Load position from a file
- local function loadPosition()
- if fs.exists("position.txt") then
- log("Loading position from file.")
- local file = fs.open("position.txt", "r")
- position = textutils.unserialize(file.readAll())
- file.close()
- log("Loaded position: " .. textutils.serialize(position))
- else
- log("Position file not found.")
- end
- end
- -- Refuel the turtle
- local function refuel()
- log("Checking fuel level.")
- while turtle.getFuelLevel() < 100 do
- log("Fuel level is low: " .. turtle.getFuelLevel())
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel()
- log("Refueled using slot " .. i)
- break
- end
- end
- if turtle.getFuelLevel() < 100 then
- log("Waiting for user to add fuel.")
- print("Please add fuel to the turtle.")
- os.sleep(5)
- end
- end
- log("Fuel level sufficient: " .. turtle.getFuelLevel())
- end
- -- Update position based on movement
- local function updatePosition(axis, delta)
- position[axis] = position[axis] + delta
- log("Updated position: " .. textutils.serialize(position))
- savePosition()
- end
- -- Move functions
- local function moveForward()
- log("Attempting to move forward.")
- if turtle.forward() then
- log("Moved forward successfully.")
- if direction == 0 then
- updatePosition("z", -1)
- elseif direction == 1 then
- updatePosition("x", 1)
- elseif direction == 2 then
- updatePosition("z", 1)
- elseif direction == 3 then
- updatePosition("x", -1)
- end
- return true
- else
- log("Failed to move forward.")
- end
- return false
- end
- local function moveUp()
- log("Attempting to move up.")
- if turtle.up() then
- log("Moved up successfully.")
- updatePosition("y", 1)
- return true
- else
- log("Failed to move up.")
- end
- return false
- end
- local function moveDown()
- log("Attempting to move down.")
- if turtle.down() then
- log("Moved down successfully.")
- updatePosition("y", -1)
- return true
- else
- log("Failed to move down.")
- end
- return false
- end
- -- Turn functions
- local function turnLeft()
- log("Turning left.")
- turtle.turnLeft()
- direction = (direction - 1 + 4) % 4
- log("New direction: " .. direction)
- end
- local function turnRight()
- log("Turning right.")
- turtle.turnRight()
- direction = (direction + 1) % 4
- log("New direction: " .. direction)
- end
- -- Return to origin
- local function returnToOrigin()
- log("Returning to origin.")
- -- Return to y=0
- while position.y > 0 do moveDown() end
- while position.y < 0 do moveUp() end
- -- Return to x=0
- if position.x > 0 then
- while direction ~= 3 do turnLeft() end
- while position.x > 0 do moveForward() end
- elseif position.x < 0 then
- while direction ~= 1 do turnLeft() end
- while position.x < 0 do moveForward() end
- end
- -- Return to z=0
- if position.z > 0 then
- while direction ~= 0 do turnLeft() end
- while position.z > 0 do moveForward() end
- elseif position.z < 0 then
- while direction ~= 2 do turnLeft() end
- while position.z < 0 do moveForward() end
- end
- -- Face original direction (north)
- while direction ~= 0 do turnLeft() end
- log("Returned to origin: " .. textutils.serialize(position))
- end
- -- Deposit items in chest
- local function depositItems()
- log("Depositing items.")
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- log("Dropped items from slot " .. i)
- end
- end
- -- Strip mine at y=11
- local function stripMine()
- log("Starting strip mining.")
- -- Move to y=11
- while position.y > 11 do moveDown() end
- while position.y < 11 do moveUp() end
- -- Start mining
- for tunnel = 1, 10 do -- Adjust the number of tunnels as needed
- log("Starting tunnel " .. tunnel)
- for step = 1, 20 do -- Adjust tunnel length as needed
- log("Mining step " .. step .. " in tunnel " .. tunnel)
- refuel()
- while not moveForward() do
- turtle.dig()
- log("Dug forward.")
- end
- turtle.digUp()
- log("Dug upward.")
- turtle.digDown()
- log("Dug downward.")
- -- Check if inventory is full
- local isFull = true
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- isFull = false
- break
- end
- end
- if isFull then
- log("Inventory full. Returning to origin to deposit items.")
- returnToOrigin()
- depositItems()
- log("Returning to mining position.")
- -- Return to the current tunnel and step
- while position.y > 11 do moveDown() end
- while position.y < 11 do moveUp() end
- for _ = 1, tunnel - 1 do
- moveForward()
- turnRight()
- end
- for _ = 1, step do moveForward() end
- end
- end
- -- Turn around and return to start of tunnel
- turnRight()
- turnRight()
- for _ = 1, 20 do moveForward() end
- turnRight()
- -- Move to the next tunnel
- moveForward()
- turnRight()
- end
- log("Finished strip mining.")
- end
- -- Ask for current position if not saved before
- if not fs.exists("position.txt") then
- log("Position file not found. Asking user for current position.")
- print("Position file not found. Please enter the turtle's current position.")
- print("Enter X coordinate:")
- position.x = tonumber(read())
- print("Enter Y coordinate:")
- position.y = tonumber(read())
- print("Enter Z coordinate:")
- position.z = tonumber(read())
- print("Enter direction (0 = north, 1 = east, 2 = south, 3 = west):")
- direction = tonumber(read())
- savePosition()
- end
- -- Main program
- log("Starting main program.")
- loadPosition()
- log("refueling turtle.")
- refuel()
- log("Starting strip mining.")
- stripMine()
- log("Strip mining complete. Returning to origin and depositing items.")
- returnToOrigin()
- log("Depositing items.")
- depositItems()
- log("Mining complete. Returned to origin and deposited items.")
- logFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement