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