Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Automatic Weapon Script

Jim Bunderfeld
The Coder
Join date: 1 Mar 2004
Posts: 161
08-01-2004 14:38
First off in-game I am a midget, and when I fire this gun, it shoots below where I am aiming. Any help is greatly appreciated.

CODE

// where the gun attaches itself if you click on it
// switch this to ATTACH_LHAND if you are left-handed
integer gAttachPart = ATTACH_RHAND;

key owner;



integer gDesiredPerm;
// Indicates whether wearer has yet given permission
integer gHavePermissions;

// Bullet travel speed
float gBulletSpeed = 55.0;

// when we last fired a shot
float gLastFireTime = 0;
// how long it takes to reload when tapping
float gTapReloadTime = 0.5;
// how long it takes to reload when holding
float gHoldReloadTime = 0.01;
// how long it takes when we're out of ammo
float gOutOfAmmoReloadTime = 2.2;
// how many shots before reload is necessary
integer gAmmoClipSize = 30;
// how much ammo we have right now
integer gAmmo;

// HACK: how far away the eye is from the avatar's center (approximately)
vector gEyeOffset;



// the gun has only one state

default
{
on_rez(integer start_param)
{
owner = llGetOwner();

vector size = llGetAgentSize(llGetOwner());
gEyeOffset.z = gEyeOffset.z * (size.z / 2.0);



// NOTE: can't do this if we want to attach by dragging from
// inventory onto the av, because the llResetScript(); clears
// all the callbacks, including attach()
// llResetScript();

// NOTE 2: This can be uncommented in 1.1 because you won't have
// to ask for permissions, you can just take them. But for now
// it pops up two dialog boxes if you drag onto the av, so I'll
// leave it out.
// Try to attach to the rezzer
// if ( !gHavePermissions )
// {
// llRequestPermissions(llGetOwner(), gDesiredPerm);
// }
}

state_entry()
{
llListen(0,"",owner,"");

gHavePermissions = FALSE;
// this should be initialized directly with the variable
// but I can't do that due to a bug
gDesiredPerm = (PERMISSION_TAKE_CONTROLS
| PERMISSION_TRIGGER_ANIMATION);
// Start with a full clip
gAmmo = gAmmoClipSize;
llResetTime();
}

// the player has touched us (left-clicked)
// give directions

// Player attaches us, either by dragging onto self or from pie menu
attach(key av_key)
{



if (av_key != NULL_KEY)
{
// Can't attach if we don't own it
if ( av_key != llGetOwner() )
{

return;
}

//
// Always request permissions on attach, as we may be re-rezzing on login
llRequestPermissions(av_key, gDesiredPerm);

// run_time_permissions() is executed after this call
} else
{


if ( gHavePermissions )
{
// we are being detached
llStopAnimation("aim_r_rifle");
llReleaseControls();
llSetRot(<0.0, 0.0, 0.0, 1.0> );
gHavePermissions = FALSE;
}
}
}

// this is called whenever llRequestPermissions() returns
// i.e. the user has responded or dismissed the dialog
// perm is the permissions we now have
run_time_permissions(integer perm)
{


// see if we now have the permissions we need
if ( (perm & gDesiredPerm) == gDesiredPerm )
{


// we got the permissions we asked for
gHavePermissions = TRUE;
llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
llStartAnimation("hold_R_rifle");
} else
{

// we didn't get them, kill ourselves
llDie();
}
}




// the player has used the controls, process them
control(key owner, integer level, integer edge)
{


// has our gun reloaded?
float time = llGetTime();

// see if av has fired
// (edge & level) == down edges, (edge & !level) == up edges
// repeat rate is faster for tapping than holding
if ( ( ((edge & level) & CONTROL_ML_LBUTTON)
&& (time > gTapReloadTime) )
|| ( (time > gHoldReloadTime)
&& (level & CONTROL_ML_LBUTTON) ) )
{
// if player is out of ammo, must wait for reload
if ( (gAmmo >= 1) || (time > gOutOfAmmoReloadTime) )
{
// bang!
llTriggerSound("thompson_01", 1.0);
llResetTime(); // we fired, set timer back to 0

// HACK: gun shoots from gun pos, mouselook looks
// from player's eye, so we try to move the gun
// up to the eye. but we really don't
// know where it is, so we guess (see gEyeOffset)
vector my_pos = llGetPos() + gEyeOffset;
rotation my_rot = llGetRot();
vector my_fwd = llRot2Fwd(my_rot);

// Rez a bullet!
llRezObject("Bullet",
my_pos,
my_fwd * gBulletSpeed,
my_rot,
1);

// decrease ammo count
--gAmmo;
if ( gAmmo > 0 )
{
// still bullets left
}
else if ( gAmmo == 0 )
{
// we just ran out of shots
// play reload sound here after a delay
llTriggerSound("thompson_reload", 1.0);
}
else // gAmmo < 0
{
// we've just fired first round of new clip
gAmmo = gAmmoClipSize - 1;
}
} else
{

}
}

} // control


}
_____________________
| - Linux Users - | #SecondLifeLinux on IRC.EnterTheGame.com
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
08-01-2004 15:03
thats because you nuked the value of gEyeOffset
play with it's z value some.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Jim Bunderfeld
The Coder
Join date: 1 Mar 2004
Posts: 161
08-01-2004 15:39
Ok I figured it out <0,0,0.83>


But after I die it seems to miss up again.
_____________________
| - Linux Users - | #SecondLifeLinux on IRC.EnterTheGame.com