BWAPI
Public Types | Public Member Functions | Private Attributes
UnitGroup Class Reference

#include <UnitGroup.h>

List of all members.

Public Types

typedef std::set< Unit >::iterator iterator
typedef std::set< Unit >
::const_iterator 
const_iterator

Public Member Functions

 UnitGroup ()
int countCompletedBy (int time) const
int ratingDifference (const UnitGroup &other) const
bool isWorthEngaging (const UnitGroup &other) const
Position getCenter () const
UnitGroup getBestFittingToCircle (int circleSize, int inFramesTime=0) const
Unit getClosestUnit (Unit unit) const
Unit getClosestUnit (Position position) const
bool isAnyInRange (const UnitGroup &otherGroup) const
int minDistanceBetween (const UnitGroup &otherGroup) const
int getBuildScore () const
int getAverageTerrainHeight () const
double getAverageSpeed () const
Region getMajorityRegion () const
bool canAttackAir () const
bool canAttackGround () const
bool canMajorityAttack (const UnitGroup &other) const
bool hasDetection () const
std::set< Unit >::size_type getCloakedCount () const
std::set< Unit >::size_type getFlyingCount () const
iterator begin ()
const_iterator begin () const
iterator end ()
const_iterator end () const
void clear ()
std::set< Unit >::size_type count (Unit const &unit) const
bool empty () const
void erase (iterator position)
std::set< Unit >::size_type erase (Unit const &unit)
void erase (iterator first, iterator last)
iterator find (Unit const &unit)
std::set< Unit >::const_iterator find (Unit const &unit) const
std::pair< iterator, bool > insert (Unit const &unit)
iterator insert (iterator position, Unit const &unit)
void insert (const_iterator first, const_iterator last)
std::set< Unit >::size_type size () const
bool operator== (const UnitGroup &other) const
bool operator!= (const UnitGroup &other) const
 operator std::set< Unit > () const
UnitGroupoperator+= (const UnitGroup &other)
UnitGroup operator+ (const UnitGroup &other) const

Private Attributes

std::set< UnitmUnits

Detailed Description

Definition at line 8 of file UnitGroup.h.


Member Typedef Documentation

Definition at line 46 of file UnitGroup.h.

typedef std::set<Unit>::iterator UnitGroup::iterator

Definition at line 45 of file UnitGroup.h.


Constructor & Destructor Documentation

Definition at line 7 of file UnitGroup.cpp.

{
}

Member Function Documentation

std::set< Unit >::iterator UnitGroup::begin ( )

Definition at line 24 of file UnitGroup.cpp.

References mUnits.

Referenced by TerrainAnaysisClass::createBases(), getCenter(), MineBlockingMineralTask::preUpdate(), UnitTrackerClass::update(), BaseClass::update(), and MineBlockingMineralTask::updateRequirements().

{
        return mUnits.begin();
}

Here is the caller graph for this function:

std::set< Unit >::const_iterator UnitGroup::begin ( ) const

Definition at line 29 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits.begin();
}
bool UnitGroup::canAttackAir ( ) const

Definition at line 445 of file UnitGroup.cpp.

References mUnits.

Referenced by isWorthEngaging().

{
        for each(Unit unit in mUnits)
        {
                if(unit->canAttackAir())
                        return true;
        }

        return false;
}

Here is the caller graph for this function:

Definition at line 456 of file UnitGroup.cpp.

References mUnits.

{
        for each(Unit unit in mUnits)
        {
                if(unit->canAttackGround())
                        return true;
        }

        return false;
}
bool UnitGroup::canMajorityAttack ( const UnitGroup other) const

Definition at line 467 of file UnitGroup.cpp.

References empty(), mUnits, and size().

Referenced by DefaultSquadTask::update().

