2 // huffware script: slate reader, by fred huffhines
4 // uses the noteworthy library to read a set of notecards. the list of notecards
5 // can be queried, and the current notecard's contents can be read aloud or sent
6 // to a link as link messages.
8 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
9 // do not use it in objects without fully realizing you are implicitly accepting that license.
12 // global constants...
14 string SLATE_SIGNATURE = "#slate";
15 // the notecard must begin with this as its first line for it to be
16 // recognized as our configuration card.
18 // global variables...
20 integer response_code; // set to uniquely identify the notecard read in progress.
21 integer global_link_num; // which prim number to deliver the notecard contents to.
23 // implements an API for moving between the active notecards.
25 // do not redefine these constants.
26 integer SLATE_READER_HUFFWARE_ID = 10028;
27 // the unique id within the huffware system for the noteworthy script to
28 // accept commands on. this is used in llMessageLinked as the num parameter.
29 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
30 // this pattern is an uncommon thing to see in text, so we use it to separate
31 // our commands in link messages.
32 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
33 // used to separate lists of items from each other when stored inside a parameter.
34 // this allows lists to be passed as single string parameters if needed.
35 integer REPLY_DISTANCE = 100008; // offset added to service's huffware id in reply IDs.
37 string RESET_SLATE_READER_COMMAND = "#rsslt";
38 // causes the notecard information to be forgotten and the script restarted.
39 string SR_GET_INFORMATION_COMMAND = "#infy";
40 // used by clients to ask for information about the current number of notecards
41 // available, and their names. this information is sent back on the huffware ID
42 // plus the reply distance. first parm is the number, and the rest are the names.
43 string SR_PLAY_CARD_COMMAND = "#playvo";
44 // picks a particular notecard for reading and send the notecard's contents in a
45 // series of link messages, using this command and the reply distance. there are
46 // two parameters: an integer for the notecard number to read (from 0 through the
47 // number of notecards - 1) and the link number to send the messages to.
50 // requires noteworthy library v10.4 or better.
52 // do not redefine these constants.
53 integer NOTEWORTHY_HUFFWARE_ID = 10010;
54 // the unique id within the huffware system for the noteworthy script to
55 // accept commands on. this is used in llMessageLinked as the num parameter.
57 string BAD_NOTECARD_INDICATOR = "bad_notecard";
58 // indicates that the notecard reading process has failed to find an appropriate one.
59 string NOTECARD_READ_CONTINUATION = "continue!";
60 // returned as first parameter if there is still more data to handle.
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 blank.
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_INDICATOR if none was
69 // found) and as subsequent elements an embedded list that was read from the
70 // notecard. this necessarily limits the size of the notecards that we can read
72 string READ_SPECIFIC_NOTECARD_COMMAND = "#read_thisun#";
73 // like the read notecard command, but specifies the notecard name to use. only that
74 // specific notecard file will be consulted. first and second parm are still signature
75 // and response code, third parm is the notecard name.
78 // joins a list of parameters using the parameter sentinel for the library.
79 string wrap_parameters(list to_flatten)
80 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
83 // this function fires off a request to the noteworthy library via a link message.
84 // noteworthy will look for a notecard with our particular signature in it and
85 // if it finds one, it will read the configuration therein. an empty string is
86 // returned if noteworthy couldn't find anything.
87 request_configuration(string note_name)
89 // try to find a notecard with our configuration.
90 response_code = -1 * (integer)randomize_within_range(23, 80000, FALSE);
91 string parms_sent = wrap_parameters([SLATE_SIGNATURE, response_code, note_name]);
92 llMessageLinked(LINK_THIS, NOTEWORTHY_HUFFWARE_ID, READ_SPECIFIC_NOTECARD_COMMAND,
96 // provides the functions of the slate reader API.
97 process_slate_requests(string msg, key id)
99 if (msg == RESET_SLATE_READER_COMMAND) {
101 } else if (msg == SR_GET_INFORMATION_COMMAND) {
102 list parms = [ llGetInventoryNumber(INVENTORY_NOTECARD) ];
104 for (indy = 0; indy < llGetInventoryNumber(INVENTORY_NOTECARD); indy++) {
105 parms += llGetInventoryName(INVENTORY_NOTECARD, indy);
107 llMessageLinked(LINK_THIS, SLATE_READER_HUFFWARE_ID + REPLY_DISTANCE, msg,
108 wrap_parameters(parms));
109 } else if (msg == SR_PLAY_CARD_COMMAND) {
110 list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
111 integer note_number = llList2Integer(parms, 0);
112 integer link_number = llList2Integer(parms, 1);
113 initialize_reader(note_number, link_number);
117 // processes link messages received from the noteworthy library.
118 handle_link_message(integer which, integer num, string msg, key id)
120 if (num == SLATE_READER_HUFFWARE_ID) {
121 process_slate_requests(msg, id);
124 if ( (num != NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE)
125 || (msg != READ_NOTECARD_COMMAND) )
126 return; // not for us.
127 // process the result of reading the notecard.
128 list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
129 string notecard_name = llList2String(parms, 0);
130 integer response_for = llList2Integer(parms, 1);
131 if (response_for != response_code) return; // oops, this isn't for us.
132 if (notecard_name == BAD_NOTECARD_INDICATOR) {
133 // we hated the notecards we found, or there were none.
134 log_it("We apologize; there seem to be no notecards with a first line of '"
136 + "'. We can't read any configuration until that situation improves.");
138 // snag all but the first two elements for our config now.
139 list config_list = llList2List(parms, 2, -1);
140 // a valid notecard has been found.
141 integer lines_to_say = llGetListLength(config_list);
143 for (indy = 0; indy < lines_to_say; indy++) {
144 string line = llList2String(config_list, indy);
145 if (!is_prefix(line, "#")) {
146 llMessageLinked(global_link_num, SLATE_READER_HUFFWARE_ID + REPLY_DISTANCE,
147 SR_PLAY_CARD_COMMAND, line);
155 initialize_reader(integer note_number, integer link_number)
157 // reset our relevant variables.
158 global_link_num = link_number;
160 if (note_number >= llGetInventoryNumber(INVENTORY_NOTECARD)) {
161 llSay(0, "Cannot initialize reader to notecard number " + (string)note_number + " because that is out of range.");
164 string notecard_name = llGetInventoryName(INVENTORY_NOTECARD, note_number);
166 // request that the noteworthy library start looking for our notecard.
167 request_configuration(notecard_name);
174 integer debug_num = 0;
176 // a debugging output method. can be disabled entirely in one place.
177 log_it(string to_say)
180 // tell this to the owner.
181 llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
182 // say this on an unusual channel for chat if it's not intended for general public.
183 // llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
184 // say this on open chat that anyone can hear. we take off the bling for this one.
188 // locates the string "text" in the list to "search_in".
189 integer find_in_list(list search_in, string text)
191 integer len = llGetListLength(search_in);
193 for (i = 0; i < len; i++) {
194 if (llList2String(search_in, i) == text)
200 // returns TRUE if the "prefix" string is the first part of "compare_with".
201 integer is_prefix(string compare_with, string prefix)
202 { return (llSubStringIndex(compare_with, prefix) == 0); }
204 // returns a number at most "maximum" and at least "minimum".
205 // if "allow_negative" is TRUE, then the return may be positive or negative.
206 float randomize_within_range(float minimum, float maximum, integer allow_negative)
208 if (minimum > maximum) {
209 // flip the two if they are reversed.
210 float temp = minimum; minimum = maximum; maximum = temp;
212 float to_return = minimum + llFrand(maximum - minimum);
213 if (allow_negative) {
214 if (llFrand(1.0) < 0.5) to_return *= -1.0;
219 // strips the spaces off of the beginning and end of a string.
220 string strip_spaces(string to_strip)
222 // clean out initial spaces.
223 while (llGetSubString(to_strip, 0, 0) == " ")
224 to_strip = llDeleteSubString(to_strip, 0, 0);
225 // clean out ending spaces.
226 while (llGetSubString(to_strip, -1, -1) == " ")
227 to_strip = llDeleteSubString(to_strip, -1, -1);
231 // locates the item with "name_to_find" in the inventory items with the "type".
232 // a value from 0 to N-1 is returned if it's found, where N is the number of
233 // items in the inventory.
234 integer find_in_inventory(string name_to_find, integer inv_type)
236 integer num_inv = llGetInventoryNumber(inv_type);
237 if (num_inv == 0) return -1; // nothing there!
239 for (inv = 0; inv < num_inv; inv++) {
240 if (llGetInventoryName(inv_type, inv) == name_to_find)
243 return -2; // failed to find it.
247 // huffware script: auto-retire, by fred huffhines, version 2.8.
248 // distributed under BSD-like license.
249 // !! keep in mind that this code must be *copied* into another
250 // !! script that you wish to add auto-retirement capability to.
251 // when a script has auto_retire in it, it can be dropped into an
252 // object and the most recent version of the script will destroy
253 // all older versions.
255 // the version numbers are embedded into the script names themselves.
256 // the notation for versions uses a letter 'v', followed by two numbers
257 // in the form "major.minor".
258 // major and minor versions are implicitly considered as a floating point
259 // number that increases with each newer version of the script. thus,
260 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
261 // and "hazmap v3.2" is a more recent version.
263 // example usage of the auto-retirement script:
266 // auto_retire(); // make sure newest addition is only version of script.
269 // this script is partly based on the self-upgrading scripts from markov brodsky
270 // and jippen faddoul.
273 string self = llGetScriptName(); // the name of this script.
274 list split = compute_basename_and_version(self);
275 if (llGetListLength(split) != 2) return; // nothing to do for this script.
276 string basename = llList2String(split, 0); // script name with no version attached.
277 string version_string = llList2String(split, 1); // the version found.
279 // find any scripts that match the basename. they are variants of this script.
280 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
281 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
282 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
283 // found a basic match at least.
284 list inv_split = compute_basename_and_version(curr_script);
285 if (llGetListLength(inv_split) == 2) {
286 // see if this script is more ancient.
287 string inv_version_string = llList2String(inv_split, 1); // the version found.
288 // must make sure that the retiring script is completely the identical basename;
289 // just matching in the front doesn't make it a relative.
290 if ( (llList2String(inv_split, 0) == basename)
291 && ((float)inv_version_string < (float)version_string) ) {
292 // remove script with same name from inventory that has inferior version.
293 llRemoveInventory(curr_script);
300 // separates the base script name and version number. used by auto_retire.
301 list compute_basename_and_version(string to_chop_up)
303 // minimum script name is 2 characters plus a version.
304 integer space_v_posn;
305 // find the last useful space and 'v' combo.
306 for (space_v_posn = llStringLength(to_chop_up) - 3;
307 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
309 // look for space and v but do nothing else.
311 if (space_v_posn < 2) return []; // no space found.
312 // now we zoom through the stuff after our beloved v character and find any evil
313 // space characters, which are most likely from SL having found a duplicate item
314 // name and not so helpfully renamed it for us.
316 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
317 if (llGetSubString(to_chop_up, indy, indy) == " ") {
318 // found one; zap it. since we're going backwards we don't need to
319 // adjust the loop at all.
320 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
323 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
324 // ditch the space character for our numerical check.
325 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
326 // strip out a 'v' if there is one.
327 if (llGetSubString(chop_suffix, 0, 0) == "v")
328 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
329 // if valid floating point number and greater than zero, that works for our version.
330 string basename = to_chop_up; // script name with no version attached.
331 if ((float)chop_suffix > 0.0) {
332 // this is a big success right here.
333 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
334 return [ basename, chop_suffix ];
336 // seems like we found nothing useful.
346 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
347 on_rez(integer parm) { state rerun; }
349 state rerun { state_entry() { state default; } }
358 // on_rez(integer parm) { llResetScript(); }
360 // process the response from the noteworthy library.
361 link_message(integer which, integer num, string msg, key id)
362 { handle_link_message(which, num, msg, id); }