normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / text_label_v4.1.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 string OBJECT_LABEL = "default";  // change this if you want a specific name.
10
11 float MAX_COLOR_VALUE = 1;  // brightest color component (R, G or B) for the text.
12 float MIN_COLOR_VALUE = 0.4;  // darkest color component (R, G or B) for the text.
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     vector label_color = random_vector(MIN_COLOR_VALUE, MAX_COLOR_VALUE, FALSE);
32     llSetText(object_label, label_color, 1.0);
33 }
34
35 //////////////
36 // from hufflets...
37
38 integer debug_num = 0;
39
40 // a debugging output method.  can be disabled entirely in one place.
41 log_it(string to_say)
42 {
43     debug_num++;
44     // tell this to the owner.    
45     llOwnerSay(llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
46 //llWhisper(0, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
47     // say this on an unusual channel for chat if it's not intended for general public.
48 //    llSay(108, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
49     // say this on open chat that anyone can hear.  we take off the bling for this one.
50 //    llSay(0, to_say);
51 }
52
53 // returns the index of the first occurrence of "pattern" inside
54 // the "full_string".  if it is not found, then a negative number is returned.
55 integer find_substring(string full_string, string pattern)
56 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
57
58 // returns a number at most "maximum" and at least "minimum".
59 // if "allow_negative" is TRUE, then the return may be positive or negative.
60 float randomize_within_range(float minimum, float maximum, integer allow_negative)
61 {
62     if (minimum > maximum) {
63         // flip the two if they are reversed.
64         float temp = minimum; minimum = maximum; maximum = temp;
65     }
66     float to_return = minimum + llFrand(maximum - minimum);
67     if (allow_negative) {
68         if (llFrand(1.0) < 0.5) to_return *= -1.0;
69     }
70     return to_return;
71 }
72
73 // returns a random vector where x,y,z will be between "minimums" and "maximums"
74 // x,y,z components.  if "allow_negative" is true, then any component will
75 // randomly be negative or positive.
76 vector random_bound_vector(vector minimums, vector maximums, integer allow_negative)
77 {
78     return <randomize_within_range(minimums.x, maximums.x, allow_negative),
79         randomize_within_range(minimums.y, maximums.y, allow_negative),
80         randomize_within_range(minimums.z, maximums.z, allow_negative)>;
81 }
82
83 // returns a vector whose components are between minimum and maximum.
84 // if allow_negative is true, then they can be either positive or negative.
85 vector random_vector(float minimum, float maximum, integer allow_negative)
86 {
87     return random_bound_vector(<minimum, minimum, minimum>,
88         <maximum, maximum, maximum>, allow_negative);
89 }
90
91 //////////////
92 // huffware script: auto-retire, by fred huffhines, version 2.8.
93 // distributed under BSD-like license.
94 //   !!  keep in mind that this code must be *copied* into another
95 //   !!  script that you wish to add auto-retirement capability to.
96 // when a script has auto_retire in it, it can be dropped into an
97 // object and the most recent version of the script will destroy
98 // all older versions.
99 //
100 // the version numbers are embedded into the script names themselves.
101 // the notation for versions uses a letter 'v', followed by two numbers
102 // in the form "major.minor".
103 // major and minor versions are implicitly considered as a floating point
104 // number that increases with each newer version of the script.  thus,
105 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
106 // and "hazmap v3.2" is a more recent version.
107 //
108 // example usage of the auto-retirement script:
109 //     default {
110 //         state_entry() {
111 //            auto_retire();  // make sure newest addition is only version of script.
112 //        }
113 //     }
114 // this script is partly based on the self-upgrading scripts from markov brodsky
115 // and jippen faddoul.
116 //////////////
117 auto_retire() {
118     string self = llGetScriptName();  // the name of this script.
119     list split = compute_basename_and_version(self);
120     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
121     string basename = llList2String(split, 0);  // script name with no version attached.
122     string version_string = llList2String(split, 1);  // the version found.
123     integer posn;
124     // find any scripts that match the basename.  they are variants of this script.
125     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
126         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
127         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
128             // found a basic match at least.
129             list inv_split = compute_basename_and_version(curr_script);
130             if (llGetListLength(inv_split) == 2) {
131                 // see if this script is more ancient.
132                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
133                 // must make sure that the retiring script is completely the identical basename;
134                 // just matching in the front doesn't make it a relative.
135                 if ( (llList2String(inv_split, 0) == basename)
136                     && ((float)inv_version_string < (float)version_string) ) {
137                     // remove script with same name from inventory that has inferior version.
138                     llRemoveInventory(curr_script);
139                 }
140             }
141         }
142     }
143 }
144 //
145 // separates the base script name and version number.  used by auto_retire.
146 list compute_basename_and_version(string to_chop_up)
147 {
148     // minimum script name is 2 characters plus a version.
149     integer space_v_posn;
150     // find the last useful space and 'v' combo.
151     for (space_v_posn = llStringLength(to_chop_up) - 3;
152         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
153         space_v_posn--) {
154         // look for space and v but do nothing else.
155     }
156     if (space_v_posn < 2) return [];  // no space found.
157     // now we zoom through the stuff after our beloved v character and find any evil
158     // space characters, which are most likely from SL having found a duplicate item
159     // name and not so helpfully renamed it for us.
160     integer indy;
161     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
162         if (llGetSubString(to_chop_up, indy, indy) == " ") {
163             // found one; zap it.  since we're going backwards we don't need to
164             // adjust the loop at all.
165             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
166         }
167     }
168     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
169     // ditch the space character for our numerical check.
170     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
171     // strip out a 'v' if there is one.
172     if (llGetSubString(chop_suffix, 0, 0) == "v")
173         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
174     // if valid floating point number and greater than zero, that works for our version.
175     string basename = to_chop_up;  // script name with no version attached.
176     if ((float)chop_suffix > 0.0) {
177         // this is a big success right here.
178         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
179         return [ basename, chop_suffix ];
180     }
181     // seems like we found nothing useful.
182     return [];
183 }
184 //
185 //////////////
186
187 //end hufflets...
188 //////////////
189
190 default
191 {
192     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
193     on_rez(integer parm) { state rerun; }
194 }
195 state rerun { state_entry() { state default; } }
196
197 state real_default
198 {
199     state_entry() { auto_retire(); set_text(); }
200     on_rez(integer start_parm) { state default; }
201     touch_start(integer count) { set_text(); }
202     changed(integer change) { if (change & CHANGED_INVENTORY) set_text(); }
203 }
204