PDA

View Full Version : Weapon Samples



Matsilagi
31-01-2016, 07:16 PM
Where can i get a few weapon samples? Just to get hold of AngelScript.

I only need a gun that fires an ent, an bullet ent (if possible), a bullet-firing gun and a simple melee gun.

I wanna remake some Quake weapons, but i have no idea on where to start, and the documentation seems to have too much info for such simple weapons.

JonnyBoy0719
01-02-2016, 07:04 AM
For some good examples, check the default HL ones under scripts/maps/hl_weapons :)

Zorbos
02-02-2016, 01:31 AM
Here's a remake of the default 9mmhandgun written in AS. You can probably change the models and sounds to get it to fit what you need for your bullet-based Quake weapons.


// Sven Co-op Weapon Script
// 9mm Handgun
// Author: Zorbos

const int HANDGUN_DEFAULT_AMMO = 17;
const int HANDGUN_MAX_CARRY = 250;
const int HANDGUN_MAX_CLIP = 17;
const int HANDGUN_WEIGHT = 5;

enum HandgunAnimation
{
HANDGUN_IDLE1 = 0,
HANDGUN_IDLE2,
HANDGUN_IDLE3,
HANDGUN_SHOOT,
HANDGUN_SHOOT_EMPTY,
HANDGUN_RELOAD,
HANDGUN_RELOAD_NOT_EMPTY,
HANDGUN_DRAW,
HANDGUN_HOLSTER,
HANDGUN_ADD_SILENCER
};

class weapon_sc9mmhandgun : ScriptBasePlayerWeaponEntity
{
int m_iShell;

void Spawn()
{
Precache();
g_EntityFuncs.SetModel( self, "models/w_9mmhandgun.mdl" );

self.m_iDefaultAmmo = HANDGUN_DEFAULT_AMMO;

self.FallInit();// get ready to fall
}

void Precache()
{
self.PrecacheCustomModels();
g_Game.PrecacheModel( "models/v_9mmhandgun.mdl" );
g_Game.PrecacheModel( "models/w_9mmhandgun.mdl" );
g_Game.PrecacheModel( "models/p_9mmhandgun.mdl" );

m_iShell = g_Game.PrecacheModel( "models/shell.mdl" ); // brass casing

g_SoundSystem.PrecacheSound( "items/9mmclip2.wav" ); // reloading sound
g_SoundSystem.PrecacheSound( "weapons/dryfire.wav" ); // empty sound
g_SoundSystem.PrecacheSound( "weapons/pl_gun3.wav" ); // firing sound
}

int AddToPlayer( CBasePlayer@ pPlayer )
{
if( BaseClass.AddToPlayer( pPlayer ) == TRUE )
{
NetworkMessage message( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
message.WriteLong( self.m_iId );
message.End();
return TRUE;
}

return FALSE;
}

int GetItemInfo( ItemInfo& out info )
{
info.iMaxAmmo1 = HANDGUN_MAX_CARRY;
info.iMaxAmmo2 = -1;
info.iMaxClip = HANDGUN_MAX_CLIP;
info.iSlot = 1;
info.iPosition = 6;
info.iFlags = 0;
info.iWeight = HANDGUN_WEIGHT;

return 1;
}

BOOL PlayEmptySound()
{
if( self.m_iPlayEmptySound != 0 )
{
self.m_iPlayEmptySound = 0;

g_SoundSystem.EmitSoundDyn( self.m_pPlayer.edict(), CHAN_WEAPON, "weapons/dryfire.wav", 0.8, ATTN_NORM, 0, PITCH_NORM );
}

return FALSE;
}

BOOL Deploy()
{
return self.DefaultDeploy( self.GetV_Model( "models/v_9mmhandgun.mdl" ), self.GetP_Model( "models/p_9mmhandgun.mdl" ), HANDGUN_DRAW, "onehanded" );
}

float WeaponTimeBase()
{
return g_Engine.time; //g_WeaponFuncs.WeaponTimeBase();
}

void PrimaryAttack()
{
if (self.m_iClip == 0)
{
self.PlayEmptySound();
self.m_flNextPrimaryAttack = g_Engine.time + 0.30;
return;
}
else if (self.m_iClip != 0)
{
self.SendWeaponAnim( HANDGUN_SHOOT, 0, 0 );
}
else
{
self.SendWeaponAnim( HANDGUN_SHOOT_EMPTY, 0, 0 );
}

g_SoundSystem.EmitSoundDyn( self.m_pPlayer.edict(), CHAN_WEAPON, "weapons/pl_gun3.wav", Math.RandomFloat( 0.92, 1.0 ), ATTN_NORM, 0, 98 + Math.RandomLong( 0, 3 ) );

self.m_pPlayer.m_iWeaponVolume = LOUD_GUN_VOLUME;
self.m_pPlayer.m_iWeaponFlash = NORMAL_GUN_FLASH;

--self.m_iClip;

// player "shoot" animation
self.m_pPlayer.SetAnimation( PLAYER_ATTACK1 );

Vector vecSrc = self.m_pPlayer.GetGunPosition();
Vector vecAiming = self.m_pPlayer.GetAutoaimVector( AUTOAIM_2DEGREES );

self.m_pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_2DEGREES, 8192, BULLET_PLAYER_9MM, 0 );
self.m_flNextPrimaryAttack = g_Engine.time + 0.30;
self.m_flTimeWeaponIdle = g_Engine.time + g_PlayerFuncs.SharedRandomFloat( self.m_pPlayer.random_seed, 10, 15 );

self.m_pPlayer.pev.punchangle.x = -2.0;

TraceResult tr;
float x, y;

g_Utility.GetCircularGaussianSpread( x, y );

Vector vecDir = vecAiming
+ x * VECTOR_CONE_2DEGREES.x * g_Engine.v_right
+ y * VECTOR_CONE_2DEGREES.y * g_Engine.v_up;

Vector vecEnd = vecSrc + vecDir * 4096;

g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, self.m_pPlayer.edict(), tr );

if( tr.flFraction < 1.0 )
{
if( tr.pHit !is null )
{
CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );

if( pHit is null || pHit.IsBSPModel() == TRUE )
g_WeaponFuncs.DecalGunshot( tr, BULLET_PLAYER_9MM );
}
}
}

