f3201ee186776fa7db7ea775afd3308fe277ac90
[feisty_meow.git] / huffware / huffotronic_scripts / particle_projector_v3.1.txt
1 
2 // huffware script: particle projector, by fred huffhines.
3 //
4 // takes a texture from one side of the object and projects it in a forward X and upward Z direction.
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 that can be modified for different behavior.
11  
12 float TEXTURE_DISPLAY_LIFE_SPAN = 14.0;  // how long the texture should be shown.
13
14 integer SIDE_TO_ACQUIRE_TEXTURE_FROM = 1;  // which side of the object should give us our display texture?
15
16 float MAGNIFIER = 4.0;  // how much to expand the texture as it runs.
17
18 vector TEXTURE_ACCELERATION = <0.02, 0.0, 0.01>;  // change in speed of the texture over time.
19
20 // global variables used in the script.
21
22 string texture_to_display = "95c7bb7c-d777-26b1-4052-acd778bfcb40";  // a simple default texture.
23
24 // displays a particle system with a texture that gradually enlarges over the object.
25 texture_hallucination()
26 {
27     // get a bounding box around the object.
28     list bounds = llGetBoundingBox(llGetKey());
29     // calculate the size of the object, which will be our starting particle size.
30     vector size = (vector)llList2String(bounds, 1) - (vector)llList2String(bounds, 0);
31     float chosen_dimension = size.x;
32     if (size.y > chosen_dimension) chosen_dimension = size.y;
33     if (size.z > chosen_dimension) chosen_dimension = size.z;
34     llParticleSystem([
35         // properties regarding the generated particles.
36         PSYS_PART_FLAGS, PSYS_PART_BOUNCE_MASK | PSYS_PART_INTERP_SCALE_MASK,
37         PSYS_PART_START_SCALE, <chosen_dimension, chosen_dimension, 0.0>,
38         PSYS_PART_END_SCALE, <chosen_dimension * MAGNIFIER, chosen_dimension * MAGNIFIER, 0.0>,
39         PSYS_PART_MAX_AGE, TEXTURE_DISPLAY_LIFE_SPAN,
40         PSYS_PART_START_COLOR, <1.0, 1.0, 1.0>,
41         PSYS_PART_END_COLOR, <1.0, 1.0, 1.0>,
42         PSYS_PART_START_ALPHA, 1.0,
43         PSYS_PART_END_ALPHA, 1.0,
44         // properties for the particle system's source.
45         PSYS_SRC_MAX_AGE, TEXTURE_DISPLAY_LIFE_SPAN,
46         PSYS_SRC_TEXTURE, texture_to_display,
47         PSYS_SRC_ACCEL, TEXTURE_ACCELERATION,
48         PSYS_SRC_PATTERN, PSYS_SRC_PATTERN_DROP,
49         PSYS_SRC_BURST_PART_COUNT, 1,
50         PSYS_SRC_BURST_RADIUS, chosen_dimension,
51         PSYS_SRC_BURST_RATE, TEXTURE_DISPLAY_LIFE_SPAN,
52         PSYS_SRC_BURST_SPEED_MIN, 0.01,
53         PSYS_SRC_BURST_SPEED_MAX, 0.01,
54         PSYS_SRC_ANGLE_BEGIN, 0.0,
55         PSYS_SRC_ANGLE_END, 0.0,
56         PSYS_SRC_OMEGA, <0.0, 0.0, 0.0>
57     ]);
58 }
59
60 // from hufflets...
61
62 // returns a number at most "maximum" and at least "minimum".
63 // if "allow_negative" is TRUE, then the return may be positive or negative.
64 float randomize_within_range(float minimum, float maximum, integer allow_negative)
65 {
66     if (minimum > maximum) {
67         // flip the two if they are reversed.
68         float temp = minimum; minimum = maximum; maximum = temp;
69     }
70     float to_return = minimum + llFrand(maximum - minimum);
71     if (allow_negative) {
72         if (llFrand(1.0) < 0.5) to_return *= -1.0;
73     }
74     return to_return;
75 }
76
77 //////////////
78 // huffware script: auto-retire, by fred huffhines, version 2.5.
79 // distributed under BSD-like license.
80 //   !!  keep in mind that this code must be *copied* into another
81 //   !!  script that you wish to add auto-retirement capability to.
82 // when a script has auto_retire in it, it can be dropped into an
83 // object and the most recent version of the script will destroy
84 // all older versions.
85 //
86 // the version numbers are embedded into the script names themselves.
87 // the notation for versions uses a letter 'v', followed by two numbers
88 // in the form "major.minor".
89 // major and minor versions are implicitly considered as a floating point
90 // number that increases with each newer version of the script.  thus,
91 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
92 // and "hazmap v3.2" is a more recent version.
93 //
94 // example usage of the auto-retirement script:
95 //     default {
96 //         state_entry() {
97 //            auto_retire();  // make sure newest addition is only version of script.
98 //        }
99 //     }
100 // this script is partly based on the self-upgrading scripts from markov brodsky
101 // and jippen faddoul.
102 //////////////
103 auto_retire() {
104     string self = llGetScriptName();  // the name of this script.
105     list split = compute_basename_and_version(self);
106     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
107     string basename = llList2String(split, 0);  // script name with no version attached.
108     string version_string = llList2String(split, 1);  // the version found.
109     integer posn;
110     // find any scripts that match the basename.  they are variants of this script.
111     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
112 //log_it("invpo=" + (string)posn);
113         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
114         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
115             // found a basic match at least.
116             list inv_split = compute_basename_and_version(curr_script);
117             if (llGetListLength(inv_split) == 2) {
118                 // see if this script is more ancient.
119                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
120                 // must make sure that the retiring script is completely the identical basename;
121                 // just matching in the front doesn't make it a relative.
122                 if ( (llList2String(inv_split, 0) == basename)
123                     && ((float)inv_version_string < (float)version_string) ) {
124                     // remove script with same name from inventory that has inferior version.
125                     llRemoveInventory(curr_script);
126                 }
127             }
128         }
129     }
130 }
131 //
132 // separates the base script name and version number.  used by auto_retire.
133 list compute_basename_and_version(string to_chop_up)
134 {
135     // minimum script name is 2 characters plus a version.
136     integer space_v_posn;
137     // find the last useful space and 'v' combo.
138     for (space_v_posn = llStringLength(to_chop_up) - 3;
139         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
140         space_v_posn--) {
141         // look for space and v but do nothing else.
142 //log_it("pos=" + (string)space_v_posn);
143     }
144     if (space_v_posn < 2) return [];  // no space found.
145 //log_it("space v@" + (string)space_v_posn);
146     // now we zoom through the stuff after our beloved v character and find any evil
147     // space characters, which are most likely from SL having found a duplicate item
148     // name and not so helpfully renamed it for us.
149     integer indy;
150     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
151 //log_it("indy=" + (string)space_v_posn);
152         if (llGetSubString(to_chop_up, indy, indy) == " ") {
153             // found one; zap it.  since we're going backwards we don't need to
154             // adjust the loop at all.
155             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
156 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
157         }
158     }
159     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
160     // ditch the space character for our numerical check.
161     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
162     // strip out a 'v' if there is one.
163     if (llGetSubString(chop_suffix, 0, 0) == "v")
164         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
165     // if valid floating point number and greater than zero, that works for our version.
166     string basename = to_chop_up;  // script name with no version attached.
167     if ((float)chop_suffix > 0.0) {
168         // this is a big success right here.
169         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
170         return [ basename, chop_suffix ];
171     }
172     // seems like we found nothing useful.
173     return [];
174 }
175 //
176 //////////////
177
178 default
179 {
180     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
181     on_rez(integer parm) { state rerun; }
182 }
183 state rerun { state_entry() { state default; } }
184
185 state real_default {
186     state_entry() {
187         auto_retire();
188     }
189
190     touch_start(integer touch_count) {
191         integer count = llGetInventoryNumber(INVENTORY_TEXTURE);
192         if (count > 0) {
193             // try getting a texture out of inventory.
194             integer which_texture = llRound(randomize_within_range(0, count - 1, FALSE));
195             texture_to_display = llGetInventoryName(INVENTORY_TEXTURE, which_texture);
196 //if (texture_to_display == "") llOwnerSay("fail!  got bad index from random.");
197         } else {
198             // if none in inventory, load the texture to show from our side.
199             texture_to_display = llGetTexture(SIDE_TO_ACQUIRE_TEXTURE_FROM);
200         }
201 //llOwnerSay("texture is now: " + texture_to_display);
202         texture_hallucination();
203         llSetTimerEvent(TEXTURE_DISPLAY_LIFE_SPAN + 0.1);
204     }
205
206     timer() {
207         llParticleSystem([]);  // zap it back out again.
208         llSetTimerEvent(0);  // turn off timer.
209     }
210 }