Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local galymoveAPI = {}
- -- Variables
- local position = { x = 0, y = 0, z = 0 }
- local direction = 0 -- 0: North, 1: East, 2: South, 3: West
- local positionFile = "position.txt"
- -- Functions
- local function breakBlock()
- while turtle.detect() do
- turtle.dig()
- sleep(0.5) -- Wait for block to break
- end
- end
- local function handleGravel()
- while turtle.detect() and turtle.compare() do
- turtle.dig()
- sleep(0.5) -- Wait for gravel to fall
- end
- end
- local function handleEntity()
- while turtle.detect() do
- turtle.attack()
- sleep(0.5) -- Wait for entity to move
- end
- end
- local function moveForward(steps)
- for _ = 1, steps do
- while not turtle.forward() do
- if turtle.detect() then
- breakBlock()
- elseif turtle.detectDown() and turtle.compareDown() then
- handleGravel()
- elseif turtle.detectUp() then
- handleEntity()
- end
- end
- if direction == 0 then
- position.z = position.z + 1
- elseif direction == 1 then
- position.x = position.x + 1
- elseif direction == 2 then
- position.z = position.z - 1
- elseif direction == 3 then
- position.x = position.x - 1
- end
- end
- end
- local function moveBack(steps)
- for _ = 1, steps do
- while not turtle.back() do
- if turtle.detect() then
- breakBlock()
- elseif turtle.detectDown() and turtle.compareDown() then
- handleGravel()
- elseif turtle.detectUp() then
- handleEntity()
- end
- end
- if direction == 0 then
- position.z = position.z - 1
- elseif direction == 1 then
- position.x = position.x - 1
- elseif direction == 2 then
- position.z = position.z + 1
- elseif direction == 3 then
- position.x = position.x + 1
- end
- end
- end
- local function moveUp(steps)
- for _ = 1, steps do
- while not turtle.up() do
- if turtle.detect() then
- breakBlock()
- elseif turtle.detectDown() and turtle.compareDown() then
- handleGravel()
- elseif turtle.detectUp() then
- handleEntity()
- end
- end
- position.y = position.y + 1
- end
- end
- local function moveDown(steps)
- for _ = 1, steps do
- while not turtle.down() do
- if turtle.detect() then
- breakBlock()
- elseif turtle.detectDown() and turtle.compareDown() then
- handleGravel()
- elseif turtle.detectUp() then
- handleEntity()
- end
- end
- position.y = position.y - 1
- end
- end
- local function turnRight()
- turtle.turnRight()
- direction = (direction - 1) % 4
- end
- local function turnLeft()
- turtle.turnLeft()
- direction = (direction + 1) % 4
- end
- local function moveRight(steps)
- turtle.turnRight()
- moveForward(steps or 1)
- turtle.turnLeft()
- position.x = position.x + 1
- end
- local function moveLeft(steps)
- turtle.turnLeft()
- moveForward(steps or 1)
- turtle.turnRight()
- position.x = position.x - 1
- end
- function galymoveAPI.move(direction, steps)
- steps = steps or 1 -- Set default value to 1 if steps is not provided
- if direction == "f" then
- moveForward(steps)
- elseif direction == "b" then
- moveBack(steps)
- elseif direction == "u" then
- moveUp(steps)
- elseif direction == "d" then
- moveDown(steps)
- elseif direction == "r" then
- turnRight()
- elseif direction == "l" then
- turnLeft()
- end
- end
- function galymoveAPI.getPosition()
- return position
- end
- function galymoveAPI.getDirection()
- return direction
- end
- function galymoveAPI.savePositionToFile()
- local file = fs.open(positionFile, "w")
- file.writeLine(position.x)
- file.writeLine(position.y)
- file.writeLine(position.z)
- file.writeLine(direction)
- file.close()
- end
- function galymoveAPI.loadPositionFromFile()
- if fs.exists(positionFile) then
- local file = fs.open(positionFile, "r")
- position.x = tonumber(file.readLine())
- position.y = tonumber(file.readLine())
- position.z = tonumber(file.readLine())
- direction = tonumber(file.readLine())
- file.close()
- end
- end
- return galymoveAPI
Advertisement
Add Comment
Please, Sign In to add comment