MLPV2 LockGuard plugin

From Second Life Wiki
Revision as of 13:13, 14 April 2011 by Grey Mars (talk | contribs) (A plugin script for MLP2 to add LockGuard Support)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Basically this is an alternate way of adding particle chain support to MLP2 using the LockGuard protocol. When the object is rezzed in world the script makes a list of the prim names and keys for later. It then reads the note card with the linking information each time inventory changes. When MLP2 gives pose info the script determines if it pertains to it's list and if so notifies lockguard enabled attachments of what to do and what prim keys to do it to.

So what do you need to do?

Step 1: Figure out what prims you want to use as anchor points, and give each a unique name. Personally I recommend the boring yet easy to remember format of point1, point2, point3, etc. Remember these names for later.

Step 2: Add the following script to your furniture. It may complain, but you can ignore that for now.

<lsl> // ---------------------------------------------------------------------------------- // MLPV2 Plugin for LockGuard Partical Chaining v1.1 // // Notecard .chains format: // menulable;animation;anchorprimname;LGattachpoint optionalparams // // Example data: // Bound;Short Posts-1;point2;rightwrist gravity 0.2 life 0.75 // Bound;Short Posts-1;point4;leftwrist gravity 0.2 life 0.75 // Bound;Short Posts-1;point3;collarfrontloop gravity 1.75 life 1.5 // Displayed;dungeon-2;point1;wrists gravity 0.0 life 2.0 // Hung;dungeon-3;point1;wrists gravity 0.0 life 0.75 // Dangled;Hung-2A;point1;ankles gravity 0.0 life 0.75 // Captured;Why Me-v2;point3;wrists // // ---------------------------------------------------------------------------------- // Copyright 2011, Grey Mars. All rights reserved. // ---------------------------------------------------------------------------------- // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this list of // conditions and the following disclaimer. // // 2. 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. // // THIS SOFTWARE IS PROVIDED BY GREY MARS ``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 <COPYRIGHT HOLDER> OR // 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 views and conclusions contained in the software and documentation are those of the // authors and should not be interpreted as representing official policies, either expressed // or implied, of Grey Mars.


// ---------------------------------------------------------------------------------- // Globals // ----------------------------------------------------------------------------------

integer VERBOSE = FALSE; // Give extra info

integer LGChannel = -9119; // The LG listen channel integer LGHandle; // Handle ID for LG messages

// Globals for reading card config integer ConfigLineIndex; list ConfigCards; // list of names of config cards string ConfigCardName; // name of card being read integer ConfigCardIndex; // index of next card to read key ConfigQueryId;

// Currently unused variables. In the future may connect with some verification key SimRatingReq; string Rating; integer Verified;

// Variables used with chain data and pose reading. // It could be worse, but it could also be a lot more // efficient. string Pose; string Avname; key Avkey;

string CurrentSet;

list chainData = []; integer chainCount = 0; integer chainStride = 4;

list primKeys = []; integer primKeysCount = 0; integer primKeysStride = 2;

list avData = []; integer avDataCount = 0; integer avDataStride = 2;

// ---------------------------------------------------------------------------------- // Support functions // ----------------------------------------------------------------------------------

// Debugging outputs debug(string text) { if (VERBOSE) llOwnerSay(text); }

// get the next card in case there is more than one integer next_card() {

   if (ConfigCardIndex >= llGetListLength(ConfigCards)) {
       ConfigCards = [];
       return (FALSE);
   }
   
   ConfigLineIndex = 0;
   ConfigCardName = llList2String(ConfigCards, ConfigCardIndex);
   ConfigCardIndex++;
   ConfigQueryId = llGetNotecardLine(ConfigCardName, ConfigLineIndex);
   debug("[?] Reading file: " + ConfigCardName);
   return (TRUE);

}

// add chain data to the list... add_cd(string lable, string anim, string target, string params) {

   chainData = (chainData=[]) + chainData + (list)lable;
   chainData = (chainData=[]) + chainData + (list)anim;
   chainData = (chainData=[]) + chainData + (list)target;
   chainData = (chainData=[]) + chainData + (list)params;
   ++chainCount;

}

// does this point actually referance a set of points? list UnAliasChainPoints(list originalData) { // wrists -> leftwrist rightwrist // ankles -> leftankle rightankle // allfour -> leftwrist rightwrist leftankle rightankle // nipples -> leftnipplering rightnipplering // arms -> leftupperarm rightupperarm // thighs -> leftupperthigh rightupperthigh // knees -> leftknee rightknee

   list modData = [];
   integer xx;
   
   // Hacky.  Feel free to optimize.
   for(xx = 0; xx < llGetListLength(originalData); xx++) {
       if (llToLower(llList2String(originalData,xx)) == "wrists") {
           modData = modData + ["leftwrist", "rightwrist"];
       } else if (llToLower(llList2String(originalData,xx)) == "ankles") {
           modData = modData + ["leftankle", "rightankle"];
       } else if (llToLower(llList2String(originalData,xx)) == "allfour") {
           modData = modData + ["leftankle", "rightankle", "leftwrist", "rightwrist"];
       } else if (llToLower(llList2String(originalData,xx)) == "nipples") {
           modData = modData + ["leftnipplering", "rightnipplering"];
       } else if (llToLower(llList2String(originalData,xx)) == "arms") {
           modData = modData + ["leftupperarm", "rightupperarm"];
       } else if (llToLower(llList2String(originalData,xx)) == "thighs") {
           modData = modData + ["leftupperthigh", "rightupperthigh"];
       } else if (llToLower(llList2String(originalData,xx)) == "knees") {
           modData = modData + ["leftknee", "rightknee"];
       } else {    modData = modData + llList2List(originalData,xx,xx);     }
   }
   
   return modData;   

}

