Advertisement
theoriginalbit

Screensaver Lite

Jan 28th, 2013
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.80 KB | None | 0 0
  1. --[[
  2. Author: TheOriginalBIT
  3. Version: 1.0
  4. Created: 29 Jan 2013
  5. Last Update: 29 Jan 2013
  6.  
  7. License:
  8.  
  9. COPYRIGHT NOTICE
  10. Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com]
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  13. associated documentation files (the "Software"), to deal in the Software without restriction,
  14. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  16. subject to the following conditions:
  17.  
  18. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19. -Visible credit is given to the original author.
  20. -The software is distributed in a non-profit way.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ]]--
  27.  
  28. ----------------------------------------------------------
  29. -------------------- Screensaver Utils -------------------
  30. ----------------------------------------------------------
  31.  
  32. local w,h = term.getSize()
  33.  
  34. local function hexLookup( char )
  35.   local value = tonumber(char, 16)
  36.   return (value and math.pow(2, value) or nil)
  37. end
  38.  
  39. local function initTimers( update_freq )
  40.   local t = {}
  41.  
  42.   t["update"] = os.startTimer( update_freq or 0.2 )
  43.   t["time"] = os.startTimer(0.4)
  44.  
  45.   return t
  46. end
  47.  
  48. local function colorCheck()
  49.   return (term.isColor and term.isColor())
  50. end
  51.  
  52. local function clear(b,t)
  53.   term.setBackgroundColor(b or colors.black)
  54.   term.setTextColor(t or colors.white)
  55.   term.setCursorPos(1,1)
  56.   term.clear()
  57. end
  58.  
  59. ----------------------------------------------------------
  60. --------------- Bouncing Ball Render Mode ----------------
  61. ----------------------------------------------------------
  62.  
  63. local function data_struct_ball()
  64.    
  65.     local t = {}
  66.    
  67.     t["x"] = w / 2
  68.     t["y"] = h / 2
  69.     t["color"] = colors.red
  70.     t["moveX"] = 1
  71.     t["moveY"] = 1
  72.    
  73.     return t
  74. end
  75.  
  76. local function render_ball( data )
  77.     term.setCursorPos( data["x"], data["y"] )
  78.     term.setBackgroundColor( data["color"] )
  79.     write(" ")
  80. end
  81.  
  82. local function update_ball( data )
  83.     if data["x"] >= w then
  84.         data["moveX"] = -1
  85.     elseif data["x"] <= 2 then
  86.         data["moveX"] = 1
  87.     end
  88.  
  89.     if data["y"] >= h then
  90.         data["moveY"] = -1
  91.     elseif data["y"] <= 2 then
  92.         data["moveY"] = 1
  93.     end
  94.  
  95.     data["x"] = data["x"] + data["moveX"]
  96.     data["y"] = data["y"] + data["moveY"]
  97. end
  98.  
  99. ----------------------------------------------------------
  100. -------------------- Dots Render Mode --------------------
  101. ----------------------------------------------------------
  102.  
  103. local function data_struct_dots()
  104.     local t = {}
  105.    
  106.     t["image"] = {}
  107.    
  108.     return t
  109. end
  110.  
  111. local function render_dots( data )
  112.     for _, i in pairs(data["image"]) do
  113.         term.setCursorPos(i["x"], i["y"])
  114.         term.setBackgroundColor(i["color"])
  115.         term.write(" ")
  116.     end
  117. end
  118.  
  119. local function update_dots( data )
  120.     if #data["image"] >= 100 then
  121.         data["image"] = {}
  122.     end
  123.  
  124.     newDot = {
  125.         ["x"] = math.random(1, w),
  126.         ["y"] = math.random(1, h),
  127.         ["color"] = 2 ^ math.random(0,15)
  128.     }
  129.    
  130.     table.insert(data["image"], newDot)
  131. end
  132.  
  133. ----------------------------------------------------------
  134. -------------- Horizontal Lines Render Mode --------------
  135. ----------------------------------------------------------
  136.  
  137. local function data_struct_horizontallines()
  138.     return {}
  139. end
  140.  
  141. local function render_horizontallines( data )
  142.     for y = 1, h do
  143.         term.setBackgroundColor( 2 ^ math.random(0,15) )
  144.         term.setCursorPos(1,y)
  145.         term.clearLine()
  146.     end
  147. end
  148.  
  149. local function update_horizontallines( data ) end
  150.  
  151. ----------------------------------------------------------
  152. --------------- Vertical Lines Render Mode ---------------
  153. ----------------------------------------------------------
  154.  
  155. local function data_struct_verticallines()
  156.     return {}
  157. end
  158.  
  159. local function render_verticallines( data )
  160.     for x = 1, w do
  161.         term.setBackgroundColor( 2 ^ math.random(0,15) )
  162.         for y = 1, h do
  163.             term.setCursorPos(x,y)
  164.             write(" ")
  165.         end
  166.     end
  167. end
  168.  
  169. local function update_verticallines( data ) end
  170.  
  171. ----------------------------------------------------------
  172. ---------------- TheOriginalBIT's Mode!! -----------------
  173. ---------------- The Creator Render Mode -----------------
  174. ----------------------------------------------------------
  175.  
  176. local function data_struct_tobit()
  177.     local t = {}
  178.    
  179.     t["image"] = {
  180.         "7   7              7     7        77 3   3 3  ",
  181.         "7   7                              7 3     3  ",
  182.         "777 777 777 777 77 7 777 7 777 777 7 333 3 333",
  183.         "7   7 7 7 7 7 7 7  7 7 7 7 7 7 7 7 7 3 3 3 3  ",
  184.         "7   7 7 777 7 7 7  7 777 7 7 7 777 7 3 3 3 3  ",
  185.         "7   7 7 7   7 7 7  7   7 7 7 7 7 7 7 3 3 3 3  ",
  186.         " 77 7 7 777 777 7  7 777 7 7 7 7 7 7 333 3  33"
  187.     }
  188.    
  189.     t["x"] = math.random(1, w - #t["image"][1])
  190.     t["y"] = math.random(1, h - (#t["image"] + 2))
  191.     t["moveX"] = 1
  192.     t["moveY"] = 1
  193.    
  194.     return t
  195. end
  196.  
  197. local function render_tobit( data )
  198.     term.setCursorPos(1,1)
  199.     term.setBackgroundColor(colors.white)
  200.     term.clear()
  201.     for r = 1, #data["image"] do
  202.         for c = 1, data["image"][r]:len() do
  203.             term.setCursorPos(data["x"] + c, data["y"] + r)
  204.             char = data["image"][r]:sub(c,c)
  205.  
  206.             if char ~= " " then
  207.                 term.setBackgroundColor( hexLookup( char ) )
  208.                 write(" ")
  209.             end
  210.         end
  211.         print()
  212.     end
  213.    
  214.     term.setBackgroundColor(colors.white)
  215.     term.setTextColor(colors.black)
  216.     local theTime = textutils.formatTime(os.time(), false)
  217.     local timeX = data["x"] + (data["image"][1]:len() / 2) - 2
  218.     local timeY = data["y"] + (#data["image"]) + 2
  219.     term.setCursorPos(timeX, timeY)
  220.     term.write(theTime)
  221.     term.setTextColor(colors.white)
  222. end
  223.  
  224. local function update_tobit( data )
  225.     if data["x"] + data["image"][1]:len() >= w then
  226.         data["moveX"] = -1
  227.     elseif data["x"] < 1 then
  228.         data["moveX"] = 1
  229.     end
  230.  
  231.     if data["y"] + #data["image"] >= h - 2 then
  232.         data["moveY"] = -1
  233.     elseif data["y"] < 1 then
  234.         data["moveY"] = 1
  235.     end
  236.    
  237.     data["x"] = data["x"] + data["moveX"]
  238.     data["y"] = data["y"] + data["moveY"]
  239. end
  240.  
  241. ----------------------------------------------------------
  242. ---------------- Moving Time Render Mode -----------------
  243. ----------------------------------------------------------
  244.  
  245. local function data_struct_timemove()
  246.     local t = {}
  247.     local theTime = textutils.formatTime(os.time(), false)
  248.    
  249.     t["timeX"] = math.random(1, (w - theTime:len()))
  250.     t["timeY"] = math.random(1, h)
  251.     t["moveX"] = 1
  252.     t["moveY"] = 1
  253.    
  254.     return t
  255. end
  256.  
  257. local function render_timemove( data )
  258.     term.setCursorPos( data["timeX"], data["timeY"] )
  259.     write( textutils.formatTime(os.time(), false) )
  260. end
  261.  
  262. local function update_timemove( data )
  263.     local theTime = textutils.formatTime(os.time(), false)
  264.    
  265.     data["timeX"] = data["timeX"] + data["moveX"]
  266.     data["timeY"] = data["timeY"] + data["moveY"]
  267.    
  268.     if data["timeX"] <= 1 then
  269.         data["timeX"] = 1
  270.         data["moveX"] = 1
  271.     elseif (data["timeX"] + theTime:len()) - 1 >= w then
  272.         data["timeX"] = w - theTime:len() + 1
  273.         data["moveX"] = -1
  274.     end
  275.    
  276.     if data["timeY"] <= 1 then
  277.         data["timeY"] = 1
  278.         data["moveY"] = 1
  279.     elseif data["timeY"] >= h then
  280.         data["timeY"] = h
  281.         data["moveY"] = -1
  282.     end
  283.    
  284.     rand = math.random(0,10)
  285.  
  286.     if rand == 2 then
  287.         data["moveX"] = -1
  288.     elseif rand == 4 then
  289.         data["moveX"] = 1
  290.     elseif rand == 6 then
  291.         data["moveY"] = -1
  292.     elseif rand == 8 then
  293.         data["moveY"] = 1
  294.     end
  295. end
  296.  
  297. ----------------------------------------------------------
  298. -------------------- Main Render Func --------------------
  299. ----------------------------------------------------------
  300.  
  301. local function render( render_func, update_func, data_struct, update_freq, draw_time )
  302.     local timers = initTimers( update_freq )
  303.     local theTime = textutils.formatTime(os.time(), false)
  304.    
  305.     while true do
  306.         local event = { os.pullEventRaw() }
  307.         clear()
  308.         if event[1] == "key" or event[1] == "mouse_click" or event[1] == "mouse_scroll" or event[1] == "mouse_drag" then
  309.             timers = nil
  310.             return
  311.         elseif event[1] == "timer" then
  312.             if event[2] == timers["update"] then
  313.                 update_func( data_struct )
  314.                 timers["update"] = os.startTimer( update_freq )
  315.             elseif event[2] == timers["time"] then
  316.                 theTime = textutils.formatTime(os.time(), false)
  317.                 timers["time"] = os.startTimer(0.4)
  318.             end
  319.         end
  320.        
  321.         render_func( data_struct )
  322.  
  323.         term.setBackgroundColor(colors.white)
  324.         term.setTextColor(colors.black)
  325.        
  326.         if draw_time then
  327.             term.setCursorPos(w - theTime:len() - 2, h - 1)
  328.             write(theTime)
  329.         end
  330.     end
  331. end
  332.  
  333. ----------------------------------------------------------
  334. ---------------- Render Modes Table Funcs ----------------
  335. ----------------------------------------------------------
  336.  
  337. local RENDER_MODES = {
  338.     ["bouncing_ball"] = { render_ball, update_ball, data_struct_ball, 0.2, true, "Moving Ball" },
  339.     ["dots"] = { render_dots, update_dots, data_struct_dots, 0.2, true, "Random Dots" },
  340.     ["horizontal_lines"] = { render_horizontallines, update_horizontallines, data_struct_horizontallines, 2, true, "Colorful Lines (Horizontal)" },
  341.     ["vertical_lines"] = { render_verticallines, update_verticallines, data_struct_verticallines, 2, true, "Colorful Lines (Vertical)" },
  342.     ["the-creator"] = { render_tobit, update_tobit, data_struct_tobit, 0.4, false, "TheOriginalBIT ( Creator )" },
  343.     ["moving_time"] = { render_timemove, update_timemove, data_struct_timemove, 0.5, false, "Bouncing Time"},
  344. }
  345.  
  346. function getRenderModeInfo()
  347.     local retTable = {}
  348.     for k, v in pairs(RENDER_MODES) do
  349.         local t = { k, v[6] }
  350.         table.insert( retTable, t )
  351.     end
  352.    
  353.     return retTable
  354. end
  355.  
  356. function getRenderMode(key)
  357.     return RENDER_MODES[key]
  358. end
  359.  
  360. ----------------------------------------------------------
  361. ------------------ Entry Func of Program -----------------
  362. ----------------------------------------------------------
  363.  
  364. function run(render_mode, show_time)
  365.     if show_time == nil then show_time = true end
  366.  
  367.     if not colorCheck() then error( "Sorry, this API only runs on Advanced Computers", 2) end
  368.  
  369.     if type(render_mode) ~= "string" then error( "Invalid render mode supplied", 2) end
  370.  
  371.     mode_info = getRenderMode( render_mode )
  372.  
  373.     if not mode_info then error( "The render mode \""..render_mode.."\" does not exist", 2) end
  374.  
  375.     render_func, update_func, update_freq, display_time = mode_info[1], mode_info[2], mode_info[4], mode_info[5]
  376.    
  377.     data_struct = mode_info[3]()
  378.    
  379.     clear()
  380.    
  381.     render( render_func, update_func, data_struct, update_freq, ((display_time == true) and (show_time == true)))
  382. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement