This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GalactusX's profile picture

A simple program to open RedNet

Started by GalactusX, 15 September 2014 - 07:05 AM
GalactusX #1
Posted 15 September 2014 - 09:05 AM
hi all again, today I bring you a program I had to do to see if I have activated the RedNet on a computer or a turtle, the program is very simple:
1 Disable any active RedNet on computer / turtle
2nd Search all sides if there is a modem connected and active RedNet
3rd If RedNet is active on one side, and there is no an attached modem, which informs you and tells you that you add one position *
4th If there is no modem connected, and RedNet not find an active connection, you reported, you add a modem *

* You must restart the program to do the check and activate both RedNet as the modem :)/>

here is the code

http://pastebin.com/0k5RvWke

Spoiler



--                           FUNCIONAMIENTO / OPERATION
-- Spanish
--------------------------------------------------------------------------------
-- 1º Si hay presente un moden, activa la rednet en el 1º lado que lo encuentre
-- 2º si no hay un modem, pero esta activa la rednet, lo desactiva     
-- 3º Si no hay un modem, sale del programa
--------------------------------------------------------------------------------
-- English
--------------------------------------------------------------------------------
-- 1º If present a moden, active it in the 1st RedNet side who finds
-- 2º if no modem, but is RedNet activated, it disabled
-- 3º If no modem, exit the program
--------------------------------------------------------------------------------

-- Limpia la pantalla
-- clean the screen
function Limpiar()
term.clear()
term.setCursorPos(1,1)
end
-- Escribe el texto letra a letra
-- Write the text letter by letter
sp2 = textutils.slowPrint

-- Desactiva la conexion RedNet activa
-- Disables the connection active RedNet
function DesactivarRedNet()
        for side, label in ipairs(rs.getSides()) do
                rednet.close(label)
        end
end
-- English / Spanish translate
local translate = {["top"] = "Arriba", ["bottom"] = "Abajo", ["left"] = "Izquierda", ["right"] = "Derecha", ["front"] = "Delante", ["back"] = "Detras"}

-- Funcion para conectar RedNet
-- Function to connect RedNet
function ConetarRedNet()
local lado
        for side, label in ipairs(rs.getSides()) do
                lado = translate[label]
                Limpiar()
                print("looking modem")
--              print("Buscando modem")
                term.setCursorPos(15,1)
                sp2(".............")
                if peripheral.isPresent(label) then
                        if peripheral.getType(label) == "modem" then
                                DesactivarRedNet()
                                rednet.open(label)
                                Limpiar()
                                print("Modem enabled: ", lado)
--                              print("Modem activado: ", lado)
                                sp2("Closing program ....")
--                              sp2("Cerrando programa....")
                                sleep(2)
                                Limpiar()
                                return(true)
                        end
                end

                if not peripheral.isPresent(label) then
                        if rednet.isOpen(label) == true then
                                DesactivarRedNet()
                                Limpiar()
                                print("Active RedNet on: "..lado..", but is not a modem added in that position, add one")
--                              print("RedNet activo en: "..lado..", pero no se encuentra añadido un modem en esa posición, añada uno.")
                                sleep(3)
                                sp2("Closing connection RedNet....")
--                              sp2("Cerrando conexión a RedNet....")
                                sleep(3)
                                Limpiar()
                                return(true)
                        end
                end                    
    end
                sp2("No modem, no connection, add one to the device")
                --sp2("No hay modem, imposible conexion, añada uno al dispositivo")
                sleep(4)
                Limpiar()
    return(false)
end
ConetarRedNet()


be free to tell me if you find any bug, mistake or anything, and again, sorry for my bad english Q_Q
Edited on 15 September 2014 - 05:24 PM
Bomb Bloke #2
Posted 15 September 2014 - 02:49 PM
A couple of pointers;

Lua is case-sensitive. "Lado" (line 36) is not the same as "lado".

Tables are a great way to shrink bits of code down. Say you defined a table like this up the top of your script:

local translate = {["top"] = "Arriba",["bottom"] = "Abaajo", ["left"] = "Izquierda",
	["right"] = "Derecha", ["front"] = "Delante",["back"] = "Detras"}

Later on, you could then replace this chunk:

                                        if label == "top" then lado = "Arriba" -- Para traducir del ingles al español la posicion
                                                        elseif label == "bottom" then lado = "Abaajo"
                                                        elseif label == "left" then lado = "Izquierda"
                                                        elseif label == "right" then lado = "Derecha"                           
                                                        elseif label == "front" then lado = "Delante"
                                                        elseif label == "back" then lado = "Detras"
                                        end

… with this:

lado = translate[label]
GalactusX #3
Posted 15 September 2014 - 07:01 PM
thanks for the correction, my knowledge of Lua is still very crude, I've corrected the code :)/>

could someone tell me some server using ComputerCraft? I am Moderator on a server mods for 2 years, but when upgrading to version 1.7.10, this great Mod is not available, and I would like to keep playing and learning more
TurtleHunter #4
Posted 17 September 2014 - 06:30 PM
thanks for the correction, my knowledge of Lua is still very crude, I've corrected the code :)/>

could someone tell me some server using ComputerCraft? I am Moderator on a server mods for 2 years, but when upgrading to version 1.7.10, this great Mod is not available, and I would like to keep playing and learning more
Look in the beta testing sub-forum