#include "stdafx.h"
#include "WanderBehavior.h"
#include "../Layer1FSM.h"
#include <time.h>

//------------------------------------------------------------------------------
// Going state
//------------------------------------------------------------------------------
WanderBehavior::Going::Going(WanderBehavior& behavior)
  : BehaviorStateT(behavior)
  //, m_moveCommand(*this)
  , m_destinationReached(*this, Scheduler::FSM_SCHEDULING_PRIORITY, 5, 5)
  , m_transitionToSelf(
      *this,
	    IsUnitIdle(behavior.m_unit),
      NoTransitionAction(),
      *this)
  , m_events(m_destinationReached)
  , m_transitions(m_transitionToSelf)
{
}

void WanderBehavior::Going::onEnter()
{ 
  srand(static_cast<unsigned int>(time(NULL)));
  double randx = (rand() % 400) + 25; 
  double randy = (rand() % 400) + 25;
  if((rand() % 100) > 50){
    randx = randx * -1;
  }
  if((rand() % 100) > 50){
    randy = randy * -1;
  }
  int x = static_cast<int>(m_context.m_unit->getPosition().x() + randx);
  int y = static_cast<int>(m_context.m_unit->getPosition().y() + randy);
  Layer1FSM::getInstance().getArbitrator().
    submitCommand(m_context.m_unit, MoveCommand(*this, BWAPI::Position(x,y)), Arbitration::ActionImportance(m_context));
}
