normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / searchbert_armature_v15.3.txt
1 
2 // huffware script: searchbert armature, by fred huffhines
3 //
4 // this script serves two useful functions for searchbert:
5 // 1) this is searchbert's ease-of-use auto-repair feature.  the script puts searchbert's arms back
6 // on if they should ever get ripped off.  this happens more frequently than one might expect, but this
7 // script makes searchbert feel all better again.
8 // 2) this script also manages the exchange of resources between the root prim and the arms.  if there
9 // are updates to the search pointer script or others, they'll get synched on a periodic basis.
10 //
11 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
12 // do not use it in objects without fully realizing you are implicitly accepting that license.
13 //
14
15 //////////////
16
17 // global constants.
18
19 integer DEBUGGING = FALSE;  // set to true to make the script noisier.
20
21 integer SPECIAL_STARTUP_SAUCE = -10408;
22     // used to signal that this is a real searchbert arm, so it should evaporate on detach.
23
24 integer INVENTORY_UPDATE_INTERVAL = 6000;
25     // how often to rescan the inventory so sub-prims get the right contents, in seconds.
26
27 integer TOTAL_SEARCH_RODS = 17;  // given our current pattern, this is how many we rez.
28
29 // object maintenance variables...
30
31 integer objects_rezzed = 0;  // how many kids did we see created?
32
33 integer alerted_brainiac = FALSE;  // did we tell the brain that our arms are good?
34
35 //////////////
36
37 // exported interfaces...
38
39 // the armature button pushing API.
40 // (we have subclassed the simple button pushing API for searchbert armature.)
41 //////////////
42 integer BUTTON_PUSHER_HUFFWARE_ID = 10029;
43     // a unique ID within the huffware system for this script.
44 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
45     // this pattern is an uncommon thing to see in text, so we use it to separate
46     // our commands in link messages.
47 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
48     // used to separate lists of items from each other when stored inside a parameter.
49     // this allows lists to be passed as single string parameters if needed.
50 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
51 //////////////
52 string BUTTON_PUSHED_ALERT = "#btnp";
53     // this event is generated when the button is pushed.  the number parameter will be
54     // the huffware id plus the reply distance.  the id parameter in the link message will
55     // contain the name of the button that was pushed.
56 //////////////
57 string CHECK_ARMS_BUTTON_NAME = "^checkarms";
58     // this is the signal given to the armature script that it should check the
59     // number of arms present on searchbert.  if the number is fine, it will
60     // push the arms are good button back at the brainiac (using the button
61     // push api plus reply distance).
62 string ARMS_ARE_GOOD_BUTTON_NAME = "^goodarmz";
63     // the event sent back by the searchbert armature when all arms are ready
64     // to go.
65 string PROBLEM_WITH_MY_THUMBS_BUTTON_NAME = "^ouch";
66     // a problem was noticed with the number of arms and we could not fix it.
67     // the brain needs to try reconfiguring again.
68 //////////////
69
70 // imported interfaces...
71
72 // inventory exchanger API.
73 //////////////
74 // do not redefine these constants.
75 integer INVENTORY_EXCHANGER_HUFFWARE_ID = 10021;
76     // the unique id within the huffware system for the jaunt script to
77     // accept commands on.  this is used in llMessageLinked as the num parameter.
78 //string HUFFWARE_PARM_SEPARATOR = "{~~~}";
79     // this pattern is an uncommon thing to see in text, so we use it to separate
80     // our commands in link messages.
81 //string HUFFWARE_ITEM_SEPARATOR = "{|||}";
82     // used to separate lists of items from each other when stored inside a parameter.
83     // this allows lists to be passed as single string parameters if needed.
84 //integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
85 //////////////
86 // commands available via the library:
87 string REGISTER_ASSETS_COMMAND = "#regis#";
88     // makes the root prim's inventory exchanger track a set of items which each participating
89     // child prim should have.  this command is only available to the root prim; child prims
90     // cannot register any assets but instead can get updates on the assets.  the parameter
91     // required is a list of asset definitions, wrapped by the huffware item separator.  each
92     // asset definition is in turn a two part list wrapped with the asset field separator.
93 string ASSET_FIELD_SEPARATOR = "&&";  // separates the type from the name in our list.
94 string WILDCARD_INVENTORY_NAME = "ALL";
95     // this keyword can be used to register all of the inventory items available of the
96     // specified type.  this should be passed as the inventory item name in the second half of
97     // an asset definition list.
98 string WHACK_INVENTORY_SIGNAL = "DEL*";
99     // ensures that the child prim doesn't maintain *any* of the inventory of the type specified.
100 string AVAILABLE_ASSETS_EVENT = "#gotz#";
101     // the root prim publishes this event to list out what items it has available.  the child
102     // prims need to ask for anything that they're currently missing.  the included parameters
103     // are in the same format as the register assets command, but no wildcards will be present;
104     // all asset names will be fully expanded.
105 string REQUEST_UPDATES = "#nupd#";
106     // used by the child prim to request an updated version of an asset or assets.  the parameters
107     // should follow the register assets command.
108 string FINISHED_UPDATING = "#donq#";
109     // a signal sent from the root prim to the child prim when the root has finished updating
110     // the assets requested.  this lets the child know that it can clean out any outdated versions
111     // of items which got an update.  there are no parameters to this, because the child prim
112     // should have at most one update outstanding at a time.
113 //////////////
114
115 // startup function that gets things going.  this is not called at startup time, it
116 // has to be activated by the brain script.
117 initialize_armatures()
118 {
119     objects_rezzed = 0;
120     alerted_brainiac = FALSE;
121     check_integrity();
122 }
123
124 // starts the process of exchanging the inventory items with our arms.
125 initialize_exchanger()
126 {
127     llSetTimerEvent(5);  // check on inventory soon.
128 }
129
130 // looks for an inventory item with the same prefix as the "basename_to_seek".
131 integer find_basename_in_inventory(string basename_to_seek, integer inv_type)
132 {
133     integer num_inv = llGetInventoryNumber(inv_type);
134     if (num_inv == 0) return -1;  // nothing there!
135     integer indy;
136     for (indy = 0; indy < num_inv; indy++) {
137         if (is_prefix(llGetInventoryName(inv_type, indy), basename_to_seek))
138             return indy;
139     }
140     return -2;  // failed to find it.
141 }
142
143 // lets the searchbert brain know the arms are all ready.
144 report_arms_are_good()
145 {
146     if (!alerted_brainiac) {
147         // let the brain know we're doing okay on our arm count.
148         alerted_brainiac = TRUE;    
149         llMessageLinked(LINK_SET, BUTTON_PUSHER_HUFFWARE_ID + REPLY_DISTANCE,
150             BUTTON_PUSHED_ALERT, ARMS_ARE_GOOD_BUTTON_NAME);
151     }
152 }
153
154 // we ran into a snag, like the user didn't give us permission.  we need the
155 // controlling script to try this all again.
156 report_arms_are_bad()
157 {
158     alerted_brainiac = FALSE;
159     llSay(0, "I did not get permission to attach my seeker arms.");
160     llMessageLinked(LINK_SET, BUTTON_PUSHER_HUFFWARE_ID + REPLY_DISTANCE,
161         BUTTON_PUSHED_ALERT, PROBLEM_WITH_MY_THUMBS_BUTTON_NAME);
162 }
163
164 // returns the name of the inventory exchange script, if one is present.  we don't worry
165 // about there being more than one present; that's a different script's problem.
166 list get_exchanger_and_pointer_names()
167 {
168     list to_return;
169     integer found = find_basename_in_inventory("inventory exchanger", INVENTORY_SCRIPT);
170     if (found >= 0) {
171         to_return += [ (string)INVENTORY_SCRIPT + ASSET_FIELD_SEPARATOR
172             + llGetInventoryName(INVENTORY_SCRIPT, found) ];
173     }
174     found = find_basename_in_inventory("huff-search pointer", INVENTORY_SCRIPT);
175     if (found >= 0) {
176         to_return += [ (string)INVENTORY_SCRIPT + ASSET_FIELD_SEPARATOR
177             + llGetInventoryName(INVENTORY_SCRIPT, found) ];
178     }
179     return to_return;
180 }
181
182 // tell the root prim's inventory exchanger what to monitor.
183 post_exchangeable_assets()
184 {
185     // it's time to post.  we allow a couple seconds leeways since timers are inexact.
186     llMessageLinked(LINK_THIS, INVENTORY_EXCHANGER_HUFFWARE_ID, REGISTER_ASSETS_COMMAND,
187         // what we really want here is to make sure our pointer script is kept up to date.
188         wrap_parameters(get_exchanger_and_pointer_names() ));
189 }
190
191 // our timer handling function; this will move the search engine to the next place on
192 // the current spiral (or to the start of the next spiral) when it is called.
193 // if we're not currently running a search, then it will just periodically update the
194 // sub-prims to make sure they have the latest versions.
195 handle_timer()
196 {
197     // stop the clock.
198     llSetTimerEvent(0);
199     // if we haven't updated the inventory assets in a while, then we want
200     // to make sure the sub-prims have all the right versions of things.
201     post_exchangeable_assets();
202     // restart ticking.
203     llSetTimerEvent(INVENTORY_UPDATE_INTERVAL);
204 }
205
206 // this function is called when we have acquired permissions to manipulate this object,
207 // so we can crank up all the search rods to be our seekers.
208 got_link_permissions()
209 {
210     if ( (llGetNumberOfPrims() - 1) >= TOTAL_SEARCH_RODS) {
211         // seems like the arms are already present.
212         objects_rezzed = TOTAL_SEARCH_RODS;        
213         if (DEBUGGING) log_it("I'm done setting up my arms.");
214         // make sure we alert brain.
215         check_integrity();
216         return;
217     }
218
219     // reset our count now that we're going to reattach everything.
220     objects_rezzed = 0;
221
222     // postpone the next inventory exchange until we have all our arms on.
223     // each link addition takes at least a second of built in delay (currently).
224     llSetTimerEvent(TOTAL_SEARCH_RODS + 7);
225
226     llBreakAllLinks();  // drop all current attachments and let them evaporate.
227
228     string search_object = llGetInventoryName(INVENTORY_OBJECT, 0);
229
230     integer which_locator = 2;
231     float curr_rotation;
232     // calculate the size of the root prim so we can put the search arms in the
233     // right locations.
234     list sizes = llGetBoundingBox(llGetKey());
235     vector min = llList2Vector(sizes, 0);
236     vector max = llList2Vector(sizes, 1);
237     // not exactly sure if this formula will always be right.  have seen some anomalous
238     // results from get bounding box.
239     float radius = (max.x - min.x) / 2.0;
240
241     for (curr_rotation = 0.0; curr_rotation < PI / 4.0 + .001;
242             curr_rotation += PI / 4.0) {
243         float x = 0.0;
244         // handle dots on x axis.
245         for (x = -0.4 / 0.54 * radius; x < 0.41 / 0.54 * radius; x += 0.2 / 0.54 * radius) {
246             if ( (curr_rotation != 0.0) || (llRound(x * 100.0) != 0) ) {
247                 vector rez_place = <x, 0.0, 0.0>;
248                 rotation z_45 = llEuler2Rot(<0.0, 0.0, curr_rotation>);
249                 rez_place *= z_45;
250                 rez_place += llGetPos();
251 //log_it("x rez place " + (string)rez_place + " rot=" + (string)curr_rotation);
252                 which_locator++;
253                 llRezObject(search_object, rez_place, ZERO_VECTOR, ZERO_ROTATION,
254                     SPECIAL_STARTUP_SAUCE);
255             }
256         }
257         float y = 0.0;
258         // handle dots on y axis.
259         for (y = -0.4 / 0.54 * radius; y < 0.41 / 0.54 * radius; y += 0.2 / 0.54 * radius) {
260             if (llRound(y * 100.0) != 0) {
261                 vector rez_place = <0.0, y, 0.0>;
262                 rotation z_45 = llEuler2Rot(<0.0, 0.0, curr_rotation>);
263                 rez_place *= z_45;
264                 rez_place += llGetPos();
265 //log_it("y rez place " + (string)rez_place + " rot=" + (string)curr_rotation);
266                 which_locator++;
267                 llRezObject(search_object, rez_place, ZERO_VECTOR, ZERO_ROTATION,
268                     SPECIAL_STARTUP_SAUCE);
269             }
270         }
271     }
272 }
273
274 handle_new_child(key new_child)
275 {
276     // report (via text label) how many others there are still to hook in.
277     integer count;
278     integer remaining_to_rez = TOTAL_SEARCH_RODS - objects_rezzed;
279     string tag;
280     for (count = 0; count < remaining_to_rez; count++) tag += "+";
281     llSetText(tag + " " + (string)remaining_to_rez + " " + tag,
282         <0.4 + llFrand(0.6), 0.4 + llFrand(0.6), 0.4 + llFrand(0.6)>, 1.0);
283     // link the new kid to us.
284     // this also delays the script a whole second?!  argh.
285     llCreateLink(new_child, TRUE);
286     // check if we're done yet.
287     if (++objects_rezzed >= TOTAL_SEARCH_RODS) {
288         // that was the last one.
289         llSetText("", <0.0, 0.0, 0.0>, 0.0);
290         if (DEBUGGING) log_it("All of my arms have been rezzed and connected now.");
291         // one last check and alert the brain about us being ready.
292         check_integrity();
293     }
294 }
295
296 // processes a request for our armature services, probably from the search brain.
297 handle_link_message(integer which, integer num, string msg, key id)
298 {
299     if ( (num == BUTTON_PUSHER_HUFFWARE_ID)
300             && (msg == BUTTON_PUSHED_ALERT)
301             && (id ==  CHECK_ARMS_BUTTON_NAME) ) {
302         // see if we can put our arms on, if they're not already.
303         initialize_armatures();
304     }
305 }
306
307 // makes sure that searchbert is all assembled properly.
308 check_integrity()
309 {
310     if (llGetNumberOfPrims() - 1 < TOTAL_SEARCH_RODS) {
311         alerted_brainiac = FALSE;
312         if (DEBUGGING) log_it("I do not have enough arms on my body.  Please grant me permission to link and delink.");
313         // get permissions to take off our arms and add possibly updated ones.
314         llRequestPermissions(llGetOwner(), PERMISSION_CHANGE_LINKS);
315     } else {
316         if (!alerted_brainiac) {
317             // let the brain know we're doing okay on our arm count.
318             report_arms_are_good();
319             alerted_brainiac = TRUE;
320         }
321     }
322 }
323
324 //////////////
325 // from hufflets...
326
327 //////////////
328 // huffware script: auto-retire, by fred huffhines, version 2.5.
329 // distributed under BSD-like license.
330 //   !!  keep in mind that this code must be *copied* into another
331 //   !!  script that you wish to add auto-retirement capability to.
332 // when a script has auto_retire in it, it can be dropped into an
333 // object and the most recent version of the script will destroy
334 // all older versions.
335 //
336 // the version numbers are embedded into the script names themselves.
337 // the notation for versions uses a letter 'v', followed by two numbers
338 // in the form "major.minor".
339 // major and minor versions are implicitly considered as a floating point
340 // number that increases with each newer version of the script.  thus,
341 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
342 // and "hazmap v3.2" is a more recent version.
343 //
344 // example usage of the auto-retirement script:
345 //     default {
346 //         state_entry() {
347 //            auto_retire();  // make sure newest addition is only version of script.
348 //        }
349 //     }
350 // this script is partly based on the self-upgrading scripts from markov brodsky
351 // and jippen faddoul.
352 //////////////
353 auto_retire() {
354     string self = llGetScriptName();  // the name of this script.
355     list split = compute_basename_and_version(self);
356     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
357     string basename = llList2String(split, 0);  // script name with no version attached.
358     string version_string = llList2String(split, 1);  // the version found.
359     integer posn;
360     // find any scripts that match the basename.  they are variants of this script.
361     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
362         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
363         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
364             // found a basic match at least.
365             list inv_split = compute_basename_and_version(curr_script);
366             if (llGetListLength(inv_split) == 2) {
367                 // see if this script is more ancient.
368                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
369                 // must make sure that the retiring script is completely the identical basename;
370                 // just matching in the front doesn't make it a relative.
371                 if ( (llList2String(inv_split, 0) == basename)
372                     && ((float)inv_version_string < (float)version_string) ) {
373                     // remove script with same name from inventory that has inferior version.
374                     llRemoveInventory(curr_script);
375                 }
376             }
377         }
378     }
379 }
380 //
381 // separates the base script name and version number.  used by auto_retire.
382 list compute_basename_and_version(string to_chop_up)
383 {
384     // minimum script name is 2 characters plus a version.
385     integer space_v_posn;
386     // find the last useful space and 'v' combo.
387     for (space_v_posn = llStringLength(to_chop_up) - 3;
388         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
389         space_v_posn--) {
390         // look for space and v but do nothing else.
391     }
392     if (space_v_posn < 2) return [];  // no space found.
393     // now we zoom through the stuff after our beloved v character and find any evil
394     // space characters, which are most likely from SL having found a duplicate item
395     // name and not so helpfully renamed it for us.
396     integer indy;
397     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
398         if (llGetSubString(to_chop_up, indy, indy) == " ") {
399             // found one; zap it.  since we're going backwards we don't need to
400             // adjust the loop at all.
401             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
402         }
403     }
404     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
405     // ditch the space character for our numerical check.
406     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
407     // strip out a 'v' if there is one.
408     if (llGetSubString(chop_suffix, 0, 0) == "v")
409         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
410     // if valid floating point number and greater than zero, that works for our version.
411     string basename = to_chop_up;  // script name with no version attached.
412     if ((float)chop_suffix > 0.0) {
413         // this is a big success right here.
414         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
415         return [ basename, chop_suffix ];
416     }
417     // seems like we found nothing useful.
418     return [];
419 }
420 //
421 //////////////
422
423 integer debug_num = 0;
424 // a debugging output method.  can be disabled entirely in one place.
425 log_it(string to_say)
426 {
427     debug_num++;
428     // tell this to the owner.    
429     llWhisper(0, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
430     // say this on an unusual channel for chat if it's not intended for general public.
431 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
432     // say this on open chat that anyone can hear.  we take off the bling for this one.
433 //    llSay(0, to_say);
434 }
435
436 // joins a list of parameters using the parameter sentinel for the library.
437 string wrap_parameters(list to_flatten)
438 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
439
440 string wrap_item_list(list to_wrap)
441 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
442
443 // returns the index of the first occurrence of "pattern" inside
444 // the "full_string".  if it is not found, then a negative number is returned.
445 integer find_substring(string full_string, string pattern)
446 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
447
448 // returns TRUE if the "prefix" string is the first part of "compare_with".
449 integer is_prefix(string compare_with, string prefix)
450 { return find_substring(compare_with, prefix) == 0; }
451
452 // end hufflets.
453 //////////////
454
455 default {
456     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
457     on_rez(integer parm) { state rerun; }
458 }
459 state rerun { state_entry() { state default; } }
460
461 state real_default {
462     state_entry() {
463         auto_retire();
464         initialize_exchanger();
465     }
466     
467     on_rez(integer parm) { llResetScript(); }
468     
469     object_rez(key new_child) { handle_new_child(new_child); }
470
471     timer() { handle_timer(); }
472
473     link_message(integer which, integer num, string str, key id)
474     { handle_link_message(which, num, str, id); }
475
476     run_time_permissions(integer perm) {
477         if (perm & PERMISSION_CHANGE_LINKS)
478             got_link_permissions();
479         else
480             report_arms_are_bad();
481     }
482
483     changed(integer change) {
484         if (change & CHANGED_INVENTORY) {
485             llSetTimerEvent(2);  // fire exchange sooner in case scripts changed.
486         } else if (change & CHANGED_LINK) {
487             check_integrity();
488         }
489     }
490
491 }
492