crx_ak47_models

Pluginuri pentru modul AmxModX.
Post Reply
in_so@pta
Administrator
Posts: 1037
Joined: Mon Jul 06, 2009 7:38 am
Jucator SA:MP ?: Da
Nick:: in_so@pta
Jucator CS ?: Da
Contact:

crx_ak47_models

Post by in_so@pta » Wed Mar 31, 2021 4:51 am

Code: Select all

#include <amxmodx>
#include <amxmisc>
#include <cromchat>
#include <fakemeta>
#include <hamsandwich>
#include <nvault>

native crxranks_get_max_levels()
native crxranks_get_rank_by_level(level, buffer[], len)
native crxranks_get_user_level(id)
native crxranks_get_user_xp(id)

new const g_szNatives[][] =
{
	"crxranks_get_max_levels",
	"crxranks_get_rank_by_level",
	"crxranks_get_user_level",
	"crxranks_get_user_xp"
}

#if !defined m_pPlayer
	#define m_pPlayer 41
#endif

#if defined client_disconnected
	#define client_disconnect client_disconnected
#endif

#define PLUGIN_VERSION "2.1.4"
#define DEFAULT_V "models/v_ak47.mdl"
#define DEFAULT_P "models/p_ak47.mdl"
#define MAX_SOUND_LENGTH 128
#define MAX_AUTHID_LENGTH 35

#if !defined MAX_NAME_LENGTH
	#define MAX_NAME_LENGTH 32
#endif

#if !defined MAX_PLAYERS
	#define MAX_PLAYERS 32
#endif

enum _:AK47
{
	NAME[MAX_NAME_LENGTH],
	V_MODEL[MAX_SOUND_LENGTH],
	P_MODEL[MAX_SOUND_LENGTH],
	SELECT_SOUND[MAX_SOUND_LENGTH],
	FLAG,
	LEVEL,
	bool:SHOW_RANK,
	XP
}

new Array:g_aAK47,
	bool:g_bFirstTime[MAX_PLAYERS + 1],
	bool:g_bRankSystem,
	bool:g_bGetLevel,
	bool:g_bGetXP,
	g_eAK47[MAX_PLAYERS + 1][AK47],
	g_szAuth[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH],
	g_iAK47[MAX_PLAYERS + 1],
	g_iCallback,
	g_pAtSpawn,
	g_pSaveChoice,
	g_iSaveChoice,
	g_iAK47Num,
	g_iVault

public plugin_init()
{
	register_plugin("AK47 Models", PLUGIN_VERSION, "OciXCrom")
	register_cvar("CRXAK47Models", PLUGIN_VERSION, FCVAR_SERVER|FCVAR_SPONLY|FCVAR_UNLOGGED)
	
	if(!g_iAK47Num)
		set_fail_state("No AK47s found in the configuration file.")
	
	register_dictionary("AK47Models.txt")
	
	RegisterHam(Ham_Spawn, "player", "OnPlayerSpawn", 1)
	RegisterHam(Ham_Item_Deploy, "weapon_ak47", "OnSelectAK47", 1)
	
	register_clcmd("say /ak47", "ShowMenu")
	register_clcmd("say_team /ak47", "ShowMenu")
	
	g_iCallback = menu_makecallback("CheckAK47Access")
	g_pAtSpawn = register_cvar("am_open_at_spawn", "0")
	g_pSaveChoice = register_cvar("am_save_choice", "0")
}

public plugin_precache()
{
	if(LibraryExists("crxranks", LibType_Library))
		g_bRankSystem = true
		
	g_aAK47 = ArrayCreate(AK47)
	ReadFile()
}

public plugin_cfg()
{
	g_iSaveChoice = get_pcvar_num(g_pSaveChoice)
	
	if(g_iSaveChoice)
		g_iVault = nvault_open("AK47Models")
}

public plugin_natives()
	set_native_filter("native_filter")
	
public native_filter(const szNative[], id, iTrap)
{
	if(!iTrap)
	{
		static i
		
		for(i = 0; i < sizeof(g_szNatives); i++)
		{
			if(equal(szNative, g_szNatives[i]))
				return PLUGIN_HANDLED
		}
	}
	
	return PLUGIN_CONTINUE
}
	
public plugin_end()
{
	ArrayDestroy(g_aAK47)
	
	if(g_iSaveChoice)
		nvault_close(g_iVault)
}

