
public sqrt(num)
{
	new div = num;
	new result = 1;
	
	while (div > result)
	{
		div=(div + result)/2;
		result=num/div;
	}
	
	return div;
}

public random_chance(num,max)
{
	static rand;
	rand=random_num(0,max);
	
	if(num >= rand)
	{
		return true;
	}
	
	return false;
}

public buy_time_over()
{
	g_event_buy_time=false;
	if(task_exists (BLEACH_TASK_BUYTIME,0))
	{
		remove_task(BLEACH_TASK_BUYTIME);
	}
}

public get_cooldown(id,Float:cooldown,Float:last_used,notice)
{
	static Float:time;
	time=get_gametime();
	
	if(time-last_used < cooldown)
	{
		if(notice)
		{
			static time_left;
			time_left = floatround((last_used + cooldown) - time);
			
			if(time_left > 0)
			{
				print_hud_status(id,"%L",id,"L_STATUS_CAST_TIME",time_left);
			}
		}
		return true;
	}

	return false;
}

public remove_status_icon(id)
{
	if(!equal(g_player_statusicon[id],"") && is_user_connected(id))
	{
		create_status_icon(id,0,g_player_statusicon[id],0,0,0);
		g_player_statusicon[id]="";
	}
}

public drop_player_item(id)
{
	if(g_player_item[id] > 0)
	{
		item_reset(id);
		item_drop(id,g_player_item[id]);
		
		g_player_item[id]=0;
		
		print_hud_player(id);
	}
}

public reselect_skills(id)
{
	static money,cost;
	money = cs_get_user_money(id); 
	cost = BLEACH_RESKILL_COST*g_player_level[id];
	
	if(money < cost)
	{
		client_print(id,print_chat,"%L",id,"L_SKILL_MONEY_TO_RESTORE",PLUGIN_TAG,cost);
		return PLUGIN_CONTINUE;
	}
	
	client_print(id,print_chat,"%L",id,"L_SKILL_RESELECTED",PLUGIN_TAG);
	
	g_player_skillpoints[id]=g_player_level[id]*BLEACH_SP_PER_LEVEL;
	reset_player_skills(id);
	restore_cooldowns(id);
	menu_print_skills(id);
	print_hud_player(id);
	cs_set_user_money(id,money-cost,1);
	g_player_skill_reseted[id]=true;
	
	return PLUGIN_CONTINUE;
}

public check_target(id,target)
{
	if(pev_valid(target) && is_user_alive(target))
	{		
		if(get_user_team(id) == get_user_team(target))
		{
			print_hud_status(id,"%L",id,"L_STATUS_TARGET_TEAM");
			return true;
		}
		else if(get_entity_distance(id,target) > 850)
		{
			print_hud_status(id,"%L",id,"L_STATUS_TARGET_FA");
			return true;
		}
	}
	else
	{
		print_hud_status(id,"%L",id,"L_STATUS_NO_TARGET");
		return true;
	}
	
	return false;
}

public set_bleach_damage(id,victim,Float:damage)
{
	static Float:bonus_damage;
	
	if(!is_user_alive(victim))
	{
		return PLUGIN_HANDLED;
	}
	else if(random_chance(g_player_level[victim]*4,floatround(100+damage)))
	{
		print_hud_status(victim,"%L",victim,"L_STATUS_EVADED");
		return PLUGIN_HANDLED;
	}
	else if(g_player_spirit[victim] >= damage)
	{
		if(g_player_spirit_releasing[victim])
		{
			print_hud_status(victim,"%L",victim,"L_STATUS_NULLIFIED");
			return PLUGIN_HANDLED;
		}
		else if(random_chance(g_player_spirit[victim]-floatround(damage),g_player_spirit[id]))
		{
			print_hud_status(victim,"%L",victim,"L_STATUS_KNOCKED_DAMAGE");
			bonus_damage=0.5;
			spirit_set(victim,g_player_spirit[victim]-floatround(damage));
		}
		else
		{
			bonus_damage=0.9;
		}
	}
	else if(g_player_spirit[victim] < random_num(1,floatround(damage)))
	{
		print_hud_status(id,"%L",id,"L_STATUS_CRITICAL");
		bonus_damage=1.5;
	}
	else
	{
		bonus_damage=1.0;
	}
	
	if(damage*bonus_damage >= get_user_health(victim))
	{
		set_msg_block(g_msg_death_msg,BLOCK_ONCE);
		g_player_died_by_skill[victim]=true;
	}
	
	fakedamage(victim,"skill",damage*bonus_damage,GENERIC);
	skill_deathmsg(id,victim);
	
	return PLUGIN_CONTINUE;
}

