CCCPaste

a9ee3acd308ee

//
// Popgun
//
// This script is a basic gun- it waits for a mouseclick in mouselook and
// then fires a bullet in the direction the user is facing.
// It also animates the avatar holding it, to create a convining hold and
// firing look for the gun.
//
// This script can be used as a good basis for other weapons.
//


/////
//Channel Setup - Unique Channel for the RP System
/////
//Code I'm not releasing

/////
//Weapon Chat and Listen Variables
/////

string g_message; //The variable for the message received
integer listen_handle; //The listener variable for the RP System
integer listen_gesture; //The listener variable for the gestures
integer channumOffset = a number; //The RP System Channel Offset Number
integer channum; //The RP System Channel
integer gestchan = 5; //The Gesture Channel
integer weaponchanOffset = -a number; //The Weapon Strike Communications Channel - Offset
integer weaponchan; //The Weapon Strike Communications Channel
string HipDraw = "hdraw"; //Hip Draw Message
string SpineDraw = "sdraw"; //Spine Draw Message
string HipSheathe = "hsheathe"; //Hip Sheathe Message
string SpineSheathe = "ssheathe"; //Spine Sheathe Message
string WeaponOff = "Weapon Off"; //Player Has Zero HP; Turn The Weapon Off; Message From HUD

/////
//Main Gun Variables
/////

float SPEED = 20.0; // Speed of arrow in meters/sec
integer LIFETIME = 7; // How many seconds will bullets live
// before deleting themselves
float DELAY = 0.2; // Delay between shots to impose

vector vel; // Used to store velocity of arrow to be shot
vector pos; // Used to store position of arrow to be shot
rotation rot; // Used to store rotation of arrow to be shot

integer have_permissions = FALSE; // Indicates whether wearer has yet given permission
// to take over their controls and animation.

integer armed = TRUE; // Used to impose a short delay between firings

string instruction_held_1 = "Use Mouselook (press 'M') or the crouch button to shoot me.";
string instruction_held_2 = "Choose 'Detach' from my menu to take me off.";
// Echoed to wearer when they are holding the bow
string instruction_not_held = "Right-click (apple-click) me, and choose More > Wear' from the menu to use me.";
// Echoed to toucher if not worn
string Bullet_Name = "projectile";

/////
//Notecard Information
/////

string notecardName = "Gun Information";
string ANIM1 = "The gun firing animation"; //Gun Firing Animation; the key value for the LSD pair
string Animation1; //Gun Firing Animation; the value for the LSD pair
string ANIM2 = "The gun holding animation"; //Gun Holding Animation; the key value for the LSD pair
string Animation2; //Gun Holding Animation; the value for the LSD pair
string ANIM3 = "The gun aiming animation"; //Gun Aiming Animation; the key value for the LSD pair
string Animation3; //Gun Aiming Animation; the key value for the LSD pair
string SOUND1 = "The gun shooting sound effect"; //Gun Shooting Sound Effect; the key value for the LSD pair
string SoundEffect; //Gun Shooting Sound Effect; the key value for the LSD pair

/////
//The Safety and Release Variables
/////

integer SAFETY_ON = TRUE; //Is the weapon active?
string ReleaseCon = "Releasing Controls"; //Release Controls Message
string Armed = "Armed"; //Let's the HUD know the Player is Armed
string Unarmed = "Unarmed"; //Let's the HUD know the Player is Unarmed

/////
//Permissions Variables
/////

integer permsgiven; //Did the player give permissions? True or False

/////
//Safety On
/////

