Changing default spawn weapons

From Valve Developer Community
Jump to navigation Jump to search

This is a simple programming tutorial for Source Engine SDKbase 2013 Multiplayer. In this tutorial i will show you how to change the weapons that all players will spawn into a map with by default. This segment will effect both deathmatch and team deathmatch spawning defaults but user referencing is limited to all the precached models at hand in the code meaning a user can be forced to spawn with any of the default listed weapons found in half life 2 deathmatch.

-To start we will open the source engine solution in your main folder setup, mine is in C:\anh\src\game.sln using Visual Studio 2022. -Now with the solution open and running; into the folder of the game.sln go to /game/server/hl2mp/hl2mp_player.cpp and open this in your studio accompanying the game.sln.

Around line 135 at the top of the .cpp youll find void CHL2MP_Player::Precache( void ) this is the beginning process of caching the player spawning.

void CHL2MP_Player::Precache( void )
{
	BaseClass::Precache();

	PrecacheModel ( "sprites/glow01.vmt" );

	//Precache Citizen models
	int nHeads = ARRAYSIZE( g_ppszRandomCitizenModels );
	int i;	

	for ( i = 0; i < nHeads; ++i )
	   	 PrecacheModel( g_ppszRandomCitizenModels[i] );

	//Precache Combine Models
	nHeads = ARRAYSIZE( g_ppszRandomCombineModels );

	for ( i = 0; i < nHeads; ++i )
	   	 PrecacheModel( g_ppszRandomCombineModels[i] );

	PrecacheFootStepSounds();

	PrecacheScriptSound( "NPC_MetroPolice.Die" );
	PrecacheScriptSound( "NPC_CombineS.Die" );
	PrecacheScriptSound( "NPC_Citizen.die" );
}

below this chunk of code you should see another chunk segment around line 161 void CHL2MP_Player::GiveAllItems( void ) This chunk of code is whats listing ALL of the assets within the engine that can be possibly touched, for us for now this is not to be touched

 void CHL2MP_Player::GiveAllItems( void )
{
	EquipSuit();

	CBasePlayer::GiveAmmo( 255,	"Pistol");
	CBasePlayer::GiveAmmo( 255,	"AR2" );
	CBasePlayer::GiveAmmo( 5,	"AR2AltFire" );
	CBasePlayer::GiveAmmo( 255,	"SMG1");
	CBasePlayer::GiveAmmo( 1,	"smg1_grenade");
	CBasePlayer::GiveAmmo( 255,	"Buckshot");
	CBasePlayer::GiveAmmo( 32,	"357" );
	CBasePlayer::GiveAmmo( 3,	"rpg_round");

	CBasePlayer::GiveAmmo( 1,	"grenade" );
	CBasePlayer::GiveAmmo( 2,	"slam" );

	GiveNamedItem( "weapon_crowbar" );
	GiveNamedItem( "weapon_stunstick" );
	GiveNamedItem( "weapon_pistol" );
	GiveNamedItem( "weapon_357" );

	GiveNamedItem( "weapon_smg1" );
	GiveNamedItem( "weapon_ar2" );
	
	GiveNamedItem( "weapon_shotgun" );
	GiveNamedItem( "weapon_frag" );
	
	GiveNamedItem( "weapon_crossbow" );
	
	GiveNamedItem( "weapon_rpg" );

	GiveNamedItem( "weapon_slam" );

	GiveNamedItem( "weapon_physcannon" );
	
}

but then here below this chunk what we will be tampering with on line 198 void CHL2MP_Player::GiveDefaultItems( void ) This chunk of code segment is whats loading into the model at the end of the strings to finally spawn the player. here we will manipulate the listings to give us the results wed actually want.

