d550a2772e66e0b882026aed36e787ce3265d205
[feisty_meow.git] / huffware / huffotronic_scripts / clear_text_and_effects_v2.2.txt
1 
2 // huffware script: clear text and effects, by fred huffhines
3 //
4 // a simple helper for cleaning the text off of objects after a script has
5 // set the sit text or main text.  it also stops any particle systems that
6 // had been running previously and stops any sensors.
7 //
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.
10 //
11
12 //////////////
13 // huffware script: auto-retire, by fred huffhines, version 2.5.
14 // distributed under BSD-like license.
15 //   !!  keep in mind that this code must be *copied* into another
16 //   !!  script that you wish to add auto-retirement capability to.
17 // when a script has auto_retire in it, it can be dropped into an
18 // object and the most recent version of the script will destroy
19 // all older versions.
20 //
21 // the version numbers are embedded into the script names themselves.
22 // the notation for versions uses a letter 'v', followed by two numbers
23 // in the form "major.minor".
24 // major and minor versions are implicitly considered as a floating point
25 // number that increases with each newer version of the script.  thus,
26 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
27 // and "hazmap v3.2" is a more recent version.
28 //
29 // example usage of the auto-retirement script:
30 //     default {
31 //         state_entry() {
32 //            auto_retire();  // make sure newest addition is only version of script.
33 //        }
34 //     }
35 // this script is partly based on the self-upgrading scripts from markov brodsky
36 // and jippen faddoul.
37 //////////////
38 auto_retire() {
39     string self = llGetScriptName();  // the name of this script.
40     list split = compute_basename_and_version(self);
41     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
42     string basename = llList2String(split, 0);  // script name with no version attached.
43     string version_string = llList2String(split, 1);  // the version found.
44     integer posn;
45     // find any scripts that match the basename.  they are variants of this script.
46     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
47 //log_it("invpo=" + (string)posn);
48         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
49         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
50             // found a basic match at least.
51             list inv_split = compute_basename_and_version(curr_script);
52             if (llGetListLength(inv_split) == 2) {
53                 // see if this script is more ancient.
54                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
55                 // must make sure that the retiring script is completely the identical basename;
56                 // just matching in the front doesn't make it a relative.
57                 if ( (llList2String(inv_split, 0) == basename)
58                     && ((float)inv_version_string < (float)version_string) ) {
59                     // remove script with same name from inventory that has inferior version.
60                     llRemoveInventory(curr_script);
61                 }
62             }
63         }
64     }
65 }
66 //
67 // separates the base script name and version number.  used by auto_retire.
68 list compute_basename_and_version(string to_chop_up)
69 {
70     // minimum script name is 2 characters plus a version.
71     integer space_v_posn;
72     // find the last useful space and 'v' combo.
73     for (space_v_posn = llStringLength(to_chop_up) - 3;
74         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
75         space_v_posn--) {
76         // look for space and v but do nothing else.
77 //log_it("pos=" + (string)space_v_posn);
78     }
79     if (space_v_posn < 2) return [];  // no space found.
80 //log_it("space v@" + (string)space_v_posn);
81     // now we zoom through the stuff after our beloved v character and find any evil
82     // space characters, which are most likely from SL having found a duplicate item
83     // name and not so helpfully renamed it for us.
84     integer indy;
85     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
86 //log_it("indy=" + (string)space_v_posn);
87         if (llGetSubString(to_chop_up, indy, indy) == " ") {
88             // found one; zap it.  since we're going backwards we don't need to
89             // adjust the loop at all.
90             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
91 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
92         }
93     }
94     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
95     // ditch the space character for our numerical check.
96     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
97     // strip out a 'v' if there is one.
98     if (llGetSubString(chop_suffix, 0, 0) == "v")
99         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
100     // if valid floating point number and greater than zero, that works for our version.
101     string basename = to_chop_up;  // script name with no version attached.
102     if ((float)chop_suffix > 0.0) {
103         // this is a big success right here.
104         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
105         return [ basename, chop_suffix ];
106     }
107     // seems like we found nothing useful.
108     return [];
109 }
110 //
111 //////////////
112
113 default
114 {
115     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
116     on_rez(integer parm) { state rerun; }
117 }
118 state rerun { state_entry() { state default; } }
119
120 state real_default
121 {
122     state_entry()
123     {
124         auto_retire();
125
126         llSetText("", <0,0,0>, 0);  // clear any text above object.
127         llSetSitText("Sit Here");  // reset sit text to default state.
128         llSetTouchText("Touch");  // similarly for touch.
129         llSitTarget(ZERO_VECTOR, ZERO_ROTATION);  // reset sit target position.
130
131         llParticleSystem([]);  // turn off all particles.
132
133         llSensorRemove();  // stop any running sensors.
134
135         llTargetOmega(<0.0, 0.0, 0.0>, 0, 1.0);  // stop rotations of object.
136
137         llSetTextureAnim(0, ALL_SIDES, 0, 0, 0, 0, 0);  // stop texture animations.
138
139         llSetLinkAlpha(LINK_SET, 1.0, ALL_SIDES);  // turn off any transparency.
140         llSetLinkColor(LINK_SET, <1.0, 1.0, 1.0>, ALL_SIDES);  // reset color to white.
141
142         // keep it from being physical and from disapparating.
143         llSetPrimitiveParams([PRIM_TEMP_ON_REZ, FALSE, PRIM_PHYSICS, FALSE]);
144     }
145
146     // make sure that we apply these settings when the object is rezzed, if it
147     // had been stored with annoying text or whatever and needs to be cleared.
148     on_rez(integer parm) { llResetScript(); }
149 }
150