Light Switch

Written by Kitsune
integer lightMasterChannel = 4874320;
integer channel = 4445566;
integer gLightOn = 0;
vector onCol = <1.0, 1.0, 0.5>;
vector offCol = <0.2, 0.2, 0.2>;

turnOn()
{
        llSetColor(onCol, ALL_SIDES);
        gLightOn = 1;
        llWhisper(channel, "on");
}

turnOff()
{
        llSetColor(offCol, ALL_SIDES);
        gLightOn = 0;
        llWhisper(channel, "off");    
}

flicker()
{
    integer x;
    for (x = 0; x < 5; x++)
    {
        if (gLightOn) turnOff();
        else turnOn();
        llSleep(llFrand(2));
    }
}
        

default
{
    state_entry()
    {
        llListen(lightMasterChannel, "", "", "");
        llPassTouches(TRUE);
    }

    touch_end(integer total_number)
    {
        if (gLightOn == 0)
        {
            turnOn();
        }
        else
        {
            turnOff();
        }
    }
    
    listen(integer channel, string who, key id, string msg)
    {
        if (msg == "on")
        {
            llSleep(llFrand(8));
            flicker();
            turnOn();
        }
        else if (msg == "off")
        {
            llSleep(llFrand(8));
            flicker();
            turnOff();
        }
    }

}