User:Allen Kerensky/Myriad Lite Preview2/Firearm v1.2 20110515 CC-BY-SA 3.0

From Second Life Wiki
< User:Allen Kerensky
Revision as of 14:11, 11 June 2011 by Allen Kerensky (talk | contribs) (added lsl header)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Firearm v1.2 20110515 CC-BY-SA 3.0

<lsl> // Firearm v1.2 20110515 CC-BY-SA // Based on phillip linden (SL) gunscript // With many refinements by Rhonin Nissondorf (SL) // Retrieved 2011-04-30 from http://wiki.secondlife.com/wiki/Gun_Script // Copyright © 2009 Linden Research, Inc. // Licensed under Creative Commons Attribution-Share Alike 3.0 (CC-BY-SA 3.0) // Converted to OSSL by Allen Kerensky (SL/OSG). //=========================================================================== // variables to be changed below. float BULLET_VELOCITY = 30.0; // change this to change the speed of the bullet. float REPEAT_DELAY = 0.20; //delay between bullets, i recommend you dont' set it to low. string gunsound = "gun"; // string; name of sound in inventory string ammo = "Myriad Lite Bullet v0.0.2 20110522"; //name of desired object to be shot out. Must be in the inventory of the "gun". string message1 = "stop playing with mine and ask for yours"; //message when you touch it. string message2 = " Hey put a message here"; string ANIM_HOLD = "hold_R_handgun"; // animation to use when holding pistol string ANIM_AIM = "PistolRightSteady1"; // animation to use when aiming in mouselook float TIMER_RES = 0.5; // time in seconds to check for mouselook to start aim animation, like an AO integer AVSTATE = 0; // is av in mouselook? vector OFFSET = <1.10, -0.25, 0.75>; // rez offset for bullet integer DAMAGEDICE = 1; // many damage dice does this bullet do in Myriad?

//=========================================================================== // don't alter anything below if your not familiar with it. // Runtime variables which change as we go. integer on = TRUE; vector pos; rotation rot; vector offset;

//=========================================================================== //The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled. default {

   state_entry() {    // Triggered on any state transition and start up
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH);
       llSetTimerEvent(TIMER_RES);
   }

   on_rez(integer rezparams) {    // Triggered on any state transition and start up
       llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH);
       //llPreloadSound(gunsound);
       //llResetScript();
       llSetTimerEvent(TIMER_RES);
   }
   run_time_permissions(integer perm) {    //Triggered when an agent grants run time permissions to task ,its auto grant when attached.
       //if (perm & PERMISSION_ATTACH ) {
       //    llAttachToAvatar(ATTACH_RHAND);
       //}
       if ( perm & PERMISSION_TAKE_CONTROLS ) {
           llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
       }
       if ( perm & PERMISSION_TRIGGER_ANIMATION ) {
           llStartAnimation(ANIM_HOLD);
       }
   }

   //touch_start(integer nd) {  // Triggered by the start of agent clicking on object
   //    if( on == TRUE ) {
   //        llOwnerSay(message1);
   //        on = FALSE;
   //    } else if (on == FALSE) {
   //        llOwnerSay(message2);
   //        on = TRUE;
   //    } 
   //}

   attach(key id) {    // When the object is dettached by an avatar 
       if ( id == NULL_KEY) {
           llStopAnimation(ANIM_HOLD);
       }          
   }

   changed(integer change) { //Various changes to the object/prim trigger this event.
       if(change & CHANGED_OWNER) {
           llStopAnimation(ANIM_HOLD);
           llResetScript();
       }
   }
   // Result of llTakeControls library function call and user input.
   control(key owner, integer level, integer edge) {
       integer pressed = level & edge;
       //level is when you click ,edge would be if you let up.
       if ( pressed & CONTROL_ML_LBUTTON ) {
           // Fire 1 bullet,,  the heart of the gun script.
           pos = llGetPos();
           rot = llGetRot();
           offset = OFFSET;
           offset *= rot;
           pos += offset;
           //llPointAt(pos); 
           vector fwd = llRot2Fwd(rot); 
           fwd *= BULLET_VELOCITY; 
           //integer i = 5;
           rot *= llEuler2Rot(<0, PI_BY_TWO, 0>);
           llPlaySound(gunsound,1.0); // here "gunsound"is a variable defined above.
           llRezObject(ammo, pos, fwd, rot, DAMAGEDICE); // does the actual work rezzes the ammo in the specified variables.
           llSleep(REPEAT_DELAY); // ditto with  REPEAT_DELAY.
       }
   }
   timer() {
       AVSTATE = llGetAgentInfo(llGetOwner());
       if ( AVSTATE & 0x0008 ) { // AGENT_MOUSELOOK
           llStopAnimation(ANIM_HOLD);
           llSleep(0.05);
           llStartAnimation(ANIM_AIM);
       } else {
           llStopAnimation(ANIM_AIM);
           llSleep(0.05);
           llStartAnimation(ANIM_HOLD);
       }
       AVSTATE = 0;
   }

} </lsl>