drunkensouljah
3/25/2013 - 10:17 PM

Sterne Bildschirmschoner für Computercraft (Monitor muss angeschlossen sein). Original Code stars by Abdiel

Sterne Bildschirmschoner für Computercraft (Monitor muss angeschlossen sein). Original Code stars by Abdiel

-- Sternenfeld
-- Windows 98 Bildschirmschoner Emulator auf einem ComputerCraft Monitor.
--
-- Mindestens 1 Bildschirm muss mit dem Computer verbunden sein.
-- Um Bildschirmschoner zu beenden STRG + T kurz gedrueckt halten.
--
-- Arguments:
--   sterne [ANZAHL] [GESCHWINDIGKEIT] [FARBE] [ZEICHEN]
--
-- ANZAHL - Anzahl der Sterne (normal 25). 
-- GESCHWINDIGKEIT - Geschwindigkeit mit der sich die Sterne bewegen (normal 1 pixel pro tick)
-- FARBE - Farbe der Sterne (nur Advanced Monitor).
--         (see http://computercraft.info/wiki/Colors_(API) )
-- ZEICHEN  - Zeichen aus dem die Sterne bestehen sollen. (normal "." Punkt)
--
-- Beispiel bei einem 3x3 Monitor:
--   sterne 10 0.3 red |-o-|
--
-- Basiert auf dem Script stars von Abdiel und wurde unter der WTFPL 2 veroeffentlicht.
--
-- This work is free. You can redistribute it and/or modify it under the
-- terms of the Do What The Fuck You Want To Public License, Version 2,
-- as published by Sam Hocevar. See http://www.wtfpl.net/about/ for more details.

local args = {...}

-- Argument parsing
-- Number of stars.
local n = tonumber(args[1])
if not n then n = 25 end

-- Speed of stars (pixels/tick).
local speed = tonumber(args[2])
if not speed then speed = 1 end

-- Color of stars. Can be any member of the colors table.
local col = colors[args[3]]
if not col then col = colors.white end

-- Character to draw stars with.
local stCh = args[4]
if not stCh then stCh = "." end
local clr = string.rep(" ", string.len(stCh))

-- Find a monitor
local m
for i,s in ipairs(redstone.getSides()) do
  m = peripheral.wrap(s)
  if m then break end
end

if not m then
  print("To run this program, connect the computer to a monitor.")
  return false
end

-- Set color (only if we have an advanced monitor)
if m.isColor() then
  m.setTextColor(col)
  m.setBackgroundColor(colors.black)
end
m.setTextScale(0.5)
m.clear()

-- width, height of the monitor
local w,h = m.getSize()

-- x and y coordinates of the monitor's center
local cx, cy = w/2, h/2

local stars = {}

-- Generates k new stars in the middle of the screen.
-- Position of a star in polar coordinates: r,phi.
function genStars(k)
  for i = 1,k do
    table.insert(stars, {0, math.random() * 2 * math.pi})
  end
end

-- Returns the position of a star as x,y values.
function getPos(star)
  local x = math.sin(star[2]) * star[1]
  local y = math.cos(star[2]) * star[1]
  return cx + x, cy + y
end
  
-- Main program
genStars(n)

while true do
  local toRemove = {}
  
  for i = 1,#stars do
    -- Remove star from display
    local x,y = getPos(stars[i])
    m.setCursorPos(x,y)
    m.write(clr)
    
    -- Calculate new position of the star.
    stars[i][1] = stars[i][1] + speed
    x,y = getPos(stars[i])
    
    if x<0 or x>w or y<0 or y>h then
      -- Remove star.
      table.insert(toRemove, i)
    else
      -- Draw star.
      m.setCursorPos(x,y)
      m.write(stCh)
    end
  end
  
  -- Remove stars which fell off the screen.
  for j = #toRemove, 1, -1 do
    table.remove(stars, toRemove[j])
  end
  
  -- Generate new stars.
  genStars(#toRemove)
  
  sleep(0.05)
end