Script sensor

From The SchomEmunity Wiki
Jump to: navigation, search

Script to sense avatars in Second Life. Top level variables are used to set frequency of scan, range, and target email (current version periodically - .ie. when array is about to overflow - emails data to the target email).

Fairly well commented, but any comments/questions can be sent to Dan.

Dan- quick hack - use the <pre> </pre> html tag above and below your code snippet. Does the below look ok for you? cheers, --Mgaved 16:44, 23 January 2007 (GMT)

superb, works perfectly --Dan 17:20, 23 January 2007 (GMT)



integer strides=0;
list visitors=[];
integer distance = 40; // distance to sense in metres
integer timeperiod = 60; // how frequently to sense, in seconds
string myemail = "youremailgoeshere";  // put your email in here
string sensorlocation = "areanamegoeshere" ;  // name the area being scanned


 default
 {
    touch_start(integer total_number)
    {
        // repeats 360 degree sweep over distance metres every timeperiod seconds 
        llSensorRepeat("", NULL_KEY, AGENT, distance, PI, timeperiod);
        
        // can be used to check the current state of the visitors list
        llListen(90,"",llGetOwner(),"");
    }

    sensor(integer total_number) // total_number is the number of avatars detected.
    {
        //llSay(0, (string)total_number + " avatars detected" );

        if ( (strides + total_number)*3 > 70 ) 
        {
            // build & send email message of comma separated variables in form
        // avatarname,date,time 
        // followed by an end of line character (\n)

            string emailMessage;
            integer r;
            integer n;
            for (r=0; r<llGetListLength(visitors); r++) 
            {
                emailMessage += llList2String(visitors,r);
                n++;
                if (n==3)
                {
                    emailMessage += "\n";
                    n=0;        
                }
                else
                {
                    emailMessage += ",";
                }      
            }
            
            llEmail(myemail, sensorlocation, emailMessage);
            
            // clear visitors list and stride count
            strides = 0;
            visitors = [];    
        }


    // establish current time and parse into date and time strings
    // parses current time stamp into two element list of [date, time]
    // date is in form YYYY-MM-DD, time is in form hh:mm:ssZ
    list parsed1 = llParseString2List(llGetTimestamp(), ["T"], []);

    // parse and turn around to form DD-MM-YYYY
    list d1 = llParseString2List(llList2String(parsed1,0), ["-"], []);
    string currentDate = llList2String(d1,2) + "-" + llList2String(d1,1) + "-" + llList2String(d1,0);

    // parse time into form dd:mm
    list t1 = llParseString2List(llList2String(parsed1,1), [":"], []);
    string currentTime =  llList2String(t1,0) + ":" + llList2String(t1,1);

    // now put the detected avatars into the visitors array, along with the formatted date and time
        integer i;
        for (i = 0; i < total_number; i++)
        {        
            visitors += [llDetectedName(i), currentDate, currentTime];
            strides++;
        }
    }
    
    // if nobody is within 10 meters, say so.
    no_sensor() {
        //llSay(0, "Nobody is around.");
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (llToLower(message) == "status")
        {
            llSay(0,"Status of visitors list is:"); 
            integer r;
            for (r=0; r<llGetListLength(visitors); r++)
            {
                llSay(0,llList2String(visitors,r));
            }
        }
    }
}