/**
 * @author(s): Reconx86
 * @created: 2016.04.12
 *
 * RCX Navigation Lights:
 * A script that enables a glow and strobing particle effect
 *
 * @license:
 * You are allowed to share this script full-perm with others and even use it in your own projects.
 * But do not sell the script seperately nor as part of a script package
 */

integer strobing;
vector strobeColor = <1.0, 1.0, 1.0>;

key objKey;
string objName = "nav light";
 
integer links;
list objects;
integer objTotal;

rcxGetObjects()
{
    integer link_nr;
    for (link_nr = 0; link_nr <= links; link_nr++) {
        string linkName = llToLower(llGetLinkName(link_nr));
        if (~llSubStringIndex(linkName, objName)) {
            objects += [link_nr];
        }
    }
    objTotal = llGetListLength(objects);
}

rcxUnsetParticles()
{
    integer i;
    for (i = 0; i < objTotal; i++) {
        integer link = llList2Integer(objects, i);
        llLinkParticleSystem(link, []);
    }
}

default
{
    state_entry()
    {
        llSetMemoryLimit(0x3000);
        links = llGetObjectPrimCount(llGetKey());
        rcxGetObjects();
        rcxUnsetParticles();
    }
    
    changed(integer change)
    {
        if (change & CHANGED_LINK) {
            if (links != llGetObjectPrimCount(llGetKey())) {
                llResetScript();
            }
        }
    }
    
    link_message(integer sender, integer num, string msg, key id)
    {
        if (msg == "#nav") {
            integer i;
            for (i = 0; i < objTotal; i++) {
                integer link = llList2Integer(objects, i);
                llSetLinkPrimitiveParamsFast(link, [PRIM_GLOW, ALL_SIDES, num]);
            }
            
        } else if (msg == "#strobe") {
            if (strobing = num) {
                integer i;
                for (i = 0; i < objTotal; i++) {
                    integer link = llList2Integer(objects, i);
                    llLinkParticleSystem(link, [
                        PSYS_PART_FLAGS, 0
                            | PSYS_PART_INTERP_COLOR_MASK
                            | PSYS_PART_INTERP_SCALE_MASK
                            | PSYS_PART_EMISSIVE_MASK,
                        PSYS_PART_MAX_AGE, 0.1,
                        PSYS_PART_START_COLOR, strobeColor,
                        PSYS_PART_END_COLOR, strobeColor,
                        PSYS_PART_START_SCALE, <2.0, 2.0, FALSE>,
                        PSYS_PART_END_SCALE, <0.01, 0.01, FALSE>,
                        PSYS_SRC_PATTERN, 1,
                        PSYS_SRC_BURST_RATE, 1.0,
                        PSYS_SRC_BURST_PART_COUNT, 1,
                        PSYS_SRC_TARGET_KEY, llGetKey(),
                        PSYS_SRC_TEXTURE, "-",
                        PSYS_SRC_MAX_AGE, 0.0,
                        PSYS_PART_START_ALPHA, 1.0,
                        PSYS_PART_END_ALPHA, 1.0
                    ]);
                }
            } else {
                rcxUnsetParticles();
            }
        }
    }
}