#include "stdafx.h"
#include "CircularLocation.h"

bool CircularLocation::isInLocation(BWAPI::TilePosition tile) const
{
  // Intersection circle/square

  // TODO: improve this
  BWAPI::Position tileCenter(tile.x() * TILE_SIZE + TILE_SIZE/2,
                             tile.y() * TILE_SIZE + TILE_SIZE/2);
  if (m_center.getApproxDistance(BWAPI::Position(tileCenter.x() - TILE_SIZE/2,
                                                 tileCenter.y() - TILE_SIZE/2)) <= m_radius) return true;
  if (m_center.getApproxDistance(BWAPI::Position(tileCenter.x() - TILE_SIZE/2,
                                                 tileCenter.y() + TILE_SIZE/2)) <= m_radius) return true;
  if (m_center.getApproxDistance(BWAPI::Position(tileCenter.x() + TILE_SIZE/2,
                                                 tileCenter.y() - TILE_SIZE/2)) <= m_radius) return true;
  if (m_center.getApproxDistance(BWAPI::Position(tileCenter.x() + TILE_SIZE/2,
                                                 tileCenter.y() + TILE_SIZE/2)) <= m_radius) return true;
  if (is_between<int, true>(tileCenter.x() - TILE_SIZE/2, tileCenter.x() + TILE_SIZE/2)(m_center.x()))
  {
    if (abs(m_center.y() - tileCenter.y()) <= m_radius) return true;
  }
  if (is_between<int, true>(tileCenter.y() - TILE_SIZE/2, tileCenter.y() + TILE_SIZE/2)(m_center.y()))
  {
    if (abs(m_center.x() - tileCenter.x()) <= m_radius) return true;
  }
  return false;
}
