Importing CSS Weapons Into HL2

From Valve Developer Community
Jump to: navigation, search
English (en)русский (ru)
... Icon-Important.png
Icon-broom.png
This article or section needs to be cleaned up to conform to a higher standard of quality.
For help, see the VDC Editing Help and Wikipedia cleanup process. Also, remember to check for any notes left by the tagger at this article's talk page.
Icon-broom.png
This article or section should be converted to third person to conform to wiki standards.


Tip.pngTip:The source code for a Mapbase Mapbase mod that achieves this is also available.

Update 9th Jan 14: Okay so I wrote this original monstrosity nearly 9 years ago (holy hell) and it was very much out of date and untidy, I've gone through and removed anything that is no longer useful and had a general tidy up. This now conforms with the latest 2013 SDK.

Requirements

Model Editing

Note.pngNote:If your mod is based on the 2013 SDK, CSS model models will work out of the box without any model editing, just a little bit of coding is required to remove/fix the things in the other sections.

Fine-Tuning the Model

Once you have both your v_ and w_ models set up you will need to assign the models to a weapon using its weapon script file. Locate your new weapons script file (e.g. 🖿weapon_357.txt) inside your mod's 🖿scripts/ folder.

Now to fix the issue of CSS weapons only displaying on the left side of the screen we can do the following.

Inside the weapon script file, add these two lines:

"BuiltRightHanded" "0"
"AllowFlipping"    "1"

This will send information to the Client regarding the cl_righthand command, which we will enable next.

In 🖿c_baseviewmodel.cpp, at around line 30, find the line that looks like this and simply comment out the #ifdef block like so:

//#ifdef CSTRIKE_DLL 
ConVar cl_righthand( "cl_righthand", "1", FCVAR_ARCHIVE, "Use right-handed view models." ); 
//#endif

Then, go to line 173 and comment out these lines as follows:

//#ifdef CSTRIKE_DLL
	// If cl_righthand is set, then we want them all right-handed.
	CBaseCombatWeapon *pWeapon = m_hWeapon.Get();
	if ( pWeapon )
	{
		const FileWeaponInfo_t *pInfo = &pWeapon->GetWpnData();
		return pInfo->m_bAllowFlipping && pInfo->m_bBuiltRightHanded != cl_righthand.GetBool();
	}
//#endif

This will enable the cl_righthand ConVar to be activated and your weapon model will not display on the wrong side of the screen. Now just save and compile.

Particle Muzzle Flashes

If you aren't using particle muzzle flashes (which you really should be, they're very pretty!) you may have some issues with the old-style muzzle flashes when using CSS weapons. In that case, you can use the fix below.

In 🖿c_baseanimating.cpp, inside the FireObsoleteEvent( ... ) function at around line 4197 (after the big switch statement), comment out the following marked code and insert this new piece of code (also marked) like so:

			if ( iAttachment != -1 && m_Attachments.Count() > iAttachment )
			{
				/*
				GetAttachment( iAttachment+1, attachOrigin, attachAngles );
				int entId = render->GetViewEntity();
				ClientEntityHandle_t hEntity = ClientEntityList().EntIndexToHandle( entId );
				tempents->MuzzleFlash( attachOrigin, attachAngles, atoi( options ), hEntity, bFirstPerson );
				*/

				if ( input->CAM_IsThirdPerson() )
				{
					C_BaseCombatWeapon *pWeapon = GetActiveWeapon();
					pWeapon->GetAttachment( iAttachment+1, attachOrigin, attachAngles );
				}
				else
				{
					C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
					CBaseViewModel *vm = pPlayer->GetViewModel();
					vm->GetAttachment( iAttachment+1, attachOrigin, attachAngles );
					engine->GetViewAngles( attachAngles );
				}
				g_pEffects->MuzzleFlash( attachOrigin, attachAngles, 1.0, MUZZLEFLASH_TYPE_DEFAULT );
			}

Go to 🖿fx.cpp, under the statement pParticle->m_vecVelocity.Init(); in FX_MuzzleEffect, place the following code:

		C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
		Vector velocity = pPlayer->GetLocalVelocity();
		pParticle->m_vecVelocity += velocity;

Recoil Animation Fix

Ever seen the CSS recoil animation choking up while shooting at a high fire rate? This is how to fix that.

Open up the weapon(s) that use a CSS viewmodel (e.g. 🖿weapon_pistol.cpp). Then scroll down to the GetPrimaryAttackActivity( void ) function and replace everything inside of it with the following:

return ACT_VM_PRIMARYATTACK;

Shell Casing Ejection

You'll probably notice that your newly imported CSS weapons don't eject any shell casings like your stock HL2 weapons, so let's fix that.

In your Client project 🖿src/game/client/ add a new file called 🖿fx_cs_weaponfx.cpp and paste the following into it:

//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Game-specific impact effect hooks
//
//=============================================================================//

#include "cbase.h"
#include "fx_impact.h"
#include "tempent.h"
#include "c_te_effect_dispatch.h"
#include "c_te_legacytempents.h"

//-----------------------------------------------------------------------------
// Purpose: Handle weapon effect callbacks
//-----------------------------------------------------------------------------
void CStrike_EjectBrass( int shell, const CEffectData &data )
{
	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();

	if ( !pPlayer )
		return;

	tempents->CSEjectBrass( data.m_vOrigin, data.m_vAngles, data.m_fFlags, shell, pPlayer );
}

void CStrike_FX_EjectBrass_9mm_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_9MM, data );
}

void CStrike_FX_EjectBrass_57_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_57, data );
}

