Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local h -- Holds file to be manipulated
- local input
- local success, data -- Turtle inspect values
- local stuck = false
- local running = true
- local timeout = 2
- local constantIdentifiers
- = {"Origin X", "Origin Y", "Origin Z", "Origin Side (0 to 3)", "Origin Next to Entrance (0 or 1)", "Signal Block (mod:blockname)"}
- local originX
- local originY
- local originZ
- local originSide
- local originNextToEntrance
- local signalBlock
- local checkpointX
- local checkpointY
- local checkpointZ
- local checkpointFacing
- local x, y, z
- local facing -- 0 to 3 (north to west)
- local blocksTravelled -- From origin
- local allInventorySlotsUsed
- local function setConstants()
- h = fs.open("constants", "w")
- print("Enter constants:")
- for i = 1, #constantIdentifiers do
- write(" ")
- write(constantIdentifiers[i])
- write(" = ")
- input = read()
- h.writeLine(input)
- end
- h.close()
- end
- local function setState(values)
- h = fs.open("state", "w")
- for i = 1, 4 do
- h.writeLine(values[i])
- end
- h.close()
- end
- local function readConstants()
- h = fs.open("constants", "r")
- originX = tonumber(h.readLine())
- originY = tonumber(h.readLine())
- originZ = tonumber(h.readLine())
- originSide = tonumber(h.readLine())
- originNextToEntrance = tonumber(h.readLine())
- signalBlock = h.readLine()
- h.close()
- end
- local function readState()
- h = fs.open("state", "r")
- checkpointX = tonumber(h.readLine())
- checkpointY = tonumber(h.readLine())
- checkpointZ = tonumber(h.readLine())
- checkpointFacing = tonumber(h.readLine())
- h.close()
- end
- local function refuel()
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel()
- end
- end
- local function getFacing()
- local xOld, yOld, zOld
- repeat
- xOld, yOld, zOld = gps.locate(timeout)
- until xOld and yOld and zOld
- repeat
- turtle.dig()
- stuck = not turtle.forward()
- until not stuck
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- turtle.back()
- if z < zOld then
- return 0
- elseif z > zOld then
- return 2
- elseif x < xOld then
- return 3
- elseif x > xOld then
- return 1
- end
- end
- local function trackTurnLeft()
- turtle.turnLeft()
- facing = (facing - 1) % 4
- end
- local function trackTurnRight()
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- local function faceDirection(target)
- if facing == (target + 1) % 4 then
- trackTurnLeft()
- else
- while facing ~= target do
- trackTurnRight()
- end
- end
- end
- local function goTo(targetX, targetY, targetZ, targetDirection)
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- if z > targetZ then
- faceDirection(0)
- repeat
- turtle.dig()
- turtle.forward()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until z == targetZ
- elseif z < targetZ then
- faceDirection(2)
- repeat
- turtle.dig()
- turtle.forward()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until z == targetZ
- end
- if x > targetX then
- faceDirection(3)
- repeat
- turtle.dig()
- turtle.forward()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until x == targetX
- elseif x < targetX then
- faceDirection(1)
- repeat
- turtle.dig()
- turtle.forward()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until x == targetX
- end
- if y > targetY then
- repeat
- turtle.digDown()
- turtle.down()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until y == targetY
- elseif y < targetY then
- repeat
- turtle.digUp()
- turtle.up()
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- until y == targetY
- end
- faceDirection(targetDirection)
- end
- local function mineOres()
- for i = 1, 3 do
- success, data = turtle.inspect()
- if success then
- if string.find(string.lower(data.name), "ore") then
- turtle.dig()
- turtle.forward()
- mineOres()
- turtle.back()
- end
- end
- if i == 2 then
- trackTurnRight()
- end
- trackTurnRight()
- end
- success, data = turtle.inspectUp()
- if success then
- if string.find(string.lower(data.name), "ore") then
- turtle.digUp()
- turtle.up()
- mineOres()
- turtle.down()
- end
- end
- success, data = turtle.inspectDown()
- if success then
- if string.find(string.lower(data.name), "ore") then
- turtle.digDown()
- turtle.down()
- mineOres()
- turtle.up()
- end
- end
- end
- if not fs.exists("constants") then
- setConstants()
- end
- if not fs.exists("state") then
- setState({"nil", "nil", "nil", "nil"})
- end
- readConstants()
- readState()
- refuel()
- facing = getFacing()
- if checkpointX ~= nil and checkpointY ~= nil and checkpointZ ~= nil and checkpointFacing ~= nil then
- goTo(checkpointX, checkpointY, checkpointZ, checkpointFacing)
- end
- while running do
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- setState({tostring(x), tostring(y), tostring(z), tostring(facing)})
- blocksTravelled = math.abs(x - originX) + math.abs(y - originY) + math.abs(z - originX)
- allInventorySlotsUsed = true
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount() == 0 then
- allInventorySlotsUsed = false
- end
- end
- if blocksTravelled >= turtle.getFuelLevel() + 10 or allInventorySlotsUsed then
- if originNextToEntrance == 0 then
- if facing == (originSide + 1) % 4 then
- trackTurnRight()
- trackTurnRight()
- end
- elseif originNextToEntrance == 1 then
- if facing == (originSide - 1) % 4 then
- trackTurnRight()
- trackTurnRight()
- end
- end
- end
- success, data = turtle.inspectDown()
- if data.name == signalBlock then
- if facing == originSide then
- if originNextToEntrance == 0 then
- trackTurnRight()
- elseif originNextToEntrance == 1 then
- trackTurnLeft()
- end
- elseif facing == (originSide + 2) % 4 then
- if originNextToEntrance == 0 then
- trackTurnLeft()
- elseif originNextToEntrance == 1 then
- trackTurnRight()
- end
- end
- turtle.select(1)
- turtle.digDown()
- else
- x, y, z = gps.locate(timeout)
- success, data = turtle.inspectUp()
- if originSide == 0 then
- if data.name == signalBlock then
- if z > originZ then
- if originNextToEntrance == 0 then
- trackTurnRight()
- elseif originNextToEntrance == 1 then
- trackTurnLeft()
- end
- elseif z < originZ then
- if originNextToEntrance == 0 then
- trackTurnLeft()
- elseif originNextToEntrance == 1 then
- trackTurnRight()
- end
- end
- turtle.select(1)
- turtle.placeDown()
- end
- elseif originSide == 2 then
- if data.name == signalBlock then
- if z > originZ then
- if originNextToEntrance == 0 then
- trackTurnLeft()
- elseif originNextToEntrance == 1 then
- trackTurnRight()
- end
- elseif z < originZ then
- if originNextToEntrance == 0 then
- trackTurnRight()
- elseif originNextToEntrance == 1 then
- trackTurnLeft()
- end
- end
- turtle.select(1)
- turtle.placeDown()
- end
- elseif originSide == 1 then
- if data.name == signalBlock then
- if x > originX then
- if originNextToEntrance == 0 then
- trackTurnLeft()
- elseif originNextToEntrance == 1 then
- trackTurnRight()
- end
- elseif x < originX then
- if originNextToEntrance == 0 then
- trackTurnRight()
- elseif originNextToEntrance == 1 then
- trackTurnLeft()
- end
- end
- turtle.select(1)
- turtle.placeDown()
- end
- elseif originSide == 3 then
- if data.name == signalBlock then
- if x > originX then
- if originNextToEntrance == 0 then
- trackTurnRight()
- elseif originNextToEntrance == 1 then
- trackTurnLeft()
- end
- elseif x < originX then
- if originNextToEntrance == 0 then
- trackTurnLeft()
- elseif originNextToEntrance == 1 then
- trackTurnRight()
- end
- end
- turtle.select(1)
- turtle.placeDown()
- end
- end
- end
- success, data = turtle.inspect()
- if data.name == signalBlock then
- trackTurnRight()
- trackTurnRight()
- end
- repeat
- x, y, z = gps.locate(timeout)
- until x and y and z
- setState({tostring(x), tostring(y), tostring(z), tostring(facing)})
- mineOres()
- success, data = turtle.inspectDown()
- if data.name == "minecraft:chest" then
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- turtle.dig()
- turtle.forward()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement