Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cargar funciones externas
- dofile("NearEDGE-Functions")
- -- DIRECCIÓN Y POSICIÓN RELATIVA
- Dir = 0 -- Dirección actual (0:+Z, 1:+X, 2:-Z, 3:-X)
- LocX, LocY, LocZ = 0, 0, 0 -- Posición relativa desde el inicio
- DirNames = { "in +Z", "in +X", "in -Z", "in -X" }
- -- GIRO Y ORIENTACIÓN
- function TurnLeft()
- turtle.turnLeft()
- Dir = (Dir - 1) % 4
- end
- function TurnRight()
- turtle.turnRight()
- Dir = (Dir + 1) % 4
- end
- function TurnShortest(targetDir)
- if ((Dir - 1) % 4 == targetDir) then
- TurnLeft()
- else
- TurnRight()
- end
- end
- function TurnToDir(targetDir)
- while Dir ~= targetDir do
- TurnShortest(targetDir)
- end
- end
- -- ACTUALIZACIÓN DE POSICIÓN RELATIVA
- function UpdateLocZX()
- local isX = Dir % 2 == 1
- local val = (Dir > 1) and -1 or 1
- if isX then
- LocX = LocX + val
- else
- LocZ = LocZ + val
- end
- end
- -- MOVIMIENTOS CON EXCAVACIÓN
- function DoDigMove()
- assert(DigMove(), "No se pudo mover " .. DirNames[Dir + 1])
- UpdateLocZX()
- end
- function DoDigMoveUp()
- assert(DigMoveUp(), "No se pudo subir")
- LocY = LocY + 1
- end
- function DoDigMoveDown()
- assert(DigMoveDown(), "No se pudo bajar")
- LocY = LocY - 1
- end
- -- MOVIMIENTO A COORDENADAS
- function DigMoveToX(x)
- local dir = (x - LocX > 0) and 1 or 3
- TurnToDir(dir)
- while LocX ~= x do DoDigMove() end
- end
- function DigMoveToY(y)
- while LocY ~= y do
- if y - LocY > 0 then DoDigMoveUp()
- else DoDigMoveDown() end
- end
- end
- function DigMoveToZ(z)
- local dir = (z - LocZ > 0) and 0 or 2
- TurnToDir(dir)
- while LocZ ~= z do DoDigMove() end
- end
- function DigMoveTo(x, y, z)
- DigMoveToY(y)
- DigMoveToX(x)
- DigMoveToZ(z)
- end
- -- INVENTARIO Y COMBUSTIBLE
- function deposit()
- for i = 1, 16 do
- turtle.select(i)
- while turtle.getItemCount() > 0 and not turtle.drop() do
- sleep(1)
- end
- end
- turtle.select(1)
- end
- function checkInventoryFull()
- return turtle.getItemCount(15) == 0
- end
- function CalculateFuelCost()
- local total = (LenX * LenY * LenZ - 1) + (LenX - 1) + (LenY - 1) + (LenZ - 1)
- if takeInitialStep then total = total + 2 end
- return total
- end
- -- INTERFAZ DE USUARIO
- function PromptUserForParameters()
- LenZ = ReadNum("Bloques hacia delante a cavar:")
- LenX = ReadNum("Filas:")
- LenY = ReadNum("Alto:")
- takeInitialStep = BooleanQuery("¿Empieza un bloque adelantado?")
- __HaltOnInterruption = BooleanQuery("¿Parar al tener un obstáculo?")
- print(string.format("X: %i\nY: %i\nZ: %i\nTake Initial Step: %s\nHalt on Interruption: %s\n",
- LenX, LenY, LenZ, tostring(takeInitialStep), tostring(__HaltOnInterruption)))
- return BooleanQuery("¿Es correcto?")
- end
- function CheckAndRefuel()
- local needed = CalculateFuelCost()
- local current = turtle.getFuelLevel()
- if current == "unlimited" then return true end
- if needed > turtle.getFuelLimit() then
- print("Requiere demasiado combustible. ¡Necesitas una turtle avanzada!")
- return false
- elseif current < needed then
- local recommended = GetRecommendedFuel(needed)
- print("Combustible insuficiente. Inserta al menos " .. needed ..
- " unidades y pulsa Enter para continuar.\n(Approx. " .. recommended[2] .. " " .. recommended[1] .. " recomendados)")
- io.read()
- if not ConsumeFuel() or turtle.getFuelLevel() < needed then
- print("No hay suficiente combustible.")
- return false
- end
- end
- return true
- end
- -- CICLO DE CAVADO
- function ExcavateCube()
- local ModX, ModZ = 0, 0
- if takeInitialStep then
- DoDigMove()
- LocZ = 0
- end
- while LocY ~= LenY do
- while LocX ~= (LenX - 1 + ModX) do
- while LocZ ~= (LenZ - 1 + ModZ) do
- DoDigMove()
- end
- -- Verificar inventario y depositar si es necesario
- if checkInventoryFull() then
- local x, y, z, d = LocX, LocY, LocZ, Dir
- DigMoveTo(0, 0, 0)
- TurnToDir(2)
- deposit()
- DigMoveTo(x, y, z)
- TurnToDir(d)
- end
- -- Cambio de dirección en el zigzag
- local nextDir = ((ModX * (1 / (LenX - 1))) * -2) + 1
- TurnToDir(nextDir)
- DoDigMove()
- TurnToDir((Dir + 2) % 4)
- ModZ = LocZ * -1
- end
- while LocZ ~= (LenZ - 1 + ModZ) do
- DoDigMove()
- end
- -- Preparar siguiente capa
- TurnToDir((Dir + 2) % 4)
- ModX, ModZ = LocX * -1, LocZ * -1
- if LocY ~= LenY - 1 then
- DoDigMoveUp()
- else
- break
- end
- end
- -- Regresar al punto de inicio
- if takeInitialStep then
- DigMoveTo(0, 0, -1)
- else
- DigMoveTo(0, 0, 0)
- end
- print("¡Excavación terminada!")
- end
- -- PROGRAMA PRINCIPAL
- function Main()
- cls()
- print("DigCubeArea v1.0\n---------------------------\n")
- local confirmed = PromptUserForParameters()
- if not confirmed then
- print("¡Inténtalo de nuevo!")
- return
- end
- if not CheckAndRefuel() then
- return
- end
- cls()
- print("Iniciando excavación. ¡Suerte!")
- ExcavateCube()
- end
- -- EJECUTAR
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement