Weapon CPP

A Forum Thread for Half-Life 2: Episode Two

Hey, you! We are one of the oldest mod sites on the Interwebs. Sign up and maximize your browsing experience.

  • Subscribe: Get notified of new submissions like this.
  • Say Thanks: Show your appreciation by sending Darththork99 points.
  • Post: Give Darththork99 your thoughts on this Thread.
  • Flag: Alert moderators and warn members of a problem with this Thread.
  • Watch: Get notified when this Thread is updated.
Mascot

Join the community!

Sign up

Already a Bananite? Login

I REALLY need some assistance.

I've been trying to make some custom weapons for a Source Mod for quite some time now. No matter where I go. I always come across use Visual Studio 2005. So I do. But I have no clue how to edit the weapons in Ep2. Regular weapon scripts are like this: // Pistol WeaponData { // Weapon data is loaded by both the Game and Client DLLs. "printname" "Sidearm" "viewmodel" "models/weapons/v_pistol.mdl" "playermodel" "models/weapons/w_pistol.mdl" "anim_prefix" "pistol" "bucket" "1" "bucket_position" "0" "bucket_360" "0" "bucket_position_360" "0" "clip_size" "26" "primary_ammo" "Pistol" "secondary_ammo" "None" "weight" "2" "item_flags" "0" "damage" "29" // Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds) SoundData { "reload" "Weapon_Pistol.Reload" "reload_npc" "Weapon_Pistol.NPC_Reload" "empty" "Weapon_Pistol.Empty" "single_shot" "Weapon_Pistol.Single" "single_shot_npc" "Weapon_Pistol.NPC_Single" "special1" "Weapon_Pistol.Special1" "special2" "Weapon_Pistol.Special2" "burst" "Weapon_Pistol.Burst" } // Weapon Sprite data is loaded by the Client DLL. TextureData { "weapon" { "font" "WeaponIcons" "character" "d" } "weapon_s" { "font" "WeaponIconsSelected" "character" "d" } "ammo" { "font" "WeaponIcons" "character" "p" } "crosshair" { "font" "Crosshairs" "character" "Q" } "autoaim" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } } } However. One of these CPP scripts look like this: ////========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// // Purpose: Pistol - hand gun // // $NoKeywords: $ //=============================================================================// #include "cbase.h" #include "NPCEvent.h" #include "basehlcombatweapon.h" #include "basecombatcharacter.h" #include "AI_BaseNPC.h" #include "player.h" #include "gamerules.h" #include "in_buttons.h" #include "soundent.h" #include "game.h" #include "vstdlib/random.h" #include "gamestats.h" // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" #define PISTOL_FASTEST_REFIRE_TIME 0.1f #define PISTOL_FASTEST_DRY_REFIRE_TIME 0.2f #define PISTOL_ACCURACY_SHOT_PENALTY_TIME 0.2f // Applied amount of time each shot adds to the time we must recover from #define PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME 1.5f // Maximum penalty to deal out ConVar pistol_use_new_accuracy( "pistol_use_new_accuracy", "1" ); //----------------------------------------------------------------------------- // CWeaponPistol //----------------------------------------------------------------------------- class CWeaponPistol : public CBaseHLCombatWeapon { DECLARE_DATADESC(); public: DECLARE_CLASS( CWeaponPistol, CBaseHLCombatWeapon ); CWeaponPistol(void); DECLARE_SERVERCLASS(); void Precache( void ); void ItemPostFrame( void ); void ItemPreFrame( void ); void ItemBusyFrame( void ); void PrimaryAttack( void ); void AddViewKick( void ); void DryFire( void ); void Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator ); void UpdatePenaltyTime( void ); int CapabilitiesGet( void ) { return bits_CAP_WEAPON_RANGE_ATTACK1; } Activity GetPrimaryAttackActivity( void ); virtual bool Reload( void ); virtual const Vector& GetBulletSpread( void ) { // Handle NPCs first static Vector npcCone = VECTOR_CONE_5DEGREES; if ( GetOwner() && GetOwner()->IsNPC() ) return npcCone; static Vector cone; if ( pistol_use_new_accuracy.GetBool() ) { float ramp = RemapValClamped( m_flAccuracyPenalty, 0.0f, PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME, 0.0f, 1.0f ); // We lerp from very accurate to inaccurate over time VectorLerp( VECTOR_CONE_1DEGREES, VECTOR_CONE_6DEGREES, ramp, cone ); } else { // Old value cone = VECTOR_CONE_4DEGREES; } return cone; } virtual int GetMinBurst() { return 1; } virtual int GetMaxBurst() { return 3; } virtual float GetFireRate( void ) { return 0.5f; } DECLARE_ACTTABLE(); private: float m_flSoonestPrimaryAttack; float m_flLastAttackTime; float m_flAccuracyPenalty; int m_nNumShotsFired; }; IMPLEMENT_SERVERCLASS_ST(CWeaponPistol, DT_WeaponPistol) END_SEND_TABLE() LINK_ENTITY_TO_CLASS( weapon_pistol, CWeaponPistol ); PRECACHE_WEAPON_REGISTER( weapon_pistol ); BEGIN_DATADESC( CWeaponPistol ) DEFINE_FIELD( m_flSoonestPrimaryAttack, FIELD_TIME ), DEFINE_FIELD( m_flLastAttackTime, FIELD_TIME ), DEFINE_FIELD( m_flAccuracyPenalty, FIELD_FLOAT ), //NOTENOTE: This is NOT tracking game time DEFINE_FIELD( m_nNumShotsFired, FIELD_INTEGER ), END_DATADESC() acttable_t CWeaponPistol::m_acttable[] = { { ACT_IDLE, ACT_IDLE_PISTOL, true }, { ACT_IDLE_ANGRY, ACT_IDLE_ANGRY_PISTOL, true }, { ACT_RANGE_ATTACK1, ACT_RANGE_ATTACK_PISTOL, true }, { ACT_RELOAD, ACT_RELOAD_PISTOL, true }, { ACT_WALK_AIM, ACT_WALK_AIM_PISTOL, true }, { ACT_RUN_AIM, ACT_RUN_AIM_PISTOL, true }, { ACT_GESTURE_RANGE_ATTACK1, ACT_GESTURE_RANGE_ATTACK_PISTOL,true }, { ACT_RELOAD_LOW, ACT_RELOAD_PISTOL_LOW, false }, { ACT_RANGE_ATTACK1_LOW, ACT_RANGE_ATTACK_PISTOL_LOW, false }, { ACT_COVER_LOW, ACT_COVER_PISTOL_LOW, false }, { ACT_RANGE_AIM_LOW, ACT_RANGE_AIM_PISTOL_LOW, false }, { ACT_GESTURE_RELOAD, ACT_GESTURE_RELOAD_PISTOL, false }, { ACT_WALK, ACT_WALK_PISTOL, false }, { ACT_RUN, ACT_RUN_PISTOL, false }, }; IMPLEMENT_ACTTABLE( CWeaponPistol ); //----------------------------------------------------------------------------- // Purpose: Constructor //----------------------------------------------------------------------------- CWeaponPistol::CWeaponPistol( void ) { m_flSoonestPrimaryAttack = gpGlobals->curtime; m_flAccuracyPenalty = 0.0f; m_fMinRange1 = 24; m_fMaxRange1 = 1500; m_fMinRange2 = 24; m_fMaxRange2 = 200; m_bFiresUnderwater = true; } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::Precache( void ) { BaseClass::Precache(); } //----------------------------------------------------------------------------- // Purpose: // Input : // Output : //----------------------------------------------------------------------------- void CWeaponPistol::Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator ) { switch( pEvent->event ) { case EVENT_WEAPON_PISTOL_FIRE: { Vector vecShootOrigin, vecShootDir; vecShootOrigin = pOperator->Weapon_ShootPosition(); CAI_BaseNPC *npc = pOperator->MyNPCPointer(); ASSERT( npc != NULL ); vecShootDir = npc->GetActualShootTrajectory( vecShootOrigin ); CSoundEnt::InsertSound( SOUND_COMBAT|SOUND_CONTEXT_GUNFIRE, pOperator->GetAbsOrigin(), SOUNDENT_VOLUME_PISTOL, 0.2, pOperator, SOUNDENT_CHANNEL_WEAPON, pOperator->GetEnemy() ); WeaponSound( SINGLE_NPC ); pOperator->FireBullets( 1, vecShootOrigin, vecShootDir, VECTOR_CONE_PRECALCULATED, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 2 ); pOperator->DoMuzzleFlash(); m_iClip1 = m_iClip1 - 1; } break; default: BaseClass::Operator_HandleAnimEvent( pEvent, pOperator ); break; } } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::DryFire( void ) { WeaponSound( EMPTY ); SendWeaponAnim( ACT_VM_DRYFIRE ); m_flSoonestPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_DRY_REFIRE_TIME; m_flNextPrimaryAttack = gpGlobals->curtime + SequenceDuration(); } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::PrimaryAttack( void ) { if ( ( gpGlobals->curtime - m_flLastAttackTime ) > 0.5f ) { m_nNumShotsFired = 0; } else { m_nNumShotsFired++; } m_flLastAttackTime = gpGlobals->curtime; m_flSoonestPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_REFIRE_TIME; CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), SOUNDENT_VOLUME_PISTOL, 0.2, GetOwner() ); CBasePlayer *pOwner = ToBasePlayer( GetOwner() ); if( pOwner ) { // Each time the player fires the pistol, reset the view punch. This prevents // the aim from 'drifting off' when the player fires very quickly. This may // not be the ideal way to achieve this, but it's cheap and it works, which is // great for a feature we're evaluating. (sjb) pOwner->ViewPunchReset(); } BaseClass::PrimaryAttack(); // Add an accuracy penalty which can move past our maximum penalty time if we're really spastic m_flAccuracyPenalty += PISTOL_ACCURACY_SHOT_PENALTY_TIME; m_iPrimaryAttacks++; gamestats->Event_WeaponFired( pOwner, true, GetClassname() ); } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::UpdatePenaltyTime( void ) { CBasePlayer *pOwner = ToBasePlayer( GetOwner() ); if ( pOwner == NULL ) return; // Check our penalty time decay if ( ( ( pOwner->m_nButtons & IN_ATTACK ) == false ) && ( m_flSoonestPrimaryAttack < gpGlobals->curtime ) ) { m_flAccuracyPenalty -= gpGlobals->frametime; m_flAccuracyPenalty = clamp( m_flAccuracyPenalty, 0.0f, PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME ); } } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::ItemPreFrame( void ) { UpdatePenaltyTime(); BaseClass::ItemPreFrame(); } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::ItemBusyFrame( void ) { UpdatePenaltyTime(); BaseClass::ItemBusyFrame(); } //----------------------------------------------------------------------------- // Purpose: Allows firing as fast as button is pressed //----------------------------------------------------------------------------- void CWeaponPistol::ItemPostFrame( void ) { BaseClass::ItemPostFrame(); if ( m_bInReload ) return; CBasePlayer *pOwner = ToBasePlayer( GetOwner() ); if ( pOwner == NULL ) return; //Allow a refire as fast as the player can click if ( ( ( pOwner->m_nButtons & IN_ATTACK ) == false ) && ( m_flSoonestPrimaryAttack < gpGlobals->curtime ) ) { m_flNextPrimaryAttack = gpGlobals->curtime - 0.1f; } else if ( ( pOwner->m_nButtons & IN_ATTACK ) && ( m_flNextPrimaryAttack < gpGlobals->curtime ) && ( m_iClip1 <= 0 ) ) { DryFire(); } } //----------------------------------------------------------------------------- // Purpose: // Output : int //----------------------------------------------------------------------------- Activity CWeaponPistol::GetPrimaryAttackActivity( void ) { if ( m_nNumShotsFired < 1 ) return ACT_VM_PRIMARYATTACK; if ( m_nNumShotsFired < 2 ) return ACT_VM_RECOIL1; if ( m_nNumShotsFired < 3 ) return ACT_VM_RECOIL2; return ACT_VM_RECOIL3; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- bool CWeaponPistol::Reload( void ) { bool fRet = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD ); if ( fRet ) { WeaponSound( RELOAD ); m_flAccuracyPenalty = 0.0f; } return fRet; } //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CWeaponPistol::AddViewKick( void ) { CBasePlayer *pPlayer = ToBasePlayer( GetOwner() ); if ( pPlayer == NULL ) return; QAngle viewPunch; viewPunch.x = random->RandomFloat( 0.25f, 0.5f ); viewPunch.y = random->RandomFloat( -.6f, .6f ); viewPunch.z = 0.0f; //Add it to the view punch pPlayer->ViewPunch( viewPunch ); } I also don't know how it will affect the weapon in the mod. PLEASE HELP!

