#include "stdafx.h"
#include "UnitTypeCreatable.h"
#include "UnitTypeObject.h"
#include "CurrentWorld.h"
#include "UnitTypeInstanceCompleted.h"

UnitTypeCreatable::UnitTypeCreatable(UnitTypeObject* unitType)
  : planning::AndExp(0, (ILogicalExp**)NULL, (ILogicalExp**)NULL)
  , m_unitType(unitType)
{
  BWAPI::UnitType type = m_unitType->getUnitType();

  const std::map<BWAPI::UnitType, int>& requiredUnits = type.requiredUnits();
  for (std::map<BWAPI::UnitType, int>::const_iterator it = requiredUnits.begin(); it != requiredUnits.end(); ++it)
  {
    this->_pushBack(CurrentWorld::getUnitTypeObject(it->first)->getUnitTypeCompletedExpression());
  }

  BWAPI::TechType requiredTech = type.requiredTech();
  if (requiredTech != BWAPI::TechTypes::None)
  {
    TechObject* tech = CurrentWorld::getTechTypeObject(requiredTech);
    this->_pushBack(CurrentWorld::TechAcquired->getFormulaApplication(&tech, &tech + 1));
  }

  const std::pair<BWAPI::UnitType, int> whatBuilds = type.whatBuilds();
  if (whatBuilds.first != BWAPI::UnitTypes::None && requiredUnits.find(whatBuilds.first) == requiredUnits.end())
  {
    this->_pushBack(CurrentWorld::getUnitTypeObject(whatBuilds.first)->getUnitTypeCompletedExpression());
  }
}
