95e8902673fa9155446a0ce17f5b9044527429d0
[feisty_meow.git] / huffware / huffotronic_scripts / fade_prim_v3.2.txt
1 
2 // huffware script: fade prim, by fred huffhines
3 //
4 // makes an object fade in and out of visibility.
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 // constants...
11
12 float OPACITY_MIN = 0.4;  // the lowest value to allow the transparency to reach.
13 float OPACITY_MAX = 1.0;  // the highest value to allow the transparent
14   // if you set this above the max of 1.0, it keeps the object solid for longer.
15
16 float TIMER_INTERVAL = 1.5;  // how often do we adjust our appearance?
17
18 // variables...
19
20 float current_opacity = 0.42;  // what is our current opacity?
21 float current_adjustment = 0.2;  // amount added/removed each time.
22
23 //////////////
24 // from hufflets...
25
26 integer debug_num = 0;
27
28 // a debugging output method.  can be disabled entirely in one place.
29 log_it(string to_say)
30 {
31     debug_num++;
32     // tell this to the owner.    
33     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
34     // say this on an unusual channel for chat if it's not intended for general public.
35 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
36     // say this on open chat that anyone can hear.  we take off the bling for this one.
37 //    llSay(0, to_say);
38 }
39 //
40 //////////////
41
42 //////////////
43 // huffware script: auto-retire, by fred huffhines, version 2.4.
44 // distributed under BSD-like license.
45 //   partly based on the self-upgrading scripts from markov brodsky and jippen faddoul.
46 // the function auto_retire() should be added *inside* a version numbered script that
47 // you wish to give the capability of self-upgrading.
48 //   this script supports a notation for versions embedded in script names where a 'v'
49 // is followed by a number in the form "major.minor", e.g. "grunkle script by ted v8.2".
50 // when the containing script is dropped into an object with a different version, the
51 // most recent version eats any existing ones.
52 //   keep in mind that this code must be *copied* into your script you wish to add
53 // auto-retirement capability to.
54 // example usage of the auto-retirement script:
55 //     default {
56 //         state_entry() {
57 //            auto_retire();  // make sure newest addition is only version of script.
58 //        }
59 //     }
60 auto_retire() {
61     string self = llGetScriptName();  // the name of this script.
62     list split = compute_basename_and_version(self);
63     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
64     string basename = llList2String(split, 0);  // script name with no version attached.
65     string version_string = llList2String(split, 1);  // the version found.
66     integer posn;
67     // find any scripts that match the basename.  they are variants of this script.
68     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
69 //log_it("invpo=" + (string)posn);
70         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
71         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
72             // found a basic match at least.
73             list inv_split = compute_basename_and_version(curr_script);
74             if (llGetListLength(inv_split) == 2) {
75                 // see if this script is more ancient.
76                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
77                 // must make sure that the retiring script is completely the identical basename;
78                 // just matching in the front doesn't make it a relative.
79                 if ( (llList2String(inv_split, 0) == basename)
80                     && ((float)inv_version_string < (float)version_string) ) {
81                     // remove script with same name from inventory that has inferior version.
82                     llRemoveInventory(curr_script);
83                 }
84             }
85         }
86     }
87 }
88 //
89 // separates the base script name and version number.  used by auto_retire.
90 list compute_basename_and_version(string to_chop_up)
91 {
92     // minimum script name is 2 characters plus a version.
93     integer space_v_posn;
94     // find the last useful space and 'v' combo.
95     for (space_v_posn = llStringLength(to_chop_up) - 3;
96         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
97         space_v_posn--) {
98         // look for space and v but do nothing else.
99 //log_it("pos=" + (string)space_v_posn);
100     }
101     if (space_v_posn < 2) return [];  // no space found.
102 //log_it("space v@" + (string)space_v_posn);
103     // now we zoom through the stuff after our beloved v character and find any evil
104     // space characters, which are most likely from SL having found a duplicate item
105     // name and not so helpfully renamed it for us.
106     integer indy;
107     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
108 //log_it("indy=" + (string)space_v_posn);
109         if (llGetSubString(to_chop_up, indy, indy) == " ") {
110             // found one; zap it.  since we're going backwards we don't need to
111             // adjust the loop at all.
112             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
113 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
114         }
115     }
116     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
117     // ditch the space character for our numerical check.
118     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
119     // strip out a 'v' if there is one.
120     if (llGetSubString(chop_suffix, 0, 0) == "v")
121         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
122     // if valid floating point number and greater than zero, that works for our version.
123     string basename = to_chop_up;  // script name with no version attached.
124     if ((float)chop_suffix > 0.0) {
125         // this is a big success right here.
126         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
127         return [ basename, chop_suffix ];
128     }
129     // seems like we found nothing useful.
130     return [];
131 }
132 //
133 //////////////
134
135 default
136 {
137     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
138     on_rez(integer parm) { state rerun; }
139 }
140 state rerun { state_entry() { state default; } }
141
142 state real_default
143 {
144     state_entry() {
145         auto_retire();
146         llSetTimerEvent(TIMER_INTERVAL);
147     }
148
149     timer() {
150         llSetTimerEvent(0);
151         llSetLinkAlpha(LINK_SET, current_opacity, ALL_SIDES);
152         current_opacity += current_adjustment;
153         if (current_opacity < OPACITY_MIN) { current_opacity = OPACITY_MIN; current_adjustment *= -1.0; }
154         if (current_opacity > OPACITY_MAX) { current_opacity = OPACITY_MAX; current_adjustment *= -1.0; }
155         llSetTimerEvent(TIMER_INTERVAL);
156     }
157 }
158