fixed names back to .txt, since we cannot get second inventory to
[feisty_meow.git] / huffware / huffotronic_eepaw_knowledge_v60.9 / menutini_library_v5.9.txt
1 
2 // huffware script: menutini library, by fred huffhines.
3 //
4 // this is a library script for menuing that provides a way to remote control the
5 // menu, somewhat.  another script can zing link messages at this script and a menu
6 // will be shown based on the specified description and buttons.  when the user
7 // selects an answer, that result is sent back in a link message reply.
8 //
9 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
10 // do not use it in objects without fully realizing you are implicitly accepting that license.
11 //
12
13 // useful constants you might want to change:
14
15 integer TIMEOUT_FOR_MENU = 42;
16     // timeout for the menu in seconds.
17 //hmmm: may want this to be selectable from menu request.
18 //      or may even want to never time out!
19 //      if we managed a list of ongoing menus, that would work.
20 //      currently it cannot.
21
22 integer DEBUGGING = FALSE;
23     // if this is true, then extra info will be printed when handling a menu.
24
25 string NEXT_MENU_TEXT = "Next >>";
26     // what the next item will say for showing next menu page.
27
28 // menutini link message API...
29 //////////////
30 // do not redefine these constants.
31 integer MENUTINI_HUFFWARE_ID = 10009;
32     // the unique id within the huffware system for the jaunt script to
33     // accept commands on.  this is used in llMessageLinked as the num parameter.
34 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
35     // this pattern is an uncommon thing to see in text, so we use it to separate
36     // our commands in link messages.
37 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
38     // used to separate lists of items from each other when stored inside a parameter.
39     // this allows lists to be passed as single string parameters if needed.
40 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
41 string SHOW_MENU_COMMAND = "#menu#";
42     // the command that tells menutini to show a menu defined by parameters
43     // that are passed along.  these must be: the menu name, the menu's title
44     // (which is really the info to show as content in the main box of the menu),
45     // the wrapped list of commands to show as menu buttons, the menu system
46     // channel's for listening, and the key to listen to.
47     // the reply will include: the menu name, the choice made and the key for
48     // the avatar.
49 //
50 //////////////
51 // joins a list of sub-items using the item sentinel for the library.
52 string wrap_item_list(list to_wrap)
53 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
54 //
55 //////////////
56
57 // global variables...
58
59 list _private_global_buttons;  // holds onto the active set of menu options.
60 string _private_global_av_key;  // the key for the avatar who clicks the menu.
61 string _private_global_title;  // holds onto current title text.
62
63 integer _menu_base_start = 0;  // position in the items of the current menu.
64
65 integer listening_id = 0;
66     // the current id of our listening for the menu.  it's an id returned by LSL
67     // that we need to track so we can cancel the listen.
68
69 integer menu_system_channel = 0;
70     // messages come back to us from this channel when user clicks the dialog.
71     // this is set later and the default is meaningless.
72
73 string global_menu_name = "";
74     // hangs onto the current menu's name.
75
76 //hmmm: note; to manage multiple concurrent menus on different channels,
77 //      we must make these into lists.  then the timeouts should apply
78 //      individually to these instead of overall (if we even do timeouts;
79 //      it's nicer if menus never stop being active).
80
81
82 // displays the menu requested.  it's "menu_name" is an internal name that is
83 // not displayed to the user.  the "title" is the content shown in the main area
84 // of the menu.  "commands_in" is the list of menu items to show as buttons.
85 // the "menu_channel" is where the user's clicked response will be sent.  the
86 // "listen_to" key is the avatar expected to click the menu, which is needed to
87 // listen to his response.
88 show_menu(string menu_name, string title, list buttons,
89     integer menu_channel, key listen_to)
90 {
91     // save our new parms.
92     global_menu_name = menu_name;
93     _private_global_title = title;
94     _private_global_buttons = buttons;
95     menu_system_channel = menu_channel;
96     _private_global_av_key = listen_to;
97     if (DEBUGGING) {
98         log_it("menu name: " + global_menu_name);
99         log_it("title: " + _private_global_title);
100         log_it("buttons: " + (string)buttons);
101         log_it("channel: " + (string)menu_system_channel);
102         log_it("listen key: " + (string)listen_to);
103     }
104
105     integer add_next = FALSE;  // true if we should add a next menu item.
106
107     // the math here incorporates current button position.
108     integer current = _menu_base_start;
109     integer max_buttons = llGetListLength(buttons) - current;
110
111     if (max_buttons > 12) {
112         // limitation of SL: menus have a max of 12 buttons.
113         max_buttons = 12;
114         add_next = TRUE;
115     } else if (llGetListLength(buttons) > 12) {
116         // we already have been adding next.  let's make sure this gets
117         // a wrap-around next button.
118         add_next = TRUE;
119     }
120     // chop out what we can use in a menu.
121     list trunc_buttons = llList2List(buttons, current, current + max_buttons - 1);
122     if (add_next) {
123         // we were asked to add a menu item for the next screen.
124         trunc_buttons = llList2List(trunc_buttons, 0, 10) + NEXT_MENU_TEXT;
125     }
126
127     listening_id = llListen(menu_channel, "", listen_to, "");
128     list commands;
129     integer i;
130     // take only the prefix of the string, to avoid getting a length complaint.
131     for (i = 0; i < llGetListLength(trunc_buttons); i++) {
132         string curr = llList2String(trunc_buttons, i);
133         integer last_pos = 23;  // default maximum, highest possible is 24.
134         if (llStringLength(curr) - 1 < last_pos) last_pos = llStringLength(curr) - 1;
135         curr = llGetSubString(curr, 0, last_pos);
136         commands += curr;
137     }
138     llDialog(listen_to, title, commands, menu_channel);
139     llSetTimerEvent(TIMEOUT_FOR_MENU);
140 }
141
142 // shuts down any connection we might have had with any active menu.  we will not
143 // send any responses after this point (although we might already have responded when
144 // the user clicked the menu).
145 clear_menu()
146 {
147     llListenRemove(listening_id);
148     llSetTimerEvent(0.0);
149 }
150
151 // a simple version of a reply for a command that has been executed.  the parameters
152 // might contain an outcome or result of the operation that was requested.
153 // ours do differ from normal in that we send back the channel as the number parameter
154 // instead of enforcing that being MENU_HUFFWARE_ID.
155 send_reply(integer destination, integer channel, list parms, string command)
156 {
157     llMessageLinked(destination, channel, command,
158         llDumpList2String(parms, HUFFWARE_PARM_SEPARATOR));
159 }
160
161 // processes the menu requests.
162 handle_link_message(integer sender, integer huff_id, string msg, key id)
163 {
164     if (huff_id != MENUTINI_HUFFWARE_ID) return;  // not for us.
165
166     if (msg == SHOW_MENU_COMMAND) {
167         _menu_base_start = 0;  // reset the position in the menus.
168         // separate the list out.
169 //log_it("id showing: " + id);
170         list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
171         // toss any existing menu info.
172         clear_menu();
173 //log_it("key here early: " + llList2String(parms, 4));
174         show_menu(llList2String(parms, 0), llList2String(parms, 1),
175             llParseString2List(llList2String(parms, 2),
176                 [HUFFWARE_ITEM_SEPARATOR], []),
177             (integer)llList2String(parms, 3),
178             (key)llList2String(parms, 4));
179     }
180 }
181
182 // process the response when the user chooses a menu item.  this causes our
183 // caller to be told what was selected.
184 process_menu_response(integer channel, string name, key id, string message)
185 {
186     if (channel != menu_system_channel) return;  // not for us.
187
188     if (message == NEXT_MENU_TEXT) {
189         // this is the special choice, so we need to go to the next page.
190         _menu_base_start += 11;
191         if (_menu_base_start > llGetListLength(_private_global_buttons)) {
192             // we have wrapped around the list.  go to the start again.
193             _menu_base_start = 0;
194         }
195         show_menu(global_menu_name, _private_global_title,
196             _private_global_buttons, menu_system_channel,
197             _private_global_av_key);
198         return;  // handled by opening a new menu.
199     }
200     
201     string calculated_name;
202     integer indy;
203     // first try for an exact match.
204     for (indy = 0; indy < llGetListLength(_private_global_buttons); indy++) {
205         string curr = llList2String(_private_global_buttons, indy);
206         if (curr == message) {
207             // correct the answer based on the full button string.
208             calculated_name = curr;
209         }
210     }
211     if (calculated_name == "") {
212         // try an imprecise match if the exact matching didn't work.
213         for (indy = 0; indy < llGetListLength(_private_global_buttons); indy++) {
214             string curr = llList2String(_private_global_buttons, indy);
215             if (is_prefix(curr, message)) {
216                 // correct the answer based on the full button string.
217                 calculated_name = curr;
218             }
219         }
220     }
221     if (calculated_name != "") {
222         // only send a response if that menu choice made sense to us.
223         send_reply(LINK_THIS, MENUTINI_HUFFWARE_ID + REPLY_DISTANCE,
224             [ global_menu_name, calculated_name, _private_global_av_key ],
225             SHOW_MENU_COMMAND);
226     }
227 }
228
229 //////////////
230 // from hufflets...
231
232 integer debug_num = 0;
233
234 // a debugging output method.  can be disabled entirely in one place.
235 log_it(string to_say)
236 {
237     debug_num++;
238     // tell this to the owner.    
239     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
240     // say this on open chat, but use an unusual channel.
241 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
242 }
243
244 //////////////
245
246 // returns TRUE if the "prefix" string is the first part of "compare_with".
247 integer is_prefix(string compare_with, string prefix)
248 { return (llSubStringIndex(compare_with, prefix) == 0); }
249
250 //////////////
251
252 //////////////
253 // huffware script: auto-retire, by fred huffhines, version 2.8.
254 // distributed under BSD-like license.
255 //   !!  keep in mind that this code must be *copied* into another
256 //   !!  script that you wish to add auto-retirement capability to.
257 // when a script has auto_retire in it, it can be dropped into an
258 // object and the most recent version of the script will destroy
259 // all older versions.
260 //
261 // the version numbers are embedded into the script names themselves.
262 // the notation for versions uses a letter 'v', followed by two numbers
263 // in the form "major.minor".
264 // major and minor versions are implicitly considered as a floating point
265 // number that increases with each newer version of the script.  thus,
266 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
267 // and "hazmap v3.2" is a more recent version.
268 //
269 // example usage of the auto-retirement script:
270 //     default {
271 //         state_entry() {
272 //            auto_retire();  // make sure newest addition is only version of script.
273 //        }
274 //     }
275 // this script is partly based on the self-upgrading scripts from markov brodsky
276 // and jippen faddoul.
277 //////////////
278 auto_retire() {
279     string self = llGetScriptName();  // the name of this script.
280     list split = compute_basename_and_version(self);
281     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
282     string basename = llList2String(split, 0);  // script name with no version attached.
283     string version_string = llList2String(split, 1);  // the version found.
284     integer posn;
285     // find any scripts that match the basename.  they are variants of this script.
286     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
287         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
288         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
289             // found a basic match at least.
290             list inv_split = compute_basename_and_version(curr_script);
291             if (llGetListLength(inv_split) == 2) {
292                 // see if this script is more ancient.
293                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
294                 // must make sure that the retiring script is completely the identical basename;
295                 // just matching in the front doesn't make it a relative.
296                 if ( (llList2String(inv_split, 0) == basename)
297                     && ((float)inv_version_string < (float)version_string) ) {
298                     // remove script with same name from inventory that has inferior version.
299                     llRemoveInventory(curr_script);
300                 }
301             }
302         }
303     }
304 }
305 //
306 // separates the base script name and version number.  used by auto_retire.
307 list compute_basename_and_version(string to_chop_up)
308 {
309     // minimum script name is 2 characters plus a version.
310     integer space_v_posn;
311     // find the last useful space and 'v' combo.
312     for (space_v_posn = llStringLength(to_chop_up) - 3;
313         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
314         space_v_posn--) {
315         // look for space and v but do nothing else.
316     }
317     if (space_v_posn < 2) return [];  // no space found.
318     // now we zoom through the stuff after our beloved v character and find any evil
319     // space characters, which are most likely from SL having found a duplicate item
320     // name and not so helpfully renamed it for us.
321     integer indy;
322     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
323         if (llGetSubString(to_chop_up, indy, indy) == " ") {
324             // found one; zap it.  since we're going backwards we don't need to
325             // adjust the loop at all.
326             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
327         }
328     }
329     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
330     // ditch the space character for our numerical check.
331     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
332     // strip out a 'v' if there is one.
333     if (llGetSubString(chop_suffix, 0, 0) == "v")
334         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
335     // if valid floating point number and greater than zero, that works for our version.
336     string basename = to_chop_up;  // script name with no version attached.
337     if ((float)chop_suffix > 0.0) {
338         // this is a big success right here.
339         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
340         return [ basename, chop_suffix ];
341     }
342     // seems like we found nothing useful.
343     return [];
344 }
345 //
346 //////////////
347
348 //hmmm: extract this code to a menutini example!
349
350 //////////////
351 // how to invoke a menu (assuming menutini is in same prim as calling script):
352 //
353 list buttons;  // holds onto the set of menu options.
354 //
355 integer random_channel() { return -(integer)(llFrand(40000) + 20000); }
356 //
357 example_invocation()
358 {
359     string menu_name = "grumfazoid";
360     string title = "These united colors of ben's futon have unfortunately run.";
361     buttons = [ "garp out", "sklonar", "fuzzlenog" ];
362     integer menu_channel = random_channel();
363     key listen_to = llGetOwner();
364     llMessageLinked(LINK_THIS, MENUTINI_HUFFWARE_ID, SHOW_MENU_COMMAND,
365         menu_name + HUFFWARE_PARM_SEPARATOR
366         + title + HUFFWARE_PARM_SEPARATOR
367         + wrap_item_list(buttons) + HUFFWARE_PARM_SEPARATOR
368         + (string)menu_channel + HUFFWARE_PARM_SEPARATOR
369         + (string)listen_to);
370 }
371 //
372 // how to handle the response message when the user chooses a button.
373 //
374 react_to_menu(string menu_name, string which_choice)
375 {
376     // one might use the menu_name when dealing with multiple different menus.
377
378     integer indy = 0;
379     // find the specified item and process it.
380     while (indy < llGetListLength(buttons)) {
381         // see if the current destination matches.
382         if (llSubStringIndex(llList2String(buttons, indy), which_choice) == 0) {
383             // this is the chosen item.
384 //            process_menu_item(indy);  // using numerical numbering.
385 // this function must be implemented in your own code; it is what handles the
386 // user picking a particular button on the menu.
387             return;            
388         }
389         indy++;
390     }
391     llSay(0, "did not find menu option");
392 }
393
394 // an example for menu handling.  this gets the response from menutini library
395 // and calls the menu processing method "react_to_menu".
396 example_handle_link_message(integer sender, integer num, string msg, key id)
397 {
398     if (num != MENUTINI_HUFFWARE_ID + REPLY_DISTANCE) return;  // not for us.
399     if (msg != SHOW_MENU_COMMAND) return;  // also not for us.
400     list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
401     string menu_name = llList2String(parms, 0);
402     string which_choice = llList2String(parms, 1);
403     react_to_menu(menu_name, which_choice);
404 }
405
406 // then inside a state, you need an event handler like so:
407 //
408 // link_message(integer sender, integer num, string msg, key id)
409 // { example_handle_link_message(sender, num, msg, id); }
410
411 //
412 // end invocation sample code...
413 //////////////
414
415 default
416 {
417     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
418     on_rez(integer parm) { state rerun; }
419 }
420 state rerun { state_entry() { state default; } }
421
422 state real_default
423 {
424     state_entry() { auto_retire(); }
425
426     link_message(integer sender, integer huff_id, string msg, key id)
427     { handle_link_message(sender, huff_id, msg, id); }
428
429     listen(integer channel, string name, key id, string message)
430     { process_menu_response(channel, name, id, message); }
431     
432     // if the timer goes off, then the user has ignored the menu for longer than the
433     // timeout.  we need to turn off our listen and ignore that menu.
434     timer() { clear_menu(); }
435 }
436