Difference between revisions of "MLPV2 RLV Capture Plugin"

From Second Life Wiki
Jump to: navigation, search
m
(fixed a bug with names >24 characters)
Line 4: Line 4:
 
This is another RLV plugin I wrote. It is used in a Ball. On touch it allows to choose an avatar within a range and tries to capture the selected avatar, forcing them to sit on the ball.
 
This is another RLV plugin I wrote. It is used in a Ball. On touch it allows to choose an avatar within a range and tries to capture the selected avatar, forcing them to sit on the ball.
  
<lsl>// ~capture Script V1.0
+
<lsl>// ~capture Script V1.01
 
// drop into a ball
 
// drop into a ball
 
//
 
//
Line 35: Line 35:
 
log(string message)
 
log(string message)
 
{
 
{
    if (DEBUG) llOwnerSay("MENU: " + message);
+
if (DEBUG) llOwnerSay("MENU: " + message);
 
}
 
}
  
Line 41: Line 41:
 
out(string message)
 
out(string message)
 
{
 
{
    llSay(0, message);
+
llSay(0, message);
 
}
 
}
  
 
capture(key avatar)
 
capture(key avatar)
 
{
 
{
    relay(avatar, "@sit:" + (string)llGetKey() + "=force");
+
relay(avatar, "@sit:" + (string)llGetKey() + "=force");
 
}
 
}
  
 
lock(key avatar)
 
lock(key avatar)
 
{
 
{
    relay(avatar, "@unsit=n");
+
relay(avatar, "@unsit=n");
 
}
 
}
  
 
release(key avatar)
 
release(key avatar)
 
{
 
{
    relay(avatar, "@unsit=y");
+
relay(avatar, "@unsit=y");
    relay(avatar, "!release");
+
relay(avatar, "!release");
 
}
 
}
  
Line 63: Line 63:
 
relay(key avatar, string message)
 
relay(key avatar, string message)
 
{
 
{
    if (avatar != NULL_KEY)
+
if (avatar != NULL_KEY)
    {
+
{
        llSay(RELAY_CHANNEL, llGetObjectName() + "," + (string) avatar + "," + message);
+
llSay(RELAY_CHANNEL, llGetObjectName() + "," + (string) avatar + "," + message);
        log("RLV: " + llGetObjectName() + "," + (string) avatar + "," + message);
+
log("RLV: " + llGetObjectName() + "," + (string) avatar + "," + message);
    }
+
}
 
}
 
}
  
 
init()
 
init()
 
{
 
{
    dialog_channel= -(integer)(llFrand(100000 - 10000) + 10000);
+
dialog_channel= -(integer)(llFrand(100000 - 10000) + 10000);
    dialog_handle = llListen(dialog_channel, "", NULL_KEY, "");
+
dialog_handle = llListen(dialog_channel, "", NULL_KEY, "");
    llListenControl(dialog_handle, FALSE);
+
llListenControl(dialog_handle, FALSE);
    avatar_menu = NULL_KEY;
+
avatar_menu = NULL_KEY;
 
}
 
}
  
Line 81: Line 81:
 
default
 
default
 
