normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / giftorse_v7.6.txt
1 
2 // huffware script: giftorse, by fred huffhines
3 //
4 // this script advertises a product in a store.
5 // how to configure this script: the object name is known automatically, but the seller
6 // and store names are not.  also the cost is not currently automatic, since there seems
7 // to be no way to figure it out.  all of the variable data can be stored in a notecard
8 // in the following format:
9 //
10 //   #giftorse
11 //   cost = N
12 //   creator = X
13 //   shop = S
14 //   label = L
15 //
16 // where N is a numerical cost for the item in linden dollars, X is the name of the creator
17 // of the object, S is the name of the shop where the object is being sold, and L is a label
18 // to show above the object.
19 //
20 // this script supports a sales paradigm, where normally it just shows the
21 // appropriate title, but once the customer has bought it, it will permanently set
22 // it's last title and exit, but not before telling the user how to get the contents.
23 // this script depends on the "non-script giver" script also being in the same object.
24 // that's what let's it promise that the stuff will be given out on touch.
25 //
26 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
27 // do not use it in objects without fully realizing you are implicitly accepting that license.
28 //
29
30
31 // every product has a different cost.
32 string DEFAULT_COST = "**UNSET**";
33 string OBJ_COST;
34
35 // these are hard-coded for my own shop, since it's more convenient for me.
36 // remember to change these to your name and store name.
37 string SCRIPT_CREATOR = "Fred Huffhines";  // uber-default, do not change.
38 string CREATOR = "Fred Huffhines";  // change this if you are not me.
39 string SHOP_NAME = "eepaw shop (Eclectic Electric Patterns and Widgets)";
40
41 // if this is anything other than "default", then the text is used as the label.
42 // if left as "default", then the object's own name is used as the label.
43 string OBJECT_LABEL = "default";
44
45 // the color of the text above the object.
46 vector TEXT_COLOR = <0.6, 0.7, 0.8>;
47
48 // how long should the text stay on the object after a potential customer clicks it?
49 float DELAY_BEFORE_CLEARING_TEXT = 10.0;
50     // a fairly liberal allowance for how long it might take to read the object.
51     // we are trying to balance information flow with how obnoxious people feel text labels are.
52
53 //////////////
54
55 // constants that should really stay constant, like they are now...
56
57 string GIFTORSE_SIGNATURE = "#giftorse";  // the expected first line of our notecards.
58
59 //////////////
60
61 // global variables...
62
63 string global_notecard_name;  // name of our notecard in the object's inventory.
64
65 integer response_code;  // set to uniquely identify the notecard read in progress.
66
67 // programmer friendly variables...
68
69 integer DEBUGGING = FALSE;
70     // if true, then the code will be noisy during its processing.
71
72 // requires noteworthy library v3.3 or higher.
73 //////////////
74 // do not redefine these constants.
75 integer NOTEWORTHY_HUFFWARE_ID = 10010;
76     // the unique id within the huffware system for the noteworthy 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 string BAD_NOTECARD_INDICATOR = "bad_notecard";
86     // indicates that the notecard reading process has failed to find an appropriate one.
87 string NOTECARD_READ_CONTINUATION = "continue!";
88     // returned as first parameter if there is still more data to handle.
89 //////////////
90 // commands available via the noteworthy library:
91 string READ_NOTECARD_COMMAND = "#read_note#";
92     // command used to tell the script to read notecards.  needs a signature to find
93     // in the card as the only parameter.  the signature can be empty or missing.
94     // the results will be fired back as the string value returned, which will have
95     // an embedded list that was read from the notecard.  this necessarily limits the
96     // size of the notecards that we can read and return.
97 //
98 // joins a list of parameters using the parameter sentinel for the library.
99 string wrap_parameters(list to_flatten)
100 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
101 //////////////
102
103 //////////////
104
105 // from hufflets...
106
107 integer debug_num = 0;
108
109 // a debugging output method.  can be disabled entirely in one place.
110 log_it(string to_say)
111 {
112     debug_num++;
113     // tell this to the owner.    
114     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
115     // say this on open chat, but use an unusual channel.
116 //    llSay(108, (string)debug_num + "- " + to_say);
117 }
118
119 /////////////
120
121 // returns a number at most "maximum" and at least "minimum".
122 // if "allow_negative" is TRUE, then the return may be positive or negative.
123 float randomize_within_range(float minimum, float maximum, integer allow_negative)
124 {
125     if (minimum > maximum) {
126         // flip the two if they are reversed.
127         float temp = minimum; minimum = maximum; maximum = temp;
128     }
129     float to_return = minimum + llFrand(maximum - minimum);
130     if (allow_negative) {
131         if (llFrand(1.0) < 0.5) to_return *= -1.0;
132     }
133     return to_return;
134 }
135
136 // returns a non-empty string if "to_check" defines a value for "variable_name".
137 // this must be in the form "X=Y", where X is the variable_name and Y is the value.
138 string get_variable_value(string to_check, string variable_name)
139 {
140     // clean initial spaces.
141     while (llGetSubString(to_check, 0, 0) == " ")
142         to_check = llDeleteSubString(to_check, 0, 0);
143     if (!is_prefix(to_check, variable_name)) return "";
144     to_check = llDeleteSubString(to_check, 0, llStringLength(variable_name) - 1);
145     // clean any spaces or valid assignment characters.
146     while ( (llGetSubString(to_check, 0, 0) == " ")
147             || (llGetSubString(to_check, 0, 0) == "=")
148             || (llGetSubString(to_check, 0, 0) == ",") )
149         to_check = llDeleteSubString(to_check, 0, 0);
150     if (DEBUGGING)
151         log_it("set " + variable_name + " = " + to_check);
152     string chewed_content = to_check;
153     integer indy;
154     for (indy = 0; indy < llStringLength(chewed_content); indy++) {
155         if (llGetSubString(chewed_content, indy, indy) == "\\") {
156             if (llGetSubString(chewed_content, indy+1, indy+1) == "n") {
157                 chewed_content = llGetSubString(chewed_content, 0, indy - 1)
158                     + "\n"
159                     + llGetSubString(chewed_content, indy + 2,
160                         llStringLength(chewed_content) - 1);
161             }
162         }
163     }
164     // return what's left of the string.
165     return chewed_content;
166 }
167
168 /////////////
169
170 // is the merchant/shop guy still the owner?
171 integer merchant_owns()
172 {
173     string owner_now = llToLower(llKey2Name(llGetOwner()));
174     if ( (owner_now != "") && (owner_now != llToLower(CREATOR))
175             && (owner_now != llToLower(SCRIPT_CREATOR) ) ) {
176         // we're not able to compare the owner to what we expect.
177         return FALSE;
178     }
179     return TRUE;
180 }
181
182 // returns the index of the first occurrence of "pattern" inside
183 // the "full_string".  if it is not found, then a negative number is returned.
184 integer find_substring(string full_string, string pattern)
185 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
186
187 // applies a text label above the object.
188 set_text(integer enabled)
189 {
190     if (enabled) {
191         string object_label = OBJECT_LABEL;
192         // reset the label to a decorated version of object name if it was default.
193         if (object_label == "default") {
194             list name_bits = compute_basename_and_version(llGetObjectName());
195             string name = llList2String(name_bits, 0);
196             if (name == "") name = llGetObjectName();  // didn't split, use the full name.
197             object_label = "[ " + name + " ]";
198         }
199 //hmmm: retired, since it's an untruth to people nearby who don't own it.
200 //        if (!merchant_owns()) object_label += "\n-\nClick on this object to get its contents.";
201         llSetText(object_label, TEXT_COLOR, 1.0);
202     } else {
203         // no label, no how.
204         llSetText("", TEXT_COLOR, 0.0);
205     }
206 }
207
208 // returns TRUE if the "prefix" string is the first part of "compare_with".
209 integer is_prefix(string compare_with, string prefix)
210 { return (llSubStringIndex(compare_with, prefix) == 0); }
211
212 parse_variable_definition(string to_parse)
213 {
214     string content;  // filled after finding a variable name.
215     if ( (content = get_variable_value(to_parse, "cost")) != "")
216         OBJ_COST = content;
217     else if ( (content = get_variable_value(to_parse, "creator")) != "")
218         CREATOR = content;
219     else if ( (content = get_variable_value(to_parse, "shop")) != "")
220         SHOP_NAME = content;
221     else if ( (content = get_variable_value(to_parse, "label")) != "")
222         OBJECT_LABEL = content;
223     else if ( (content = get_variable_value(to_parse, "text_color")) != "")
224         TEXT_COLOR = (vector)content;
225 }
226
227 // handles a set of information from the notecard about this product.
228 process_product_definition(list lines)
229 {
230     integer indy;
231     for (indy = 0; indy < llGetListLength(lines); indy++) {
232         string line = llList2String(lines, indy);
233         // try to interpret that as a variable setting.
234         parse_variable_definition(line);
235     }
236 }
237
238 // display information about the product in open chat.
239 show_product_info()
240 {
241     string product_name = llGetObjectName();
242     string cost_string = "L$" + OBJ_COST + ".";
243     if (OBJ_COST == "0") cost_string = "free!";
244     llSay(0, " is available for " + cost_string + "\n"
245         + "[Created by " + CREATOR + ", Sold by " + SHOP_NAME + "]");
246 }
247
248 //////////////
249 // huffware script: auto-retire, by fred huffhines, version 2.4.
250 // distributed under BSD-like license.
251 //   partly based on the self-upgrading scripts from markov brodsky and jippen faddoul.
252 // the function auto_retire() should be added *inside* a version numbered script that
253 // you wish to give the capability of self-upgrading.
254 //   this script supports a notation for versions embedded in script names where a 'v'
255 // is followed by a number in the form "major.minor", e.g. "grunkle script by ted v8.2".
256 // when the containing script is dropped into an object with a different version, the
257 // most recent version eats any existing ones.
258 //   keep in mind that this code must be *copied* into your script you wish to add
259 // auto-retirement capability to.
260 // example usage of the auto-retirement script:
261 //     default {
262 //         state_entry() {
263 //            auto_retire();  // make sure newest addition is only version of script.
264 //        }
265 //     }
266 auto_retire() {
267     string self = llGetScriptName();  // the name of this script.
268     list split = compute_basename_and_version(self);
269     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
270     string basename = llList2String(split, 0);  // script name with no version attached.
271     string version_string = llList2String(split, 1);  // the version found.
272     integer posn;
273     // find any scripts that match the basename.  they are variants of this script.
274     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
275 //log_it("invpo=" + (string)posn);
276         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
277         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
278             // found a basic match at least.
279             list inv_split = compute_basename_and_version(curr_script);
280             if (llGetListLength(inv_split) == 2) {
281                 // see if this script is more ancient.
282                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
283                 // must make sure that the retiring script is completely the identical basename;
284                 // just matching in the front doesn't make it a relative.
285                 if ( (llList2String(inv_split, 0) == basename)
286                     && ((float)inv_version_string < (float)version_string) ) {
287                     // remove script with same name from inventory that has inferior version.
288                     llRemoveInventory(curr_script);
289                 }
290             }
291         }
292     }
293 }
294 //
295 // separates the base script name and version number.  used by auto_retire.
296 list compute_basename_and_version(string to_chop_up)
297 {
298     // minimum script name is 2 characters plus a version.
299     integer space_v_posn;
300     // find the last useful space and 'v' combo.
301     for (space_v_posn = llStringLength(to_chop_up) - 3;
302         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
303         space_v_posn--) {
304         // look for space and v but do nothing else.
305 //log_it("pos=" + (string)space_v_posn);
306     }
307     if (space_v_posn < 2) return [];  // no space found.
308 //log_it("space v@" + (string)space_v_posn);
309     // now we zoom through the stuff after our beloved v character and find any evil
310     // space characters, which are most likely from SL having found a duplicate item
311     // name and not so helpfully renamed it for us.
312     integer indy;
313     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
314 //log_it("indy=" + (string)space_v_posn);
315         if (llGetSubString(to_chop_up, indy, indy) == " ") {
316             // found one; zap it.  since we're going backwards we don't need to
317             // adjust the loop at all.
318             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
319 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
320         }
321     }
322     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
323     // ditch the space character for our numerical check.
324     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
325     // strip out a 'v' if there is one.
326     if (llGetSubString(chop_suffix, 0, 0) == "v")
327         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
328     // if valid floating point number and greater than zero, that works for our version.
329     string basename = to_chop_up;  // script name with no version attached.
330     if ((float)chop_suffix > 0.0) {
331         // this is a big success right here.
332         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
333         return [ basename, chop_suffix ];
334     }
335     // seems like we found nothing useful.
336     return [];
337 }
338 //
339 //////////////
340
341 default
342 {
343     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
344     on_rez(integer parm) { state rerun; }
345 }
346 state rerun { state_entry() { state default; } }
347
348 state real_default
349 {
350     state_entry() {
351         auto_retire();
352         set_text(FALSE);  // no label to start out.
353         global_notecard_name = "";
354         response_code = 0;
355         OBJ_COST = DEFAULT_COST;  // reset in opensim friendly way.
356
357         // see if we can load a notecard for product info.
358         response_code = -1 * (integer)randomize_within_range(23, 80000, FALSE);
359         string parms_sent = wrap_parameters([GIFTORSE_SIGNATURE, response_code]);
360         llMessageLinked(LINK_THIS, NOTEWORTHY_HUFFWARE_ID, READ_NOTECARD_COMMAND,
361              parms_sent);
362         llSetTimerEvent(32);  // make sure that if we don't get a config, we try again.
363     }
364     
365     // processes link messages received from support libraries.
366     link_message(integer which, integer num, string msg, key id) {
367         if (num != NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE) return;  // not for us.
368         if (msg == READ_NOTECARD_COMMAND) {
369             // process the result of reading the notecard.
370             list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
371             string notecard_name = llList2String(parms, 0);
372             integer response_for = llList2Integer(parms, 1);
373             if (response_for != response_code) {
374                 // oops, this isn't for us.  stop looking at it.
375                 return;
376             }
377
378             integer done_reading = TRUE;
379             if (notecard_name == NOTECARD_READ_CONTINUATION) done_reading = FALSE;
380             if (notecard_name != BAD_NOTECARD_INDICATOR) {
381                 global_notecard_name = notecard_name;
382                 list lines = llDeleteSubList(parms, 0, 1);
383                 process_product_definition(lines);
384             } else {
385                 // we hated the notecards we found, or there were none.
386 //                log_it("There is no product definition found.  We will proceed with defaults.");
387 //                state describe_product_and_neigh;
388             }
389             if (done_reading) {
390                 // since we got our final response for config, stop the timer.
391                 llSetTimerEvent(0);
392                 state describe_product_and_neigh;
393             }
394         }
395     }
396
397     on_rez(integer parm) { llResetScript(); }
398     
399     timer() {
400         log_it("failed to read configuration; retrying.");
401         llResetScript();
402     }
403     
404 }
405
406 // this is the active state, once we have read our product configuration from
407 // a notecard.
408 state describe_product_and_neigh
409 {
410     state_entry() {
411         set_text(FALSE);  // clear our text label just in case.
412         if (OBJ_COST == DEFAULT_COST) {
413             log_it("resetting due to missing configuration.");
414             llResetScript();
415         }
416         show_product_info();
417         if (!merchant_owns()) {
418 //hmmm: potentially might want to allow for multiple merchants so your partners don't go nuts hearing this.
419             // rip this script out, since it's not intended for other folks.
420             llSay(0, "\n\nThanks for your purchase from " + SHOP_NAME
421                 + "!\nTo get your product's contents, just click this package.");
422         }
423     }
424     
425     on_rez(integer parm) { llResetScript(); }
426
427     touch_start(integer total_number) {
428         set_text(TRUE);  // let them see our name.
429         show_product_info();
430         llSetTimerEvent(DELAY_BEFORE_CLEARING_TEXT);
431     }
432
433     timer() {
434         // the only reason we should be here is if we are trying to clear up our text.
435         set_text(FALSE);
436         llSetTimerEvent(0);
437     }
438             
439     changed(integer change) {
440         if (change & CHANGED_INVENTORY) {
441             llSleep(3.14159265358);
442                 // snooze to ensure that we don't reset before a new version gets a
443                 // chance to remove this.
444             llResetScript();  // start over.
445         }
446     }
447 }
448
449 //eternal questions...
450
451 //hmmm: why can't we automatically get the object's sale price?