User:Niaht Nakamichi

From Second Life Wiki
Revision as of 18:25, 28 July 2012 by Niaht Nakamichi (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

About Me

I do what it is that I do.

I am a mentor/teacher. This means I will answer specific questions about scripting, or guide you through specific subjects. This does not mean I will write your code for you. Please do not ask me to unless you intend to hire me (see below), or inspire me.

Sure, I can do work for hire, at L$27,000/hr. :P

Data type Tests

Here are some quick and dirty data type tests.. so far they work for me. They have not been optimized, most need to be replaced with *far* better methods.

isKey

Testing for valid keys is made very simple. Invalid key values won't be positively compared to the null key static NULL_KEY, where with other data types invalid values will be converted to the zero value static for that data type (or zero) and thusly will compare positively to their corresponding zero value.

To test if a given key is a valid (and non-null), the code 'if ((key)stringvar)' is sufficient.

As Strife Onizuka pointed out you can use the following code to determine if a key is valid and also take into consideration if the key is a NULL_KEY, making for a more proper isKey test. <lsl> integer isKey(key k) {

   if (k) return 2;
   return (k == NULL_KEY);

} </lsl> This works as expected for standard if(isKey("key")) tests since 'if' evaluates any integer greater than zero to TRUE. The rest of the tests below could easily be modified to behave the same way if desired, or the above isKey could be modified to simply return 1 for all valid cases.

isInteger

<lsl> integer isInteger(string s) {

   if (s != "") {
       integer i;
       integer l = llStringLength(s);
       do {
           if(! ~llListFindList(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], (list)llGetSubString(s, i, i))) return FALSE;
           ++i;
       } while (i < l);
     return TRUE;
   }
   return FALSE;

} </lsl>

isFloat

