#include "stdafx.h"
#include "RetreatUnitBehavior.h"

void RetreatUnitBehavior::Goto::onTimer()
{
  if (m_context.m_moveOptions.m_threatEvaluator(m_context.m_unit->getTilePosition()) == 0)
  {
    m_context.terminate(Success);
  }
  else if(m_currentTarget != m_smoothPath.end())
  {
    // If the path is not empty, keep following it
    BWAPI::Position step = getCenter(*m_currentTarget);
    submitMoveCommand(step);
    if(step.getDistance(m_context.m_unit->getPosition()) <=  m_context.m_moveOptions.m_maxDistance)
      ++m_currentTarget;
  }
  else
  {
    // If the path is empty, try planning again
    Path retreatPath = calculateRetreatPath(
      m_context.m_unit,
      m_context.m_moveOptions.m_isObstacle,
      m_context.m_moveOptions.m_threatEvaluator).path;
    m_smoothPath = smoothPath(retreatPath.begin(), retreatPath.end());
    m_currentTarget = m_smoothPath.begin();
  }
}