normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / bouncer_v2.1.txt
1 
2 // huffware script: bouncer, by fred huffhines.
3 //
4 // this script repels an object or avatar that slams into it.
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 integer PUSH_UPWARDS = TRUE;
11     // if this is true, the velocity is reflected upwards.  this is sort of
12     // a trampoline effect.
13 integer PUSH_DIRECTLY_BACK = TRUE;
14     // if this is true, then the object's velocity is reflected directly back.
15     // this is not a realistic bounce since it is like every bounce hits the
16     // object at a tangent rather than having the angle be rotated as a
17     // reflection.
18
19 integer AVATARS_ONLY = FALSE;
20     // if true, then the bouncer will only react to people and not things.
21
22 float PUSH_MULTIPLIER = 32.0;
23     // increase the object's velocity by this factor, as part of how we
24     // help it along when it collides with us.
25
26 integer LOCAL_AXIS = FALSE;
27     // use the axis relative to the object's rotation?
28
29 // originally came from reverse_velocity from concussive script.
30 vector push_back(key act_upon, integer local_axis)
31 {
32     list details = llGetObjectDetails(act_upon, [ OBJECT_VELOCITY ]);
33     vector current_velocity = llList2Vector(details, 0);    
34
35     if (local_axis) {
36         rotation rot = llGetRot();
37         current_velocity /= rot;  // undo the rotation.
38     }
39     // here's where we return the velocity exactly backwards along
40     // its own axis.  this is not really very true to physics.
41     vector new_velocity = -PUSH_MULTIPLIER * current_velocity;
42     new_velocity *= llGetMass();
43 //    llPushObject(act_upon, new_velocity, ZERO_VECTOR, local_axis);
44     return new_velocity;
45 }
46
47 // causes the object to be thrown up into the air and magnifies their
48 // velocity.
49 vector push_up(key act_upon, integer local_axis)
50 {
51     list details = llGetObjectDetails(act_upon, [ OBJECT_VELOCITY ]);
52     vector current_velocity = llList2Vector(details, 0);
53     if (local_axis) {
54         rotation rot = llGetRot();
55         current_velocity /= rot;  // undo the rotation.
56     }
57     // another example that's like the trampoline...
58 //    vector new_velocity = PUSH_MULTIPLIER * llRot2Up(llGetRot());
59     vector new_velocity = current_velocity;
60     new_velocity.z = -PUSH_MULTIPLIER * new_velocity.z;
61     new_velocity *= llGetMass();
62 //    llPushObject(act_upon, new_velocity, ZERO_VECTOR, local_axis);
63     return new_velocity;
64 }
65
66 // a newer method that can be used to 'continue' the velocity away from
67 // the bouncer but more upwards from an elastic collision than back at the av.
68 /////not implemented yet.
69 // needs good physics normal vector implementation.
70
71 // called by the collision event handler when we detect something
72 // bumping into us.
73 handle_collisions(integer count)
74 {
75     integer i;
76     for (i = 0; i < count; i++) {
77         if ( (AVATARS_ONLY && (llDetectedType(i) & AGENT) )
78                 || !AVATARS_ONLY) {
79             vector upwards;
80             if (PUSH_UPWARDS)
81                 upwards = push_up(llDetectedKey(i), LOCAL_AXIS);
82             vector back;
83             if (PUSH_DIRECTLY_BACK)
84                 back = push_back(llDetectedKey(i), LOCAL_AXIS);
85             llPushObject(llDetectedKey(i), upwards + back, ZERO_VECTOR, LOCAL_AXIS);
86         }
87     }
88 }
89
90 //////////////
91 // from hufflets...
92
93 //////////////
94 // huffware script: auto-retire, by fred huffhines, version 2.5.
95 // distributed under BSD-like license.
96 //   !!  keep in mind that this code must be *copied* into another
97 //   !!  script that you wish to add auto-retirement capability to.
98 // when a script has auto_retire in it, it can be dropped into an
99 // object and the most recent version of the script will destroy
100 // all older versions.
101 //
102 // the version numbers are embedded into the script names themselves.
103 // the notation for versions uses a letter 'v', followed by two numbers
104 // in the form "major.minor".
105 // major and minor versions are implicitly considered as a floating point
106 // number that increases with each newer version of the script.  thus,
107 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
108 // and "hazmap v3.2" is a more recent version.
109 //
110 // example usage of the auto-retirement script:
111 //     default {
112 //         state_entry() {
113 //            auto_retire();  // make sure newest addition is only version of script.
114 //        }
115 //     }
116 // this script is partly based on the self-upgrading scripts from markov brodsky
117 // and jippen faddoul.
118 //////////////
119 auto_retire() {
120     string self = llGetScriptName();  // the name of this script.
121     list split = compute_basename_and_version(self);
122     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
123     string basename = llList2String(split, 0);  // script name with no version attached.
124     string version_string = llList2String(split, 1);  // the version found.
125     integer posn;
126     // find any scripts that match the basename.  they are variants of this script.
127     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
128 //log_it("invpo=" + (string)posn);
129         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
130         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
131             // found a basic match at least.
132             list inv_split = compute_basename_and_version(curr_script);
133             if (llGetListLength(inv_split) == 2) {
134                 // see if this script is more ancient.
135                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
136                 // must make sure that the retiring script is completely the identical basename;
137                 // just matching in the front doesn't make it a relative.
138                 if ( (llList2String(inv_split, 0) == basename)
139                     && ((float)inv_version_string < (float)version_string) ) {
140                     // remove script with same name from inventory that has inferior version.
141                     llRemoveInventory(curr_script);
142                 }
143             }
144         }
145     }
146 }
147 //
148 // separates the base script name and version number.  used by auto_retire.
149 list compute_basename_and_version(string to_chop_up)
150 {
151     // minimum script name is 2 characters plus a version.
152     integer space_v_posn;
153     // find the last useful space and 'v' combo.
154     for (space_v_posn = llStringLength(to_chop_up) - 3;
155         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
156         space_v_posn--) {
157         // look for space and v but do nothing else.
158 //log_it("pos=" + (string)space_v_posn);
159     }
160     if (space_v_posn < 2) return [];  // no space found.
161 //log_it("space v@" + (string)space_v_posn);
162     // now we zoom through the stuff after our beloved v character and find any evil
163     // space characters, which are most likely from SL having found a duplicate item
164     // name and not so helpfully renamed it for us.
165     integer indy;
166     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
167 //log_it("indy=" + (string)space_v_posn);
168         if (llGetSubString(to_chop_up, indy, indy) == " ") {
169             // found one; zap it.  since we're going backwards we don't need to
170             // adjust the loop at all.
171             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
172 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
173         }
174     }
175     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
176     // ditch the space character for our numerical check.
177     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
178     // strip out a 'v' if there is one.
179     if (llGetSubString(chop_suffix, 0, 0) == "v")
180         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
181     // if valid floating point number and greater than zero, that works for our version.
182     string basename = to_chop_up;  // script name with no version attached.
183     if ((float)chop_suffix > 0.0) {
184         // this is a big success right here.
185         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
186         return [ basename, chop_suffix ];
187     }
188     // seems like we found nothing useful.
189     return [];
190 }
191 //
192 //////////////
193
194 // end hufflets.
195 //////////////
196
197
198 default
199 {
200     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
201     on_rez(integer parm) { state rerun; }
202 }
203 state rerun { state_entry() { state default; } }
204
205 state real_default
206 {
207     state_entry() { auto_retire(); }
208
209     collision(integer count) { handle_collisions(count); }
210 }
211