void CStrike_FX_EjectBrass_12Gauge_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_12GAUGE, data );
}

void CStrike_FX_EjectBrass_556_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_556, data );
}

void CStrike_FX_EjectBrass_762Nato_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_762NATO, data );
}

void CStrike_FX_EjectBrass_338Mag_Callback( const CEffectData &data )
{
	CStrike_EjectBrass( CS_SHELL_338MAG, data );
}

DECLARE_CLIENT_EFFECT( "EjectBrass_9mm",		CStrike_FX_EjectBrass_9mm_Callback );
DECLARE_CLIENT_EFFECT( "EjectBrass_12Gauge",	CStrike_FX_EjectBrass_12Gauge_Callback );
DECLARE_CLIENT_EFFECT( "EjectBrass_57",			CStrike_FX_EjectBrass_57_Callback );
DECLARE_CLIENT_EFFECT( "EjectBrass_556",		CStrike_FX_EjectBrass_556_Callback );
DECLARE_CLIENT_EFFECT( "EjectBrass_762Nato",	CStrike_FX_EjectBrass_762Nato_Callback );
DECLARE_CLIENT_EFFECT( "EjectBrass_338Mag",		CStrike_FX_EjectBrass_338Mag_Callback );

Now head over to c_te_legacytempents.h and you'll find two instances of this block #if defined( CSTRIKE_DLL ) || defined( SDK_DLL ), and all you have to do is comment them out like so:

//#if defined( CSTRIKE_DLL ) || defined( SDK_DLL )
enum
{
	CS_SHELL_9MM = 0,
	CS_SHELL_57,
	CS_SHELL_12GAUGE,
	CS_SHELL_556,
	CS_SHELL_762NATO,
	CS_SHELL_338MAG
};
//#endif

...

//#if defined( CSTRIKE_DLL ) || defined ( SDK_DLL )
	struct model_t			*m_pCS_9MMShell;
	struct model_t			*m_pCS_57Shell;
	struct model_t			*m_pCS_12GaugeShell;
	struct model_t			*m_pCS_556Shell;
	struct model_t			*m_pCS_762NATOShell;
	struct model_t			*m_pCS_338MAGShell;
//#endif

And finally, head over to c_te_legacytempents.cpp where we'll comment out five more #ifdef blocks.

The first block will be in the C_LocalTempEntity constructor:

//#if defined( CSTRIKE_DLL ) || defined (SDK_DLL )

#define TE_RIFLE_SHELL 1024
#define TE_PISTOL_SHELL 2048
#define TE_SHOTGUN_SHELL 4096

//#endif

The second one will be in the PlaySound ( C_LocalTempEntity *pTemp, float damp ) function:

//#ifdef CSTRIKE_DLL

		case TE_PISTOL_SHELL:
		{
			soundname = "Bounce.PistolShell";
		}
		break;

		case TE_RIFLE_SHELL:
		{
			soundname = "Bounce.RifleShell";
		}
		break;

		case TE_SHOTGUN_SHELL:
		{
			soundname = "Bounce.ShotgunShell";
		}
		break;
//#endif

The third one will be in the LevelInit() function:

//#if defined( CSTRIKE_DLL ) || defined ( SDK_DLL )
	m_pCS_9MMShell		= (model_t *)engine->LoadModel( "models/Shells/shell_9mm.mdl" );
	m_pCS_57Shell		= (model_t *)engine->LoadModel( "models/Shells/shell_57.mdl" );
	m_pCS_12GaugeShell	= (model_t *)engine->LoadModel( "models/Shells/shell_12gauge.mdl" );
	m_pCS_556Shell		= (model_t *)engine->LoadModel( "models/Shells/shell_556.mdl" );
	m_pCS_762NATOShell	= (model_t *)engine->LoadModel( "models/Shells/shell_762nato.mdl" );
	m_pCS_338MAGShell	= (model_t *)engine->LoadModel( "models/Shells/shell_338mag.mdl" );
//#endif

The fourth one will be in the Init (void) function, right below the last one:

//#if defined( CSTRIKE_DLL ) || defined ( SDK_DLL )
	m_pCS_9MMShell		= NULL;
	m_pCS_57Shell		= NULL;
	m_pCS_12GaugeShell	= NULL;
	m_pCS_556Shell		= NULL;
	m_pCS_762NATOShell	= NULL;
	m_pCS_338MAGShell	= NULL;
//#endif

And the last one will be in the CSEjectBrass( ... ) function, at the bottom of the file:

//#if defined ( CSTRIKE_DLL ) || defined ( SDK_DLL )

	switch( shellType )
	{
	default:
	case CS_SHELL_9MM:
		hitsound = TE_PISTOL_SHELL;
		pModel = m_pCS_9MMShell;
		break;
	case CS_SHELL_57:
		hitsound = TE_PISTOL_SHELL;
		pModel = m_pCS_57Shell;
		break;
	case CS_SHELL_12GAUGE:
		hitsound = TE_SHOTGUN_SHELL;
		pModel = m_pCS_12GaugeShell;
		break;
	case CS_SHELL_556:
		hitsound = TE_RIFLE_SHELL;
		pModel = m_pCS_556Shell;
		break;
	case CS_SHELL_762NATO:
		hitsound = TE_RIFLE_SHELL;
		pModel = m_pCS_762NATOShell;
		break;
	case CS_SHELL_338MAG:
		hitsound = TE_RIFLE_SHELL;
		pModel = m_pCS_338MAGShell;
		break;
	}
//#endif

Conclusion

And that's it for now, enjoy!