ReadFile()
{
	new szConfigsName[256], szFilename[256]
	get_configsdir(szConfigsName, charsmax(szConfigsName))
	formatex(szFilename, charsmax(szFilename), "%s/AK47Models.ini", szConfigsName)
	new iFilePointer = fopen(szFilename, "rt")
	
	if(iFilePointer)
	{
		new eAK47[AK47], szData[160], szKey[32], szValue[128], iMaxLevels
		
		if(g_bRankSystem)
			iMaxLevels = crxranks_get_max_levels()
		
		while(!feof(iFilePointer))
		{
			fgets(iFilePointer, szData, charsmax(szData))
			trim(szData)
			
			switch(szData[0])
			{
				case EOS, '#', ';': continue
				case '[':
				{
					if(szData[strlen(szData) - 1] == ']')
					{
						if(g_iAK47Num)
							PushAK47(eAK47)
							
						g_iAK47Num++
						replace(szData, charsmax(szData), "[", "")
						replace(szData, charsmax(szData), "]", "")
						copy(eAK47[NAME], charsmax(eAK47[NAME]), szData)
						
						eAK47[V_MODEL][0] = EOS
						eAK47[P_MODEL][0] = EOS
						eAK47[SELECT_SOUND][0] = EOS
						eAK47[FLAG] = ADMIN_ALL
						
						if(g_bRankSystem)
						{
							eAK47[LEVEL] = 0
							eAK47[SHOW_RANK] = false
							eAK47[XP] = 0
						}
					}
					else continue
				}
				default:
				{
					strtok(szData, szKey, charsmax(szKey), szValue, charsmax(szValue), '=')
					trim(szKey); trim(szValue)
					
					if(equal(szKey, "FLAG"))
						eAK47[FLAG] = read_flags(szValue)
					else if(equal(szKey, "LEVEL") && g_bRankSystem)
					{
						eAK47[LEVEL] = clamp(str_to_num(szValue), 0, iMaxLevels)
						
						if(!g_bGetLevel)
							g_bGetLevel = true
					}
					else if(equal(szKey, "SHOW_RANK") && g_bRankSystem)
						eAK47[SHOW_RANK] = _:clamp(str_to_num(szValue), false, true)
					else if(equal(szKey, "XP") && g_bRankSystem)
					{
						eAK47[XP] = _:clamp(str_to_num(szValue), 0)
						
						if(!g_bGetXP)
							g_bGetXP = true
					}
					else if(equal(szKey, "V_MODEL"))
					{
						if(!file_exists(szValue))
							log_amx("ERROR: model ^"%s^" not found!", szValue)
						else
						{
							precache_model(szValue)
							copy(eAK47[V_MODEL], charsmax(eAK47[V_MODEL]), szValue)
						}
					}
					else if(equal(szKey, "P_MODEL"))
					{
						if(!file_exists(szValue))
							log_amx("ERROR: model ^"%s^" not found!", szValue)
						else
						{
							precache_model(szValue)
							copy(eAK47[P_MODEL], charsmax(eAK47[P_MODEL]), szValue)
						}
					}
					else if(equal(szKey, "SELECT_SOUND"))
					{
						precache_sound(szValue)
						copy(eAK47[SELECT_SOUND], charsmax(eAK47[SELECT_SOUND]), szValue)
					}
				}
			}
		}
		
		if(g_iAK47Num)
			PushAK47(eAK47)
		
		fclose(iFilePointer)
	}
}

public client_connect(id)
{
	g_bFirstTime[id] = true
	ArrayGetArray(g_aAK47, 0, g_eAK47[id])
	g_iAK47[id] = 0
	
	if(g_iSaveChoice)
	{
		get_user_authid(id, g_szAuth[id], charsmax(g_szAuth[]))
		UseVault(id, false)
	}
}

public client_disconnect(id)
{
	if(g_iSaveChoice)
		UseVault(id, true)
}

public ShowMenu(id)
{
	static eAK47[AK47]
	new szTitle[128], szItem[128], iLevel, iXP
	formatex(szTitle, charsmax(szTitle), "%L", id, "AM_MENU_TITLE")

	if(g_bGetLevel)
		iLevel = crxranks_get_user_level(id)
	
	if(g_bGetXP)
		iXP = crxranks_get_user_xp(id)
		
	new iMenu = menu_create(szTitle, "MenuHandler")
	
	for(new iFlags = get_user_flags(id), i; i < g_iAK47Num; i++)
	{
		ArrayGetArray(g_aAK47, i, eAK47)
		copy(szItem, charsmax(szItem), eAK47[NAME])
		
		if(g_bRankSystem)
		{
			if(eAK47[LEVEL] && iLevel < eAK47[LEVEL])
			{
				if(eAK47[SHOW_RANK])
				{
					static szRank[32]
					crxranks_get_rank_by_level(eAK47[LEVEL], szRank, charsmax(szRank))
					format(szItem, charsmax(szItem), "%s %L", szItem, id, "AM_MENU_RANK", szRank)
				}
				else
					format(szItem, charsmax(szItem), "%s %L", szItem, id, "AM_MENU_LEVEL", eAK47[LEVEL])
			}
			
			if(eAK47[XP] && iXP < eAK47[XP])
				format(szItem, charsmax(szItem), "%s %L", szItem, id, "AM_MENU_XP", eAK47[XP])
		}
		
		if(eAK47[FLAG] != ADMIN_ALL && !(iFlags & eAK47[FLAG]))
			format(szItem, charsmax(szItem), "%s %L", szItem, id, "AM_MENU_VIP_ONLY")
			
		if(g_iAK47[id] == i)
			format(szItem, charsmax(szItem), "%s %L", szItem, id, "AM_MENU_SELECTED")
		
		menu_additem(iMenu, szItem, eAK47[NAME], eAK47[FLAG], g_iCallback)
	}
	
	if(menu_pages(iMenu) > 1)
	{
		formatex(szItem, charsmax(szItem), "%s%L", szTitle, id, "AM_MENU_TITLE_PAGE")
		menu_setprop(iMenu, MPROP_TITLE, szItem)
	}
		
	menu_display(id, iMenu)
	return PLUGIN_HANDLED
}

