User:Allen Kerensky/Myriad Lite/Myriad Lite Region Status-Preview6.lsl

From OpenSimulator

Jump to: navigation, search

Myriad_Lite_Region_Status-Preview6.lsl

// Myriad_Lite_Region_Status-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
 
//============================================================================
// MESSAGE FORMAT REFERENCE
//============================================================================
// CHANMYRIAD IN - METER|str PLAYER NAME|str GAMENAME|int CURWOUNDS|int MAXWOUNDS|int CURCRITICAL|int MAXCRITICAL|int ISDEAD|int ISINCAPACITATED|str SPECIES|str BACKGROUND|str CAREER
 
//============================================================================
// GLOBAL VARIABLES
//============================================================================
integer CHANMYRIAD = -999; // Region Status Channel
integer HANDMYRIAD; // attach channel handle for llRemove
string DIV = "|"; // Myriad message field divider
string FORMATSTRING = " @GN (@PN) @HP @ST "; // GN = GameName, HP = HealthPercent, ST = Status (incap, dead, etc)
string  STATUS; // settext message
 
list PLAYERS = []; // strided list of player data
 
//============================================================================
// HEALTHPERCENT - generate a percentage health status
//============================================================================
string HEALTHPERCENT(integer curwounds,integer maxwounds,integer curcritical,integer maxcritical) {
    string out;
    float currentpoints = (float)curwounds + (float)curcritical; // add up noncritical and critical wounds boxes remaining
    float maxpoints = (float)maxwounds + (float)maxcritical; // add up total wounds boxes player should have
    if ( currentpoints > 0.0 ) { // if player has some wounds left
        float health = ( ( currentpoints / maxpoints ) * 100.0 ); // get a percentage
        out = "Health: "+(string)llRound(health)+"%"; // add the percentage health to the status
    } else { // oops all resilience gone
        out = "Health: 0%"; // so add a zero% to status
    }
    return out;
}
 
//============================================================================
// REPORT
//============================================================================
REPORT() {
    integer count = 0;
    list reportlist = [];
    for ( count = 0; count < llGetListLength(PLAYERS) / 12 ; count += 12 ) {
        key userid = llList2Key(PLAYERS,count);
        list region = llGetAgentList(AGENT_LIST_REGION,[]);
        if ( llListFindList(region,[userid]) < 0 ) {
            reportlist = llList2List(PLAYERS,count, count + 12 ) + reportlist;
        }
    }
    PLAYERS = reportlist;
    count = 0;
    integer num_players = llGetListLength(PLAYERS) / 12; // stride size
    string DISPLAY = "";
    for ( count = 0; count < num_players; count += 12 ) {
        //key    userid       = llList2Key(PLAYERS,count);        
        string playername   = llList2String(PLAYERS,count + 1); // get the full player name from the list of METER values
        list   name2        = llParseString2List(playername,[" "],[]); // separate first and last name by space
        string firstname    = llList2String(name2,0); // get the firstname from the name2
        string lastname     = llList2String(name2,1); // get the lastname, if any, from the name2
        string gamename     = llList2String(PLAYERS,count + 2); // get player's alias/game name (originally set in their character sheet)
        integer curwounds   = llList2Integer(PLAYERS,count + 3); // what is player's current wound value?
        integer maxwounds   = llList2Integer(PLAYERS,count + 4); // what is player's maximum healed wounds allowed by level/stats?
        integer curcritical = llList2Integer(PLAYERS,count + 5); // what is player's current critical wounds value?
        integer maxcritical = llList2Integer(PLAYERS,count + 6); // what is player's maximum healed critical wounds value?
        integer isdead      = llList2Integer(PLAYERS,count + 7); // is player dead?
        integer isincap     = llList2Integer(PLAYERS,count + 8); // is player incapacitated?
        string  species     = llList2String(PLAYERS,count + 9); // player species
        string  background  = llList2String(PLAYERS,count + 10); // player background
        string  career      = llList2String(PLAYERS,count + 11); // player career
 
        // okay, we've broken down status, lets create a banner from that using colors
        STATUS = FORMATSTRING; // start with a default FORMATSTRING
        STATUS = SEARCHANDREPLACE(STATUS,"@PN",playername); // @PN = Player Name
        STATUS = SEARCHANDREPLACE(STATUS,"@FN",firstname); // @FN = First Name
        STATUS = SEARCHANDREPLACE(STATUS,"@LN",lastname); // @PN = Last Name                
        STATUS = SEARCHANDREPLACE(STATUS,"@GN",gamename); // @GN = Game Name
        STATUS = SEARCHANDREPLACE(STATUS,"@CW",(string)curwounds); // @CW = Current Wounds
        STATUS = SEARCHANDREPLACE(STATUS,"@MW",(string)maxwounds); // @MW = Max Wounds
        STATUS = SEARCHANDREPLACE(STATUS,"@CC",(string)curcritical); // @CC = Current Critical                
        STATUS = SEARCHANDREPLACE(STATUS,"@MC",(string)maxcritical); // @MC = Max Critical
        if ( isincap == 1 && isdead == 0 ) {
            STATUS = SEARCHANDREPLACE(STATUS,"@ST"," ! INCAPACITATED ! "); // @ST = Status
        } else {
            STATUS = SEARCHANDREPLACE(STATUS,"@ST",""); // @ST = Status
        }
        if ( isdead == 1 ) {
            STATUS = SEARCHANDREPLACE(STATUS,"@ST"," !!! DEAD !!! "); // @ST = Status
        } else {
            STATUS = SEARCHANDREPLACE(STATUS,"@ST",""); // @ST = Status
        }
        STATUS = SEARCHANDREPLACE(STATUS,"@SP",species); // @SP = Species
        STATUS = SEARCHANDREPLACE(STATUS,"@BG",background); // @BG = Background Name
        STATUS = SEARCHANDREPLACE(STATUS,"@CA",career); // @CA = Career
        string hp = HEALTHPERCENT(curwounds,maxwounds,curcritical,maxcritical);
        STATUS = SEARCHANDREPLACE(STATUS,"@HP",hp); // @HP = Health Percent
        DISPLAY = DISPLAY + "\n" + STATUS;
    }
    llSetText(DISPLAY,<1,1,1>,1);
}
 
