// See end of script for menu access options

// You can remote command Single Avatar MultiSit from a script, using:
// llMessageLinked(LINK_THIS, 0, "stRC", ""); 
// Using 0 (zero) and LINK_THIS you trigger the 1st pose of the same prim (on a sync prim, even with nobody on it, this triggers people on other sync prims).
// The /h and /k options added to the notecard will stop Single Avatar MultiSit reaction on touch, never show its dialog, and always play the 1st pose on sit.

list poses =["p1","p2","p3"]; // Poses Names Here: max 12 buttons on this script


integer AgentSitting(key kk) {
    integer a = llGetNumberOfPrims();
    while(1 < a){
        key lk=llGetLinkKey(a);
        if(llGetAgentSize(lk)){
            if (lk==kk) return TRUE;
            --a;
        }
        else return FALSE;
    }
    return FALSE;
}

integer channel; 
integer listen_handle;
key owner;

dlg(key kk) {
    stopt();
    channel = -((integer)llFrand(20000000)+1000); //random channel so multiple scripts don't interfere with each other
    listen_handle = llListen(channel, "", kk, "");
    llSetTimerEvent(120); // Timeout in seconds
    llDialog(kk, "\n\nSelect a Pose", poses, channel);
}

stopt(){
   llListenRemove(listen_handle);
   llSetTimerEvent(0);
}

default 
{
    on_rez(integer s) { llResetScript(); }
    state_entry() { owner = llGetOwner(); }
        
    listen(integer chan, string name, key id, string message) {
       integer index = llListFindList(poses, [message]);
        if (index != -1) {
            llMessageLinked(LINK_THIS, index, "stRC", ""); // Same prim of "single avatar multisit"
            stopt();
        }
        
    }   
    
    timer(){ stopt(); }
    
    touch_start(integer s) {
        key k=llDetectedKey(0);
        if (AgentSitting(k)) return; // must not be sitting on object
        //if (!llSameGroup(k) && k != owner) return; // allow group/owner only
        //if (k != owner) return; // allow owner only
        dlg(k);
    }

}