/**
 * @author(s): Reconx86
 * @created: 2016.04.12
 *
 * RCX Exhaust Flame:
 * Creates a soot effect when starting engines
 *
 * @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
 */

//Afterburner Units
integer time = 3;
string objName = "soot";
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);
}

rcxStartParticles()
{
    integer i;
    for (i = 0; i < objTotal; i++) {
        integer link = llList2Integer(objects, i);
        llLinkParticleSystem(link, [
            PSYS_SRC_PATTERN, PSYS_SRC_PATTERN_ANGLE_CONE,
            PSYS_SRC_BURST_RADIUS, 0.1,
            PSYS_PART_START_COLOR, <0.0, 0.0, 0.0>,
            PSYS_PART_END_COLOR, <0.0, 0.0, 0.0>,
            PSYS_PART_START_ALPHA, 0.8,
            PSYS_PART_END_ALPHA, 0,
            PSYS_PART_START_SCALE, <1, 1, 2>,
            PSYS_PART_END_SCALE, <4, 4, 0.1>,
            PSYS_SRC_TEXTURE, "e75df406-a153-7695-0f08-bbcb844987be",
            PSYS_SRC_MAX_AGE, 1,
            PSYS_PART_MAX_AGE, time,
            PSYS_SRC_BURST_RATE, 0.01,
            PSYS_SRC_BURST_PART_COUNT, 40,
            PSYS_SRC_ACCEL, <0.0, 0.0, 0.0>, 
            PSYS_SRC_OMEGA, <0.0, 0.0, 0.0>,
            PSYS_SRC_BURST_SPEED_MIN, 4.5,
            PSYS_SRC_BURST_SPEED_MAX, 6.0,
            PSYS_PART_FLAGS, 0
                | PSYS_PART_INTERP_COLOR_MASK
                | PSYS_PART_INTERP_SCALE_MASK
                | PSYS_PART_WIND_MASK
        ]);
    }
    llSleep(time);
    rcxStopParticles();
}

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

default
{
    state_entry()
    {
        llSetMemoryLimit(0x3000);
        links = llGetObjectPrimCount(llGetKey());
        rcxGetObjects();
    }
    
    changed(integer change)
    {
        if (change & CHANGED_LINK) {
            if (links != llGetObjectPrimCount(llGetKey())) {
                llResetScript();
            }
        }
    }
    
    link_message(integer sender, integer num, string msg, key id)
    {
        if (msg == "#engine") {
            if (num) {
                rcxStartParticles();
                
            } else {
                rcxStopParticles();
            }
        }
    }
}