2 // huffware script: texture shower, by fred huffhines.
4 // displays a texture from its inventory when the name is spoken in chat.
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.
11 // global constants...
13 string SHOWER_SIGNATURE = "#texture_shower";
14 // the notecard must begin with this as its first line for it to be
15 // recognized as our configuration card.
17 // global variables...
19 string global_notecard_name; // name of our notecard in the object's inventory.
20 integer response_code; // set to uniquely identify the notecard read in progress.
21 list global_config_list; // a collection of configuration parameters from our notecard.
22 integer global_config_index; // allows wrap-around feature, which we don't use here.
24 list texture_names; // the set of textures we've got in inventory.
26 integer LISTENING_CHANNEL = 1008;
27 // the default listening channel is hardly ever used; the channel should
28 // come from the notecard.
32 // processes the variables that come from the notecard.
33 parse_variable_definition(string to_parse)
35 string content; // filled after finding a variable name.
36 if ( (content = get_variable_value(to_parse, "channel")) != "") {
37 // log_it("channel=" + content);
38 LISTENING_CHANNEL = (integer)content;
44 // sets the viewing sides' textures to a particular inventory item.
45 set_texture_to_item(string inv_tex)
47 llSetTexture(inv_tex, 1);
48 llSetTexture(inv_tex, 3);
51 // sets our viewing sides back to the default texture.
54 string tex_cur = llGetTexture(0);
55 set_texture_to_item(tex_cur);
58 // startup for the very early part of the object's lifetime.
61 // reset our relevant variables.
62 global_notecard_name = "";
63 global_config_list = [];
64 global_config_index = 0;
67 // request that the noteworthy library start looking for our notecard.
68 request_configuration();
70 // log_it("started, free mem=" + (string)llGetFreeMemory());
73 // this gets us ready to enter our active state.
76 // listen for commands on the channel we're configured for.
77 llListen(LISTENING_CHANNEL, "", NULL_KEY, "");
78 // load all the texture names we know about.
79 integer texture_count = llGetInventoryNumber(INVENTORY_TEXTURE);
81 for (indy = 0; indy < texture_count; indy++)
82 texture_names += llGetInventoryName(INVENTORY_TEXTURE, indy);
85 manage_those_voices(integer channel, string message)
87 if (channel != LISTENING_CHANNEL) return; // not for us.
88 if (message == "reset-texture") {
92 // wasn't a command to reset, so let's see if we know it.
93 integer texture_count = llGetListLength(texture_names);
95 for (indy = 0; indy < texture_count; indy++) {
96 integer posn = llListFindList(texture_names, (message));
99 set_texture_to_item(message);
107 // this chunk largely comes from the example noteworthy usage...
109 // requires noteworthy library v8.4 or better.
111 // do not redefine these constants.
112 integer NOTEWORTHY_HUFFWARE_ID = 10010;
113 // the unique id within the huffware system for the noteworthy script to
114 // accept commands on. this is used in llMessageLinked as the num parameter.
115 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
116 // this pattern is an uncommon thing to see in text, so we use it to separate
117 // our commands in link messages.
118 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
119 // used to separate lists of items from each other when stored inside a parameter.
120 // this allows lists to be passed as single string parameters if needed.
121 integer REPLY_DISTANCE = 100008; // offset added to service's huffware id in reply IDs.
122 // commands available via the noteworthy library:
123 string READ_NOTECARD_COMMAND = "#read_note#";
124 // command used to tell the script to read notecards. needs a signature to find
125 // in the card as the first parameter, and a randomly generated response code for
126 // the second parameter. the response code is used to uniquely identify a set of
127 // pending notecard readings (hopefully). the signature can be empty or missing.
128 // the results will be fired back as the string value returned, which will have
129 // as first element the notecard's name (or "bad_notecard" if none was found) and
130 // as subsequent elements an embedded list that was read from the notecard. this
131 // necessarily limits the size of the notecards that we can read and return.
134 // joins a list of parameters using the parameter sentinel for the library.
135 string wrap_parameters(list to_flatten)
136 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
139 // this function fires off a request to the noteworthy library via a link message.
140 // noteworthy will look for a notecard with our particular signature in it and
141 // if it finds one, it will read the configuration therein. an empty string is
142 // returned if noteworthy couldn't find anything.
143 request_configuration()
145 global_notecard_name = ""; // reset any previous card.
146 // try to find a notecard with our configuration.
147 response_code = -1 * (integer)randomize_within_range(23, 80000, FALSE);
148 string parms_sent = wrap_parameters([SHOWER_SIGNATURE, response_code]);
149 llMessageLinked(LINK_THIS, NOTEWORTHY_HUFFWARE_ID, READ_NOTECARD_COMMAND,
153 // processes link messages received from the noteworthy library and others.
154 // if the message indicates we should change states, then TRUE is returned.
155 integer handle_link_message(integer which, integer num, string msg, key id)
157 if (num != NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE)
158 return FALSE; // not for us.
159 // process the result of reading the notecard.
160 list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
161 string notecard_name = llList2String(parms, 0);
162 integer response_for = llList2Integer(parms, 1);
163 if (response_for != response_code) return FALSE; // oops, this isn't for us.
164 if (notecard_name != "bad_notecard") {
165 // a valid notecard has been found.
166 global_notecard_name = notecard_name; // record its name for later use.
167 global_config_index = 0; // we are starting over in the config list.
168 // snag all but the first two elements for our config now.
169 global_config_list = llList2List(parms, 2, -1);
170 // and process the file as a set of definitions.
171 process_ini_config();
174 // we hated the notecards we found, or there were none.
175 log_it("We apologize; there seem to be no notecards with a first line of '"
177 + "'. We can't read any configuration until that situation improves.");
183 // huffware script: auto-retire, by fred huffhines, version 2.5.
184 // distributed under BSD-like license.
185 // !! keep in mind that this code must be *copied* into another
186 // !! script that you wish to add auto-retirement capability to.
187 // when a script has auto_retire in it, it can be dropped into an
188 // object and the most recent version of the script will destroy
189 // all older versions.
191 // the version numbers are embedded into the script names themselves.
192 // the notation for versions uses a letter 'v', followed by two numbers
193 // in the form "major.minor".
194 // major and minor versions are implicitly considered as a floating point
195 // number that increases with each newer version of the script. thus,
196 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
197 // and "hazmap v3.2" is a more recent version.
199 // example usage of the auto-retirement script:
202 // auto_retire(); // make sure newest addition is only version of script.
205 // this script is partly based on the self-upgrading scripts from markov brodsky
206 // and jippen faddoul.
209 string self = llGetScriptName(); // the name of this script.
210 list split = compute_basename_and_version(self);
211 if (llGetListLength(split) != 2) return; // nothing to do for this script.
212 string basename = llList2String(split, 0); // script name with no version attached.
213 string version_string = llList2String(split, 1); // the version found.
215 // find any scripts that match the basename. they are variants of this script.
216 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
217 //log_it("invpo=" + (string)posn);
218 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
219 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
220 // found a basic match at least.
221 list inv_split = compute_basename_and_version(curr_script);
222 if (llGetListLength(inv_split) == 2) {
223 // see if this script is more ancient.
224 string inv_version_string = llList2String(inv_split, 1); // the version found.
225 // must make sure that the retiring script is completely the identical basename;
226 // just matching in the front doesn't make it a relative.
227 if ( (llList2String(inv_split, 0) == basename)
228 && ((float)inv_version_string < (float)version_string) ) {
229 // remove script with same name from inventory that has inferior version.
230 llRemoveInventory(curr_script);
237 // separates the base script name and version number. used by auto_retire.
238 list compute_basename_and_version(string to_chop_up)
240 // minimum script name is 2 characters plus a version.
241 integer space_v_posn;
242 // find the last useful space and 'v' combo.
243 for (space_v_posn = llStringLength(to_chop_up) - 3;
244 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
246 // look for space and v but do nothing else.
247 //log_it("pos=" + (string)space_v_posn);
249 if (space_v_posn < 2) return []; // no space found.
250 //log_it("space v@" + (string)space_v_posn);
251 // now we zoom through the stuff after our beloved v character and find any evil
252 // space characters, which are most likely from SL having found a duplicate item
253 // name and not so helpfully renamed it for us.
255 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
256 //log_it("indy=" + (string)space_v_posn);
257 if (llGetSubString(to_chop_up, indy, indy) == " ") {
258 // found one; zap it. since we're going backwards we don't need to
259 // adjust the loop at all.
260 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
261 //log_it("saw case of previously redundant item, aieee. flattened: " + to_chop_up);
264 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
265 // ditch the space character for our numerical check.
266 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
267 // strip out a 'v' if there is one.
268 if (llGetSubString(chop_suffix, 0, 0) == "v")
269 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
270 // if valid floating point number and greater than zero, that works for our version.
271 string basename = to_chop_up; // script name with no version attached.
272 if ((float)chop_suffix > 0.0) {
273 // this is a big success right here.
274 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
275 return [ basename, chop_suffix ];
277 // seems like we found nothing useful.
287 integer debug_num = 0;
289 // a debugging output method. can be disabled entirely in one place.
290 log_it(string to_say)
293 // tell this to the owner.
294 llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
295 // say this on an unusual channel for chat if it's not intended for general public.
296 // llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
297 // say this on open chat that anyone can hear. we take off the bling for this one.
301 // locates the string "text" in the list to "search_in".
302 integer find_in_list(list search_in, string text)
304 integer len = llGetListLength(search_in);
306 for (i = 0; i < len; i++) {
307 if (llList2String(search_in, i) == text)
313 // returns TRUE if the "prefix" string is the first part of "compare_with".
314 integer is_prefix(string compare_with, string prefix)
315 { return (llSubStringIndex(compare_with, prefix) == 0); }
317 // returns a number at most "maximum" and at least "minimum".
318 // if "allow_negative" is TRUE, then the return may be positive or negative.
319 float randomize_within_range(float minimum, float maximum, integer allow_negative)
321 if (minimum > maximum) {
322 // flip the two if they are reversed.
323 float temp = minimum; minimum = maximum; maximum = temp;
325 float to_return = minimum + llFrand(maximum - minimum);
326 if (allow_negative) {
327 if (llFrand(1.0) < 0.5) to_return *= -1.0;
332 // strips the spaces off of the beginning and end of a string.
333 string strip_spaces(string to_strip)
335 // clean out initial spaces.
336 while (llGetSubString(to_strip, 0, 0) == " ")
337 to_strip = llDeleteSubString(to_strip, 0, 0);
338 // clean out ending spaces.
339 while (llGetSubString(to_strip, -1, -1) == " ")
340 to_strip = llDeleteSubString(to_strip, -1, -1);
344 // parses a variable definition to find the name of the variable and its value.
345 // this returns two strings [X, Y], if "to_split" is in the form X=Y.
346 list separate_variable_definition(string to_split)
348 integer equals_indy = llSubStringIndex(to_split, "=");
349 // we don't support missing an equals sign, and we don't support it as the first character.
350 if (equals_indy <= 0) return []; // no match.
351 string x = llGetSubString(to_split, 0, equals_indy - 1);
352 string y = llGetSubString(to_split, equals_indy + 1, -1);
353 to_split = ""; // save space.
354 return [ strip_spaces(x), strip_spaces(y) ];
357 // returns a non-empty string if "to_check" defines a value for "variable_name".
358 // this must be in the form "X=Y", where X is the variable_name and Y is the value.
359 string get_variable_value(string to_check, string variable_name)
361 list x_y = separate_variable_definition(to_check);
362 if (llGetListLength(x_y) != 2) return ""; // failure to parse a variable def at all.
363 if (!is_prefix(llList2String(x_y, 0), variable_name)) return ""; // no match.
364 return llList2String(x_y, 1); // a match!
367 // examines all entries that we got from the notecard to see if any contain definitions.
368 // this is basically an INI file reader, but it uses a list instead of a file.
369 // ini files provide a format with multiple sections of config information, like so:
372 // name2=value2 ...etc...
374 // name1=value1 ...etc...
377 // log_it("scanning notecard for variable definitions...");
379 integer count = llGetListLength(global_config_list);
381 // iterate across the items in our configuration to look for ones that are not done yet.
382 for (indy = global_config_index; indy < count; indy++) {
383 string line = llList2String(global_config_list, indy);
384 // search for a section beginning.
385 if (llGetSubString(line, 0, 0) == "[") {
386 // we found the start of a section name. now read the contents.
387 indy++; // skip section line.
388 log_it("reading section: " + llGetSubString(line, 1, -2));
391 for (sec_indy = indy; sec_indy < count; sec_indy++) {
392 // read the lines in the section.
393 line = llList2String(global_config_list, sec_indy);
394 if (llGetSubString(line, 0, 0) != "[") {
395 // try to interpret this line as a variable setting. this is just
396 // one example of a way to handle the config file; one might instead
397 // want to do something below once a whole section is read.
398 parse_variable_definition(line);
400 // we're at the beginning of a new section now, so start processing its
401 // configuration in the outer loop.
402 indy = sec_indy - 1; // set indy to proper beginning of section.
403 global_config_index = indy; // remember where we had read to.
404 sec_indy = count + 3; // skip remainder of inner loop.
409 global_config_index = 0; // reset outer position if want to re-read.
412 // locates the item with "name_to_find" in the inventory items with the "type".
413 // a value from 0 to N-1 is returned if it's found, where N is the number of
414 // items in the inventory.
415 integer find_in_inventory(string name_to_find, integer inv_type)
417 integer num_inv = llGetInventoryNumber(inv_type);
418 if (num_inv == 0) return -1; // nothing there!
420 for (inv = 0; inv < num_inv; inv++) {
421 if (llGetInventoryName(inv_type, inv) == name_to_find)
424 return -2; // failed to find it.
430 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
431 on_rez(integer parm) { state rerun; }
433 state rerun { state_entry() { state default; } }
438 auto_retire(); // make sure newest addition is only version of script.
439 initialize_phase_1();
442 on_rez(integer param) { state rerun; }
444 // reset when we see changes to our notecard configuration.
445 changed(integer change) {
446 if (change & CHANGED_INVENTORY) {
447 llSleep(3.14159265358); // delay to avoid interfering with upgrade.
452 // process the response from the noteworthy library.
453 link_message(integer which, integer num, string msg, key id) {
454 integer retval = handle_link_message(which, num, msg, id);
455 if (retval) state configured; // skip states if we were asked to.
462 initialize_phase_2();
465 listen(integer channel, string name, key id, string message) {
466 manage_those_voices(channel, message);
469 link_message(integer which, integer num, string msg, key id) {
470 manage_those_voices(num, msg);
473 // reset when we see changes to our notecard configuration.
474 changed(integer change) {
475 if (change & CHANGED_INVENTORY) {
476 llSleep(3.14159265358); // delay to avoid interfering with upgrade.