{
        if(empty() || other.empty())
                return false;

        double thisGroupAttackCount = 0.0;
        for each(Unit thisUnit in mUnits)
        {
                for each(Unit otherUnit in other)
                {
                        if(thisUnit->canAttack(otherUnit))
                                ++thisGroupAttackCount;
                }
        }

        thisGroupAttackCount /= other.size();
        thisGroupAttackCount /= size();

        return thisGroupAttackCount > 0.5;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void UnitGroup::clear ( )

Definition at line 44 of file UnitGroup.cpp.

References mUnits.

Referenced by BaseClass::update().

{
        mUnits.clear();
}

Here is the caller graph for this function:

std::set< Unit >::size_type UnitGroup::count ( Unit const &  unit) const

Definition at line 49 of file UnitGroup.cpp.

References mUnits.

Referenced by PylonPowerTrackerClass::onAddPylon(), PylonPowerTrackerClass::onRemovePylon(), AdvancedGroup::update(), ResourceManagerClass::update(), and DefaultSquadTask::updateRequirements().

{
        return mUnits.count(unit);
}

Here is the caller graph for this function:

int UnitGroup::countCompletedBy ( int  time) const

Definition at line 11 of file UnitGroup.cpp.

References BWAPI::Broodwar, BWAPI::Game::getFrameCount(), and mUnits.

{
        const int &timeNow = BWAPI::Broodwar->getFrameCount();

        int total = 0;
        for each(Unit unit in mUnits)
        {
                if(unit->isCompleted() || unit->getCompletedTime() <= time)
                        ++total;
        }
        return total;
}

Here is the call graph for this function:

bool UnitGroup::empty ( ) const
std::set< Unit >::iterator UnitGroup::end ( )

Definition at line 34 of file UnitGroup.cpp.

References mUnits.

Referenced by TerrainAnaysisClass::createBases(), MineBlockingMineralTask::preUpdate(), BaseClass::removeUnit(), UnitTrackerClass::update(), and BaseClass::update().

{
        return mUnits.end();
}

Here is the caller graph for this function:

std::set< Unit >::const_iterator UnitGroup::end ( ) const

Definition at line 39 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits.end();
}
void UnitGroup::erase ( iterator  position)
std::set< Unit >::size_type UnitGroup::erase ( Unit const &  unit)

Definition at line 64 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits.erase(unit);
}
void UnitGroup::erase ( iterator  first,
iterator  last 
)
std::set< Unit >::iterator UnitGroup::find ( Unit const &  unit)

Definition at line 74 of file UnitGroup.cpp.

References mUnits.

Referenced by BaseClass::removeUnit().

{
        return mUnits.find(unit);
}

Here is the caller graph for this function:

std::set< Unit >::const_iterator UnitGroup::find ( Unit const &  unit) const

Definition at line 79 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits.find(unit);
}
double UnitGroup::getAverageSpeed ( ) const

Definition at line 376 of file UnitGroup.cpp.

References mUnits.

Referenced by isWorthEngaging().

{
        double averageSpeed = 0;
        for each(Unit unit in mUnits)
        {
                averageSpeed += unit->getType().topSpeed();
        }

        return averageSpeed / double(mUnits.size());
}

Here is the caller graph for this function:

Definition at line 365 of file UnitGroup.cpp.

References BWAPI::Broodwar, BWAPI::Game::getGroundHeight(), and mUnits.

{
        int height = 0;
        for each(Unit unit in mUnits)
        {
                height += BWAPI::Broodwar->getGroundHeight(unit->getTilePosition());
        }

        return height / mUnits.size();
}

Here is the call graph for this function:

UnitGroup UnitGroup::getBestFittingToCircle ( int  circleSize,
int  inFramesTime = 0 
) const

Definition at line 191 of file UnitGroup.cpp.

References getCenter(), and BWAPI::Position::getDistance().

Referenced by ArbiterAction::update(), and PsiStormAction::update().

