Forward Avatar Intersect

Code summary: 

Detects avatars whose bounding box intersects with a straight line.

// Forward sector avatar intersect
// Ordinal Malaprop
// 2006-09-22

// Should report the first avatar whose bounding box "sort of"
// intersects the forward vector of the toucher, adjusted for eye level.

// An assumption is made for simplicity that the box is actually a
// cylinder; this won't make any real difference in practice.

default
{
   touch_start(integer n)
   {
      llSensor("", NULL_KEY, AGENT, 96.0, PI_BY_TWO);
   }

   sensor(integer n)
   {
      // Adjust position for eye level
      vector mypos = llGetPos() + <0.0, 0.0, 0.75>;
      // Current forward vector
      vector fwd = llRot2Fwd(llGetRot());
      integer f = 0;
      key target = NULL_KEY; // key of identified target
      do {
         key id = llDetectedKey(f);
         // This returns a list; we assume the avatar has
         // a bounding box that is symmetrical about their axes
         // and calculate its size based on the maximum corner.
         // Diameter of the "cylinder" is based on width.
         list box_list = llGetBoundingBox(id);
         vector pos = llDetectedPos(f);
         vector box = llList2Vector(box_list, 1);
         // Nearest point along the forward axis to the target's
         // position
         vector nearest = fwd * (fwd * (pos - mypos)) + mypos;
         // Find the distances of this from target pos on the XY plane
         // and the Z axis
         float z_diff = llVecMag(<0.0, 0.0, nearest.z> - <0.0, 0.0,
pos.z>);
         float xy_diff = llVecMag(<nearest.x, nearest.y, 0.0> - <pos.x,
pos.y, 0.0>);
         llOwnerSay("Checking " + llDetectedName(f) + " @ " +
(string)pos + ", nearest pos " + (string)nearest + ", z_diff="
+ (string)z_diff + ", xy_diff=" + (string)xy_diff + ", box=" +
(string)box);
         if (xy_diff <= (box.y + box.x) / 2 && z_diff <= box.z) {
            // projection of forward vector within box
            target = id;
            llOwnerSay("Hit " + llDetectedName(0));
         }
      } while (++f < n && target == NULL_KEY);
      if (target == NULL_KEY) {
         llOwnerSay("Missed");
      }
   }

   no_sensor()
   {
      llOwnerSay("Nothing here, you fool");
   }
}