Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This script is mining diamonds scheme. Place miner on height 5, in the edge of the chunk.
- On left side place chest for resources.
- On back side place chest with coal.
- First inventoy slot should be filled with coal.
- ]]--
- -- Fuel operations
- function refuel()
- turtle.select(1)
- local curFuel, maxFuel, count, result
- curFuel = turtle.getFuelLevel()
- maxFuel = turtle.getFuelLimit()
- count = turtle.getItemCount()
- count = (count > 0) and (count - 1) or 0
- count = math.min(count, math.floor((maxFuel - curFuel) / 80)) -- 1 coal = 80 fuel
- turtle.refuel(count)
- return count > 0
- end
- function loadFuel()
- turtle.select(1)
- local result
- result = true
- while (result) do
- result = turtle.suck(64 - turtle.getItemCount())
- result = result and refuel()
- end
- end
- -- Other inventory operations
- function inventoryFilled()
- local count
- count = turtle.getItemCount(16)
- return count > 0
- end
- function inventoryUnload()
- local i
- for i = 2, 16 do
- turtle.select(i)
- if (turtle.compareTo(1)) then
- turnLeft(1)
- turtle.drop()
- turnRight(1)
- else
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- -- Basic rotation operations
- function turnRight(times)
- times = times % 4
- while (times > 0) do
- turtle.turnRight()
- dx, dz = -dz, dx
- times = times - 1
- end
- end
- function turnLeft(times)
- times = times % 4
- while (times > 0) do
- turtle.turnLeft()
- dx, dz = dz, -dx
- times = times - 1
- end
- end
- function rotateToDirection(rx, rz)
- if (rx == -dx and rz == -dz) then
- turnRight(2)
- elseif (rx == -dz and rz == dx) then
- turnRight(1)
- elseif (rx == dz and rz == -dx) then
- turnLeft(1)
- end
- end
- -- Basic move operations
- function digForward(length)
- local result
- while (length > 0) do
- result = not turtle.detect() or (turtle.dig() == true)
- result = turtle.forward() and result
- if (result) then
- length = length - 1
- x = x + dx
- z = z + dz
- distance = distance + dx + dz
- end
- end
- end
- function digUp(length)
- local result
- while (length > 0) do
- result = not turtle.detectUp() or (turtle.digUp() == true)
- result = turtle.up() and result
- if (result) then
- length = length - 1
- y = y + 1
- distance = distance + 1
- end
- end
- end
- function digDown(length)
- local result
- while (length > 0) do
- result = not turtle.detectDown() or (turtle.digDown() == true)
- result = turtle.down() and result
- if (result) then
- length = length - 1
- y = y - 1
- distance = distance - 1
- end
- end
- end
- function strafeRight(length)
- turnRight(1)
- digForward(length)
- turnLeft(1)
- end
- function strafeLeft(length)
- turnLeft(1)
- digForward(length)
- turnRight(1)
- end
- -- Intelligence functions
- -- Home Functions
- function goHomeHigh()
- if ((x ~= 0 or z ~= 0) and y < 16) then
- digUp(16 - y)
- end
- if (z < 0) then
- rotateToDirection(0, 1)
- digForward(-z)
- elseif (z > 0) then
- rotateToDirection(0, -1)
- digForward(z)
- elseif (x < 0) then
- rotateToDirection(1, 0)
- digForward(-x)
- elseif (x > 0) then
- rotateToDirection(-1, 0)
- digForward(x)
- elseif (y > 5) then
- digDown(y - 5)
- end
- end
- function goHomeLow()
- if (x % 16 ~= 0) then
- if (y % 3 == 0) then
- digDown(1)
- else
- rotateToDirection(-1, 0)
- digForward(x)
- end
- elseif (y > 5) then
- digDown(y - 5)
- elseif (z > 0) then
- rotateToDirection(0, -1)
- digForward(z)
- else
- rotateToDirection(-1, 0)
- digForward(x)
- end
- end
- function homeActions()
- rotateToDirection(0, -1)
- inventoryUnload()
- turnLeft(1)
- loadFuel()
- turnLeft(2)
- if (newChunk) then
- tx = cx
- ty = 5
- dz = 0
- end
- if (tx - x + ty - y + tz - z > turtle.getFuelLevel() / 2 - 400) then
- print("Not enough fuel to start working")
- goHome = true
- sleep(10)
- else
- print("Successfully resupplied, digging")
- goHome = false
- end
- end
- function home()
- if (y > 15 or x < 0 or z < 0 or z > 14) then
- goHomeHigh()
- elseif (x ~= 0 or y ~= 5 or z ~= 0) then
- goHomeLow()
- else
- homeActions()
- end
- end
- -- Mine functions
- function moveToTarget()
- local maxHeight
- maxHeight = ty
- if (maxHeight % 3 == 0 and x ~= tx) then
- maxHeight = maxHeight - 1
- end
- if (x < cx) then
- rotateToDirection(1, 0)
- digForward(cx - x)
- elseif (z ~= tz) then
- rotateToDirection(0, 1)
- digForward(tz - z)
- elseif (y ~= maxHeight) then
- digUp(maxHeight - y)
- else
- rotateToDirection(1, 0)
- digForward(tx - x)
- end
- end
- function makeNewTunnel()
- if (z % 2 == 0) then
- if (y > 6) then
- digDown(y - 6)
- else
- rotateToDirection(0, 1)
- digForward(1)
- end
- else
- if (y == 6) then
- digDown(1)
- else
- newTunnel = false
- digForward(1)
- end
- end
- function startNewChunk()
- if (y > 5) then
- digDown(y - 5)
- elseif (z > 0) then
- rotateToDirection(0, -1)
- digForward(14)
- else
- if (x < cx) then
- rotateToDirection(1, 0)
- digForward(1)
- else
- newChunk = false
- end
- end
- end
- function usualMining()
- if (y % 3 == 2) then
- if (x % 16 ~= 15) then
- rotateToDirection(1, 0)
- digForward(1)
- else
- digUp(1)
- end
- elseif (y % 3 == 0) then
- if (x % 16 ~= 0) then
- rotateToDirection(-1, 0)
- digForward(1)
- else
- if (y ~= 15) then
- digUp(1)
- elseif (z ~= 14) then
- print("Starting new tunnel")
- newTunnel = true
- else
- print("Starting new chunk")
- newChunk = true
- cx = cx + 16
- saveImportantData()
- end
- end
- else
- digUp(1)
- end
- end
- function mine()
- local maxHeight
- if (tx ~= x or ty ~= y or tz ~= z) then
- moveToTarget()
- else
- if (newTunnel) then
- makeNewTunnel()
- elseif (newChunk) then
- startNewChunk()
- else
- usualMining()
- end
- tx = x
- ty = y
- tz = z
- if (turtle.getFuelLevel() <= distance + 3 and (not refuel() or turtle.getFuelLevel() <= distance + 3)) then
- print("Low fuel level, returning")
- goHome = true
- elseif (inventoryFilled()) then
- print("Full inventory, returning")
- goHome = true
- end
- end
- end
- -- Initialization functions
- function reset()
- -- local chunk x start pos
- cx = 0
- -- local move direction
- dx = 1
- dz = 0
- -- local target position
- tx = 0
- ty = 5
- tz = 0
- -- local current position
- x = 0 -- curr x pos
- y = 5 -- curr y pos
- z = 0 -- curr z pos
- -- real home position
- hx = 0
- hz = 0
- -- real start move direction
- mx = 1
- mz = 0
- -- other information for algorithms
- distance = 0 -- blocks from internal point (0, 5, 0)
- newTunnel = false -- flag to dig new tunnel in same chunk
- newChunk = false -- flag to start dig new chunk
- goHome = true -- flag to go to the home location
- saveVersion = 1 -- version of save file
- dataPath = "/data/miner.data"
- tempDataPath = "/data/miner_temp.data"
- end
- function saveImportantData()
- local dataFile = fs.open(tempDataPath, "w")
- dataFile.writeLine(tostring(saveVersion))
- dataFile.writeLine(tostring(hx))
- dataFile.writeLine(tostring(hz))
- dataFile.writeLine(tostring(mx))
- dataFile.writeLine(tostring(mz))
- dataFile.writeLine(tostring(cx))
- dataFile.close()
- fs.delete(dataPath)
- fs.move(tempDataPath, dataPath)
- print("Important data saved")
- end
- function adapt()
- local data = {}
- local dataFile
- local str
- if (not fs.exists(dataPath) and fs.exists(tempDataPath)) then
- fs.move(tempDataPath, dataPath)
- end
- if (fs.exists(dataPath)) then
- fs.delete(tempDataPath)
- dataFile = fs.open(dataPath, "r")
- -- Save File Version
- str = dataFile.readLine()
- data["version"] = str and tonumber(str) or nil
- -- World coords of home
- str = dataFile.readLine()
- data["homeX"] = str and tonumber(str) or nil
- str = dataFile.readLine()
- data["homeZ"] = str and tonumber(str) or nil
- -- World direction vector of work
- str = dataFile.readLine()
- data["dirX"] = str and tonumber(str) or nil
- str = dataFile.readLine()
- data["dirZ"] = str and tonumber(str) or nil
- -- Local chunk start X
- str = dataFile.readLine()
- data["chunkX"] = str and tonumber(str) or nil
- dataFile.close()
- end
- if (fs.exists(dataPath) and data["version"] == saveVersion and data["chunkX"]) then
- -- memorize info
- hx = data["homeX"]
- hz = data["homeZ"]
- mx = data["dirX"]
- mz = data["dirZ"]
- cx = data["chunkX"]
- print("Home Coords: ", hx, hz)
- print("Start Look Direction: ", mx, mz)
- print("Local Chunk X Coord: ", cx)
- else
- io.write("Real Home X: ")
- hx = tonumber(read())
- io.write("Real Home Z: ")
- hz = tonumber(read())
- io.write("Real Start Direction X: ")
- mx = tonumber(read())
- io.write("Real Start Direction Z: ")
- mz = tonumber(read())
- io.write("Local Chunk Start X: ")
- cx = tonumber(read())
- end
- -- adapt coords
- x = x - hx
- z = z - hz
- -- rotate local coords
- if (mx == -1) then
- x, z = -x, -z
- dx, dz = -dx, -dz
- elseif (mz == 1) then
- x, z = z, -x
- dx, dz = dz, -dx
- elseif (mz == -1) then
- x, z = -z, x
- dx, dz = -dz, dx
- end
- -- update info
- distance = x + z + y
- tx = cx
- ty = 5
- tz = 0
- end
- function locate()
- if (peripheral.find("modem") and gps.locate()) then
- x, y, z = gps.locate()
- local nx, ny, nz
- local success, data, name
- -- avoiding the destruction of chest
- success, data = turtle.inspect()
- if (success) then
- name = data.name
- name = string.sub(name, string.find(name, ":") + 1)
- if (string.find(name, "chest")) then
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- --getting second location
- turtle.dig()
- turtle.forward()
- nx, ny, nz = gps.locate()
- dx = nx - x
- dz = nz - z
- turtle.turnRight()
- turtle.turnRight()
- turtle.dig()
- turtle.forward()
- turtle.turnRight()
- turtle.turnRight()
- print("Real Coords: ", x, y, z)
- print("Look Direction: ", dx, dz)
- else
- io.write("Real Current X: ")
- x = tonumber(read())
- io.write("Real Current Y: ")
- y = tonumber(read())
- io.write("Real Current Z: ")
- z = tonumber(read())
- io.write("Real Current Direction X: ")
- dx = tonumber(read())
- io.write("Real Current Direction Z: ")
- dz = tonumber(read())
- end
- end
- function init()
- print("Initialization starts")
- reset()
- sleep(0.05)
- locate()
- sleep(0.05)
- adapt()
- sleep(0.05)
- saveImportantData()
- sleep(0.05)
- newChunk = true -- flag to start dig new chunk
- goHome = true -- flag to go to the home location
- print("Initialization ended, going home")
- end
- init()
- while (true) do
- if (goHome) then
- home()
- else
- mine()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement