2009-05-06 02:28:09 +02:00
/*
* ============================================================================
*
2009-04-09 18:30:14 +02:00
* Zombie : Reloaded
2009-05-06 02:28:09 +02:00
*
* File : restrict . inc
* Type : Core
* Description : Weapon restriction system .
*
* ============================================================================
2009-04-09 18:30:14 +02:00
*/
/**
2009-04-10 01:08:51 +02:00
* Array to store restricted weapon names .
2009-04-12 08:04:00 +02:00
*/
2009-04-09 18:30:14 +02:00
new Handle : gRestrictedWeapons = INVALID_HANDLE ;
/**
2009-05-01 07:09:18 +02:00
* Keyvalue handle to store weapon groups data .
*
* @ redir config . inc
2009-04-12 08:04:00 +02:00
*/
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-04-13 06:00:58 +02:00
new gCanUseHookID [ MAXPLAYERS + 1 ];
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-04-09 18:30:14 +02:00
enum WpnRestrictQuery
{
2009-04-10 01:08:51 +02:00
Successful_Weapon , /** Weapon (un)restrict query was successful. */
Successful_Group , /** Group (un)restrict query was successful. */
2009-04-12 08:04:00 +02:00
Failed_Weapon , /** Weapon (un)restrict was unsuccessful */
Failed_Group , /** Group (un)restrict was unsuccessful */
2009-04-09 18:30:14 +02:00
Invalid , /** Weapon/Group invalid */
}
/**
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
// Initialize weapon restrict array.
2009-05-08 04:57:21 +02:00
gRestrictedWeapons = CreateArray ( WEAPONS_MAX_LENGTH , 0 );
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-04-15 09:42:12 +02:00
* Clears weapon restrict data .
2009-04-14 04:58:05 +02:00
*/
RestrictClearData ()
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Clear restricted weapons.
2009-04-09 18:30:14 +02:00
RestrictWeaponUnrestrictAll ();
2009-04-15 09:42:12 +02:00
// Load weapon group data.
2009-04-09 18:30:14 +02:00
if ( kvWeaponGroups != INVALID_HANDLE )
{
CloseHandle ( kvWeaponGroups );
}
kvWeaponGroups = CreateKeyValues ( " weapongroups " );
2009-04-14 04:58:05 +02:00
}
/**
* Loads weapon data from file .
*/
RestrictOnMapStart ()
{
2009-04-20 02:56:26 +02:00
// Clear weapon restrict data.
RestrictClearData ();
// If module is disabled, then stop.
new bool : restrict = GetConVarBool ( g_hCvarsList [ CVAR_WEAPONS_RESTRICT ]);
if ( ! restrict )
{
return ;
}
2009-04-15 09:42:12 +02:00
// Restrict default restrictions. (set in weapons.txt)
2009-04-14 04:58:05 +02:00
RestrictDefaultRestrictions ();
2009-04-09 18:30:14 +02:00
2009-04-30 07:36:57 +02:00
// Get weapon groups config path.
decl String : pathweapongroups [ PLATFORM_MAX_PATH ];
new bool : exists = ConfigGetFilePath ( CVAR_CONFIG_PATH_WEAPONGROUPS , pathweapongroups );
2009-04-09 18:30:14 +02:00
2009-04-30 07:36:57 +02:00
// If file doesn't exist, then log and stop.
if ( ! exists )
2009-04-09 18:30:14 +02:00
{
2009-04-30 07:36:57 +02:00
// Log failure.
2009-04-20 05:43:20 +02:00
if ( LogCheckFlag ( LOG_CORE_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
2009-04-30 07:36:57 +02:00
LogMessageFormatted ( - 1 , " Weapons " , " Config Validation " , " Missing weapon groups config file: %s " , LOG_FORMAT_TYPE_ERROR , pathweapongroups );
2009-04-14 04:58:05 +02:00
}
return ;
2009-04-09 18:30:14 +02:00
}
2009-04-12 08:04:00 +02:00
2009-04-30 07:36:57 +02:00
// Put file data into memory.
FileToKeyValues ( kvWeaponGroups , pathweapongroups );
// Validate weapon groups config.
2009-04-13 20:33:13 +02:00
RestrictValidateWeaponGroups ();
}
2009-04-14 01:29:24 +02:00
/**
2009-04-15 09:42:12 +02:00
* Restrict default restrictions . ( set in weapons . txt )
2009-04-14 01:29:24 +02:00
*/
RestrictDefaultRestrictions ()
{
KvRewind ( kvWeapons );
if ( KvGotoFirstSubKey ( kvWeapons ))
{
decl String : weapon [ WEAPONS_MAX_LENGTH ];
2009-04-14 04:58:05 +02:00
decl String : display [ WEAPONS_MAX_LENGTH ];
2009-04-14 01:29:24 +02:00
do
{
KvGetSectionName ( kvWeapons , weapon , sizeof ( weapon ));
2009-04-15 09:42:12 +02:00
// If weapon is defaulted to restricted, then restrict weapon.
2009-04-14 04:58:05 +02:00
decl String : restrict [ 8 ];
KvGetString ( kvWeapons , " restrict " , restrict , sizeof ( restrict ), " no " );
2009-05-01 07:09:18 +02:00
if ( ConfigSettingToBool ( restrict ))
2009-04-14 01:29:24 +02:00
{
new WpnRestrictQuery : output = RestrictRestrict ( weapon , display );
RestrictPrintRestrictOutput ( 0 , output , display , true );
2009-04-14 04:58:05 +02:00
// Function calls above screwed with the keyvalue stack, so we have to set it back
2009-04-15 09:42:12 +02:00
// to where it was before those calls.
2009-04-14 04:58:05 +02:00
KvRewind ( kvWeapons );
KvJumpToKey ( kvWeapons , weapon );
2009-04-14 01:29:24 +02:00
}
} while ( KvGotoNextKey ( kvWeapons ));
}
}
/**
* Validate weapon group options
*/
2009-04-13 20:33:13 +02:00
RestrictValidateWeaponGroups ()
{
2009-04-27 04:00:38 +02:00
// If log flag check fails, then don't log.
2009-04-20 05:43:20 +02:00
if ( ! LogCheckFlag ( LOG_CORE_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
return ;
}
2009-04-15 03:24:02 +02:00
// Reset keygroup's traversal stack.
2009-04-12 08:04:00 +02:00
KvRewind ( kvWeaponGroups );
2009-04-15 03:24:02 +02:00
// Traverse into the keygroup. (weapon groups level)
2009-04-12 08:04:00 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
2009-04-14 04:58:05 +02:00
decl String : weapongroup [ WEAPONS_MAX_LENGTH ];
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-12 08:04:00 +02:00
do
{
2009-04-14 04:58:05 +02:00
KvGetSectionName ( kvWeaponGroups , weapongroup , sizeof ( weapongroup ));
2009-04-15 03:24:02 +02:00
// Traverse into the keygroup. (weapons level)
2009-04-12 08:04:00 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
do
{
KvGetSectionName ( kvWeaponGroups , groupweapon , sizeof ( groupweapon ));
2009-04-15 03:24:02 +02:00
// If weapon is invalid, then log it.
2009-04-12 08:04:00 +02:00
if ( ! WeaponsIsValidWeapon ( groupweapon ))
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( - 1 , " Weapon Restrict " , " Config Validation " , " Invalid weapon \" %s \" in group \" %s \" configured in weapongroups.txt. " , LOG_FORMAT_TYPE_ERROR , groupweapon , weapongroup );
2009-04-12 08:04:00 +02:00
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
KvGoBack ( kvWeaponGroups );
}
2009-04-15 03:24:02 +02:00
// If it couldn't traverse to the weapons, then log no weapons within group.
2009-04-14 04:58:05 +02:00
else
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( - 1 , " Weapon Restrict " , " Config Validation " , " No weapons listed in weapon group \" %s \" in weapongroups.txt. " , LOG_FORMAT_TYPE_ERROR , weapongroup );
2009-04-14 04:58:05 +02:00
}
2009-04-12 08:04:00 +02:00
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
2009-04-09 18:30:14 +02:00
}
/**
2009-04-10 01:08:51 +02:00
* Clears restricted weapon array .
2009-04-12 08:04:00 +02:00
*/
2009-04-09 18:30:14 +02:00
RestrictWeaponUnrestrictAll ()
{
ClearArray ( gRestrictedWeapons );
}
/**
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-04-30 07:36:57 +02:00
// Hook "canuse" on client.
2009-04-13 20:33:13 +02:00
gCanUseHookID [ client ] = Hacks_Hook ( client , HACKS_HTYPE_WEAPON_CANUSE , RestrictCanUse , 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-04-30 07:36:57 +02:00
// Unhook "canuse" on client.
Hacks_Unhook ( gCanUseHookID [ client ]);
}
/**
* Client is spawning into the game .
*
* @ param client The client index .
*/
RestrictOnClientSpawn ( client )
{
// Re-hook "canuse" on client.
2009-04-09 18:30:14 +02:00
Hacks_Unhook ( gCanUseHookID [ client ]);
2009-04-30 07:36:57 +02:00
gCanUseHookID [ client ] = Hacks_Hook ( client , HACKS_HTYPE_WEAPON_CANUSE , RestrictCanUse , false );
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
{
2009-04-15 09:42:12 +02:00
// If plugin is disabled then stop.
2009-04-27 04:00:38 +02:00
/* new bool : enabled = GetConVarBool ( g_hCvarsList [ CVAR_ENABLE ]);
2009-04-09 18:30:14 +02:00
if ( ! enabled )
{
2009-04-15 09:42:12 +02:00
// Allow command.
2009-04-09 18:30:14 +02:00
return Plugin_Continue ;
2009-04-27 04:00:38 +02:00
} */
// Disabled
2009-04-09 18:30:14 +02:00
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
{
ZR_PrintToChat ( client , " Zombie cant use weapon " );
// Block command
return Plugin_Handled ;
}
decl String : weapon [ 64 ];
GetCmdArg ( 1 , weapon , sizeof ( weapon ));
ReplaceString ( weapon , sizeof ( weapon ), " weapon_ " , " " );
2009-04-15 09:42:12 +02:00
// Check if the weapon is restricted, if so then block command.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponRestricted ( weapon ))
2009-04-09 18:30:14 +02:00
{
ZR_PrintToChat ( client , " Weapon is restricted " , weapon );
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-04-10 01:08:51 +02:00
* Restricts a weapon .
2009-04-09 18:30:14 +02:00
*
* @ param weapon The weapon / group name .
2009-04-14 04:58:05 +02:00
* @ param display String set to the name set in weapons . txt
* Set to the value of 'weapon' if invalid
2009-04-09 18:30:14 +02:00
* @ return Successful_Weapon : The call successfully restricted a weapon .
* Successful_Group : The call successfully restricted a weapon group .
2009-04-12 08:04:00 +02:00
* Failed_Weapon : The call failed to restrict a weapon .
* Failed_Group : The call failed to restrict a weapon group .
2009-04-10 01:08:51 +02:00
* Invalid : The call was unsuccessful due to invalid weapon .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 23:55:02 +02:00
WpnRestrictQuery : RestrictRestrict ( const String : weapon [], String : display [] = " " )
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Check if weapon is a custom group name.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponGroup ( weapon ))
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Return restrict failed if group is already restricted.
2009-04-13 20:33:13 +02:00
if ( RestrictIsGroupRestricted ( weapon ))
{
return Failed_Group ;
}
2009-04-15 09:42:12 +02:00
// Jump to weapon group key.
2009-04-09 18:30:14 +02:00
KvRewind ( kvWeaponGroups );
KvJumpToKey ( kvWeaponGroups , weapon );
2009-04-15 09:42:12 +02:00
// Get display name of the weapon group.
2009-04-13 20:33:13 +02:00
KvGetSectionName ( kvWeaponGroups , display , WEAPONS_MAX_LENGTH );
2009-04-15 09:42:12 +02:00
// Traverse into the group's weapons.
2009-04-09 18:30:14 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
2009-04-13 20:33:13 +02:00
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
do
{
KvGetSectionName ( kvWeaponGroups , groupweapon , sizeof ( groupweapon ));
2009-04-15 09:42:12 +02:00
// If weapon is invalid, then skip.
2009-04-12 08:04:00 +02:00
if ( ! WeaponsIsValidWeapon ( groupweapon ))
{
continue ;
}
2009-04-15 09:42:12 +02:00
// Add to restricted weapon array if not already restricted.
2009-04-13 20:33:13 +02:00
if ( ! RestrictIsWeaponRestricted ( groupweapon ))
2009-04-09 18:30:14 +02:00
{
PushArrayString ( gRestrictedWeapons , groupweapon );
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
2009-04-15 09:42:12 +02:00
// Successfully restricted a group
2009-04-09 18:30:14 +02:00
return Successful_Group ;
}
2009-04-15 09:42:12 +02:00
// If weapon name is invalid then set display to invalid weapon name.
2009-04-12 08:04:00 +02:00
if ( ! WeaponsIsValidWeapon ( weapon ))
2009-04-09 18:30:14 +02:00
{
2009-04-14 04:58:05 +02:00
strcopy ( display , WEAPONS_MAX_LENGTH , weapon );
2009-04-15 09:42:12 +02:00
// Weapon name was invalid.
2009-04-12 08:04:00 +02:00
return Invalid ;
2009-04-09 18:30:14 +02:00
}
2009-04-15 09:42:12 +02:00
// Get display name of the weapon.
2009-04-14 04:58:05 +02:00
WeaponGetDisplayName ( weapon , display );
2009-04-15 09:42:12 +02:00
// Return restrict failed if weapon is already restricted.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponRestricted ( weapon ))
2009-04-12 08:04:00 +02:00
{
return Failed_Weapon ;
}
2009-04-15 09:42:12 +02:00
// Add to restricted weapon array.
2009-04-13 20:33:13 +02:00
PushArrayString ( gRestrictedWeapons , display );
2009-04-12 08:04:00 +02:00
2009-04-09 18:30:14 +02:00
return Successful_Weapon ;
}
/**
2009-04-10 01:08:51 +02:00
* Unrestricts a weapon .
2009-04-09 18:30:14 +02:00
*
* @ param weapon The weapon / group name .
2009-04-15 09:42:12 +02:00
* @ param display String set to the name set in weapons . txt .
* Set to the value of 'weapon' if invalid .
2009-04-09 18:30:14 +02:00
* @ return Successful_Weapon : The call successfully restricted a weapon .
* Successful_Group : The call successfully restricted a weapon group .
2009-04-12 08:04:00 +02:00
* Failed_Weapon : The call failed to restrict a weapon .
* Failed_Group : The call failed to restrict a weapon group .
* Invalid : The call was unsuccessful due to invalid weapon .
*/
2009-04-13 23:55:02 +02:00
WpnRestrictQuery : RestrictUnrestrict ( const String : weapon [], String : display [] = " " )
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Check if weapon is a custom group name.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponGroup ( weapon ))
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Return restrict failed if group isn't restricted.
2009-04-13 23:55:02 +02:00
if ( RestrictIsGroupUnrestricted ( weapon ))
{
return Failed_Group ;
}
2009-04-15 09:42:12 +02:00
// Jump to weapon group key.
2009-04-09 18:30:14 +02:00
KvRewind ( kvWeaponGroups );
KvJumpToKey ( kvWeaponGroups , weapon );
2009-04-15 09:42:12 +02:00
// Get display name of the weapon group.
2009-04-13 20:33:13 +02:00
KvGetSectionName ( kvWeaponGroups , display , WEAPONS_MAX_LENGTH );
2009-04-15 09:42:12 +02:00
// Traverse into the group's weapons.
2009-04-09 18:30:14 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
2009-04-12 08:04:00 +02:00
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
do
{
KvGetSectionName ( kvWeaponGroups , groupweapon , sizeof ( groupweapon ));
2009-04-12 08:04:00 +02:00
// If weapon is invalid, then skip
if ( ! WeaponsIsValidWeapon ( groupweapon ))
{
continue ;
}
2009-04-15 09:42:12 +02:00
// Remove from restricted weapon array if currently restricted.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponRestricted ( groupweapon ))
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Verify weapon is in the array.
2009-04-13 20:33:13 +02:00
new weaponindex = RestrictGetIndex ( groupweapon );
2009-04-09 18:30:14 +02:00
if ( weaponindex > - 1 )
{
RemoveFromArray ( gRestrictedWeapons , weaponindex );
}
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
2009-04-15 09:42:12 +02:00
// Successfully unrestricted a group
2009-04-09 18:30:14 +02:00
return Successful_Group ;
}
2009-04-15 09:42:12 +02:00
// If weapon name is invalid then set display to invalid weapon name.
2009-04-12 08:04:00 +02:00
if ( ! WeaponsIsValidWeapon ( weapon ))
2009-04-09 18:30:14 +02:00
{
2009-04-14 04:58:05 +02:00
strcopy ( display , WEAPONS_MAX_LENGTH , weapon );
2009-04-12 08:04:00 +02:00
return Invalid ;
2009-04-09 18:30:14 +02:00
}
2009-04-15 09:42:12 +02:00
// Get display name of the weapon.
2009-04-14 04:58:05 +02:00
WeaponGetDisplayName ( weapon , display );
2009-04-15 09:42:12 +02:00
// Return unrestrict failed if weapon isn't restricted.
2009-04-13 20:33:13 +02:00
if ( ! RestrictIsWeaponRestricted ( weapon ))
2009-04-12 08:04:00 +02:00
{
return Failed_Weapon ;
}
2009-04-15 09:42:12 +02:00
// Verify weapon is in the array.
2009-04-13 20:33:13 +02:00
new weaponindex = RestrictGetIndex ( display );
2009-04-12 08:04:00 +02:00
if ( weaponindex > - 1 )
{
2009-04-15 09:42:12 +02:00
// Remove from restricted weapon array.
2009-04-12 08:04:00 +02:00
RemoveFromArray ( gRestrictedWeapons , weaponindex );
}
return Successful_Weapon ;
2009-04-09 18:30:14 +02:00
}
2009-04-13 23:55:02 +02:00
/**
* Prints text to server or client based off the output it RestrictRestrict () .
* @ param client The client index .
* @ param output The output of RestrictRestrict () .
* @ param weapon The weapon client is trying to restrict .
* @ param cmd True if printing output in reply to a client command .
*/
2009-04-14 01:29:24 +02:00
RestrictPrintRestrictOutput ( client , WpnRestrictQuery : output , const String : weapon [], bool : reply )
2009-04-13 23:55:02 +02:00
{
switch ( output )
2009-04-15 09:42:12 +02:00
{
// Weapon was successfully restricted.
2009-04-13 23:55:02 +02:00
case Successful_Weapon :
{
ZR_PrintToChat ( 0 , " Restrict weapon " , weapon );
2009-04-14 04:58:05 +02:00
2009-04-20 05:43:20 +02:00
if ( LogCheckFlag ( LOG_GAME_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( client , " Weapon Restrict " , " Restrict " , " \" %L \" restricted weapon: \" %s \" . " , LOG_FORMAT_TYPE_FULL , client , weapon );
2009-04-14 04:58:05 +02:00
}
2009-04-13 23:55:02 +02:00
}
2009-04-15 09:42:12 +02:00
// Weapon group was successfully restricted.
2009-04-13 23:55:02 +02:00
case Successful_Group :
{
decl String : weaponlist [ 128 ];
RestrictGetGroupWeapons ( weapon , weaponlist , sizeof ( weaponlist ), " , " );
ZR_PrintToChat ( 0 , " Restrict custom weapon group " , weapon , weaponlist );
2009-04-14 04:58:05 +02:00
2009-04-20 05:43:20 +02:00
if ( LogCheckFlag ( LOG_GAME_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( client , " Weapon Restrict " , " Restrict " , " \" %L \" restricted weapon group: \" %s \" . " , LOG_FORMAT_TYPE_FULL , client , weapon );
2009-04-14 04:58:05 +02:00
}
2009-04-13 23:55:02 +02:00
}
2009-04-15 09:42:12 +02:00
// Weapon was already restricted.
2009-04-13 23:55:02 +02:00
case Failed_Weapon :
{
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Restrict weapon failed " , weapon );
}
else
{
ZR_PrintToChat ( client , " Restrict weapon failed " , weapon );
}
}
2009-04-15 09:42:12 +02:00
// Weapon group was already restricted.
2009-04-13 23:55:02 +02:00
case Failed_Group :
{
decl String : weaponlist [ 128 ];
RestrictGetGroupWeapons ( weapon , weaponlist , sizeof ( weaponlist ), " , " );
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Restrict custom weapon group failed " , weapon , weaponlist );
}
else
{
ZR_PrintToChat ( client , " Restrict custom weapon group failed " , weapon , weaponlist );
}
}
2009-04-15 09:42:12 +02:00
// Weapon name was invalid.
2009-04-13 23:55:02 +02:00
case Invalid :
{
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Weapon invalid " , weapon );
}
else
{
ZR_PrintToChat ( client , " Weapon invalid " , weapon );
}
}
}
}
/**
* Prints text to server or client based off the output it RestrictUnrestrict () .
* @ param client The client index .
* @ param output The output of RestrictUnrestrict () .
* @ param weapon The weapon client is trying to unrestrict .
* @ param cmd True if printing output in reply to a client command .
*/
2009-04-14 01:29:24 +02:00
RestrictPrintUnrestrictOutput ( client , WpnRestrictQuery : output , const String : weapon [], bool : reply )
2009-04-13 23:55:02 +02:00
{
switch ( output )
{
2009-04-15 09:42:12 +02:00
// Weapon was successfully unrestricted.
2009-04-13 23:55:02 +02:00
case Successful_Weapon :
{
ZR_PrintToChat ( 0 , " Unrestrict weapon " , weapon );
2009-04-14 04:58:05 +02:00
2009-04-20 05:43:20 +02:00
if ( LogCheckFlag ( LOG_GAME_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( client , " Weapon Restrict " , " Unrestrict " , " \" %L \" unrestricted weapon: \" %s \" . " , LOG_FORMAT_TYPE_FULL , client , weapon );
2009-04-14 04:58:05 +02:00
}
2009-04-13 23:55:02 +02:00
}
2009-04-15 09:42:12 +02:00
// Weapon group was successfully unrestricted.
2009-04-13 23:55:02 +02:00
case Successful_Group :
{
decl String : weaponlist [ 128 ];
RestrictGetGroupWeapons ( weapon , weaponlist , sizeof ( weaponlist ), " , " );
ZR_PrintToChat ( 0 , " Unrestrict custom weapon group " , weapon , weaponlist );
2009-04-14 04:58:05 +02:00
2009-04-20 05:43:20 +02:00
if ( LogCheckFlag ( LOG_GAME_EVENTS , LOG_MODULE_WEAPONS ))
2009-04-14 04:58:05 +02:00
{
2009-04-20 05:43:20 +02:00
LogMessageFormatted ( client , " Weapon Restrict " , " Unrestrict " , " \" %L \" unrestricted weapon group: \" %s \" . " , LOG_FORMAT_TYPE_FULL , client , weapon );
2009-04-14 04:58:05 +02:00
}
2009-04-13 23:55:02 +02:00
}
2009-04-15 09:42:12 +02:00
// Weapon wasn't restricted.
2009-04-13 23:55:02 +02:00
case Failed_Weapon :
{
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Unrestrict weapon failed " , weapon );
}
else
{
ZR_PrintToChat ( client , " Unrestrict weapon failed " , weapon );
}
}
2009-04-15 09:42:12 +02:00
// Weapon group wasn't restricted.
2009-04-13 23:55:02 +02:00
case Failed_Group :
{
decl String : weaponlist [ 128 ];
RestrictGetGroupWeapons ( weapon , weaponlist , sizeof ( weaponlist ), " , " );
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Unrestrict custom weapon group failed " , weapon , weaponlist );
}
else
{
ZR_PrintToChat ( client , " Unrestrict custom weapon group failed " , weapon , weaponlist );
}
}
2009-04-15 09:42:12 +02:00
// Weapon name was invalid.
2009-04-13 23:55:02 +02:00
case Invalid :
{
2009-04-14 01:29:24 +02:00
if ( reply )
2009-04-13 23:55:02 +02:00
{
ZR_ReplyToCommand ( client , " Weapon invalid " , weapon );
}
else
{
ZR_PrintToChat ( client , " Weapon invalid " , weapon );
}
}
}
}
2009-04-09 18:30:14 +02:00
/**
* Checks if a weapon is restricted .
*
* @ param weapon The weapon name .
2009-04-15 09:42:12 +02:00
* @ return True if weapon is restricted , false if not .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
bool : RestrictIsWeaponRestricted ( const String : weapon [])
2009-04-09 18:30:14 +02:00
{
2009-04-14 04:58:05 +02:00
decl String : restrictedweapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
new size = GetArraySize ( gRestrictedWeapons );
2009-04-15 09:42:12 +02:00
// x = restricted weapon index.
2009-04-09 18:30:14 +02:00
for ( new x = 0 ; x < size ; x ++ )
{
GetArrayString ( gRestrictedWeapons , x , restrictedweapon , sizeof ( restrictedweapon ));
2009-04-15 09:42:12 +02:00
// Check if weapon matches any weapon names in the restricted weapon array.
2009-04-09 18:30:14 +02:00
if ( StrEqual ( weapon , restrictedweapon , false ))
{
2009-04-15 09:42:12 +02:00
// Weapon is restricted.
2009-04-09 18:30:14 +02:00
return true ;
}
}
2009-04-15 09:42:12 +02:00
// Weapon is not restricted.
2009-04-09 18:30:14 +02:00
return false ;
}
2009-04-13 20:33:13 +02:00
/**
2009-04-13 22:00:37 +02:00
* Checks if a weapon group is completely restricted .
2009-04-13 20:33:13 +02:00
*
* @ param weapongroup The weapon group name .
*/
bool : RestrictIsGroupRestricted ( const String : weapongroup [])
{
2009-04-15 09:42:12 +02:00
// Reset keygroup's traversal stack.
2009-04-13 20:33:13 +02:00
KvRewind ( kvWeaponGroups );
2009-04-15 09:42:12 +02:00
// Traverse in to the group names.
2009-04-13 20:33:13 +02:00
if ( KvJumpToKey ( kvWeaponGroups , weapongroup ))
{
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-15 09:42:12 +02:00
// Traverse into the group's weapons.
2009-04-13 23:55:02 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
2009-04-13 20:33:13 +02:00
{
2009-04-13 23:55:02 +02:00
do
2009-04-13 20:33:13 +02:00
{
2009-04-13 23:55:02 +02:00
KvGetSectionName ( kvWeaponGroups , groupweapon , WEAPONS_MAX_LENGTH );
// Return false is a weapon isn't restricted, but only if the weapon is valid (we ignore invalid ones)
if ( WeaponsIsValidWeapon ( groupweapon ) && ! RestrictIsWeaponRestricted ( groupweapon ))
{
return false ;
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
return true ;
}
2009-04-13 20:33:13 +02:00
}
return false ;
}
2009-04-13 22:00:37 +02:00
/**
* Checks if a weapon group is completely unrestricted .
*
* @ param weapongroup The weapon group name .
*/
bool : RestrictIsGroupUnrestricted ( const String : weapongroup [])
{
2009-04-15 09:42:12 +02:00
// Reset keygroup's traversal stack.
2009-04-13 22:00:37 +02:00
KvRewind ( kvWeaponGroups );
2009-04-15 09:42:12 +02:00
// Traverse in to the group names.
2009-04-13 22:00:37 +02:00
if ( KvJumpToKey ( kvWeaponGroups , weapongroup ))
{
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-15 09:42:12 +02:00
// Traverse into the group's weapons.
2009-04-13 23:55:02 +02:00
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
2009-04-13 22:00:37 +02:00
{
2009-04-13 23:55:02 +02:00
do
2009-04-13 22:00:37 +02:00
{
2009-04-13 23:55:02 +02:00
KvGetSectionName ( kvWeaponGroups , groupweapon , WEAPONS_MAX_LENGTH );
// Return false if a weapon is restricted
if ( RestrictIsWeaponRestricted ( groupweapon ))
{
return false ;
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
return true ;
}
2009-04-13 22:00:37 +02:00
}
return false ;
}
/**
* Checks if a weapon group is partially restricted .
*
* @ param weapongroup The weapon group name .
*/
bool : RestrictIsPartialRestricted ( const String : weapongroup [])
{
return ( ! RestrictIsGroupRestricted ( weapongroup ) && ! RestrictIsGroupUnrestricted ( weapongroup ));
}
2009-04-09 18:30:14 +02:00
/**
* Returns the array index of the restricted weapon .
*
* @ param weapon The weapon name .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
RestrictGetIndex ( const String : weapon [])
2009-04-09 18:30:14 +02:00
{
2009-04-14 04:58:05 +02:00
decl String : restrictedweapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
new size = GetArraySize ( gRestrictedWeapons );
2009-04-15 09:42:12 +02:00
// x = restricted weapon index.
2009-04-09 18:30:14 +02:00
for ( new x = 0 ; x < size ; x ++ )
{
GetArrayString ( gRestrictedWeapons , x , restrictedweapon , sizeof ( restrictedweapon ));
2009-04-15 09:42:12 +02:00
// Check if weapon matches weapon in restricted weapons array.
2009-04-09 18:30:14 +02:00
if ( StrEqual ( weapon , restrictedweapon , false ))
{
2009-04-15 09:42:12 +02:00
// Return restricted weapon's index.
2009-04-09 18:30:14 +02:00
return x ;
}
}
2009-04-15 09:42:12 +02:00
// Weapon isn't restricted.
2009-04-09 18:30:14 +02:00
return - 1 ;
}
/**
* Checks if the provided name is a custom group .
*
* @ param groupname Name of the group to check .
* @ return True if it ' s a group , false if not .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
bool : RestrictIsWeaponGroup ( const String : groupname [])
2009-04-09 18:30:14 +02:00
{
2009-04-15 09:42:12 +02:00
// Reset keygroup's traversal stack.
2009-04-09 18:30:14 +02:00
KvRewind ( kvWeaponGroups );
2009-04-15 09:42:12 +02:00
// Returns true if groupname is listed in the custom groups file.
2009-04-09 18:30:14 +02:00
return KvJumpToKey ( kvWeaponGroups , groupname );
}
2009-04-13 20:33:13 +02:00
/**
* Creates an array of all listed weapon groups in weapongroups . txt .
* @ param arrayWeaponGroups The handle of the array , don ' t forget to call CloseHandle
* on it when finished !
* @ return The size of the array .
*/
RestrictCreateGroupArray ( & Handle : arrayWeaponGroups , maxlen = WEAPONS_MAX_LENGTH )
{
arrayWeaponGroups = CreateArray ( maxlen );
new count = 0 ;
KvRewind ( kvWeaponGroups );
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
decl String : weapongroup [ maxlen ];
do
{
KvGetSectionName ( kvWeaponGroups , weapongroup , maxlen );
PushArrayString ( arrayWeaponGroups , weapongroup );
count ++ ;
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
return count ;
}
2009-04-13 23:55:02 +02:00
/**
* Creates an array of all weapons listed in a custom weapon group .
* @ param arrayWeaponGroups The handle of the array , don ' t forget to call CloseHandle
* on it when finished !
* @ return The size of the array .
*/
RestrictCreateGroupWeaponsArray ( & Handle : arrayGroupWeapons , const String : weapongroup [], maxlen = WEAPONS_MAX_LENGTH )
{
arrayGroupWeapons = CreateArray ( maxlen );
new count = 0 ;
KvRewind ( kvWeaponGroups );
if ( KvJumpToKey ( kvWeaponGroups , weapongroup ))
{
decl String : groupweapon [ maxlen ];
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
do
{
KvGetSectionName ( kvWeaponGroups , groupweapon , maxlen );
2009-04-15 09:42:12 +02:00
// If the weapon is invalid, then stop.
2009-04-13 23:55:02 +02:00
if ( ! WeaponsIsValidWeapon ( groupweapon ))
{
continue ;
}
PushArrayString ( arrayGroupWeapons , groupweapon );
count ++ ;
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
}
return count ;
}
2009-04-09 18:30:14 +02:00
/**
* Returns a string of all weapons in a custom weapon group separated
* by the provided character .
*
* @ param groupname Name of the group to get weapon list from .
* @ param weaponlist Variable to store weapon list string in .
* @ param maxlen Maximum length of the weapon list , the rest is truncated .
* @ param separator Separator character between weapon names .
2009-04-12 08:04:00 +02:00
*/
2009-05-02 18:08:33 +02:00
RestrictGetGroupWeapons ( const String : groupname [], String : weaponlist [], maxlen , const String : separator [])
2009-04-09 18:30:14 +02:00
{
KvRewind ( kvWeaponGroups );
KvJumpToKey ( kvWeaponGroups , groupname );
if ( KvGotoFirstSubKey ( kvWeaponGroups ))
{
2009-04-12 08:04:00 +02:00
decl String : groupweapon [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
strcopy ( weaponlist , maxlen , " " );
do
{
KvGetSectionName ( kvWeaponGroups , groupweapon , sizeof ( groupweapon ));
2009-04-15 09:42:12 +02:00
// If weapon is invalid, then skip.
2009-04-12 08:04:00 +02:00
if ( ! WeaponsIsValidWeapon ( groupweapon ))
{
continue ;
}
2009-04-09 18:30:14 +02:00
if ( ! weaponlist [ 0 ])
{
strcopy ( weaponlist , maxlen , groupweapon );
}
else
{
Format ( weaponlist , maxlen , " %s%s%s " , weaponlist , separator , groupweapon );
}
} while ( KvGotoNextKey ( kvWeaponGroups ));
}
}
/**
* Hook callback , called when a player is trying to pick up a weapon .
* @ param client The client index .
* @ param weapon The weapon index .
* @ return 0 to block weapon pickup , Hacks_Continue to allow .
2009-04-12 08:04:00 +02:00
*/
2009-04-13 20:33:13 +02:00
public RestrictCanUse ( client , weapon , dummy1 , dummy2 , dummy3 , dummy4 )
2009-04-09 18:30:14 +02:00
{
2009-04-12 08:04:00 +02:00
new String : weaponname [ WEAPONS_MAX_LENGTH ];
2009-04-09 18:30:14 +02:00
GetEdictClassname ( weapon , weaponname , sizeof ( weaponname ));
2009-04-15 09:42:12 +02:00
// Strip "weapon_" from entity name.
2009-04-09 18:30:14 +02:00
ReplaceString ( weaponname , sizeof ( weaponname ), " weapon_ " , " " );
2009-04-29 01:58:41 +02:00
// If weapon is a knife, then allow pickup.
if ( StrEqual ( weaponname , " knife " ))
{
return Hacks_Continue ;
}
// If the weapon is restricted, then prevent pickup.
2009-04-13 20:33:13 +02:00
if ( RestrictIsWeaponRestricted ( weaponname ))
2009-04-09 18:30:14 +02:00
{
return 0 ;
}
2009-04-29 01:58:41 +02:00
// If the player is a zombie, then prevent pickup.
if ( InfectIsClientInfected ( client ))
2009-04-09 18:30:14 +02:00
{
return 0 ;
}
2009-04-29 01:58:41 +02:00
// Allow pickup.
2009-04-09 18:30:14 +02:00
return Hacks_Continue ;
2009-05-01 11:22:45 +02:00
}