moving this to a better name in a better location.
[feisty_meow.git] / huffware / huffotronic_eepaw_knowledge_v60.9 / text_label_v3.9.txt
1 
2 // huffware script: text label, by fred huffhines
3 //
4 // a super simple script for giving an object readable text.
5 //
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.
8 //
9
10 string OBJECT_LABEL = "default";  // change this if you want a specific name.
11
12 vector LABEL_COLOR = <0.3, 0.9, 0.4>;  // color of the text above object.
13
14 set_text()
15 {
16     string object_label = OBJECT_LABEL;
17     // reset the label to a decorated version of object name if it was default.
18     if (object_label == "default") object_label = llGetObjectName();
19     integer indy;
20     integer keep_going = TRUE;
21     while (keep_going) {
22         indy = find_substring(object_label, "\\n");
23         if (indy < 0) {
24             keep_going = FALSE;
25         } else {
26             object_label = llGetSubString(object_label, 0, indy - 1)
27                 + "\n" + llGetSubString(object_label, indy + 2, -1);
28         }
29     }
30 //log_it("setting text: " + object_label);
31     llSetText(object_label, LABEL_COLOR, 1.0);
32 }
33
34 //////////////
35 // from hufflets...
36
37 integer debug_num = 0;
38
39 // a debugging output method.  can be disabled entirely in one place.
40 log_it(string to_say)
41 {
42     debug_num++;
43     // tell this to the owner.    
44     llOwnerSay(llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
45 //llWhisper(0, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
46     // say this on an unusual channel for chat if it's not intended for general public.
47 //    llSay(108, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
48     // say this on open chat that anyone can hear.  we take off the bling for this one.
49 //    llSay(0, to_say);
50 }
51
52 // returns the index of the first occurrence of "pattern" inside
53 // the "full_string".  if it is not found, then a negative number is returned.
54 integer find_substring(string full_string, string pattern)
55 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
56
57 //////////////
58 // huffware script: auto-retire, by fred huffhines, version 2.8.
59 // distributed under BSD-like license.
60 //   !!  keep in mind that this code must be *copied* into another
61 //   !!  script that you wish to add auto-retirement capability to.
62 // when a script has auto_retire in it, it can be dropped into an
63 // object and the most recent version of the script will destroy
64 // all older versions.
65 //
66 // the version numbers are embedded into the script names themselves.
67 // the notation for versions uses a letter 'v', followed by two numbers
68 // in the form "major.minor".
69 // major and minor versions are implicitly considered as a floating point
70 // number that increases with each newer version of the script.  thus,
71 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
72 // and "hazmap v3.2" is a more recent version.
73 //
74 // example usage of the auto-retirement script:
75 //     default {
76 //         state_entry() {
77 //            auto_retire();  // make sure newest addition is only version of script.
78 //        }
79 //     }
80 // this script is partly based on the self-upgrading scripts from markov brodsky
81 // and jippen faddoul.
82 //////////////
83 auto_retire() {
84     string self = llGetScriptName();  // the name of this script.
85     list split = compute_basename_and_version(self);
86     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
87     string basename = llList2String(split, 0);  // script name with no version attached.
88     string version_string = llList2String(split, 1);  // the version found.
89     integer posn;
90     // find any scripts that match the basename.  they are variants of this script.
91     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
92         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
93         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
94             // found a basic match at least.
95             list inv_split = compute_basename_and_version(curr_script);
96             if (llGetListLength(inv_split) == 2) {
97                 // see if this script is more ancient.
98                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
99                 // must make sure that the retiring script is completely the identical basename;
100                 // just matching in the front doesn't make it a relative.
101                 if ( (llList2String(inv_split, 0) == basename)
102                     && ((float)inv_version_string < (float)version_string) ) {
103                     // remove script with same name from inventory that has inferior version.
104                     llRemoveInventory(curr_script);
105                 }
106             }
107         }
108     }
109 }
110 //
111 // separates the base script name and version number.  used by auto_retire.
112 list compute_basename_and_version(string to_chop_up)
113 {
114     // minimum script name is 2 characters plus a version.
115     integer space_v_posn;
116     // find the last useful space and 'v' combo.
117     for (space_v_posn = llStringLength(to_chop_up) - 3;
118         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
119         space_v_posn--) {
120         // look for space and v but do nothing else.
121     }
122     if (space_v_posn < 2) return [];  // no space found.
123     // now we zoom through the stuff after our beloved v character and find any evil
124     // space characters, which are most likely from SL having found a duplicate item
125     // name and not so helpfully renamed it for us.
126     integer indy;
127     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
128         if (llGetSubString(to_chop_up, indy, indy) == " ") {
129             // found one; zap it.  since we're going backwards we don't need to
130             // adjust the loop at all.
131             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
132         }
133     }
134     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
135     // ditch the space character for our numerical check.
136     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
137     // strip out a 'v' if there is one.
138     if (llGetSubString(chop_suffix, 0, 0) == "v")
139         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
140     // if valid floating point number and greater than zero, that works for our version.
141     string basename = to_chop_up;  // script name with no version attached.
142     if ((float)chop_suffix > 0.0) {
143         // this is a big success right here.
144         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
145         return [ basename, chop_suffix ];
146     }
147     // seems like we found nothing useful.
148     return [];
149 }
150 //
151 //////////////
152
153 //end hufflets...
154 //////////////
155
156 default
157 {
158     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
159     on_rez(integer parm) { state rerun; }
160 }
161 state rerun { state_entry() { state default; } }
162
163 state real_default
164 {
165     state_entry() { auto_retire(); set_text(); }
166     on_rez(integer start_parm) { state default; }
167     touch_start(integer count) { set_text(); }
168     changed(integer change) { if (change & CHANGED_INVENTORY) set_text(); }
169 }
170