Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- K_VERSION = 0.299
- -- TODO:
- -- Handle startup script
- -- Unload chest when full
- -- Waypoints
- -- Work with negative
- -- DONE:
- -- Verify fuel and refuel
- local args = {...}
- local home = {0, 0, 0, "south"}
- local isContinue = args[4]
- local size = {x = tonumber(args[1]), y = tonumber(args[2]), z = tonumber(args[3])}
- local x, y, z, facing
- function init()
- if not lama then
- os.loadAPI("lama")
- lama.overwrite()
- end
- if (isContinue) then
- x, y, z, facing = lama.getPosition()
- else
- x = 0
- y = 0
- z = 0
- facing = "south"
- lama.setPosition(x, y, z, facing)
- end
- end
- function checkFuel()
- let res = true
- if (turtle.getFuelLevel() < 10) then
- print("Fuel level is low, put fuel in #16 slot")
- local selectedSlot = turtle.getSelectedSlot()
- turtle.select(16)
- res = lama.refuel(10)
- turtle.select(selectedSlot)
- end
- return res
- end
- function addWaypoint(name)
- local file = fs.open("waypoints", "w")
- file.write(textutils.serialize(table))
- file.close()
- return true
- end
- function getWaypoint(name)
- if (fs.exists("waypoints")) then
- local file = fs.open("resumeParams", "r")
- local data = file.readAll()
- file.close()
- local params = textutils.unserialize(data)
- fs.delete("waypoints")
- return params
- end
- end
- function unloadInChest()
- if (not lama.detectUp()) then print("Can't find a chest below!") return end
- addWaypoint("unload")
- goTo(home)
- for slot = 1, 15 do
- turtle.select(slot)
- if turtle.detectUp() then turtle.dropUp() end
- end
- turtle.select(1)
- getWaypoint("unload")
- end
- function getPos()
- local _x, _y, _z, _facing = lama.getPosition()
- return {x = tonumber(_x), y = tonumber(_y), z = tonumber(_z), f = _facing}
- end
- function setForward()
- local _facing = getPos().f
- if (_facing == "east") then lama.turnRight()
- elseif (_facing == "north") then lama.turnLeft() lama.turnLeft()
- elseif (_facing == "west") then lama.turnLeft() end
- end
- function setBack()
- local _facing = getPos().f
- if (_facing == "south") then lama.turnLeft() lama.turnLeft()
- elseif (_facing == "east") then lama.turnLeft()
- elseif (_facing == "west") then lama.turnRight() end
- end
- function setLeft()
- local _facing = getPos().f;
- if (_facing == "south") then lama.turnLeft()
- elseif (_facing == "north") then lama.turnRight()
- elseif (_facing== "west") then lama.turnLeft() lama.turnLeft() end
- end
- function setRight()
- local _facing = getPos().f;
- if (_facing == "south") then
- lama.turnRight()
- elseif (_facing == "north") then
- lama.turnLeft()
- elseif (_facing == "east") then
- lama.turnRight()
- lama.turnRight()
- end
- end
- function dig(detectFn, digFn)
- detectFn = detectFn or turtle.detect
- digFn = digFn or turtle.dig
- while detectFn() do
- digFn()
- os.sleep(0.5)
- end
- end
- function digUp() dig(turtle.detectUp, turtle.digUp) end
- function move(moveFn)
- if (turtle.getItemCount(15) > 0) then unloadInChest() end
- if (checkFuel()) then moveFn() end
- end
- function fForward(n)
- n = n or 1
- for i = 1, n do
- dig()
- move(lama.forward)
- end
- end
- function fBack() move(lama.back) end
- function fUp() digUp() move(lama.up) end
- function fDown() turtle.digDown() move(lama.down) end
- function fRight() setRight() fForward() setForward() end
- function fLeft(n) setLeft() fForward(n) setForward() end
- function utils()
- local isRowStart = getPos().x == 0
- local isRowEnd = getPos().x == size.x - 1
- local isColStart = getPos().y == 0
- local isColEnd = getPos().y == size.y - 1
- local isLenStart = getPos().z == 0
- local isLenEnd = getPos().z == size.z
- function isUp() return turtle.detectUp() end
- function isDown() return turtle.detectDown() end
- function isForward() return turtle.detect() end
- function isLeft()
- setLeft()
- local res = turtle.detect()
- setForward()
- return res
- end
- function isRight()
- setRight()
- local res = turtle.detect()
- setForward()
- return res
- end
- function isNextLeft()
- if (isLeft()) then return true end
- local res = false
- if (getPos().x == size.x - 2) then return res end
- fLeft()
- res = isLeft()
- fRight()
- return res
- end
- function isNextRight()
- if (isRight()) then return true end
- local res = false
- if (getPos().x == 1) then return res end
- fRight()
- res = isRight()
- fLeft()
- return res
- end
- function isBlockAround()
- local res = false
- if (isRowStart) then res = res or isNextLeft() end
- if (isRowEnd) then res = res or isNextRight() end
- if (isColStart) then res = res or isUp() end
- if (isColEnd) then res = res or isDown() end
- if (not isRowStart and not isRowEnd) then res = res or isLeft() or isRight() end
- if (not isColStart and not isColEnd) then res = res or isUp() or isDown() end
- return res
- end
- return {
- isLeft = isLeft, isRight = isRight, isUp = isUp, isDown = isDown, isForward = isForward,
- isNextLeft = isNextLeft, isNextRight = isNextRight,
- isBlockAround = isBlockAround,
- isRowStart = isRowStart, isRowEnd = isRowEnd,
- isColStart = isColStart, isColEnd = isColEnd,
- isLenStart = isLenStart, isLenEnd = isLenEnd
- }
- end
- function goTo(waypoint)
- local x, y, z, facing = unpack(waypoint)
- while (getPos().x ~= x) do
- if (getPos().x < x) then fRight()
- else fLeft() end
- end
- while (getPos().y ~= y) do
- if (getPos().y < y) then fUp()
- else fDown() end
- end
- while (getPos().z ~= z) do
- if (getPos().z < z) then fForward()
- else fBack() end
- end
- end
- function mine()
- function moveToNextRow()
- repeat
- fForward()
- until(utils().isBlockAround())
- end
- function digRow()
- function _dig() if (not utils().isLenEnd) then dig() end end
- local cond, moveFn
- if (utils().isRowStart) then
- cond = size.x - 1
- moveFn = fLeft
- elseif (utils().isRowEnd) then
- cond = 0
- moveFn = fRight
- elseif (utils().isNextLeft()) then
- cond = size.x - 1
- moveFn = fLeft
- elseif (utils().isNextRight()) then
- cond = 0
- moveFn = fRight
- end
- while (getPos().x ~= cond) do
- _dig()
- moveFn()
- end
- _dig()
- end
- function digSquare()
- local cond, moveFn
- if (utils().isColStart) then
- cond = size.y - 1
- moveFn = fUp
- elseif (utils().isColEnd) then
- cond = 0
- moveFn = fDown
- elseif (utils().isUp()) then
- cond = size.y - 1
- moveFn = fUp
- elseif (utils().isDown()) then
- cond = 0
- moveFn = fDown
- end
- while (getPos().y ~= cond or utils().isBlockAround()) do
- digRow()
- if (getPos().y ~= cond) then moveFn() end
- end
- end
- function digArea()
- while (getPos().z ~= size.z or utils().isBlockAround()) do
- digSquare()
- if (getPos().z ~= size.z) then moveToNextRow() end
- end
- end
- if (isContinue) then setForward() end
- if (utils().isLenStart) then fForward() end
- digArea()
- end
- init()
- mine()
- goTo(home)
- return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement