RLib - A LSL Developer Library
Created by Hitani Fugazi
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:
- id - key of the avatar
Returns:
- The first name of the avatar specified, as a string
string rl_GetLastName ( key id )
Retrievs the last name of an avatar
Parameters:
- id - key of the avatar
Returns:
- 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:
- str - The string to seperate
- pattern - The pattern to seperate at
Returns:
- The parts of the string in a list
Example:
- 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:
- str - The string
- pattern - The pattern to find
- alternative - The string to replace pattern with
Returns:
- The string with pattern replaced by alternative
Example:
- rl_Replace( "the bar", "the", "foo" ) = "foo bar"
- 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:
- min - The minimum value of the integer
- max - The maximum value of the integer
Returns:
- 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:
- peices - The list of strings
- glue - The pattern to place inbetween each value in peices
Returns:
- A string with all values of peices in order
Example:
- rl_Implode( [ "a", "b", "c" ], "-" ) = "a-b-c"
- rl_Implode( [ "t", "e", "s", "t" ], "" ) = "test"
- 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:
- num - An integer to convert
Returns:
- A list of 'bits' ( integers with a value of 1 or 0 )
Example:
- 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]
- 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:
- Signed integers are not supported by LSL. Becuase of this, num has a maximum value of 21474836487
- 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
- An integer with a value of 0 is FALSE
- An integer with a value of 1 is TRUE
- 'if ( a == TRUE )' is the same as 'if ( a )'
integer rl_Bin2Int ( list bin )
Converts a list of bits into a signed integer
Parameters:
- bin - A list of bits, either TRUE ( 1 ) or FALSE ( 0 )
Returns:
- An integer represented by the bits provided
Example:
- 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
- rl_Bin2Int( [1, 0, 0, 0, 0, 0] ) = 32
Notes:
- 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
- The list bin cannot have a length higher than 31
- 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;
}