2009-05-06 02:28:09 +02:00
/*
* ============================================================================
*
2009-07-05 08:49:23 +02:00
* Zombie : Reloaded
2009-05-06 02:28:09 +02:00
*
2009-06-12 05:51:26 +02:00
* File : restrict . inc
* Type : Core
* Description : Weapon restriction system .
*
* Copyright ( C ) 2009 Greyscale , Richard Helgeby
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2009-05-06 02:28:09 +02:00
*
* ============================================================================
2009-04-09 18:30:14 +02:00
*/
/**
2009-05-29 08:43:15 +02:00
* Maximum types allowed for a single weapon .
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
#define WEAPONS_RESTRICT_MAX_TYPES 8
2009-04-09 18:30:14 +02:00
/**
2009-05-29 08:43:15 +02:00
* Restrict config data indexes .
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
enum RestrictData
{
RESTRICT_DATA_NAME = 0 ,
}
2009-04-09 18:30:14 +02:00
/**
* Array that stores the " HookID " to be later unhooked on player disconnect .
2009-04-12 08:04:00 +02:00
*/
2009-05-14 02:28:26 +02:00
new g_iCanUseHookID [ MAXPLAYERS + 1 ] = { - 1 , ... };
2009-04-09 18:30:14 +02:00
2009-06-27 03:33:09 +02:00
/**
* Array to block any client from picking up weapons .
*/
new bool : g_bRestrictBlockWeapon [ MAXPLAYERS + 1 ];
2009-05-29 08:43:15 +02:00
/**
* Array to store a list of different weapon groups
*/
new Handle : arrayWeaponTypes = INVALID_HANDLE ;
2009-04-09 18:30:14 +02:00
/**
2009-04-10 01:08:51 +02:00
* Query results returned when ( un ) restricting a weapon .
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
enum RestrictQuery
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
Query_Successful , /** (Un)restrict was successful. */
Query_Stopped , /** (Un)restrict was stopped because action was redundant. */
Query_Locked , /** (Un)restrict was stopped because the weapon is marked "untoggleable." */
Query_Invalid , /** (Un)restrict failed because invalid info was given. */
2009-04-09 18:30:14 +02:00
}
/**
2009-04-10 01:08:51 +02:00
* Initialize data and hook commands .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
RestrictInit ()
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Hook buy command.
2009-04-27 04:00:38 +02:00
RegConsoleCmd ( " buy " , RestrictBuyCommand );
RegConsoleCmd ( " autobuy " , RestrictBuyCommand );
RegConsoleCmd ( " rebuy " , RestrictBuyCommand );
2009-04-09 18:30:14 +02:00
}
2009-05-14 02:28:26 +02:00
/**
2009-05-29 08:43:15 +02:00
* Auto - create a list of types loaded by weapons module .
2009-04-14 04:58:05 +02:00
*/
2009-05-18 06:26:13 +02:00
RestrictLoad ()
2009-04-14 04:58:05 +02:00
{
2009-05-29 08:43:15 +02:00
// If array exists, then destroy it.
if ( arrayWeaponTypes != INVALID_HANDLE )
2009-04-20 02:56:26 +02:00
{
2009-05-29 08:43:15 +02:00
CloseHandle ( arrayWeaponTypes );
2009-04-20 02:56:26 +02:00
}
2009-05-29 08:43:15 +02:00
// Initialize array.
arrayWeaponTypes = CreateArray ( WEAPONS_MAX_LENGTH );
2009-04-09 18:30:14 +02:00
2009-05-29 08:43:15 +02:00
decl String : weapontype [ WEAPONS_MAX_LENGTH ];
new String : weapontypes [ WEAPONS_RESTRICT_MAX_TYPES ][ WEAPONS_MAX_LENGTH ];
2009-04-14 01:29:24 +02:00
2009-06-05 05:58:48 +02:00
// x = Array index.
2009-05-29 08:43:15 +02:00
new size = GetArraySize ( arrayWeapons );
for ( new x = 0 ; x < size ; x ++ )
2009-04-14 01:29:24 +02:00
{
2009-05-29 08:43:15 +02:00
WeaponsGetType ( x , weapontype , sizeof ( weapontype ));
2009-04-14 01:29:24 +02:00
2009-07-24 00:05:14 +02:00
ExplodeString ( weapontype , " , " , weapontypes , sizeof ( weapontypes ), sizeof ( weapontypes []));
2009-05-29 08:43:15 +02:00
for ( new y = 0 ; y < WEAPONS_RESTRICT_MAX_TYPES ; y ++ )
2009-04-14 01:29:24 +02:00
{
2009-05-29 08:43:15 +02:00
// Cut off whitespace.
TrimString ( weapontypes [ y ]);
2009-04-14 04:58:05 +02:00
2009-05-29 08:43:15 +02:00
// If we've reached the end of the weapon's types, then stop.
if ( ! weapontypes [ y ][ 0 ])
2009-04-14 01:29:24 +02:00
{
2009-05-29 08:43:15 +02:00
break ;
2009-04-14 01:29:24 +02:00
}
2009-04-14 04:58:05 +02:00
2009-05-29 08:43:15 +02:00
// If the weapon type isn't in the main array, then push it in.
if ( FindStringInArray ( arrayWeaponTypes , weapontypes [ y ]) == - 1 )
2009-04-12 08:04:00 +02:00
{
2009-05-29 08:43:15 +02:00
// Push weapon type name into weapon type array.
PushArrayString ( arrayWeaponTypes , weapontypes [ y ]);
2009-04-12 08:04:00 +02:00
}
2009-05-29 08:43:15 +02:00
}
2009-04-12 08:04:00 +02:00
}
2009-04-09 18:30:14 +02:00
}
/**
2009-05-29 08:43:15 +02:00
* Hook commands related to restrict here .
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
RestrictOnCommandsCreate ()
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
// Create weapon admin commands.
2009-10-26 23:17:22 +01:00
RegConsoleCmd ( " zr_restrict " , RestrictCommand , " Restricts a weapon or a weapon type. Usage: zr_restrict <weapon|weapon type> [weapon2|weapontype2] ... " );
RegConsoleCmd ( " zr_unrestrict " , UnrestrictCommand , " Unrestricts a weapon or a weapon type. Usage: zr_unrestrict <weapon|weapon type> [weapon2|weapontype2] ... " );
2009-04-09 18:30:14 +02:00
}
/**
2009-04-15 09:42:12 +02:00
* Client is joining the server .
2009-04-09 18:30:14 +02:00
*
2009-04-10 01:08:51 +02:00
* @ param client The client index .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
RestrictClientInit ( client )
2009-04-09 18:30:14 +02:00
{
2009-05-12 03:12:05 +02:00
// Hook "Weapon_CanUse" on client.
2009-11-04 21:37:26 +01:00
#if defined USE_SDKHOOKS
2010-01-25 11:07:39 +01:00
SDKHook ( client , SDKHook_WeaponCanUse , RestrictCanUse );
2009-11-04 21:37:26 +01:00
// Set dummy value so it think it's hooked.
g_iCanUseHookID [ client ] = 1 ;
#else
g_iCanUseHookID [ client ] = ZRTools_HookWeapon_CanUse ( client , RestrictCanUse );
#endif
2009-06-27 03:33:09 +02:00
// Reset block weapons flag.
g_bRestrictBlockWeapon [ client ] = false ;
2009-04-09 18:30:14 +02:00
}
/**
2009-04-10 01:08:51 +02:00
* Unhook Weapon_CanUse function on a client .
2009-04-09 18:30:14 +02:00
*
2009-04-10 01:08:51 +02:00
* @ param client The client index .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
RestrictOnClientDisconnect ( client )
2009-04-09 18:30:14 +02:00
{
2009-05-14 02:28:26 +02:00
// Unhook "Weapon_CanUse" callback, and reset variable.
if ( g_iCanUseHookID [ client ] != - 1 )
{
2009-11-04 21:37:26 +01:00
#if defined USE_SDKHOOKS
2009-12-02 18:42:57 +01:00
SDKUnhook ( client , SDKHook_WeaponCanUse , RestrictCanUse );
2009-11-04 21:37:26 +01:00
#else
ZRTools_UnhookWeapon_CanUse ( g_iCanUseHookID [ client ]);
#endif
2009-05-14 02:28:26 +02:00
g_iCanUseHookID [ client ] = - 1 ;
}
2009-04-30 07:36:57 +02:00
}
/**
* Client is spawning into the game .
*
* @ param client The client index .
*/
RestrictOnClientSpawn ( client )
{
// Re-hook "canuse" on client.
2009-11-04 21:37:26 +01:00
#if defined USE_SDKHOOKS
2009-12-02 18:42:57 +01:00
SDKUnhook ( client , SDKHook_WeaponCanUse , RestrictCanUse ); // <--- What happens if it's not already hooked???
SDKHook ( client , SDKHook_WeaponCanUse , RestrictCanUse );
2009-11-04 21:37:26 +01:00
g_iCanUseHookID [ client ] = 1 ;
#else
ZRTools_UnhookWeapon_CanUse ( g_iCanUseHookID [ client ]);
g_iCanUseHookID [ client ] = ZRTools_HookWeapon_CanUse ( client , RestrictCanUse );
#endif
2009-06-27 03:33:09 +02:00
// H4x. Unfortunately I can't disable this flag early enough for CS:S to give start USP/Glock.
// So I have to give BOOTLEG weapons. (Sorry Valve)
if ( g_bRestrictBlockWeapon [ client ])
{
// Reset block weapons flag.
g_bRestrictBlockWeapon [ client ] = false ;
if ( ZRIsClientOnTeam ( client , CS_TEAM_T ))
{
GivePlayerItem ( client , WEAPONS_SPAWN_T_WEAPON );
}
else if ( ZRIsClientOnTeam ( client , CS_TEAM_CT ))
{
GivePlayerItem ( client , WEAPONS_SPAWN_CT_WEAPON );
}
}
}
/**
* The round is ending .
*/
RestrictOnRoundEnd ()
{
2010-01-04 08:25:35 +01:00
new bool : restrictzombieequip = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS_RESTRICT_ENDEQUIP ]);
if ( ! restrictzombieequip )
{
return ;
}
2009-06-27 03:33:09 +02:00
// x = Client index.
for ( new x = 1 ; x <= MaxClients ; x ++ )
{
// If client isn't in-game, then stop.
if ( ! IsClientInGame ( x ))
{
continue ;
}
// If client is a human, then stop.
if ( InfectIsClientHuman ( x ))
{
continue ;
}
// Enable block weapon flag.
g_bRestrictBlockWeapon [ x ] = true ;
}
2009-04-09 18:30:14 +02:00
}
/**
* Command callback function for the " buy " command
2009-04-10 01:08:51 +02:00
* Used to block use of this command under certain conditions .
2009-04-09 18:30:14 +02:00
*
* @ param client The client index .
2009-04-10 01:08:51 +02:00
* @ param argc Argument count .
2009-04-12 08:04:00 +02:00
*/
2009-04-27 04:00:38 +02:00
public Action : RestrictBuyCommand ( client , argc )
2009-04-09 18:30:14 +02:00
{
2010-02-14 17:03:19 +01:00
// If client isn't valid, then stop.
if ( ! ZRIsClientValid ( client ))
{
return Plugin_Continue ;
}
// If the client isn't in a buyzone, then stop.
if ( ! WeaponsIsClientInBuyZone ( client ))
2009-07-21 22:25:11 +02:00
{
return Plugin_Continue ;
}
2009-04-27 04:00:38 +02:00
// If player is a zombie, then block command.
2009-04-24 05:02:19 +02:00
if ( InfectIsClientInfected ( client ))
2009-04-09 18:30:14 +02:00
{
2009-05-14 09:32:01 +02:00
TranslationPrintToChat ( client , " Zombie cant use weapon " );
2009-04-09 18:30:14 +02:00
// Block command
return Plugin_Handled ;
}
2009-07-07 06:32:34 +02:00
decl String : weapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
GetCmdArg ( 1 , weapon , sizeof ( weapon ));
2009-05-29 08:43:15 +02:00
// If the weapon is restricted, then prevent pickup.
2009-08-25 22:07:10 +02:00
decl String : weaponname [ WEAPONS_MAX_LENGTH ];
new weaponindex = WeaponsEntityToDisplay ( weapon , weaponname , sizeof ( weaponname ), true );
2009-05-29 08:43:15 +02:00
// If weapon isn't configged, then allow pickup.
2009-08-25 22:07:10 +02:00
if ( weaponindex == - 1 )
2009-05-29 08:43:15 +02:00
{
2009-07-21 22:25:11 +02:00
// Allow command.
2009-05-29 08:43:15 +02:00
return Plugin_Continue ;
}
// If weapon is restricted, then stop.
2009-08-25 22:07:10 +02:00
if ( RestrictIsWeaponRestricted ( weaponindex ))
2009-04-09 18:30:14 +02:00
{
2009-08-25 22:07:10 +02:00
TranslationPrintToChat ( client , " Weapon is restricted " , weaponname );
2009-04-09 18:30:14 +02:00
2009-04-15 09:42:12 +02:00
// Block command.
2009-04-09 18:30:14 +02:00
return Plugin_Handled ;
}
2009-04-15 09:42:12 +02:00
// Allow command.
2009-04-09 18:30:14 +02:00
return Plugin_Continue ;
}
/**
2009-05-29 08:43:15 +02:00
* Restricts ( or unrestricts ) a given weapon or weapon type .
2009-04-09 18:30:14 +02:00
*
2009-05-29 08:43:15 +02:00
* @ param restrict True to restrict , false to unrestrict .
* @ param target Weapon or weapon type to restrict / unrestrict .
* @ param single True if a single weapon is being restricted , false if weapon type .
* @ param returntarget The proper targetname . ( same as param 'target' if invalid )
* @ param maxlen The maximum length of param 'returntarget.'
* @ return Query result . ( See enum RestrictQuery )
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
RestrictQuery : RestrictWeapon ( bool : restrict , const String : target [], & bool : single = true , String : returntarget [], maxlen )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
// Copy 'target' to 'returntarget' to be possibly changed later.
strcopy ( returntarget , maxlen , target );
// Find index of the given weapon type.
2009-05-29 08:47:04 +02:00
new typeindex = RestrictTypeToIndex ( returntarget );
2009-05-29 08:43:15 +02:00
// Single weapon.
if ( typeindex == - 1 )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
single = true ;
2009-04-13 20:33:13 +02:00
2009-05-29 08:47:04 +02:00
new weaponindex = WeaponsNameToIndex ( returntarget );
2009-04-09 18:30:14 +02:00
2009-05-29 08:43:15 +02:00
// If weapon index is invalid, then return invalid.
if ( weaponindex == - 1 )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
return Query_Invalid ;
2009-04-09 18:30:14 +02:00
}
2009-05-29 08:43:15 +02:00
// Return proper weapon name.
WeaponsGetName ( weaponindex , returntarget , maxlen );
2009-04-14 04:58:05 +02:00
2009-05-29 08:43:15 +02:00
// If weapon is untoggleable, then return locked.
if ( ! WeaponsGetToggleable ( weaponindex ))
2009-05-19 08:14:18 +02:00
{
2009-05-29 08:43:15 +02:00
return Query_Locked ;
2009-05-19 08:14:18 +02:00
}
2009-05-29 08:43:15 +02:00
// If weapon restriction is redundant then return stopped.
if ( RestrictIsWeaponRestricted ( weaponindex ) == restrict )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
return Query_Stopped ;
2009-04-09 18:30:14 +02:00
}
2009-05-29 08:43:15 +02:00
// Set weapon's restricted state.
RestrictSetWeaponRestricted ( weaponindex , false , restrict );
2009-04-14 04:58:05 +02:00
2009-05-29 08:43:15 +02:00
// Successfully restricted weapon.
return Query_Successful ;
2009-04-12 08:04:00 +02:00
}
2009-05-29 08:43:15 +02:00
// Weapon type.
else
2009-04-15 09:42:12 +02:00
{
2009-05-29 08:43:15 +02:00
single = false ;
// Get all weapons in the given type.
new Handle : arrayTypeWeapons ;
new count = RestrictGetTypeWeapons ( typeindex , arrayTypeWeapons );
// Return proper weapon name.
RestrictWeaponTypeGetName ( typeindex , returntarget , maxlen );
// If weapon restriction is redundant then return stopped.
if ( RestrictIsTypeUniform ( restrict , typeindex ))
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
return Query_Stopped ;
2009-04-13 23:55:02 +02:00
}
2009-05-29 08:43:15 +02:00
for ( new x = 0 ; x < count ; x ++ )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
// Get weapon index.
new weaponindex = GetArrayCell ( arrayTypeWeapons , x );
2009-04-13 23:55:02 +02:00
2009-05-29 08:43:15 +02:00
// If weapon is untoggleable, then stop.
if ( ! WeaponsGetToggleable ( weaponindex ))
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
continue ;
2009-04-13 23:55:02 +02:00
}
2009-05-29 08:43:15 +02:00
// Set weapon's restricted state.
RestrictSetWeaponRestricted ( weaponindex , false , restrict );
2009-04-13 23:55:02 +02:00
}
2009-05-29 08:43:15 +02:00
// Successfully restricted weapon type.
return Query_Successful ;
2009-04-13 23:55:02 +02:00
}
}
2009-05-29 08:43:15 +02:00
/** Print weapon ( un ) restriction query result to client ( s ) .
*
* @ param client The client to print response to . ( 0 for all clients )
* @ param query The query result .
* @ param single True if a single weapon is being restricted .
* @ param restrict True if the query was to restrict / unrestrict a weapon .
* @ param target The target to be restricted / unrestricted .
*/
RestrictPrintQueryResponse ( client , RestrictQuery : query , bool : single , bool : restrict , const String : target [])
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
switch ( query )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
// Query was successful.
case Query_Successful :
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
if ( single )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
if ( restrict )
{
TranslationPrintToChatAll ( true , false , " Restrict weapon " , target );
}
else
{
TranslationPrintToChatAll ( true , false , " Unrestrict weapon " , target );
}
2009-04-13 23:55:02 +02:00
}
else
{
2009-05-29 08:43:15 +02:00
if ( restrict )
{
TranslationPrintToChatAll ( true , false , " Restrict weapon type " , target );
}
else
{
TranslationPrintToChatAll ( true , false , " Unrestrict weapon type " , target );
}
2009-04-13 23:55:02 +02:00
}
}
2009-05-29 08:43:15 +02:00
// Query was redundant.
case Query_Stopped :
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
if ( single )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
if ( restrict )
{
TranslationReplyToCommand ( client , " Restrict weapon stopped " , target );
}
else
{
TranslationReplyToCommand ( client , " Unrestrict weapon stopped " , target );
}
2009-04-13 23:55:02 +02:00
}
else
{
2009-05-29 08:43:15 +02:00
if ( restrict )
{
TranslationReplyToCommand ( client , " Restrict weapon type stopped " , target );
}
else
{
TranslationReplyToCommand ( client , " Unrestrict weapon type stopped " , target );
}
2009-04-13 23:55:02 +02:00
}
}
2009-05-29 08:43:15 +02:00
// Weapon is untoggleable.
case Query_Locked :
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
TranslationReplyToCommand ( client , " Restrict weapon untoggleable " , target );
2009-04-09 18:30:14 +02:00
}
2009-05-29 08:43:15 +02:00
// Weapon was invalid.
case Query_Invalid :
2009-04-13 20:33:13 +02:00
{
2009-05-29 08:43:15 +02:00
TranslationReplyToCommand ( client , " Weapon invalid " , target );
2009-04-13 23:55:02 +02:00
}
2009-04-13 20:33:13 +02:00
}
}
2009-05-29 08:43:15 +02:00
stock RestrictTypeToIndex ( const String : type [])
2009-04-13 22:00:37 +02:00
{
2009-05-29 08:43:15 +02:00
decl String : typename [ WEAPONS_MAX_LENGTH ];
2009-04-15 09:42:12 +02:00
2009-05-29 08:43:15 +02:00
// x = Array index.
new size = GetArraySize ( arrayWeaponTypes );
for ( new x = 0 ; x < size ; x ++ )
2009-04-13 22:00:37 +02:00
{
2009-05-29 08:43:15 +02:00
RestrictWeaponTypeGetName ( x , typename , sizeof ( typename ));
2009-04-13 22:00:37 +02:00
2009-05-29 08:43:15 +02:00
// If types match, then return index.
if ( StrEqual ( type , typename , false ))
2009-04-13 22:00:37 +02:00
{
2009-05-29 08:43:15 +02:00
return x ;
2009-04-13 23:55:02 +02:00
}
2009-04-13 22:00:37 +02:00
}
2009-05-29 08:43:15 +02:00
// Type doesn't exist.
return - 1 ;
2009-04-13 22:00:37 +02:00
}
/**
2009-05-29 08:43:15 +02:00
* Gets the name of a weapon type at a given index .
* @ param index The weapon type index .
* @ param weapon The string to return name in .
* @ param maxlen The max length of the string .
2009-04-13 22:00:37 +02:00
*/
2009-05-29 08:43:15 +02:00
stock RestrictWeaponTypeGetName ( index , String : weapontype [], maxlen )
2009-04-13 22:00:37 +02:00
{
2009-05-29 08:43:15 +02:00
// Get weapon type name at given index.
GetArrayString ( arrayWeaponTypes , index , weapontype , maxlen );
2009-04-13 22:00:37 +02:00
}
2009-04-09 18:30:14 +02:00
/**
2009-05-29 08:43:15 +02:00
* Returns an array containing all weapon indexes matching the given type .
2009-04-09 18:30:14 +02:00
*
2009-05-29 08:43:15 +02:00
* @ param index The weapon type index .
* @ param arrayTypeWeapons A handle to store array containing matching weapons .
* Don ' t forget to close this !
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
stock RestrictGetTypeWeapons ( index , & Handle : arrayTypeWeapons )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
// Create array to hold weapons of the given type.
arrayTypeWeapons = CreateArray ();
// Get name of the weapon type at given index.
decl String : typename [ WEAPONS_MAX_LENGTH ];
RestrictWeaponTypeGetName ( index , typename , sizeof ( typename ));
2009-04-14 04:58:05 +02:00
2009-05-29 08:43:15 +02:00
new count ;
decl String : weapontype [ WEAPONS_MAX_LENGTH ];
new String : weapontypes [ WEAPONS_RESTRICT_MAX_TYPES ][ WEAPONS_MAX_LENGTH ];
2009-04-15 09:42:12 +02:00
2009-05-29 08:43:15 +02:00
// x = Array index.
new size = GetArraySize ( arrayWeapons );
2009-04-09 18:30:14 +02:00
for ( new x = 0 ; x < size ; x ++ )
{
2009-05-29 08:43:15 +02:00
WeaponsGetType ( x , weapontype , sizeof ( weapontype ));
2009-04-09 18:30:14 +02:00
2009-07-24 00:05:14 +02:00
ExplodeString ( weapontype , " , " , weapontypes , sizeof ( weapontypes ), sizeof ( weapontypes []));
2009-05-29 08:43:15 +02:00
for ( new y = 0 ; y < WEAPONS_RESTRICT_MAX_TYPES ; y ++ )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
// Cut off whitespace.
TrimString ( weapontypes [ y ]);
// If we've reached the end of the weapon's types, then stop.
if ( ! weapontypes [ y ][ 0 ])
{
break ;
}
// If types match, then add weapon to array.
if ( StrEqual ( typename , weapontypes [ y ], false ))
{
PushArrayCell ( arrayTypeWeapons , x );
count ++ ;
}
2009-04-09 18:30:14 +02:00
}
}
2009-05-29 08:43:15 +02:00
// Return number of weapons of the given type.
return count ;
2009-04-09 18:30:14 +02:00
}
/**
2009-05-29 08:43:15 +02:00
* Gets the restricted status on a weapon .
2009-04-09 18:30:14 +02:00
*
2009-05-29 08:43:15 +02:00
* @ param index The weapon index .
* @ param toggle If true , the value is toggled , otherwise 'restrict' param is used .
* @ param restrict ( Only if 'toggle' is 'false' ) Restricted status of the weapon .
2009-04-12 08:04:00 +02:00
*/
2009-05-29 08:43:15 +02:00
stock RestrictSetWeaponRestricted ( index , bool : toggle , bool : restrict = false )
2009-04-09 18:30:14 +02:00
{
2009-05-29 08:43:15 +02:00
// Get array handle of weapon at given index.
new Handle : arrayWeapon = GetArrayCell ( arrayWeapons , index );
2009-04-09 18:30:14 +02:00
2009-05-29 08:43:15 +02:00
// Set restricted status.
new bool : value = toggle ? ! RestrictIsWeaponRestricted ( index ) : restrict ;
SetArrayCell ( arrayWeapon , _ : WEAPONS_DATA_RESTRICTED , value );
2009-04-09 18:30:14 +02:00
}
2009-04-13 20:33:13 +02:00
/**
2009-05-29 08:43:15 +02:00
* Gets the restricted status on a weapon .
*
* @ param index The weapon index .
* @ return True if weapon is restricted , false if not .
2009-04-13 20:33:13 +02:00
*/
2009-05-29 08:43:15 +02:00
stock bool : RestrictIsWeaponRestricted ( index )
2009-04-13 20:33:13 +02:00
{
2009-05-29 08:43:15 +02:00
// Get array handle of weapon at given index.
new Handle : arrayWeapon = GetArrayCell ( arrayWeapons , index );
2009-04-13 20:33:13 +02:00
2009-05-29 08:43:15 +02:00
// Return restricted status.
return bool : GetArrayCell ( arrayWeapon , _ : WEAPONS_DATA_RESTRICTED );
2009-04-13 20:33:13 +02:00
}
2009-04-13 23:55:02 +02:00
/**
2009-05-29 08:43:15 +02:00
* Used to check if all weapons of a type are restricted .
*
* @ param restricted True to check if all weapons of given type are restricted .
* @ param index The weapon type index .
* @ return True if all weapons of the given type are restricted or not , false if not .
2009-04-13 23:55:02 +02:00
*/
2009-05-29 08:43:15 +02:00
stock bool : RestrictIsTypeUniform ( bool : restricted , index )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
new Handle : arrayTypeWeapons ;
new count = RestrictGetTypeWeapons ( index , arrayTypeWeapons );
2009-04-13 23:55:02 +02:00
2009-05-29 08:43:15 +02:00
// x = array index
for ( new x = 0 ; x < count ; x ++ )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
// Get weapon index to check restricted status of.
new weaponindex = GetArrayCell ( arrayTypeWeapons , x );
2009-04-13 23:55:02 +02:00
2009-05-29 08:43:15 +02:00
// If weapon is toggleable and it's not uniform with the given status, then return false.
if ( WeaponsGetToggleable ( weaponindex ) && RestrictIsWeaponRestricted ( weaponindex ) != restricted )
2009-04-13 23:55:02 +02:00
{
2009-05-29 08:43:15 +02:00
return false ;
2009-04-13 23:55:02 +02:00
}
}
2009-05-29 08:43:15 +02:00
// All weapons are restricted, so return true.
return true ;
2009-04-09 18:30:14 +02:00
}
/**
* Hook callback , called when a player is trying to pick up a weapon .
* @ param client The client index .
* @ param weapon The weapon index .
2009-05-11 04:47:09 +02:00
* @ return Return ZRTools_Handled to stop weapon pickup .
* ZRTools_Continue to allow weapon pickup .
2009-04-12 08:04:00 +02:00
*/
2009-11-04 21:37:26 +01:00
#if defined USE_SDKHOOKS
public Action : RestrictCanUse ( client , weapon )
#else
2009-05-11 04:47:09 +02:00
public ZRTools_Action : RestrictCanUse ( client , weapon )
2009-11-04 21:37:26 +01:00
#endif
2009-04-09 18:30:14 +02:00
{
2009-08-25 22:07:10 +02:00
new String : weaponentity [ WEAPONS_MAX_LENGTH ];
GetEdictClassname ( weapon , weaponentity , sizeof ( weaponentity ));
2009-04-09 18:30:14 +02:00
2009-04-29 01:58:41 +02:00
// If weapon is a knife, then allow pickup.
2009-08-25 22:07:10 +02:00
if ( StrEqual ( weaponentity , " weapon_knife " ))
2009-04-29 01:58:41 +02:00
{
2009-11-04 21:37:26 +01:00
return ACTION_CONTINUE ;
2009-04-29 01:58:41 +02:00
}
2009-06-27 03:33:09 +02:00
// If the player is a zombie, then prevent pickup.
if ( InfectIsClientInfected ( client ))
{
2009-11-04 21:37:26 +01:00
return ACTION_HANDLED ;
2009-06-27 03:33:09 +02:00
}
// If client is flagged for not picking up weapons, then stop.
if ( g_bRestrictBlockWeapon [ client ])
{
2009-11-04 21:37:26 +01:00
return ACTION_HANDLED ;
2009-06-27 03:33:09 +02:00
}
2009-06-08 06:05:50 +02:00
// If weapons module is disabled, then stop.
new bool : weapons = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS ]);
if ( ! weapons )
{
2009-11-04 21:37:26 +01:00
return ACTION_CONTINUE ;
2009-06-08 06:05:50 +02:00
}
// If restrict module is disabled, then stop.
new bool : restrict = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS_RESTRICT ]);
if ( ! restrict )
{
2009-11-04 21:37:26 +01:00
return ACTION_CONTINUE ;
2009-06-08 06:05:50 +02:00
}
2009-04-29 01:58:41 +02:00
// If the weapon is restricted, then prevent pickup.
2009-08-25 22:07:10 +02:00
decl String : weaponname [ WEAPONS_MAX_LENGTH ];
new weaponindex = WeaponsEntityToDisplay ( weaponentity , weaponname , sizeof ( weaponname ));
2009-05-29 08:43:15 +02:00
// If weapon isn't configged, then allow pickup.
2009-08-25 22:07:10 +02:00
if ( weaponindex == - 1 )
2009-05-29 08:43:15 +02:00
{
// Allow pickup.
2009-11-04 21:37:26 +01:00
return ACTION_CONTINUE ;
2009-05-29 08:43:15 +02:00
}
// If weapon is restricted, then stop.
2009-08-25 22:07:10 +02:00
if ( RestrictIsWeaponRestricted ( weaponindex ))
2009-04-09 18:30:14 +02:00
{
2009-11-04 21:37:26 +01:00
return ACTION_HANDLED ;
2009-04-09 18:30:14 +02:00
}
2009-06-21 21:24:24 +02:00
// Forward event to weapons module.
WeaponsOnItemPickup ( client , weapon );
2009-05-12 03:12:05 +02:00
2009-04-29 01:58:41 +02:00
// Allow pickup.
2009-11-04 21:37:26 +01:00
return ACTION_CONTINUE ;
2009-05-14 02:28:26 +02:00
}
2009-05-18 06:26:13 +02:00
/**
* Command callbacks .
*/
2009-05-14 02:28:26 +02:00
/**
* Command callback ( zr_restrict )
* Restricts a weapon or group
*
* @ param client The client index .
* @ param argc Argument count .
*/
2009-05-29 08:43:15 +02:00
public Action : RestrictCommand ( client , argc )
2009-05-14 02:28:26 +02:00
{
2009-10-26 23:17:22 +01:00
// Check if privileged.
if ( ! ZRIsClientPrivileged ( client , OperationType_Configuration ))
{
TranslationReplyToCommand ( client , " No access to command " );
return Plugin_Handled ;
}
2009-05-14 02:28:26 +02:00
// If weapons module is disabled, then stop.
new bool : weapons = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS ]);
if ( ! weapons )
{
// Tell client command is disabled.
2009-05-14 09:32:01 +02:00
TranslationReplyToCommand ( client , " Feature is disabled " );
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
}
// If restrict module is disabled, then stop.
new bool : restrict = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS_RESTRICT ]);
if ( ! restrict )
{
// Tell client command is disabled.
2009-05-14 09:32:01 +02:00
TranslationReplyToCommand ( client , " Feature is disabled " );
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
}
2009-06-08 06:05:50 +02:00
// If not enough arguments given, then stop.
if ( argc < 1 )
{
TranslationReplyToCommand ( client , " Weapons command restrict syntax " );
return Plugin_Handled ;
}
2009-05-29 08:43:15 +02:00
decl String : target [ WEAPONS_MAX_LENGTH ];
2009-05-14 02:28:26 +02:00
2009-05-29 08:43:15 +02:00
new args = GetCmdArgs ();
for ( new x = 1 ; x <= args ; x ++ )
{
// Get target to restrict.
GetCmdArg ( x , target , sizeof ( target ));
// Query restrict on this target, and get a result back.
new bool : single ;
decl String : returntarget [ WEAPONS_MAX_LENGTH ];
new RestrictQuery : query = RestrictWeapon ( true , target , single , returntarget , sizeof ( returntarget ));
// Print response to client(s).
RestrictPrintQueryResponse ( client , query , single , true , returntarget );
}
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
}
/**
* Command callback ( zr_unrestrict )
* Unrestricts a weapon or group
*
* @ param client The client index .
* @ param argc Argument count .
*/
2009-05-29 08:43:15 +02:00
public Action : UnrestrictCommand ( client , argc )
2009-05-14 02:28:26 +02:00
{
2009-10-26 23:17:22 +01:00
// Check if privileged.
if ( ! ZRIsClientPrivileged ( client , OperationType_Configuration ))
{
TranslationReplyToCommand ( client , " No access to command " );
return Plugin_Handled ;
}
2009-05-14 02:28:26 +02:00
// If weapons module is disabled, then stop.
new bool : weapons = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS ]);
if ( ! weapons )
{
// Tell client command is disabled.
2009-05-14 09:32:01 +02:00
TranslationReplyToCommand ( client , " Feature is disabled " );
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
}
// If restrict module is disabled, then stop.
new bool : restrict = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS_RESTRICT ]);
if ( ! restrict )
{
// Tell client command is disabled.
2009-05-14 09:32:01 +02:00
TranslationReplyToCommand ( client , " Feature is disabled " );
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
}
2009-06-08 06:05:50 +02:00
// If not enough arguments given, then stop.
if ( argc < 1 )
{
TranslationReplyToCommand ( client , " Weapons command unrestrict syntax " );
return Plugin_Handled ;
}
2009-05-14 02:28:26 +02:00
// arg1 = weapon being restricted
2009-05-29 08:43:15 +02:00
decl String : target [ WEAPONS_MAX_LENGTH ];
2009-05-14 02:28:26 +02:00
2009-05-29 08:43:15 +02:00
new args = GetCmdArgs ();
for ( new x = 1 ; x <= args ; x ++ )
{
// Get target to restrict.
GetCmdArg ( x , target , sizeof ( target ));
// Query unrestrict on this target, and get a result back.
new bool : single ;
decl String : returntarget [ WEAPONS_MAX_LENGTH ];
new RestrictQuery : query = RestrictWeapon ( false , target , single , returntarget , sizeof ( returntarget ));
// Print response to client(s).
RestrictPrintQueryResponse ( client , query , single , false , returntarget );
}
2009-05-14 02:28:26 +02:00
return Plugin_Handled ;
2009-06-12 15:52:51 +02:00
}