{
        UnitGroup circleUnits(*this);
        bool removed = false;
        do
        {
                removed = false;
                Position center = circleUnits.getCenter();

                Unit furthestUnit;
                int furthestDistance = 0;

                for each(Unit unit in circleUnits)
                {
                        int distanceToCenter;
                        if(inFramesTime == 0)
                                distanceToCenter = unit->getDistance(center);
                        else
                                distanceToCenter = unit->getDistance(center, inFramesTime);

                        if(distanceToCenter > circleSize && distanceToCenter > furthestDistance)
                        {
                                furthestUnit = unit;
                                furthestDistance = distanceToCenter;
                        }
                }

                if(furthestUnit)
                {
                        circleUnits.erase(furthestUnit);
                        removed = true;
                }
        }
        while(removed);

        return circleUnits;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int UnitGroup::getBuildScore ( ) const

Definition at line 302 of file UnitGroup.cpp.

References mUnits.

{
        int rating = 0;
        for each(Unit unit in mUnits)
        {
                rating += unit->getType().buildScore();
        }

        return rating;
}

Definition at line 153 of file UnitGroup.cpp.

References begin(), empty(), mUnits, BWAPI::BulletTypes::None, and size().

Referenced by TerrainAnaysisClass::createBases(), getBestFittingToCircle(), Goal::getPositionForMerge(), PsiStormAction::update(), and ArbiterAction::update().

{
        if(empty())
                return BWAPI::Positions::None;

        if(size() == 1)
                return (*begin())->getPosition();

        Vector position;

        for each(Unit unit in mUnits)
        {
                position += Vector(unit->getPosition());
        }

        return position / float(size());
}

Here is the call graph for this function:

Here is the caller graph for this function:

std::set< Unit >::size_type UnitGroup::getCloakedCount ( ) const

Definition at line 421 of file UnitGroup.cpp.

References mUnits.

Referenced by isWorthEngaging().

{
        std::set<Unit>::size_type cloakedCount = 0;
        for each(Unit unit in mUnits)
        {
                if(unit->getType().hasPermanentCloak() || unit->isCloaked())
                        ++cloakedCount;
        }

        return cloakedCount;
}

Here is the caller graph for this function:

Definition at line 229 of file UnitGroup.cpp.

References mUnits.

Referenced by GoalAction::update(), and AdvancedGroup::update().

{
        Unit closestUnit;
        int closestDistance = std::numeric_limits<int>::max();

        for each(Unit groupUnit in mUnits)
        {
                int thisDistance = unit->getDistance(groupUnit);
                if(thisDistance < closestDistance)
                {
                        closestDistance = thisDistance;
                        closestUnit = groupUnit;
                }
        }

        return closestUnit;
}

Here is the caller graph for this function:

Definition at line 247 of file UnitGroup.cpp.

References mUnits.

{
        Unit closestUnit;
        int closestDistance = std::numeric_limits<int>::max();

        for each(Unit groupUnit in mUnits)
        {
                int thisDistance = groupUnit->getDistance(position);
                if(thisDistance < closestDistance)
                {
                        closestDistance = thisDistance;
                        closestUnit = groupUnit;
                }
        }

        return closestUnit;
}
std::set< Unit >::size_type UnitGroup::getFlyingCount ( ) const

Definition at line 433 of file UnitGroup.cpp.

References mUnits.

Referenced by isWorthEngaging().

{
        std::set<Unit>::size_type flyingCount = 0;
        for each(Unit unit in mUnits)
        {
                if(unit->getType().isFlyer() || unit->isLifted())
                        ++flyingCount;
        }

        return flyingCount;
}

Here is the caller graph for this function:

Definition at line 387 of file UnitGroup.cpp.

References Singleton< T >::Instance(), and mUnits.

Referenced by isWorthEngaging().

{
        int currentCount = 1;
        Region currentCandidate;

        for each(Unit unit in mUnits)
        {
                const Region &thisRegion = TerrainAnaysis::Instance().getRegion(unit->getPosition());
                if(thisRegion == currentCandidate)
                        ++currentCount;
                else
                        --currentCount;

                if(currentCount == 0)
                {
                        currentCandidate = thisRegion;
                        currentCount = 1;
                }
        }

        return currentCandidate;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool UnitGroup::hasDetection ( ) const

Definition at line 410 of file UnitGroup.cpp.

References mUnits.

Referenced by isWorthEngaging().

{
        for each(Unit unit in mUnits)
        {
                if(unit->getType().isDetector())
                        return true;
        }

        return false;
}

Here is the caller graph for this function:

std::pair< std::set< Unit >::iterator, bool > UnitGroup::insert ( Unit const &  unit)
iterator UnitGroup::insert ( iterator  position,
Unit const &  unit 
)
void UnitGroup::insert ( const_iterator  first,
const_iterator  last 
)
bool UnitGroup::isAnyInRange ( const UnitGroup otherGroup) const

Definition at line 265 of file UnitGroup.cpp.

References mUnits.

Referenced by DefaultSquadTask::update().

{
        for each(Unit groupUnit in mUnits)
        {
                for each(Unit otherGroupUnit in otherGroup)
                {
                        if(groupUnit->isInRange(otherGroupUnit))
                                return true;
                }
        }

        return false;
}

Here is the caller graph for this function:

bool UnitGroup::isWorthEngaging ( const UnitGroup other) const

Definition at line 313 of file UnitGroup.cpp.

References canAttackAir(), empty(), getAverageSpeed(), getCloakedCount(), getFlyingCount(), getMajorityRegion(), hasDetection(), and size().

Referenced by DefaultSquadTask::update().

{
        // Don't chase after a faster group
        double thisAverageSpeed = getAverageSpeed();
        double otherAverageSpeed = other.getAverageSpeed();
        if(otherAverageSpeed >= thisAverageSpeed)
                return false;

        size_t thisFlyingCount = getFlyingCount();
        double thisFlyingAverage = 0.0;
        if(!empty())
                thisFlyingAverage = thisFlyingCount / size();

        size_t otherFlyingCount = other.getFlyingCount();
        double otherFlyingAverage = 0.0;
        if(!other.empty())
                otherFlyingAverage = otherFlyingCount / other.size();

        if((thisFlyingAverage + 0.3) < otherFlyingAverage)
                return false;

        Region thisRegion = getMajorityRegion();
        Region otherRegion = other.getMajorityRegion();
        if(thisRegion && thisRegion != otherRegion)
        {
                // dont try attacking a group that isnt connected without a majority of flying units
                if(!otherRegion || thisRegion->isConnected(otherRegion))
                {
                        if(thisFlyingAverage > 0.5)
                                return false;
                }
        }

        size_t otherCloakedCount = other.getCloakedCount();
        if(!hasDetection() && otherCloakedCount > 0)
        {
                size_t thisCloakedCount = getCloakedCount();
                if(!other.hasDetection() && thisCloakedCount > 0)
                {
                        if(thisCloakedCount < otherCloakedCount)
                                return false;
                }
                else if(otherCloakedCount > 0)
                        return false;
        }

        if(otherFlyingAverage > 0.2 && !canAttackAir())
                return false;

        return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int UnitGroup::minDistanceBetween ( const UnitGroup otherGroup) const

Definition at line 279 of file UnitGroup.cpp.

References mUnits.

Referenced by DefaultSquadTask::update().

{
        int minDistance = std::numeric_limits<int>::max();
        for each(Unit groupUnit in mUnits)
        {
                for each(Unit otherGroupUnit in otherGroup)
                {
                        int thisDistance = otherGroupUnit->getDistance(groupUnit);
                        if(thisDistance < minDistance)
                                minDistance = thisDistance;
                }
        }
        return minDistance;
}

Here is the caller graph for this function:

UnitGroup::operator std::set< Unit > ( ) const

Definition at line 104 of file UnitGroup.cpp.

{
        return mUnits;
}
bool UnitGroup::operator!= ( const UnitGroup other) const

Definition at line 176 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits != other.mUnits;
}
UnitGroup UnitGroup::operator+ ( const UnitGroup other) const

Definition at line 294 of file UnitGroup.cpp.

{
        UnitGroup ret(*this);
        ret += other;

        return ret;
}
UnitGroup & UnitGroup::operator+= ( const UnitGroup other)

Definition at line 181 of file UnitGroup.cpp.

References insert().

{
        for each(Unit unit in other)
        {
                insert(unit);
        }

        return *this;
}

Here is the call graph for this function:

bool UnitGroup::operator== ( const UnitGroup other) const

Definition at line 171 of file UnitGroup.cpp.

References mUnits.

{
        return mUnits == other.mUnits;
}
int UnitGroup::ratingDifference ( const UnitGroup other) const

Definition at line 109 of file UnitGroup.cpp.

References mUnits.

Referenced by DefaultSquadTask::update(), and BaseClass::update().

{
        // not sure about this, along the right lines with using time to kill as that in turns considers weapon damage, terrain, health etc but bad atm
        if(mUnits.empty() && other.mUnits.empty())
                return 0;
        else if(mUnits.empty())
                return -100;
        else if(other.mUnits.empty())
                return 100;

        int OtherKillThisTime = 0;
        int ThisKillOtherTime = 0;
        for each(Unit thisUnit in mUnits)
        {
                for each(Unit otherUnit in other.mUnits)
                {
                        if(otherUnit->canAttack(thisUnit) && !thisUnit->isUnderDarkSwarm() && otherUnit->getDistance(thisUnit) < 600)
                        {
                                int killTime = otherUnit->getTimeToKill(thisUnit);
                                if(otherUnit->getType().groundWeapon().outerSplashRadius() > 15)
                                        killTime /= 2;

                                OtherKillThisTime += std::min(killTime, 500);
                        }
                        else
                                OtherKillThisTime += 500;

                        if(thisUnit->canAttack(otherUnit) && !otherUnit->isUnderDarkSwarm() && thisUnit->getDistance(otherUnit) < 600)
                                ThisKillOtherTime += std::min(thisUnit->getTimeToKill(otherUnit), 500);
                        else
                                ThisKillOtherTime += 500;
                }
        }

        OtherKillThisTime /= other.mUnits.size();
        ThisKillOtherTime /= mUnits.size();

        int ratingTotal = std::max(OtherKillThisTime, ThisKillOtherTime) - std::min(OtherKillThisTime, ThisKillOtherTime);
        if(OtherKillThisTime < ThisKillOtherTime)
                ratingTotal = 0 - ratingTotal;

        return ratingTotal;
}

Here is the caller graph for this function:

std::set< Unit >::size_type UnitGroup::size ( ) const

Member Data Documentation

std::set<Unit> UnitGroup::mUnits [private]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines