2 // huffware script: huff-search pointer, by fred huffhines
4 // note: parts of this script were written by the attributed authors below.
6 // this script is one portion of a search system. the pointer is meant to be in a child prim.
7 // it is told what to point at by the root prim.
9 // the newer version of this supports a search command that locates objects in the
10 // direction specified.
12 // original attributions: started life as "Particle Script 0.4, Created by Ama Omega, 3-7-2004"
13 // some code in this script is also from: Christopher Omega.
15 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
16 // do not use it in objects without fully realizing you are implicitly accepting that license.
19 // huff-search pointer API:
21 // do not redefine these constants.
22 integer HUFF_SEARCH_POINTER_HUFFWARE_ID = 10032;
23 // the unique id within the huffware system for this script.
24 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
25 // this pattern is an uncommon thing to see in text, so we use it to separate
26 // our commands in link messages.
27 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
28 // used to separate lists of items from each other when stored inside a parameter.
29 // this allows lists to be passed as single string parameters if needed.
30 integer REPLY_DISTANCE = 100008; // offset added to service's huffware id in reply IDs.
32 string HUFF_SEARCH_RESET = "#reset#";
33 // returns the script to its starting state.
34 string HUFF_SEARCH_POINT_PARTY = "#point_particles#";
35 // aim at an object and show a particle stream leading to it.
36 string HUFF_SEARCH_JUST_POINT = "#just_point#";
37 // aim at an object, but don't do any particles.
38 string HUFF_SEARCH_SENSEI = "#sensor#";
39 // set up a sensor request for a search pattern. pings will cause
40 // the pattern to be sought in names of nearby objects. the parameters are:
41 // (1) the maximum range for the sensor, (2) the arc angle to use in sensing,
42 // (3) the search pattern to look for in object names, (4) the maximum number
43 // of matches to look for.
44 string HUFF_SEARCH_STOP_SENSE = "#stop_sensor#";
45 // turn off the sensor but don't totally reset.
46 string HUFF_SEARCH_PING = "#ping#";
47 // cause the searcher to actively sensor ping the targets.
48 string HUFF_SEARCH_MATCH_EVENT = "#match#";
49 // fired at the root prim when matches are found for the search term.
50 // the payload is a list of matched item pairs [item key, location].
53 // global variables...
55 // SENSOR_TYPE_ALL: a constant that tells the sensor to look for (ACTIVE|PASSIVE|AGENT).
56 integer SENSOR_TYPE_ALL = 7;
58 // AXIS_* constants, represent the unit vector 1 unit on the specified axis.
59 vector AXIS_UP = <0.0, 0.0, 1.0>;
60 vector AXIS_LEFT = <0.0, 1.0, 0.0>;
61 vector AXIS_FWD = <1.0, 0.0, 0.0>;
63 integer MAX_LIST_LEN = 17;
64 // the maximum matches we hang onto, to avoid using too many resources.
65 // this should be no longer than the number of search arms created by
66 // the brain script, but it can be less if fewer matches are needed.
70 // global variables...
72 string search_pattern;
73 // the pattern that we are hoping to find from our sensor hits.
75 list global_matches_found;
76 // a list of keys that match the specified search terms.
78 integer our_link_number = 0;
79 // set to the number for our link if we ever see ourselves as a link greater than 1.
80 // this only happens when we have been made a sub-prim and are definitely not the root prim,
81 // so it is our key for whether the prim should die if it becomes unlinked.
83 // Mask Flags - set to TRUE to enable
84 integer glow = TRUE; // Make the particles glow
85 integer bounce = FALSE; // Make particles bounce on Z plane of object
86 integer interpColor = TRUE; // Go from start to end color
87 integer interpSize = TRUE; // Go from start to end size
88 integer wind = FALSE; // Particles effected by wind
89 integer followSource = TRUE; // Particles follow the source
90 integer followVel = TRUE; // Particles turn to velocity direction
92 // Choose a pattern from the following:
93 // PSYS_SRC_PATTERN_EXPLODE
94 // PSYS_SRC_PATTERN_DROP
95 // PSYS_SRC_PATTERN_ANGLE_CONE_EMPTY
96 // PSYS_SRC_PATTERN_ANGLE_CONE
97 // PSYS_SRC_PATTERN_ANGLE
98 integer pattern = PSYS_SRC_PATTERN_DROP;
100 // Select a target for particles to go towards
101 // "" for no target, "owner" will follow object owner
102 // and "self" will target this object
103 // or put the key of an object for particles to go to
104 key target = "owner";
106 // useful particle parameters.
107 float age = 14; // Life of each particle
108 float startAlpha = 1.0; // Start alpha (transparency) value
109 float endAlpha = 1.0; // End alpha (transparency) value
110 vector startSize = <0.2, 0.2, 0.2>; // Start size of particles
111 vector endSize = <0.8, 0.8, 0.8>; // End size of particles (if interpSize == TRUE)
113 // colors are now assigned dynamically per search.
114 vector startColor; // Start color of particles <R,G,B>
115 vector endColor; // End color of particles <R,G,B> (if interpColor == TRUE)
117 // unused particle parameters.
118 float maxSpeed = 2; // Max speed each particle is spit out at
119 float minSpeed = 2; // Min speed each particle is spit out at
120 string texture; // Texture used for particles, default used if blank
121 vector push = <0.0, 0.0, 0.0>; // Force pushed on particles
124 float rate = 0.08; // burst rate to emit particles, zero is fastest.
125 float radius = 1; // Radius to emit particles for BURST pattern
126 integer count = 1; // How many particles to emit per BURST
127 float outerAngle = 1.54; // Outer angle for all ANGLE patterns
128 float innerAngle = 1.55; // Inner angle for all ANGLE patterns
129 vector omega = <0,0,0>; // Rotation of ANGLE patterns around the source
130 float life = 0; // Life in seconds for the system to make particles
133 integer precision = 2; //Adjust the precision of the generated list.
135 integer running_particles = FALSE; // is the particle system running?
137 ///integer start_parm; // set from the on_rez parameter.
139 string float2String(float in)
141 return llGetSubString((string)in, 0, precision - 7);
148 if (target == "owner") target = llGetOwner();
149 if (target == "self") target = llGetKey();
150 if (glow) flags = flags | PSYS_PART_EMISSIVE_MASK;
151 if (bounce) flags = flags | PSYS_PART_BOUNCE_MASK;
152 if (interpColor) flags = flags | PSYS_PART_INTERP_COLOR_MASK;
153 if (interpSize) flags = flags | PSYS_PART_INTERP_SCALE_MASK;
154 if (wind) flags = flags | PSYS_PART_WIND_MASK;
155 if (followSource) flags = flags | PSYS_PART_FOLLOW_SRC_MASK;
156 if (followVel) flags = flags | PSYS_PART_FOLLOW_VELOCITY_MASK;
157 if (target != "") flags = flags | PSYS_PART_TARGET_POS_MASK;
159 // original recipe searchbert pointer...
160 // startColor = <0.92, 0.79412, 0.66863>; // Start color of particles <R,G,B>
161 // endColor = <0.0, 1.0, 0.7>; // End color of particles <R,G,B> (if interpColor == TRUE)
162 // new randomized version for colors. the current aesthetic here is to start
163 // with a relatively dark color and end on a relatively light color.
164 startColor = <randomize_within_range(0.14, 0.95, FALSE),
165 randomize_within_range(0.14, 0.95, FALSE),
166 randomize_within_range(0.14, 0.95, FALSE)>;
167 endColor = <randomize_within_range(0.28, 0.95, FALSE),
168 randomize_within_range(0.28, 0.95, FALSE),
169 randomize_within_range(0.28, 0.95, FALSE)>;
171 system_content = [ PSYS_PART_MAX_AGE, age,
172 PSYS_PART_FLAGS,flags,
173 PSYS_PART_START_COLOR, startColor,
174 PSYS_PART_END_COLOR, endColor,
175 PSYS_PART_START_SCALE, startSize,
176 PSYS_PART_END_SCALE, endSize,
177 PSYS_SRC_PATTERN, pattern,
178 PSYS_SRC_BURST_RATE, rate,
179 PSYS_SRC_ACCEL, push,
180 PSYS_SRC_BURST_PART_COUNT, count,
181 PSYS_SRC_BURST_RADIUS, radius,
182 PSYS_SRC_BURST_SPEED_MIN, minSpeed,
183 PSYS_SRC_BURST_SPEED_MAX, maxSpeed,
184 // PSYS_SRC_INNERANGLE,innerAngle,
185 // PSYS_SRC_OUTERANGLE,outerAngle,
186 PSYS_SRC_OMEGA, omega,
187 PSYS_SRC_MAX_AGE, life,
188 // PSYS_SRC_TEXTURE, texture,
189 PSYS_PART_START_ALPHA, startAlpha,
190 PSYS_PART_END_ALPHA, endAlpha
192 if (target != NULL_KEY) {
193 system_content += [ PSYS_SRC_TARGET_KEY, target ];
196 llParticleSystem(system_content);
197 running_particles = TRUE;
200 // returns TRUE if the value in "to_check" specifies a legal x or y value in a sim.
201 integer valid_sim_value(float to_check)
203 if (to_check < 0.0) return FALSE;
204 if (to_check >= 257.0) return FALSE;
208 integer outside_of_sim(vector to_check)
210 return !valid_sim_value(to_check.x) || !valid_sim_value(to_check.y);
216 // In a linked set, points a child object to the rotation.
217 // @param rot The rotation to rotate to.
218 SetLocalRot(rotation rot)
220 if(llGetLinkNumber() > 1)
222 rotation locRot = llGetLocalRot();
223 locRot.s = -locRot.s; // Invert local rot.
225 rotation parentRot = locRot * llGetRot();
226 parentRot.s = -parentRot.s; // Invert parent's rot.
228 llSetLocalRot(rot * parentRot);
232 // Gets the rotation to point the specified axis at the specified position.
233 // @param axis The axis to point. Easiest to just use an AXIS_* constant.
234 // @param target The target, in region-local coordinates, to point the axis at.
235 // @return The rotation necessary to point axis at target.
236 rotation getRotToPointAxisAt(vector axis, vector target)
238 return llGetRot() * llRotBetween(axis * llGetRot(), target - llGetPos());
241 // aims in the direction of the target.
242 aim_at(vector targetPos)
244 SetLocalRot(getRotToPointAxisAt(AXIS_UP, targetPos));
247 // locates the string "text" in the list to "search_in".
248 integer find_in_list(list search_in, string text)
250 integer len = llGetListLength(search_in);
252 for (i = 0; i < len; i++) {
253 if (llList2String(search_in, i) == text)
260 // Points up axis at targetPos, and emits a particle system at targetKey.
261 // @param targetKey The UUID of the target to emit particles to.
262 // @param targetPos The poaition of the target in region-local coordinates.
263 point_at(key targetKey, vector targetPos)
270 // variables that are established by a search and used periodically in the timer.
271 float max_range = 0.0;
272 float arc_angle = 0.0;
273 float sensor_interval = 0.0;
278 llSetTimerEvent(0.0);
285 integer debug_num = 0;
287 // a debugging output method. can be disabled entirely in one place.
288 log_it(string to_say)
291 // tell this to the owner.
292 llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
293 // say this on an unusual channel for chat if it's not intended for general public.
294 // llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
295 // say this on open chat that anyone can hear. we take off the bling for this one.
299 // returns TRUE if the "pattern" is found in the "full_string".
300 integer matches_substring(string full_string, string pattern)
301 { return (find_substring(full_string, pattern) >= 0); }
303 // returns the index of the first occurrence of "pattern" inside
304 // the "full_string". if it is not found, then a negative number is returned.
305 integer find_substring(string full_string, string pattern)
306 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
308 // returns TRUE if the "prefix" string is the first part of "compare_with".
309 integer is_prefix(string compare_with, string prefix)
310 { return find_substring(compare_with, prefix) == 0; }
312 // returns a number at most maximum and at least minimum.
313 // if "allow_negative" is TRUE, then the return may be positive or negative.
314 float randomize_within_range(float minimum, float maximum, integer allow_negative)
316 float to_return = minimum + llFrand(maximum - minimum);
317 if (allow_negative) {
318 if (llFrand(1.0) < 0.5) to_return *= -1.0;
323 // makes sure that we record the current link number if it's higher than 1; this
324 // is how we know that we're a sub-prim.
325 record_link_num_if_useful()
327 if (llGetLinkNumber() > 1) our_link_number = llGetLinkNumber();
331 // huffware script: auto-retire, by fred huffhines, version 2.5.
332 // distributed under BSD-like license.
333 // !! keep in mind that this code must be *copied* into another
334 // !! script that you wish to add auto-retirement capability to.
335 // when a script has auto_retire in it, it can be dropped into an
336 // object and the most recent version of the script will destroy
337 // all older versions.
339 // the version numbers are embedded into the script names themselves.
340 // the notation for versions uses a letter 'v', followed by two numbers
341 // in the form "major.minor".
342 // major and minor versions are implicitly considered as a floating point
343 // number that increases with each newer version of the script. thus,
344 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
345 // and "hazmap v3.2" is a more recent version.
347 // example usage of the auto-retirement script:
350 // auto_retire(); // make sure newest addition is only version of script.
353 // this script is partly based on the self-upgrading scripts from markov brodsky
354 // and jippen faddoul.
357 string self = llGetScriptName(); // the name of this script.
358 list split = compute_basename_and_version(self);
359 if (llGetListLength(split) != 2) return; // nothing to do for this script.
360 string basename = llList2String(split, 0); // script name with no version attached.
361 string version_string = llList2String(split, 1); // the version found.
363 // find any scripts that match the basename. they are variants of this script.
364 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
365 //log_it("invpo=" + (string)posn);
366 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
367 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
368 // found a basic match at least.
369 list inv_split = compute_basename_and_version(curr_script);
370 if (llGetListLength(inv_split) == 2) {
371 // see if this script is more ancient.
372 string inv_version_string = llList2String(inv_split, 1); // the version found.
373 // must make sure that the retiring script is completely the identical basename;
374 // just matching in the front doesn't make it a relative.
375 if ( (llList2String(inv_split, 0) == basename)
376 && ((float)inv_version_string < (float)version_string) ) {
377 // remove script with same name from inventory that has inferior version.
378 llRemoveInventory(curr_script);
385 // separates the base script name and version number. used by auto_retire.
386 list compute_basename_and_version(string to_chop_up)
388 // minimum script name is 2 characters plus a version.
389 integer space_v_posn;
390 // find the last useful space and 'v' combo.
391 for (space_v_posn = llStringLength(to_chop_up) - 3;
392 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
394 // look for space and v but do nothing else.
395 //log_it("pos=" + (string)space_v_posn);
397 if (space_v_posn < 2) return []; // no space found.
398 //log_it("space v@" + (string)space_v_posn);
399 // now we zoom through the stuff after our beloved v character and find any evil
400 // space characters, which are most likely from SL having found a duplicate item
401 // name and not so helpfully renamed it for us.
403 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
404 //log_it("indy=" + (string)space_v_posn);
405 if (llGetSubString(to_chop_up, indy, indy) == " ") {
406 // found one; zap it. since we're going backwards we don't need to
407 // adjust the loop at all.
408 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
409 //log_it("saw case of previously redundant item, aieee. flattened: " + to_chop_up);
412 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
413 // ditch the space character for our numerical check.
414 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
415 // strip out a 'v' if there is one.
416 if (llGetSubString(chop_suffix, 0, 0) == "v")
417 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
418 // if valid floating point number and greater than zero, that works for our version.
419 string basename = to_chop_up; // script name with no version attached.
420 if ((float)chop_suffix > 0.0) {
421 // this is a big success right here.
422 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
423 return [ basename, chop_suffix ];
425 // seems like we found nothing useful.
433 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
434 on_rez(integer parm) { state rerun; }
436 state rerun { state_entry() { state default; } }
442 llParticleSystem([]);
443 running_particles = FALSE;
444 SetLocalRot(<0.0, 0.0, 0.0, 1.0>);
445 global_matches_found = [];
446 record_link_num_if_useful();
448 on_rez(integer parm) {
449 record_link_num_if_useful();
452 link_message(integer sender, integer num, string command, key parameters) {
453 if (num != HUFF_SEARCH_POINTER_HUFFWARE_ID) return; // not for us.
454 if (llGetLinkNumber() <= 1) return; // do nothing as root prim.
455 if (command == HUFF_SEARCH_RESET) {
456 // returns to the normal state of the object.
458 SetLocalRot(<0, 0, 0, 1>);
459 llParticleSystem([]);
460 running_particles = FALSE;
461 global_matches_found = [];
462 } else if (command == HUFF_SEARCH_POINT_PARTY) {
463 // aim at an object and show a particle stream leading to it.
465 list parsedParameters = llParseString2List(parameters, [HUFFWARE_PARM_SEPARATOR], []);
466 key targetKey = (key)llList2String(parsedParameters, 0);
467 vector targetPos = (vector)llList2String(parsedParameters, 1);
468 point_at(targetKey, targetPos);
469 } else if (command == HUFF_SEARCH_JUST_POINT) {
470 // aim at an object, but don't do any particles.
472 list parsedParameters = llParseString2List(parameters, [HUFFWARE_PARM_SEPARATOR], []);
473 vector targetPos = (vector)llList2String(parsedParameters, 0);
475 } else if (command == HUFF_SEARCH_SENSEI) {
476 // set up a sensor request for a search pattern. pings will cause
477 // the pattern to be sought in names of nearby objects.
479 global_matches_found = []; // reset any previous matches.
480 list parsedParameters = llParseString2List(parameters, [HUFFWARE_PARM_SEPARATOR], []);
481 max_range = (float)llList2String(parsedParameters, 0);
482 arc_angle = (float)llList2String(parsedParameters, 1);
483 search_pattern = llToLower((string)llList2String(parsedParameters, 2));
484 MAX_LIST_LEN = (integer)llList2String(parsedParameters, 3);
487 log_it("failed to get list length param");
489 } else if (command == HUFF_SEARCH_STOP_SENSE) {
490 // turn off the sensor but don't totally reset yet.
492 global_matches_found = [];
493 } else if (command == HUFF_SEARCH_PING) {
494 // do a little particle emission while searching, just to let them know
498 if (!running_particles) create_particles();
499 // they want to check for objects right here, right now...
500 llSensor("", NULL_KEY, SENSOR_TYPE_ALL, max_range, arc_angle);
503 changed(integer change) {
504 if (change & CHANGED_LINK) {
505 // we have been linked or unlinked or sat upon.
506 if ( (our_link_number > 1) && (llGetLinkNumber() == 0) ) {
507 // this is now a single prim linked to nothing.
509 } else if (our_link_number <= 1) {
510 // we had no link number recorded, so let's track this if needed.
511 record_link_num_if_useful();
515 sensor(integer num_detected) {
516 if (llGetLinkNumber() <= 1) return; // do nothing as root prim.
517 if (llGetListLength(global_matches_found) > MAX_LIST_LEN) {
518 // we have enough matches already. stop adding more.
522 list parms = []; // the full set of matches we found.
523 integer i; // loop variable.
524 integer matches_found = 0;
525 key root_key = llGetLinkKey(1);
526 for (i = 0; i < num_detected; i++) {
527 key targetKey = llDetectedKey(i);
528 string target_name = llDetectedName(i);
529 if ( (targetKey != root_key)
530 // we don't want to report our own object.
531 && matches_substring(target_name, search_pattern)
532 // check whether the current target matches our search pattern.
533 && (find_in_list(global_matches_found, targetKey) < 0) ) {
534 // make sure we haven't already reported this one.
536 //if (matches_substring(target_name, "searchbert v")) {
537 //log_it("somehow got past the is it myself check with a no-answer, my key=" + llGetKey() + " their key=" + targetKey);
540 // store the match now. even if we don't like where it's located
541 // (as in, outside our sim), we still don't want to keep matching it
542 // and looking at it.
543 global_matches_found += targetKey;
545 // the name matched the search pattern... but make sure the
546 // location is worth reporting; if it's outside the sim, we will
547 // not be able to name it or point at it properly.
548 vector location = llDetectedPos(i);
549 if (!outside_of_sim(location)) {
550 // it's a match that's inside the sim. send it along.
551 parms += [ targetKey, location ];
552 matches_found++; // we got one!
553 //log_it("match added: " + (string)targetKey);
554 // shorten lists of matches so we don't overload the brain.
555 if (llGetListLength(parms) >= 8) {
556 llMessageLinked(LINK_ROOT,
557 HUFF_SEARCH_POINTER_HUFFWARE_ID + REPLY_DISTANCE,
558 HUFF_SEARCH_MATCH_EVENT,
559 llDumpList2String(parms, HUFFWARE_PARM_SEPARATOR));
567 //log_it("sending " + (string)matches_found + " matches in message...");
568 // send message about matches back to parent.
569 llMessageLinked(LINK_ROOT, HUFF_SEARCH_POINTER_HUFFWARE_ID + REPLY_DISTANCE,
570 HUFF_SEARCH_MATCH_EVENT,
571 llDumpList2String(parms, HUFFWARE_PARM_SEPARATOR));