User:Allen Kerensky/Myriad Lite Preview 5/Module Armor

From OpenSimulator

< User:Allen Kerensky | Myriad Lite Preview 5
Revision as of 14:31, 6 February 2012 by Allen Kerensky (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Myriad Lite Module: Armor

The Myriad Lite Module Armor script (below) goes into the Myriad Lite HUD.

More information such as API messages will be in a later Preview release.

Myriad_Lite_Module_Armor-v0.0.5-20120130.lsl

// Myriad_Lite_Module_Armor-v0.0.0-20120125.lsl
// Copyright (c) 2012 By Allen Kerensky (OSG/SL)
// The Myriad RPG System was designed, written, and illustrated by Ashok Desai
// Myriad RPG licensed under the Creative Commons Attribution 2.0 UK: England and Wales
// http://creativecommons.org/licenses/by/2.0/uk/
// Myriad Lite software Copyright (c) 2011-2012 by Allen Kerensky (OSG/SL)
// Baroun's Adventure Machine Copyright (c) 2008-2011 by Baroun Tardis (SL)
// Myriad Lite and Baroun's Adventure Machine licensed under the
// Creative Commons Attribution-Share Alike-Non-Commercial 3.0 Unported
// http://creativecommons.org/licenses/by-nc-sa/3.0/
// You must agree to the terms of this license before making any use of this software.
// If you do not agree to this license, simply delete these materials.
// There is no warranty, express or implied, for your use of these materials.

key PLAYERID = NULL_KEY; // cached player UUID

integer LM_ATTACHARMOR   = 0x80000000;
integer LM_DETACHARMOR   = 0x80000001;
integer LM_ARMORCURRENT  = 0x80000002;
integer LM_ARMORRESET    = 0x80000003;
integer LM_ARMORON       = 0x80000004;
integer LM_ARMOROFF      = 0x80000005;
integer LM_ARMORBATTERY  = 0x80000006;
integer LM_ARMORRECHARGE = 0x80000007;
integer LM_ARMORCHECK    = 0x80000008;

integer MINATTACH = 1; // min valid attach point
integer MAXWEAR = 30; // max valid in-world wearable attach point
integer MINARMOR = 1; // min armor defense value
integer MAXARMOR = 5; // max armor defense value
integer CURARMOR = 0; // highest armor value worn out of all armor worn, not a total
list ARMOR = [];
// string names for each attach point - waste of memory?
list ATTACHPOINTS = ["INVALID","chest","head","left shoulder","right shoulder","left hand","right hand","left foot","right foot","back","pelvis","mouth","chin","left ear","right ear","left eye","right eye","nose","right upper arm","right lower arm","left upper arm","left lower arm","right hip","right upper leg","right lower leg","left hip","left upper leg","left lower leg","stomach","left pectoral","right pectoral","HUD Center 2","HUD Top Right","HUD Top","HUD Top Left","HUD Center","HUD Bottom Left","HUD Bottom","HUD Bottom Right" ];

string CHAN_PREFIX = "0x"; // channel prefix for calculating dynamic channels
string DIV = "|"; // message field divider
integer CHANATTACH = 0; // dynamic channel for attachments

// ERROR - show errors on debug channel with wearer name for sorting
ERROR(string emessage) {
    llSay(DEBUG_CHANNEL,"ERROR ("+llKey2Name(PLAYERID)+"): "+emessage);
}

// WEARARMOR - Wearing a piece of armor
WEARARMOR(integer waattachpoint,integer waamount,string waname) {
    if ( waattachpoint < MINATTACH || waattachpoint > MAXWEAR ) { // valid attach point?
        ERROR("Invalid armor attachment point "+llList2String(ATTACHPOINTS,waattachpoint));
        return;
    }
    if ( waamount < MINARMOR || waamount > MAXARMOR ) { // is armor rating valid or legal?
        ERROR("Invalid armor amount "+(string)waamount+" out of range "+(string)MINARMOR+"-"+(string)MAXARMOR);
        return;
    }
    // FIXME move ARMOR to 3-element strided list? [attachpoint,value,name?]
    ARMOR = llListReplaceList(ARMOR,[waamount],waattachpoint,waattachpoint); // insert armor value into armor list
    llOwnerSay("Armor "+waname+" ("+(string)waamount+") attached to "+llList2String(ATTACHPOINTS,waattachpoint));
    RECALCULATE_ARMOR(); // find new highest armor value
}

// REMOVEARMOR - Removing a piece of armor
REMOVEARMOR(integer raattachpoint,integer raamount,string raname) {
    if ( raattachpoint < MINATTACH || raattachpoint > MAXWEAR ) { // valid attach point?
        ERROR("Invalid armor detachment point "+llList2String(ATTACHPOINTS,raattachpoint));
        return;
    }
    if ( raamount < MINARMOR || raamount > MAXARMOR ) { // is armor rating valid or legal?
        ERROR("Invalid armor amount "+(string)raamount+" out of range "+(string)MINARMOR+"-"+(string)MAXARMOR);
        return;
    }    
    ARMOR = llListReplaceList(ARMOR,[0],raattachpoint,raattachpoint); // zero out the armor value in armor list
    llOwnerSay("Armor "+raname+" ("+(string)raamount+") detached from "+llList2String(ATTACHPOINTS,raattachpoint));
    RECALCULATE_ARMOR(); // find new highest armor value
}

// RECALCULATE_ARMOR - sets CURARMOR to highest armor value worn after attach or detach
RECALCULATE_ARMOR() {
    CURARMOR = 0; // start with zero armor
    integer racount = llGetListLength(ARMOR); // how long is armor list?
    while (racount--) { // look at each list item from last to first
        integer rapoints = llList2Integer(ARMOR,racount); // what is armor value at this point in list?
        if ( rapoints > CURARMOR ) { // is this armor value higher than current max?
            CURARMOR = rapoints; // yes, save new highest amount
        }
    }
    llOwnerSay("Armor Rating is "+(string)CURARMOR);
    llMessageLinked(LINK_THIS,LM_ARMORCURRENT,"ARMORCURRENT|"+(string)CURARMOR,PLAYERID);
    // FIXME tell world too?
}

// ARMOR ON
ARMORON() {
    llWhisper(CHANATTACH,"ARMORON");
}

// ARMOROFF
ARMOROFF() {
    llWhisper(CHANATTACH,"ARMOROFF");
}

// CHECKBATTERY
CHECKBATTERY() {
    llWhisper(CHANATTACH,"CHECKBATTERY");
}

// RECHARGE
RECHARGE() {
    llWhisper(CHANATTACH,"RECHARGE");
}

// SETUP - begin
SETUP() {
    PLAYERID = llGetOwner(); // remember the owner's UUID
    integer attachpoints = 37; // counting from 0 to 37
    while ( attachpoints-- ) {
        ARMOR = ARMOR + [0]; // create 38 empty armor slots - avoids SL stack depth error and LSLINT warning
    }
    CHANATTACH = (integer)(CHAN_PREFIX+llGetSubString((string)PLAYERID,1,7)); // attachment-specific channel
}

// RESET - shut down running animations then reset the script to reload character sheet
RESET() {
    llResetScript(); // now reset
}

default {
    state_entry() {
        SETUP();
    }
    
    on_rez(integer params) {
        params = 0; // LSLINT
        RESET();
    }
    
    attach(key id) {
        id = NULL_KEY; // LSLINT
        RESET();
    }
    
    // CHANGED - triggered for many changes to the avatar
    // TODO reload sim-specific settings on region change
    changed(integer changes) {
        if ( changes & CHANGED_INVENTORY ) { // inventory changed somehow?
            RESET();
        }
        if ( changes & CHANGED_REGION || changes & CHANGED_TELEPORT ) {
            RESET();
        }
    }
    // LINK MESSAGE - commands to and from other prims in HUD
    link_message(integer sender,integer num,string cmd, key id) {
        sender = 0 ; // LSLINT
        id = NULL_KEY; // LSLINT
        // break down the commands and messages into units we can work with
        list fields = llParseString2List(cmd,[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
        if ( num == LM_ATTACHARMOR || command == "attacharmor" ) {        
            integer armorrating = llList2Integer(fields,1); // get armor value
            integer attachpoint = llList2Integer(fields,2); // get armor location
            string armorname = llList2String(fields,3); // get armor's name
            WEARARMOR(attachpoint,armorrating,armorname); // add armor to set of armor worn
            return;
        }
        if ( num == LM_DETACHARMOR || command == "detacharmor" ) {
            integer armorrating = llList2Integer(fields,1); // get armor value
            integer attachpoint = llList2Integer(fields,2); // get armor location
            string armorname = llList2String(fields,3); // get armor's name
            REMOVEARMOR(attachpoint,armorrating,armorname); // detach armor from set of armor worn
            return;
        }
        if ( num == LM_ARMOROFF || command == "armoroff" ) { ARMOROFF(); return;} // turn off power armor
        if ( num == LM_ARMORON || command == "armoron" ) { ARMORON(); return;} // turn on power armor
        if ( num == LM_ARMORCHECK || command == "checkarmor" ) { RECALCULATE_ARMOR(); return; } // check our current armor value
        if ( num == LM_ARMORBATTERY || command == "checkbattery") { CHECKBATTERY(); return;}  // check power armor battery
        if ( num == LM_ARMORRECHARGE || command == "recharge" ) { RECHARGE(); return;} // recharge power armor battery
        if ( num == LM_ARMORRESET || command == "armorreset" ) { RESET(); return;} // reset HUD
    }
}

Personal tools
General
About This Wiki