// ---------------------------------------------------------------------------------- // Main Script // ----------------------------------------------------------------------------------

default {

   state_entry() {
       
       // build prim key list
       integer n;
       integer linkcount = llGetNumberOfPrims();
       for (n = 2; n <= linkcount; n++)    {
           string thiselement = llGetLinkName(n);
           if (thiselement != "Object") {
               primKeys = primKeys + [thiselement, llGetLinkKey(n)];
               primKeysCount++;
           }
       }
       
       state load;
   }

}

state load {

   state_entry() {
       string item;
       ConfigCards = [];
       integer n = llGetInventoryNumber(INVENTORY_NOTECARD);
       while (n-- > 0) { // get the data off cards with the right name
           item = llGetInventoryName(INVENTORY_NOTECARD, n);
           if (llSubStringIndex(item, ".chains") != -1) {
               ConfigCards = (ConfigCards=[]) + ConfigCards + (list) item;
           }
       }
       ConfigCardIndex = 0;
       ConfigCards = llListSort(ConfigCards, 1, TRUE);
       next_card();
       SimRatingReq = llRequestSimulatorData(llGetRegionName(), DATA_SIM_RATING);
   }
   
   dataserver(key query_id, string data) {
       if (query_id == SimRatingReq) {
           Rating = data;
           return;
       }
       if (query_id != ConfigQueryId) {    return;     }                             
       if (data == EOF) {
           if ( next_card() ) { return;  }
           state on;
       }
       
       data = llStringTrim(data, STRING_TRIM);
       if (llGetSubString(data,0,0) != "/" && llStringLength(data)) {          // skip comments and blank lines
           list ldata = llParseStringKeepNulls(data, [";"], []);
           string lable = llStringTrim(llList2String(ldata, 0), STRING_TRIM);
           string anim = llStringTrim(llList2String(ldata, 1), STRING_TRIM);
           string target = llStringTrim(llList2String(ldata, 2), STRING_TRIM);
           string param = llStringTrim(llList2String(ldata, 3), STRING_TRIM);
           add_cd(lable,anim,target,param);
       }
       
       ++ConfigLineIndex;
       ConfigQueryId = llGetNotecardLine(ConfigCardName, ConfigLineIndex);       //read next line of positions notecard
   }

}


