normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / collider_v1.3.txt
1 
2 // huffware script: collider, by fred huffhines
3 //
4 // a simple script that plays sounds in the inventory when a collision occurs.
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 SOUND_VOLUME = 0.3;
11     // how loudly to play the sounds we find.
12
13 integer TIME_BETWEEN_PLAYS = 3;
14     // we only make the sound every X seconds.
15
16 integer last_played = 0;
17     // records the last time we hit the sound.
18
19 //////////////
20 // huffware script: auto-retire, by fred huffhines, version 2.5.
21 // distributed under BSD-like license.
22 //   !!  keep in mind that this code must be *copied* into another
23 //   !!  script that you wish to add auto-retirement capability to.
24 // when a script has auto_retire in it, it can be dropped into an
25 // object and the most recent version of the script will destroy
26 // all older versions.
27 //
28 // the version numbers are embedded into the script names themselves.
29 // the notation for versions uses a letter 'v', followed by two numbers
30 // in the form "major.minor".
31 // major and minor versions are implicitly considered as a floating point
32 // number that increases with each newer version of the script.  thus,
33 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
34 // and "hazmap v3.2" is a more recent version.
35 //
36 // example usage of the auto-retirement script:
37 //     default {
38 //         state_entry() {
39 //            auto_retire();  // make sure newest addition is only version of script.
40 //        }
41 //     }
42 // this script is partly based on the self-upgrading scripts from markov brodsky
43 // and jippen faddoul.
44 //////////////
45 auto_retire() {
46     string self = llGetScriptName();  // the name of this script.
47     list split = compute_basename_and_version(self);
48     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
49     string basename = llList2String(split, 0);  // script name with no version attached.
50     string version_string = llList2String(split, 1);  // the version found.
51     integer posn;
52     // find any scripts that match the basename.  they are variants of this script.
53     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
54 //log_it("invpo=" + (string)posn);
55         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
56         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
57             // found a basic match at least.
58             list inv_split = compute_basename_and_version(curr_script);
59             if (llGetListLength(inv_split) == 2) {
60                 // see if this script is more ancient.
61                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
62                 // must make sure that the retiring script is completely the identical basename;
63                 // just matching in the front doesn't make it a relative.
64                 if ( (llList2String(inv_split, 0) == basename)
65                     && ((float)inv_version_string < (float)version_string) ) {
66                     // remove script with same name from inventory that has inferior version.
67                     llRemoveInventory(curr_script);
68                 }
69             }
70         }
71     }
72 }
73 //
74 // separates the base script name and version number.  used by auto_retire.
75 list compute_basename_and_version(string to_chop_up)
76 {
77     // minimum script name is 2 characters plus a version.
78     integer space_v_posn;
79     // find the last useful space and 'v' combo.
80     for (space_v_posn = llStringLength(to_chop_up) - 3;
81         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
82         space_v_posn--) {
83         // look for space and v but do nothing else.
84 //log_it("pos=" + (string)space_v_posn);
85     }
86     if (space_v_posn < 2) return [];  // no space found.
87 //log_it("space v@" + (string)space_v_posn);
88     // now we zoom through the stuff after our beloved v character and find any evil
89     // space characters, which are most likely from SL having found a duplicate item
90     // name and not so helpfully renamed it for us.
91     integer indy;
92     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
93 //log_it("indy=" + (string)space_v_posn);
94         if (llGetSubString(to_chop_up, indy, indy) == " ") {
95             // found one; zap it.  since we're going backwards we don't need to
96             // adjust the loop at all.
97             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
98 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
99         }
100     }
101     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
102     // ditch the space character for our numerical check.
103     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
104     // strip out a 'v' if there is one.
105     if (llGetSubString(chop_suffix, 0, 0) == "v")
106         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
107     // if valid floating point number and greater than zero, that works for our version.
108     string basename = to_chop_up;  // script name with no version attached.
109     if ((float)chop_suffix > 0.0) {
110         // this is a big success right here.
111         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
112         return [ basename, chop_suffix ];
113     }
114     // seems like we found nothing useful.
115     return [];
116 }
117 //
118 //////////////
119
120 default
121 {
122     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
123     on_rez(integer parm) { state rerun; }
124 }
125 state rerun { state_entry() { state default; } }
126
127 state real_default
128 {
129     state_entry() {
130         auto_retire();
131
132         integer i;
133         for (i = 0; i < llGetInventoryNumber(INVENTORY_SOUND); i++)
134             llPreloadSound(llGetInventoryName(INVENTORY_SOUND, i));
135     }
136
137     collision_start(integer total_number) {
138         if (llGetUnixTime() - last_played < TIME_BETWEEN_PLAYS) return;
139         last_played = llGetUnixTime();
140         integer i;
141         for (i = 0; i < llGetInventoryNumber(INVENTORY_SOUND); i++) {
142             float vol = SOUND_VOLUME;
143             if (llGetInventoryName(INVENTORY_SOUND, i) == "water-flowing3")
144                 vol /= 2.0;  // not so loud for this one.
145             llTriggerSound(llGetInventoryName(INVENTORY_SOUND, i), vol);
146 //llSay(0, "vol is " + (string)vol);
147         }
148     }
149 }
150