Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tunnel script V2.2 --
- -- By Ninetainedo -- NoFake Dev Team--
- -- Dig a tunnel and fills walls --
- args = {...}
- local width, height, length = 3, 3, 10
- local config = false
- if (#args == 1 and args[1] == "-c") then
- config = true
- elseif #args ~= 1 then
- shell.run("clear")
- print("Usage : tunnel option\n")
- print("Option can be :")
- print(" -c to run in config mode.")
- print(" -d to run in default mode.\n")
- print("In default mode, it makes a 3x3x10 tunnel and fills walls, floor and ceiling.\nIt also empties itself and comes back when it needs blocks.")
- return
- end
- local name = "Tunnel"
- local side, x, y, z = 0, 0, 0, 0
- local moved = 0
- local a, b, c
- local blockSlotsToKeep = 8
- local slots = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
- local goBackForCobble, goBackForEmpty, digOnly, sendOnRednet = true, true, false, true
- local fillCeiling, fillFloor, fillLeftWall, fillRightWall, fillBackWall = true, true, true, true, true
- local pos = "left"
- local percent, oldPercent = 0, -1
- local wTerm, hTerm = term.getSize()
- local screen = {}
- local messages = {}
- local percent = 50
- local msgLen = math.ceil(hTerm / 2) - 2
- local currentMsgIndex = 1
- local infos = { blocksDug = 0, blocksPlaced = 0}
- local p = 0
- function main()
- shell.run("clear")
- if (config == true) then
- configMode()
- end
- if (sendOnRednet) then
- rednet.open("right")
- end
- screenQuery("init")
- tell("Launching !")
- checkBlock()
- a = 0
- -- Boucle sur la longueur --
- while (a < length) do
- b = 0
- if (side == 3) then
- turnRight()
- elseif (side == 1) then
- turnLeft()
- end
- if (turtle.detect()) then dig() end
- forward()
- -- Boucle de balayage vertical --
- while (b < height) do
- c = 0
- if (side == 0 and pos == "left") then
- turnLeft()
- elseif (side == 0 and pos == "right") then
- turnRight()
- end
- putBlock()
- if (pos == "right") then
- turnLeft()
- elseif (pos == "left") then
- turnRight()
- end
- if (side == 0 and pos == "right") then
- turnLeft()
- elseif (side == 0 and pos == "left") then
- turnRight()
- end
- -- Boucle de balayage horizontal --
- while (c < width) do
- if (c == width - 1) then
- putBlock()
- end
- if (z == 0) then
- putBlock("down")
- end
- if (z == height - 1) then
- putBlock("up")
- end
- if (not digOnly and fillBackWall and a == length - 1) then
- if (pos == "left") then
- turnLeft()
- else
- turnRight()
- end
- putBlock()
- if (pos == "right") then
- turnLeft()
- else
- turnRight()
- end
- end
- if (c < width - 1) then
- if (turtle.detect()) then dig() end
- end
- if (z < height - 1) then
- if (turtle.detectUp()) then dig("up") end
- end
- if (c < width - 1) then
- forward()
- end
- c = c + 1
- checkBlock()
- checkInventory()
- updatePercent()
- end -- While y
- if (z < height - 1) then
- up()
- end
- if (pos == "left") then
- pos = "right"
- else
- pos = "left"
- end
- b = b + 1
- end -- While z
- while (z ~= 0) do down() end
- a = a + 1
- end -- While x
- goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx")
- tell("Done !!")
- if (sendOnRednet) then
- rednet.close("right")
- end
- end
- function updatePercent()
- percent = math.floor(reMap(p, 0, length * width * height, 0, 100))
- screenQuery("update", "progress", percent)
- screenQuery("draw")
- end
- function reMap(val, min1, max1, min2, max2)
- local div = max1 - min1
- if (div == 0) then div = 1 end
- return ((val - min1) * (max2 - min2) / div + min2)
- end
- function screenQuery(query, ...)
- local tArgs = {...}
- local writeAtPos = function(message, x, y)
- term.setCursorPos(x, y)
- write(message)
- end
- local getCenteredX = function(message)
- return math.ceil((wTerm / 2) - (#message / 2))
- end
- local writeInTable = function(message, x, y)
- if (x + #message > wTerm) then
- return false
- end
- for i = 1, #message do
- screen[y][x + i - 1] = message:sub(i, i)
- end
- return true
- end
- local printProgressBar = function(percent)
- local y = math.ceil(hTerm / 2 * 3 / 2)
- local barPercent = reMap(percent, 0, 100, 4, wTerm - 2)
- local a = 4
- screen[y][3] = '['
- screen[y][wTerm - 2] = ']'
- while (a < barPercent) do
- screen[y][a] = '='
- a = a + 1
- end
- writeInTable(tostring(percent).."%", getCenteredX(tostring(percent).."%") + 1, y + 1)
- end
- if (query == "init") then
- local mid = math.ceil(hTerm / 2)
- for j = 1, hTerm do
- screen[j] = {}
- for i = 1, wTerm do
- if ((i == 1 or i == wTerm) and (j == 1 or j == hTerm)) then
- screen[j][i] = '+'
- elseif (i == 1 or i == wTerm) then
- screen[j][i] = '|'
- elseif (j == 1 or j == hTerm) then
- screen[j][i] = '-'
- elseif (j == mid) then
- screen[j][i] = '-'
- else
- screen[j][i] = ' '
- end
- end
- end
- printProgressBar(0)
- for j = 1, msgLen do
- messages[j] = ""
- end
- elseif (query == "draw") then
- for j = 1, hTerm do
- for i = 1, wTerm do
- writeAtPos(screen[j][i], i, j)
- end
- end
- elseif (query == "update" and tArgs[1] == "progress") then
- printProgressBar(tArgs[2])
- elseif (query == "update" and tArgs[1] == "message") then
- messages[currentMsgIndex] = tArgs[2]
- for j = 1, msgLen do
- writeInTable(messages[j], 2, j + 1)
- end
- if (currentMsgIndex + 1 > msgLen)then
- table.remove(messages, 1)
- currentMsgIndex = msgLen
- else
- currentMsgIndex = currentMsgIndex + 1
- end
- end
- end
- function checkInventory()
- local count = 1
- if (goBackForEmpty and isInventoryFull()) then
- tell("Need empty")
- goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx")
- turtle.turnLeft()
- turtle.turnLeft()
- for i = (digOnly and 1 or 2),16 do
- turtle.select(i)
- if (digOnly or count >= blockSlotsToKeep or not turtle.compareTo(1)) then
- turtle.drop()
- else
- count = count + 1
- end
- end
- turtle.select(1)
- tell("Empty done !")
- turtle.turnRight()
- turtle.turnRight()
- goToPos(x, y, z, side, "zyxs")
- end
- end
- function manageInventory()
- local blockSlots = {}
- local emptySlot = -1
- if (digOnly) then
- return true
- end
- -- Superposition d'un maximum d'items --
- for i, slot in ipairs(slots) do
- turtle.select(slot)
- if (turtle.getItemCount(slot) > 0) then
- for j, slot2 in ipairs(slots) do
- if (slot2 > slot) then
- if (turtle.getItemSpace(slot) == 0) then
- break
- end
- if (turtle.compareTo(slot2)) then
- turtle.select(slot2)
- turtle.transferTo(slot)
- turtle.select(slot)
- end
- end
- end
- end
- end
- -- On met tous les items le plus en haut à gauche possible --
- for i, slot in ipairs(slots) do
- if (turtle.getItemCount(slot) == 0) then
- for j, slot2 in ipairs(slots) do
- if (slot2 > slot and turtle.getItemCount(slot2) > 0) then
- turtle.select(slot2)
- turtle.transferTo(slot)
- turtle.select(slot)
- break
- end
- end
- end
- end
- -- Chargement des infos de l'inventaire pour la gestion des blocs de remplissage --
- for i, slot in ipairs(slots) do
- turtle.select(slot)
- if (turtle.compareTo(1)) then
- table.insert(blockSlots, slot)
- end
- end
- -- On cherche un slot vide --
- for i, slot in ipairs(slots) do
- if (not digOnly and slot > blockSlotsToKeep and turtle.getItemCount(slot) == 0) then
- emptySlot = slot
- break
- end
- end
- if (emptySlot == -1) then
- return false
- end
- -- On supprime de la table le slot de référence --
- table.remove(blockSlots, 1)
- local blockSlotsLen = #blockSlots
- -- Pour chaque emplacement de block qu'on
- -- veut "reserver", on y place de la block
- -- s'il y en a ailleurs --
- -- --
- -- Ex : Si le slot 2 est vide alors que
- -- blockslotsToKeep vaut 4, on va chercher
- -- de la block dans les slots de 3 a 16 et,
- -- si on en trouve, on va la mettre dans le
- -- slot 2 --
- for i = 2, blockSlotsToKeep do
- turtle.select(i)
- if (not turtle.compareTo(1) and turtle.getItemCount(i) > 0) then
- if (blockSlotsLen > 0) then
- slotTransfer(blockSlots[1], i, emptySlot)
- table.remove(blockSlots, 1)
- blockSlotsLen = blockSlotsLen - 1
- else
- break
- end
- end
- end
- end
- function slotTransfer(slotSrc, slotDest, slotEmpty)
- local hasEmpty = false
- if (turtle.getItemCount(slotEmpty) > 0) then
- turtle.select(slotEmpty)
- turtle.drop()
- hasEmpty = true
- end
- if (turtle.getItemCount(slotDest) == 0) then
- turtle.select(slotSrc)
- turtle.transferTo(slotDest)
- else
- turtle.select(slotDest)
- turtle.transferTo(slotEmpty)
- turtle.select(slotSrc)
- turtle.transferTo(slotDest)
- turtle.select(slotEmpty)
- turtle.transferTo(slotSrc)
- end
- if (hasEmpty) then
- turtle.suck()
- end
- end
- function isInventoryFull(var)
- for i, slot in ipairs(slots) do
- if (turtle.getItemCount(slot) == 0) then
- return false
- end
- end
- if (not var) then
- manageInventory()
- return isInventoryFull(true)
- end
- return true
- end
- function putBlock(face)
- turtle.select(1)
- if (digOnly) then
- return
- end
- if (face == nil) then
- if ((side == 1 and fillRightWall) or (side == 3 and fillLeftWall) or (side == 0 and fillBackWall)) then
- if (not turtle.detect()) then
- while (not turtle.place()) do
- turtle.attack()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- elseif (not turtle.compare()) then
- dig()
- while (not turtle.place()) do
- turtle.attack()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- end
- end
- elseif (face == "up") then
- if (fillCeiling) then
- if (not turtle.detectUp()) then
- while (not turtle.placeUp()) do
- turtle.attackUp()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- elseif (not turtle.compareUp()) then
- dig("up")
- while (not turtle.placeUp()) do
- turtle.attackUp()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- end
- end
- elseif (face == "down") then
- if (fillFloor) then
- if (not turtle.detectDown()) then
- while (not turtle.placeDown()) do
- turtle.attackDown()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- elseif (not turtle.compareDown()) then
- dig("down")
- while (not turtle.placeDown()) do
- turtle.attackDown()
- end
- infos.blocksPlaced = infos.blocksPlaced + 1
- end
- end
- end
- end
- function readPrompt(prompt, mode, onError, onSuccess, needClear, stricly)
- local value = nil
- while (value == nil) do
- if (needClear) then
- shell.run("clear")
- end
- write(prompt)
- value = read()
- if (mode == 1) then
- value = tonumber(value)
- if (value == nil or (stricly and value <= 0)) then
- onError()
- value = nil
- end
- elseif (mode == 2) then
- if (value ~= "y" and value ~= "n") then
- onError()
- value = nil
- end
- end
- end
- onSuccess()
- return value
- end
- function configMode()
- local isOk = false
- local widTmp, heiTmp, lenTmp, tmpBlockSlotsToKeep = nil, nil, nil, nil
- local goBackForCobbleTmp, goBackToEmptyTmp, sendOnRednetTmp = nil, nil, nil
- local fillLeftWallTmp, fillRightWallTmp, fillFloorTmp, fillCeilingTmp, fillBackWallTmp = nil, nil, nil, nil, nil
- local digOnlyTmp = nil
- local timeToWait = 0
- local success = function()
- print("Hum... Ok !")
- os.sleep(timeToWait)
- end
- local numberError = function()
- print("Invalid entry : need a strictly positive number.")
- os.sleep(timeToWait)
- end
- local ynError = function()
- print("I told you to ask y or n.")
- os.sleep(timeToWait)
- end
- while (not isOk) do
- widTmp = readPrompt("Tunnel width : ", 1, numberError, success, true, true)
- heiTmp = readPrompt("Tunnel height : ", 1, numberError, success, true, true)
- lenTmp = readPrompt("Tunnel length : ", 1, numberError, success, true, true)
- digOnlyTmp = readPrompt("Run in dig only ? (y/n) ", 2, ynError, success, true)
- if (digOnlyTmp == "n") then
- fillLeftWallTmp = readPrompt("Should I fill the left wall ? (y/n) ", 2, ynError, success, true)
- fillRightWallTmp = readPrompt("Should I fill the right wall ? (y/n) ", 2, ynError, success, true)
- fillBackWallTmp = readPrompt("Should I fill the back wall ? (y/n) ", 2, ynError, success, true)
- fillFloorTmp = readPrompt("Should I fill the floor ? (y/n) ", 2, ynError, success, true)
- fillCeilingTmp = readPrompt("Should I fill the ceiling ? (y/n) ", 2, ynError, success, true)
- if (fillLeftWallTmp == "n" and fillRightWallTmp == "n" and fillBackWallTmp == "n" and fillFloorTmp == "n" and fillCeilingTmp == "n") then
- digOnlyTmp = "y"
- end
- end
- if (digOnlyTmp == "n") then
- tmpBlockSlotsToKeep = readPrompt("How many slots do you want me to keep for filling blocks ? ", 1, numberError, success, true, true)
- goBackForCobbleTmp = readPrompt("Should I go back to start when I need filling blocks ? (y/n) ", 2, ynError, success, true)
- end
- goBackToEmptyTmp = readPrompt("Should I empty when I'm full ? (y/n) ", 2, ynError, success, true)
- sendOnRednetTmp = readPrompt("Should I send infos on rednet ? (y/n) ", 2, ynError, success, true)
- shell.run("clear")
- print("Tunnel w = "..widTmp.." h = "..heiTmp.." l = "..lenTmp)
- print("Dig Only : "..digOnlyTmp)
- if (digOnlyTmp == "n") then
- print("Fill left wall : "..fillLeftWallTmp)
- print("Fill right wall : "..fillRightWallTmp)
- print("Fill back wall : "..fillBackWallTmp)
- print("Fill floor wall : "..fillFloorTmp)
- print("Fill ceiling wall : "..fillCeilingTmp)
- print("Blocks to keep : "..tmpBlockSlotsToKeep)
- print("Back when need blocks : "..goBackForCobbleTmp)
- end
- print("Empty when full : "..goBackToEmptyTmp)
- print("Infos on rednet : "..sendOnRednetTmp)
- print("")
- isOk = readPrompt("Is this ok for you ? (y/n) ", 2, ynError, success, false)
- if (isOk == "y") then isOk = true else isOk = false end
- end
- width = widTmp
- height = heiTmp
- length = lenTmp
- blockSlotsToKeep = tmpBlockSlotsToKeep
- if (digOnlyTmp == "y") then digOnly = true else digOnly = false end
- if (fillLeftWallTmp == "y") then fillLeftWall = true else fillLeftWall = false end
- if (fillRightWallTmp == "y") then fillRightWall = true else fillRightWall = false end
- if (fillBackWallTmp == "y") then fillBackWall = true else fillBackWall = false end
- if (fillCeilingTmp == "y") then fillCeiling = true else fillCeiling = false end
- if (fillFloorTmp == "y") then fillFloor = true else fillFloor = false end
- if (goBackForCobbleTmp == "y") then goBackForCobble = true else goBackForCobble = false end
- if (goBackToEmptyTmp == "y") then goBackForEmpty = true else goBackForEmpty = false end
- if (sendOnRednetTmp == "y") then sendOnRednet = true else sendOnRednet = false end
- end
- function dig(face)
- if (face == nil) then
- while (turtle.detect()) do
- while (not turtle.dig()) do
- turtle.attack()
- end
- os.sleep(0.5)
- infos.blocksDug = infos.blocksDug + 1
- end
- elseif (face == "up") then
- while (turtle.detectUp()) do
- while (not turtle.digUp()) do
- turtle.attackUp()
- end
- os.sleep(0.5)
- infos.blocksDug = infos.blocksDug + 1
- end
- elseif (face == "down") then
- while (not turtle.digDown()) do
- turtle.attackDown()
- end
- infos.blocksDug = infos.blocksDug + 1
- end
- end
- function checkBlock()
- local countBlock = 0
- if (digOnly) then
- return
- end
- if (turtle.getItemCount(1) < 5) then
- manageInventory()
- else
- return
- end
- for i = 1, blockSlotsToKeep do
- turtle.select(i)
- if (turtle.compareTo(1)) then
- countBlock = countBlock + turtle.getItemCount(i)
- end
- end
- if (countBlock < 5 and goBackForCobble) then
- tell("Need block !")
- if (moved == 0) then goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx") end
- moved = 1
- os.sleep(10)
- return checkBlock()
- end
- if (moved == 1) then
- tell("Going back to work !")
- goToPos(x, y, z, side, "zyxs")
- moved = 0
- end
- end
- function left()
- turnLeft()
- forward()
- turnRight()
- end
- function forward()
- while (not turtle.forward()) do
- if (turtle.detect()) then
- turtle.dig()
- else
- turtle.attack()
- end
- end
- if (side == 0) then
- x = x + 1
- elseif (side == 1) then
- y = y + 1
- elseif (side == 2) then
- x = x - 1
- elseif (side == 3) then
- y = y - 1
- else
- tell("Unknown side found : "..side)
- end
- p = p + 1
- end
- function back()
- if (turtle.back()) then
- if (side == 0) then
- x = x - 1
- elseif (side == 1) then
- y = y - 1
- elseif (side == 2) then
- x = x + 1
- elseif (side == 3) then
- y = y + 1
- else
- tell("Unknown side found : "..side)
- end
- else
- turtle.turnLeft()
- turtle.turnLeft()
- forward()
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- function up()
- while (not turtle.up()) do
- if (turtle.detectUp()) then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- end
- z = z + 1
- p = p + 1
- end
- function down()
- while (not turtle.down()) do
- if (turtle.detectDown()) then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- end
- z = z - 1
- end
- function turnLeft()
- turtle.turnLeft()
- side = side - 1
- if (side < 0) then
- side = side + 4
- end
- end
- function turnRight()
- turtle.turnRight()
- side = side + 1
- if (side >= 4) then
- side = side - 4
- end
- end
- function right()
- turnRight()
- forward()
- turnLeft()
- end
- function tell(message)
- screenQuery("update", "message", message)
- screenQuery("draw")
- if (sendOnRednet) then
- rednet.broadcast(name.." "..message)
- end
- end
- function goToPos(xTmp, yTmp, zTmp, sideTmp, order)
- local xMove = function()
- while (xTmp ~= 0) do
- if (xTmp > 0) then
- while (not turtle.forward()) do
- turtle.dig()
- turtle.attack()
- os.sleep(0.4)
- end
- xTmp = xTmp - 1
- else
- if (not turtle.back()) then
- turtle.turnLeft()
- turtle.turnLeft()
- while (not turtle.forward()) do
- turtle.dig()
- turtle.attack()
- os.sleep(0.4)
- end
- turtle.turnRight()
- turtle.turnRight()
- end
- xTmp = xTmp + 1
- end
- end
- end
- local yMove = function()
- if (yTmp > 0) then
- turtle.turnRight()
- while (yTmp > 0) do
- while (not turtle.forward()) do
- turtle.dig()
- turtle.attack()
- os.sleep(0.4)
- end
- yTmp = yTmp - 1
- end
- turtle.turnLeft()
- elseif (yTmp < 0) then
- turtle.turnLeft()
- while (yTmp < 0) do
- while (not turtle.forward()) do
- turtle.dig()
- turtle.attack()
- os.sleep(0.4)
- end
- yTmp = yTmp + 1
- end
- turtle.turnRight()
- end
- end
- local zMove = function()
- while (zTmp ~= 0) do
- if (zTmp < 0) then
- while (not turtle.down()) do
- turtle.attackDown()
- turtle.digDown()
- os.sleep(0.4)
- end
- zTmp = zTmp + 1
- else
- while (not turtle.up()) do
- turtle.attackUp()
- turtle.digUp()
- os.sleep(0.4)
- end
- zTmp = zTmp - 1
- end
- end
- end
- local sMove = function()
- while (sideTmp ~= 0) do
- if (sideTmp > 0) then
- turtle.turnRight()
- sideTmp = sideTmp - 1
- else
- turtle.turnLeft()
- sideTmp = sideTmp + 1
- end
- end
- end
- for a = 1, 4 do
- if (order:sub(a, a) == "x") then
- xMove()
- elseif (order:sub(a, a) == "y") then
- yMove()
- elseif (order:sub(a, a) == "z") then
- zMove()
- elseif (order:sub(a, a) == "s") then
- sMove()
- end
- end
- end
- -- Point de départ du programme --
- main()
Add Comment
Please, Sign In to add comment