User:Allen Kerensky/Myriad Lite/Myriad Lite Module Meter-v0.0.3-20120827.lsl

From OpenSimulator

Jump to: navigation, search

Myriad_Lite_Module_Meter-v0.0.3-20120827.lsl

// Myriad_Lite_Module_Meter-v0.0.3-20120827.lsl
// Copyright (c) 2012 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/
 
string  VERSION = "0.0.3"; // version number
string  VERDATE = "20120827"; // version date
 
// RUNTIME GLOBALS - CAN CHANGE DURING RUN
integer FLAG_DEBUG; // see debug messages?
key PLAYERID = NULL_KEY; // cached player UUID
string PLAYERNAME = ""; // cached player name
string NAME = ""; // character name
string SPECIES = ""; // character species
string BACKGROUND = ""; // character childhood history
string CAREER = ""; // character career or faction
list RESILIENCES = [];
list CURRENT_RESILIENCES = [];
 
integer CHANATTACH; // dynamic channel for attachments
 
integer METERWORN; // using meter?
 
integer CHANMYRIAD = -999;
// Module to Module Messaging Constants
// integer MODULE_HUD = -1;
// integer MODULE_CHARSHEET = -2;
// integer MODULE_ARMOR = -3;
// integer MODULE_BAM = -4;
// integer MODULE_RUMORS = -5;
// integer MODULE_CLOSE = -6;
// integer MODULE_RANGED = -7;
// integer MODULE_RESILIENCE = -8;
// integer MODULE_PROGRESS = -9;
// integer MODULE_WELL = -10;
integer MODULE_METER = -11;
integer LM_SENDTOATTACHMENT = 0x80000000;
 
string  DIV = "|";
integer FLAG_INCAPACITATED; // incapacitated by wounds?
integer FLAG_DEAD; // killed by critical wounds?
integer MINRESILIENCE = 0; // min value for resilience
integer MAXRESILIENCE = 20; // max value for resilience
 
//============================================================================
// DEBUG - show debug chat with wearer name for sorting
//============================================================================
DEBUG(string dmessage) {
    if ( FLAG_DEBUG == TRUE ) { // are we debugging?
        llSay(DEBUG_CHANNEL,"("+llKey2Name(PLAYERID)+") MOD METER: "+dmessage);
    }
}
 
//============================================================================
// ERROR - show errors on debug channel with wearer name for sorting
//============================================================================
ERROR(string emessage) {
    llSay(DEBUG_CHANNEL,"ERROR ("+llKey2Name(PLAYERID)+") MOD METER: "+emessage);
}
 
//============================================================================
// GET_MAX_RESILIENCE
//============================================================================
integer GET_MAX_RESILIENCE(string name) {
    integer pos = llListFindList(RESILIENCES,[name]);
    if ( pos >= 0 ) {
        return llList2Integer(RESILIENCES,pos + 1);
    }
    return 0;
}
 
//============================================================================
// GET_RESILIENCE
//============================================================================
integer GET_RESILIENCE(string name) {
    integer pos = llListFindList(CURRENT_RESILIENCES,[name]);
    if ( pos >= 0 ) {
        return llList2Integer(CURRENT_RESILIENCES,pos + 1);
    }
    return 0;
}
 
//============================================================================
// GETVERSION
//============================================================================
GETVERSION() {
    SENDTOHUD("VERSION="+VERSION+DIV+"VERSIONDATE="+VERDATE+DIV+llGetObjectName());
}
 
//============================================================================
// METER - update a hovertext health meter or HUD bar graph
//============================================================================
METER() {
    //if ( METERWORN == FALSE ) return;
    integer curwounds = GET_RESILIENCE("Wounds");
    integer maxwounds = GET_MAX_RESILIENCE("Wounds");
    integer curcritical = GET_RESILIENCE("Critical");
    integer maxcritical = GET_MAX_RESILIENCE("Critical");
    // create a meter message packet
    string message = "METER"+DIV+PLAYERNAME+DIV+NAME+DIV+(string)curwounds+DIV+(string)maxwounds+DIV+(string)curcritical+DIV+(string)maxcritical+DIV+(string)FLAG_DEAD+DIV+(string)FLAG_INCAPACITATED+DIV+SPECIES+DIV+BACKGROUND+DIV+CAREER;
    llRegionSay(CHANMYRIAD,message); // send the update to region for scorekeepers, etc
    llWhisper(CHANATTACH,message); // whisper to the wearer's actual meter
    llMessageLinked(LINK_THIS,MODULE_METER,message,llGetOwner()); // send meter updates to bus
    DEBUG("Wounds: "+(string)curwounds+" of "+(string)maxwounds+" wound boxes. Critical: "+(string)curcritical+" of "+(string)maxcritical+" critical wound boxes.");
}
 
//============================================================================
// RESET
//============================================================================
RESET() {
    llResetScript(); // now reset
}
 
//============================================================================
// SENDTOHUD - send reponses to HUD as Link Messages
//============================================================================
SENDTOHUD(string str) {
    DEBUG("SENDTOHUD("+str+")");
    llMessageLinked(LINK_THIS,LM_SENDTOATTACHMENT,str,PLAYERID);    
}
 
