Page 1 of 1

(ReAPI) BackWeapons (solved)

Posted: 21 Jan 2018, 20:12
by ArminC
As I saw eariler.. I wana make a request. Optimize this plugin trough ReAPI and make it to use the models of the weapons (somehow rotated) on the backside, without needing of puting the model backweapons.mdl (as cheapsuit said he didn't tried but it should work). (and to make it compatibile with ZP/normal)

Code: Select all

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <biohazard>

#define PLUGIN	"Back Weapons"
#define AUTHOR	"hoboman313/cheap_suit"
#define VERSION	"1.87"

#define MAX_PLAYERS 		32
#define OFFSET_PRIMARYWEAPON 	116
#define OFFSET_WEAPONTYPE 	43
#define EXTRAOFFSET_WEAPONS 	4
#define OFFSET_AUTOSWITCH 	509
#define OFFSET_SHIELD 		510
#define HAS_SHIELD 		(1<<24)

#define PRIMARY_WEAPONS (1<<CSW_SCOUT | 1<<CSW_XM1014 | 1<<CSW_MAC10 | 1<<CSW_AUG | 1<<CSW_UMP45 | 1<<CSW_SG550 | 1<<CSW_GALIL | 1<<CSW_FAMAS | 1<<CSW_AWP | 1<<CSW_MP5NAVY | 1<<CSW_M249 | 1<<CSW_M3 | 1<<CSW_M4A1 | 1<<CSW_TMP | 1<<CSW_G3SG1 | 1<<CSW_SG552 | 1<<CSW_AK47 | 1<<CSW_P90)

#define is_weapon_primary(%1)      (PRIMARY_WEAPONS & (1<<%1))
#define cs_get_weapon_type(%1)     get_pdata_int(%1, OFFSET_WEAPONTYPE, EXTRAOFFSET_WEAPONS)
#define cs_get_user_hasprim(%1)    get_pdata_int(%1, OFFSET_PRIMARYWEAPON)
#define cs_get_user_autoswitch(%1) get_pdata_int(%1, OFFSET_AUTOSWITCH)
#define cs_get_user_shield(%1)	   (get_pdata_int(%1, OFFSET_SHIELD) & HAS_SHIELD) ? 1 : 0

enum
{
	MODEL_NULL    = 0,
	MODEL_AUG     = 1,
	MODEL_AK47    = 2,
	MODEL_AWP     = 3,
	MODEL_MP5NAVY = 4,
	MODEL_P90     = 5,
	MODEL_GALIL   = 6,
	MODEL_M4A1    = 7,
	MODEL_SG550   = 8,
	MODEL_SG552   = 9,
	MODEL_SCOUT   = 10,
	MODEL_XM1014  = 11,
	MODEL_M3      = 12,
	MODEL_G3SG1   = 13,
	MODEL_M249    = 14,
	MODEL_FAMAS   = 15,
	MODEL_UMP45   = 16
}

new g_weapons[][] =
{	
	"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"
}

new g_weaponclass[] = "backweapon"
new g_weaponmodel[] = "models/backweapons.mdl"

new g_weaponent[MAX_PLAYERS+1]

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_cvar(PLUGIN, VERSION, FCVAR_SPONLY|FCVAR_SERVER)
	
	RegisterHam(Ham_Killed,           "player", "bacon_killed")
	RegisterHam(Ham_Spawn,            "player", "bacon_spawn_post", 1)
	RegisterHam(Ham_AddPlayerItem,    "player", "bacon_addplayeritem")
	RegisterHam(Ham_RemovePlayerItem, "player", "bacon_removeplayeritem")
	
	for(new i = 0; i < sizeof g_weapons; i++)
	{
		RegisterHam(Ham_Item_AttachToPlayer, g_weapons[i], "bacon_item_attachtoplayer_post", 1)
		RegisterHam(Ham_Item_Deploy,         g_weapons[i], "bacon_item_deploy_post",         1)
	}
}

public plugin_precache()
	precache_model(g_weaponmodel)