public get_num_diff(num,num2)
{
	if(num > num2)
	{
		return (num-num2);
	}
	else
	{
		return (num2-num);
	}

	return 0;
}

public bleach_stop()
{
	buy_time_over();
	remove_task(BLEACH_TASK_SPIRIT);
	item_drop_remove();
	save_all_players();
	database_close();
	reset_player_all();
	set_pcvar_num(cvar_bleach,0);
	set_cvar_num("sv_restartround",1);
}

public bleach_start()
{
	set_pcvar_num(cvar_bleach,1);
	set_task(5.0,"spirit_regenerate",BLEACH_TASK_SPIRIT,"",0,"b");
	reset_player_all();
	database_init();
}

public validate_max_speed(id)
{
	if(g_event_freeze_time)
	{
		return PLUGIN_HANDLED;
	}
	else if(g_player_stunned[id] > 0 || g_player_cero_charged[id])
	{
		set_user_maxspeed(id,0.1);
	}
	else if(g_player_negating[id] || g_player_frozen[id])
	{
		if(g_player_negating[id])
		{
			client_cmd(id,"weapon_knife");
		}
		set_user_maxspeed(id,0.1);
	}
	else if(g_player_frosted[id] > 0)
	{
		switch(g_player_frosted[id])
		{
			case 1:
			{
				set_user_maxspeed(id,BLEACH_SKILL_NOVICE_FROSTED);
			}
			case 2:
			{
				set_user_maxspeed(id,BLEACH_SKILL_NORMAL_FROSTED);
			}
			case 3:
			{
				set_user_maxspeed(id,BLEACH_SKILL_GREATER_FROSTED);
			}
			case 4:
			{
				set_user_maxspeed(id,BLEACH_SKILL_MASTER_FROSTED);
			}
		}
	}
	else
	{
		new clip,ammo,Float:speed;
		new weapon = get_user_weapon(id,clip,ammo);
		
		if(cs_get_user_vip(id))
		{
			speed=g_cs_speed_vip;
		}
		else if(g_player_zoomed[id])
		{
			speed=g_cs_weapon_speed_zoom[weapon];
		}
		else
		{
			speed=g_cs_weapon_speed[weapon];
		}
		
		set_user_maxspeed(id,speed+g_player_athletic[id]);
	}
	
	return PLUGIN_CONTINUE;
}

public validate_gravity(id)
{
	new Float:gravity;
	gravity=g_player_acrobatic[id];
	
	if((g_player_item[id] == 3))
	{
		if((gravity >= 0.60))
		{
			set_user_gravity(id,gravity - 0.10);
		}
		else
		{
			set_user_gravity(id,0.50);
		}
	}
	else
	{
		set_user_gravity(id,gravity);
	}
}

public Float:radius_calucation(center_origin[3],origin[3],radius,Float:min_value,Float:max_value)
{
	static Float:percent,distance;
	
	distance = get_distance(center_origin,origin);

	if(distance < 40.0)
	{
		return max_value;
	}
	else
	{
		percent = 1.0 - (distance / radius);
	}

	return min_value + (percent * (max_value - min_value));
}

public random_target(id)
{
	new players[MAXPLAYERS];
	new victims[MAXPLAYERS+1], total_targets = 0;
	static num;
	
	get_players(players,num,"ah");
	for (new i = 0; i < num; i++ )
	{
		if (get_user_team(players[i]) != get_user_team(id))
		{
			victims[total_targets++] = players[i];
		}
	}
	
	if(total_targets == 0)
	{
		return -1;
	}

	new victim = 0, random_id;
	while (victim == 0)
	{
		random_id = random_num(0,total_targets);
		victim = victims[random_id];
	}

	return victim;
}
