2 // huffware script: weapon fire, by fred huffhines.
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.
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.
11 // modifiable constants...
13 integer LEFT_HANDED = TRUE;
15 //float SPEED = 100.0;
18 float DELAY = 0.0; // an enforced delay before the weapon can fire again.
22 integer USE_ANIMATION = FALSE; // if this is true, then the bearer is posed.
24 // global variables...
28 rotation rotation_vec;
30 integer have_permissions = FALSE;
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.
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.
50 // example usage of the auto-retirement script:
53 // auto_retire(); // make sure newest addition is only version of script.
56 // this script is partly based on the self-upgrading scripts from markov brodsky
57 // and jippen faddoul.
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.
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);
88 // separates the base script name and version number. used by auto_retire.
89 list compute_basename_and_version(string to_chop_up)
91 // minimum script name is 2 characters plus a version.
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");
97 // look for space and v but do nothing else.
98 //log_it("pos=" + (string)space_v_posn);
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.
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);
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 ];
128 // seems like we found nothing useful.
134 // performs the act of firing the bullet.
138 rotation_vec = llGetRot();
139 velocity = llRot2Fwd(rotation_vec);
140 position = llGetPos();
141 position = position + velocity;
143 velocity = velocity * SPEED;
144 if (llGetInventoryNumber(INVENTORY_SOUND) > 0)
145 llTriggerSound(llGetInventoryName(INVENTORY_SOUND, 0), VOLUME);
147 for (i = 0; i < llGetInventoryNumber(INVENTORY_OBJECT); i++)
148 llRezObject(llGetInventoryName(INVENTORY_OBJECT, i), position, velocity, rotation_vec, 2814);
151 llSetTimerEvent(DELAY);
158 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
159 on_rez(integer parm) { state rerun; }
161 state rerun { state_entry() { state default; } }
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);
175 on_rez(integer param) { llResetScript(); }
177 run_time_permissions(integer permissions) {
178 if (permissions == PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS) {
179 llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
182 llStartAnimation("hold_L_bow");
183 llStopAnimation("aim_L_bow");
185 llStartAnimation("hold_R_handgun");
186 llStopAnimation("aim_R_handgun");
189 have_permissions = TRUE;
193 attach(key attachedAgent) {
194 if (attachedAgent != NULL_KEY) {
195 llRequestPermissions(llGetOwner(),
196 PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);
198 if (have_permissions) {
199 llStopAnimation("hold_R_handgun");
200 llStopAnimation("aim_R_handgun");
203 have_permissions = FALSE;
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) ) {
216 llSetTimerEvent(0.0);