#include "stdafx.h"
#include "BuildGatewaysProcess.h"
#include "../Layer3Script.h"
#include "../../../../PerceptualState/PerceptualState.h"

static const size_t MAX_SIMULTANEOUS_GATEWAYS = 2;

void BuildGatewaysProcess::run()
{
  if (Layer3Script::getInstance().getLayer2().getNbAvailableBuildings(BWAPI::UnitTypes::Protoss_Gateway, Spar->getPerceptualState()->getMap()) == 0
   && m_buildGateways.size() < MAX_SIMULTANEOUS_GATEWAYS) // 2 gateways max at once, which is an ad-hoc way to prevent some problems
  {
    const Resources surplus = Layer3Script::getInstance().getLayer2().getAvailableResources(PRIORITY_BUILD_GATEWAYS);
    if (surplus.getMinerals() >= BWAPI::UnitTypes::Protoss_Gateway.mineralPrice())
    {
      Build* buildGateway = Layer3Script::getInstance().getLayer2().instantiateBuild(
        this,
        PRIORITY_BUILD_GATEWAYS,
        Spar->getPerceptualState()->getMap(), 
        BWAPI::UnitTypes::Protoss_Gateway);
      m_buildGateways.add(buildGateway);
      buildGateway->start();
    }
  }
}
