#include "stdafx.h"
#include "../../../../Layer1FSM.h"
#include "NativeUnitMoveBehavior.h"

///////////////////////////////////////////////////
// Goto state
///////////////////////////////////////////////////

NativeUnitMoveBehavior::Goto::Goto(NativeUnitMoveBehavior& behavior, size_t refreshDelay)
  : UnitMoveBehaviorT::Goto(behavior, refreshDelay)
  , m_events(m_timerEvent
#ifdef NON_FINAL_VERSION
    , m_frameDebugEvent
#endif
  )
  , m_transitions()
{
}

void NativeUnitMoveBehavior::Goto::onEnter()
{
  UnitMoveBehaviorT::Goto::onEnter();

  //submitMoveCommand(m_context.m_destination);
  //onTimer();
}

void NativeUnitMoveBehavior::Goto::onExit()
{
  UnitMoveBehaviorT::Goto::onExit();
}

void NativeUnitMoveBehavior::Goto::onTimer()
{
  static const int SUBMIT_MOVE_RATE = 40;

  UnitMoveBehaviorT::Goto::onTimer();
  if (m_context.isTerminated())
    return;

  // Periodically send the move action. Can also be used to compute a new path or making sure that the unit follows the right path.      
  if ((BWAPI::Broodwar->getFrameCount() - SUBMIT_MOVE_RATE) >= m_lastFrameMoveSubmitted)
  {
    // It is possible that an action cannot be sent for obscure reason, make sure to send it again in another frame when this happens
#ifdef _DEBUG
    if(!m_context.m_unit->canIssueCommand(BWAPI::UnitCommand::move(m_context.m_unit, m_context.m_destination)))
      SPAR_LOG(LogTypes.GENERAL_WARNING, "Command move could not be issued for %s (%d)"
        , m_context.m_unit->getType().getName().c_str(), m_context.m_unit->getID());
    else
#endif
    submitMoveCommand(m_context.m_destination);
  }

  // TODO: would need to call onPathPlanning() at some point
}