public MenuHandler(id, iMenu, iItem)
{
	if(iItem != MENU_EXIT)
	{
		g_iAK47[id] = iItem
		ArrayGetArray(g_aAK47, iItem, g_eAK47[id])
		
		if(is_user_alive(id) && get_user_weapon(id) == CSW_AK47)
			RefreshAK47Model(id)
		
		new szName[MAX_NAME_LENGTH], iUnused
		menu_item_getinfo(iMenu, iItem, iUnused, szName, charsmax(szName), .callback = iUnused)
		CC_SendMessage(id, "%L %L", id, "AM_CHAT_PREFIX", id, "AM_CHAT_SELECTED", szName)
		
		if(g_eAK47[id][SELECT_SOUND][0])
			engfunc(EngFunc_EmitSound, id, CHAN_AUTO, g_eAK47[id][SELECT_SOUND], 1.0, ATTN_NORM, 0, PITCH_NORM)
	}
	
	menu_destroy(iMenu)
	return PLUGIN_HANDLED
}	

public CheckAK47Access(id, iMenu, iItem)
	return ((g_iAK47[id] == iItem) || !HasAK47Access(id, iItem)) ? ITEM_DISABLED : ITEM_ENABLED

public OnPlayerSpawn(id)
{
	if(is_user_alive(id) && get_pcvar_num(g_pAtSpawn) && !g_iAK47[id] && g_bFirstTime[id])
	{
		g_bFirstTime[id] = false
		ShowMenu(id)
	}
}

public OnSelectAK47(iEnt)
{
	new id = get_pdata_cbase(iEnt, m_pPlayer, 4)
	
	if(is_user_connected(id))
		RefreshAK47Model(id)
}

RefreshAK47Model(const id)
{
	set_pev(id, pev_viewmodel2, g_eAK47[id][V_MODEL])
	set_pev(id, pev_weaponmodel2, g_eAK47[id][P_MODEL])
}

PushAK47(eAK47[AK47])
{
	if(!eAK47[V_MODEL][0])
		copy(eAK47[V_MODEL], charsmax(eAK47[V_MODEL]), DEFAULT_V)
		
	if(!eAK47[P_MODEL][0])
		copy(eAK47[P_MODEL], charsmax(eAK47[P_MODEL]), DEFAULT_P)
		
	ArrayPushArray(g_aAK47, eAK47)
}

bool:HasAK47Access(const id, const iAK47)
{		
	static eAK47[AK47]
	ArrayGetArray(g_aAK47, iAK47, eAK47)
	
	if(g_bRankSystem)
	{
		if(eAK47[LEVEL] && crxranks_get_user_level(id) < eAK47[LEVEL])
			return false
			
		if(eAK47[XP] && crxranks_get_user_xp(id) < eAK47[XP])
			return false
	}
		
	if(eAK47[FLAG] != ADMIN_ALL && !(get_user_flags(id) & eAK47[FLAG]))
		return false
		
	return true
}

UseVault(const id, const bool:bSave)
{
	if(bSave)
	{
		static szData[4]
		num_to_str(g_iAK47[id], szData, charsmax(szData))
		nvault_set(g_iVault, g_szAuth[id], szData)
	}
	else
	{
		static iAK47
		iAK47 = nvault_get(g_iVault, g_szAuth[id])
		
		if(iAK47 > g_iAK47Num)
			iAK47 = 0
		
		if(iAK47 && HasAK47Access(id, iAK47))
		{
			g_iAK47[id] = iAK47
			ArrayGetArray(g_aAK47, iAK47, g_eAK47[id])
			
			if(is_user_alive(id) && get_user_weapon(id) == CSW_AK47)
				RefreshAK47Model(id)
		}
	}
}	

Post Reply

Return to “AmxModX ”