void CHL2MP_Player::GiveDefaultItems( void )
{
	//EquipSuit();//weapon spawn hack - drN0 <-- HERE I REMOVE THE SUIT

	//CBasePlayer::GiveAmmo( 255,	"Pistol");//weapon spawn hack - drN0 <-- HERE I REMOVE ALL AMMOS SPAWNED WITH THE GUNS BELOW
	//CBasePlayer::GiveAmmo( 45,	"SMG1");//weapon spawn hack - drN0 <-- HERE I REMOVE ALL AMMOS SPAWNED WITH
	//CBasePlayer::GiveAmmo( 1,	"grenade" );//weapon spawn hack - drN0 <-- HERE I REMOVE ALL AMMOS SPAWNED WITH
	//CBasePlayer::GiveAmmo( 6,	"Buckshot");//weapon spawn hack - drN0 <-- HERE I REMOVE ALL AMMOS SPAWNED WITH
	//CBasePlayer::GiveAmmo( 6,	"357" );//weapon spawn hack - drN0 <-- HERE I REMOVE ALL AMMOS SPAWNED WITH

	if ( GetPlayerModelType() == PLAYER_SOUNDS_METROPOLICE || GetPlayerModelType() == PLAYER_SOUNDS_COMBINESOLDIER )
	{
		GiveNamedItem( "weapon_physcannon" );//weapon STUNSTICK//weapon spawn hack - drN0 <-- HERE I CHOOSE TEAM COMBINE
	}
	else if ( GetPlayerModelType() == PLAYER_SOUNDS_CITIZEN )
	{
		GiveNamedItem( "weapon_physcannon" );//weapon CROWBAR//weapon spawn hack - drN0 <-- HERE I CHOOSE TEAM REBEL
	}
	
	//GiveNamedItem( "weapon_pistol" );//weapon spawn hack - drN0 <-- HERE I REMOVE THE GUNS TO GO WITH THE AMMO ABOVE
	//GiveNamedItem( "weapon_smg1" );//weapon spawn hack - drN0 <-- HERE I REMOVE THE GUNS
	//GiveNamedItem( "weapon_frag" );//weapon spawn hack - drN0 <-- HERE I REMOVE THE GUNS
	GiveNamedItem( "weapon_physcannon" ); <-- HERE I LEAVE ONLY THE PHYSCANNON

	const char *szDefaultWeaponName = engine->GetClientConVarValue( engine->IndexOfEdict( edict() ), "cl_defaultweapon" ); <--NOTES

	CBaseCombatWeapon *pDefaultWeapon = Weapon_OwnsThisType( szDefaultWeaponName );

	if ( pDefaultWeapon )
	{
		Weapon_Switch( pDefaultWeapon ); <-- this will end up being cl_defaultweapon listed in config.cfg
	}
	else
	{
		Weapon_Switch( Weapon_OwnsThisType( "weapon_physcannon" ) ); <-- THIS IS THE PRIMARY DEFAULT WEAPON *ground zero*
	}
}

Once these codes are altered as so. rebuild your entire solution now under release mode. go to src/game/server and src/game/client and copy and paste both folders client.dll and server.dll into your mods bin/ folder. upon restarting steam entirely your sourcemods sdk 2013 multiplayer mod should now be forcing players to spawn with the weapons youve chosen.

NOTES: Noticing that cl_defaultweapon is encoded here. You may need to back up this default structure with the config.cfg in your config/ folder of your mods sourcemods/ directory. under binds as cl_defaultweapon "weapon_pistol" or any single weapon thats spawned with other than the physcannon. *this is speculation now because after initially altering codes and adding cl_defaultweapon "weapon_physcannon" to config.cfg and launching the game, after stopping the game and rechecking the config.cfg is seems the cl_defaultweapon "weapon_physcannon" is gone, when originally i had included cl_defaultweapon "weapon_pistol". This means that if as a default every user on either team or non team is spawning with the physcannon then being the physcannon a primary default it has no need for a default weapon. if your default weapon is NOT only a physcannon then you would want to apply the default weapon to your choice either spawn with the weapon 1 or weapon 2. pistol and shotty. crowbar and grenades etc... then you would want to add the cl_defaultweapon "weapon_choice" to your config.cfg as the engine identifies precaching multiple models into a player spawn at some point.

- This was implemented in A Nights Haunting: Source -- feel free to contact me if needed