#include "stdafx.h"
#include "TechMonitoringProcess.h"
#include "../../World/TechTypeObject.h"


TechMonitoringProcess::TechMonitoringProcess(const Process* parent, BWAPI::Unit* building, TechTypeObject* tech)
  : InBuildingMonitoringProcessT(parent, building)
  , InBuildingMonitoringProcess(parent, building)
  , MonitoringProcess(parent)
  , m_tech(tech)
{
}

void TechMonitoringProcess::_output(std::ostream& out) const
{
  out << "Tech in progress: " << *m_tech;
}

bool TechMonitoringProcess::testCompletion()
{
  if (BWAPI::Broodwar->self()->hasResearched(m_tech->getTech()))
  {
    m_tech->setTechAcquired();
    terminate(Success);
  }
  else if (!m_building->isResearching())
  {
    terminate(Cancelled);
  }
  return isTerminated();
}

void TechMonitoringProcess::setNewTimeout(bool error)
{
  if (error)
  {
    SPAR_LOG(LogTypes.GENERAL_ERROR, "Tech not finished when it should: %s", toString(m_tech).c_str());
  }
  m_timeoutEvent.setWaitingTimeBeforeExecution(m_building->getRemainingResearchTime()+3);
}

void TechMonitoringProcess::cancel()
{
  m_building->cancelResearch();

  m_timeoutEvent.deactivate();
  m_timeoutEvent.setWaitingTimeBeforeExecution(1);
  m_timeoutEvent.activate();
}
