Myriad Lite Module Rumors

// Myriad_Lite_Module_Rumors-v0.0.3-20131026.lsl 
// Copyright (c) 2013 by Allen Kerensky (OSG/SL) All Rights Reserved.
// This work is dual-licensed under
// Creative Commons Attribution (CC BY) 3.0 Unported
// http://creativecommons.org/licenses/by/3.0/
// - or -
// Modified BSD License (3-clause)
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// * Neither the name of Myriad Lite nor the names of its contributors may be
//   used to endorse or promote products derived from this software without
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
// NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The Myriad RPG System was designed, written, and illustrated by Ashok Desai
// Myriad RPG System licensed under:
// Creative Commons Attribution (CC BY) 2.0 UK: England and Wales
// http://creativecommons.org/licenses/by/2.0/uk/
 
// FIXME - add RPEVENT
// FIXME - add HELP
 
// CONSTANTS - DO NOT CHANGE DURING RUN
string BASENAME = "Myriad Lite Module Rumors";
string VERSION = "0.0.3"; // Allen Kerensky's script version
string VERSIONDATE = "20131026"; // Allen Kerensky's script yyyymmdd
 
// Module to Module Messaging Constants
integer MODULE_RUMORS = -5; // Which Module Link Message number do I listen for?
integer LM_SENDTOATTACHMENT = 0x80000000;
 
integer RENDEZVOUS1 = -999; // regionwide rendezvous channel
 
// RUNTIME GLOBALS - CAN CHANGE DURING RUN
integer CHANOBJ; // channel of thing we're talking to
string RUMOR; // the current rumor we've heard
 
//
// PPMA GET_VAL
//
string KEY_NOT_FOUND = "[KEY_NOT_FOUND]";
string GET_VAL(string dbkey,string field) {
    //OWNERSAY("GET_VAL KEY=["+dbkey+"] FIELD=["+field+"]");
    string out = KEY_NOT_FOUND;
    integer i;
    string name;
    string desc;
    for ( i = 2; i <= llGetNumberOfPrims(); i++ ) {
        name = llList2String(llGetLinkPrimitiveParams(i,[PRIM_NAME]),0);
        desc = llList2String(llGetLinkPrimitiveParams(i,[PRIM_DESC]),0);
        if ( llToLower(name) == llToLower(dbkey) && llToLower(desc) == llToLower(field) ) {
            out = llList2String(llGetLinkPrimitiveParams(i,[PRIM_TEXT]),0);
            //DEBUG("GET_VAL RETURN=["+out+"]");
            return out; // bail on first match?
        }
    }
    //OWNERSAY("GET_VAL RETURN=["+out+"]");
    return out;
}
 
//SET_VAL(string dbkey, string field, string val) {
//    //OWNERSAY("SET_VAL KEY=["+dbkey+"] FIELD=["+field+"] VAL=["+val+"]");
//    integer i;
//    string name;
//    string desc;
//    integer written = FALSE;
//    for ( i = 2; i <= llGetNumberOfPrims(); i++ ) {
//        name = llStringTrim(llList2String(llGetLinkPrimitiveParams(i,[PRIM_NAME]),0),STRING_TRIM);
//        desc = llStringTrim(llList2String(llGetLinkPrimitiveParams(i,[PRIM_DESC]),0),STRING_TRIM);
//        if ( ( llToLower(name) == llToLower(dbkey) && llToLower(desc) == llToLower(field) ) && written == FALSE ) {
//            //OWNERSAY("SET_VAL UPDATE RECORD=["+(string)i+"] DBKEY=["+dbkey+"] DESC=["+field+"] VAL=["+val+"]");
//            llSetLinkPrimitiveParamsFast(i,[PRIM_NAME,dbkey,PRIM_DESC,field,PRIM_TEXT,val,ZERO_VECTOR,0.0]);
//            written = TRUE; // we did an update, remember it
//        }    
//    }
//    if ( written == TRUE ) {
//        //OWNERSAY("SET_VAL UPDATE COMPLETE.");
//        return;
//    }
//    // data hasn't been written, scan for free prim
//    for ( i = 2; i <= llGetNumberOfPrims(); i++ ) {
//        name = llStringTrim(llList2String(llGetLinkPrimitiveParams(i,[PRIM_NAME]),0),STRING_TRIM);
//        desc = llStringTrim(llList2String(llGetLinkPrimitiveParams(i,[PRIM_DESC]),0),STRING_TRIM);
//        if ( ( name == "" && desc == "" ) && written == FALSE ) {
//            //OWNERSAY("SET_VAL INSERT RECORD=["+(string)i+"] DBKEY=["+dbkey+"] DESC=["+field+"] VAL=["+val+"]");
//            llSetLinkPrimitiveParamsFast(i,[PRIM_NAME,dbkey,PRIM_DESC,field,PRIM_TEXT,val,ZERO_VECTOR,0.0]);
//            written = TRUE; // we did an update, remember it
//        }
//    }
//    if ( written == TRUE ) {
//        //OWNERSAY("SET_VAL INSERT COMPLETED.");
//        return;
//    }
//    //OWNERSAY("SET_VAL NO FREE RECORD FOUND TO INSERT INTO! DATA LOST");
//}
 
