#pragma once
#include "../../../../Utils/Process/FSM/Condition.h"
#include "../../../../Utils/PersistentUnitGroup.h"

/**
 * @brief Condition that is satisfied if a unit is targetting an enemy unit
 */
class IsAttacking : public Condition
{
public:
  IsAttacking(BWAPI::Unit* unit): m_unit(unit) {}
  bool evaluate() const
  {
    BWAPI::Unit* target = m_unit->getOrderTarget();
    return target != NULL && target->getPlayer()->isEnemy(BWAPI::Broodwar->self());
  }
private:
  BWAPI::Unit* m_unit;
};

/**
 * @brief Condition that is satisfied if one unit in a group is targetting an enemy unit
 */
class GroupIsAttacking : public Condition
{
public:
  GroupIsAttacking(const PersistentUnitGroup* group): m_group(group) {}
  bool evaluate() const
  {
    for(PersistentUnitGroup::const_iterator it=m_group->begin(); it != m_group->end(); ++it)
    {
      BWAPI::Unit* unit = *it;
      if (unit->isAttacking())
        return true;
      BWAPI::Unit* target = unit->getOrderTarget();
      if(target != NULL && target->getPlayer()->isEnemy(BWAPI::Broodwar->self()))
        return true;
    }
    return false;
  }
private:
  const PersistentUnitGroup* m_group;
};