public client_putinserver(id)
{
	static infotarget 
	if(!infotarget) infotarget = engfunc(EngFunc_AllocString, "info_target")
	
	g_weaponent[id] = engfunc(EngFunc_CreateNamedEntity, infotarget)
	if(pev_valid(g_weaponent[id]))
	{
		engfunc(EngFunc_SetModel, g_weaponent[id], g_weaponmodel)
		set_pev(g_weaponent[id], pev_classname, g_weaponclass)
		set_pev(g_weaponent[id], pev_movetype, MOVETYPE_FOLLOW)
		set_pev(g_weaponent[id], pev_effects, EF_NODRAW)
		set_pev(g_weaponent[id], pev_aiment, id)
	}
}

public client_disconnect(id)
{
	if(g_weaponent[id] > 0 && pev_valid(g_weaponent[id]))
		engfunc(EngFunc_RemoveEntity, g_weaponent[id])
	
	g_weaponent[id] = 0
}

public bacon_killed(id, idattacker, shouldgib)
	fm_set_entity_visibility(g_weaponent[id], 0)

public bacon_addplayeritem(id, ent)
{
	static weaponid; weaponid = cs_get_weapon_type(ent)
	if(is_weapon_primary(weaponid) && pev_valid(g_weaponent[id]))
	{
		fm_set_entity_visibility(g_weaponent[id], 0)
		set_pev(g_weaponent[id], pev_body, get_weapon_model(weaponid))
	}
}

public bacon_removeplayeritem(id, ent)
{
	if(is_weapon_primary(cs_get_weapon_type(ent)) && pev_valid(g_weaponent[id]))
		fm_set_entity_visibility(g_weaponent[id], 0)
}

public bacon_spawn_post(id) if(is_user_alive(id))
{
	if(!cs_get_user_hasprim(id))
		fm_set_entity_visibility(g_weaponent[id], 0)
}

public bacon_item_attachtoplayer_post(ent, id) if(is_user_alive(id) && !cs_get_user_autoswitch(id))
{
	if(is_weapon_primary(cs_get_weapon_type(ent)) && pev_valid(g_weaponent[id]))
		fm_set_entity_visibility(g_weaponent[id], 1)
}

public bacon_item_deploy_post(ent)
{
	static id; id = pev(ent, pev_owner)
	if(is_user_alive(id)) 
	{
		static weapon; weapon = cs_get_weapon_type(ent)
		if(is_weapon_primary(weapon) || cs_get_user_shield(id))
			fm_set_entity_visibility(g_weaponent[id], 0)
		
		else if(cs_get_user_hasprim(id))
			fm_set_entity_visibility(g_weaponent[id], 1)
	}
}

stock get_weapon_model(weapon)
{
	switch(weapon)
	{
		case CSW_SCOUT:   return MODEL_SCOUT
		case CSW_XM1014:  return MODEL_XM1014
		case CSW_AUG:	  return MODEL_AUG
		case CSW_UMP45:   return MODEL_UMP45
		case CSW_SG550:   return MODEL_SG550
		case CSW_GALIL:   return MODEL_GALIL
		case CSW_FAMAS:   return MODEL_FAMAS
		case CSW_AWP:     return MODEL_AWP
		case CSW_MP5NAVY: return MODEL_MP5NAVY
		case CSW_M249:    return MODEL_M249
		case CSW_M3:      return MODEL_M3
		case CSW_M4A1:    return MODEL_M4A1
		case CSW_G3SG1:   return MODEL_G3SG1
		case CSW_SG552:   return MODEL_SG552
		case CSW_AK47:    return MODEL_AK47
		case CSW_P90:     return MODEL_P90
	}
	return 0
}

public event_infect2(id)
{	
	fm_set_entity_visibility(g_weaponent[id], 0)
}
stock fm_set_entity_visibility(index, visible = 1) 
	set_pev(index, pev_effects, visible == 1 ? pev(index, pev_effects) & ~EF_NODRAW : pev(index, pev_effects) | EF_NODRAW)
-- Here is a fixed edition by Connor

Code: Select all

include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#if defined TEST_3RD
#include <engine>
#endif

