2009-04-15 03:24:02 +02:00
/*
* ============================================================================
*
* Zombie : Reloaded
*
2009-06-12 05:51:26 +02:00
* File : hitgroup . inc
* Type : Core
* Description : API for loading hitgroup specific settings .
*
* 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-04-15 03:24:02 +02:00
*
* ============================================================================
*/
/**
2009-05-29 08:43:15 +02:00
* Maximum length for a hitgroup name
2009-04-15 03:24:02 +02:00
*/
2009-05-29 08:43:15 +02:00
#define HITGROUPS_MAX_LENGTH 32
2009-04-15 03:24:02 +02:00
/**
* @ section Player hitgroup values .
*/
2009-04-30 07:36:57 +02:00
#define HITGROUP_GENERIC 0
#define HITGROUP_HEAD 1
#define HITGROUP_CHEST 2
#define HITGROUP_STOMACH 3
2009-04-15 03:24:02 +02:00
#define HITGROUP_LEFTARM 4
#define HITGROUP_RIGHTARM 5
#define HITGROUP_LEFTLEG 6
#define HITGROUP_RIGHTLEG 7
#define HITGROUP_GEAR 10
/**
* @ endsection
*/
2009-04-15 09:42:12 +02:00
/**
2009-05-29 08:43:15 +02:00
* Hitgroup config data indexes .
*/
enum HitgroupsData
2009-04-15 03:24:02 +02:00
{
2009-05-29 08:43:15 +02:00
HITGROUPS_DATA_NAME = 0 ,
HITGROUPS_DATA_INDEX ,
HITGROUPS_DATA_DAMAGE ,
HITGROUPS_DATA_KNOCKBACK ,
2009-04-15 03:24:02 +02:00
}
2009-05-29 08:43:15 +02:00
/**
* Array handle to store hitgroups data .
*/
new Handle : arrayHitgroups = INVALID_HANDLE ;
2009-04-15 09:42:12 +02:00
/**
* Loads hitgroup data from file .
*/
2009-04-15 03:24:02 +02:00
HitgroupsLoad ()
{
2009-05-29 08:43:15 +02:00
// Register config file.
ConfigRegisterConfig ( File_Hitgroups , Structure_Keyvalue , CONFIG_FILE_ALIAS_HITGROUPS );
2009-05-18 06:26:13 +02:00
2009-04-20 02:56:26 +02:00
// If module is disabled, then stop.
new bool : hitgroups = GetConVarBool ( g_hCvarsList [ CVAR_HITGROUPS ]);
if ( ! hitgroups )
{
return ;
}
2009-05-29 08:43:15 +02:00
// Get hitgroups config path.
decl String : pathhitgroups [ PLATFORM_MAX_PATH ];
new bool : exists = ConfigGetCvarFilePath ( CVAR_CONFIG_PATH_HITGROUPS , pathhitgroups );
2009-04-30 07:36:57 +02:00
// If file doesn't exist, then log and stop.
if ( ! exists )
2009-04-15 03:24:02 +02:00
{
2009-04-30 07:36:57 +02:00
// Log failure.
2009-06-01 23:29:26 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_Hitgroups , " Config Validation " , " Missing hitgroups config file: %s " , pathhitgroups );
2009-05-29 08:43:15 +02:00
2009-04-15 03:24:02 +02:00
return ;
}
2009-05-29 08:43:15 +02:00
// Set the path to the config file.
ConfigSetConfigPath ( File_Hitgroups , pathhitgroups );
// Load config from file and create array structure.
new bool : success = ConfigLoadConfig ( File_Hitgroups , arrayHitgroups );
// Unexpected error, stop plugin.
if ( ! success )
{
2009-06-01 23:29:26 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_Hitgroups , " Config Validation " , " Unexpected error encountered loading: %s " , pathhitgroups );
2009-05-29 08:43:15 +02:00
return ;
}
2009-04-30 07:36:57 +02:00
2009-04-15 09:42:12 +02:00
// Validate hitgroups config.
2009-05-29 08:43:15 +02:00
new size = GetArraySize ( arrayHitgroups );
if ( ! size )
{
2009-06-01 23:29:26 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_Hitgroups , " Config Validation " , " No usable data found in hitgroups config file: %s " , pathhitgroups );
2009-05-29 08:43:15 +02:00
}
// Now copy data to array structure.
HitgroupsCacheData ();
2009-05-18 06:26:13 +02:00
// Set config data.
2009-05-29 08:43:15 +02:00
ConfigSetConfigLoaded ( File_Hitgroups , true );
ConfigSetConfigReloadFunc ( File_Hitgroups , GetFunctionByName ( GetMyHandle (), " HitgroupsOnConfigReload " ));
ConfigSetConfigHandle ( File_Hitgroups , arrayHitgroups );
}
/**
* Caches hitgroup data from file into arrays .
* Make sure the file is loaded before ( ConfigLoadConfig ) to prep array structure .
*/
HitgroupsCacheData ()
{
// Get config's file path.
decl String : pathhitgroups [ PLATFORM_MAX_PATH ];
ConfigGetConfigPath ( File_Hitgroups , pathhitgroups , sizeof ( pathhitgroups ));
new Handle : kvHitgroups ;
new bool : success = ConfigOpenConfigFile ( File_Hitgroups , kvHitgroups );
if ( ! success )
{
2009-06-01 23:29:26 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_Hitgroups , " Config Validation " , " Unexpected error caching data from hitgroups config file: %s " , pathhitgroups );
2009-05-29 08:43:15 +02:00
}
decl String : hitgroupname [ HITGROUPS_MAX_LENGTH ];
// x = array index
new size = GetArraySize ( arrayHitgroups );
for ( new x = 0 ; x < size ; x ++ )
{
HitgroupsGetName ( x , hitgroupname , sizeof ( hitgroupname ));
KvRewind ( kvHitgroups );
if ( ! KvJumpToKey ( kvHitgroups , hitgroupname ))
{
2009-06-01 23:29:26 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_Hitgroups , " Config Validation " , " Couldn't cache hitgroup data for: %s (check hitgroup config) " , hitgroupname );
2009-05-29 08:43:15 +02:00
continue ;
}
// General
new index = KvGetNum ( kvHitgroups , " index " , - 1 );
// Damage
new bool : damage = ConfigKvGetStringBool ( kvHitgroups , " damage " , " yes " );
// Knockback (module)
new Float : knockback = KvGetFloat ( kvHitgroups , " knockback " , 1.0 );
new Handle : arrayHitgroup = GetArrayCell ( arrayHitgroups , x );
// Push data into array.
PushArrayCell ( arrayHitgroup , index ); // Index: 1
PushArrayCell ( arrayHitgroup , damage ); // Index: 2
PushArrayCell ( arrayHitgroup , knockback ); // Index: 3
}
// We're done with this file now, so we can close it.
CloseHandle ( kvHitgroups );
2009-05-18 06:26:13 +02:00
}
/**
* Called when configs are being reloaded .
*
* @ param config The config being reloaded . ( only if 'all' is false )
*/
public HitgroupsOnConfigReload ( ConfigFile : config )
{
// Reload hitgroups config.
2009-05-29 08:43:15 +02:00
HitgroupsLoad ();
2009-04-15 03:24:02 +02:00
}
2009-04-15 09:42:12 +02:00
/**
2009-05-29 08:43:15 +02:00
* Find the array index at which the hitgroup index is at .
*
* @ param hitgroup The hitgroup index to search for .
* @ return The array index that contains the given hitgroup index .
*/
stock HitgroupToIndex ( hitgroup )
2009-04-15 03:24:02 +02:00
{
2009-05-29 08:43:15 +02:00
// x = Array index.
new size = GetArraySize ( arrayHitgroups );
for ( new x = 0 ; x < size ; x ++ )
2009-04-15 03:24:02 +02:00
{
2009-05-29 08:43:15 +02:00
// Get hitgroup index at this array index.
new index = HitgroupsGetIndex ( x );
// If hitgroup indexes match, then return array index.
if ( hitgroup == index )
{
return x ;
}
2009-04-15 03:24:02 +02:00
}
2009-05-29 08:43:15 +02:00
// Hitgroup index doesn't exist.
return - 1 ;
2009-04-15 03:24:02 +02:00
}
2009-04-15 09:42:12 +02:00
/**
2009-05-29 08:43:15 +02:00
* Gets the name of a hitgroup at a given index .
* @ param index The hitgroup index .
* @ param hitgroup The string to return name in .
* @ param maxlen The max length of the string .
*/
stock HitgroupsGetName ( index , String : hitgroup [], maxlen )
{
// Get array handle of hitgroup at given index.
new Handle : arrayHitgroup = GetArrayCell ( arrayHitgroups , index );
// Get hitgroup name.
GetArrayString ( arrayHitgroup , _ : HITGROUPS_DATA_NAME , hitgroup , maxlen );
}
/**
* Retrieve hitgroup index .
2009-04-15 09:42:12 +02:00
*
2009-05-29 08:43:15 +02:00
* @ param index The array index .
* @ return The hitgroup index .
2009-04-23 06:39:11 +02:00
*/
2009-05-29 08:43:15 +02:00
stock HitgroupsGetIndex ( index )
2009-04-15 03:24:02 +02:00
{
2009-05-29 08:43:15 +02:00
// Get array handle of hitgroup at given index.
new Handle : arrayHitgroup = GetArrayCell ( arrayHitgroups , index );
2009-04-15 03:24:02 +02:00
2009-05-29 08:43:15 +02:00
// Return hitgroup index of the hitgroup.
return GetArrayCell ( arrayHitgroup , _ : HITGROUPS_DATA_INDEX );
2009-04-23 06:39:11 +02:00
}
/**
* Retrieve hitgroup damage value .
*
2009-05-29 08:43:15 +02:00
* @ param index The array index .
2009-04-23 06:39:11 +02:00
* @ return True if hitgroup can be damaged , false if not .
*/
2009-05-29 08:43:15 +02:00
stock bool : HitgroupsCanDamage ( index )
2009-04-23 06:39:11 +02:00
{
2009-05-29 08:43:15 +02:00
// Get array handle of hitgroup at given index.
new Handle : arrayHitgroup = GetArrayCell ( arrayHitgroups , index );
// Return true if hitgroup can be damaged, false if not.
return bool : GetArrayCell ( arrayHitgroup , _ : HITGROUPS_DATA_DAMAGE );
}
/**
* Retrieve hitgroup knockback value .
*
* @ param index The array index .
* @ return The knockback multiplier of the hitgroup .
*/
stock Float : HitgroupsGetKnockback ( index )
{
// Get array handle of hitgroup at given index.
new Handle : arrayHitgroup = GetArrayCell ( arrayHitgroups , index );
2009-04-23 06:39:11 +02:00
2009-05-29 08:43:15 +02:00
// Return the knockback multiplier for the hitgroup.
return Float : GetArrayCell ( arrayHitgroup , _ : HITGROUPS_DATA_KNOCKBACK );
2009-05-01 11:22:45 +02:00
}