SAFETYON()
{

SAFETY_ON = TRUE; //Switch the safety on

llRequestPermissions(llGetOwner(),PERMISSION_TAKE_CONTROLS|PERMISSION_TRIGGER_ANIMATION|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA); // request permissions

permsgiven = llGetPermissions(); //Set the integer to check that permissions were granted

if (permsgiven != (PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION | PERMISSION_ATTACH| PERMISSION_TRACK_CAMERA)) //If permissions weren't granted, request permissions
{
llRequestPermissions(llGetOwner(),PERMISSION_TAKE_CONTROLS|PERMISSION_TRIGGER_ANIMATION|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
}

else //Take controls if permissions are granted
{
llTakeControls(CONTROL_ML_LBUTTON | CONTROL_LBUTTON | CONTROL_UP | CONTROL_FWD | CONTROL_BACK | CONTROL_ROT_LEFT | CONTROL_LEFT | CONTROL_RIGHT | CONTROL_ROT_RIGHT | CONTROL_DOWN, TRUE, TRUE);
}

llSay(0, ReleaseCon); //Message to Players that controls were released
llSay(channum, Unarmed); //Message to HUD that Player is Unarmed
llStopAnimation(llLinksetDataRead(ANIM2));
llStopAnimation(llLinksetDataRead(ANIM3));
llReleaseControls(); //Release the controls
llSetRot(<0,0,0,1>);
have_permissions = FALSE;
//llDetachFromAvatar(); //Optional: Can detatch the weapon when releasing controls, but will force player to have to reattach to arm the weapon
}

/////
//Safety Off
/////

SAFETYOFF()
{
llSay(channum, Armed); //Tell the HUD that Player is Armed
SAFETY_ON = FALSE; //switch the safety off

llRequestPermissions(llGetOwner(),PERMISSION_TAKE_CONTROLS|PERMISSION_TRIGGER_ANIMATION|PERMISSION_TRACK_CAMERA); // request permissions

permsgiven = llGetPermissions(); //Set the integer to check that permissions were granted

if (permsgiven != (PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION|PERMISSION_TRACK_CAMERA)) //If permissions weren't granted, request permissions
{
llRequestPermissions(llGetOwner(),PERMISSION_TAKE_CONTROLS|PERMISSION_TRIGGER_ANIMATION|PERMISSION_TRACK_CAMERA);
}
else //Take controls if permissions are granted
{
llTakeControls(CONTROL_ML_LBUTTON | CONTROL_LBUTTON | CONTROL_UP | CONTROL_FWD | CONTROL_BACK | CONTROL_ROT_LEFT | CONTROL_LEFT | CONTROL_RIGHT | CONTROL_ROT_RIGHT | CONTROL_DOWN, TRUE, TRUE);
llStartAnimation(llLinksetDataRead(ANIM2));
}
}

/////
//Release Controls and Reset Script
/////

ReleaseReset()
{
llSay(0, ReleaseCon); //Inform Players that controls were released
llSay(channum, Unarmed); //Inform HUD that Player is Unarmed
llStopAnimation(llLinksetDataRead(ANIM2));
llStopAnimation(llLinksetDataRead(ANIM3));
llReleaseControls(); //Release Controls
llSetRot(<0,0,0,1>);
have_permissions = FALSE;
llResetScript(); //Reset Script
}

/////
//The Fire Function
/////

fire()
{
//
// This subroutine creates and fires an arrow
//
if (armed)
{
//
// Actually fires the arrow
//
armed = FALSE;
rot = llGetCameraRot(); // Get the camera rotation position in relation to the avatar
vel = llRot2Fwd(rot); // Convert rotation to a direction vector
pos = llGetPos(); // Get position of avatar to create arrow
pos = pos + vel; // Create projectile slightly in direction of travel
pos.z += 0.75; // Correct creation point upward to eye point
// from hips, so that in mouselook we see projectile
// travelling away from the camera.
vel = vel * SPEED; // Multiply normalized vector by speed

llStartAnimation(llLinksetDataRead(ANIM1)); // Trigger the gun fire animation
llTriggerSound(llLinksetDataRead(SOUND1), 1.0); // Make the sound of the gun being shot
llRezObject(Bullet_Name, pos, vel, rot, LIFETIME);
// Create the actual projectile from object
// inventory, and set its position, velocity,
// and rotation. Pass a parameter to it to
// tell it how long to live.

llSetTimerEvent(DELAY); // Wait until can fire again
}
}

/////
//The Listen Function
/////

thelistenFunction()
{
list data = llCSV2List(g_message); //Make the message into a list
string ID = llList2String(data,0); //Identification String is the first datum on the list

if( g_message == HipDraw )
{
SAFETYOFF();
}
else if( g_message == SpineDraw )
{
SAFETYOFF();
}
else if(g_message == HipSheathe)
{
SAFETYON();
}
else if(g_message == SpineSheathe)
{
SAFETYON();
}
else if(g_message == WeaponOff)
{
SAFETYON();
}
}

default
{
//What To Do When The Object Changes Owner
changed(integer change)
{
if(change & CHANGED_OWNER) // reset the script when a new person owns the weapon
{
llResetScript();
}
}

attach(key attachedAgent)
{
//
// If attached/detached from agent, change behavior
//
if (attachedAgent != NULL_KEY)
{
// Bow has been attached or rezzed from inventory, so
// ask for needed permissions.
llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA);
}
else
{
// Bow has been detached from avatar, so stop animation and release controls
if (have_permissions)
{
ReleaseReset();
}
}
}

state_entry()
//
// This routine is called whenever the script is edited and restarted. So if you
// are editing the bow while wearing it, this code will re-request permissions
// to animate and capture controls.
//
{
if (llStringLength(llLinksetDataRead(ANIM1)) == 0) //If there is no data stored in this key value, then do the following
{
llGetNumberOfNotecardLines(notecardName); //Put the notecard in the cache
Animation1 = llGetNotecardLineSync(notecardName, 0); //Assign the value in the notecard to the Animation1 variable
Animation2 = llGetNotecardLineSync(notecardName, 1); //Assign the value in the notecard to the Animation2 variable
Animation3 = llGetNotecardLineSync(notecardName, 2); //Assign the value in the notecard to the Animation3 variable
SoundEffect = llGetNotecardLineSync(notecardName, 3); //Assign the value in the notecard to the SoundEffect variable

llLinksetDataWrite(ANIM1, Animation1); //Record the Animation1 Value in LSD
llLinksetDataWrite(ANIM2, Animation2); //Record the Animation2 Value in LSD
llLinksetDataWrite(ANIM3, Animation3); //Record the Animation3 Value in LSD
llLinksetDataWrite(SOUND1, SoundEffect); //Record the SoundEffect Value in LSD
llRemoveInventory(notecardName); //Remove the notecard from inventory
}

if (!have_permissions)
{
llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA);
}

AChannel = something
weaponchan = something
channum = something
llListenRemove(listen_handle); //Remove the listener
llListenRemove(listen_gesture); //Remove the listener
listen_handle = llListen(channum, "", "", ""); //Add the listener for Player HUD communications
listen_gesture = llListen(gestchan, "","",""); //Add the listener for Gesture communications
SAFETYOFF(); //Safety is off, the weapon is active, and the player is armed

}
on_rez(integer param)
{
//
// Called when the gun is created from inventory.
//
llPreloadSound(llLinksetDataRead(SOUND1)); // Preload shooting sound so you hear it
}

