User:Omei Qunhua/Example Scripts
Compute a negative channel based on user id <lsl> // Global:- integer gChannel;
//state_entry etc. :-
gChannel = 0x80000000 | (integer) ( "0x" + (string) llGetOwner() );
</lsl>
Compute a random integer
<lsl> // 16777216 is the largest positive integer that can be accurately represented in a float in LSL // Using llFrand with larger number will start producing even numbers only, then eventually numbers divisible by 4, then 8 ... etc.
integer iRand = (integer) llFrand(16777216); </lsl>
Detect a long mouse click+hold for accessing special functionality
<lsl> default {
touch_start(integer num_detected) {
llResetTime();
} touch(integer num_detected) {
if (llDetectedKey(0) == llGetOwner() && llGetTime() > 1.0) { // The owner has touched this object for longer than 1 second // execute some special feature such as issuing a management dialog // ... }
} touch_end(integer num_detected) {
if (llGetTime() < 1.0) { // The user did a normal quick click on the object // execute actions for normal clicks // ... }
}
} </lsl>
Convert a Unix Time (in Seconds) to a Date+Time string including Day-of-Week and GMT/BST indication
<lsl> list weekdays = ["Sun ", "Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat "];
// Convert a Unix time in seconds to Day-of-week + YYYY-MM-DD HH:MM:SS + GMT or BST string Unix2GMT_BST(integer insecs) {
string str = Convert(insecs); if (llGetSubString(str, -3, -1) == "BST") // if the result indicates BST ... return Convert(insecs + 3600); // ... Recompute at 1 hour later return str;
}
string Convert(integer insecs) {
integer w; integer month; integer daysinyear; integer mins = insecs / 60; integer secs = insecs % 60; integer hours = mins / 60; mins = mins % 60; integer days = hours / 24; hours = hours % 24; integer DayOfWeek = (days + 4) % 7; // 0=Sun thru 6=Sat
integer years = 1970 + 4 * (days / 1461); days = days % 1461; // number of days into a 4-year cycle
@loop; integer leap = !(years % 4); daysinyear = 365 + leap; if (days >= daysinyear) { days -= daysinyear; ++years; jump loop; } ++days; list DaysPerMonth = [31, (28 + leap ), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
for (w = month = 0; days > w; ++month) { days -= w; w = llList2Integer (DaysPerMonth, month); } string str = ((string) years + "-" + llGetSubString ("0" + (string) month, -2, -1) + "-" + llGetSubString ("0" + (string) days, -2, -1) + " " + llGetSubString ("0" + (string) hours, -2, -1) + ":" + llGetSubString ("0" + (string) mins, -2, -1) ); str = llList2String(weekdays, DayOfWeek) + str;
string GMT_BST = " GMT"; integer LastSunday = days - DayOfWeek; // BST is from the last Sunday in March to the last Sunday in October if (month == 3 && LastSunday > 24) GMT_BST = " BST"; if (month > 3) GMT_BST = " BST"; if (month == 10 && LastSunday > 24) GMT_BST = " GMT"; if (month > 10) GMT_BST = " GMT"; return (str + GMT_BST);
}
// Examples
llSay(0, Unix2GMT_BST(llGetUnixTime() ) );
llSay(0, Unix2GMT_BST(975153600) ); // produces: Sat 2000-11-25 12:00 GMT
llSay(0, Unix2GMT_BST(2027678400) ); // produces: Mon 2034-04-03 13:00 BST
</lsl>