5e3908ea27f8df5745e290bbdcb8ab64beff1eaf
[feisty_meow.git] / huffware / huffotronic_scripts / weapon_fire_v2.2.txt
1 
2 // huffware script: weapon fire, by fred huffhines.
3 //
4 // finds the first item in inventory and uses it as a bullet.  it also finds the first
5 // sound in inventory and uses that as the report noise for firing the bullet.
6 //
7 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 // do not use it in objects without fully realizing you are implicitly accepting that license.
9 //
10
11 // modifiable constants...
12
13 integer LEFT_HANDED = TRUE;
14
15 //float SPEED = 100.0;
16 float SPEED = 20.0;
17
18 float DELAY = 0.0;  // an enforced delay before the weapon can fire again.
19
20 float VOLUME = 0.2;
21
22 integer USE_ANIMATION = FALSE;  // if this is true, then the bearer is posed.
23
24 // global variables...
25
26 vector velocity;                          
27 vector position;                         
28 rotation rotation_vec;                       
29
30 integer have_permissions = FALSE;
31 integer armed = TRUE;
32
33 //////////////
34 // huffware script: auto-retire, by fred huffhines, version 2.5.
35 // distributed under BSD-like license.
36 //   !!  keep in mind that this code must be *copied* into another
37 //   !!  script that you wish to add auto-retirement capability to.
38 // when a script has auto_retire in it, it can be dropped into an
39 // object and the most recent version of the script will destroy
40 // all older versions.
41 //
42 // the version numbers are embedded into the script names themselves.
43 // the notation for versions uses a letter 'v', followed by two numbers
44 // in the form "major.minor".
45 // major and minor versions are implicitly considered as a floating point
46 // number that increases with each newer version of the script.  thus,
47 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
48 // and "hazmap v3.2" is a more recent version.
49 //
50 // example usage of the auto-retirement script:
51 //     default {
52 //         state_entry() {
53 //            auto_retire();  // make sure newest addition is only version of script.
54 //        }
55 //     }
56 // this script is partly based on the self-upgrading scripts from markov brodsky
57 // and jippen faddoul.
58 //////////////
59 auto_retire() {
60     string self = llGetScriptName();  // the name of this script.
61     list split = compute_basename_and_version(self);
62     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
63     string basename = llList2String(split, 0);  // script name with no version attached.
64     string version_string = llList2String(split, 1);  // the version found.
65     integer posn;
66     // find any scripts that match the basename.  they are variants of this script.
67     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
68 //log_it("invpo=" + (string)posn);
69         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
70         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
71             // found a basic match at least.
72             list inv_split = compute_basename_and_version(curr_script);
73             if (llGetListLength(inv_split) == 2) {
74                 // see if this script is more ancient.
75                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
76                 // must make sure that the retiring script is completely the identical basename;
77                 // just matching in the front doesn't make it a relative.
78                 if ( (llList2String(inv_split, 0) == basename)
79                     && ((float)inv_version_string < (float)version_string) ) {
80                     // remove script with same name from inventory that has inferior version.
81                     llRemoveInventory(curr_script);
82                 }
83             }
84         }
85     }
86 }
87 //
88 // separates the base script name and version number.  used by auto_retire.
89 list compute_basename_and_version(string to_chop_up)
90 {
91     // minimum script name is 2 characters plus a version.
92     integer space_v_posn;
93     // find the last useful space and 'v' combo.
94     for (space_v_posn = llStringLength(to_chop_up) - 3;
95         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
96         space_v_posn--) {
97         // look for space and v but do nothing else.
98 //log_it("pos=" + (string)space_v_posn);
99     }
100     if (space_v_posn < 2) return [];  // no space found.
101 //log_it("space v@" + (string)space_v_posn);
102     // now we zoom through the stuff after our beloved v character and find any evil
103     // space characters, which are most likely from SL having found a duplicate item
104     // name and not so helpfully renamed it for us.
105     integer indy;
106     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
107 //log_it("indy=" + (string)space_v_posn);
108         if (llGetSubString(to_chop_up, indy, indy) == " ") {
109             // found one; zap it.  since we're going backwards we don't need to
110             // adjust the loop at all.
111             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
112 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
113         }
114     }
115     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
116     // ditch the space character for our numerical check.
117     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
118     // strip out a 'v' if there is one.
119     if (llGetSubString(chop_suffix, 0, 0) == "v")
120         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
121     // if valid floating point number and greater than zero, that works for our version.
122     string basename = to_chop_up;  // script name with no version attached.
123     if ((float)chop_suffix > 0.0) {
124         // this is a big success right here.
125         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
126         return [ basename, chop_suffix ];
127     }
128     // seems like we found nothing useful.
129     return [];
130 }
131 //
132 //////////////
133
134 // performs the act of firing the bullet.
135 fire()
136 {
137     if (armed) {
138         rotation_vec = llGetRot();               
139         velocity = llRot2Fwd(rotation_vec);           
140         position = llGetPos();               
141         position = position + velocity;               
142         position.z += 0.75;                 
143         velocity = velocity * SPEED;
144         if (llGetInventoryNumber(INVENTORY_SOUND) > 0)
145             llTriggerSound(llGetInventoryName(INVENTORY_SOUND, 0), VOLUME);
146         integer i;
147         for (i = 0; i < llGetInventoryNumber(INVENTORY_OBJECT); i++)
148             llRezObject(llGetInventoryName(INVENTORY_OBJECT, i), position, velocity, rotation_vec, 2814);
149         if (DELAY != 0.0) {
150             armed = FALSE;
151             llSetTimerEvent(DELAY);
152         }
153     }
154 }
155
156 default
157 {
158     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
159     on_rez(integer parm) { state rerun; }
160 }
161 state rerun { state_entry() { state default; } }
162
163 state real_default
164 {
165     state_entry() {
166         auto_retire();
167         if (llGetInventoryNumber(INVENTORY_SOUND) > 0)
168             llPreloadSound(llGetInventoryName(INVENTORY_SOUND, 0));
169         if (!have_permissions) {
170             llRequestPermissions(llGetOwner(),
171                 PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);
172         }
173     }
174
175     on_rez(integer param) { llResetScript(); }
176
177     run_time_permissions(integer permissions) {
178         if (permissions == PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS) {
179             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
180             if (USE_ANIMATION) {
181                 if (LEFT_HANDED) {
182                     llStartAnimation("hold_L_bow");
183                     llStopAnimation("aim_L_bow");
184                 } else {
185                     llStartAnimation("hold_R_handgun");
186                     llStopAnimation("aim_R_handgun");
187                 }
188             }
189             have_permissions = TRUE;
190         }
191     }
192
193     attach(key attachedAgent) {
194         if (attachedAgent != NULL_KEY) {
195             llRequestPermissions(llGetOwner(),
196                 PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);   
197         } else {
198             if (have_permissions) {
199                 llStopAnimation("hold_R_handgun");
200                 llStopAnimation("aim_R_handgun");
201                 llReleaseControls();
202                 llSetRot(<0,0,0,1>);
203                 have_permissions = FALSE;
204             }
205         }
206     }
207
208     control(key name, integer levels, integer edges)  {
209         if ( ((edges & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
210                 && ((levels & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON) ) {
211             fire();
212         }
213     }
214     
215     timer() {
216         llSetTimerEvent(0.0);
217         armed = TRUE;
218     }
219 }