Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

simple lap timer script

Whispering Hush
Join date: 20 Mar 2007
Posts: 277
09-09-2007 02:52
Hi gang,

I've been working on a lap timer script, and having some fun with it. I'm using a strided list and sortingto find the last lap, and indexing to find the previous.

Here's the code i've come up with so far.

CODE


float mytime;
float previous;
float laptime;

list laps;
list times;
list timestamp;

string me;
float yx;

vector time()
{
timestamp=[];
timestamp = (timestamp=[]) + timestamp + llParseString2List( llGetTimestamp(), ["T",":","Z"] , [] );
return < llList2Float( timestamp, 1 ),
llList2Float( timestamp, 2 ),
llList2Float( timestamp, 3 ) >;
}


default
{
on_rez (integer stuff)
{
llResetScript();
}
state_entry()
{

llVolumeDetect(TRUE);
llSetTimerEvent(7.0);

}


collision_start(integer ouch)
{
if (llDetectedType(0) & SCRIPTED )
{
string x = llKey2Name(llDetectedOwner(0));
llCollisionFilter(x, NULL_KEY , TRUE);
vector y = time();
yx = (float)y.x;
float secondtime = ((float)(yx * 60)) + (float)y.z;
times = (times=[]) + times + [secondtime, x];
times = llListSort(times, 2, 0);
string searchFor = x;
integer index = llListFindList( times, [searchFor] );
me = llList2String(times, index);
mytime = llList2Float(times, index -1);
previous = llList2Float(times, index +1);
laptime = mytime - previous;
llOwnerSay( me + " completed a lap in " + (string)laptime);

}
}

collision_end(integer whew)
{


}

timer()
{
llCollisionFilter("", NULL_KEY, TRUE);
}

}



it works fine for a few test runs, then stops adding strides to the list.

here's some output.

[2:45] lap timer gate: Whispering Hush completed a lap in 576.563660
[2:45] lap timer gate: Whispering Hush completed a lap in 6.870422
[2:45] lap timer gate: Whispering Hush completed a lap in 6.633179
[2:45] lap timer gate: Whispering Hush completed a lap in 6.873352
[2:46] lap timer gate: Whispering Hush completed a lap in 6.873352
[2:46] lap timer gate: Whispering Hush completed a lap in 6.873352

Any clues or comments appreciated.

bump

Whisper.
Mystical Cookie
Registered User
Join date: 10 Feb 2006
Posts: 38
09-09-2007 10:48
I have inserted copious comments and debugging stuff. Please trim the fat as necessary. :D

CODE

// Lap Timer 0.1
// by Mystical Cookie, 2007 September
//
// Do not use this script for critical applications. Obtaining time differences via llGetTimestamp is difficult. :)
//
// Timer event, hovertext references, touch_start, and debug things should be removed before deploying this script.
//
// Set collider type in collision_start event. Currently set to AGENT|SCRIPTED for debugging.
//
// Expiration of list entries is not necessary, it will happen automatically.
//
// Hovertext listing should only be used during debugging, it will skew lap times.

// Config
integer max_racers = 20; // maximum how many racers to keep track of

integer min_time = 7; // in seconds
integer max_time = 60; // in seconds

integer show_hovertext = 1; // display racer list in hovertext?

integer DEBUG = 1;

// Script vars
list racers; // [(float)second_time, (string)owner_name]





float get_seconds () {
// http://www.lslwiki.net/lslwiki/wakka.php?wakka=llGetTimestamp
// A function that returns a vector in the form of <hours,minutes,seconds> using llGetTimestamp()
// Keknehv Psaltery 06/15/05
list timestamp = llParseString2List( llGetTimestamp(), ["T",":","Z"] , [] );
vector time_vec = < llList2Float( timestamp, 1 )
, llList2Float( timestamp, 2 )
, llList2Float( timestamp, 3 )
>;

// we only need minutes + seconds
return (time_vec.y * 60.0) + time_vec.z;
}





default
{
on_rez (integer start_param) {
// clear racers list
racers = [];
}

state_entry () {
// enable volume detection
llVolumeDetect(TRUE);

// show racer list in hovertext?
if (show_hovertext == 1) llSetTimerEvent(1);
}

touch_start (integer num) {
integer t;
for (t = 0; t < num; t++) {
if (llDetectedKey(t) == llGetOwner()) {
// DEBUG
llOwnerSay("Total racer entries: " + (string)(llGetListLength(racers) / 2) + ", Free memory: " + (string)llGetFreeMemory() );
}
}
}

collision_start (integer num) {
integer t;
for (t = 0; t < num; t++) {
if (llDetectedType(t) & (AGENT|SCRIPTED) ) {
// get the collider's owner's name
string owner_name = llKey2Name(llDetectedOwner(t));

// get current time
float second_time = get_seconds();

// already tracking this collider?
integer idx = llListFindList(racers, [owner_name]);

// new collider, add to list and record time
if (idx == -1) {
racers = (racers=[]) + racers + [second_time, owner_name];

// DEBUG
if (DEBUG == 1) llOwnerSay("New collider recorded: " + owner_name);
}

// we already have this collider
else {
// get this collider's recorded entry time
float entry_time = llList2Float(racers, idx - 1);

// time elapsed?
float time_elapsed = second_time - entry_time;

// if minimum time has not passed OR maximum time elapsed, replace entry time in list
if (time_elapsed < min_time || time_elapsed > max_time) {
racers = llListReplaceList( (racers=[]) + racers, [second_time], idx - 1, idx - 1);

// DEBUG
if (DEBUG == 1) llOwnerSay("Replacing collider recorded time: " + owner_name + " (Time elapsed: " + (string)time_elapsed + ")");
}

// enough time has elapsed, remove list item and report time elapsed
else {
// remove the list item
racers = llDeleteSubList( (racers=[]) + racers, idx - 1, idx);

// report time elapsed
llOwnerSay(owner_name + " completed a lap in " + (string)time_elapsed + " seconds.");
}
}
}

// prune racer list, don't run out of memory! :)
while (llGetListLength(racers) > max_racers) racers = llDeleteSubList( (racers=[]) + racers, 0, 1);
}
}

timer () {
string hovertext;

// get current time
float second_time = get_seconds();

// build the hovertext string
integer t;
for (t = 0; t < llGetListLength(racers); t = t+2) {
integer entry_seconds = llList2Integer(racers, t);
string racer_name = llList2String(racers, t+1);

// calculate time elapsed
integer secs_elapsed = (integer)second_time - entry_seconds;

// append hovertext string
hovertext = (hovertext="") + hovertext + "\n" + racer_name + ": " + (string)secs_elapsed + " secs";
}

// set the hovertext
llSetText(hovertext, <1,1,1>, 1.0);
}
}