Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function tobool(value)
- return value == 'true'
- end
- math.sign = math.sign or function(x)
- return x < 0 and -1 or x > 0 and 1 or 0
- end
- table.shallowCopy = function(t)
- local t2 = {}
- for k,v in pairs(t) do
- t2[k] = v
- end
- return t2
- end
- function printMessage(line)
- term.clear()
- print(line)
- end
- function printSleep(line)
- printMessage(line)
- sleep(1)
- term.clear()
- end
- local arg = { ... }
- local width = tonumber(arg[1])
- local height = tonumber(arg[2])
- local length = tonumber(arg[3])
- local down = tobool(arg[4])
- local startPosition = {
- x = 1;
- y = down == false and 1 or -1;
- z = 1;
- }
- local currentPosition = table.shallowCopy(startPosition)
- local currentDirection = 4
- local persisted = false
- local currentSlot = nil
- local fuelWhitelist = {
- 'minecraft:coal',
- 'minecraft:charcoal'
- }
- function readData(name)
- if fs.exists('/data/'..name) then
- local handle = fs.open('/data/'..name, 'r')
- local stuff = handle.readAll()
- handle.close()
- return textutils.unserialize(stuff)
- end
- return
- end
- function writeData(name, data)
- if not fs.exists('/data') then
- fs.makeDir('/data')
- end
- local handle = fs.open('/data/'..name, 'w')
- handle.write(textutils.serialize(data))
- handle.close()
- end
- function loadData()
- local loadedPosition = readData('position')
- local loadedDirection = readData('direction')
- if (loadedPosition ~= nil) or (loadedDirection ~= nil) then
- currentPosition = loadedPosition or currentPosition
- currentDirection = loadedDirection or currentDirection
- persisted = true
- end
- end
- function updatePosition(deltaX, deltaY, deltaZ)
- currentPosition.x = currentPosition.x + deltaX
- currentPosition.y = currentPosition.y + deltaY
- currentPosition.z = currentPosition.z + deltaZ
- writeData('position', currentPosition)
- end
- function updateDirection(deltaD)
- currentDirection = (currentDirection + deltaD - 1) % 4 + 1
- writeData('direction', currentDirection)
- end
- function directionToXZ(direction)
- return {
- x = (direction- 2) * (direction% 2),
- z = (direction- 3) * ((direction+ 1) % 2)
- }
- end
- function moveForward(steps, force)
- steps = steps or 1
- for i = 1, steps do
- handleFuel()
- if force == true then
- while turtle.detect() do
- turtle.dig()
- end
- end
- moved = turtle.forward()
- if moved == true then
- deltaXZ = directionToXZ(currentDirection)
- updatePosition(deltaXZ.x, 0, deltaXZ.z)
- else
- printSleep('turtle is stuck, please remove the block in front')
- i = i - 1
- end
- end
- end
- function moveUp(steps, force)
- steps = steps or 1
- for i = 1, steps do
- handleFuel()
- if force == true then
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- moved = turtle.up()
- if moved == true then
- updatePosition(0, 1, 0)
- else
- printSleep('turtle is stuck, please remove the block above')
- i = i - 1
- end
- end
- end
- function moveDown(steps, force)
- steps = steps or 1
- for i = 1, steps do
- handleFuel()
- if force == true then
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- moved = turtle.down()
- if moved == true then
- updatePosition(0, -1, 0)
- else
- printSleep('turtle is stuck, please remove the block below')
- i = i - 1
- end
- end
- end
- function moveBack(steps, force, maintaintDirection)
- steps = steps or 1
- for i = 1, steps do
- handleFuel()
- moved = turtle.back()
- if moved == true then
- deltaXZ = directionToXZ(currentDirection)
- updatePosition(-deltaXZ.x, 0, -deltaXZ.z)
- else
- if force == true then
- turnAround()
- moveForward(steps - i + 1, true)
- if maintaintDirection == true then
- turnAround()
- end
- return
- else
- printSleep('turtle is stuck, please remove the block behind')
- i = i - 1
- end
- end
- end
- end
- function turnLeft()
- turtle.turnLeft()
- updateDirection(1)
- end
- function turnRight()
- turtle.turnRight()
- updateDirection(-1)
- end
- function turnAround()
- turnRight()
- turnRight()
- end
- function turnTo(direction)
- if direction == nil or direction == currentDirection then
- return
- end
- local deltaDir = currentDirection - direction
- local temp = (currentDirection % 4) - (direction % 4)
- if math.abs(temp) < math.abs(deltaDir) then
- deltaDir = temp
- end
- if deltaDir < 0 then
- for i = 1, -deltaDir do
- turnLeft()
- end
- elseif deltaDir > 0 then
- for i = 1, deltaDir do
- turnRight()
- end
- end
- end
- function turnToXAxis(preferedSign)
- if currentDirection == 4 then
- if preferedSign == -1 then
- turnLeft()
- else
- turnRight()
- end
- elseif currentDirection == 2 then
- if preferedSign == -1 then
- turnRight()
- else
- turnLeft()
- end
- end
- end
- function turnToZAxis(preferedSign)
- if currentDirection == 1 then
- if preferedSign == -1 then
- turnLeft()
- else
- turnRight()
- end
- elseif currentDirection == 3 then
- if preferedSign == -1 then
- turnRight()
- else
- turnLeft()
- end
- end
- end
- function moveOnXAxis(delta, force, maintaintDirection)
- if delta == 0 or delta == nil then
- return
- end
- local startDir = currentDirection
- local distance = math.abs(delta)
- local sign = math.sign(delta)
- turnToXAxis(sign)
- local currentSign = (currentDirection == 3) and 1 or -1
- if sign * currentSign == 1 then
- moveForward(distance, force)
- else
- moveBack(distance, force, false)
- end
- if maintainDirection then
- turnTo(startDir)
- end
- end
- function moveOnYAxis(delta, force)
- if delta == 0 or delta == nil then
- return
- end
- local distance = math.abs(delta)
- local sign = math.sign(delta)
- if sign == 1 then
- moveUp(distance, force)
- else
- moveDown(distance, force)
- end
- end
- function moveOnZAxis(delta, force, maintaintDirection)
- if delta == 0 or delta == nil then
- return
- end
- local startDir = currentDirection
- local distance = math.abs(delta)
- local sign = math.sign(delta)
- turnToZAxis(sign)
- local currentSign = (currentDirection == 4) and 1 or -1
- if sign * currentSign == 1 then
- moveForward(distance, force)
- else
- moveBack(distance, force, false)
- end
- if maintainDirection then
- turnTo(startDir)
- end
- end
- function moveToX(x, force, maintaintDirection)
- if x == nil then
- return
- end
- local delta = x - currentPosition.x
- moveOnXAxis(delta, force, maintainDirection)
- end
- function moveToY(y, force)
- if y == nil then
- return
- end
- local delta = y - currentPosition.y
- moveOnYAxis(delta, force, maintainDirection)
- end
- function moveToZ(z, force, maintaintDirection)
- if z == nil then
- return
- end
- local delta = z - currentPosition.z
- moveOnZAxis(delta, force, maintainDirection)
- end
- function moveTo(x, y, z, force, maintaintDirection)
- local startDir = currentDirection
- if currentPosition.y < 0 then
- moveToY(y, force)
- if math.abs(currentPosition.x) > math.abs(currentPosition.z) then
- moveToX(x, force, false)
- moveToZ(z, force, false)
- else
- moveToZ(z, force, false)
- moveToX(x, force, false)
- end
- else
- if math.abs(currentPosition.x) > math.abs(currentPosition.z) then
- moveToX(x, force, false)
- moveToZ(z, force, false)
- else
- moveToZ(z, force, false)
- moveToX(x, force, false)
- end
- moveToY(y, force)
- end
- if maintainDirection == true then
- turnTo(startDir)
- end
- end
- function digForward()
- turtle.dig()
- moveForward(1, true)
- end
- function digUp()
- turtle.digUp()
- moveUp(1, true)
- end
- function digDown()
- turtle.digDown()
- moveDown(1, true)
- end
- function refuel()
- selectFuel()
- return turtle.refuel(1)
- end
- function handleFuel()
- local fuel = turtle.getFuelLevel()
- if fuel == 0 then
- while not refuel() do
- printSleep('turtle is out of fuel, please add some fuel')
- end
- end
- end
- function getItemName(slot)
- if slot == nil then
- return nil
- end
- local detail = turtle.getItemDetail(slot)
- return detail and detail.name or nil
- end
- function select(slot)
- if slot == nil then
- return
- end
- turtle.select(slot)
- currentSlot = slot
- end
- function selectFuel()
- local holdingFuel = isFuel(getItemName(currentSlot))
- if holdingFuel == false then
- for i = 1, 16 do
- if i ~= currentSlot then
- local foundFuel = isFuel(getItemName(i))
- if foundFuel == true then
- select(i)
- return true
- end
- end
- end
- return false
- end
- return true
- end
- function isFuel(name)
- for i = 1, #fuelWhitelist do
- if (name == fuelWhitelist[i]) then
- return true
- end
- end
- return false
- end
- function main()
- for y = 1, height do
- for x = 1, width do
- local curX = y % 2 == 1 and x or width+1 - x
- for z = 1, length do
- local curZ = (curX + (y % 2) + 1) % 2 == 1 and z or length+1 - z
- moveTo(curX, down == false and y or -y, curZ, true, false)
- end
- end
- end
- moveTo(startPosition.x, startPosition.y, startPosition.z)
- end
- main()
Add Comment
Please, Sign In to add comment