#define VERSION "0.0.6"

const MAX_MODEL_PATH_LENGTH = 64 // "models/backweapons2.mdl" "backweapons2_css_ns.mdl"

const XO_CBASEPLAYERITEM = 4
const m_pPlayer = 41
const m_pNext = 42
const m_iId = 43

const XO_CBASEPLAYERWEAPON = 4
const m_fWeaponState = 74
// enum ( <<=1 )
// {
	// WEAPONSTATE_USP_SILENCED = 1,
	// WEAPONSTATE_GLOCK18_BURST_MODE,
	// WEAPONSTATE_M4A1_SILENCED,
	// WEAPONSTATE_ELITE_LEFT,
	// WEAPONSTATE_FAMAS_BURST_MODE,
	// WEAPONSTATE_SHIELD_DRAWN
// }
const WEAPONSTATE_M4A1_SILENCED = 1<<2

new const m_rgpPlayerItems_CBasePlayer_1 = 368;
const m_pActiveItem = 373;

const BODY_M4A1_SILENCED = 7;
const BODY_M4A1_NORMAL = 19;

new const g_iModelIndexLookupTable[CSW_P90+1] = {
	0, // 
	0, // CSW_P228
	0, // 
	10, // CSW_SCOUT
	0, // CSW_HEGRENADE
	11, // CSW_XM1014
	0, // CSW_C4
	18, // CSW_MAC10
	1, // CSW_AUG
	0, // CSW_SMOKEGRENADE
	0, // CSW_ELITE
	0, // CSW_FIVESEVEN
	16, // CSW_UMP45
	8, // CSW_SG550
	6, // CSW_GALIL
	15, // CSW_FAMAS
	0, // CSW_USP
	0, // CSW_GLOCK18
	3, // CSW_AWP
	4, // CSW_MP5NAVY
	14, // CSW_M249
	12, // CSW_M3
	BODY_M4A1_NORMAL, // CSW_M4A1
	17, // CSW_TMP
	13, // CSW_G3SG1
	0, // CSW_FLASHBANG
	0, // CSW_DEAGLE
	9, // CSW_SG552
	2, // CSW_AK47
	0, // CSW_KNIFE
	5 // CSW_P90
}

new g_iMaxPlayers
#define IsPlayer(%1)	( 1 <= %1 <= g_iMaxPlayers )
#define CHECK_PLAYER_ALIVE(%1)	( IsPlayer(%1) && is_user_alive(%1) )

new g_szModel[MAX_MODEL_PATH_LENGTH]
new bool:g_bNoSil, g_iMaxWeapons = 18

public plugin_init()
{
	register_plugin("BackWeapon 3", VERSION, "ConnorMcLeod")

	new szWeaponName[32]
	for(new iId=CSW_P228; iId<=CSW_P90; iId++)
	{
		if( g_iModelIndexLookupTable[iId] && get_weaponname(iId, szWeaponName, charsmax(szWeaponName)) )
		{	
			RegisterHam( Ham_Item_AttachToPlayer , szWeaponName , "Primary_AttachToPlayer_Post" , true )
			RegisterHam( Ham_Item_Holster , szWeaponName , "Primary_Holster_Post" , true )
			RegisterHam( Ham_Item_Deploy , szWeaponName , "Primary_Deploy_Pre" , false )
		}
	}
	RegisterHam( Ham_RemovePlayerItem , "player" , "Player_RemovePlayerItem_Post" , true)

	g_iMaxPlayers = get_maxplayers()

#if defined TEST_3RD
	RegisterHam( Ham_Spawn , "player" , "Player_Spawn_Post" , true)
}

public Player_Spawn_Post(id)
{
	if( is_user_alive(id) && !is_user_bot(id) )
	{
		set_view(id, CAMERA_3RDPERSON)
	}
#endif
}

