/// Copyright (c) 2010 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Flock.h"
#include <boost\math\constants\constants.hpp>

using namespace TaskBasedFlockSim;



Flock::Flock(size_t boidsCount)	:	m_boidsCount(boidsCount), 
									m_positions(new TestFrameWork::Vector3[boidsCount]), 
									m_velocities(new TestFrameWork::Vector3[boidsCount]),
									m_angles(new float[boidsCount])
{
}

Flock::~Flock(void)
{
}

void Flock::SetMaxSize(size_t boidsCount)
{
	m_maxFlockSize = boidsCount;
}

float Flock::GetMaxSize(void) const
{
	return m_maxFlockSize;
}

void Flock::SetAngleIncrement(float angle)
{
	m_angleIncrement = angle;
}

float Flock::GetAngleIncrement(void) const
{
	return m_angleIncrement;
}

void Flock::SetSeparationWeight(float weight)
{
	m_separationWeight = weight;
}

float Flock::GetSeparationWeight(void) const
{
	return m_separationWeight;
}

void Flock::SetCohesionWeight(float weight)
{
	m_cohesionWeight = weight;
}

float Flock::GetCohesionWeight(void) const
{
	return m_cohesionWeight;
}

void Flock::SetAlignmentWeight(float weight)
{
	m_alignmentWeight = weight;
}

float Flock::GetAlignmentWeight(void) const
{
	return m_alignmentWeight;
}

void Flock::SetTrefoilKnotPathWeight(float weight)
{
	m_trefoilknotPathWeight = weight;
}

float Flock::GetTrefoilKnotPathWeight(void) const
{
	return m_trefoilknotPathWeight;
}

size_t Flock::GetBoidsCount(void) const
{
	return  m_boidsCount;
}

Positions& Flock::GetPositions(void)
{
	return m_positions;
}

Velocities& Flock::GetVelocities(void)
{
	return m_velocities;
}

Angles& Flock::GetAngles(void)
{
	return m_angles;
}

const Positions& Flock::GetPositions(void) const
{
	return m_positions;
}

const Velocities& Flock::GetVelocities(void) const
{
	return m_velocities;
}

const Angles& Flock::GetAngles(void) const
{
	return m_angles;
}

Flock& Flock::operator=(const Flock& flock)
{
	if (&flock == this)
	{
		return *this;
	}
	
	m_positions = flock.m_positions;
	m_velocities = flock.m_velocities;
	m_angles = flock.m_angles;

	m_boidsCount = flock.m_boidsCount;

	m_maxFlockSize = flock.m_maxFlockSize;

	m_angleIncrement = flock.m_angleIncrement;

	m_separationWeight = flock.m_separationWeight;
	m_cohesionWeight = flock.m_cohesionWeight;
	m_alignmentWeight = flock.m_alignmentWeight;
	m_trefoilknotPathWeight = flock.m_trefoilknotPathWeight;

	m_maxSpeed = flock.m_maxSpeed;
	m_separationDistance = flock.m_separationDistance;
	m_groupDistance = flock.m_groupDistance;

	return *this;
}

void Flock::SetMaxSpeed(const TestFrameWork::Vector3& speed)
{
	m_maxSpeed = speed;
}

void Flock::SetSeparationDistance(float distance)
{
	m_separationDistance = distance;
}

void Flock::SetGroupDistance(float distance)
{
	m_groupDistance = distance;
}

const TestFrameWork::Vector3& Flock::GetMaxSpeed(void) const
{
	return m_maxSpeed;
}

float Flock::GetSeparationDistance(void) const
{
	return m_separationDistance;
}

float Flock::GetGroupDistance(void) const
{
	return m_groupDistance;
}

void Flock::Resize(size_t boidsCount)
{
	Positions positions(new TestFrameWork::Vector3[boidsCount]);
	Velocities velocities(new TestFrameWork::Vector3[boidsCount]);
	Angles angles(new float[boidsCount]);

	size_t boidsToCopyCount = std::min(m_boidsCount, boidsCount);
	memcpy(positions.get(), m_positions.get(),  boidsToCopyCount  * sizeof(TestFrameWork::Vector3));
	memcpy(velocities.get(), m_velocities.get(), boidsToCopyCount * sizeof(TestFrameWork::Vector3));
	memcpy(angles.get(), m_angles.get(), boidsToCopyCount * sizeof(float));
		
	m_boidsCount = boidsCount;
	m_positions = positions;
	m_velocities = velocities;
	m_angles = angles;
}
