#include "stdafx.h"
#include "CompositeLocation.h"
//#include "PrimitiveLocation.h"

void CompositeLocation::addSubLocation(AutoPopulatedLocation* location)
{
  assert(std::find(m_subLocations.begin(), m_subLocations.end(), location) == m_subLocations.end());

  m_subLocations.insert(m_subLocations.end(), location);
  location->addSubscriber(this);

  // Add all units to composite location
  RaiseEventTaskList deferredEvents(*this);
  std::pair<unit_iterator, unit_iterator> ownUnits = location->getOwnUnits();
  for (; ownUnits.first != ownUnits.second; ++ownUnits.first)
    onUnitAddedToLocation(*ownUnits.first, location, deferredEvents);
  std::pair<unit_iterator, unit_iterator> enemyUnits = location->getEnemyUnits();
  for (; enemyUnits.first != enemyUnits.second; ++enemyUnits.first)
    onUnitAddedToLocation(*enemyUnits.first, location, deferredEvents);
  std::pair<unit_iterator, unit_iterator> neutralUnits = location->getNeutralUnits();
  for (; neutralUnits.first != neutralUnits.second; ++neutralUnits.first)
    onUnitAddedToLocation(*neutralUnits.first, location, deferredEvents);

  //PrimitiveLocation* primitiveLocation = dynamic_cast<PrimitiveLocation*>(location);
  //if (primitiveLocation != NULL)
  //{
  //  onPrimitiveSubLocationAdded(primitiveLocation);
  //}
}