{
 
{
    // on state entry: show dialog
+
// on state entry: show dialog
    state_entry()
+
state_entry()
    {
+
{
        init();
+
init();
    }
+
}
  
    on_rez(integer start_param)
+
on_rez(integer start_param)
    {
+
{
        channel = start_param;
+
channel = start_param;
        init();
+
init();
    }
+
}
  
    // av touched me
+
// av touched me
    touch_start(integer total_number)
+
touch_start(integer total_number)
    {
+
{
        key id = llDetectedKey(0);
+
key id = llDetectedKey(0);
  
        // check if menu is in use
+
// check if menu is in use
        if (avatar_menu != NULL_KEY && id != avatar_menu)
+
if (avatar_menu != NULL_KEY && id != avatar_menu)
        {
+
{
            out(llKey2Name(avatar_menu) + MSG_MENU_IN_USE);
+
out(llKey2Name(avatar_menu) + MSG_MENU_IN_USE);
            return;
+
return;
        }
+
}
  
        avatar_menu = id;
+
avatar_menu = id;
        llSensor("", NULL_KEY, AGENT, RANGE_CAPTURE, PI);
+
llSensor("", NULL_KEY, AGENT, RANGE_CAPTURE, PI);
    }
+
}
  
    // no av in sensor range
+
// no av in sensor range
    no_sensor()
+
no_sensor()
    {
+
{
        out(MSG_NO_SENSOR);
+
out(MSG_NO_SENSOR);
    }
+
}
  
    // some av in sensor range
+
// some av in sensor range
    sensor(integer total_number)
+
sensor(integer total_number)
    {
+
{
        sensor_keys = [];
+
sensor_keys = [];
        sensor_names = [];
+
sensor_names = [];
        integer i;
+
integer i;
        for(i=0; i < total_number; i++)
+
for(i=0; i < total_number; i++)
        {
+
{
            key id = llDetectedKey(i);
+
key id = llDetectedKey(i);
            string name = llKey2Name(id);
+
string name = llKey2Name(id);
 +
if (llStringLength(name) > 24)
 +
{
 +
name = llGetSubString(name, 0, 23);
 +
}
 +
sensor_keys += [id];
 +
sensor_names += [name];
 +
log("found: " + name);
 +
}
  
            sensor_keys += [id];
+
// show dialog if list contains names
            sensor_names += [name];
+
if (llGetListLength(sensor_names) > 0)
            log("found: " + name);
+
{
 +
llListenControl(dialog_handle, TRUE);
 +
llSetTimerEvent(TIMER_TIMEOUT);
 +
llDialog(avatar_menu, MSG_CHOOSE_AV, sensor_names, dialog_channel);
 +
}
 +
}
  
        }
+
// menu selected
 +
listen(integer channel, string name, key id, string message)
 +
{
 +
llListenControl(dialog_handle, FALSE);
 +
integer index = llListFindList(sensor_names, [message]);
 +
if (index != -1)
 +
{
 +
key selected = llList2Key(sensor_keys, index);
 +
log ("capture " + llKey2Name(selected));
 +
capture(selected);
 +
}
 +
avatar_menu = NULL_KEY;
 +
}
  
        // show dialog if list contains names
+
// dialog timeout
        if (llGetListLength(sensor_names) > 0)
+
timer()
        {
+
{
            llListenControl(dialog_handle, TRUE);
+
if (avatar_menu != NULL_KEY)
            llSetTimerEvent(TIMER_TIMEOUT);
+
{
            llDialog(avatar_menu, MSG_CHOOSE_AV, sensor_names, dialog_channel);
+
out(MSG_TIMEOUT);
        }
+
avatar_menu = NULL_KEY;
    }
+
llListenControl(dialog_handle, FALSE);
 +
}
  
    // menu selected
+
llSetTimerEvent(0);
    listen(integer channel, string name, key id, string message)
+
}
    {
+
        llListenControl(dialog_handle, FALSE);
+
        integer index = llListFindList(sensor_names, [message]);
+
        if (index != -1)
+
        {
+
            key selected = llList2Key(sensor_keys, index);
+
            log ("capture " + llKey2Name(selected));
+
            capture(selected);
+
        }
+
        avatar_menu = NULL_KEY;
+
    }
+
  
    // dialog timeout
+
changed(integer change) {
    timer()
+
if(change & CHANGED_LINK)
    {
+
{
        if (avatar_menu != NULL_KEY)
+
key id = llAvatarOnSitTarget();
        {
+
if (id != NULL_KEY)
            out(MSG_TIMEOUT);
+
{
            avatar_menu = NULL_KEY;
+
avatar_victim = id;
            llListenControl(dialog_handle, FALSE);
+
lock(avatar_victim);
        }
+
log ("sitting: " + llKey2Name(avatar_victim));
 
+
state locked;
        llSetTimerEvent(0);
+
}
    }
+
}
 
+
}
    changed(integer change) {
+
        if(change & CHANGED_LINK)
+
        {
+
            key id = llAvatarOnSitTarget();
+
            if (id != NULL_KEY)
+
            {
+
                avatar_victim = id;
+
                lock(avatar_victim);
+
                log ("sitting: " + llKey2Name(avatar_victim));
+
                state locked;
+
            }
+
        }
+
    }
+
 
}
 
}
  
 
state locked
 
state locked
 
{
 
{
    state_entry()
+
state_entry()
    {
+
{
        llListen(channel, "", NULL_KEY, "DIE");
+
llListen(channel, "", NULL_KEY, "DIE");
        llSetTimerEvent(TIMER_LOCKED);
+
llSetTimerEvent(TIMER_LOCKED);
    }
+
}
  
    listen(integer channel, string name, key id, string message)
+
listen(integer channel, string name, key id, string message)
    {
+
{
        if (message == "DIE")
+
if (message == "DIE")
        {
+
{
            log ("DIE");
+
log ("DIE");
            release(avatar_victim);
+
release(avatar_victim);
        }
+
}
    }
+
}
  
    changed(integer change) {
+
changed(integer change) {
        if(change & CHANGED_LINK)
+
if(change & CHANGED_LINK)
        {
+
{
            key id = llAvatarOnSitTarget();
+
key id = llAvatarOnSitTarget();
            if (id == NULL_KEY)
+
if (id == NULL_KEY)
            {
+
{
                log ("unsitting " + llKey2Name(avatar_victim));
+
log ("unsitting " + llKey2Name(avatar_victim));
                release(avatar_victim);
+
release(avatar_victim);
                avatar_victim = id;
+
avatar_victim = id;
                state default;
+
state default;
            }
+
}
        }
+
}
    }
+
}
  
    timer()
+
timer()
    {
+
{
        release(avatar_victim);
+
release(avatar_victim);
    }
+
}
 
}</lsl>
 
}</lsl>
  

