/**
 * @author: reconx86 (RCX Productions)
 *
 * @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.
 *
 * This script is meant to read sound files from the LCONF script and play them based on throttle data.
 * NOTE: if you see this error: Could not find sound '', your cofiguration script is not set correctly
 */

//Sound Settings
float volume = 1.0;
float startDelay = 9.0;

//Continuous engine loops
list loops = [
    "77799a90-1781-e4f3-a09a-31d9abefff26", 0,
    "1bd8936f-0774-f509-1fad-490e13644dd9", 50,
    "dc7747ee-a119-f00f-ba53-ce86715d201b", 70
];

//Start and stop engine sounds
string sndEngStart = "ad3d1008-653c-665f-7295-afb9700aa517";
string sndEngStop = "e5656532-e6da-5afd-1d44-335102b8ad46";

//Water sounds
string sndSplash = "0da6809f-c3cf-6e0c-4e77-1b58518926c2";
string sndUnsplash = "2cf64119-69e6-48e1-8d43-7e66f24f38d7";

list sndLoopThrust;
list sndLoopKeys;
integer loop_total;
integer fwd;

integer started;
string currEngLoop;
string prevSnd;
string idle;

integer lastVtype;

// Vtype constants
integer TAXI_MODE = 0;
integer PLANE_MODE = 1;
integer BOAT_MODE = 2;

start()
{
    prevSnd = "";
    llStopSound();
    idle = llList2Key(sndLoopKeys, 0);
    if (idle != "") {
        llPreloadSound(idle);
    }
    if (sndEngStart != "") {
        // This will play the sound until the end, because its not attached to the object
        llTriggerSound(sndEngStart, volume);
        prevSnd = sndEngStart;
        llSetTimerEvent(startDelay);
    } else {
        started = TRUE;
        setSndLoop(fwd);
    }
    if (sndEngStop != "") {
        llPreloadSound(sndEngStop);
    }
}

stop()
{
    llSetTimerEvent(0);
    llStopSound();
    if (sndEngStop != "") {
        llPlaySound(sndEngStop, volume);
    }
    started = FALSE;
}

setSndLoop(integer num)
{
    if (started) {
        
        integer i;
        for (i = 0; i < loop_total; i++) {
            if (num <= llList2Integer(sndLoopThrust, i)) {
                currEngLoop = llList2Key(sndLoopKeys, i);
                i = loop_total;
                
            } else if (num > llList2Integer(sndLoopThrust, -1)) {
                currEngLoop = llList2Key(sndLoopKeys, -1);
                i = loop_total;
            }
        }
        if (currEngLoop != prevSnd) {
            llLoopSound(prevSnd = currEngLoop, volume);
        }
    }
}

default
{
    state_entry()
    {
        llStopSound();
        sndLoopKeys = llList2ListStrided(loops, 0, -1, 2);
        sndLoopThrust = llList2ListStrided(llDeleteSubList(loops, 0, 0), 0, -1, 2);
        loop_total = llGetListLength(sndLoopThrust);
        
        if (sndEngStart != "") {
            llPreloadSound(sndEngStart);
        }
    }
    
    on_rez(integer start_num)
    {
        if (sndEngStart != "") {
            llPreloadSound(sndEngStart);
        }
    }

    link_message(integer sender, integer num, string msg, key id)
    {
        if (msg == "#engine") {
            if (started != num) {
                if (num) {
                    start();
                } else {
                    stop();
                }
                started = num;
            }
            
        } else if (msg == "#throttle") {
            setSndLoop(fwd = num);
            
        } else if (msg == "#vType") {
            if (num != lastVtype) {
                
                if (lastVtype == TAXI_MODE) {
                    if (num == BOAT_MODE) {
                        llTriggerSound(sndUnsplash, volume); // light splash
                    }
                    
                } else if (lastVtype == PLANE_MODE) {
                    
                    if (num == BOAT_MODE) {
                        llTriggerSound(sndSplash, volume); // light splash
                    }
                    
                }
                lastVtype = num;
            }
        }
    }
    
    timer()
    {
        llSetTimerEvent(0);
        started = TRUE;
        setSndLoop(fwd);
    }
}