d658d55e8632f6becfc92b519356f7474b0dd205
[feisty_meow.git] / huffware / huffotronic_scripts / concussive_v19.2.txt
1 
2 // huffware script: concussive (aka blow em up), by fred huffhines
3 //
4 // provides a script for a missile that will start tracking the last position it
5 // hit something at and will start a sensor probe to see if there are avatars there.
6 // if there are, it will blow up and try to take avatars and active objects with it.
7 //
8 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
9 // do not use it in objects without fully realizing you are implicitly accepting that license.
10 //
11
12 // hmmm: unimplemented ideas...
13 //  buddy list; ids to not blow up besides the owner.
14 //    maybe that's just a team setup.
15 //    probably better in a notecard?
16
17
18 // configurable parameters...
19
20 float sense_distance = 23.0;
21     // the distance from the bisconation device within which to search for happy targets.
22
23 float sensory_addition = 14.0;
24     // the amount of distance that can be added in the case of
25     // a failure to sense any avatars or items.
26
27 float push_magnitude = 2147483646.0;  //maxint - 1, dealing with svc-2723.
28     // how much to push the targets that we have located.
29
30 integer MAXIMUM_HITS = 2;
31     // the most times the object is allowed through its sensor loops.
32
33 float MAX_VEL_ADJUST = 20.0;
34     // the maximum amount we would randomly add to the object's velocity after a
35     // failure to sense any items.
36
37 //////////////
38 // huffware script: auto-retire, by fred huffhines, version 2.5.
39 // distributed under BSD-like license.
40 //   !!  keep in mind that this code must be *copied* into another
41 //   !!  script that you wish to add auto-retirement capability to.
42 // when a script has auto_retire in it, it can be dropped into an
43 // object and the most recent version of the script will destroy
44 // all older versions.
45 //
46 // the version numbers are embedded into the script names themselves.
47 // the notation for versions uses a letter 'v', followed by two numbers
48 // in the form "major.minor".
49 // major and minor versions are implicitly considered as a floating point
50 // number that increases with each newer version of the script.  thus,
51 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
52 // and "hazmap v3.2" is a more recent version.
53 //
54 // example usage of the auto-retirement script:
55 //     default {
56 //         state_entry() {
57 //            auto_retire();  // make sure newest addition is only version of script.
58 //        }
59 //     }
60 // this script is partly based on the self-upgrading scripts from markov brodsky
61 // and jippen faddoul.
62 //////////////
63 auto_retire() {
64     string self = llGetScriptName();  // the name of this script.
65     list split = compute_basename_and_version(self);
66     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
67     string basename = llList2String(split, 0);  // script name with no version attached.
68     string version_string = llList2String(split, 1);  // the version found.
69     integer posn;
70     // find any scripts that match the basename.  they are variants of this script.
71     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
72 //log_it("invpo=" + (string)posn);
73         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
74         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
75             // found a basic match at least.
76             list inv_split = compute_basename_and_version(curr_script);
77             if (llGetListLength(inv_split) == 2) {
78                 // see if this script is more ancient.
79                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
80                 // must make sure that the retiring script is completely the identical basename;
81                 // just matching in the front doesn't make it a relative.
82                 if ( (llList2String(inv_split, 0) == basename)
83                     && ((float)inv_version_string < (float)version_string) ) {
84                     // remove script with same name from inventory that has inferior version.
85                     llRemoveInventory(curr_script);
86                 }
87             }
88         }
89     }
90 }
91 //
92 // separates the base script name and version number.  used by auto_retire.
93 list compute_basename_and_version(string to_chop_up)
94 {
95     // minimum script name is 2 characters plus a version.
96     integer space_v_posn;
97     // find the last useful space and 'v' combo.
98     for (space_v_posn = llStringLength(to_chop_up) - 3;
99         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
100         space_v_posn--) {
101         // look for space and v but do nothing else.
102 //log_it("pos=" + (string)space_v_posn);
103     }
104     if (space_v_posn < 2) return [];  // no space found.
105 //log_it("space v@" + (string)space_v_posn);
106     // now we zoom through the stuff after our beloved v character and find any evil
107     // space characters, which are most likely from SL having found a duplicate item
108     // name and not so helpfully renamed it for us.
109     integer indy;
110     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
111 //log_it("indy=" + (string)space_v_posn);
112         if (llGetSubString(to_chop_up, indy, indy) == " ") {
113             // found one; zap it.  since we're going backwards we don't need to
114             // adjust the loop at all.
115             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
116 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
117         }
118     }
119     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
120     // ditch the space character for our numerical check.
121     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
122     // strip out a 'v' if there is one.
123     if (llGetSubString(chop_suffix, 0, 0) == "v")
124         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
125     // if valid floating point number and greater than zero, that works for our version.
126     string basename = to_chop_up;  // script name with no version attached.
127     if ((float)chop_suffix > 0.0) {
128         // this is a big success right here.
129         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
130         return [ basename, chop_suffix ];
131     }
132     // seems like we found nothing useful.
133     return [];
134 }
135 //
136 //////////////
137
138 //borrowed from jump good
139 set_velocity(vector new_velocity, integer local_axis)
140 {
141     vector current_velocity = llGetVel();
142          
143     if (local_axis) {
144         rotation rot = llGetRot();
145         current_velocity /= rot;  // undo the rotation.
146     }
147     
148     new_velocity -= current_velocity;
149     new_velocity *= llGetMass();
150     
151     llApplyImpulse(new_velocity, local_axis);
152 }
153
154 // attempts to bring the object to a complete stop.
155 full_stop()
156 {
157     llSetForce(<0,0,0>, FALSE);
158     set_velocity(<0,0,0>, FALSE);
159 }
160
161 //////////////
162
163 //////////////
164 //borrowed from jaunt script...
165 ///////////////
166
167 // returns the portion of the list between start and end, but only if they are valid compared with
168 // the list length.  an attempt to use negative start or end values also returns a blank list.
169 list chop_list(list to_chop, integer start, integer end)
170 {
171     integer last_len = llGetListLength(to_chop) - 1;
172     if ( (start < 0) || (end < 0) || (start > last_len) || (end > last_len) ) return [];
173     return llList2List(to_chop, start, end);
174 }
175
176 // the most jumps the script will try to take.  the overall distance from the start
177 // to the end can be 10 * MAXIMUM_JUMPS meters.
178 integer MAXIMUM_JUMPS = 100;
179
180 // used to calculate jump distances.
181 vector last_posn;
182
183 // helper function for warp_across_list.  this adds one jump vector to the
184 // list of rules as long as the destination is interesting.
185 list process_warp_item(vector next_jump)
186 {
187 //tell_owner("last posn " + (string)last_posn);
188 //tell_owner("next warp " + (string)next_jump);
189     // calculate the number of jumps needed.
190     integer jumps = (integer)(llVecDist(next_jump, last_posn) / 10.0) + 1;
191     // calculate the offset needed for crossing sim boundaries.
192 ///    adjust_offset(next_jump);
193     last_posn = next_jump;  // record for next check.
194     // set up our list which we'll replicate.
195     list rules = [ PRIM_POSITION, next_jump ];
196     // Try and avoid stack/heap collisions.
197     if (jumps > MAXIMUM_JUMPS) jumps = MAXIMUM_JUMPS;
198     // add the rules repeatedly to get the effective overall jump done.
199     integer count = 1;
200     while ( (count = count << 1) < jumps) rules += rules;
201     // magnify the rule list before really adding it.  this gets us to the proper
202     // final number of jumps.
203     return rules + llList2List(rules, (count - jumps) << 1, count);
204 }
205
206 // originally based on warpPos from lsl wiki but drastically modified.
207 // uses a set of
208 // transfer points instead of a single target.
209 list warp_across_list(list full_journey, integer forwards)
210 {
211 //tell_owner("entry to warp across list, list size=" + (string)llGetListLength(full_journey));    
212     // make sure the list didn't run out.
213     if (llGetListLength(full_journey) == 0) return [];
214     if (forwards) {
215         // forwards traversal of the list.
216         vector next_jump = (vector)llList2String(full_journey, 0);
217         // shortcut the jumps if we're already there.
218         if (next_jump == llGetPos())
219             return warp_across_list(chop_list(full_journey, 1, llGetListLength(full_journey) - 1), forwards);
220         // calculate our trajectory for the next jump and add in all subsequent jumps.
221         return process_warp_item(next_jump)
222             + warp_across_list(chop_list(full_journey, 1, llGetListLength(full_journey) - 1), forwards);
223     } else {
224         // reverse traversal of the list.
225         vector next_jump = (vector)llList2String(full_journey, llGetListLength(full_journey) - 1);
226         // shortcut the jumps if we're already there.
227         if (next_jump == llGetPos())
228             return warp_across_list(chop_list(full_journey, 0, llGetListLength(full_journey) - 2), forwards);
229         // calculate our trajectory for the next jump and add in all subsequent jumps.
230         return process_warp_item(next_jump)
231             + warp_across_list(chop_list(full_journey, 0, llGetListLength(full_journey) - 2), forwards);
232     }
233 }
234
235 // the main function that performs the jaunting process.
236 jaunt(list full_journey, integer forwards)
237 {
238     // set up our global variables...
239     last_posn = llGetPos();
240     // calculate the trip and run it.
241     llSetPrimitiveParams(warp_across_list(full_journey, forwards));
242     // failsafe to patch up any math issues...
243     integer last_indy = 0;
244     if (forwards == TRUE) last_indy = llGetListLength(full_journey) - 1;
245     
246     // pick out the last target in the list based on the direction we're moving.
247     vector last_jump = (vector)llList2String(full_journey, last_indy);
248     integer max_attempts = 3;  // a rough guess at most adjustments we'd ever need.
249     while ( (llVecDist(llGetPos(), last_jump) > .001) && (max_attempts-- > 0) ) {
250 ////llWhisper(0, "touch up jump from " + (string)llGetPos());
251         llSetPos(last_jump);
252     }
253 }
254
255 //end borrowed from jaunt
256 //////////////
257
258 // sends an object back along its trajectory.
259 reverse_velocity(integer local_axis)
260 {
261     vector current_velocity = llGetVel();
262          
263     if (local_axis) {
264         rotation rot = llGetRot();
265         current_velocity /= rot;  // undo the rotation.
266     }
267     
268     vector new_velocity = -2 * current_velocity;
269     new_velocity *= llGetMass();
270     
271     llApplyImpulse(new_velocity, local_axis);
272 }
273
274 //////////////
275
276 // globals that record parameters during runtime.
277
278 vector last_victim = <0.0, 0.0, 0.0>;
279     // the last place we collided or sensed something and decided to check out.
280
281 vector previous_velocity;
282     // the rate the object was going when it started to hit something.
283
284 // the steps taken when the bullet itself dies.
285 dying_scene()
286 {
287     // make fire appear again.
288     llMakeExplosion(20, 1.0, 5, 3.0, 1.0, "fire", ZERO_VECTOR);
289     // replay the explosion noise, for good measure
290     llTriggerSound("Explosion", 1.0);
291     // make the smoke visible.  it would technically be faster than the sound, but
292     // the call also pauses the script.
293     llMakeExplosion(20, 1.0, 5, 3.0, 1.0, "Smoke", ZERO_VECTOR);
294     // gone!
295     llDie();
296 }
297
298 fling_at(vector location)
299 {
300     if (location == <0.0, 0.0, 0.0>) return;
301     vector proper_direction = location - llGetPos();
302     set_velocity(100 * proper_direction, FALSE);
303 }
304
305 jaunt_to_location(vector location)
306 {
307     if (location == <0.0, 0.0, 0.0>) return;
308     // this turns off the physics property for the object, so that jaunt and
309     // llSetPos will still work.  this became necessary due to havok4.
310     llSetStatus(STATUS_PHYSICS, FALSE);
311     llSetStatus(STATUS_PHANTOM, TRUE);
312     // go to position specified.
313     jaunt([llGetPos(), location], TRUE);
314     // return to prior characteristics.
315     llSetStatus(STATUS_PHANTOM, FALSE);
316     llSetStatus(STATUS_PHYSICS, TRUE);
317
318 //simplistic version.
319 ////    llSetPos(location);
320 }
321
322 blow_up_item(key to_whack, integer type)
323 {
324     if (to_whack == NULL_KEY) {
325         llOwnerSay("wtf?  passed a null key in blow up item.");
326         return;
327     }
328     list details = llGetObjectDetails(to_whack, [ OBJECT_POS ]);
329     vector current_victim = llList2Vector(details, 0);
330 ///    fling_at(current_victim);
331     jaunt_to_location(current_victim - <0.0, 0.0, 0.042>);
332     if (type & AGENT) {
333         llOwnerSay("targeting: " + llKey2Name(to_whack));
334         // flame first.
335         llMakeExplosion(20, 1.0, 5, 3.0, 1.0, "fire", ZERO_VECTOR);
336         // reset the last victim for a *real* victim.
337         last_victim = current_victim;
338     }
339 //fling_at(last_victim);//trying new approach.
340     // then a huge push.
341     llPushObject(to_whack, push_magnitude * llRot2Up(llGetRot()), ZERO_VECTOR, FALSE);
342     // then the noise, if the victim is important enough.
343     if (type & AGENT) {
344         // make a glad noise.  sound is slower, right?
345 llSetStatus(STATUS_PHYSICS, FALSE);//guess temp.
346         llTriggerSound("Explosion", 1.0);
347 llSetStatus(STATUS_PHYSICS, TRUE);//guess temp.
348     }
349 }
350
351 reset_to_last_hit()
352 {
353    jaunt_to_location(last_victim);  // start back where we first collided.
354     // onward at nearly the same rate, but backwards.
355     vector adjusted = <llFrand(MAX_VEL_ADJUST), llFrand(MAX_VEL_ADJUST),
356         llFrand(MAX_VEL_ADJUST)>;
357     set_velocity(-previous_velocity + adjusted, FALSE);
358     last_victim = <0.0, 0.0, 0.0>;  // allow collisions to start being noticed again.
359  }
360
361 integer blow_up_avatars(integer total_number)
362 {
363 //    fling_at(last_victim);
364     jaunt_to_location(last_victim);
365
366 //if (last_victim != <0.0, 0.0, 0.0>) {
367 //full_stop();
368 //// check where it actually thinks we hit.
369 //integer i;
370 //for (i = 0; i < 20; i++) 
371 //llMakeExplosion(20, 1.0, 5, 3.0, 1.0, "fire", ZERO_VECTOR);   
372 //}
373     
374     integer found_avatar = FALSE;
375
376     // now whack the avatars.    
377     integer i;
378     for (i = 0; i < total_number; i++) {
379         // if we hit an avatar, slam them away.
380         key curr_key = llDetectedKey(i);
381         if (curr_key != NULL_KEY) {
382             // why would the key ever be null?  yet it seems we have seen that.
383             integer type = llDetectedType(i);
384             // make sure the target is an avatar and it's NOT the owner.
385             if (curr_key != llGetOwner()) {
386                 // blow them up regardless of what type they are.
387                 blow_up_item(curr_key, type);
388                 // set that we found an avatar if the type is right.
389                 if (type & AGENT) found_avatar = TRUE;
390             }
391         }
392     }
393
394     if (!found_avatar) {
395         reset_to_last_hit();
396         return FALSE;  // don't die right now.  we didn't achieve our objective.
397     }
398
399     dying_scene();
400     return TRUE;  // i'm not sure we'll ever see this statement...
401 }
402
403 integer enabled = FALSE;    // true if bullet is ready to operate.
404
405 integer hits_used = 0;  // number of hits consumed so far on sensors.
406
407 prepare_bullet()
408 {    
409 ///    llSensorRemove();
410
411 //    llSetDamage(5000);  // hang onto that.
412 //what do they mean by "task will be killed"?
413
414     // pick the object characteristics that seem to work best.
415     llSetStatus(STATUS_PHYSICS, TRUE);
416     llSetStatus(STATUS_PHANTOM, FALSE);
417
418     hits_used = 0;
419     last_victim = <0.0, 0.0, 0.0>;
420 }
421
422 limit_obnoxiousness()
423 {
424     hits_used++;
425     if (hits_used > MAXIMUM_HITS) dying_scene();
426 }
427
428 default
429 {
430     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
431     on_rez(integer parm) { state rerun; }
432 }
433 state rerun { state_entry() { state default; } }
434
435 state real_default {
436     state_entry() {
437         auto_retire();
438         llPreloadSound("Explosion");
439         prepare_bullet();
440     }
441
442     on_rez(integer start_param) {
443         // make this a bit more specific so we can work on it.
444         if (start_param == 2814) {
445             prepare_bullet();
446             enabled = TRUE;
447         }
448     }
449     
450     collision_start(integer total_number)
451     {
452         if (!enabled) return;
453         // initialize our boom position if it hasn't been yet.
454         if (last_victim == <0.0, 0.0, 0.0>) {
455             previous_velocity = llGetVel();
456             last_victim = llGetPos();
457 //            reverse_velocity(FALSE);
458             // sense up any bogies.
459             llSensor("", NULL_KEY, AGENT | ACTIVE, sense_distance, 2.0 * PI);
460         }
461     }
462
463     sensor(integer total_number) {
464         if (!enabled) return;
465         limit_obnoxiousness();
466         // act on who we sensed around us.
467         integer found_one = blow_up_avatars(total_number);
468         // did we find an avatar?  that makes us explode with pleasure.
469         if (!found_one) {
470             // start looking again.
471             llSensor("", NULL_KEY, AGENT | ACTIVE, sensory_addition + sense_distance, 2.0 * PI);
472         } else {
473             // we had seen one, so croak.
474             dying_scene();
475         }
476     }
477
478     no_sensor() {
479         if (!enabled) return;
480         limit_obnoxiousness();
481         // try again until we find a victim.
482         reset_to_last_hit();
483         llSensor("", NULL_KEY, AGENT | ACTIVE, 2.0 * sensory_addition
484             + sense_distance, 2.0 * PI);            
485     }
486 }
487