<lsl> integer isFloat(string s) {

   if (s != "") {
       integer i; integer d; string c;
       integer l = llStringLength(s);
       do {
           c = llGetSubString(s, i, i);
           if (! ~llListFindList([".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], (list)c)) return FALSE;
           if (c == ".") ++d;
           if (d > 1) return FALSE;
           ++i;
       } while (i < l);
       return TRUE;
   }
   return FALSE;

} </lsl>

isVector

Note: Uses 'isFloat' above . . . <lsl> integer isVector(string s) {

   list f;
   integer i = 0;
   if ((rotation)s) return FALSE; // don't accept rotations
   if ((vector)s) return TRUE;    // accept any positive valid
   // Everything past here is mostly for testing for a proper ZERO vector, so possibly excessive.
   if (llSubStringIndex(s, "<") == 0)
       if (llSubStringIndex(s, ">") == (llStringLength(s) - 1))
           if (llGetListLength(f = llParseString2List(s, ["<",">", " "], [","])) == 5)
               if (isFloat(llList2String(f, 0)))
                   if (llList2String(f, 1) == ",")
                       if (isFloat(llList2String(f, 0)))
                           if (llList2String(f, 3) == ",")
                               if (isFloat(llList2String(f, 4))) return TRUE;
   return FALSE;

} </lsl>

Looping

These are some examples of fancy looping. This is hardly a complete or even best set of examples.


This example might be used in the case of processing a list in reverse. <lsl> integer i = 10; while(~(i = ~ -i)) {

 // Do something

} </lsl>

The same thing as a do-while <lsl> integer i = 10 - 1; do {

 // Do something

} while (i = ~ -i); </lsl>

Object Rezzing

X-Pointed

<lsl>

       string obj = "My Object";
       vector offset = <0.75, 0.0, 0.0>;
       float velocity = 10.0;
       integer start_param = 0;
       rotation rot = llGetRot();
       llRezAtRoot(obj, llGetPos() + offset * rot, llRot2Fwd(rot) * velocity, rot, start_param);

</lsl>

Y-Pointed

<lsl>

       string obj = "My Object";
       vector offset = <0.0, 0.75, 0.0>;
       float velocity = 10.0;
       integer start_param = 0;
       rotation rot = llGetRot();
       llRezAtRoot(obj, llGetPos() + offset * rot, llRot2Left(rot) * velocity, rot, start_param);

</lsl>

Z-Pointed

<lsl>

       string obj = "My Object";
       vector offset = <0.0, 0.0, 0.75>;
       float velocity = 10.0;
       integer start_param = 0;
       rotation rot = llGetRot();
       llRezAtRoot(obj, llGetPos() + offset * rot, llRot2Up(rot) * velocity, rot, start_param);

</lsl>

Rotations

Global To Local

To convert the global rotation of a child to a local rotation <lsl>

       integer link = 2;
       rotation child_rot_local = (rotation)((string)llGetLinkPrimitiveParams(link, [PRIM_ROTATION])) / llGetRootRotation();

</lsl>

Local To Global

To convert a local rotation to a global rotation <lsl>

       rotation child_rot_global = child_rot_local * llGetRootRotation();

</lsl>

Random Number Generation

The following code handles both positive and negative numbers. Your min should be the number closest to zero, however you can span negative and positive. <lsl> integer rand_between(integer min, integer max) {

   max += (max >= 0) - (max < 0);
   return (integer)llFrand(max - min) + min;

} </lsl>

Random Listen Channels

Generate a positive random channel excluding the debug channel and a bottom series of commonly used channels. Though there really isn't a good reason to do so, you may include the debug channel, or channels down to zero, but not both or you will experience an "integer overflow" with unexpected results. <lsl> integer chan = rand_between(6000, 2147483646); </lsl>

Generate a negative random channel excluding a bottom series of commonly used channels. You may include channel zero if you drop channel max down to no more than -2147483647 (You probably don't want to do that). <lsl> integer chan = rand_between(-6000, -2147483648); </lsl>

Simple PHP Code

Someone asked for this once. It's old, lacking, but should work. <php> <?php /**

Simple PHP Form Mailer
Written by Niaht Nakamichi (2008-2010)
You are free to do whatever you want with this.
  • /

/**

  • Message Configuration
  • /

$to = 'nobody@example.com'; // Were to send the message too. $from = 'webmaster@example.com'; // Who the message is from. $reply_to = 'nobody@example.com'; // Who to reply to. $subject = 'Message sent from your website'; // The default Subject (if none is provided in the form). $thanks = 'Thanks for your message!'; // Message given after submitting the form. $thanks_url = // Set this to a valid URL if you want a redirect.

/**

  • Form settings
  • /

$display_form = true; // Set this to false if this page should not display a form $form_title = "Contact Me";

/**

  • Form field names (fill these in to match your html form if using an external form)
  • /

$subject_field = 'subject'; // Leave this blank to ONLY use default subject. $message_field = 'message';

/**

  • Form templates
  • If you know what you are doing, you can customize these.
  • /

// Thank you message template function print_thanks($title, $message) { ?>

 <html>
 <head>
   <title><?php echo $title; ?></title>
 </head>
 <body>
   <?php echo $message; ?>
 </body>
 </html>

<?php }

// Form template function print_form($title, $subject_field, $message_field) { ?>

 <html>
 <head>
   <title><?php echo $title; ?></title>
 </head>
 <html>
   <form id="mailer" method="post">
     <?php if (!empty($subject_field)): ?>
       <label for="<?php echo $subject_field ?>">Subject: </label>
       <input type="text" id="<?php echo $subject_field; ?>" name="<?php echo $subject_field; ?>">
<?php endif; ?> <label for="<?php echo $message_field; ?>">Message: </label> <textarea id="<?php echo $message_field; ?>" name="<?php echo $message_field; ?>"></textarea>
<input type="submit" id="submit" name="submit" value="Submit"> <input type="reset" id="reset"> </form>

<?php }

//************************************************************* // You probably don't need to change anything below here. //*************************************************************

// Process the post if ($_POST[$message_field]) {

   $headers = 'From: ' . $from . "\r\n" .
       'Reply-To: ' . $reply_to . "\r\n" .
       'X-Mailer: PHP/' . phpversion();
   
   if ($subject_field !=  && isset($_POST[$subject_field])) {
       $subject = strip_tags($_POST[$subject_field]);
   }
   $message = strip_tags($_POST[$message_field]);
   mail($to, $subject, $message, $headers);
   // Perform a redirect
   if (!empty($thanks_url)) {
       header('Location: ' . $thanks_url);
   }
   // Otherwise print a thank you note
   else if (!empty($thanks)) {
      print_thanks($form_title, $thanks);
   }

}

// Display the form if enabled else if ($display_form) {

   print_form($form_title, $subject_field, $message_field);

} </php>