// Use on ALL "single avatar multisit" prims (with a mixed setup)
// When an avatar chooses a sync pose, the ones using a single pose will be forced to sync

// On return from couple to single, other avatars will play last used single pose
// Optionally, you can make the other avatars return to a specific single pose: see below...


key gQueryID;
string gName;
integer gLine;
integer SINGLE_POSES;
string sme ="stpsync";
string sme2 ="stpsync2";
integer lastpose;
integer LAST_SINGLE_POSE;

default{
   state_entry() {
      gName = llGetInventoryName(INVENTORY_NOTECARD, 0);
      if (gName != "") gQueryID = llGetNotecardLine(gName, gLine);
   }
   link_message(integer sender_num, integer num, string str, key id){
      if (llAvatarOnSitTarget()!=NULL_KEY){
         string ms=llList2String(llCSV2List(str),0);
         if (ms == "start" || ms == "startf") {
            if (num > SINGLE_POSES) llMessageLinked(LINK_ALL_OTHERS, num, sme, "");
            else {
               if (lastpose > SINGLE_POSES) llMessageLinked(LINK_ALL_OTHERS, num, sme2, "");
               LAST_SINGLE_POSE = num - 1;
            }
            lastpose=num;
         }
         else if (str==sme && lastpose <= SINGLE_POSES) {
            lastpose=num;
            llSleep(.6);
            llMessageLinked(LINK_THIS, num - 1, "stRC", "");
         }
         else if (str==sme2 && lastpose > SINGLE_POSES) { // return to single pose
            lastpose=num;
            llSleep(.6);

            // sync to last used single pose:
            llMessageLinked(LINK_THIS, LAST_SINGLE_POSE , "stRC", "");
            // Note: if you change the line above to
            //   llMessageLinked(LINK_THIS, 0 , "stRC", "");
            // the other avatar(s) will return to their 1st pose on notecard...

         }
      }
   }
   dataserver(key query_id, string data) {
      if (query_id == gQueryID) {
         if (data != EOF) {
            data=llStringTrim(data, STRING_TRIM);
            if (llGetSubString(data, 0, 0)=="-") {
               SINGLE_POSES=-((integer)data);
               if (SINGLE_POSES < 1 ) llOwnerSay("Singles Sync: Wrong single poses setup!");
               else llOwnerSay("Singles Sync: "+(string)SINGLE_POSES);
            }
            else {
               ++gLine;
               gQueryID = llGetNotecardLine(gName, gLine);
            }
         }
         else{
            llOwnerSay("Singles Sync: Single poses setup not found!");
         }
      }
   }
   changed(integer change) {
      if(change & CHANGED_INVENTORY)  llResetScript();
      if(lastpose && change & CHANGED_LINK) 
         if (llAvatarOnSitTarget()== NULL_KEY) lastpose=0;
   }
}