06-21-2006 15:04
NOTE: As used here, 'Developer Library' is a collection of general purpose functions commonly used by developers

RLib - A LSL Developer Library
Created by Hitani Fugazi


RLib is designed to make your lives easier, by allowing you to write in LSL logically, without having to stress over unimportant, yet essential details. Simply copy the RLib source to the top of any script you wish to use it in, and you will be able to use all of these functions with ease. RLib has been written carefully, taking every possible precaution to make sure that it is functional, flexible, and will not clutter your code.

Questions, comments, and concerns to:
[email]instinkt.aka.ink@gmail.com[/email]


Function List

string rl_GetFirstName ( key id )
Retrieves the first name of an avatar

Parameters:
  1. id - key of the avatar

Returns:
  1. The first name of the avatar specified, as a string




string rl_GetLastName ( key id )
Retrievs the last name of an avatar

Parameters:
  1. id - key of the avatar

Returns:
  1. The last name of the avatar specified, as a string




list rl_Explode ( string str, string pattern )
Seperates a string into a list using a pattern

Parameters:
  1. str - The string to seperate
  2. pattern - The pattern to seperate at

Returns:
  1. The parts of the string in a list

Example:
  1. rl_Explode( "a b c 1 2 3", " " ) = [ "a", "b", "c", "1", "2", "3" ]




string rl_Replace ( string str, string pattern, string alternative )
Replaces a pattern in a string with an alternative pattern

Parameters:
  1. str - The string
  2. pattern - The pattern to find
  3. alternative - The string to replace pattern with

Returns:
  1. The string with pattern replaced by alternative

Example:
  1. rl_Replace( "the bar", "the", "foo" ) = "foo bar"
  2. rl_Replace( "Welcome, come right in!", "come", "fly" ) = "Welfly, fly right in!"




integer rl_RandomInteger ( integer min, integer max )
Retrieves a random integer between min and max, inclusively

Parameters:
  1. min - The minimum value of the integer
  2. max - The maximum value of the integer

Returns:
  1. A random integer from min to max




string rl_Implode ( list peices, string glue )
Combines a list of strings into a single string, with a pattern inbetween each one

Parameters:
  1. peices - The list of strings
  2. glue - The pattern to place inbetween each value in peices

Returns:
  1. A string with all values of peices in order

Example:
  1. rl_Implode( [ "a", "b", "c" ], "-" ) = "a-b-c"
  2. rl_Implode( [ "t", "e", "s", "t" ], "" ) = "test"
  3. rl_Implode( rl_Explode( "1-2-3-4-5-6-7-8-9-0", "-" ), "-" ) = "1-2-3-4-5-6-7-8-9-0"




list rl_Int2Bin ( integer num )
Converts a signed 32-bit integer into a list of integers representing each bit

Parameters:
  1. num - An integer to convert

Returns:
  1. A list of 'bits' ( integers with a value of 1 or 0 )

Example:
  1. rl_Int2Bin( 32 ) = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  2. rl_Int2Bin( 21474836487 ) = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Notes:
  1. Signed integers are not supported by LSL. Becuase of this, num has a maximum value of 21474836487
  2. Although the maximum value uses only 31 bits, to prevent incompatibility with future updates, the list returned by this function will always have a length of 32
  3. An integer with a value of 0 is FALSE
  4. An integer with a value of 1 is TRUE
  5. 'if ( a == TRUE )' is the same as 'if ( a )'




integer rl_Bin2Int ( list bin )
Converts a list of bits into a signed integer

Parameters:
  1. bin - A list of bits, either TRUE ( 1 ) or FALSE ( 0 )

Returns:
  1. An integer represented by the bits provided

Example:
  1. rl_Bin2Int( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ) = 32
  2. rl_Bin2Int( [1, 0, 0, 0, 0, 0] ) = 32

Notes:
  1. The list bin does not have to have a length of 32, any extra FALSE values proceeding the first occurrence of 1 in the list may be omitted
  2. The list bin cannot have a length higher than 31
  3. Regardless of the length of the length of bin, 'llList2Key( bin, llGetListLength( bin ) - 1 )' should represent 'llPow( 2, 0 )', or 2 to the 0 power


Source:
Place this at the top of any script to use it there
CODE
string rl_GetFirstName ( key id )
{
string name = llKey2Name( id );
integer name_split = llSubStringIndex( name, " " ) - 1;
string first_name = llGetSubString( name, 0, name_split );
return first_name;
}

string rl_GetLastName ( key id )
{
string name = llKey2Name( id );
integer name_split = llSubStringIndex( name, " " ) + 1;
integer name_length = llStringLength( name ) - 1;
string last_name = llGetSubString( name, name_split, name_length );
return last_name;
}

list rl_Explode ( string str, string pattern )
{
list temp = [];
integer CONTINUE = TRUE;
do
{
if ( llSubStringIndex( str, pattern ) == -1 )
{
integer loc = llSubStringIndex( str, pattern ) - 1;
integer len = llStringLength( str );
temp += llGetSubString( str, 0, loc );
str = llGetSubString( str, loc + llStringLength( pattern ), len );
}
else
{
temp += str;
CONTINUE = FALSE;
}
} while ( CONTINUE );
return temp;
}

string rl_Replace ( string str, string pattern, string alternative )
{
integer CONTINUE = TRUE;
do
{
if ( llSubStringIndex( str, pattern ) != -1 )
{
integer loc = llSubStringIndex( str, pattern ) - 1;
integer len = llStringLength( str );
string a = llGetSubString( str, 0, loc );
string b = llGetSubString( str, loc + llStringLength( pattern ) + 1, len );
str = a + alternative + b;
}
else
{
CONTINUE = FALSE;
}
} while ( CONTINUE );
return str;
}

integer rl_RandomInteger ( integer min, integer max )
{
integer choices = max - min + 1;
float random = llFrand( choices );
integer result = llCeil( random );
if ( result == 0 )
{
return rl_RandomInteger ( min, max );
}
result += ( min - 1 );
return result;
}

string rl_Implode ( list peices, string glue )
{
integer length = llGetListLength( peices );
integer i;
string result = "";
for ( i = 0; i < length; i++ )
{
result += llList2String( peices, i );
}
return result;
}

list rl_Int2Bin ( integer num )
{
integer i;
list binary = [];
integer bit;
for ( i = 31; i >= 0; i-- )
{
bit = llFloor( llPow( 2, i ) );
if ( num >= bit )
{
num -= bit;
binary += [TRUE];
}
else
{
binary += [FALSE];
}
}
return binary;
}

integer rl_Bin2Int ( list bin )
{
integer val = 0;
integer length = llGetListLength( bin );
integer i;
for ( i = 0; i < length; i++ )
{
if ( llList2Integer( bin, i ) == TRUE )
{
val += llFloor( llPow( 2, ( ( length - 1 ) - i ) ) );
}
}
return val;
}