User:Allen Kerensky/Myriad Lite/Myriad Lite Module Meter-Preview6.lsl

From OpenSimulator

Jump to: navigation, search

Myriad_Lite_Module_Meter-Preview6.lsl

// Myriad_Lite_Module_Meter-Preview6.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.0"; // version number
string  VERDATE = "20120810"; // 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 = 0; // dynamic channel for attachments
 
integer METERWORN; // using meter?
 
integer CHANMYRIAD = -999;
integer MODULE_METER = -11;
string  DIV = "|";
integer FLAG_INCAPACITATED; // incapacitated by wounds?
integer FLAG_DEAD; // killed by critical wounds?
integer MINRESILIENCE = 1; // 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)+") HUD: "+dmessage);
    }
}
 
// ERROR - show errors on debug channel with wearer name for sorting
ERROR(string emessage) {
    llSay(DEBUG_CHANNEL,"ERROR ("+llKey2Name(PLAYERID)+"): "+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;
}
 
// 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
}
 
// 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 {
 
    // ATTACH - logged in with meter or worn from inventory/ground while running
    attach(key id) {
        id = NULL_KEY; // LSLINT
        RESET(); // a reset to reload character
    }
 
    // CHANGED - triggered for many changes to the avatar
    // TODO reload sim-specific settings on region change
    changed(integer changes) {
        if ( changes & CHANGED_REGION || changes & CHANGED_TELEPORT ) {
            RESET();
        }
    }
 
    // 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 ) return; // ignore our own link messages
        DEBUG("Module Meter Link Message: "+str);
        sender = 0; // LSLINT
        id = NULL_KEY; // LSLINT
        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
        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
            // 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; }
    }
 
 
    // ON_REZ - logged in with meter, or worn from inventory while running
    on_rez(integer param) {
        param = 0; // LSLINT
        RESET(); // a reset to reload character
    }
 
    // STATE ENTRY
    state_entry() {
        SETUP();        
    }
} // end default state
// END
Personal tools
General
About This Wiki