--[[ Author: TheOriginalBIT Version: 1.0 Created: 29 Jan 2013 Last Update: 29 Jan 2013 License: COPYRIGHT NOTICE Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -Visible credit is given to the original author. -The software is distributed in a non-profit way. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- ---------------------------------------------------------- -------------------- Screensaver Utils ------------------- ---------------------------------------------------------- local w,h = term.getSize() local function hexLookup( char ) local value = tonumber(char, 16) return (value and math.pow(2, value) or nil) end local function initTimers( update_freq ) local t = {} t["update"] = os.startTimer( update_freq or 0.2 ) t["time"] = os.startTimer(0.4) return t end local function colorCheck() return (term.isColor and term.isColor()) end local function clear(b,t) term.setBackgroundColor(b or colors.black) term.setTextColor(t or colors.white) term.setCursorPos(1,1) term.clear() end ---------------------------------------------------------- --------------- Bouncing Ball Render Mode ---------------- ---------------------------------------------------------- local function data_struct_ball() local t = {} t["x"] = w / 2 t["y"] = h / 2 t["color"] = colors.red t["moveX"] = 1 t["moveY"] = 1 return t end local function render_ball( data ) term.setCursorPos( data["x"], data["y"] ) term.setBackgroundColor( data["color"] ) write(" ") end local function update_ball( data ) if data["x"] >= w then data["moveX"] = -1 elseif data["x"] <= 2 then data["moveX"] = 1 end if data["y"] >= h then data["moveY"] = -1 elseif data["y"] <= 2 then data["moveY"] = 1 end data["x"] = data["x"] + data["moveX"] data["y"] = data["y"] + data["moveY"] end ---------------------------------------------------------- -------------------- Dots Render Mode -------------------- ---------------------------------------------------------- local function data_struct_dots() local t = {} t["image"] = {} return t end local function render_dots( data ) for _, i in pairs(data["image"]) do term.setCursorPos(i["x"], i["y"]) term.setBackgroundColor(i["color"]) term.write(" ") end end local function update_dots( data ) if #data["image"] >= 100 then data["image"] = {} end newDot = { ["x"] = math.random(1, w), ["y"] = math.random(1, h), ["color"] = 2 ^ math.random(0,15) } table.insert(data["image"], newDot) end ---------------------------------------------------------- -------------- Horizontal Lines Render Mode -------------- ---------------------------------------------------------- local function data_struct_horizontallines() return {} end local function render_horizontallines( data ) for y = 1, h do term.setBackgroundColor( 2 ^ math.random(0,15) ) term.setCursorPos(1,y) term.clearLine() end end local function update_horizontallines( data ) end ---------------------------------------------------------- --------------- Vertical Lines Render Mode --------------- ---------------------------------------------------------- local function data_struct_verticallines() return {} end local function render_verticallines( data ) for x = 1, w do term.setBackgroundColor( 2 ^ math.random(0,15) ) for y = 1, h do term.setCursorPos(x,y) write(" ") end end end local function update_verticallines( data ) end ---------------------------------------------------------- ---------------- TheOriginalBIT's Mode!! ----------------- ---------------- The Creator Render Mode ----------------- ---------------------------------------------------------- local function data_struct_tobit() local t = {} t["image"] = { "7 7 7 7 77 3 3 3 ", "7 7 7 3 3 ", "777 777 777 777 77 7 777 7 777 777 7 333 3 333", "7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 ", "7 7 7 777 7 7 7 7 777 7 7 7 777 7 3 3 3 3 ", "7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 ", " 77 7 7 777 777 7 7 777 7 7 7 7 7 7 333 3 33" } t["x"] = math.random(1, w - #t["image"][1]) t["y"] = math.random(1, h - (#t["image"] + 2)) t["moveX"] = 1 t["moveY"] = 1 return t end local function render_tobit( data ) term.setCursorPos(1,1) term.setBackgroundColor(colors.white) term.clear() for r = 1, #data["image"] do for c = 1, data["image"][r]:len() do term.setCursorPos(data["x"] + c, data["y"] + r) char = data["image"][r]:sub(c,c) if char ~= " " then term.setBackgroundColor( hexLookup( char ) ) write(" ") end end print() end term.setBackgroundColor(colors.white) term.setTextColor(colors.black) local theTime = textutils.formatTime(os.time(), false) local timeX = data["x"] + (data["image"][1]:len() / 2) - 2 local timeY = data["y"] + (#data["image"]) + 2 term.setCursorPos(timeX, timeY) term.write(theTime) term.setTextColor(colors.white) end local function update_tobit( data ) if data["x"] + data["image"][1]:len() >= w then data["moveX"] = -1 elseif data["x"] < 1 then data["moveX"] = 1 end if data["y"] + #data["image"] >= h - 2 then data["moveY"] = -1 elseif data["y"] < 1 then data["moveY"] = 1 end data["x"] = data["x"] + data["moveX"] data["y"] = data["y"] + data["moveY"] end ---------------------------------------------------------- ---------------- Moving Time Render Mode ----------------- ---------------------------------------------------------- local function data_struct_timemove() local t = {} local theTime = textutils.formatTime(os.time(), false) t["timeX"] = math.random(1, (w - theTime:len())) t["timeY"] = math.random(1, h) t["moveX"] = 1 t["moveY"] = 1 return t end local function render_timemove( data ) term.setCursorPos( data["timeX"], data["timeY"] ) write( textutils.formatTime(os.time(), false) ) end local function update_timemove( data ) local theTime = textutils.formatTime(os.time(), false) data["timeX"] = data["timeX"] + data["moveX"] data["timeY"] = data["timeY"] + data["moveY"] if data["timeX"] <= 1 then data["timeX"] = 1 data["moveX"] = 1 elseif (data["timeX"] + theTime:len()) - 1 >= w then data["timeX"] = w - theTime:len() + 1 data["moveX"] = -1 end if data["timeY"] <= 1 then data["timeY"] = 1 data["moveY"] = 1 elseif data["timeY"] >= h then data["timeY"] = h data["moveY"] = -1 end rand = math.random(0,10) if rand == 2 then data["moveX"] = -1 elseif rand == 4 then data["moveX"] = 1 elseif rand == 6 then data["moveY"] = -1 elseif rand == 8 then data["moveY"] = 1 end end ---------------------------------------------------------- -------------------- Main Render Func -------------------- ---------------------------------------------------------- local function render( render_func, update_func, data_struct, update_freq, draw_time ) local timers = initTimers( update_freq ) local theTime = textutils.formatTime(os.time(), false) while true do local event = { os.pullEventRaw() } clear() if event[1] == "key" or event[1] == "mouse_click" or event[1] == "mouse_scroll" or event[1] == "mouse_drag" then timers = nil return elseif event[1] == "timer" then if event[2] == timers["update"] then update_func( data_struct ) timers["update"] = os.startTimer( update_freq ) elseif event[2] == timers["time"] then theTime = textutils.formatTime(os.time(), false) timers["time"] = os.startTimer(0.4) end end render_func( data_struct ) term.setBackgroundColor(colors.white) term.setTextColor(colors.black) if draw_time then term.setCursorPos(w - theTime:len() - 2, h - 1) write(theTime) end end end ---------------------------------------------------------- ---------------- Render Modes Table Funcs ---------------- ---------------------------------------------------------- local RENDER_MODES = { ["bouncing_ball"] = { render_ball, update_ball, data_struct_ball, 0.2, true, "Moving Ball" }, ["dots"] = { render_dots, update_dots, data_struct_dots, 0.2, true, "Random Dots" }, ["horizontal_lines"] = { render_horizontallines, update_horizontallines, data_struct_horizontallines, 2, true, "Colorful Lines (Horizontal)" }, ["vertical_lines"] = { render_verticallines, update_verticallines, data_struct_verticallines, 2, true, "Colorful Lines (Vertical)" }, ["the-creator"] = { render_tobit, update_tobit, data_struct_tobit, 0.4, false, "TheOriginalBIT ( Creator )" }, ["moving_time"] = { render_timemove, update_timemove, data_struct_timemove, 0.5, false, "Bouncing Time"}, } function getRenderModeInfo() local retTable = {} for k, v in pairs(RENDER_MODES) do local t = { k, v[6] } table.insert( retTable, t ) end return retTable end function getRenderMode(key) return RENDER_MODES[key] end ---------------------------------------------------------- ------------------ Entry Func of Program ----------------- ---------------------------------------------------------- function run(render_mode, show_time) if show_time == nil then show_time = true end if not colorCheck() then error( "Sorry, this API only runs on Advanced Computers", 2) end if type(render_mode) ~= "string" then error( "Invalid render mode supplied", 2) end mode_info = getRenderMode( render_mode ) if not mode_info then error( "The render mode \""..render_mode.."\" does not exist", 2) end render_func, update_func, update_freq, display_time = mode_info[1], mode_info[2], mode_info[4], mode_info[5] data_struct = mode_info[3]() clear() render( render_func, update_func, data_struct, update_freq, ((display_time == true) and (show_time == true))) end