Computercraft programs

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

sabreman

New Member
Jul 29, 2019
2
0
1
Having spent this evening playing with computer craft I can see how useful this one mod can be, but at the same time how tedious it can be to code some basic and very useful functions. So in this thread I think we should all share the small programs, code snippets, and functions that we find super useful (or just cool).

I wrote two very small functions, one that can find and return peripherals for you (and the side it's hooked into to) and another that returns whether the computer is powered by redstone and on what side. Using these I wrote a very basic ping host/client programs that let a variable number of clients ping one or more servers.

I guess this would be useful if you wanted to know what rednet network your turtles were on or something like that.

pingHost:
Code:
function getPeripheral(thing)
        local Sides = {
        "left","right",
        "top","bottom",
        "front","back",
    }
     
    for i,v in pairs(Sides) do
        if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
            print("'"..thing.."' found on side '"..v.."'.")
            return peripheral.wrap(v), v
        end
    end
 
    print("ERROR: '"..thing.."' not found!")
    return nil, nil
end
 
function checkRedstone()
    local power = false
    local poweredSides = {}
        poweredSides["left"] = false
        poweredSides["right"] = false
        poweredSides["top"] = false
        poweredSides["bottom"] = false
        poweredSides["front"] = false
        poweredSides["back"] = false
    local Sides = {
        "left","right",
        "top","bottom",
        "front","back",
    }
 
    for i,v in pairs(Sides) do
        poweredSides[v] = redstone.getInput(v)
        if (not power and poweredSides[v]) then
            power = true
        end
     
    end
 
    return power, poweredSides
end
 
 
function main()
    local Modem, side = getPeripheral("modem")
    local Monitor = getPeripheral("monitor")
 
    local msg = ""
    local id = 0
    local dist = 0
 
    if (Modem) then
        rednet.open(side)
        if (Monitor) then
            Monitor.setTextScale(0.5)
            local power = false
            local sides = {}
            power, sides = checkRedstone()
            while (power) do
                if (side["back"]) then
                    Monitor.clear()
                    Monitor.setCursorPos(1,1)
                    Monitor.write("Ping Host: on")
                    Monitor.setCursorPos(1,3)
                    Monitor.write("Last Client:")
                    Monitor.setCursorPos(1,4)
                    Monitor.write(tostring(id))
                    if (msg == "ping") then
                        rednet.send(id, "pong")
                    end
                else
                    Monitor.clear()
                    Monitor.setCursorPos(1,1)
                    Monitor.write("Ping Host: off")
                end
             
                local ti, tm, td = rednet.receive(0.1)
             
                if (tm == "ping") then
                    id = ti
                    msg = tm
                    dist = td
                end
                power, side = checkRedstone()
            end
         
            Monitor.clear()
            Monitor.setCursorPos(1,1)
            Monitor.write("Program")
            Monitor.setCursorPos(1,2)
            Monitor.write("Terminated")
        end
        rednet.close(side)
    end
end
 
main()
Powering the computer that's ruing this program from any side will keep the program itself running, while powering specifically from the back will turn the actual host part on/off.

pingClient:
Code:
function getPeripheral(thing)
    local Sides = {
        "left","right",
        "top","bottom",
        "front","back",
    }
 
    for i,v in pairs(Sides) do
        if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
            print("'"..thing.."' found on side '"..v.."'.")
            return peripheral.wrap(v), v
        end
    end
 
    print("ERROR: '"..thing.."' not found!")
    return nil, nil
end
 
function ping()
    local modem, side = getPeripheral("modem")
    local id = -1
 
    if (modem) then
        rednet.open(side)
            rednet.broadcast("ping")
            local hostId, msg, dist = rednet.receive(1)
            if (hostId == nil and msg == nil) then
                print("Ping: no ping host responce.")
            else
                print("Host Id: "..tostring(hostId)..", Distance: "..tostring(dist).."m")
                id = hostId
            end
        rednet.close(side)
    end
 
    return id
end
 
ping()
Simply call this program and it will try pining a server.

I hope to make the server one a tad more user friendly and perhaps give it the ability to count how many clients are "connected" to it.

On a side note, I'm having a REALLY hard time finding diamonds. I have over 6 stacks of iron, 32 uranium, 5 stacks of tin and copper each, crap ton of redstone, but not a single diamond. Am I in a bad biome or something?
 

Nemesis688

New Member
Jul 29, 2019
39
0
0
I think this is a pretty good idea but you could also just go to the computer craft forums and find pretty much the same thing. The programing is a tad too tedious for me so find pre made ones is nice. You can also enable the htttp option in the configs and be able to download programs straight from pastebin.

And for the diamonds, just do branch mining at layer 11. You should have some luck that way. If you get really desperate you can make a dense ores age and mine at 11. I did that and came back with like 4 stacks of diamond ores. I then used a fortune pick on them....