Revision as of 13:09, 15 April 2011

This is another RLV plugin I wrote. It is used in a Ball. On touch it allows to choose an avatar within a range and tries to capture the selected avatar, forcing them to sit on the ball.

<lsl>// ~capture Script V1.01 // drop into a ball // // constants integer DEBUG = FALSE;

float TIMER_TIMEOUT = 60.0; float TIMER_LOCKED = 900.0; integer RANGE_CAPTURE = 20;

integer RELAY_CHANNEL = -1812221819;

// texts string MSG_NO_SENSOR = "No avatars in sensor range."; string MSG_MENU_IN_USE = " is using the menu. Please try again in a while."; string MSG_CHOOSE_AV = "Choose avatar:"; string MSG_TIMEOUT = "Dialog timeout. Please touch again to retry.";

// internal use integer dialog_handle; integer dialog_channel; key avatar_victim = NULL_KEY; integer channel;

list sensor_keys; list sensor_names; key avatar_menu;

// write a log message in DEBUG mode log(string message) { if (DEBUG) llOwnerSay("MENU: " + message); }

// out a message to the user out(string message) { llSay(0, message); }

capture(key avatar) { relay(avatar, "@sit:" + (string)llGetKey() + "=force"); }

lock(key avatar) { relay(avatar, "@unsit=n"); }

release(key avatar) { relay(avatar, "@unsit=y"); relay(avatar, "!release"); }

// write a message to the RLV Relay relay(key avatar, string message) { if (avatar != NULL_KEY) { llSay(RELAY_CHANNEL, llGetObjectName() + "," + (string) avatar + "," + message); log("RLV: " + llGetObjectName() + "," + (string) avatar + "," + message); } }

init() { dialog_channel= -(integer)(llFrand(100000 - 10000) + 10000); dialog_handle = llListen(dialog_channel, "", NULL_KEY, ""); llListenControl(dialog_handle, FALSE); avatar_menu = NULL_KEY; }

// default state default { // on state entry: show dialog state_entry() { init(); }

on_rez(integer start_param) { channel = start_param; init(); }

// av touched me touch_start(integer total_number) { key id = llDetectedKey(0);

// check if menu is in use if (avatar_menu != NULL_KEY && id != avatar_menu) { out(llKey2Name(avatar_menu) + MSG_MENU_IN_USE); return; }

avatar_menu = id; llSensor("", NULL_KEY, AGENT, RANGE_CAPTURE, PI); }

// no av in sensor range no_sensor() { out(MSG_NO_SENSOR); }

// some av in sensor range sensor(integer total_number) { sensor_keys = []; sensor_names = []; integer i; for(i=0; i < total_number; i++) { key id = llDetectedKey(i); string name = llKey2Name(id); if (llStringLength(name) > 24) { name = llGetSubString(name, 0, 23); } sensor_keys += [id]; sensor_names += [name]; log("found: " + name); }

// show dialog if list contains names if (llGetListLength(sensor_names) > 0) { llListenControl(dialog_handle, TRUE); llSetTimerEvent(TIMER_TIMEOUT); llDialog(avatar_menu, MSG_CHOOSE_AV, sensor_names, dialog_channel); } }

// menu selected listen(integer channel, string name, key id, string message) { llListenControl(dialog_handle, FALSE); integer index = llListFindList(sensor_names, [message]); if (index != -1) { key selected = llList2Key(sensor_keys, index); log ("capture " + llKey2Name(selected)); capture(selected); } avatar_menu = NULL_KEY; }

// dialog timeout timer() { if (avatar_menu != NULL_KEY) { out(MSG_TIMEOUT); avatar_menu = NULL_KEY; llListenControl(dialog_handle, FALSE); }

llSetTimerEvent(0); }

changed(integer change) { if(change & CHANGED_LINK) { key id = llAvatarOnSitTarget(); if (id != NULL_KEY) { avatar_victim = id; lock(avatar_victim); log ("sitting: " + llKey2Name(avatar_victim)); state locked; } } } }

state locked { state_entry() { llListen(channel, "", NULL_KEY, "DIE"); llSetTimerEvent(TIMER_LOCKED); }

listen(integer channel, string name, key id, string message) { if (message == "DIE") { log ("DIE"); release(avatar_victim); } }

changed(integer change) { if(change & CHANGED_LINK) { key id = llAvatarOnSitTarget(); if (id == NULL_KEY) { log ("unsitting " + llKey2Name(avatar_victim)); release(avatar_victim); avatar_victim = id; state default; } } }

timer() { release(avatar_victim); } }</lsl>

See also: