PHP Code:
/* Copyright © 2008, ConnorMcLeod
Weapons Models 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.
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 Weapons Models; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#define PLUGIN "Weapons Models"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.0.1"
#define MIN_MODEL_LENGTH 12
#define MAX_MODEL_LENGTH 64
#define MAX_WEAPONS CSW_P90
#define SHIELD_GUNS_BITSUM ( (1<<CSW_P228) | (1<<CSW_FIVESEVEN) | (1<<CSW_USP) | (1<<CSW_GLOCK18) | (1<<CSW_DEAGLE))
#define NADES_BITSUM ( (1<<CSW_HEGRENADE) | (1<<CSW_SMOKEGRENADE) | (1<<CSW_FLASHBANG) )
#define SHIELD_WEAPONS_BS ( SHIELD_GUNS_BITSUM | NADES_BITSUM | (1<<CSW_KNIFE) )
#define is_shield_weapon(%1) (SHIELD_WEAPONS_BS & (1<<%1))
#define is_weapon_nade(%1) (NADES_BITSUM & (1<<%1))
#define cs_get_user_shield(%1) (get_pdata_int(%1, 510) & (1<<24))
#define m_pPlayer 41 // VEN
#define m_iId 43
#define cs_get_weapon_owner(%1) get_pdata_cbase(%1, m_pPlayer, 4)
#define cs_get_weapon_id(%1) get_pdata_int(%1, m_iId, 4)
#define MAKE_STRING(%1) engfunc(EngFunc_AllocString, %1)
#define WEAPONBOX 1
#define GRENADE 2
#define OFFSET_WBOX_AMMO_ID_CODE_WIN32 41
new const OFFSET_WBOX_CB_WEAP_ENT_WIN32[] = {0, 35, 36, 37, 38, 39} // 39 works for c4 //38 works on cz //37 Oo
new g_iFhSetModel, g_iEntToSetModel[2]
new g_iVModel[MAX_WEAPONS+1], g_iPModel[MAX_WEAPONS+1]
new g_iShieldVModel[MAX_WEAPONS+1], g_iShieldPModel[MAX_WEAPONS+1]
new g_szWorldModel[MAX_WEAPONS+1][MAX_MODEL_LENGTH]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
}
public plugin_precache()
{
if( !cvar_exists("server_startup") )
{
register_cvar("server_startup", "1")
server_cmd("restart")
server_exec()
return
}
new szDatas[MAX_MODEL_LENGTH*6]
get_localinfo("amxx_configsdir", szDatas, charsmax(szDatas))
format(szDatas, charsmax(szDatas), "%s/weapons_models.ini", szDatas)
new iFile = fopen(szDatas, "rt")
if(!iFile)
{
return
}
new szCswName[20], iCswId, bool:bRegisterDeploy, bool:bRegisterGrenadeSpawn, bool:bRegisterWeaponBoxSpawn,
szVModel[MAX_MODEL_LENGTH], szPModel[MAX_MODEL_LENGTH], szShieldVModel[MAX_MODEL_LENGTH],
szShieldPModel[MAX_MODEL_LENGTH], szWModel[MAX_MODEL_LENGTH]
new szModName[16], bCzero
get_modname(szModName, charsmax(szModName))
bCzero = equal(szModName, "czero")
while(!feof(iFile))
{
fgets(iFile, szDatas, charsmax(szDatas))
trim(szDatas)
if(!szDatas[0] || szDatas[0] == ';' || szDatas[0] == '#' || (szDatas[0] == '/' && szDatas[1] == '/'))
continue
szVModel[0] = 0
szPModel[0] = 0
szWModel[0] = 0
szShieldVModel[0] = 0
szShieldPModel[0] = 0
szCswName[0] = 0
parse(szDatas, szCswName, charsmax(szCswName), szVModel, charsmax(szVModel), szPModel, charsmax(szPModel),
szWModel, charsmax(szWModel), szShieldVModel, charsmax(szShieldVModel), szShieldPModel, charsmax(szShieldPModel))
if( !szVModel[0] && !szPModel[0] && !szShieldVModel[0] && !szShieldPModel[0] && !szWModel[0] )
continue
if( !(iCswId = get_weaponid(szCswName)) )
{
UTIL_Log_To_File("^"%s^" is not an existing weapon name", szCswName)
continue
}
bRegisterDeploy = false
if( szVModel[0] )
{
if( Check_File( szVModel ) )
{
bRegisterDeploy = true
precache_model(szVModel)
g_iVModel[iCswId] = MAKE_STRING(szVModel)
}
}
if( szPModel[0] )
{
if( Check_File( szPModel ) )
{
bRegisterDeploy = true
precache_model(szPModel)
g_iPModel[iCswId] = MAKE_STRING(szPModel)
}
}
if( is_shield_weapon(iCswId) && szShieldVModel[0] )
{
if( Check_File( szShieldVModel ) )
{
bRegisterDeploy = true
precache_model(szShieldVModel)
g_iShieldVModel[iCswId] = MAKE_STRING(szShieldVModel)
}
}
if( is_shield_weapon(iCswId) && szShieldPModel[0] )
{
if( Check_File( szShieldPModel ) )
{
bRegisterDeploy = true
precache_model(szShieldPModel)
g_iShieldPModel[iCswId] = MAKE_STRING(szShieldPModel)
}
}
if( szWModel[0] )
{
if( Check_File( szWModel ) )
{
precache_model(szWModel)
formatex(g_szWorldModel[iCswId], MAX_MODEL_LENGTH-1, szWModel)
if( is_weapon_nade(iCswId) )
{
bRegisterGrenadeSpawn = true
if( bCzero )
{
bRegisterWeaponBoxSpawn = true
}
}
else if( iCswId == CSW_C4 )
{
RegisterHam(Ham_Item_Kill, "weapon_c4", "WeaponC4_Kill")
bRegisterWeaponBoxSpawn = true
}
else
{
bRegisterWeaponBoxSpawn = true
}
}
}
if( bRegisterDeploy )
{
RegisterHam(Ham_Item_Deploy, szCswName, "Item_Deploy", 1)
}
}
fclose(iFile)
if( bRegisterGrenadeSpawn )
{
RegisterHam(Ham_Spawn, "grenade", "Grenade_Spawn", 1)
}
if( bRegisterWeaponBoxSpawn )
{
RegisterHam(Ham_Spawn, "weaponbox", "WeaponBox_Spawn", 1)
}
}
Check_File( const szFile[] )
{
new iLen = strlen(szFile)
if( iLen < MIN_MODEL_LENGTH )
{
UTIL_Log_To_File("File name ^"%s^" is not valid", szFile)
}
if( !equal(szFile[iLen-4], ".mdl") )
{
UTIL_Log_To_File("File ^"%s^" has a wrong extension %s (.mdl required)", szFile, szFile[iLen-4])
return 0
}
else if( !file_exists(szFile) )
{
UTIL_Log_To_File("File ^"%s^" doesn't exist", szFile)
return 0
}
return 1
}
public Item_Deploy(iEnt)
{
new iCswId = cs_get_weapon_id(iEnt)
new id = cs_get_weapon_owner(iEnt)
new iVModel, iPModel
if( is_shield_weapon(iCswId) && cs_get_user_shield(id) )
{
iVModel = g_iShieldVModel[iCswId]
iPModel = g_iShieldPModel[iCswId]
}
else
{
iVModel = g_iVModel[iCswId]
iPModel = g_iPModel[iCswId]
}
if( iVModel )
{
set_pev(id, pev_viewmodel, iVModel)
}
if( iPModel )
{
set_pev(id, pev_weaponmodel, iPModel)
}
}
public Grenade_Spawn( iEnt )
{
g_iEntToSetModel[0] = iEnt
g_iEntToSetModel[1] = GRENADE
g_iFhSetModel = register_forward(FM_SetModel, "SetModel")
}
public WeaponBox_Spawn( iWeaponBox )
{
g_iEntToSetModel[0] = iWeaponBox
g_iEntToSetModel[1] = WEAPONBOX
g_iFhSetModel = register_forward(FM_SetModel, "SetModel")
}
public SetModel( iEnt )
{
if( iEnt != g_iEntToSetModel[0] )
return FMRES_IGNORED
new iCswId
switch( g_iEntToSetModel[1] )
{
case WEAPONBOX:
{
new iOtherEnt
for(new i = 1; i <= sizeof OFFSET_WBOX_CB_WEAP_ENT_WIN32; ++i)
{
if( i == sizeof OFFSET_WBOX_CB_WEAP_ENT_WIN32 )
return FMRES_IGNORED
iOtherEnt = get_pdata_cbase(iEnt, OFFSET_WBOX_CB_WEAP_ENT_WIN32[i], 4)
if(pev_valid(iOtherEnt) && pev(iOtherEnt, pev_owner) == iEnt && ExecuteHam(Ham_Item_ItemSlot, iOtherEnt) == i)
break
}
iCswId = cs_get_weapon_id(iOtherEnt)
unregister_forward(FM_SetModel, g_iFhSetModel)
if( g_szWorldModel[iCswId][0] )
{
engfunc( EngFunc_SetModel, iEnt, g_szWorldModel[iCswId] )
unregister_forward(FM_SetModel, g_iFhSetModel)
return FMRES_SUPERCEDE
}
}
case GRENADE:
{
iCswId = cs_get_grenade_type( iEnt )
if( g_szWorldModel[iCswId][0] )
{
engfunc( EngFunc_SetModel, iEnt, g_szWorldModel[iCswId] )
unregister_forward(FM_SetModel, g_iFhSetModel)
return FMRES_SUPERCEDE
}
}
}
return FMRES_IGNORED
}
cs_get_grenade_type( iEnt ) // VEN
{
new iBits = get_pdata_int(iEnt, 114)
if (iBits & (1<<0))
{
return CSW_HEGRENADE
}
else if (iBits & (1<<1))
{
return CSW_SMOKEGRENADE
}
else if (!iBits)
{
return CSW_FLASHBANG
}
return 0
}
public WeaponC4_Kill( index )
{
new iEnt = engfunc( EngFunc_FindEntityByString , FM_NULLENT , "classname" , "grenade" )
while( iEnt )
{
if( get_pdata_int(iEnt, 96) & (1<<8) )
{
engfunc( EngFunc_SetModel, iEnt, g_szWorldModel[CSW_C4])
break
}
iEnt = engfunc( EngFunc_FindEntityByString , iEnt , "classname" , "grenade" )
}
}
UTIL_Log_To_File(const fmt[], any:...) // from shptools
{
static string[512]
vformat(string, charsmax(string), fmt, 2)
static date[16], time[16], fp
get_time("%H:%M:%S", time, charsmax(time))
static logfile[192]
if(logfile[0] == 0)
{
new basedir[64]
get_localinfo("amxx_basedir", basedir, charsmax(basedir))
get_time("%m%d", date, charsmax(date))
formatex(logfile, charsmax(logfile), "%s/logs/weapons_models_%s.log", basedir, date)
}
get_time("%m/%d/%Y", date, charsmax(date))
fp = fopen(logfile, "a")
fprintf(fp, "L %s - %s: %s^n", date, time, string)
fclose(fp)
}