// SET_RESILIENCE
SET_RESILIENCE(string name,integer value) {
    if ( value < MINRESILIENCE || value > MAXRESILIENCE ) {
        ERROR("Resilience rank "+(string)name+" out of allowed range "+(string)MINRESILIENCE+"-"+(string)MAXRESILIENCE);
         return;
    } // out of range
    integer curpos = llListFindList(CURRENT_RESILIENCES,[name]);
    integer curval;
    integer maxval;
    if ( curpos >= 0 ) {
        curval = llList2Integer(CURRENT_RESILIENCES,curpos + 1);
    } else { // resilience not found
        CURRENT_RESILIENCES = [name,value] + CURRENT_RESILIENCES;
        RESILIENCES = [name,value] + RESILIENCES;
        return;
    }
    integer maxpos = llListFindList(RESILIENCES,[name]);
    if ( maxpos >=0 ) {
        maxval = llList2Integer(RESILIENCES,maxpos + 1);
    }
    if ( value <= maxval) {
        CURRENT_RESILIENCES = llListReplaceList(CURRENT_RESILIENCES,[value],curpos + 1, curpos + 1);
    }
}
 
//============================================================================
// SETUP - begin bringing the HUD online
//============================================================================
SETUP() {
    FLAG_DEBUG = FALSE;
    PLAYERID = llGetOwner(); // remember the owner's UUID
    PLAYERNAME = llKey2Name(PLAYERID); // remember the owner's legacy name
    CHANATTACH = (integer)("0x"+llGetSubString((string)PLAYERID,1,7)); // attachment-specific channel    
}
 
//============================================================================
// DEFAULT STATE - load character sheet
//============================================================================
default {
 
    // LINK MESSAGE - commands to and from other prims in HUD
    link_message(integer sender,integer sending_module,string str, key id) {
        if ( sending_module == MODULE_METER || sending_module == LM_SENDTOATTACHMENT ) return; // ignore our own link messages
        DEBUG("EVENT: link_message("+(string)sender+","+(string)sending_module+","+str+","+(string)id+")");
 
        list fields = llParseString2List(str,[DIV],[]); // break into list of fields based on DIVider
        string command = llToLower(llStringTrim(llList2String(fields,0),STRING_TRIM)); // assume the first field is a Myriad Lite command
        string data = llStringTrim(llList2String(fields,1),STRING_TRIM); // field one is the data
        list subfields = llParseString2List(data,["="],[]); // break data field into comma-delimited subfields if needed
 
        // General Myriad Module Commnads
        if ( command == "debugoff" ) { FLAG_DEBUG = FALSE; return;} // disable debugging
        if ( command == "debugon" ) { FLAG_DEBUG = TRUE; return;} // enable debugging
        if ( command == "reset" ) { RESET(); return;} // reset when told
        if ( command == "version" ) { GETVERSION(); return;} // get version when needed
 
        if ( command == "set_name" ) {
            NAME = llList2String(subfields,1); // set the name
            return;
        }
        if ( command == "set_species" ) {
            SPECIES = llList2String(subfields,1); // set the species;
            return;
        }
        if ( command == "set_background" ) {
            BACKGROUND = llList2String(subfields,1); // set the species;
            return;
        }
        if ( command == "set_career" ) {
            CAREER = llList2String(subfields,1); // set the species;
            return;
        }
        if ( command == "set_resilience" ) {
            string resname = llList2String(subfields,0); // find the boon name
            integer resrank = llList2Integer(subfields,1); // find the boon rank value
            SET_RESILIENCE(resname,resrank);
 
            // TODO how to verify resilience names are valid?
            //if ( resrank >= MINRESILIENCE && resrank <= MAXRESILIENCE ) { // rank valid?
            //    RESILIENCES = [resname,resrank] + RESILIENCES; // add resilience to list
            //    CURRENT_RESILIENCES = [resname,resrank] + CURRENT_RESILIENCES; // add to current list too
            //} else { // invalid, report it
            //    ERROR("RESILIENCE "+resname+" rank "+(string)resrank+" value out of allowed range: "+(string)MINRESILIENCE+"-"+(string)MAXRESILIENCE);
            //}
            return;
        }
        if ( command == "character_loaded" ) {
            METER();
            return; // we're out of notecard, so character sheet is loaded - start playing
        }
        if ( command == "attachmeter" ) {
            METERWORN = TRUE; // we need to send meter events
            METER(); // send update
            return;
        }
        if ( command == "detachmeter" ) { METERWORN = FALSE; return; }
        if ( command == "meter" ) { METER(); return; }
        if ( command == "incapacitated" ) { FLAG_INCAPACITATED = TRUE; return; }
        if ( command == "dead" ) { FLAG_DEAD = TRUE; return; }
        if ( command == "alive" ) { FLAG_DEAD = FALSE; return; }
        if ( command == "revived" ) { FLAG_INCAPACITATED = FALSE; return; }
    }
 
    // STATE ENTRY
    state_entry() {
        SETUP();        
    }
} // end default state
// END
Personal tools
General
About This Wiki