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


key gQueryID;
string gName;
integer gLine;
integer SINGLE_POSES;
string sme ="stpsync"; // link message
integer lastpose;

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, "");
            lastpose=num;
         }
         else if (str==sme && lastpose <= SINGLE_POSES) {
            lastpose=num;
            llSleep(.6);
            llMessageLinked(LINK_THIS, num - 1, "stRC", "");
         }
      }
   }
   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; // increase line count
               gQueryID = llGetNotecardLine(gName, gLine); // request next line
            }
         }
         else{
            llOwnerSay("Singles Sync: Single poses setup not found!");
         }
      }
   }
   changed(integer change) {
      if(change & CHANGED_INVENTORY)  llResetScript();
   }
}