57a1e641b49cf28f46b42511e8397b69b2b4b093
[feisty_meow.git] / huffware / huffotronic_scripts / texture_mover_v3.0.txt
1 
2 // huffware script: texture mover, by fred huffhines
3 //
4 // moves a texture across the object, either by smooth animation or brute force offsetting.
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 integer USE_TEXTURE_ANIMATION = TRUE;
13     // by default, we use texture animation which is a lower-lag way to
14     // make the texture move.  if this is false, then we use a timed update
15     // to move the texture instead.
16     // note: these modes are actually really different.  the texture animation
17     // is included here because i wanted to smooth out some objects that used
18     // texture movement.
19
20 // used in both types...
21
22 float TIMER_INTERVAL = 0.2;  // how fast do we move texture?
23
24 // for timed movement...
25
26 vector OFFSET_MOVEMENT = ZERO_VECTOR;  // no movement.
27 //vector OFFSET_MOVEMENT = <0.0, -0.02, 0.0>;
28     // how much to move the texture by in x,y,z directions.
29
30 //float OFFSET_ROTATION = 0.0;  // in degrees.
31 float OFFSET_ROTATION = -1.0;  // in degrees.
32
33 // for texture animations...
34
35 float MOVER_TIMER_DIVISOR = 30.0;
36     // makes the movement comparable to timed method.
37
38 float ROTATER_TIMER_DIVISOR = 7.0;
39     // makes the rotation comparable to timed method.
40
41 // iterative movement of texture.
42 move_texture(vector offset)
43 {
44     vector current = llGetTextureOffset(ALL_SIDES);
45     current += offset;
46     if (current.x > 1.0) current.x = -1.0;
47     if (current.x < -1.0) current.x = 1.0;
48     if (current.y > 1.0) current.y = -1.0;
49     if (current.y < -1.0) current.y = 1.0;
50     llOffsetTexture(current.x, current.y, ALL_SIDES);
51 }
52
53 // iterative rotation of texture.  "spin" is measured in degrees.
54 spin_texture(float spin)
55 {
56     float rot = llGetTextureRot(ALL_SIDES);  // get our current state.
57     rot += spin * DEG_TO_RAD;  // add some rotation.
58     llRotateTexture(rot, ALL_SIDES); //rotate the object   
59 }
60
61 initialize_texture_mover()
62 {
63     if (!USE_TEXTURE_ANIMATION) {
64         // we're stuck with the timed update style for movement.
65         llSetTimerEvent(TIMER_INTERVAL);
66         // turn off previous animation.
67         llSetTextureAnim(0, ALL_SIDES, 0, 0, 0, 0, 0);
68     } else {
69         // we can just set the texture movement here and be done with it.
70         integer x_frames = 1;
71         integer y_frames = 1;
72         llSetTimerEvent(0);  // we don't use a timer.
73
74 //hmmm: how do we combine rotation and offsets?  currently mutually exclusive.
75
76         if (OFFSET_MOVEMENT != ZERO_VECTOR) {
77             float timer_interval = TIMER_INTERVAL / MOVER_TIMER_DIVISOR;
78 log_it("getting x_frames=" + (string)x_frames
79 + " y_frames=" + (string)y_frames
80 + " timer_intvl=" + (string)timer_interval);
81
82             llSetTextureAnim(ANIM_ON | LOOP | SMOOTH, ALL_SIDES,
83                 x_frames, y_frames,
84                 0, 100, timer_interval);
85         } else if (OFFSET_ROTATION != 0.0) {
86             float timer_interval = TIMER_INTERVAL / ROTATER_TIMER_DIVISOR;
87             // we're actually not using the rotation at all here, except for
88             // the sign.  that seems pretty busted.
89             if (OFFSET_ROTATION < 0) timer_interval *= -1.0;
90             llSetTextureAnim(ANIM_ON | LOOP | SMOOTH | ROTATE, ALL_SIDES,
91                 0, 0,
92                 0, TWO_PI, timer_interval);
93         }
94     }
95 }
96
97 //////////////
98 // from hufflets...
99
100 integer debug_num = 0;
101
102 // a debugging output method.  can be disabled entirely in one place.
103 log_it(string to_say)
104 {
105     debug_num++;
106     // tell this to the owner.    
107     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
108     // say this on an unusual channel for chat if it's not intended for general public.
109 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
110     // say this on open chat that anyone can hear.  we take off the bling for this one.
111 //    llSay(0, to_say);
112 }
113 //
114 //////////////
115
116 //////////////
117 // huffware script: auto-retire, by fred huffhines, version 2.5.
118 // distributed under BSD-like license.
119 //   !!  keep in mind that this code must be *copied* into another
120 //   !!  script that you wish to add auto-retirement capability to.
121 // when a script has auto_retire in it, it can be dropped into an
122 // object and the most recent version of the script will destroy
123 // all older versions.
124 //
125 // the version numbers are embedded into the script names themselves.
126 // the notation for versions uses a letter 'v', followed by two numbers
127 // in the form "major.minor".
128 // major and minor versions are implicitly considered as a floating point
129 // number that increases with each newer version of the script.  thus,
130 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
131 // and "hazmap v3.2" is a more recent version.
132 //
133 // example usage of the auto-retirement script:
134 //     default {
135 //         state_entry() {
136 //            auto_retire();  // make sure newest addition is only version of script.
137 //        }
138 //     }
139 // this script is partly based on the self-upgrading scripts from markov brodsky
140 // and jippen faddoul.
141 //////////////
142 auto_retire() {
143     string self = llGetScriptName();  // the name of this script.
144     list split = compute_basename_and_version(self);
145     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
146     string basename = llList2String(split, 0);  // script name with no version attached.
147     string version_string = llList2String(split, 1);  // the version found.
148     integer posn;
149     // find any scripts that match the basename.  they are variants of this script.
150     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
151 //log_it("invpo=" + (string)posn);
152         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
153         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
154             // found a basic match at least.
155             list inv_split = compute_basename_and_version(curr_script);
156             if (llGetListLength(inv_split) == 2) {
157                 // see if this script is more ancient.
158                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
159                 // must make sure that the retiring script is completely the identical basename;
160                 // just matching in the front doesn't make it a relative.
161                 if ( (llList2String(inv_split, 0) == basename)
162                     && ((float)inv_version_string < (float)version_string) ) {
163                     // remove script with same name from inventory that has inferior version.
164                     llRemoveInventory(curr_script);
165                 }
166             }
167         }
168     }
169 }
170 //
171 // separates the base script name and version number.  used by auto_retire.
172 list compute_basename_and_version(string to_chop_up)
173 {
174     // minimum script name is 2 characters plus a version.
175     integer space_v_posn;
176     // find the last useful space and 'v' combo.
177     for (space_v_posn = llStringLength(to_chop_up) - 3;
178         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
179         space_v_posn--) {
180         // look for space and v but do nothing else.
181 //log_it("pos=" + (string)space_v_posn);
182     }
183     if (space_v_posn < 2) return [];  // no space found.
184 //log_it("space v@" + (string)space_v_posn);
185     // now we zoom through the stuff after our beloved v character and find any evil
186     // space characters, which are most likely from SL having found a duplicate item
187     // name and not so helpfully renamed it for us.
188     integer indy;
189     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
190 //log_it("indy=" + (string)space_v_posn);
191         if (llGetSubString(to_chop_up, indy, indy) == " ") {
192             // found one; zap it.  since we're going backwards we don't need to
193             // adjust the loop at all.
194             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
195 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
196         }
197     }
198     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
199     // ditch the space character for our numerical check.
200     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
201     // strip out a 'v' if there is one.
202     if (llGetSubString(chop_suffix, 0, 0) == "v")
203         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
204     // if valid floating point number and greater than zero, that works for our version.
205     string basename = to_chop_up;  // script name with no version attached.
206     if ((float)chop_suffix > 0.0) {
207         // this is a big success right here.
208         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
209         return [ basename, chop_suffix ];
210     }
211     // seems like we found nothing useful.
212     return [];
213 }
214 //
215 //////////////
216
217 default {
218     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
219     on_rez(integer parm) { state rerun; }
220 }
221 state rerun { state_entry() { state default; } }
222
223 state real_default
224 {
225     state_entry() {
226         auto_retire();
227         initialize_texture_mover();
228     }
229
230     timer() {
231         // the timed approach is the only one so far that allows both a
232         // movement of the texture and a rotation.
233         if (OFFSET_MOVEMENT != ZERO_VECTOR) move_texture(OFFSET_MOVEMENT);
234         if (OFFSET_ROTATION != 0.0) spin_texture(OFFSET_ROTATION);
235     }
236 }
237