normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / a_jumping_bean_v2.7.txt
1 
2 // huffware script: a jumping bean, by fred huffhines
3 //
4 // an object will hop around randomly when it has this script.
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 float MINIMUM_IMPULSE_MULTIPLIER = 0.05;
11     // the smallest amount we will allow the object to be bumped, relative to its mass.
12 float Z_IMPULSE_MULTIPLIER = 3.4;
13     // we push the object upwards relative to its mass by this amount (or less, since
14     // we randomly choose a value up to this large).
15 float X_IMPULSE_MULTIPLIER = 0.8;
16 float Y_IMPULSE_MULTIPLIER = 0.8;
17     // there are smaller x and y components possible.
18
19 float MINIMUM_ROTATION_MULTIPLIER = 0.001;
20 // multipliers for rotation.
21 float X_ROTATION_MULTIPLIER = 0.08;
22 float Y_ROTATION_MULTIPLIER = 0.08;
23 float Z_ROTATION_MULTIPLIER = 0.08;
24
25 //////////////
26 // from hufflets...
27 //
28 // returns a number at most "maximum" and at least "minimum".
29 // if "allow_negative" is TRUE, then the return may be positive or negative.
30 float randomize_within_range(float minimum, float maximum, integer allow_negative)
31 {
32     if (minimum > maximum) {
33         // flip the two if they are reversed.
34         float temp = minimum; minimum = maximum; maximum = temp;
35     }
36     float to_return = minimum + llFrand(maximum - minimum);
37     if (allow_negative) {
38         if (llFrand(1.0) < 0.5) to_return *= -1.0;
39     }
40     return to_return;
41 }
42 //
43 //////////////
44
45 //////////////
46 // huffware script: auto-retire, by fred huffhines, version 2.5.
47 // distributed under BSD-like license.
48 //   !!  keep in mind that this code must be *copied* into another
49 //   !!  script that you wish to add auto-retirement capability to.
50 // when a script has auto_retire in it, it can be dropped into an
51 // object and the most recent version of the script will destroy
52 // all older versions.
53 //
54 // the version numbers are embedded into the script names themselves.
55 // the notation for versions uses a letter 'v', followed by two numbers
56 // in the form "major.minor".
57 // major and minor versions are implicitly considered as a floating point
58 // number that increases with each newer version of the script.  thus,
59 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
60 // and "hazmap v3.2" is a more recent version.
61 //
62 // example usage of the auto-retirement script:
63 //     default {
64 //         state_entry() {
65 //            auto_retire();  // make sure newest addition is only version of script.
66 //        }
67 //     }
68 // this script is partly based on the self-upgrading scripts from markov brodsky
69 // and jippen faddoul.
70 //////////////
71 auto_retire() {
72     string self = llGetScriptName();  // the name of this script.
73     list split = compute_basename_and_version(self);
74     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
75     string basename = llList2String(split, 0);  // script name with no version attached.
76     string version_string = llList2String(split, 1);  // the version found.
77     integer posn;
78     // find any scripts that match the basename.  they are variants of this script.
79     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
80 //log_it("invpo=" + (string)posn);
81         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
82         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
83             // found a basic match at least.
84             list inv_split = compute_basename_and_version(curr_script);
85             if (llGetListLength(inv_split) == 2) {
86                 // see if this script is more ancient.
87                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
88                 // must make sure that the retiring script is completely the identical basename;
89                 // just matching in the front doesn't make it a relative.
90                 if ( (llList2String(inv_split, 0) == basename)
91                     && ((float)inv_version_string < (float)version_string) ) {
92                     // remove script with same name from inventory that has inferior version.
93                     llRemoveInventory(curr_script);
94                 }
95             }
96         }
97     }
98 }
99 //
100 // separates the base script name and version number.  used by auto_retire.
101 list compute_basename_and_version(string to_chop_up)
102 {
103     // minimum script name is 2 characters plus a version.
104     integer space_v_posn;
105     // find the last useful space and 'v' combo.
106     for (space_v_posn = llStringLength(to_chop_up) - 3;
107         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
108         space_v_posn--) {
109         // look for space and v but do nothing else.
110 //log_it("pos=" + (string)space_v_posn);
111     }
112     if (space_v_posn < 2) return [];  // no space found.
113 //log_it("space v@" + (string)space_v_posn);
114     // now we zoom through the stuff after our beloved v character and find any evil
115     // space characters, which are most likely from SL having found a duplicate item
116     // name and not so helpfully renamed it for us.
117     integer indy;
118     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
119 //log_it("indy=" + (string)space_v_posn);
120         if (llGetSubString(to_chop_up, indy, indy) == " ") {
121             // found one; zap it.  since we're going backwards we don't need to
122             // adjust the loop at all.
123             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
124 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
125         }
126     }
127     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
128     // ditch the space character for our numerical check.
129     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
130     // strip out a 'v' if there is one.
131     if (llGetSubString(chop_suffix, 0, 0) == "v")
132         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
133     // if valid floating point number and greater than zero, that works for our version.
134     string basename = to_chop_up;  // script name with no version attached.
135     if ((float)chop_suffix > 0.0) {
136         // this is a big success right here.
137         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
138         return [ basename, chop_suffix ];
139     }
140     // seems like we found nothing useful.
141     return [];
142 }
143 //
144 //////////////
145
146 default {
147     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
148     on_rez(integer parm) { state rerun; }
149 }
150 state rerun { state_entry() { state default; } }
151
152 state real_default
153 {
154     state_entry()
155     {
156         auto_retire();  // make sure newest addition is only version of script.
157         llSetTimerEvent(0.3);
158     }
159
160     timer()
161     {
162         if (llFrand(1.0) > 0.5) {
163             vector impulsive = <randomize_within_range(MINIMUM_IMPULSE_MULTIPLIER, X_IMPULSE_MULTIPLIER, TRUE),
164                 randomize_within_range(MINIMUM_IMPULSE_MULTIPLIER, Y_IMPULSE_MULTIPLIER, TRUE),
165                 randomize_within_range(MINIMUM_IMPULSE_MULTIPLIER, Z_IMPULSE_MULTIPLIER, TRUE)>;
166             llApplyImpulse(llGetMass() * impulsive, FALSE);
167             impulsive = <randomize_within_range(MINIMUM_ROTATION_MULTIPLIER, X_ROTATION_MULTIPLIER, TRUE),
168                 randomize_within_range(MINIMUM_ROTATION_MULTIPLIER, Y_ROTATION_MULTIPLIER, TRUE),
169                 randomize_within_range(MINIMUM_ROTATION_MULTIPLIER, Z_ROTATION_MULTIPLIER, TRUE)>;
170             llApplyRotationalImpulse(impulsive, FALSE);
171         }
172     }
173
174     changed(integer change) {
175         if (change & CHANGED_LINK) {
176             key av = llAvatarOnSitTarget();
177             if (av != NULL_KEY) {
178                 // someone sat down; let's animate them to make them tuck into the
179                 // bean, if possible.
180                 llStartAnimation("crouch");
181             }
182         }
183     }
184     
185 }
186