c26062cd3597ec4cc1ae9d0a041fcfcbd06ccc91
[feisty_meow.git] / huffware / huffotronic_scripts / axis_rider_v2.9.txt
1 
2 // huffware script: axis rider, by fred huffhines.
3 //
4 // causes an object to move up and down on the z axis, although that and
5 // other parameters can be modified.
6 //
7 //   this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 //   do not use it in objects without fully realizing you are implicitly accepting that license.
9 //
10
11 // these two control how far from the original position the object may travel.
12 float MIN_POSITION = -0.3;
13 float MAX_POSITION = 1.0;
14 // these specify which axis to modify and compare on.
15 integer USE_X_AXIS = FALSE;
16 integer USE_Y_AXIS = FALSE;
17 integer USE_Z_AXIS = TRUE;
18 // these are the limits on how much the object can move during one timer click.
19 float MIN_POSITION_ADJUSTMENT = 0.05;
20 float MAX_POSITION_ADJUSTMENT = 0.07;
21
22 // jitter is the capability for the object to not be aligned on a straight
23 // up and down axis.
24 integer JITTER_EFFECT = TRUE;
25     // if this is true, then the rider will move unevenly at various angles.
26 float CHANCE_FOR_JITTER = 0.60;
27     // this is the probability of changing the current direction (0.0 to 1.0).
28 vector MIN_ANGULAR_POSITION = <0.0, 0.0, 0.0>;  // minimum jitter amount, in degrees.
29 vector MAX_ANGULAR_POSITION = <1.0, 1.0, 4.0>;  // maximum jitter amount, in degrees.
30
31 float TIMER_FREQUENCY = 0.20;
32     // this is the fastest that prim changes can happen anyhow,
33     // so we fire the timer at this rate.
34
35 //////////////
36 // huffware script: auto-retire, by fred huffhines, version 2.5.
37 // distributed under BSD-like license.
38 //   !!  keep in mind that this code must be *copied* into another
39 //   !!  script that you wish to add auto-retirement capability to.
40 // when a script has auto_retire in it, it can be dropped into an
41 // object and the most recent version of the script will destroy
42 // all older versions.
43 //
44 // the version numbers are embedded into the script names themselves.
45 // the notation for versions uses a letter 'v', followed by two numbers
46 // in the form "major.minor".
47 // major and minor versions are implicitly considered as a floating point
48 // number that increases with each newer version of the script.  thus,
49 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
50 // and "hazmap v3.2" is a more recent version.
51 //
52 // example usage of the auto-retirement script:
53 //     default {
54 //         state_entry() {
55 //            auto_retire();  // make sure newest addition is only version of script.
56 //        }
57 //     }
58 // this script is partly based on the self-upgrading scripts from markov brodsky
59 // and jippen faddoul.
60 //////////////
61 auto_retire() {
62     string self = llGetScriptName();  // the name of this script.
63     list split = compute_basename_and_version(self);
64     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
65     string basename = llList2String(split, 0);  // script name with no version attached.
66     string version_string = llList2String(split, 1);  // the version found.
67     integer posn;
68     // find any scripts that match the basename.  they are variants of this script.
69     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
70 //log_it("invpo=" + (string)posn);
71         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
72         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
73             // found a basic match at least.
74             list inv_split = compute_basename_and_version(curr_script);
75             if (llGetListLength(inv_split) == 2) {
76                 // see if this script is more ancient.
77                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
78                 // must make sure that the retiring script is completely the identical basename;
79                 // just matching in the front doesn't make it a relative.
80                 if ( (llList2String(inv_split, 0) == basename)
81                     && ((float)inv_version_string < (float)version_string) ) {
82                     // remove script with same name from inventory that has inferior version.
83                     llRemoveInventory(curr_script);
84                 }
85             }
86         }
87     }
88 }
89 //
90 // separates the base script name and version number.  used by auto_retire.
91 list compute_basename_and_version(string to_chop_up)
92 {
93     // minimum script name is 2 characters plus a version.
94     integer space_v_posn;
95     // find the last useful space and 'v' combo.
96     for (space_v_posn = llStringLength(to_chop_up) - 3;
97         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
98         space_v_posn--) {
99         // look for space and v but do nothing else.
100 //log_it("pos=" + (string)space_v_posn);
101     }
102     if (space_v_posn < 2) return [];  // no space found.
103 //log_it("space v@" + (string)space_v_posn);
104     // now we zoom through the stuff after our beloved v character and find any evil
105     // space characters, which are most likely from SL having found a duplicate item
106     // name and not so helpfully renamed it for us.
107     integer indy;
108     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
109 //log_it("indy=" + (string)space_v_posn);
110         if (llGetSubString(to_chop_up, indy, indy) == " ") {
111             // found one; zap it.  since we're going backwards we don't need to
112             // adjust the loop at all.
113             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
114 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
115         }
116     }
117     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
118     // ditch the space character for our numerical check.
119     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
120     // strip out a 'v' if there is one.
121     if (llGetSubString(chop_suffix, 0, 0) == "v")
122         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
123     // if valid floating point number and greater than zero, that works for our version.
124     string basename = to_chop_up;  // script name with no version attached.
125     if ((float)chop_suffix > 0.0) {
126         // this is a big success right here.
127         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
128         return [ basename, chop_suffix ];
129     }
130     // seems like we found nothing useful.
131     return [];
132 }
133 //
134 //////////////
135
136 // from hufflets...
137
138 integer debug_num = 0;
139
140 // a debugging output method.  can be disabled entirely in one place.
141 log_it(string to_say)
142 {
143     debug_num++;
144     // tell this to the owner.    
145     llOwnerSay(llGetScriptName() + "--" + (string)debug_num + ": " + to_say);
146     // say this on open chat, but use an unusual channel.
147 //    llSay(108, llGetScriptName() + "--" + (string)debug_num + ": " + to_say);
148 }
149
150 // returns a number at most "maximum" and at least "minimum".
151 // if "allow_negative" is TRUE, then the return may be positive or negative.
152 float randomize_within_range(float minimum, float maximum, integer allow_negative)
153 {
154     if (minimum > maximum) {
155         // flip the two if they are reversed.
156         float temp = minimum; minimum = maximum; maximum = temp;
157     }
158     float to_return = minimum + llFrand(maximum - minimum);
159     if (allow_negative) {
160         if (llFrand(1.0) < 0.5) to_return *= -1.0;
161     }
162     return to_return;
163 }
164
165 // returns a random vector where x,y,z will be between "minimums" and "maximums"
166 // x,y,z components.  if "allow_negative" is true, then any component will
167 // randomly be negative or positive.
168 vector random_bound_vector(vector minimums, vector maximums, integer allow_negative)
169 {
170     return <randomize_within_range(minimums.x, maximums.x, allow_negative),
171         randomize_within_range(minimums.y, maximums.y, allow_negative),
172         randomize_within_range(minimums.z, maximums.z, allow_negative)>;
173 }
174
175 // returns a vector whose components are between minimum and maximum.
176 // if allow_negative is true, then they can be either positive or negative.
177 vector random_vector(float minimum, float maximum, integer allow_negative)
178 {
179     return random_bound_vector(<minimum, minimum, minimum>,
180         <maximum, maximum, maximum>, allow_negative);
181 }
182
183 // returns TRUE if a is less than b in any component.
184 integer vector_less_than(vector a, vector b)
185 { return (a.x < b.x) || (a.y < b.y) || (a.z < b.z); }
186
187 // returns TRUE if a is greater than b in any component.
188 integer vector_greater_than(vector a, vector b)
189 { return (a.x > b.x) || (a.y > b.y) || (a.z > b.z); }
190
191 // returns a list with two components; a new vector and a boolean.
192 // the new vector starts from "starting_point".  it will have a vector
193 // between "minimum_addition" and "maximum_addition" added to it.
194 // if it is over the "minimum_allowed" or the "maximum_allowed", then
195 // it is reset to whichever it would have crossed over.  two booleans
196 // are also returned to indicate when the lower and upper limits were
197 // exceeded (in that order).
198 list limit_and_add(vector starting_point,
199     vector minimum_allowed, vector maximum_allowed,
200     vector minimum_addition, vector maximum_addition)
201 {
202     integer too_low = FALSE;
203     integer too_high = FALSE;
204     vector new_location = starting_point;
205     vector addition = random_bound_vector(minimum_addition, maximum_addition, FALSE);
206 //log_it("start=" + (string)starting_point + " addin=" + (string)addition);
207     new_location += addition;
208     if (vector_less_than(new_location, minimum_allowed)) {
209         too_low = TRUE;
210         new_location = minimum_allowed;
211     } else if (vector_greater_than(new_location, maximum_allowed)) {
212         too_high = TRUE;
213         new_location = maximum_allowed;
214     }
215     return [ new_location, too_low, too_high ];
216 }
217
218 //////////////
219
220 // variables used during the script.
221
222 vector rez_position;
223     // set at time of object rez from object's current location.
224
225 vector rez_rotation;
226     // set at time of object rez from object's current rotation.
227
228 vector current_position_addin = <0.0, 0.0, 0.0>;
229     // the amount that we're adding to the rider's position right now.
230
231 vector current_rotation_addin = <0.0, 0.0, 0.0>;
232     // randomly assigned to if JITTER_EFFECT is true.
233
234 vector current_direction = <1.0, 1.0, 1.0>;
235     // we start out by adding to all axes.
236
237 // provides a random vector that could be negative or positive for
238 // any of the values.  the range is given by the two constants
239 // MIN_ADDITION and MAX_ADDITION.
240 vector rando_vector()
241 {
242     return random_bound_vector(MIN_ANGULAR_POSITION * DEG_TO_RAD,
243         MAX_ANGULAR_POSITION * DEG_TO_RAD, TRUE);
244 }
245
246 default {
247     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
248     on_rez(integer parm) { state rerun; }
249 }
250 state rerun { state_entry() { state default; } }
251
252 state real_default
253 {
254     state_entry()
255     {
256         auto_retire();
257         rez_position = llGetPos();
258         rez_rotation = llRot2Euler(llGetRot());
259 //vector calc_rot = rez_rotation * RAD_TO_DEG;
260 //log_it("rotation at start is " + (string)calc_rot);
261         llSetTimerEvent(TIMER_FREQUENCY);
262     }
263     
264     on_rez(integer parm) { llResetScript(); }
265
266     timer()
267     {
268         if (USE_X_AXIS) {
269              list add_result = limit_and_add(current_position_addin,
270                 <MIN_POSITION, current_position_addin.y, current_position_addin.z>,
271                 <MAX_POSITION, current_position_addin.y, current_position_addin.z>,
272                 <current_direction.x * MIN_POSITION_ADJUSTMENT, 0.0, 0.0>,
273                 <current_direction.x * MAX_POSITION_ADJUSTMENT, 0.0, 0.0>);
274             current_position_addin = llList2Vector(add_result, 0);
275             integer too_low = llList2Integer(add_result, 1);
276             integer too_high = llList2Integer(add_result, 2);
277             if (too_low) current_direction.x = 1.0;
278             else if (too_high) current_direction.x = -1.0;
279         }
280         if (USE_Y_AXIS) {
281              list add_result = limit_and_add(current_position_addin,
282                 <current_position_addin.x, MIN_POSITION, current_position_addin.z>,
283                 <current_position_addin.x, MAX_POSITION, current_position_addin.z>,
284                 <0.0, current_direction.y * MIN_POSITION_ADJUSTMENT, 0.0>,
285                 <0.0, current_direction.y * MAX_POSITION_ADJUSTMENT, 0.0>);
286             current_position_addin = llList2Vector(add_result, 0);
287             integer too_low = llList2Integer(add_result, 1);
288             integer too_high = llList2Integer(add_result, 2);
289             if (too_low) current_direction.y = 1.0;
290             else if (too_high) current_direction.y = -1.0;
291         }
292         if (USE_Z_AXIS) {
293              list add_result = limit_and_add(current_position_addin,
294                 <current_position_addin.x, current_position_addin.y, MIN_POSITION>,
295                 <current_position_addin.x, current_position_addin.y, MAX_POSITION>,
296                 <0.0, 0.0, current_direction.z * MIN_POSITION_ADJUSTMENT>,
297                 <0.0, 0.0, current_direction.z * MAX_POSITION_ADJUSTMENT>);
298             current_position_addin = llList2Vector(add_result, 0);
299             integer too_low = llList2Integer(add_result, 1);
300             integer too_high = llList2Integer(add_result, 2);
301             if (too_low) current_direction.z = 1.0;
302             else if (too_high) current_direction.z = -1.0;
303         }
304         
305 //logic below for randomness is a bit odd.
306         // change the jitter position if we get a chance.
307         float starter = 0.420;  // where we start looking for change.
308         float change_cap = starter + CHANCE_FOR_JITTER;
309         float randomness = llFrand(1.000);
310         if ( (randomness <= change_cap) && (randomness >= starter) ) {
311             // time for a change in the rotation.
312             if (JITTER_EFFECT) {
313                 current_rotation_addin = rando_vector();
314             }
315         }
316
317         llSetPrimitiveParams([
318             PRIM_POSITION, rez_position + current_position_addin,
319             PRIM_ROTATION, llEuler2Rot(rez_rotation + current_rotation_addin)
320             ]);
321
322     }
323 }
324