Skip to content

Commit

Permalink
Add weapon autocompletion to "give" command
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyMehis committed Aug 9, 2024
1 parent 5b813b8 commit 603a8b9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 18 deletions.
2 changes: 2 additions & 0 deletions mp/src/game/server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ CON_COMMAND( say_team, "Display player message to team" )

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifndef ZMR
CON_COMMAND( give, "Give item to player.\n\tArguments: <item_name>" )
{
CBasePlayer *pPlayer = ToBasePlayer( UTIL_GetCommandClient() );
Expand Down Expand Up @@ -894,6 +895,7 @@ CON_COMMAND( give, "Give item to player.\n\tArguments: <item_name>" )
pPlayer->GiveNamedItem( STRING(iszItem) );
}
}
#endif


//------------------------------------------------------------------------------
Expand Down
80 changes: 62 additions & 18 deletions mp/src/game/server/zmr/zmr_concommands_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "zmr_player.h"
#include "zmr_gamerules.h"
#include "weapons/zmr_weaponconfig.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
Expand Down Expand Up @@ -293,25 +294,13 @@ void ZM_GibZombies( const CCommand &args )

static ConCommand zm_gibzombies( "zm_gibzombies", ZM_GibZombies );

/*
Create zombie at crosshair.
*/
static int zm_zombie_create_completion( const char* partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
{
// Autocomplete zombie classnames.
const char* completions[] = {
"npc_zombie",
"npc_fastzombie",
"npc_poisonzombie",
"npc_dragzombie",
"npc_burnzombie"
};


static int AutoCompletion( const char* partial, const char** completions, int numCompletions, char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH] )
{
char cmd[128];
Q_strncpy( cmd, partial, sizeof( cmd ) );


// Skip the command part to compare the argument.
auto* pszArg = Q_strstr( cmd, " " );
if ( pszArg )
Expand All @@ -326,7 +315,7 @@ static int zm_zombie_create_completion( const char* partial, char commands[ COMM


int cmds = 0;
for ( int i = 0; i < ARRAYSIZE( completions ); i++ )
for ( int i = 0; i < numCompletions; i++ )
{
if ( !pszArg || Q_strstr( completions[i], pszArg ) == completions[i] )
{
Expand All @@ -337,6 +326,25 @@ static int zm_zombie_create_completion( const char* partial, char commands[ COMM
return cmds;
}


/*
Create zombie at crosshair.
*/
static int ZM_Zombie_Create_Completion( const char* partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
{
// Autocomplete zombie classnames.
const char* completions[] = {
"npc_zombie",
"npc_fastzombie",
"npc_poisonzombie",
"npc_dragzombie",
"npc_burnzombie"
};


return AutoCompletion( partial, completions, ARRAYSIZE( completions ), commands );
}

static void ZM_Zombie_Create( const CCommand& args )
{
auto* pPlayer = ToZMPlayer( UTIL_GetCommandClient() );
Expand Down Expand Up @@ -390,5 +398,41 @@ static void ZM_Zombie_Create( const CCommand& args )

#define ZOMBIECREATE_DESC "Creates a zombie at your crosshair. Takes a zombie classname."

static ConCommand zm_zombie_create( "zm_zombie_create", ZM_Zombie_Create, ZOMBIECREATE_DESC, 0, zm_zombie_create_completion );
static ConCommand npc_create( "npc_create", ZM_Zombie_Create, ZOMBIECREATE_DESC, 0, zm_zombie_create_completion );
static ConCommand zm_zombie_create( "zm_zombie_create", ZM_Zombie_Create, ZOMBIECREATE_DESC, 0, ZM_Zombie_Create_Completion );
static ConCommand npc_create( "npc_create", ZM_Zombie_Create, ZOMBIECREATE_DESC, 0, ZM_Zombie_Create_Completion );


/*
Give weapon
*/
static int ZM_Give_Completion( const char* partial, char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH] )
{
// Autocomplete weapon classnames.
const char* weapons[32];
int numWeapons = ZMWeaponConfig::GetWeaponConfigSystem()->GetWeaponClassnames( weapons, ARRAYSIZE( weapons ) );

return AutoCompletion( partial, weapons, numWeapons, commands );
}

static void ZM_Give( const CCommand& args )
{
auto* pPlayer = ToZMPlayer( UTIL_GetCommandClient() );
if ( !pPlayer || args.ArgC() != 2 )
{
return;
}

char weapon[32];
Q_strncpy( weapon, args[1], sizeof( weapon ) );
Q_strlower( weapon );

if ( Q_stricmp( weapon, "weapon_" ) != 0 )
{
return;
}


pPlayer->GiveNamedItem( weapon );
}

static ConCommand give( "give", ZM_Give, "Give weapon to player.\n\tArguments: <weapon_zm_name>", FCVAR_CHEAT, ZM_Give_Completion );
17 changes: 17 additions & 0 deletions mp/src/game/shared/zmr/weapons/zmr_weaponconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,20 @@ CZMBaseWeaponConfig* CZMWeaponConfigSystem::LoadCustomConfigFromFile( WeaponConf

return pConfig;
}

int CZMWeaponConfigSystem::GetWeaponClassnames( const char** pszWeaponNames, int nMaxWeapons )
{
if ( nMaxWeapons < 1 )
{
return 0;
}


int n = Min( nMaxWeapons, (int) ARRAYSIZE( m_ConfigRegisters ) );
for ( int i = 0; i < n; i++ )
{
pszWeaponNames[i] = m_ConfigRegisters[i].pszWeaponName;
}

return n;
}
2 changes: 2 additions & 0 deletions mp/src/game/shared/zmr/weapons/zmr_weaponconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ namespace ZMWeaponConfig

acttable_t* GetActivityList( const char* szAnimName, int& nActivityCount ) const;

int GetWeaponClassnames( const char** pszWeaponNames, int nMaxWeapons );

protected:
void RegisterBaseConfig( WeaponConfigSlot_t slot, CreateWeaponConfigFn fn );

Expand Down

0 comments on commit 603a8b9

Please sign in to comment.