Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------
- --Position Management
- --------------------------------------------------------
- local depth = 0
- local unloaded = 0
- local collected = 0
- local xPos,zPos = 0,0
- local xDir,zDir = 0,1
- movements = {}
- movements["forward"] = {}
- movements["forward"]["detect"] = turtle.detect
- movements["forward"]["dig"] = turtle.dig
- movements["forward"]["move"] = turtle.forward
- movements["forward"]["attack"] = turtle.attack
- movements["up"] = {}
- movements["up"]["detect"] = turtle.detectUp
- movements["up"]["dig"] = turtle.digUp
- movements["up"]["move"] = turtle.up
- movements["up"]["attack"] = turtle.attackUp
- movements["down"] = {}
- movements["down"]["detect"] = turtle.detectDown
- movements["down"]["dig"] = turtle.digDown
- movements["down"]["move"] = turtle.down
- movements["down"]["attack"] = turtle.attackDown
- local function move(direction)
- --print("Attempting to move with " .. direction)
- while not movements[direction].move() do
- if movements[direction].detect() then
- if movements[direction].dig() == false then
- error("Failed to dig block")
- end
- elseif movements[direction].attack() then
- -- Do nothing, next loop after attacking
- else
- sleep( 0.5 )
- end
- end
- if direction == "forward" then
- xPos = xPos + xDir
- zPos = zPos + zDir
- elseif direction == "up" then
- depth = depth - 1
- elseif direction == "down" then
- depth = depth + 1
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- function alignXDir( xGoalDir )
- if xGoalDir == xDir then
- return
- elseif xGoalDir == zDir then
- turnRight()
- elseif xGoalDir == -zDir then
- turnLeft()
- else
- turnRight()
- turnRight()
- end
- if xGoalDir ~= xDir then
- error("Failed to fix direction")
- end
- end
- function alignZDir( zGoalDir )
- if zGoalDir == zDir then
- return
- elseif zGoalDir == xDir then
- turnLeft()
- elseif zGoalDir == -xDir then
- turnRight()
- else
- turnRight()
- turnRight()
- end
- if zGoalDir ~= zDir then
- error("Failed to fix direction")
- end
- end
- function goTo( x, y, z, atEachSqurare, xd, zd)
- --print( "GoTo: " .. x .. "," .. y .. "," .. z .. " From: " .. xPos .. "," .. depth .. "," .. zPos)
- if atEachSqurare == nil then
- atEachSqurare = function() end
- end
- while depth ~= y do
- -- print("Depth = " .. depth .. " | y = " .. y)
- if depth > y then
- move("up")
- elseif depth < y then
- move("down")
- end
- atEachSqurare()
- end
- local xGoalDir = (x - xPos) / math.abs(x - xPos)
- while xPos ~= x do
- alignXDir(xGoalDir)
- move("forward")
- atEachSqurare()
- end
- local zGoalDir = (z - zPos) / math.abs(z - zPos)
- while zPos ~= z do
- alignZDir(zGoalDir)
- move("forward")
- atEachSqurare()
- end
- --Fix alignment
- if xd then
- alignXDir(xd)
- end
- if zd then
- alignZDir(zd)
- end
- end
- --------------------------------------------------------
- --Inventory Management
- --------------------------------------------------------
- local fuel = {}
- fuel['chest'] = 1
- fuel['material'] = 2
- local wall = {}
- wall['chest'] = 3
- wall['material'] = 4
- local torch = {}
- torch['chest'] = 5
- torch['material'] = 6
- local foundation = {}
- foundation['chest'] = 7
- foundation['material'] = 8
- local loot = {}
- loot['chest'] = 9
- loot['material'] = 10
- loot['lastItem'] = true
- local firstSlot = loot['material']
- local chestZDir = -1
- local chestXDir = 0
- local preChestZDir;
- local preChestXDir;
- local function placeChest(item)
- preChestXdir = xDir
- preChestZdir = zDir
- alignZDir(chestZDir)
- alignXDir(chestXDir)
- if turtle.detect() then
- error("Block where chest should go")
- end
- turtle.select(item.chest)
- if turtle.place() == false then
- error("Failed to place chest from slot " .. item.chest)
- end
- end
- local function getChest(item)
- turtle.select(item.chest)
- if turtle.dig() == false then
- error("Failed to retreive chest to slot " .. item.chest)
- end
- alignZDir(preChestZdir)
- alignXDir(preChestXdir)
- end
- local function unload(item)
- placeChest(item)
- if item.lastItem then
- for n=item.material,16 do
- turtle.select(n)
- turtle.drop()
- end
- else
- turtle.select(item.material)
- turtle.drop()
- end
- getChest(item)
- end
- local function reload(item)
- if turtle.getItemSpace(item.material) == 0 then
- return
- end
- placeChest(item)
- turtle.select(item.material)
- while turtle.getItemSpace() > 0 do
- if turtle.suck(turtle.getItemSpace()) == false then
- print("Couldn't get an item for slot " .. item.material)
- sleep(30)
- end
- end
- getChest(item)
- end
- local function refuel(item, fuelLimit)
- if fuelLimit == nil then
- fuelLimit = turtle.getFuelLimit()
- end
- while turtle.getFuelLevel() < fuelLimit do
- turtle.select(item.material)
- if turtle.getItemCount() > 1 then
- turtle.refuel(turtle.getItemCount() - 1)
- else
- reload(item)
- end
- end
- end
- local Item = {}
- Item["detect"] = {}
- Item["detect"]["forward"] = turtle.detect
- Item["detect"]["up"] = turtle.detectUp
- Item["detect"]["down"] = turtle.detectDown
- Item["place"] = {}
- Item["place"]["forward"] = turtle.place
- Item["place"]["up"] = turtle.placeUp
- Item["place"]["down"] = turtle.placeDown
- Item["compare"] = {}
- Item["compare"]["forward"] = turtle.compare
- Item["compare"]["up"] = turtle.compareUp
- Item["compare"]["down"] = turtle.compareDown
- local function place(item, direction)
- if turtle.getItemCount(item.material) <= 1 then
- reload(item)
- end
- turtle.select(item.material)
- while Item.detect[direction]() == true do
- if Item.compare[direction]() == false then
- if movements[direction]["dig"]() == false then
- error("Failed to dig a block different than item.material")
- end
- else
- break
- end
- end
- if Item.detect[direction]() == false then
- while Item.place[direction]() == false do
- print("Failed to place an item from slot " .. item.material)
- movements[direction]["attack"]()
- end
- end
- end
- local function isFull()
- for n=firstSlot,16 do
- if turtle.getItemCount(n) == 0 then
- --Empty slots
- return false;
- end
- end
- print( "No empty slots left." )
- return true;
- end
- -----------------------------------------------------------------------------
- --Main Program Functions
- -----------------------------------------------------------------------------
- function stock()
- if turtle.getFuelLimit() ~= 0 and turtle.getFuelLevel() < 10000 then
- refuel(fuel, 20000)
- end
- if isFull() then
- unload(loot)
- end
- end
- function edge()
- if xPos == 0 then
- alignXDir(-1)
- place(wall, "forward")
- elseif xPos == 2 then
- alignXDir(1)
- place(wall, "forward")
- end
- if depth == 0 then
- place(foundation, "down")
- elseif depth == -2 then
- place(wall, "up")
- end
- end
- function placeTorch()
- alignZDir(-1)
- place(torch, "forward")
- end
- function mineFace(z)
- local afterEach = function() stock() edge() end
- goTo(0, 0, z, afterEach)
- if z % 5 == 0 then
- placeTorch()
- end
- goTo(2, 0, z, afterEach)
- goTo(2, -2, z, afterEach)
- goTo(1, -2, z, afterEach)
- turtle.digDown()
- goTo(0, -2, z, afterEach)
- goTo(0, 0, z, afterEach)
- end
- -----------------------------------------------------------------------------
- --Main Program
- -----------------------------------------------------------------------------
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "My Version Usage: tunnel length" )
- return
- end
- local length = tonumber( tArgs[1] ) -1
- if length < 0 then
- print( "All arguments must be positive" )
- return
- end
- print( "Tunneling" )
- reload(fuel)
- reload(torch)
- reload(wall)
- reload(foundation)
- for z=0, length do
- print("Mining face " .. z)
- mineFace(z)
- end
- print( "Emptying" )
- unload(fuel)
- unload(torch)
- unload(wall)
- unload(foundation)
- unload(loot)
- alignZDir(1)
- print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement