#include "stdafx.h"
#include "War3TypesAndConstants.h"
#include "MMPIcon.h"
#include "MiniMap.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMiniMap::CMiniMap()
{
	m_iMMPVersion = MMP_VERSION_DEFAULT;
}

CMiniMap::~CMiniMap()
{

}

/*
AddIcon
*******
Add a minimap icons in the list. It copies the icon provided.

In:
  const CMMPIcon &mmpIcon: icon to add

Out:
  nothing

Return:
  nothing

*/
void CMiniMap::AddIcon(CMMPIcon mmpIcon)
{
	m_tlistIconList.push_front(mmpIcon);
}

int CMiniMap::GetIconsCount()
{
	return m_tlistIconList.size();
}


/*
GoFirstIcon
***********
Place the current possition to the head.

In:
  nothing

Out:
  nothing

Return:
  nothing

*/
void CMiniMap::GoFirstIcon()
{
	if (!m_tlistIconList.empty())
	{
		m_tposCurrent = m_tlistIconList.begin();
	}
}


/*
GetNext
*******
Returns the next element in the list.

In:
  nothing

Out:
  nothing

Return:
  CMMPIcon: the next element in the list
*/
CMMPIcon CMiniMap::GetNext()
{
	if (!m_tlistIconList.empty())
	{
		return *++m_tposCurrent;
	}
	return *m_tposCurrent;
}


/*
ClearList
*********
Remove all the elements from the list.

In:
  nothing

Out:
  nothing

Return:
  nothing

*/
void CMiniMap::ClearList()
{
	if (!m_tlistIconList.empty())
	{
		m_tlistIconList.clear();
	}
}


/*
DelIcon
*******
Remove an icon from the list.

In:
  const CMMPIcon &mmpIcon: icon to find an remove.

Out:
  nothing

Return:
  nothing

*/
void CMiniMap::DelIcon(CMMPIcon mmpIcon)
{
	if (!m_tlistIconList.empty())
	{
		m_tlistIconList.remove(mmpIcon);
	}
}


/*
GetClosestIcon
**************
Returns the closets icons of the passed coordinates.
Coordinates must be between 0 and 255; middle of the map is 128.
Top left is (0,0), bottom right (255,255).
If no icons was found, the icon returned has an undefined icon type.

In:
  int v_iX: X coordiante (0-255)
  int v_iY: Y coordiante (0-255)

Out:
  nothing

Return:
  nothing

*/
CMMPIcon CMiniMap::GetClosestIcon(int v_iX, int v_iY)
{
	CMMPIcon retMmpIcon, mmpIcon;
	list<CMMPIcon>::iterator pos;
	double dDist;
	double dDistMin;

	// dDistMin set to max distance
	dDistMin = MMP_COORD_MAX*MMP_COORD_MAX;
	// start the search from the first element
	pos = m_tlistIconList.begin();
	// process to all the element of the list
	while(m_tlistIconList.end() != pos)
	{
		mmpIcon = *++pos;
		dDist = (v_iX - mmpIcon.m_iX)*(v_iX - mmpIcon.m_iX) + (v_iY - mmpIcon.m_iY)*(v_iY - mmpIcon.m_iY);
		if (dDist <= dDistMin)
		{
			retMmpIcon = mmpIcon;
			dDistMin = dDist;
		}
	}
	return retMmpIcon;
}
