Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ===== API do A.T.A.R.I. (Autômato para Tarefas Altamente Repetitivas e Inconvenientes) ===== --
- -- Autor: CultistaDeCrocs
- -- Variáveis
- blocosLixo = {
- "minecraft:cobblestone",
- "minecraft:stone",
- "minecraft:dirt",
- "minecraft:gravel",
- "minecraft:tuff",
- "minecraft:sand",
- "minecraft:cobbled_deepslate",
- "minecraft:diorite",
- "minecraft:granite",
- "minecraft:andesite",
- "quark:cobbled_deepslate",
- "create:ochrum",
- "minecraft:dripstone_block"
- }
- -- Funções de lógica
- function existeEm(str, lst) -- Procura um item numa lista
- for i=1, #lst do
- if(str == lst[i]) then
- return true
- end
- end
- return false
- end
- -- Funções de impressão
- function writeSync(str) -- Função de write síncrona (ocorre em todos os PCS conectados)
- write(str)
- rednet.broadcast({"write", {str}}, protocolo)
- end
- function printSync(str) -- Função de print síncrona (ocorre em todos os PCS conectados)
- print(str)
- rednet.broadcast({"print", {str}}, protocolo)
- end
- function centralizaCursorAsync(tamanho) -- [ASSÍNCRONO] Centraliza o cursor na tela dependendo do tamanho de uma string
- local x, y = term.getCursorPos()
- local width, height = term.getSize()
- term.setCursorPos(math.floor((width - tamanho) / 2) + 1, y)
- end
- function centralizaCursor(tamanho) -- Centraliza o cursor na tela dependendo do tamanho de uma string
- centralizaCursorAsync(tamanho)
- rednet.broadcast({"centralizaCursor", {tamanho}}, protocolo)
- end
- function printCorAsync(str, cor) -- [ASSÍNCRONO] Imprime o texto com a cor desejada, depois volta pra branco
- term.setTextColor(cor)
- print(str)
- term.setTextColor(colors.white)
- end
- function printCor(str, cor) -- Imprime o texto com a cor desejada, depois volta pra branco
- printCorAsync(str, cor)
- rednet.broadcast({"printCor", {str, cor}}, protocolo)
- end
- function printCentro(str) -- Escreve centralizado
- centralizaCursor(#str)
- printSync(str)
- end
- function printCentroCor(str, cor) -- Escreve centralizado e colorizado
- centralizaCursor(#str)
- printCor(str, cor)
- end
- function writeCorAsync(str, cor) -- [ASSÍNCRONO] Imprime o texto com a cor desejada, sem \n
- term.setTextColor(cor)
- write(str)
- term.setTextColor(colors.white)
- end
- function writeCor(str, cor) -- Imprime o texto com a cor desejada, sem \n
- writeCorAsync(str, cor)
- rednet.broadcast({"writeCor", {str, cor}}, protocolo)
- end
- function writeCentro(str) -- Escreve centralizado, sem \n
- centralizaCursor(#str)
- writeSync(str)
- end
- function writeCentroCor(str, cor) -- Escreve centralizado e colorizado, sem \n
- centralizaCursor(#str)
- writeCor(str, cor)
- end
- function limpaTelaAsync() -- [ASSÍNCRONO] Limpa e reseta a tela
- term.clear()
- term.setCursorPos(1,1)
- end
- function limpaTela() -- Limpa e reseta a tela
- limpaTelaAsync()
- rednet.broadcast({"limpaTela", {}}, protocolo)
- end
- function printBool(str, valor) -- Imprime um valor booleano em cinza se falso e verde se verdadeiro
- writeSync(str..": ")
- if (valor) then
- printCor("true", colors.lime)
- else
- printCor("false", colors.lightGray)
- end
- end
- function imprimirTabela(tabela) -- Imprime uma tabela com os números coloridos
- for i=1, #tabela do
- writeCor(i, colors.orange)
- printSync(" "..tabela[i])
- end
- end
- function imprimeDivisoriaAsync(cor) -- [ASSÍNCRONO] Imprime uma divisória que ocupa toda a tela
- local width, height = term.getSize()
- for i=1, width do
- writeCorAsync("=", cor)
- end
- write("\n")
- end
- function imprimeDivisoria(cor) -- Imprime uma divisória que ocupa toda a tela
- imprimeDivisoriaAsync(cor)
- rednet.broadcast({"imprimeDivisoria", {cor}}, protocolo)
- end
- -- Funções de interface de usuário
- function atualizarMonitor(imprimirInformacao) -- Atualiza o monitor e executa a função de imprimir informação do programa
- limpaTela()
- imprimeDivisoria(colors.yellow)
- printCentroCor("I N F O R M A T I O N", colors.orange)
- imprimirInformacao()
- imprimeDivisoria(colors.yellow)
- end
- function imprimirCombustivel()
- writeSync("Fuel: ")
- if(turtle.getFuelLevel() == "unlimited") then
- printCor("unlimited", colors.blue)
- elseif (turtle.getFuelLevel() > 0) then
- printCor(turtle.getFuelLevel(), colors.green)
- else
- printCor("0", colors.red)
- esperarCarvao()
- end
- end
- function esperarCarvao() -- Espera chegar carvão e imprime uma mensagem
- printCentroCor("WARNING: NO FUEL", colors.red)
- printCentroCor("Please put coal in my 1st slot.", colors.orange)
- while not recarregar() do
- os.sleep(1)
- end
- end
- function agradece(passos) -- Agradece por usar o programa
- limpaTela()
- printCor("Thank you for using this program!\n", colors.orange)
- writeCor("Blocks traveled: ", colors.cyan)
- printSync(passos)
- writeCor("Remaining fuel: ", colors.cyan)
- printSync(turtle.getFuelLevel())
- printCor("\nmade by bogProphet", colors.lime)
- imprimeDivisoria(colors.yellow)
- end
- function informacaoTitulo(nome) -- Imprime o título e a informação sobre o combustível
- limpaTela()
- printCentroCor("==== "..nome.." ====\n", colors.lime)
- end
- function informacaoCombustivelNecessario(tamanho) -- Imprime informação sobre combustível se não houver o suficiente
- local combustivel = turtle.getFuelLevel()
- if(combustivel ~= "unlimited" and combustivel < tamanho) then
- local combustivelNecessario = (tamanho) - combustivel
- write("I will need ")
- writeCor(combustivelNecessario, colors.orange)
- write(" units of fuel. That is the equivalent of ")
- writeCor(math.ceil(combustivelNecessario/80), colors.orange)
- write(" units of coal. Please put that much fuel in my 1st slot and ")
- writeCor("press any button.\n", colors.lightBlue)
- write("> ")
- read()
- end
- end
- function lerNumero() -- Lê o input e converte pra número
- write("\n> ")
- return tonumber(read())
- end
- function pedeEscolha(maximo) -- Pede para o usuário escolher e fecha se não for válida
- local escolha = lerNumero()
- if(escolha < 1 or escolha > maximo) then
- limpaTela()
- printCor("ERROR: Invalid choice. Run the program again and input a valid value (between 1 and "..maximo..").", colors.red)
- error()
- else
- return escolha
- end
- end
- -- Funções de controle do robô
- function recarregar() -- Recarrega o robô
- turtle.select(1)
- return turtle.refuel()
- end
- function jogaLixoFora() -- Dropa todos os itens do inventário que estão em blocosLixo[]
- for i=1, 16 do
- turtle.select(i)
- local currentItem = turtle.getItemDetail(i)
- if currentItem ~= nil then
- if existeEm(currentItem.name, blocosLixo) then
- turtle.drop()
- end
- end
- end
- counterDropar = 0
- turtle.select(1)
- end
- function checaInventarioVazio() -- Retorna se o inventário está vazio
- for i=2, 16, 1 do
- turtle.select(i)
- if(turtle.getItemCount (i) > 0) then
- return false
- end
- end
- return true
- end
- function colocarBloco() -- Procura blocos no inventário e coloca
- local maoCheia = false
- local mao = 1
- if checaInventarioVazio() then
- limpaTela()
- printCor("ERROR: I'm out of blocks. Ending program...", colors.red)
- error()
- end
- turtle.place()
- end
- function colocarBaixo() -- Procura blocos no inventário e coloca pra baixo
- local maoCheia = false
- local mao = 1
- if checaInventarioVazio() then
- limpaTela()
- printCor("ERROR: I'm out of blocks. Ending program...", colors.red)
- error()
- end
- turtle.placeDown()
- end
- -- Funções síncronas e suas contrapartes assíncronas
- funcoesRecebe = {
- ["print"] = print,
- ["printCor"] = printCorAsync,
- ["write"] = write,
- ["writeCor"] = writeCorAsync,
- ["limpaTela"] = limpaTelaAsync,
- ["imprimeDivisoria"] = imprimeDivisoriaAsync,
- ["centralizaCursor"] = centralizaCursorAsync,
- }
- -- Funções de rednet
- function procuraModem()
- if (peripheral.find("modem") == nil) then
- return false
- else
- return true
- end
- end
- function estaAberto() -- Retorna se o pc está aberto à rednet
- for i, lado in ipairs(rs.getSides()) do
- if rednet.isOpen(lado) then
- return true
- end
- end
- return false
- end
- function recebe() -- Recebe uma mensagem e executa a função dela se existir, retornando se a função foi executada ou não
- local id, mensagem = rednet.receive(protocolo)
- local executada
- -- Se a função de mensagem[1] existe em funcoesRecebe, executa ela, com os argumentos de mensagem[2]. Senão, executada = false
- if(funcoesRecebe[mensagem[1]]) then
- funcoesRecebe[mensagem[1]](unpack(mensagem[2]))
- executada = true
- else
- executada = false
- end
- return mensagem, executada, id
- end
- -- Return da biblioteca
- if (procuraModem()) then
- -- Retorna funções com funcionalidade síncrona, se tiver modem
- return {
- printCor = printCor,
- writeCor = writeCor,
- printCorAsync = printCorAsync,
- writeCorAsync = writeCorAsync,
- centralizaCursor = centralizaCursor,
- centralizaCursorAsync = centralizaCursorAsync,
- printBool = printBool,
- atualizarMonitor = atualizarMonitor,
- imprimirCombustivel = imprimirCombustivel,
- esperarCarvao = esperarCarvao,
- recarregar = recarregar,
- informacaoTitulo = informacaoTitulo,
- informacaoCombustivelNecessario = informacaoCombustivelNecessario,
- agradece = agradece,
- jogaLixoFora = jogaLixoFora,
- imprimirTabela = imprimirTabela,
- colocarBaixo = colocarBaixo,
- lerNumero = lerNumero,
- colocarBloco = colocarBloco,
- printCentro = printCentro,
- printCentroCor = printCentroCor,
- writeCentro = writeCentro,
- writeCentroCor = writeCentroCor,
- printSync = printSync,
- writeSync = writeSync,
- recebe = recebe,
- estaAberto = estaAberto,
- limpaTela = limpaTela,
- limpaTelaAsync = limpaTelaAsync,
- imprimeDivisoria = imprimeDivisoria,
- imprimeDivisoriaAsync = imprimeDivisoriaAsync,
- checaInventarioVazio = checaInventarioVazio,
- }
- else
- -- Senão, re-roteia as funções síncronas pra funções assíncronas
- return {
- printCor = printCorAsync,
- writeCor = writeCorAsync,
- printCorAsync = printCorAsync,
- writeCorAsync = writeCorAsync,
- centralizaCursor = centralizaCursorAsync,
- centralizaCursorAsync = centralizaCursorAsync,
- printBool = printBool,
- atualizarMonitor = atualizarMonitor,
- imprimirCombustivel = imprimirCombustivel,
- esperarCarvao = esperarCarvao,
- recarregar = recarregar,
- informacaoTitulo = informacaoTitulo,
- informacaoCombustivelNecessario = informacaoCombustivelNecessario,
- agradece = agradece,
- jogaLixoFora = jogaLixoFora,
- imprimirTabela = imprimirTabela,
- colocarBaixo = colocarBaixo,
- lerNumero = lerNumero,
- colocarBloco = colocarBloco,
- printCentro = printCentro,
- printCentroCor = printCentroCor,
- writeCentro = writeCentro,
- writeCentroCor = writeCentroCor,
- printSync = print,
- writeSync = write,
- recebe = recebe,
- estaAberto = estaAberto,
- limpaTela = limpaTelaAsync,
- limpaTelaAsync = limpaTelaAsync,
- imprimeDivisoria = imprimeDivisoriaAsync,
- imprimeDivisoriaAsync = imprimeDivisoriaAsync,
- checaInventarioVazio = checaInventarioVazio,
- }
- end
Advertisement
Add Comment
Please, Sign In to add comment