Skip to content

Instantly share code, notes, and snippets.

@ChaunceyHoover
Last active August 8, 2019 13:02
Show Gist options
  • Save ChaunceyHoover/10a9ff9d9994c9d1a78f3b40bc3061c0 to your computer and use it in GitHub Desktop.
Save ChaunceyHoover/10a9ff9d9994c9d1a78f3b40bc3061c0 to your computer and use it in GitHub Desktop.
A useful server welcome program for ComputerCraft, complete with hackermans style tendrils in the background. Can be used to show server rules and other important messages.
--[[ Configuration ]]--
-- Max amount of tendrils on screen at a time
max_tendrils = 50
-- How fast the screen updates
-- note: Setting it to 0 or less will cause program to not work properly
update_speed = 0.25
-- Possible colors tendrils can be
-- note: the more you put the same color in a table, the more likely it is to be chosen
cols = { colors.white, colors.gray, colors.lightGray, colors.white, colors.gray, colors.lightGray, colors.white, colors.gray, colors.lightGray, colors.white, colors.gray, colors.lightGray, colors.green }
-- Messages to be displayed, line by line, in a box in the center of the screen
-- Set to {""} if you want a blank line
message = {
{"SERVER RULES", colors.pink, 1},
{"==============", colors.red, 1},
{"1. No stealing or griefing", colors.green},
{"2. Do NOT use mods that create lag", colors.cyan},
{" without admin approval", colors.cyan},
{""},
{"SPAWN AREA", colors.yellow, 1},
{"============", colors.orange, 1},
{"If you want a portal in the spawn room,", colors.magenta, 1},
{"ask an admin to set you up!", colors.magenta, 1}
}
-- Message format: { <Message> [, Color, Position] } (where 0 = left aligned text, 1 = center aligned text)
-- default color = white, default position = left
-- Customizing the box - change these if you want different symbols
corner_symbol = "+"
horizontal_symbol = "-"
vertical_symbol = "|"
outline_color = colors.blue
-- dont touch below this line unless you know what you're doing :)
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
-- Get CC terminal info
w, h = term.getSize()
-- Get useful info about message
longest_line = nil
if message ~= nil then
longest_line = string.len(message[1][1])
for i = 2, #message do
if message[i] ~= nil then
local msg_len = string.len(message[i][1])
if msg_len > longest_line then
longest_line = msg_len
end
end
end
end
-- "Tendril" = the line of hackermanz bits falling down the screen
tendrils = {}
tendril = { id = '', x = 0, y = 0, color = 0, length = 0 }
-- Generate a "random" UUID4
function uuid4()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function(c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
end
-- Generate a single random letter, symbol, or number
function hacker_bit()
return string.char(math.random(32, 126))
end
-- Creates a new tendril, assign its starting position along the top of monitor,
-- set its color, and generate an ID to have it removed
function tendril:new()
o = {
id = uuid4(),
x = math.random(1, w),
y = 1, length = math.random(math.floor(h / 6), math.ceil(h / 2.5)),
color = cols[math.random(1, #cols)]
}
setmetatable(o, self)
self.__index = self
return o
end
-- Remove tendril
function tendril:destroy()
for i = 1, #tendrils do
if tendrils[i].id == self.id then
table.remove(tendrils, i)
break
end
end
end
-- Update tendril position + tail
function tendril:update()
term.setCursorPos(self.x, self.y)
term.setTextColor(self.color)
term.write(hacker_bit())
for i = 1, self.length do
term.setCursorPos(self.x, self.y - i)
term.write(hacker_bit())
end
self.y = self.y + 1
if self.y - self.length > h then
self:destroy()
end
end
-- Main program
while true do
-- Clear terminal and reset cursor position
term.clear()
term.setCursorPos(1, 1)
-- Create tendrils
if #tendrils < max_tendrils then
table.insert(tendrils, tendril:new(math.random(1, w)))
end
-- Update & draw tendrils
for i = 1, #tendrils do
if tendrils[i] then
tendrils[i]:update()
end
end
-- Draw message frame
local start_x = (w / 2) - math.floor(longest_line / 2) - 2
local start_y = (h / 2) - math.floor(#message / 2) - 2
term.setCursorPos(start_x, start_y)
term.setTextColor(outline_color)
term.write(corner_symbol .. string.rep(horizontal_symbol, longest_line + 4) .. corner_symbol)
for i = 1, #message + 2 do
term.setCursorPos(start_x, start_y + i)
term.write(vertical_symbol .. string.rep(' ', longest_line + 4) .. vertical_symbol)
end
term.setCursorPos(start_x, start_y + #message + 3)
term.write(corner_symbol .. string.rep(horizontal_symbol, longest_line + 4) .. corner_symbol)
-- Draw message
start_y = (h / 2) - math.floor(#message / 2)
term.setCursorPos((w / 2) - math.floor(longest_line / 2) + 1, start_y)
for i = 1, #message do
local x_pos = 0
local y_pos = start_y + (i - 1)
if message[i] ~= nil then
if (message[i][3] == nil or message[i][3] == 0) then
x_pos = start_x + 2
else
x_pos = (w / 2) - (message[i][1]:len() / 2) + 2
end
term.setTextColor(message[i][2] or colors.white)
end
term.setCursorPos(x_pos, y_pos)
if message[i] then
term.write(message[i][1])
end
end
sleep(update_speed)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment