Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Directions = {
- Forward = 0,
- Right = 1,
- Backwards = 2,
- Left = 3
- }
- local file_position = "saves/position"
- Direction = Directions.Forward -- 0 the default moveForward direction (direction placed), 1 right, 2 back, 3 left
- Local_x = 0
- Local_y = 0
- Local_z = 0
- Auto_refuel = true
- LastPos = {
- Local_x = 0,
- Local_y = 0,
- Local_z = 0,
- Direction = Directions.Forward
- }
- function SetLastPosition()
- if LastPos.Local_x == 0 and LastPos.Local_y == 0 and LastPos.Local_z == 0 then
- LastPos.Local_x = Local_x
- LastPos.Local_y = Local_y
- LastPos.Local_z = Local_z
- LastPos.Direction = Direction
- end
- end
- function MoveToLastPos(order)
- if LastPos.Local_x ~= 0 or LastPos.Local_y ~= 0 or LastPos.Local_z ~= 0 then
- moveTo(LastPos.Local_x, LastPos.Local_y, LastPos.Local_z, order)
- turnToDirection(LastPos.Direction)
- LastPos = {
- Local_x = 0,
- Local_y = 0,
- Local_z = 0,
- Direction = Directions.Forward
- }
- end
- end
- function moveForward()
- while not turtle.forward() do
- print("Obstructed. Couldn't move forward")
- turtle.attack()
- turtle.dig()
- CheckFuel()
- end
- if Direction == 0 then
- Local_x = Local_x + 1
- elseif Direction == 1 then
- Local_y = Local_y + 1
- elseif Direction == 2 then
- Local_x = Local_x - 1
- elseif Direction == 3 then
- Local_y = Local_y - 1
- end
- _persist_position()
- end
- function turnRight()
- turtle.turnRight()
- Direction = (Direction + 1) % 4
- _persist_position()
- end
- function turnLeft()
- turtle.turnLeft()
- Direction = (Direction + 3) % 4
- _persist_position()
- end
- function moveUp()
- while not turtle.up() do
- print("Obstructed. Couldn't move up")
- turtle.attackUp()
- turtle.digUp()
- CheckFuel()
- end
- Local_z = Local_z + 1
- _persist_position()
- end
- function moveDown()
- while not turtle.down() do
- print("Obstructed. Couldn't move down")
- turtle.attackDown()
- turtle.digDown()
- CheckFuel()
- end
- Local_z = Local_z - 1
- _persist_position()
- end
- function moveTo(x, y, z, order)
- for i = 1, string.len(order) do
- char = string.sub(order, i, i)
- if char == "x" then
- moveToX(x)
- elseif char == "y" then
- moveToY(y)
- elseif char == "z" then
- moveToZ(z)
- end
- end
- end
- function moveToX(x)
- while Local_x ~= x do
- if Local_x > x then
- turnToDirection(Directions.Backwards)
- moveForward()
- else
- turnToDirection(Directions.Forward)
- moveForward()
- end
- end
- end
- function moveToY(y)
- while Local_y ~= y do
- if Local_y > y then
- turnToDirection(Directions.Left)
- moveForward()
- else
- turnToDirection(Directions.Right)
- moveForward()
- end
- end
- end
- function moveToZ(z)
- while Local_z ~= z do
- if Local_z > z then
- moveDown()
- else
- moveUp()
- end
- end
- end
- function turnToDirection(direction)
- if Direction == direction then
- return
- elseif (Direction + 1) % 4 == direction then
- turnRight()
- elseif (Direction + 3) % 4 == direction then
- turnLeft()
- else
- turnRight()
- turnRight()
- end
- end
- function CheckFuel()
- if turtle.getFuelLevel() == 0 and Auto_refuel then
- print("Out of fuel. Feed me!")
- while turtle.getFuelLevel() == 0 do
- for i = 5, 15 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.refuel()
- end
- end
- sleep(5)
- end
- end
- end
- function _persist_position()
- position = {
- Local_x = Local_x,
- Local_y = Local_y,
- Local_z = Local_z,
- Direction = Direction,
- LastPos = LastPos,
- Auto_refuel = Auto_refuel
- }
- save_data(file_position, position)
- end
- function read_position()
- position = read_data(file_position)
- if position ~= nil then
- if position.Local_x ~= nil then
- Local_x = position.Local_x
- end
- if position.Local_y ~= nil then
- Local_y = position.Local_y
- end
- if position.Local_z ~= nil then
- Local_z = position.Local_z
- end
- if position.Direction ~= nil then
- Direction = position.Direction
- end
- if position.LastPos ~= nil then
- LastPos = position.LastPos
- end
- if position.Auto_refuel ~= nil then
- Auto_refuel = position.Auto_refuel
- end
- end
- end
- function save_data(filename, obj)
- file = io.open(filename, "w")
- file:write(textutils.serialise(obj, { compact = true }))
- file:close()
- end
- function read_data(filename)
- file = io.open(filename, "r")
- obj = {}
- if file ~= nil then
- obj = textutils.unserialise(file:read())
- file:close()
- end
- return obj
- end
Advertisement
Comments
-
- Minecraft CC Tweaked turtle movement & location management script
Add Comment
Please, Sign In to add comment
Advertisement