state on {

   state_entry()   {    }
   on_rez(integer arg) { llResetScript(); }
   changed(integer change) { if (change & CHANGED_INVENTORY) state load; }
   link_message(integer from, integer num, string message, key id) {
   
       if (!((num == -11000) || (num == -11001) || (num == -11002) || (message == "POSEB"))) { return; }
   
       if (message == "POSEB") {
           CurrentSet = (string)id;
           return;
       }
       debug("========== START");
       // it's a generic sit/unsit/change update
       list linedata = llParseStringKeepNulls(message,["|"],[]);
       string param1 = llList2String(linedata, 0);
       string param2 = llList2String(linedata, 1);
       
       // does this menu selection have any chain data attached to it?
       integer findSet = llListFindList(chainData, (list)CurrentSet);
       integer findAv = llListFindList(avData, [id]);
       if (findSet == -1) { // The avatar wasn't chained for this anim, so clear chains if any
       debug("[!] No chain data found");
           if (findAv != -1) {
               list itemData = llParseStringKeepNulls(llList2String(avData,findAv + 1),["|"],[]);
               integer ix;
               for(ix = 0; ix < llGetListLength(itemData); ix++) {
                   llWhisper(LGChannel,"lockguard " + (string)id + " " + llList2String(itemData,ix) + " unlink");
                   debug("[-] " + llList2String(itemData,ix));
               }
               avData = llDeleteSubList(avData, findAv, findAv + 1);
           }
           return;    
       }
       else debug("[!] Chain data found");
       list chainLog = []; // list of connands including anim name
       integer ii; // counter var
       for(ii = 0; ii < llGetListLength(chainData); ii = ii + chainStride) { // check each entry
           if (llList2String(chainData, ii + 1) == param2) { // if it's a match add it to the new list
               chainLog = llList2List(chainData, ii + 2, ii + 3) + chainLog;
           } // end if
       } // end for
           
       // TYPE: sit
       // FORMAT: llMessageLinked(LINK_SET, -11000, (string)BallNum + "|" + animation, avatar);
       if (num == -11000) { // A sit
           debug("[!] Sit triggered");
           string tempPoints = "";
           integer ix;
           for(ix = 0; ix < llGetListLength(chainLog); ix = ix + 2) {
               integer findKey = llListFindList(primKeys, (list)llList2String(chainLog, ix));
               llWhisper(LGChannel,"lockguard " + (string)id + " " + llList2String(chainLog,ix + 1) + 
                           " link " + llList2String(primKeys, findKey + 1));
               debug("[+] " + llList2String(chainLog,ix + 1));
               tempPoints = tempPoints +  llList2String(chainLog,ix + 1) + "|"; 
           }
           
           tempPoints = llGetSubString(tempPoints, 0, -2); // trim the final char
           avData = (avData=[]) + avData + [id]; // add new
           avData = (avData=[]) + avData + [tempPoints]; // add new
       }
       
       // TYPE: unsit
       // FORMAT:  llMessageLinked(LINK_SET, -11001, (string)BallNum, llGetPermissionsKey());
       if (num == -11001) { // An unsit
           debug("[!] Unsit triggered"); 
           if (findAv != -1) {
               list itemData = llParseStringKeepNulls(llList2String(avData,findAv + 1),["|"],[]);
               integer ix;
               for(ix = 0; ix < llGetListLength(itemData); ix++) {
                   llWhisper(LGChannel,"lockguard " + (string)id + " " + llList2String(itemData,ix) + " unlink");
                   debug("[-] " + llList2String(itemData,ix));
               }
               avData = llDeleteSubList(avData, findAv, findAv + 1);
           }
       }
       
       // TYPE: change
       // FORMAT: llMessageLinked(LINK_SET, -11002, (string)BallNum + "|" + animation, avatar);
       if (num == -11002) { // A pose change
           debug("[!] Pose change triggered");
           list itemData = [];
           if (findAv != -1) {
               itemData = llParseStringKeepNulls(llList2String(avData,findAv + 1),["|"],[]);
               itemData = UnAliasChainPoints(itemData);
           }
           
           integer ix;
           string tempPoints = "";
           for(ix = 0; ix < llGetListLength(chainLog); ix = ix + 2) {
               integer findKey = llListFindList(primKeys, (list)llList2String(chainLog, ix));
               llWhisper(LGChannel,"lockguard " + (string)id + " " + llList2String(chainLog,ix + 1) + 
                           " link " + llList2String(primKeys, findKey + 1));
               debug("[+] " + llList2String(chainLog,ix + 1));
               // cut down the command line to just the attach point for compairison
               string stripPoint = llList2String(llParseStringKeepNulls(llList2String(chainLog,ix + 1),[" "],[]), 0);
               tempPoints = tempPoints + stripPoint + "|";
           }
           tempPoints = llGetSubString(tempPoints, 0, -2); // trim the final char "|"
           
           if (findAv != -1) { avData = llDeleteSubList(avData, findAv, findAv + 1);   } // clear old
           avData = (avData=[]) + avData + [id]; // add new
           avData = (avData=[]) + avData + [tempPoints]; // add new
           
           list tempData = llParseStringKeepNulls(llList2String(avData,findAv + 1),["|"],[]);
           tempData = UnAliasChainPoints(tempData);
           // compaire the two data lists, remove dumplicates
           integer yx;
           for(yx = 0; yx < llGetListLength(tempData); yx++) {
               integer overlapWhere = llListFindList(itemData, llList2List(tempData, yx, yx));
                   if (overlapWhere != -1) {
                       itemData = llDeleteSubList(itemData, overlapWhere, overlapWhere);
                   }
           }
           
           if ((findAv != -1) && ( llGetListLength(itemData) > 0)) { // We are already tracking points for this av...
               for(ix = 0; ix < llGetListLength(itemData); ix++) {
                   llWhisper(LGChannel,"lockguard " + (string)id + " " + llList2String(itemData,ix) + " unlink");
               debug("[-] " +  llList2String(itemData,ix));
               }
           }
       }
   }

} </lsl>

Step 3: Create the data note card. This card needs to be named .chains, but can have a further name past that if you need to use more than one card. The card will look something like this:

//////////////////////////////////////////////////
// Chain control
//////////////////////////////////////////////////
//  Part 1: menulable
//  Part 2: animation chain is linked to
//  Part 3: prim name to link to
//  Part 4: LG attach point
//  Part 4 optional commands:
//      texture key
//      size # #
//      life #
//      gravity #
//      speed #
//      color # # #
//////////////////////////////////////////////////
Bound;Short Posts-1;point2;rightwrist gravity 0.2 life 0.75
Bound;Short Posts-1;point4;leftwrist gravity 0.2 life 0.75
Bound;Short Posts-1;point3;collarfrontloop gravity 1.75 life 1.5
Displayed;dungeon-2;point1;wrists gravity 0.0 life 2.0
Hung;dungeon-3;point1;wrists gravity 0.0 life 0.75
Dangled;Hung-2A;point1;ankles gravity 0.0 life 0.75
Captured;Why Me-v2;point3;wrists gravity 0.0 life 0.25