//////////////////////////////////////////////////////////////////////////////
// FLAG FUNCTIONS
// INCAPACITATED - lost wounds
// DEAD - lost critical wounds
// DEBUG - show debug messages or not
// IDEA: INDECISION - lost resolve?
 
// LMIM: SET_FLAG|<name>=<TRUE|FALSE>
// PPMA: FLAG,<flagname>,<TRUE|FALSE>
// LMOUT: FLAG|<flagname>=<TRUE|FALSE>
// LMERR: FIXME
integer GET_FLAG(string flag) {
    string val = GET_VAL("FLAG",llToUpper(flag));
    if ( val == KEY_NOT_FOUND ) {
        ERROR("Flag ["+flag+"] does not exist.");
    }
    if ( val != "0" && val != "1") {
        ERROR("Flag: "+flag+" invalid value ["+val+"]");
    }
    integer bool = (integer)val;
    return bool;
}
 
// SET_FLAG
// LMIM: SET_FLAG|<flagname>=<TRUE|FALSE>
// PPMA: FLAG,<flagname>,<TRUE|FALSE>
// LMOUT: SET_FLAG|<flagname>=<TRUE|FALSE>
// LMERR: FIXME
//SET_FLAG(string flag,integer value) {
//    if ( flag == "" ) return; // FIXME add error
//    if ( value != TRUE && value != FALSE ) return; // FIXME add err
//    SET_VAL("FLAG",llToUpper(flag),(string)value);
//}
 
// DEBUG
DEBUG(string debugmsg) {
    if ( GET_FLAG("DEBUG") == TRUE ) llMessageLinked(LINK_THIS,MODULE_RUMORS,"DEBUG|"+debugmsg,llGetOwner());
}
 
//
// ERROR
//
ERROR(string msg) {
    llMessageLinked(LINK_THIS,MODULE_RUMORS,"ERROR|"+msg,llGetOwner());
}
 
// MEMORY
GET_MEMORY() {
    OWNERSAY(BASENAME+" free memory: "+(string)llGetFreeMemory()); // show this module's free memory info
}
 
// GETVERSION
GET_VERSION() {
    OWNERSAY(BASENAME+" v"+VERSION+"-"+VERSIONDATE); // show this module's version info
}
 
//
// OWNERSAY
//
OWNERSAY(string msg) {
    llMessageLinked(LINK_THIS,MODULE_RUMORS,"OWNERSAY|"+msg,llGetOwner());
}
 
// RESET - shut down running animations then reset the script to reload character sheet
RESET() {
    // do any pre-shutddown cleanup first
    llResetScript(); // now reset
}
 
// SETUP - begin bringing the HUD online
SETUP() {
    llRegionSay(RENDEZVOUS1,"RUMOR_SERVER_FIND");
    OWNERSAY("Rumor module active.");
}
 
