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:
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:
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?
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()
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()
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?