/***********************************************************************
		XP FUNCTIONS (idea from war3x)
***********************************************************************/
stock XP_give(id, iXP){

	// Check for min. players here
	console_print(id,"### Could Gained: %d", iXP)

	new theplayers[32], CTnum, Tnum
	get_players(theplayers,CTnum,"e","CT") 
	get_players(theplayers,Tnum,"e","TERRORIST")

	if(MINIMUM_PLAYERS > 0){
		if( !(Tnum>0 && CTnum>0 && (Tnum+CTnum)>=MINIMUM_PLAYERS))
			return PLUGIN_HANDLED
	}

	console_print(id,"### XP Gained: %d", floatround(iXP * XPMULTIPLIER))

	p_data[id][P_XP] += floatround(iXP * XPMULTIPLIER)

	displaylevel(id,1)

	return iXP
}

stock XP_onDeath(victim_id, killer_id, weapon, headshot){

	// Don't award XP to world
	if(killer_id==0)
		return PLUGIN_CONTINUE

	new victimName[32], iXP, XP
	get_user_name(victim_id,victimName,31)

	if (killer_id && killer_id!=victim_id && victim_id){

		// He killed a teammate
		if (get_user_team(killer_id)==get_user_team(victim_id)){
			iXP = -1 * xpgiven[p_data[killer_id][P_LEVEL]]
			XP = XP_give(killer_id,iXP)
			if (FT_KILL_OBJECTIVES){
				client_print(killer_id,print_chat,"%L",killer_id,"AWARDED_FOR_KILLING_TEAMMATE",g_MOD,XP)
			}
		}
		else{

			if (!get_cvar_num("mp_weaponxpmodifier")){
				iXP = xpgiven[p_data[victim_id][P_LEVEL]]
				XP = XP_give(killer_id,iXP)
				if (FT_KILL_OBJECTIVES){			
					client_print(killer_id,print_chat,"%L",killer_id,"AWARDED_FOR_KILLING",g_MOD,XP,victimName)
				}
			}
			else{
				iXP = floatround(xpgiven[p_data[victim_id][P_LEVEL]] * weaponxpmultiplier[weapon])
				XP = XP_give(killer_id,iXP)
				if (FT_KILL_OBJECTIVES){
					client_print(killer_id,print_chat,"%L",killer_id,"AWARDED_FOR_KILLING",g_MOD,XP,victimName)
				}
			}

			if( headshot && killer_id!=victim_id){
				iXP = HEADSHOT_BONUS
				XP = XP_give(killer_id,iXP)
				if (FT_KILL_OBJECTIVES){
					client_print(killer_id,print_chat,"%L",killer_id,"AWARDED_FOR_HEADSHOT", g_MOD, XP)			
				}
			}

		#if MOD == 0
			if (victim_id==g_hostageSaver){
				iXP = KILLRESCUEMANXP
				XP = XP_give(killer_id,iXP)
				if (FT_OBJECTIVES){	
					client_print(killer_id,print_chat,"%L",killer_id,"AWARDED_FOR_KILLING_HOSTAGE_R",g_MOD, XP)
				}
			}
			else if (victim_id==g_bombDefuser && killer_id!=g_bombDefuser){						
				iXP = DEFUSER_KILL_BONUS
				XP = XP_give(killer_id,iXP)
				if (FT_OBJECTIVES){				
					client_print(killer_id,print_chat, "%L",killer_id,"AWARDED_FOR_KILLING_BOMB_D",g_MOD,XP)
				}	
			}
			else if (victim_id==g_bombCarrier && killer_id!=g_bombCarrier){						
				iXP = KILL_BOMB_CARRIER
				XP = XP_give(killer_id,iXP)
				if (FT_OBJECTIVES){
					client_print(killer_id,print_chat, "%L",killer_id,"AWARDED_FOR_KILLING_BOMB_C",g_MOD,XP)
				}
			}
			else if (victim_id==g_vipID && killer_id!=g_vipID){
				iXP = VIP_KILL_BONUS
				XP = XP_give(killer_id,iXP)
				if (FT_OBJECTIVES){
					client_print(killer_id,print_chat, "%L",killer_id,"AWARD_FOR_KILLING_VIP",g_MOD,XP)
				}
			}

		#endif
		}
	}

	return PLUGIN_CONTINUE
}

public XP_set(){
	#if ADVANCED_DEBUG == 1
		writeDebugInfo("XP_set",0)
	#endif

	if (get_cvar_num("mp_savexp")){
		xpgiven = {6,8,10,12,14,16,20,24,28,32,40}
		xplevel = {0,100,200,400,800,1600,3200,6400,12800,25600,51200}
	}
}
