pgs_tracker.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2011 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
integer _brownMoths = 0;
integer _blackMoths = 0;
integer _genomeBB = 0;
integer _genomeBb = 0;
integer _genomebB = 0;
integer _genomebb = 0;
integer _simTime = 0;
integer _generation = 0;
//list activeMoths = [];
string _lastDeath = "N/A";
string _lastBirth = "N/A";
 
key _owner = NULL_KEY;
float _current_p = 0;
float _current_q = 0;
 
string mode = "simple";
 
key nQuery = NULL_KEY;
integer nLine = 0;
list aList = [];
 
default {
    state_entry() {
        integer itra = llGetInventoryNumber(INVENTORY_NOTECARD)-1;
        do {
            if(llGetInventoryName(INVENTORY_NOTECARD, itra) == "Access List")
            jump found_notecard;
        } while(--itra>0);
        llOwnerSay("Failed to find notecard.");
        return;
@found_notecard;
        nQuery = llGetNotecardLine("Access List", nLine);
    }
    dataserver(key id, string data) {
        if(id != nQuery) return;
        if(data == EOF) {
            state stats;
        }
        if(data == "") jump next_line;
        aList += data;
@next_line;
        nQuery = llGetNotecardLine("Access List", ++nLine);
    }
}
 
state stats
{
    state_entry()
    {
        llPassTouches(FALSE);
        _owner = llGetOwner();
        _brownMoths = 0;
        _blackMoths = 0;
        _simTime = 0;
        _lastDeath = "N/A";
        _lastBirth = "N/A";
        //activeMoths = [];
        llSetText("", <0,0,0>, 0.0);
        //llSetAlpha(0.0, ALL_SIDES);
        llSetPrimitiveParams([PRIM_GLOW, ALL_SIDES, 0.00]);
        integer comChannel = ((integer)("0x"+llGetSubString((string)llGetOwner(),-8,-1)) & 0x3FFFFFFF) ^ 0xBFFFFFFF;
        llListen(comChannel+3, "", "[K] Brown Moth", "");
        llListen(comChannel+3, "", "[K] Black Moth", "");
    } 
 
    listen(integer channel, string name, key id, string message)
    {
        list opt = llParseString2List(message, [",", ":"], []);
        integer itra = llGetListLength(opt)-1;
        do {
            if(llList2String(opt, itra) == "PGS_START") {
                llSetAlpha(1.0, ALL_SIDES);
                llSetPrimitiveParams([PRIM_GLOW, ALL_SIDES, 0.25]);
                llSetTimerEvent(1);
                jump next_msg;
            }
            if(llList2String(opt, itra) == "PGS_STOP") {
                llSetTimerEvent(0);
                llResetScript();
                return;
            }
            if(llList2String(opt, itra) == "PGS_PAUSE") {
                llSetTimerEvent(0);
                jump next_msg;
            }
            if(llList2String(opt, itra) == "PGS_UNPAUSE") {
                llSetTimerEvent(1);
                jump next_msg;
            }
            if(llList2String(opt, itra) == "BLK_DEATH") {
                integer itrb = llGetListLength(opt)-1;
                do {
                    if(llList2String(opt, itrb) == "GENOME") {
                        if(llList2String(opt, itrb+1) == "BB") {
                            --_genomeBB;
                            _lastDeath = "BB";
                            jump bwn_death_next;
                        }
                        if(llList2String(opt, itrb+1) == "Bb") {
                            --_genomeBb;
                            _lastDeath = "Bb";
                            jump bwn_death_next;
                        }
                        if(llList2String(opt, itrb+1) == "bB") {
                            --_genomebB;
                            _lastDeath = "bB";
                            jump bwn_death_next;
                        }
                    }
@bwn_death_next;
                } while(--itrb>=0);
                --_blackMoths;
                jump next_msg;
            }
            if(llList2String(opt, itra) == "BWN_DEATH") {
                integer itrb = llGetListLength(opt)-1;
                do {
                    if(llList2String(opt, itrb) == "GENOME" && llList2String(opt, itrb+1) == "bb") {
                        --_genomebb;
                        _lastDeath = "bb";
                        jump blk_death_next;
                    }
@blk_death_next;
                } while(--itrb>=0);    
                --_brownMoths;
                jump next_msg;
            }
@next_msg;
        } while(--itra>=0);
    }
 
    link_message(integer sender_num, integer num, string str, key id)
    {
        list opt = llParseString2List(str, [",", ":"], []);
        integer itra = llGetListLength(opt)-1;
        do {
            if(llList2String(opt, itra) == "CURRENT_P") {
                _current_p = llList2Float(opt, itra+1);
                jump next_msg;
            }
            if(llList2String(opt, itra) == "CURRENT_Q") {
                _current_q = llList2Float(opt, itra+1);
                jump next_msg;
            }
            if(llList2String(opt,itra) == "BLK_BIRTH") {
                _generation++;
                integer itrb = llGetListLength(opt)-1;
                do {
                    if(llList2String(opt, itrb) == "GENOME") {
                        if(llList2String(opt, itrb+1) == "BB") {
                            ++_genomeBB;
                            _lastBirth = "BB";
                            jump bwn_regen_next;
                        }
                        if(llList2String(opt, itrb+1) == "Bb") {
                            ++_genomeBb;
                            _lastBirth = "Bb";
                            jump bwn_regen_next;
                        }
                        if(llList2String(opt, itrb+1) == "bB") {
                            ++_genomebB;
                            _lastBirth = "bB";
                            jump bwn_regen_next;
                        }
                    }
@bwn_regen_next;
                } while(--itrb>=0);
                ++_blackMoths;
                jump next_msg;
            }
            if(llList2String(opt, itra) == "BWN_BIRTH") {
                _generation++;
                integer itrb = llGetListLength(opt)-1;
                do {
                    if(llList2String(opt, itrb) == "GENOME" && llList2String(opt, itrb+1) == "bb") {
                        ++_genomebb;
                        _lastBirth = "bb";
                        jump blk_regent_next;
                    }
@blk_regent_next;
                } while(--itrb>=0);
                ++_brownMoths;
                jump next_msg;
            }
@next_msg;
        } while(--itra>=0);
 
    }
 
    timer() {
        ++_simTime;
        integer _simTimeHours = _simTime/3600;
        integer _simTimeMinutes = (_simTime % 3600) / 60;
        integer _simTimeSeconds = (_simTime % 3600) % 60;
        if(mode == "simple") {
            llSetText("Elapsed time: " + (string)_simTimeHours + "h" + (string)_simTimeMinutes + "m" + (string)_simTimeSeconds + "s" + "\nTotal moths: " + (string)(_brownMoths + _blackMoths) + "\nLast death: " + _lastDeath + "\nLast birth: " + _lastBirth, <1, 1, 0>, 1.0);
            return;
        }
        llSetText("Elapsed time: " + (string)_simTimeHours + "h" + (string)_simTimeMinutes + "m" + (string)_simTimeSeconds + "s" + "\nBrown moths: " + (string)_brownMoths + "/" + (string)(_brownMoths + _blackMoths) + "\nBlack moths: " + (string)_blackMoths + "/" + (string)(_brownMoths + _blackMoths) + "\nGenotype Count:  [ BB->" + (string)_genomeBB + ", Bb->" + (string)(_genomeBb+_genomebB) /* + ", bB->" + (string)_genomebB */ + ", bb->" + (string)_genomebb + " ]" + "\nq (brown allele): " + (string)_current_q + "\np (black allele): " + (string)_current_p + "\nLast death: " + _lastDeath + "\nLast birth: " + _lastBirth + "\nGeneration: " + (string)_generation, <1, 1, 0>, 1.0);
 
    }
 
    touch_start(integer num) {
/*        key av = llDetectedKey(0);
        integer itra=llGetListLength(aList)-1;
        do {
            if(llList2Key(aList, itra) == av) jump found_user;
        } while(--itra>-1);
        return;
@found_user; */
        if(mode == "simple") {
            mode = "advanced";
            jump modeset;
        }
        if(mode == "advanced") {
            mode = "simple";
            jump modeset;
        }
@modeset;
        llShout(0, "Mode is now: " + mode + ".");
    }
 
    changed(integer change) {
        if(change & (CHANGED_OWNER | CHANGED_INVENTORY)) {
            _owner = llGetOwner();
            llResetScript();
        }
    }
    on_rez(integer num) {
        _owner = llGetOwner();
        llResetScript();
    }
}