
public xp_get(xp)
{
	static Float:multiple,new_xp;
	
	if(get_pcvar_num(cvar_savexp) > 1) multiple=Float:get_pcvar_float(cvar_xp_long_term);
	else multiple=Float:get_pcvar_float(cvar_xp_short_term);
	
	new_xp=floatround(xp*multiple);
	
	new players[32],num_ts,num_ct;
	
	get_players(players,num_ts,"eh","TERRORIST");
	get_players(players,num_ct,"eh","CT");
	
	if(get_pcvar_num(cvar_xp_minplayers)/2 >= num_ts+num_ct)
	{
		new_xp=0;
	}
	
	return new_xp;
}

public xp_check(id)
{
	if(g_player_experience[id] < 0)
	{
		g_player_experience[id]=0;
	}
	
	static level_diffrent,level_old;
	new bool:level_down,bool:level_up;
	
	level_old = g_player_level[id];
	
	if(g_player_level[id]!=BLEACH_MAX_LEVELS)
	{
		for (new i = 0; i <= BLEACH_MAX_LEVELS; i++)
		{
			if (g_player_experience[id] >= g_player_next_level[g_player_level[id]])
			{
				g_player_level[id]=i;
				level_up=true;
			}
			else
			{
				break;
			}
		}
	}
	
	if(g_player_level[id] > 1)
	{
		for (new i=BLEACH_MAX_LEVELS; i >= g_player_level[id]; --i)
		{
			if(g_player_experience[id] < g_player_next_level[g_player_level[id]-1])
			{
				g_player_level[id]-=1;
				level_down=true;
			}
			else
			{
				break;
			}
		}
	}
	
	if(level_up || level_down) g_player_max_spirit[id]=BLEACH_MAXIMUM_SPIRIT_POWER+(g_player_level[id]*BLEACH_SPR_PER_LEVEL);
	
	if(level_up)
	{
		if(is_user_alive(id))
		{
			emit_sound(id,CHAN_STATIC,g_sound[BLEACH_SOUND_LEVELUP],VOL_NORM,ATTN_NORM,0,PITCH_NORM);
		}
		
		client_print(id,print_chat,"%L",id,"L_USER_LEVEL_UP",PLUGIN_TAG,g_player_level[id],BLEACH_SP_PER_LEVEL);

		level_diffrent=g_player_level[id]-level_old;
		g_player_skillpoints[id]+=level_diffrent*BLEACH_SP_PER_LEVEL;
		g_player_attributepoints[id]+=level_diffrent*BLEACH_AP_PER_LEVEL;
		menu_print_skills(id);
		level_up=false;
	}
	else if(level_down)
	{
		client_print(id,print_chat,"%L",id,"L_USER_LEVEL_DROP",PLUGIN_TAG,g_player_level[id]);
		
		g_player_skillpoints[id]=g_player_level[id]*BLEACH_SP_PER_LEVEL;
		g_player_attributepoints[id]=g_player_level[id]*BLEACH_AP_PER_LEVEL;
		reset_player_skills(id);
		reset_player_attributes(id);
		level_down=false;
	}
	
	return PLUGIN_CONTINUE;
}

public xp_set(id,amount)
{
	if(amount < 0) amount = 0;
	
	g_player_experience[id] = amount;
	
	xp_check(id);
	print_hud_player(id);
	
	return PLUGIN_CONTINUE;
}

public xp_win_round(team)
{
	new players[MAXPLAYERS],id;
	static xp,num;
	
	xp=xp_get(get_pcvar_num(cvar_xp_round));

	get_players(players,num,"ach");
	for(new i = 0; i < num; i++)
	{
		id=players[i];
		if (get_user_team(id) == team)
		{
			xp_set(id,g_player_experience[id]+xp);
			client_print(id,print_chat,"%s You have been awarded %d experience for winning the round!",PLUGIN_TAG,xp);
		}
	}
	
	return PLUGIN_CONTINUE;
}

public xp_share(id,xp,message[])
{
	new players[MAXPLAYERS],origin_victim[3],distance,xp_bonus;
	static origin[3],num,radius;
	
	get_user_origin(id,origin);
	radius=get_pcvar_num(cvar_xp_share_radius);
	
	get_players(players,num,"ach");
	for(new i = 0; i < num; i++)
	{
		if(get_user_team(id)!=get_user_team(players[i]) || id==players[i])
		{
			continue;
		}
		
		get_user_origin(players[i],origin_victim);
		distance = get_distance(origin,origin_victim);
		
		if(distance < radius)
		{
			xp_bonus=floatround(radius_calucation(origin,origin_victim,radius,xp/2.0,xp/1.5));
			client_print(players[i],print_chat,"%L",players[i],message,PLUGIN_TAG,xp_bonus);
			xp_set(players[i],g_player_experience[players[i]]+xp_bonus);
		}            
	}
	
	return PLUGIN_CONTINUE;
}