// DEFAULT STATE - load character sheet
default {
 
    // LINK MESSAGE - commands to and from other prims in HUD
    link_message(integer sender,integer sending_module,string message, key speakerid) {
        if ( sending_module == MODULE_RUMORS || sending_module == LM_SENDTOATTACHMENT ) return; // ignore our own link messages
 
        // break down the commands and messages into units we can work with
        list fields = llParseString2List(message,["|"],[]); // break into list of fields based on "|"ider
        string command = llToUpper(llList2String(fields,0)); // assume the first field is a Myriad Lite command
 
        if ( command == "DEBUG" || command == "ERROR" || command == "HELP" || command == "OWNERSAY" || command == "REPEVENT" ) return; // ignore WELL commands
 
 
        //string data1   = llList2String(fields,1);
        //string data2   = llList2String(fields,2);
        //string data3   = llList2String(fields,3);
 
        // General Myriad Module Commnads
        if ( command == "MEMORY" ) { GET_MEMORY(); return;} // get memory info
        if ( command == "RESET" ) { RESET(); return;} // reset when told
        if ( command == "VERSION" ) { GET_VERSION(); return;} // get version when needed
 
        // only show link message debugs for module-specific stuff...
        DEBUG("EVENT: link_message("+(string)sender+","+(string)sending_module+","+message+","+(string)speakerid+")");
 
        if ( command == "RUMOR_SERVER_FIND" ) {
            llRegionSay(RENDEZVOUS1,"RUMOR_FIND_SERVER");
            return;
        }
        if ( command == "RUMOR_SERVER_FOUND" ) {
            // calculate dynamic channel of item/player talking to us
            CHANOBJ = (integer)("0x" + llGetSubString((string)speakerid,0,6));
            OWNERSAY("This region supports Myriad Lite rumors.");
            return;
        }
        if ( command == "RUMOR_GET" ) { // does player want to get a rumor from system?
            llRegionSay(CHANOBJ,"RUMOR_GET|"+(string)llGetKey());
            return;
        }
        if ( command == "RUMOR_SHOW" ) { // incoming rumor from the system
            RUMOR = "";
            RUMOR = llGetSubString(message,llSubStringIndex(message,"|"),-1);
            OWNERSAY("You hear a rumor, \""+RUMOR+"\"");
            return;
        }
        if ( command == "RUMOR_MAIN" ) { // ask for rumor main menu (admins only)
            llRegionSay(CHANOBJ,"RUMOR_MAIN" );
            return;
        }
        if ( command == "RUMOR_MODERATE" ) { // ask for first rumor to moderate (admins only)
            llRegionSay(CHANOBJ,"RUMOR_MODERATE" );
            return;
        }
        if ( command == "RUMOR_YES" ) { // approve rumor (for admins only)
            llRegionSay(CHANOBJ,"RUMOR_LIST" );
            return;
        }
        if ( command == "RUMOR_LIST" ) { // ask for list of all rumors in system (for admins only)
            llRegionSay(CHANOBJ,"RUMOR_LIST" );
            return;
        }
        if ( command == "RUMOR_PENDING" ) { // ask for next pending rumor
            llRegionSay(CHANOBJ,"RUMOR_PENDING");
            return;
        }
        if ( command == "RUMOR_NONE_PENDING" ) { // none left
            OWNERSAY("No rumors pending");
        }
        if ( command == "RUMOR_APPROVE" ) { // approve a pending rumor (for admins only)
            llRegionSay(CHANOBJ,"RUMOR_APPROVE");
            return;
        }
        if ( command == "RUMOR_APPROVED" ) { // confirmed
            OWNERSAY("Rumor approved.");
            return;
        }
        if ( command == "RUMOR_REMOVE" ) { // remove a pending
            llRegionSay(CHANOBJ,"RUMOR_REMOVE");
            return;
        }
        if ( command == "RUMOR_REMOVED" ) { // confirmed
            OWNERSAY("Rumor Removed");
            return;
        }
        if ( command == "RUMOR_PUT" ) { // does player want to put a rumor into system?
            llRegionSay(CHANOBJ,"RUMOR_PUT|"+llKey2Name(llGetOwner())+"|"+llList2String(fields,1));
            return;
        }
    } // end listen
 
    // STATE ENTRY - called on Reset
    state_entry() {
        SETUP(); // show credits and start character sheet load
    }
} // end state running
// END