run_time_permissions(integer permissions)
{
//
// This routine is called when the user accepts the permissions request
// (sometimes this is automatic)
// so on receiving permissions, start animation and take controls.
//
if (permissions == PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA)
{
if (!have_permissions)
{
llWhisper(0, instruction_held_1);
llWhisper(0, instruction_held_2);
}
llTakeControls(CONTROL_ML_LBUTTON | CONTROL_LBUTTON | CONTROL_UP | CONTROL_FWD | CONTROL_BACK | CONTROL_ROT_LEFT | CONTROL_LEFT | CONTROL_RIGHT | CONTROL_ROT_RIGHT | CONTROL_DOWN, TRUE, TRUE);
llStartAnimation(llLinksetDataRead(ANIM2));
have_permissions = TRUE;
}
}

listen(integer channel, string name, key id, string message)
{

if(channel == channum) //If the messages are from the HUD
{
g_message = message; //setting the global variable to the message
thelistenFunction(); //Go to the listen function to figure out where to go from here
}

else if(channel == gestchan) //If the messages are from the Gesture Channel
{
key ownerofobject = llGetOwner(); //Get the UUID of the owner of this sword
key ownerofspeaker = llGetOwnerKey(id); //Get the UUID of the person speaking the commands

if(ownerofspeaker != ownerofobject) //If the speaker isn't the owner of the weapon, do nothing
{
}

else
{
g_message = llToLower(message); //Make the messages from the Player all lowercase, so the Player can type the command any way they want
thelistenFunction(); //Go to the listen function to figure out where to go from here
}
}
}

control(key name, integer levels, integer edges)
{
// This function is called when the mouse button or other controls
// are pressed, and the controls are being captured.
//
// Note the logical AND (single &) used - the levels and edges
// variables passed in are bitmasks, and must be checked with
// logical compare.
//
// Checking for both edge and level means that the button has just
// been pushed down, which is when we want to fire the arrow!
//
if ( ((edges & (CONTROL_ML_LBUTTON | CONTROL_DOWN)))
&&((levels & (CONTROL_ML_LBUTTON | CONTROL_DOWN))) )
{
// If left mousebutton is pressed, fire arrow
fire();
}
}

touch_start(integer num)
{
// If touched, remind user how to enter mouselook and shoot
if (have_permissions)
{
llWhisper(0, instruction_held_1);
llWhisper(0, instruction_held_2);
}
else
{
llWhisper(0, instruction_not_held);
}
}

timer()
{
// After timer expires, allow user to shoot gun again
llStopAnimation(llLinksetDataRead(ANIM1));
llSetTimerEvent(0.0);
armed = TRUE;
}

}