2 // huffware script: wylie controller, by fred huffhines and wam7c macchi.
4 // accepts wylie input and distributes the command to the various parts of the letter.
6 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
7 // do not use it in objects without fully realizing you are implicitly accepting that license.
10 // configurable values... the settings below are just defaults.
12 integer LISTENING_CHANNEL = 0; // the channel where we'll listen for commands.
13 integer ONLY_LISTEN_TO_OWNER = FALSE; // if true, only the owner's voice counts.
14 integer SHOW_CHANNEL = TRUE; // puts a title over our heads if true.
15 vector TITLE_COLOR = <1.0, 1.0, 1.0>; // the color the title should be.
17 integer DEBUGGING = FALSE; // if this is true, then the debugging will be noisy.
19 // constants... do not change these.
21 integer STARTUP_CODE_NOTECARD_RETRIES = 14042;
22 // secret code used to launch the wylie writers with many notecard retries.
24 // channels for speech / link messages.
25 integer LOWER_VOWEL_CHANNEL = 11008;
26 integer CONSONTANT_CHANNEL = 21008;
27 integer UPPER_VOWEL_CHANNEL = 31008;
29 string RESET_TEXTURE_WORD = "reset-texture";
30 // tells the letter blocks to go back to default texture.
31 string BROADCAST_WORD = "bcast";
32 // used to tell many wylie writers to do something at the same time.
34 string WYLIE_NOTEWORTHY_SIGNATURE = "#wylie";
35 // the notecard must begin with this as its first line for it to be
36 // recognized as our configuration card.
38 integer SNOOZE_BEFORE_CONFIG_RESET = 14;
39 // number of seconds to wait before trying to read notecard again.
41 // requires noteworthy library v10.2 or better.
43 // do not redefine these constants.
44 integer NOTEWORTHY_HUFFWARE_ID = 10010;
45 // the unique id within the huffware system for the noteworthy script to
46 // accept commands on. this is used in llMessageLinked as the num parameter.
47 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
48 // this pattern is an uncommon thing to see in text, so we use it to separate
49 // our commands in link messages.
50 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
51 // used to separate lists of items from each other when stored inside a parameter.
52 // this allows lists to be passed as single string parameters if needed.
53 integer REPLY_DISTANCE = 100008; // offset added to service's huffware id in reply IDs.
54 string NOTECARD_READ_CONTINUATION = "continue!";
55 // returned as first parameter if there is still more data to handle.
56 string BAD_NOTECARD_INDICATOR = "bad_notecard";
57 // indicates that the notecard reading process has failed to find an appropriate one.
58 string BUSY_READING_INDICATOR = "busy_already";
59 // this return value indicates that the script is already in use by some other script.
60 // the calling script should try again later.
61 // commands available via the noteworthy library:
62 string READ_NOTECARD_COMMAND = "#read_note#";
63 // command used to tell the script to read notecards. needs a signature to find
64 // in the card as the first parameter, and a randomly generated response code for
65 // the second parameter. the response code is used to uniquely identify a set of
66 // pending notecard readings (hopefully). the signature can be empty or missing.
67 // the results will be fired back as the string value returned, which will have
68 // as first element the notecard's name (or "bad_notecard" if none was found) and
69 // as subsequent elements an embedded list that was read from the notecard. this
70 // necessarily limits the size of the notecards that we can read and return.
74 // global variables that track notecard reading and any configuration found.
75 string global_notecard_name; // name of our notecard in the object's inventory.
76 integer response_code; // set to uniquely identify the notecard read in progress.
77 list global_config_list; // a collection of configuration parameters from our notecard.
78 integer global_config_index; // allows wrap-around feature, which we don't use here.
80 integer startup_parm = 0; // the startup parameter for the script.
81 integer retries_allowed = 1; // number of attempts to read notecards.
83 // sets the object up.
86 // listen for commands from the appropriate person(s) on the right channel.
88 if (ONLY_LISTEN_TO_OWNER) id = llGetOwner();
89 llListen(LISTENING_CHANNEL, "", id, "");
90 // display a text title if we were configured to.
91 float title_alpha = 1.0;
92 if (!SHOW_CHANNEL || !LISTENING_CHANNEL) title_alpha = 0.0;
93 llSetText("/" + (string)LISTENING_CHANNEL, TITLE_COLOR, title_alpha);
94 if (LISTENING_CHANNEL) {
95 // if we're not on open chat listen, then we still peek out on the
96 // open chat channel for certain instructions.
97 llListen(0, "", id, "");
99 llSetTimerEvent(SNOOZE_BEFORE_CONFIG_RESET); // snooze a bit, and try reading again.
101 if (startup_parm == STARTUP_CODE_NOTECARD_RETRIES) {
102 ///hmmm: parameterize
103 retries_allowed = 11;
104 startup_parm = -3; // no longer allowed to hit unless we get restarted.
109 // tells all the character blocks to go back to their default texture.
112 set_texture(LOWER_VOWEL_CHANNEL, RESET_TEXTURE_WORD);
113 set_texture(CONSONTANT_CHANNEL, RESET_TEXTURE_WORD);
114 set_texture(UPPER_VOWEL_CHANNEL, RESET_TEXTURE_WORD);
117 // makes one of the blocks show the tibetan character, if it knows it.
118 set_texture(integer block_number, string tib_char_wylie)
120 llMessageLinked(LINK_SET, block_number, tib_char_wylie, NULL_KEY);
123 // find the vowel in the string provided and separate the word into its
124 // components for each of the blocks (in the order: upper, middle, lower).
125 list consume_wylie(string maybe_wylie_in)
127 string maybe_wylie = llToLower(maybe_wylie_in); // drop to lower case.
128 // if it has a space in it, then it's not for us.
129 integer posn = llSubStringIndex(maybe_wylie, " ");
130 if (posn >= 0) return []; // wrong answer.
131 list vowels = [ "a", "e", "i", "o", "u" ];
133 for (vow_indy = 0; vow_indy < llGetListLength(vowels); vow_indy++) {
134 string curr_vow = llList2String(vowels, vow_indy);
135 posn = find_substring(maybe_wylie, curr_vow);
138 // log_it("found the vowel " + curr_vow);
140 //hmmm: here is where better logic would be needed to support multi-vowel words.
142 //hmmm: here is also where better logic needed for processing words, with multiple chars.
143 // that would just require splitting the remainder off instead of having the rule about
144 // vowel must be at end.
146 // compute the consonant portion.
147 string consonant = llGetSubString(maybe_wylie, 0, posn - 1) + "a";
148 // we guess that the consonant is about right already.
149 // dratted special cases for specific characters.
150 if (maybe_wylie == curr_vow + "h") consonant = "ah";
151 else if (maybe_wylie == curr_vow) consonant = "a";
152 // log_it("consonant now is " + consonant);
154 // now calculate the right vowel(s) and where they live.
157 if (curr_vow == "a") {
158 // no extra vowel markers, since there's an 'a'.
159 lower_vowel = RESET_TEXTURE_WORD;
160 upper_vowel = RESET_TEXTURE_WORD;
161 } else if (curr_vow == "u") {
162 // our one vowel that goes below.
163 lower_vowel = curr_vow;
164 upper_vowel = RESET_TEXTURE_WORD;
166 // mainstream vowels fly high.
167 lower_vowel = RESET_TEXTURE_WORD;
168 upper_vowel = curr_vow;
170 // log_it("success, answering: " + upper_vowel + " / " + consonant + " / " + lower_vowel);
171 return [ upper_vowel, consonant, lower_vowel ];
175 // no vowel was found!?
181 // joins a list of parameters using the parameter sentinel for the library.
182 string wrap_parameters(list to_flatten)
183 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
187 // this function fires off a request to the noteworthy library via a link message.
188 // noteworthy will look for a notecard with our particular signature in it and
189 // if it finds one, it will read the configuration therein. an empty string is
190 // returned if noteworthy couldn't find anything.
191 request_configuration()
193 global_notecard_name = ""; // reset any previous card.
194 global_config_list = []; // toss previous contents.
195 // try to find a notecard with our configuration.
196 response_code = -1 * (integer)randomize_within_range(23, 80000, FALSE);
197 string parms_sent = wrap_parameters([WYLIE_NOTEWORTHY_SIGNATURE, response_code]);
198 llMessageLinked(LINK_THIS, NOTEWORTHY_HUFFWARE_ID, READ_NOTECARD_COMMAND,
202 // processes link messages received from the noteworthy library.
203 integer handle_link_message(integer which, integer num, string msg, key id)
205 //log_it("got linkmsg: " + (string)which + " - " + (string)num + " - " + msg);
206 if (num != NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE) return FALSE; // not for us.
207 // process the result of reading the notecard.
208 list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
209 string notecard_name = llList2String(parms, 0);
210 integer response_for = llList2Integer(parms, 1);
211 if (response_for != response_code) return FALSE; // oops, this isn't for us.
212 if (notecard_name == BAD_NOTECARD_INDICATOR) {
213 // should we try again?
214 return FALSE; // drop that one.
217 // a valid notecard has been found.
218 //log_it("notecard name: " + notecard_name);
219 // snag all but the first two elements for our config now.
220 global_config_list += llList2List(parms, 2, -1);
221 if (notecard_name == BUSY_READING_INDICATOR) {
222 llSetTimerEvent(SNOOZE_BEFORE_CONFIG_RESET);
224 } else if (notecard_name != NOTECARD_READ_CONTINUATION) {
225 global_notecard_name = notecard_name; // record its name for later use.
226 // now we're done reading, process the file as a set of definitions.
227 global_config_index = 0; // we are starting over in the config list.
228 process_ini_config();
231 // we're going to keep reading; notecard has more.
232 llSetTimerEvent(SNOOZE_BEFORE_CONFIG_RESET);
239 // consumes the notecard in a very application specific way to retrieve our configuration items.
240 // the example script only looks for two variables: name and description. if those are found in
241 // the sample card, then they are proudly shown.
242 parse_variable_definition(string to_parse)
244 //log_it("parse_var: " + to_parse);
245 string content; // filled after finding a variable name.
246 if ( (content = get_variable_value(to_parse, "channel")) != "") {
247 LISTENING_CHANNEL = (integer)content;
248 } else if ( (content = get_variable_value(to_parse, "owner_only")) != "") {
249 ONLY_LISTEN_TO_OWNER = (integer)content;
250 } else if ( (content = get_variable_value(to_parse, "show_channel")) != "") {
251 SHOW_CHANNEL = (integer)content;
252 } else if ( (content = get_variable_value(to_parse, "title_color")) != "") {
253 TITLE_COLOR = (vector)content;
257 initialize_notecard_reader()
259 // reset our relevant variables.
260 global_notecard_name = "";
261 global_config_list = [];
262 global_config_index = 0;
263 // reset our configurable constants to the defaults.
264 LISTENING_CHANNEL = 0;
265 ONLY_LISTEN_TO_OWNER = FALSE;
267 TITLE_COLOR = <1.0, 1.0, 1.0>;
269 // request that the noteworthy library start looking for our notecard.
270 request_configuration();
272 llSetTimerEvent(SNOOZE_BEFORE_CONFIG_RESET); // snooze a bit, and try reading again.
275 hear_the_voices(integer channel, string name, key id, string message)
277 if (ONLY_LISTEN_TO_OWNER && (id != llGetOwner())) return; // not valid from this guy.
278 if (!channel && (id == llGetOwner())) {
279 ///&& LISTENING_CHANNEL &&
280 // this is general chat, which is not our listening channel. we only
281 // allow special owner commands here.
282 if (is_prefix(message, BROADCAST_WORD)) {
283 // whoa, they know the secret broadcasting code.
284 channel = LISTENING_CHANNEL;
285 // chop the bcast word, plus a space, from the command.
286 message = llDeleteSubString(message, 0, llStringLength(BROADCAST_WORD));
289 if (channel != LISTENING_CHANNEL) return; // this is not for us.
290 if (message == "reset") {
293 } else if ( (message == "self-destruct") && (id == llGetOwner())) {
294 // this is going to send us to object heaven, we hope.
297 // wasn't a command to reset, so let's see if we know it.
298 list chopped = consume_wylie(message);
299 if (llGetListLength(chopped) != 3) return; // not understood as even close to wylie.
300 set_texture(UPPER_VOWEL_CHANNEL, llList2String(chopped, 0));
301 set_texture(CONSONTANT_CHANNEL, llList2String(chopped, 1));
302 set_texture(LOWER_VOWEL_CHANNEL, llList2String(chopped, 2));
309 integer debug_num = 0;
311 // a debugging output method. can be disabled entirely in one place.
312 log_it(string to_say)
315 // tell this to the owner.
316 llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
317 // say this on an unusual channel for chat if it's not intended for general public.
318 // llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
319 // say this on open chat that anyone can hear. we take off the bling for this one.
323 // the string processing methods are not case sensitive.
325 // returns TRUE if the "pattern" is found in the "full_string".
326 integer matches_substring(string full_string, string pattern)
327 { return (find_substring(full_string, pattern) >= 0); }
329 // returns the index of the first occurrence of "pattern" inside
330 // the "full_string". if it is not found, then a negative number is returned.
331 integer find_substring(string full_string, string pattern)
332 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
334 // returns TRUE if the "prefix" string is the first part of "compare_with".
335 integer is_prefix(string compare_with, string prefix)
336 { return find_substring(compare_with, prefix) == 0; }
338 // strips the spaces off of the beginning and end of a string.
339 string strip_spaces(string to_strip)
341 // clean out initial spaces.
342 while (llGetSubString(to_strip, 0, 0) == " ")
343 to_strip = llDeleteSubString(to_strip, 0, 0);
344 // clean out ending spaces.
345 while (llGetSubString(to_strip, -1, -1) == " ")
346 to_strip = llDeleteSubString(to_strip, -1, -1);
350 // parses a variable definition to find the name of the variable and its value.
351 // this returns two strings [X, Y], if "to_split" is in the form X=Y.
352 list separate_variable_definition(string to_split)
354 integer equals_indy = llSubStringIndex(to_split, "=");
355 // we don't support missing an equals sign, and we don't support it as the first character.
356 if (equals_indy <= 0) return []; // no match.
357 string x = llGetSubString(to_split, 0, equals_indy - 1);
358 string y = llGetSubString(to_split, equals_indy + 1, -1);
359 to_split = ""; // save space.
360 return [ strip_spaces(x), strip_spaces(y) ];
363 // returns a non-empty string if "to_check" defines a value for "variable_name".
364 // this must be in the form "X=Y", where X is the variable_name and Y is the value.
365 string get_variable_value(string to_check, string variable_name)
367 list x_y = separate_variable_definition(to_check);
368 if (llGetListLength(x_y) != 2) return ""; // failure to parse a variable def at all.
369 if (!is_prefix(llList2String(x_y, 0), variable_name)) return ""; // no match.
370 return llList2String(x_y, 1); // a match!
373 // returns a number at most "maximum" and at least "minimum".
374 // if "allow_negative" is TRUE, then the return may be positive or negative.
375 float randomize_within_range(float minimum, float maximum, integer allow_negative)
377 if (minimum > maximum) {
378 // flip the two if they are reversed.
379 float temp = minimum; minimum = maximum; maximum = temp;
381 float to_return = minimum + llFrand(maximum - minimum);
382 if (allow_negative) {
383 if (llFrand(1.0) < 0.5) to_return *= -1.0;
388 // examines all entries that we got from the notecard to see if any contain definitions.
389 // this is basically an INI file reader, but it uses a list instead of a file.
390 // ini files provide a format with multiple sections of config information, like so:
393 // name2=value2 ...etc...
395 // name1=value1 ...etc...
399 integer count = llGetListLength(global_config_list);
401 // iterate across the items in our configuration to look for ones that are not done yet.
402 for (indy = global_config_index; indy < count; indy++) {
403 string line = llList2String(global_config_list, indy);
404 // search for a section beginning.
405 if (llGetSubString(line, 0, 0) == "[") {
406 // we found the start of a section name. now read the contents.
407 indy++; // skip section line.
410 for (sec_indy = indy; sec_indy < count; sec_indy++) {
411 // read the lines in the section.
412 line = llList2String(global_config_list, sec_indy);
413 if (llGetSubString(line, 0, 0) != "[") {
414 // try to interpret this line as a variable setting. this is just
415 // one example of a way to handle the config file; one might instead
416 // want to do something below once a whole section is read.
417 parse_variable_definition(line);
418 indy = sec_indy; // track that we've passed this line.
420 // we're at the beginning of a new section now, so start processing its
421 // configuration in the outer loop.
422 indy = sec_indy - 1; // set indy to proper beginning of section.
423 global_config_index = indy; // remember where we had read to.
424 sec_indy = count + 3; // skip remainder of inner loop.
429 global_config_index = 0; // reset outer position if want to re-read.
432 // locates the item with "name_to_find" in the inventory items with the "type".
433 // a value from 0 to N-1 is returned if it's found, where N is the number of
434 // items in the inventory.
435 integer find_in_inventory(string name_to_find, integer inv_type)
437 integer num_inv = llGetInventoryNumber(inv_type);
438 if (num_inv == 0) return -1; // nothing there!
440 for (inv = 0; inv < num_inv; inv++) {
441 if (llGetInventoryName(inv_type, inv) == name_to_find)
444 return -2; // failed to find it.
450 // huffware script: auto-retire, by fred huffhines, version 2.5.
451 // distributed under BSD-like license.
452 // !! keep in mind that this code must be *copied* into another
453 // !! script that you wish to add auto-retirement capability to.
454 // when a script has auto_retire in it, it can be dropped into an
455 // object and the most recent version of the script will destroy
456 // all older versions.
458 // the version numbers are embedded into the script names themselves.
459 // the notation for versions uses a letter 'v', followed by two numbers
460 // in the form "major.minor".
461 // major and minor versions are implicitly considered as a floating point
462 // number that increases with each newer version of the script. thus,
463 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
464 // and "hazmap v3.2" is a more recent version.
466 // example usage of the auto-retirement script:
469 // auto_retire(); // make sure newest addition is only version of script.
472 // this script is partly based on the self-upgrading scripts from markov brodsky
473 // and jippen faddoul.
476 string self = llGetScriptName(); // the name of this script.
477 list split = compute_basename_and_version(self);
478 if (llGetListLength(split) != 2) return; // nothing to do for this script.
479 string basename = llList2String(split, 0); // script name with no version attached.
480 string version_string = llList2String(split, 1); // the version found.
482 // find any scripts that match the basename. they are variants of this script.
483 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
484 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
485 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
486 // found a basic match at least.
487 list inv_split = compute_basename_and_version(curr_script);
488 if (llGetListLength(inv_split) == 2) {
489 // see if this script is more ancient.
490 string inv_version_string = llList2String(inv_split, 1); // the version found.
491 // must make sure that the retiring script is completely the identical basename;
492 // just matching in the front doesn't make it a relative.
493 if ( (llList2String(inv_split, 0) == basename)
494 && ((float)inv_version_string < (float)version_string) ) {
495 // remove script with same name from inventory that has inferior version.
496 llRemoveInventory(curr_script);
503 // separates the base script name and version number. used by auto_retire.
504 list compute_basename_and_version(string to_chop_up)
506 // minimum script name is 2 characters plus a version.
507 integer space_v_posn;
508 // find the last useful space and 'v' combo.
509 for (space_v_posn = llStringLength(to_chop_up) - 3;
510 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
512 // look for space and v but do nothing else.
514 if (space_v_posn < 2) return []; // no space found.
515 // now we zoom through the stuff after our beloved v character and find any evil
516 // space characters, which are most likely from SL having found a duplicate item
517 // name and not so helpfully renamed it for us.
519 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
520 if (llGetSubString(to_chop_up, indy, indy) == " ") {
521 // found one; zap it. since we're going backwards we don't need to
522 // adjust the loop at all.
523 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
526 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
527 // ditch the space character for our numerical check.
528 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
529 // strip out a 'v' if there is one.
530 if (llGetSubString(chop_suffix, 0, 0) == "v")
531 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
532 // if valid floating point number and greater than zero, that works for our version.
533 string basename = to_chop_up; // script name with no version attached.
534 if ((float)chop_suffix > 0.0) {
535 // this is a big success right here.
536 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
537 return [ basename, chop_suffix ];
539 // seems like we found nothing useful.
545 //defect in default state:
546 // if no response from noteworthy library, infinite hang.
549 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
550 on_rez(integer parm) { startup_parm = parm; state rerun; }
552 state rerun { state_entry() { state default; } }
558 if (DEBUGGING) log_it("started, free mem=" + (string)llGetFreeMemory());
559 auto_retire(); // make sure newest addition is only version of script.
560 if (DEBUGGING) log_it("entered default state.");
562 initialize_notecard_reader();
565 state_exit() { llSetTimerEvent(0); }
567 // process the response from the noteworthy library.
568 link_message(integer which, integer num, string msg, key id)
570 if (handle_link_message(which, num, msg, id))
571 state ready_for_instructions;
574 // restart when we see changes to our notecard configuration.
575 changed(integer change) {
576 if (change & CHANGED_INVENTORY) {
577 llSleep(4); // delay to avoid rez-time premature startup.
578 if (DEBUGGING) log_it("restarting due to inv change.");
583 // invoked when the notecard reading has failed.
585 if (global_notecard_name == "") {
586 if (retries_allowed < 0) {
587 if (DEBUGGING) log_it("ran out of retries, skipping reset.");
588 state ready_for_instructions;
590 if (DEBUGGING) log_it("timed out, going back to default.");
591 state default; // start over again.
595 listen(integer channel, string name, key id, string message) {
596 hear_the_voices(channel, name, id, message);
599 on_rez(integer parm) { startup_parm = parm; state rerun; }
602 state ready_for_instructions
605 if (DEBUGGING) log_it("entered ready_for_instructions state.");
609 state_exit() { llSetTimerEvent(0); }
611 listen(integer channel, string name, key id, string message) {
612 hear_the_voices(channel, name, id, message);
615 // reset when we see changes to our notecard configuration.
616 changed(integer change) {
617 if (change & CHANGED_INVENTORY) {
618 if (DEBUGGING) log_it("inventory change seen; restarting");
623 on_rez(integer parm) { startup_parm = parm; state default; }