//============================================================================
// SEARCHANDREPLACE - search and replace tokens
//============================================================================
string SEARCHANDREPLACE(string input, string old, string new)  {
   return llDumpList2String(llParseString2List(input, [old], []), new);
} 
 
//============================================================================
// GLOBAL SETUP()
//============================================================================
SETUP() {
    llSetText("--- Waiting for Myriad Update ---",<1,0,0>,1); // set a default banner to show we haven't been updated yet
    if ( HANDMYRIAD != 0 ) llListenRemove(HANDMYRIAD); // remove previously open channel
    HANDMYRIAD = llListen(CHANMYRIAD,"",NULL_KEY,""); // start a listener on the dynamic channel
}
 
//============================================================================
// DEFAULT STATE
//============================================================================
default {
    //------------------------------------------------------------------------
    // STATE_ENTRY EVENT
    //------------------------------------------------------------------------    
    state_entry() {
        SETUP(); // setup the hovertext meter
    }
 
    //------------------------------------------------------------------------
    // ON_REZ EVENT
    //------------------------------------------------------------------------    
    on_rez(integer param) {
        param = 0; // LSLINT
        llResetScript(); // setup the hovertext meter
    }
 
    //------------------------------------------------------------------------
    // CHANGED EVENT
    //------------------------------------------------------------------------    
    changed(integer changes) {
        if ( changes & CHANGED_OWNER ) { // if owner has changed, we need to recalculate the dynamic channel
            llResetScript(); // setup the hovertext meter
            return;
        }
        if ( changes & CHANGED_REGION_START ) { // owner jumped to new location? restart
            llResetScript(); // setup hovertext and channel
            return;
        }
    }
 
    //------------------------------------------------------------------------
    // LISTEN EVENT
    //------------------------------------------------------------------------    
    listen(integer channel,string name,key id,string message) {
        name = ""; // LSLINT
        if ( channel == CHANMYRIAD ) { // did this message come in on attachment channel?
            list fields = llParseString2List(message,[DIV],[]); // break message down into list separated by |
            string command = llList2String(fields,0); // read first item in list to get the Myriad command
            if ( command == "METER") { // if this is the METER command, let's update the meter status
                PLAYERS = [id] + llDeleteSubList(fields,0,0) + PLAYERS;
                PLAYERS = llListSort(PLAYERS,12,FALSE);
                REPORT();                
            } // end of command meter
        } // end if chanmyriad
    }
} // end default
//============================================================================
// END
//============================================================================
Personal tools
General
About This Wiki