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

#define AUTHOR "G_often"
#define PLUGIN "KnifeMode"
#define VERSION "2.1.9"

#define fm_set_user_nobuy(%1) set_pdata_int(%1, 235, get_pdata_int(%1, 235) & ~(1<<0))

new MaxSlots;
new TeamInfo;
new SayText;
new g_CurWeapon

new amx_gamename;
new amx_knifemode_on; // turn plugin on/off - <1|0>

new g_count[33];

new bool:IsConnected[33];

// Name of teams
new TeamName[][] = 
{
	"",
	"TERRORIST",
	"CT",
	"SPECTATOR"
}
// Colors
enum Color
{
	YELLOW = 1, 
	GREEN, 
	TEAM_COLOR,
	GREY, 
	RED, 
	BLUE, 
}

public plugin_init()
{
	amx_knifemode_on = register_cvar("amx_knifemode_on","1")
	
	register_cvar("knifemode", "0", FCVAR_SERVER|FCVAR_SPONLY)
	register_cvar("knifemode_version", VERSION, FCVAR_SERVER|FCVAR_SPONLY)

	set_cvar_string("knifemode_version", VERSION)
	
	// plugin ON
	if(get_pcvar_num(amx_knifemode_on) == 1)
	{
		register_plugin( PLUGIN, VERSION, AUTHOR );
		set_cvar_num("knifemode", 1)
		
		register_event("HLTV", "EventNewRound", "a", "1=0", "2=0");
		register_logevent("Round_end" , 2 , "1=Round_End")
		register_event("CurWeapon", "EventCurWeapon", "b", "1=1")
		
		RegisterHam(Ham_Touch, "weaponbox", "fw_TouchWeapon")
		RegisterHam(Ham_Touch, "armoury_entity", "fw_TouchWeapon")
		RegisterHam(Ham_Touch, "weapon_shield", "fw_TouchWeapon")
		
		RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
		
		amx_gamename = register_cvar( "amx_gamename", "Knife mode v2.1.9" ); 
		
		TeamInfo = get_user_msgid("TeamInfo");
		SayText = get_user_msgid("SayText");
		MaxSlots = get_maxplayers();
		g_CurWeapon = get_user_msgid("CurWeapon")

		register_forward(FM_GetGameDescription, "GameDesc");

		register_message( get_user_msgid( "StatusIcon" ), "Message_StatusIcon" );
		
	}
	// plugin OFF
	else if(get_pcvar_num(amx_knifemode_on) == 0)
	{
		register_plugin("KnifeMode ( OFF )", VERSION, AUTHOR)
		set_cvar_num("knifemode",0)
	}
}
// block buy
public Message_StatusIcon( iMsgID, iMsgDest, id )
{
	static szMessage[ 8 ]; 
	get_msg_arg_string( 2, szMessage, charsmax( szMessage ) ); 
	
	if ( equal( szMessage, "buyzone" ) && get_msg_arg_int( 1 ) )
	{
		fm_set_user_nobuy( id );
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
} 

// new round
public EventNewRound()
{
	new Players[ 32 ] , iNum
	get_players(Players , iNum)
	
	for (new i = 0 ; i < iNum; i++)
	{
		g_count[Players[ i ]] = 0
	}
}
// end of round
public Round_end()
{
	for (new id = 1; id <= MaxSlots; id++)
	{
		new team = get_user_team(id)
		ColorChat(id, GREY,"[KnifeMode]^x04 In this round you kill %d %s%s" ,g_count[ id ], team == 1 ? "CT" : "TERRORIST", g_count[ id ] != 1 ? "'S" : "")
	}
}

// touch a weapon
public fw_TouchWeapon(weapon, id)
{
	if (!is_user_connected(id))
		return HAM_IGNORED;
	
	if (id && !is_user_bot(id))
		return HAM_SUPERCEDE;
	
	return HAM_IGNORED;
}
// is player entering to the server
public client_putinserver(id)
{
	g_count[id] = 0
	
	set_task(15.0,"MessageWelcome", id)
}

// get gold to kill and get player kills
public fw_PlayerKilled(victim, attacker, Float:damage, damage_type)
{
	if(get_user_team( attacker ))
	{
		g_count[ attacker ]++
	}
}

// ColorChat
public ColorChat(id, Color:type, const msg[], {Float,Sql,Result,_}:...)
{
	static message[256];

	switch(type)
	{
		case YELLOW: 
		{
			message[0] = 0x01;
		}
		case GREEN:
		{
			message[0] = 0x04;
		}
		default:
		{
			message[0] = 0x03;
		}
	}

	vformat(message[1], 251, msg, 4);

	message[192] = '^0';

	new team, ColorChange, index, MSG_Type;
	
	if(!id)
	{
		index = FindPlayer();
		MSG_Type = MSG_ALL;
	
	} else {
		MSG_Type = MSG_ONE;
		index = id;
	}
	
	team = get_user_team(index);	
	ColorChange = ColorSelection(index, MSG_Type, type);

	ShowColorMessage(index, MSG_Type, message);
		
	if(ColorChange)
	{
		Team_Info(index, MSG_Type, TeamName[team]);
	}
}
FindPlayer()
{
	new i = -1;

	while(i <= MaxSlots)
	{
		if(IsConnected[++i])
		{
			return i;
		}
	}

	return -1;
}
ShowColorMessage(id, type, message[])
{
	message_begin(type, SayText, _, id);
	write_byte(id)		
	write_string(message);
	message_end();	
}

Team_Info(id, type, team[])
{
	message_begin(type, TeamInfo, _, id);
	write_byte(id);
	write_string(team);
	message_end();

	return 1;
}

ColorSelection(index, type, Color:Type)
{
	switch(Type)
	{
		case RED:
		{
			return Team_Info(index, type, TeamName[1]);
		}
		case BLUE:
		{
			return Team_Info(index, type, TeamName[2]);
		}
		case GREY:
		{
			return Team_Info(index, type, TeamName[0]);
		}
	}

	return 0;
}
// welcome message
public MessageWelcome(id)
{
	new Name[32]
	get_user_name(id, Name, 31)
	
	ColorChat(id, GREY, "^x04[KnifeMode]^x01 Welcome ^x03^"%s^"^x01, this server is runing ^x03^"%s^"^x01 by ^x03^"%s^"^x01",Name, PLUGIN, AUTHOR)
}
// event when the player change weapon
public EventCurWeapon(id)
{
	if (!is_user_alive(id))
		return;
	
	if (!((1<<read_data(2)) & (1<<CSW_KNIFE)))
	{
		engclient_cmd(id, "weapon_knife")
		
		emessage_begin(MSG_ONE, g_CurWeapon, _, id)
		ewrite_byte(1)
		ewrite_byte(CSW_KNIFE)
		ewrite_byte(-1)
		emessage_end()
	}
}
// change name of game
public GameDesc( )
{ 
	static gamename[ 32 ];
	get_pcvar_string( amx_gamename, gamename, 31 ); 
	forward_return( FMV_STRING, gamename ); 
	
	return FMRES_SUPERCEDE; 
}
