EStatus ActionShoot(const float frametime, CBot* bot, bool shotgun)
{
	bot->SetSpeed( 0.0f );

	bot->SetState(ai::EBotState::BotStateShooting);

	if ( bot->Aim(frametime) )
	{
		CEntityList* entList = CEntityList::GetInstance();
		CEntity* ent = entList->GetEntity( bot->GetTargetID() );

		if ( ent != NULL )
		{
			XMFLOAT3 ray = utl::Normalise( utl::Subtract( bot->GetPosition(), ent->GetPosition() ) );

			if (CheckLOS(bot, ray, entList) && bot->GetAmmo(shotgun) != 0)
			{
				bot->Shoot( frametime, shotgun );
				return Pass;
			}
			else
			{
				return Fail;
			}
		}
	}
	else
	{
		return Running;
	}

	return Fail;
}
EStatus ConditionInRange(CBot* bot)
{
	CEntity* target = CEntityList::GetInstance()->GetEntity( bot->GetTargetID() );

	if (target == NULL)
	{
		return Error;
	}

	if (utl::DistanceToSquared(bot->GetPosition(), target->GetPosition()) < gInRange)
	{
		return Pass;
	}
	return Fail;
}
EStatus ConditionLongRange(CBot* bot)
{
	CEntity* target = CEntityList::GetInstance()->GetEntity( bot->GetTargetID() );

	if (target == NULL)
	{
		return Error;
	}

	float distSq = utl::DistanceToSquared(bot->GetPosition(), target->GetPosition());

	if (distSq > gCloseRange)
	{
		return Pass;
	}
	return Fail;
}
bool CheckLOS(CBot* bot, const XMFLOAT3 &ray, CEntityList* entList)
{
	XMFLOAT3 rayPoint = bot->GetPosition();
	bool end = false;
	CEntity* ent;
	int count = 0;
	int numOfEntities = entList->GetNumberOfEntities();
	unsigned int botID = bot->GetUID();

	while (!end)
	{
		rayPoint.x += (ray.x * gRayInc);
		rayPoint.z += (ray.z * gRayInc);

		if (count == 150)
		{
			return false;
		}

		for (int i = 0; i < numOfEntities; i++)
		{
			ent = entList->GetEntityAtIndex(i);

			if (ent != NULL && ent->GetUID() != botID)
			{
				// Checks collision between ray and object
				if (typeid(*(ent->GetBoundingArea())) == typeid(CBoundingSphere))
				{
					float radius = ((CBoundingSphere*) ent->GetBoundingArea())->GetRadius();

					end = PointToSphere( rayPoint, ent->GetPosition(), radius );
				}
				else if (typeid(*(ent->GetBoundingArea())) == typeid(CBoundingBox))
				{
					SBoundingBoxData bbox = ((CBoundingBox*) ent->GetBoundingArea())->GetBoxData();

					end = PointToBox( rayPoint, ent->GetPosition(), bbox );
				}
				else
				{
					end = false;
				}

				// if the light of sight has found an object and it exists
				if (end && typeid(*ent) != typeid (CFlag) && typeid(*ent) != typeid (CBullet))
				{
					if (typeid(*ent) == typeid(CBot) || typeid(*ent) == typeid(CPlayer))
					{
						if (((CBot*)ent)->GetTeam() == bot->GetTeam())
						{
							return false;
						}
						else
						{
							bot->SetTargetID(ent->GetUID());
							return true;
						}
					}
					else
					{
						return false;
					}
				}
			}
		}
		count++;
	}

	return false;
}