#include <amxmodx>
#include <reapi>
#include <fakemeta>
new PLUGIN_NAME[] = "Weapon Knife 2";
new PLUGIN_VERSION[] = "1.0";
new PLUGIN_AUTHOR[] = "Karaulov";
new KNIFE_CLASSNAME[] = "weapon_unrealknife";
new KNIFE_WEAPONNAME[] = "weapon_unrealknife";
new KNIFE_WEAPON[] = "weapon_knife";
new const AMMO_NAME[] = "KnifeAmmo";
new const AMMO_ID = 16;
new WeaponIdType: KNIFE_UNUSED_WEAPONID = WEAPON_GLOCK;
new WeaponIdType: KNIFE_FAKE_WEAPONID = WeaponIdType:75;
new MsgIdWeaponList,MsgIdAmmoPickup, FwdRegUserMsg, MsgHookWeaponList;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
MsgIdAmmoPickup = get_user_msgid("AmmoPickup");
RegisterHookChain(RG_CBasePlayer_AddPlayerItem, "AddItem", true);
RegisterHookChain(RG_CBasePlayer_GiveAmmo, "CBasePlayer_GiveAmmo_Pre", false);
}
public plugin_precache() {
precache_generic("sprites/weapon_unrealknife.txt");
precache_generic("sprites/unreal_knife.spr");
MsgIdWeaponList = get_user_msgid("WeaponList");
if (MsgIdWeaponList)
{
MsgHookWeaponList = register_message(MsgIdWeaponList, "HookWeaponList");
}
else
{
FwdRegUserMsg = register_forward(FM_RegUserMsg, "RegUserMsg_Post", true);
}
}
public RegUserMsg_Post(const name[])
{
if (strcmp(name, "WeaponList") == 0)
{
MsgIdWeaponList = get_orig_retval();
MsgHookWeaponList = register_message(MsgIdWeaponList, "HookWeaponList");
}
}
public HookWeaponList(const msg_id, const msg_dst, const msg_entity)
{
enum
{
arg_name = 1,
arg_ammo1,
arg_ammo1_max,
arg_ammo2,
arg_ammo2_max,
arg_slot,
arg_position,
arg_id,
arg_flags,
};
if (msg_dst != MSG_INIT || WeaponIdType:get_msg_arg_int(arg_id) != WEAPON_KNIFE)
{
return PLUGIN_CONTINUE;
}
if (FwdRegUserMsg)
{
unregister_forward(FM_RegUserMsg, FwdRegUserMsg, true);
}
unregister_message(MsgIdWeaponList, MsgHookWeaponList);
UTIL_WeaponList(MSG_INIT,0,_, KNIFE_WEAPONNAME,AMMO_ID,
1,get_msg_arg_int(arg_ammo2),get_msg_arg_int(arg_ammo2_max),
get_msg_arg_int(arg_slot),get_msg_arg_int(arg_position) + 1,cell:KNIFE_UNUSED_WEAPONID,get_msg_arg_int(arg_flags));
return PLUGIN_CONTINUE;
}
public CBasePlayer_GiveAmmo_Pre(const id, const amount, const name[]) {
if (strcmp(name, AMMO_NAME) != 0) {
return HC_CONTINUE;
}
giveAmmo(id, amount, AMMO_ID, 1);
SetHookChainReturn(ATYPE_INTEGER, AMMO_ID);
return HC_SUPERCEDE;
}
stock rg_get_player_item(const id, const classname[], const InventorySlotType:slot = NONE_SLOT) {
new item = get_member(id, m_rgpPlayerItems, slot);
while (!is_nullent(item)) {
if (FClassnameIs(item, classname)) {
return item;
}
item = get_member(item, m_pNext);
}
return 0;
}
giveKnife(const id)
{
new item = rg_get_player_item(id, KNIFE_CLASSNAME, KNIFE_SLOT);
if (item != 0) {
giveAmmo(id, 1, AMMO_ID, 1);
return item;
}
item = rg_create_entity(KNIFE_WEAPON, false);
if (is_nullent(item)) {
return NULLENT;
}
new Float:origin[3];
get_entvar(id, var_origin, origin);
set_entvar(item, var_origin, origin);
set_entvar(item, var_spawnflags, get_entvar(item, var_spawnflags) | SF_NORESPAWN);
set_member(item, m_Weapon_iPrimaryAmmoType, AMMO_ID);
set_member(item, m_Weapon_iSecondaryAmmoType, -1);
set_entvar(item, var_classname, KNIFE_CLASSNAME);
dllfunc(DLLFunc_Spawn, item);
set_member(item, m_iId, KNIFE_UNUSED_WEAPONID);
rg_set_iteminfo(item, ItemInfo_pszName, KNIFE_WEAPONNAME);
rg_set_iteminfo(item, ItemInfo_pszAmmo1, AMMO_NAME);
rg_set_iteminfo(item, ItemInfo_iMaxAmmo1, 1);
rg_set_iteminfo(item, ItemInfo_iId, KNIFE_FAKE_WEAPONID);
rg_set_iteminfo(item, ItemInfo_iPosition, 10);
rg_set_iteminfo(item, ItemInfo_iWeight, 1);
rg_set_iteminfo(item, ItemInfo_iSlot, KNIFE_SLOT);
dllfunc(DLLFunc_Touch, item, id);
if (get_entvar(item, var_owner) != id) {
set_entvar(item, var_flags, FL_KILLME);
return NULLENT;
}
giveAmmo(id, 1, AMMO_ID, 1);
return item;
}
giveAmmo(const id, const amount, const ammo, const maxammo) {
if (get_entvar(id, var_flags) & FL_SPECTATOR) {
return;
}
new count = get_member(id, m_rgAmmo, ammo);
new addammo = min(amount, maxammo - count);
if (addammo < 1) {
return;
}
set_member(id, m_rgAmmo, count + addammo, ammo);
emessage_begin(MSG_ONE, MsgIdAmmoPickup, .player = id);
ewrite_byte(ammo);
ewrite_byte(addammo);
emessage_end();
}
public AddItem(id, pItem)
{
if (is_nullent(pItem))
return HC_CONTINUE;
if (get_member(pItem, m_iId) == WEAPON_KNIFE)
giveKnife(id);
return HC_CONTINUE;
}
// from https://github.com/YoshiokaHaruki/AMXX-Dynamic-Crosshair
stock UTIL_WeaponList( const iDest, const pReceiver, const pItem = -1, szWeaponName[ ] = "", const iPrimaryAmmoType = -2, iMaxPrimaryAmmo = -2, iSecondaryAmmoType = -2, iMaxSecondaryAmmo = -2, iSlot = -2, iPosition = -2, iWeaponId = -2, iFlags = -2 )
{
static iMsgId_Weaponlist; if ( !iMsgId_Weaponlist ) iMsgId_Weaponlist = get_user_msgid( "WeaponList" );
message_begin( iDest, iMsgId_Weaponlist, .player = pReceiver );
if ( szWeaponName[ 0 ] == EOS && pItem > 0)
{
new szWeaponName2[128];
rg_get_iteminfo( pItem, ItemInfo_pszName, szWeaponName2, charsmax( szWeaponName2 ) )
write_string( szWeaponName2 );
}
else
write_string( szWeaponName );
write_byte( ( iPrimaryAmmoType <= -2 && pItem > 0 ) ? get_member( pItem, m_Weapon_iPrimaryAmmoType ) : iPrimaryAmmoType );
write_byte( ( iMaxPrimaryAmmo <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iMaxAmmo1 ) : iMaxPrimaryAmmo );
write_byte( ( iSecondaryAmmoType <= -2 && pItem > 0 ) ? get_member( pItem, m_Weapon_iSecondaryAmmoType ) : iSecondaryAmmoType );
write_byte( ( iMaxSecondaryAmmo <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iMaxAmmo2 ) : iMaxSecondaryAmmo );
write_byte( ( iSlot <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iSlot ) : iSlot );
write_byte( ( iPosition <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iPosition ) : iPosition );
write_byte( ( iWeaponId <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iId ) : iWeaponId );
write_byte( ( iFlags <= -2 && pItem > 0 ) ? rg_get_iteminfo( pItem, ItemInfo_iFlags ) : iFlags );
message_end( );
}