void SecondaryAttack()
{
if (self.m_iClip == 0)
{
self.PlayEmptySound();
self.m_flNextSecondaryAttack = g_Engine.time + 0.16;
return;
}
else if (self.m_iClip != 0)
{
self.SendWeaponAnim( HANDGUN_SHOOT, 0, 0 );
}
else
{
self.SendWeaponAnim( HANDGUN_SHOOT_EMPTY, 0, 0 );
}

g_SoundSystem.EmitSoundDyn( self.m_pPlayer.edict(), CHAN_WEAPON, "weapons/pl_gun3.wav", Math.RandomFloat( 0.92, 1.0 ), ATTN_NORM, 0, 98 + Math.RandomLong( 0, 3 ) );

self.m_pPlayer.m_iWeaponVolume = LOUD_GUN_VOLUME;
self.m_pPlayer.m_iWeaponFlash = NORMAL_GUN_FLASH;

--self.m_iClip;

// player "shoot" animation
self.m_pPlayer.SetAnimation( PLAYER_ATTACK1 );

Vector vecSrc = self.m_pPlayer.GetGunPosition();
Vector vecAiming = self.m_pPlayer.GetAutoaimVector( AUTOAIM_2DEGREES );

self.m_pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_2DEGREES, 8192, BULLET_PLAYER_9MM, 0 );

self.m_flNextSecondaryAttack = g_Engine.time + 0.16;
self.m_flTimeWeaponIdle = g_Engine.time + g_PlayerFuncs.SharedRandomFloat( self.m_pPlayer.random_seed, 10, 15 );

self.m_pPlayer.pev.punchangle.x = -2.0;

TraceResult tr;
float x, y;

g_Utility.GetCircularGaussianSpread( x, y );

Vector vecDir = vecAiming
+ x * VECTOR_CONE_10DEGREES.x * g_Engine.v_right
+ y * VECTOR_CONE_10DEGREES.y * g_Engine.v_up;

Vector vecEnd = vecSrc + vecDir * 4096;

g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, self.m_pPlayer.edict(), tr );

if( tr.flFraction < 1.0 )
{
if( tr.pHit !is null )
{
CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );

if( pHit is null || pHit.IsBSPModel() == TRUE )
g_WeaponFuncs.DecalGunshot( tr, BULLET_PLAYER_9MM );
}
}
}

void Reload()
{
if (self.m_iClip == HANDGUN_MAX_CLIP) // Can't reload if we have a full magazine already!
return;

if (self.m_iClip == 0)
{
self.DefaultReload( HANDGUN_MAX_CLIP, HANDGUN_RELOAD, 1.85, 0 );
}
else
{
self.DefaultReload( 18, HANDGUN_RELOAD_NOT_EMPTY, 1.60, 0 );
}

BaseClass.Reload();
return;
}
}

string GetSCHandgunName()
{
return "weapon_sc9mmhandgun";
}

void RegisterSCHandgun()
{
g_CustomEntityFuncs.RegisterCustomEntity( "weapon_sc9mmhandgun", GetSCHandgunName() );
g_ItemRegistry.RegisterWeapon( GetSCHandgunName(), "sc_weapons", "9mm" );

Changing x value of VECTOR_CONE_xDEGREES in this line will increase/decrease the weapon's firing spread:


self.m_pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_2DEGREES, 8192, BULLET_PLAYER_9MM, 0 );

This line affects the recoil/kick of the weapon:


self.m_pPlayer.pev.punchangle.x = -2.0;

And these lines can be used to increase/decrease the firing rate for the respective attacks (Primary and Secondary). Change the floating point value at the end of the line. Lower values will make the gun shoot faster.


self.m_flNextPrimaryAttack = g_Engine.time + 0.30;
self.m_flNextSecondaryAttack = g_Engine.time + 0.16;

The bit at the end is to register the weapon. Make sure you pick a unique weapon name. The part where it says "sc_weapons" specifies the folder in the sprites/ directory where the game will look for the WEAPONNAME.txt file that it will read it's sprites (crosshairs, selection sprite, etc) from. Then you just need to put RegisterSCHandgun(); into a map script and you're good to go.

Hope this helps!

Gazyi
04-02-2016, 11:37 AM
What kind of bullets we have and can we register a new type of ammo? If yes, how to do that?

Solokiller
04-02-2016, 11:55 AM
For bullets, check the Bullet enum.
Ammo types can be registered, but it's done in a rather ugly way:
When you want your weapon to use a new ammo type, have it return the ammo information you want to use in GetItemInfo. The name, maximum carry amount, etc. When the weapon is precached, it will also register the ammo type.

We're planning to change it, so don't expect the current approach to work for much longer.