public plugin_precache()
{
	new szConfigFile[64] // "addons/amxmodx/configs/backweapons.ini" 38
	get_localinfo("amxx_configsdir", szConfigFile, charsmax(szConfigFile))
	add(szConfigFile, charsmax(szConfigFile), "/backweapons.ini")

	new fp = fopen(szConfigFile, "rt")
	if( fp )
	{
		new szDatas[MAX_MODEL_PATH_LENGTH+22], szKey[16], szValue[MAX_MODEL_PATH_LENGTH]
		while( !feof(fp) )
		{
			fgets(fp, szDatas, charsmax(szDatas))
			trim(szDatas)
			if(!szDatas[0] || szDatas[0] == ';' || szDatas[0] == '#' || szDatas[0] == '/')
			{
				continue
			}
			parse(szDatas, szKey, charsmax(szKey), szValue, charsmax(szValue))
			if( equali(szKey, "bw_model") )
			{
				copy(g_szModel, charsmax(g_szModel), szValue)
			}
			else if( equali(szKey, "bw_nosil") )
			{
				if( szValue[0] == '1' )
				{
					g_bNoSil = true
				}
			}
			else if( equali(szKey, "bw_maxweapons") )
			{
				g_iMaxWeapons = str_to_num( szValue )
			}
		}
	}
	else
	{
		copy(g_szModel, charsmax(g_szModel), "models/backweapons2.mdl")
	}

	if( !file_exists(g_szModel) )
	{
		set_fail_state("Wrong model file, check backweapons.ini file !")
		return // ? shouldn't be needed
	}
	precache_model(g_szModel)

#if defined TEST_3RD
	precache_model("models/rpgrocket.mdl")
#endif
}

public Primary_AttachToPlayer_Post(iEnt, id)
{
	if( !CHECK_PLAYER_ALIVE(id) )
	{
		return
	}

	engfunc(EngFunc_SetModel, iEnt, g_szModel) // SetModel doesn't seem to alloc

	set_pev(iEnt, pev_body, g_iModelIndexLookupTable[ get_pdata_int(iEnt, m_iId, XO_CBASEPLAYERITEM) ])

	if(	get_pdata_cbase(id, m_pActiveItem) != iEnt	)
	{
		CheckWeapons(id)
	}
}

public Primary_Holster_Post( iEnt )
{
	new id = get_pdata_cbase(iEnt, m_pPlayer, XO_CBASEPLAYERITEM)
	if( CHECK_PLAYER_ALIVE(id) )
	{
		CheckWeapons(id)
	}
}

public Player_RemovePlayerItem_Post(id, iEnt)
{
	if( ExecuteHam(Ham_Item_ItemSlot, iEnt) == 1 )
	{
		CheckWeapons(id, get_pdata_cbase(id, m_pActiveItem))
	}
}

public Primary_Deploy_Pre( iEnt )
{
	new id = get_pdata_cbase(iEnt, m_pPlayer, XO_CBASEPLAYERITEM)
	if( CHECK_PLAYER_ALIVE(id) )
	{
		CheckWeapons(id, iEnt)
	}
}

CheckWeapons(id, iSkipEnt = FM_NULLENT)
{
	new iMaxWeapons = g_iMaxWeapons

	new iWeapon = get_pdata_cbase(id, m_rgpPlayerItems_CBasePlayer_1)

	while( iWeapon > 0 )
	{
		if( iWeapon == iSkipEnt || iMaxWeapons <= 0 )
		{
			set_pev(iWeapon, pev_effects, EF_NODRAW)
		}
		else
		{
			if( g_bNoSil && get_pdata_int(iWeapon, m_iId, XO_CBASEPLAYERITEM) == CSW_M4A1 )
			{
				if( get_pdata_int(iWeapon, m_fWeaponState, XO_CBASEPLAYERWEAPON) & WEAPONSTATE_M4A1_SILENCED )
				{
					set_pev(iWeapon, pev_body, BODY_M4A1_SILENCED)
				}
				else
				{
					set_pev(iWeapon, pev_body, BODY_M4A1_NORMAL)
				}
			}
			set_pev(iWeapon, pev_effects, 0)
			--iMaxWeapons
		}
		iWeapon = get_pdata_cbase(iWeapon, m_pNext, XO_CBASEPLAYERITEM)
	}
}