//	HomingBullet.cpp - Class which makes the bullet home in on the nearest tank.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "GamePlayState.h"
#include "HomingBullet.h"
#include "MiscHelper.h"
#include "Bullet.h"
#include "Tank.h"

#include <cmath>

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BulletDirectionCreator<HomingBullet> HomingBullet::creator;

//------------------------------------------------------------------------------
HomingBullet::HomingBullet():
strength(0.0f),
strengthInc(0.0f)
{
	
}

//------------------------------------------------------------------------------
HomingBullet::~HomingBullet()
{

}

//------------------------------------------------------------------------------
void HomingBullet::alterDirection(TwoFloats& dir,
								  Bullet& bullet,
								  GamePlayState& state)
{
	int i;
	float dist;
	int index = -1;
	float minDist = 10000.0f;
	const TwoFloats& bulletPos(bullet.getPosition());
	const std::wstring& bulletParent = bullet.getParent();

	//Find the nearest tank.
	for(i=0;i<state.getNumTanks();++i)
	{
		shared_ptr<Tank>& tank = state.getTank(i);

		if((tank->getActualName() != bulletParent) && (!tank->getDead()))
		{
			dist = bulletPos.getDistance(tank->getPosition());
			if(dist < minDist)
			{
				minDist = dist;
				index = i;
			}
		}
	}

	//Now point the bullet towards it.
	if(index > -1)
	{
		TwoFloats tempDir;
		shared_ptr<Tank>& tank = state.getTank(index);

		tempDir = (tank->getPosition() - bullet.getPosition()).normal();

		dir = (dir + ((tempDir-dir) * strength)).normal();
	}

	if(strength < 0.75f)
	{
		strength += strengthInc;
		if(strength > 0.75f)
			strength = 0.75f;
	}

	if(strengthInc < 0.04f)
		strengthInc += 0.0002f;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void HomingBulletCreator::apply(GamePlayState& state)
{
	state.getBulletCreator().addDirectionModifier(&HomingBullet::creator);
}
