/*
____ _ _____ _ __ __
| _ \ | | / ____| | | \ \ / /
| |_) | __ _ ___| | __ | | _ _ ___| |_ ___ _ __ ___ \ \ /\ / /__ __ _ _ __ ___ _ __ ___
| _ < / _` |/ __| |/ / | | | | | / __| __/ _ \| '_ ` _ \ \ \/ \/ / _ \/ _` | '_ \ / _ \| '_ \/ __|
| |_) | (_| | (__| < | |___| |_| \__ \ || (_) | | | | | | \ /\ / __/ (_| | |_) | (_) | | | \__ \
|____/ \__,_|\___|_|\_\ \_____\__,_|___/\__\___/|_| |_| |_| \/ \/ \___|\__,_| .__/ \___/|_| |_|___/
| |
|_|
[1.0]
- Первый релиз
[1.0.1]
- Bugfix. Показывались не те модельки, что должны.
*/
// Original:
https://forums.alliedmods.net/showthread.php?p=359356
// Help: D34
// Idea: hoboman313/cheap_suit & cvf0n
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#define REMOVE_ENTITY(%1) set_pev(%1, pev_flags, FL_KILLME)
#define PRECACHE_MODEL(%1) engfunc(EngFunc_PrecacheModel, %1)
#define MAX_CLIENTS 32
// Linux extra offsets
#define linux_diff_weapon 4
#define linux_diff_player 5
// CBasePlayerItem
#define m_pPlayer 41
// CBasePlayer
#define m_pActiveItem 373
#define m_rpgPlayerItems 367
#define m_iAutoWepSwitch 509
new const g_szWeaponEntNames[][] =
{
"weapon_p228",
"weapon_scout",
"weapon_hegrenade",
"weapon_xm1014",
"weapon_c4",
"weapon_mac10",
"weapon_aug",
"weapon_smokegrenade",
"weapon_elite",
"weapon_fiveseven",
"weapon_ump45",
"weapon_sg550",
"weapon_galil",
"weapon_famas",
"weapon_usp",
"weapon_glock18",
"weapon_awp",
"weapon_mp5navy",
"weapon_m249",
"weapon_m3",
"weapon_m4a1",
"weapon_tmp",
"weapon_g3sg1",
"weapon_flashbang",
"weapon_deagle",
"weapon_sg552",
"weapon_ak47",
"weapon_knife",
"weapon_p90"
};
enum _: e_WeaponData
{
DATA[16],
MODEL[64],
BODY[16]
};
#define FILE_NAME "/addons/amxmodx/configs/api_backweapons.ini"
new Array: g_aWeaponData;
new g_iUserBackWeaponEntity[MAX_CLIENTS + 1];
public plugin_init()
{
register_plugin("[AMXX] API: Back Weapons", "1.0.1", "xUnicorn (t3rkecorejz)");
RegisterHam(Ham_Spawn, "player", "CPlayer__Spawn_Post", true);
RegisterHam(Ham_AddPlayerItem, "player", "CPlayer__AddPlayerItem_Pre", false);
RegisterHam(Ham_RemovePlayerItem, "player", "CPlayer__RemovePlayerItem_Pre", false);
for(new i = 0; i < sizeof g_szWeaponEntNames; i++)
{
RegisterHam(Ham_Item_Deploy, g_szWeaponEntNames
, "CWeapon__Item_Deploy_Post", true);
}
}
public plugin_precache()
{
g_aWeaponData = ArrayCreate(e_WeaponData);
new szBuffer[256], szPath[64], iLine, iLen, iFile = fopen(FILE_NAME, "r");
if(!iFile)
{
set_fail_state("| File 'api_backweapons.ini' not found.");
}
get_localinfo("amxx_configsdir", szPath, charsmax(szPath));
formatex(szPath, charsmax(szPath), "%s", FILE_NAME);
new szData[e_WeaponData];
while(read_file(szPath, iLine++, szBuffer, charsmax(szBuffer), iLen))
{
if(szBuffer[0] == '"')
{
parse(szBuffer, szData[DATA], 15, szData[MODEL], 63, szData[BODY], 15);
formatex(szBuffer, charsmax(szBuffer), "%s", szData[MODEL]);
PRECACHE_MODEL(szBuffer);
ArrayPushArray(g_aWeaponData, szData);
}
else
{
continue;
}
}
fclose(iFile);
}
public client_putinserver(iPlayer)
{
static iReference;
if(iReference || (iReference = engfunc(EngFunc_AllocString, "info_target")))
{
g_iUserBackWeaponEntity[iPlayer] = engfunc(EngFunc_CreateNamedEntity, iReference);
if(pev_valid(g_iUserBackWeaponEntity[iPlayer]))
{
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_classname, "backweapon");
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_movetype, MOVETYPE_FOLLOW);
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_effects, EF_NODRAW);
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_aiment, iPlayer);
}
}
}
public client_disconnect(iPlayer)
{
if(g_iUserBackWeaponEntity[iPlayer] > 0 && pev_valid(g_iUserBackWeaponEntity[iPlayer]))
{
REMOVE_ENTITY(g_iUserBackWeaponEntity[iPlayer]);
}
g_iUserBackWeaponEntity[iPlayer] = 0;
}
public CPlayer__Spawn_Post(iPlayer)
{
if(!is_user_alive(iPlayer))
{
return HAM_IGNORED;
}
static iEntity; iEntity = get_pdata_cbase(iPlayer, m_rpgPlayerItems + 1, linux_diff_player);
static iItem; iItem = get_pdata_cbase(iPlayer, m_pActiveItem, linux_diff_player);
if(pev_valid(iEntity) && iEntity)
{
if(ExecuteHamB(Ham_Item_ItemSlot, iItem) != 1)
{
new szData[e_WeaponData];
for(new iPos; iPos <= ArraySize(g_aWeaponData) - 1; iPos++)
{
ArrayGetArray(g_aWeaponData, iPos, szData);
if(pev(iEntity, pev_impulse) == str_to_num(szData[DATA]))
{
new szBuffer[64];
formatex(szBuffer, charsmax(szBuffer), "%s", szData[MODEL]);
engfunc(EngFunc_SetModel, g_iUserBackWeaponEntity[iPlayer], szBuffer);
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_body, str_to_num(szData[BODY]));
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 1);
return HAM_IGNORED;
}
}
}
}
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 0);
return HAM_IGNORED;
}
public CPlayer__AddPlayerItem_Pre(iPlayer, iItem)
{
if(!get_pdata_int(iPlayer, m_iAutoWepSwitch, linux_diff_player))
{
new szData[e_WeaponData];
for(new iPos; iPos <= ArraySize(g_aWeaponData) - 1; iPos++)
{
ArrayGetArray(g_aWeaponData, iPos, szData);
if(pev(iItem, pev_impulse) == str_to_num(szData[DATA]))
{
new szBuffer[64];
formatex(szBuffer, charsmax(szBuffer), "%s", szData[MODEL]);
engfunc(EngFunc_SetModel, g_iUserBackWeaponEntity[iPlayer], szBuffer);
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_body, str_to_num(szData[BODY]));
}
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 1);
}
}
if(ExecuteHamB(Ham_Item_ItemSlot, iItem) != 1)
{
if(!get_pdata_int(iPlayer, m_iAutoWepSwitch, linux_diff_player))
{
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 0);
}
}
}
public CPlayer__RemovePlayerItem_Pre(iPlayer, iItem)
{
new szData[e_WeaponData];
for(new iPos; iPos <= ArraySize(g_aWeaponData) - 1; iPos++)
{
ArrayGetArray(g_aWeaponData, iPos, szData);
if(pev(iItem, pev_impulse) == str_to_num(szData[DATA]))
{
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 0);
}
}
}
public CWeapon__Item_Deploy_Post(iItem)
{
static iPlayer; iPlayer = get_pdata_cbase(iItem, m_pPlayer, linux_diff_weapon);
static iEntity; iEntity = get_pdata_cbase(iPlayer, m_rpgPlayerItems + 1, linux_diff_player);
if(iEntity > 0)
{
if(ExecuteHamB(Ham_Item_ItemSlot, iItem) != 1)
{
new szData[e_WeaponData];
for(new iPos; iPos <= ArraySize(g_aWeaponData) - 1; iPos++)
{
ArrayGetArray(g_aWeaponData, iPos, szData);
if(pev(iEntity, pev_impulse) == str_to_num(szData[DATA]))
{
new szBuffer[64];
formatex(szBuffer, charsmax(szBuffer), "%s", szData[MODEL]);
engfunc(EngFunc_SetModel, g_iUserBackWeaponEntity[iPlayer], szBuffer);
set_pev(g_iUserBackWeaponEntity[iPlayer], pev_body, str_to_num(szData[BODY]));
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 1);
return;
}
}
}
}
fm_set_entity_visibility(g_iUserBackWeaponEntity[iPlayer], 0);
}
stock fm_set_entity_visibility(iEntity, iVisible = 1)
{
set_pev(iEntity, pev_effects, iVisible == 1 ? pev(iEntity, pev_effects) & ~EF_NODRAW : pev(iEntity, pev_effects) | EF_NODRAW);
}