Posts

  • 5y
    matuka avatar
    matuka avatar Offline
    Member Joined 9y
    1,902 points Ranked 2472nd
    13 medals 2 rare
    • 25 posts awarded Exemplary Feedback Medal icon
    • 6 years a member Medal icon
    • Reached 1,000 Points Medal icon
    • 1 post awarded Exemplary Feedback Medal icon
    • 5 posts awarded Exemplary Feedback Medal icon
    • 10 posts awarded Exemplary Feedback Medal icon
    Can you put ALL of that inside of the code tag please? You've generated a giant mess of meaninglessness. That being said, what programming background do you have? Languages, years of experience and tools of choice would help.
  • 5y
    Darththork99 avatar
    Member Joined 5y
    1,347 points Ranked 3465th
    > **Posted by Matuka**

    > Can you put ALL of that inside of the code tag please? You've generated a giant mess of meaninglessness. That being said, what programming background do you have? Languages, years of experience and tools of choice would help.

    Well. I have not used Gamebanana for very long. So Idk how to put it all into a code tag. It all just went insane randomly. Also. I do not have THAT much experience with programming. I really only have ever used the source engine.
    When will the tyranny end?
  • 5y
    matuka avatar
    matuka avatar Offline
    Member Joined 9y
    1,902 points Ranked 2472nd
    13 medals 2 rare
    • 25 posts awarded Exemplary Feedback Medal icon
    • 6 years a member Medal icon
    • Reached 1,000 Points Medal icon
    • 1 post awarded Exemplary Feedback Medal icon
    • 5 posts awarded Exemplary Feedback Medal icon
    • 10 posts awarded Exemplary Feedback Medal icon
    > **Posted by Darththork99**
    >
    > Well. I have not used Gamebanana for very long. So Idk how to put it all into a code tag. It all just went insane randomly. Also. I do not have THAT much experience with programming. I really only have ever used the source engine.

    Go to edit your post, last icon at the end of the tools is the "code" tool. Not sure how it works in multiple lines, but it should indent everything. Notice that some stuff is already in code tags, thats because the text file that you've posted already had indentation on certain parts. Following that, care to elaborate on "That much experience with programming"? What do you mean by this exactly? Refer to my previous questions.
  • 5y
    Devieus avatar
    Devieus avatar ONLINE
    Moderator Joined 7y
    80,066 points Ranked 48th
    63 medals 4 legendary 10 rare
    • 1st Place - Tutorial Contest Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2015 Medal icon
    • Returned 5000 times Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2016 Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Twelve Winner! Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Sixteen Winner! Medal icon
    There's a difference between a weapon script (CTX) and a weapon code (CPP); one exists to make modding easier, the other is the meat of the gun, what goes on under the hood.
    Sentinel of the TV remote avatar
    Mantra
    Sentinel of the TV remote
  • 5y
    Darththork99 avatar
    Member Joined 5y
    1,347 points Ranked 3465th
    > **Posted by Devieus**

    > There's a difference between a weapon script (CTX) and a weapon code (CPP); one exists to make modding easier, the other is the meat of the gun, what goes on under the hood.

    I'm looking to edit the CPP to add a function to the 357. To make it zoom like the crossbow. I've checked the valve developers wiki, but I don't understand it.
    When will the tyranny end?
  • 5y
    Devieus avatar
    Devieus avatar ONLINE
    Moderator Joined 7y
    80,066 points Ranked 48th
    63 medals 4 legendary 10 rare
    • 1st Place - Tutorial Contest Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2015 Medal icon
    • Returned 5000 times Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2016 Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Twelve Winner! Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Sixteen Winner! Medal icon
    That's okay, coding takes time to learn, especially since it's not your code; take things slow, you're not going to learn this overnight.
    Sentinel of the TV remote avatar
    Mantra
    Sentinel of the TV remote
  • 5y
    Darththork99 avatar
    Member Joined 5y
    1,347 points Ranked 3465th
    > **Posted by Devieus**

    > That's okay, coding takes time to learn, especially since it's not your code; take things slow, you're not going to learn this overnight.

    Isuppose. But the code for making a .357 zoom is on a page on the wiki. But not the entire thing. Just portions you need. I haven't a clue where to add them.
    When will the tyranny end?
  • 5y
    Devieus avatar
    Devieus avatar ONLINE
    Moderator Joined 7y
    80,066 points Ranked 48th
    63 medals 4 legendary 10 rare
    • 1st Place - Tutorial Contest Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2015 Medal icon
    • Returned 5000 times Medal icon
    • 15+ Entries! GameBanana’s Christmas Giveaway 2016 Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Twelve Winner! Medal icon
    • GameBanana’s Christmas Giveaway 2015 Day Sixteen Winner! Medal icon
    If you're referrin to this, read everything that isn't code carefully; you clearly lack any programming experience, so I'll just leave you with how functions work: you can pretty much just put that anywhere that seems logical.
    Sentinel of the TV remote avatar
    Mantra
    Sentinel of the TV remote
  • 5y
    Darththork99 avatar
    Member Joined 5y
    1,347 points Ranked 3465th
    > **Posted by Devieus**

    > If you're referrin to this, read everything that isn't code carefully; you clearly lack any programming experience, so I'll just leave you with how functions work: you can pretty much just put that anywhere that seems logical.

    Okay. I am kind of catching on. (A little) I just added in the codes to a place I deemed logical. But exactly how to I import it into the mod folder for testing?
    When will the tyranny end?

Share

Shareable Image:
Share banner
HTML embed code:
BB embed code:
Markdown embed code:

More embeddable images

Bookmark and Share

Submitter

Darththork99 avatar
Member Joined 5y
1,347 points Ranked 3465th

Darththork99
Send a Private Message to the submitter

Category

Attributes

Stats

Posts
9
Views
3,420
Date Added
5y
Date Modified
5y
bcp.crwdcntrl.net tracking pixel