computercraft-scripts/player-sensor/sugoma.lua

78 lines
1.6 KiB
Lua

-- Run this on a separate computer to display a log on an attached monitor.
-- This program expects a 6x6 monitor to be connected at the back,
-- a bell at the bottom and an ender modem at the right.
local monitor = peripheral.wrap("back")
local modem = peripheral.wrap("right")
local bell_side = "bottom"
local port = 12345
local height = 38
local messages = {}
local totalMsgs = 0
modem.open(port)
function bell()
redstone.setOutput(bell_side, true)
sleep(0.1)
redstone.setOutput(bell_side, false)
end
function clear()
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
end
function writeLine(text)
local x, y = monitor.getCursorPos()
monitor.setCursorPos(1, y + 1)
monitor.write(text)
end
function refresh()
clear()
monitor.setCursorPos(27, 1)
monitor.setTextColor(colors.blue)
monitor.write("Event log")
monitor.setTextColor(colors.white)
monitor.setCursorPos(1, 2)
if table.getn(messages) == 0 then
monitor.setCursorPos(22, height / 2)
monitor.setTextColor(colors.lightGray)
monitor.write("Nothing here yet ...")
monitor.setTextColor(colors.white)
else
for k,v in pairs(messages) do
writeLine(v)
end
monitor.setCursorPos(1, 1)
monitor.setTextColor(colors.lightGray)
monitor.write(totalMsgs.." total")
monitor.setTextColor(colors.white)
end
end
refresh()
while true do
local event, side, channel, replyChannel, message,
distance = os.pullEvent("modem_message")
table.insert(messages, message)
totalMsgs = totalMsgs + 1
while table.getn(messages) > height do
table.remove(messages, 1)
end
bell()
refresh()
end