
#include "MW4Headers.hpp"

#include "AI_MoodSquad.hpp"

#include "ai.hpp"
#include "AI_Groups.hpp"
#include "AI_UserConstants.hpp"



using namespace MW4AI;
using namespace MW4AI::Squad;
using namespace MechWarrior4;


MoodSquad::MoodSquad()
	: m_LastMoodUpdateTime(0)
	, m_NumDeadUnits(0)
{
}

ID MoodSquad::GetID() const
{
	return (GROUPAI_MOODSQUAD);
}

void MoodSquad::Update(MechWarrior4::Group& group, MechWarrior4::CombatAI& combat_ai)
{
	inherited::Update(group,combat_ai);

	if (m_LastMoodUpdateTime >= gos_GetElapsedTime())
	{
		return;
	}

	const int num_dead_units = Groups::NumDead(group.GetID());

	if ((m_LastMoodUpdateTime != 0) &&
		(num_dead_units > m_NumDeadUnits))
	{
		std::vector<MWObject*> groupmates;
		group.GetMembers(groupmates);

		if (groupmates.size() > 0)
		{
			const int dead_unit_delta = m_NumDeadUnits - num_dead_units;
			const Stuff::Scalar mood_delta = ((Stuff::Scalar)dead_unit_delta / groupmates.size()) * UserConstants::Instance()->Get(UserConstants::mood_squad_dead_unit_multiplier);

			LowerEveryonesMood(mood_delta,groupmates);
		}
	}

	m_NumDeadUnits = num_dead_units;
	m_LastMoodUpdateTime = gos_GetElapsedTime();
}

void MoodSquad::LowerEveryonesMood(Stuff::Scalar mood_delta, std::vector<MWObject*>& groupmates)
{
	{for (std::vector<MWObject*>::const_iterator i = groupmates.begin();
		  i != groupmates.end();
		  ++i)
	{
		if ((*i)->GetAI() != 0)
		{
			Stuff::Scalar mood = (*i)->GetAI()->CurrentMood();

			mood += mood_delta;
			if (mood < MW4AI::MOODMIN)
			{
				mood = MW4AI::MOODMIN;
			}

      if (mood > MW4AI::MOODMAX)
      {
        mood = MW4AI::MOODMAX;
      }

			(*i)->GetAI()->CurrentMood(mood);
		}
	}}
}

void MoodSquad::NotifyShot(MechWarrior4::Group& group, Adept::ObjectID id)
{
	inherited::NotifyShot(group,id);
}

void MoodSquad::NotifyShotFired(MechWarrior4::Group& group, const Stuff::Line3D& line, MechWarrior4::MWObject& at_who, MechWarrior4::MWObject& shooter)
{
	inherited::NotifyShotFired(group,line,at_who,shooter);
}

void MoodSquad::NotifyMemberAdded(MechWarrior4::Group& group, MechWarrior4::MWObject& who)
{
	inherited::NotifyMemberAdded(group,who);
}

void MoodSquad::NotifyMemberRemoved(MechWarrior4::Group& group, MechWarrior4::MWObject& who)
{
	inherited::NotifyMemberRemoved(group,who);
}

void MoodSquad::SetEntityToIgnore(Adept::ObjectID objectid)
{
}
