BWAPI
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes
Requirement Class Reference

#include <Requirement.h>

Collaboration diagram for Requirement:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Requirement (RequirementType type, int amount)
 Requirement (int priority, int duration, Unit unit, Position position)
 Requirement (int priority, int duration, UnitFilter unitFilter, Position position)
 Requirement (TaskPointer task)
 Requirement (BWAPI::UnitType unit)
 Requirement (BWAPI::TechType tech)
 Requirement (BWAPI::UpgradeType upgrade, int level)
int earliestTime ()
std::map< int, int > earliestUnitTime (int startTime, int endTime, std::set< Unit > &currentUnits)
void reserve (int frameTime)
RequirementType getType ()
bool unitRequirement ()
Unit getUnit () const
int getDelay ()
bool operator== (const Requirement &other) const
bool operator< (const Requirement &other) const

Static Public Attributes

static const int maxTime = std::numeric_limits<int>::max()

Private Member Functions

int earliestTimeForType (BWAPI::UnitType unitType)

Private Attributes

RequirementType mType
int mAmount
UnitFilter mUnitFilter
Unit mUnit
Position mPosition
int mPriority
int mDuration
int mDelay
TaskPointer mTask
BWAPI::UnitType mUnitType
BWAPI::TechType mTechType
BWAPI::UpgradeType mUpgradeType

Detailed Description

Definition at line 33 of file Requirement.h.


Constructor & Destructor Documentation

Requirement::Requirement ( RequirementType  type,
int  amount 
)
Requirement::Requirement ( int  priority,
int  duration,
Unit  unit,
Position  position 
)
Requirement::Requirement ( int  priority,
int  duration,
UnitFilter  unitFilter,
Position  position 
)
Requirement::Requirement ( BWAPI::UpgradeType  upgrade,
int  level 
)

Member Function Documentation

Definition at line 220 of file Requirement.cpp.

References earliestTimeForType(), RequirementTypeDef::Gas, Singleton< T >::Instance(), mAmount, maxTime, RequirementTypeDef::Mineral, mTask, mTechType, mType, mUnitType, mUpgradeType, BWAPI::UpgradeTypes::Protoss_Air_Armor, BWAPI::UpgradeTypes::Protoss_Air_Weapons, BWAPI::UnitTypes::Protoss_Cybernetics_Core, BWAPI::UnitTypes::Protoss_Fleet_Beacon, BWAPI::UpgradeTypes::Protoss_Ground_Armor, BWAPI::UpgradeTypes::Protoss_Ground_Weapons, BWAPI::UpgradeTypes::Protoss_Plasma_Shields, BWAPI::UnitTypes::Protoss_Templar_Archives, RequirementTypeDef::RequiredForTech, RequirementTypeDef::RequiredForUnit, RequirementTypeDef::RequiredForUpgrade, BWAPI::UnitType::requiredUnits(), RequirementTypeDef::Supply, RequirementTypeDef::Task, RequirementTypeDef::Time, BWAPI::TechType::whatResearches(), and BWAPI::UpgradeType::whatUpgrades().

{
        if(mType == RequirementType::Mineral)
                return ResourceTracker::Instance().earliestMineralAvailability(mAmount);
        else if(mType == RequirementType::Gas)
                return ResourceTracker::Instance().earliestGasAvailability(mAmount);
        else if(mType == RequirementType::Supply)
                return ResourceTracker::Instance().earliestSupplyAvailability(mAmount);
        else if(mType == RequirementType::Time)
                return mAmount;
        else if(mType == RequirementType::Task)
                return mTask->getEndTime();
        else if(mType == RequirementType::RequiredForUnit)
        {
                int latestTime = 0;

                for(std::map<BWAPI::UnitType, int>::const_iterator it = mUnitType.requiredUnits().begin(); it != mUnitType.requiredUnits().end(); ++it)
                        latestTime = std::max(latestTime, earliestTimeForType(it->first));

                return latestTime;
        }
        else if(mType == RequirementType::RequiredForUpgrade)
        {
                int latestTime = earliestTimeForType(mUpgradeType.whatUpgrades());

                if(mAmount > 1)
                {
                        if(mUpgradeType == BWAPI::UpgradeTypes::Protoss_Ground_Weapons || mUpgradeType == BWAPI::UpgradeTypes::Protoss_Ground_Armor)
                                latestTime = std::max(latestTime, earliestTimeForType(BWAPI::UnitTypes::Protoss_Templar_Archives));
                        else if(mUpgradeType == BWAPI::UpgradeTypes::Protoss_Plasma_Shields)
                                latestTime = std::max(latestTime, earliestTimeForType(BWAPI::UnitTypes::Protoss_Cybernetics_Core));
                        else if(mUpgradeType == BWAPI::UpgradeTypes::Protoss_Air_Armor || mUpgradeType == BWAPI::UpgradeTypes::Protoss_Air_Weapons)
                                latestTime = std::max(latestTime, earliestTimeForType(BWAPI::UnitTypes::Protoss_Fleet_Beacon));
                }

                return latestTime;
        }
        else if(mType == RequirementType::RequiredForTech)
                return earliestTimeForType(mTechType.whatResearches());

        return Requirement::maxTime;
}

Here is the call graph for this function:

Definition at line 352 of file Requirement.cpp.

References BWAPI::Broodwar, Singleton< T >::Instance(), and maxTime.

Referenced by earliestTime().

{
        int thisTime = Requirement::maxTime;
        for each(Unit requiredUnit in UnitTracker::Instance().selectAllUnits(unitType))
        {
                if(requiredUnit->isCompleted())
                        return BWAPI::Broodwar->getFrameCount();
                else
                {
                        int completeTime = requiredUnit->getCompletedTime();
                        if(completeTime < thisTime)
                                thisTime = completeTime;
                }
        }

        return thisTime;
}

Here is the call graph for this function:

Here is the caller graph for this function:

std::map< int, int > Requirement::earliestUnitTime ( int  startTime,
int  endTime,
std::set< Unit > &  currentUnits 
)

Definition at line 263 of file Requirement.cpp.

References Singleton< T >::Instance(), maxTime, mDelay, mDuration, mPosition, mPriority, mType, mUnit, mUnitFilter, BWAPI::Positions::None, UnitFilter::passesFilter(), and RequirementTypeDef::Unit.

{
        const bool &allowAnyBlockLength = mDuration == Requirement::maxTime;

        if(mType == RequirementType::Unit)
        {
                if(currentUnits.count(mUnit) > 0)
                        return std::map<int, int>();

                if(mPosition != BWAPI::Positions::None)
                        mDelay = int((mUnit->getPosition().getApproxDistance(mPosition) * 1.6) / mUnit->getType().topSpeed()) + 15;

                return TaskManager::Instance().earliestFreeTimes(mUnit, mPriority, startTime - mDelay, endTime, mDuration, allowAnyBlockLength);
        }

        std::map<int, int> bestTimes;
        int bestStartTime = Requirement::maxTime;
        int bestDelay = Requirement::maxTime;

        for each(Unit unit in UnitTracker::Instance().selectAllUnits())
        {
                if(currentUnits.count(unit) > 0)
                        continue;

                if(!mUnitFilter.passesFilter(unit))//TODO: only checks if it currently passes the filter, but some it can tell when in the future it will pass, might be worth implementing
                        continue;

                int delay = 0;
                if(mPosition != BWAPI::Positions::None)
                {
                        if(!unit->hasPath(mPosition))
                                continue;

                        delay = int((unit->getPosition().getApproxDistance(mPosition) * 1.6) / unit->getType().topSpeed()) + 15;
                }

                // If the delay for this unit pushes it outside the endtime, continue
                if(mDuration != Requirement::maxTime && startTime + mDuration + delay > endTime)
                        continue;

                const std::map<int, int> &currentTimes = TaskManager::Instance().earliestFreeTimes(unit, mPriority, startTime - delay, endTime, mDuration + delay, allowAnyBlockLength);
                if(!currentTimes.empty())
                {
                        std::map<int, int> currentTimesPlusDelay;
                        for(std::map<int, int>::const_iterator it = currentTimes.begin(); it != currentTimes.end(); ++it)
                                currentTimesPlusDelay[it->first + delay] = it->second;

                        // Find the earliest time in these blocks
                        std::map<int, int>::const_iterator it = currentTimesPlusDelay.begin();
                        if(it != currentTimesPlusDelay.end())
                        {
                                // Save it if its the best we have found, also prefer if the delay is shorter so it
                                // doesn't choose a unit really far away even though it will make it in time
                                if(it->first < bestStartTime || (it->first == bestStartTime && delay < bestDelay))
                                {
                                        mUnit = unit;
                                        mDelay = delay;
                                        
                                        bestTimes = currentTimesPlusDelay;
                                        bestStartTime = it->first;
                                        bestDelay = delay;
                                }
                        }
                }
        }

        return bestTimes;
}

Here is the call graph for this function:

int Requirement::getDelay ( ) [inline]

Definition at line 56 of file Requirement.h.

References mDelay.

{ return mDelay; }

Definition at line 50 of file Requirement.h.

References mType.

{ return mType; }
Unit Requirement::getUnit ( ) const [inline]

Definition at line 54 of file Requirement.h.

References mUnit.

{ return mUnit; }
bool Requirement::operator< ( const Requirement other) const

Definition at line 155 of file Requirement.cpp.

References mAmount, mDelay, mDuration, mPosition, mPriority, mTask, mTechType, mType, mUnit, mUnitFilter, mUnitType, and mUpgradeType.

{
        if(mType < other.mType)
                return true;
        else if(mType != other.mType)
                return false;

        if(mAmount < other.mAmount)
                return true;
        else if(mAmount != other.mAmount)
                return false;

        if(mUnitFilter < other.mUnitFilter)
                return true;
        else if(mUnitFilter != other.mUnitFilter)
                return false;

        if(mUnit < other.mUnit)
                return true;
        else if(mUnit != other.mUnit)
                return false;

        if(mPosition < other.mPosition)
                return true;
        else if(mPosition != other.mPosition)
                return false;

        if(mPriority < other.mPriority)
                return true;
        else if(mPriority != other.mPriority)
                return false;

        if(mDuration < other.mDuration)
                return true;
        else if(mDuration != other.mDuration)
                return false;

        if(mDelay < other.mDelay)
                return true;
        else if(mDelay != other.mDelay)
                return false;

        if(mTask < other.mTask)
                return true;
        else if(mTask != other.mTask)
                return false;

        if(mUnitType < other.mUnitType)
                return true;
        else if(mUnitType != other.mUnitType)
                return false;

        if(mTechType < other.mTechType)
                return true;
        else if(mTechType != other.mTechType)
                return false;

        if(mUpgradeType < other.mUpgradeType)
                return true;
        else if(mUpgradeType != other.mUpgradeType)
                return false;

        return false;
}
bool Requirement::operator== ( const Requirement other) const

Definition at line 125 of file Requirement.cpp.

References mAmount, mDelay, mDuration, mPosition, mPriority, mTask, mTechType, mType, mUnit, mUnitFilter, mUnitType, and mUpgradeType.

{
        if(mType != other.mType)
                return false;
        else if(mAmount != other.mAmount)
                return false;
        else if(mUnitFilter != other.mUnitFilter)
                return false;
        else if(mUnit != other.mUnit)
                return false;
        else if(mPosition != other.mPosition)
                return false;
        else if(mPriority != other.mPriority)
                return false;
        else if(mDuration != other.mDuration)
                return false;
        else if(mDelay != other.mDelay)
                return false;
        else if(mTask != other.mTask)
                return false;
        else if(mUnitType != other.mUnitType)
                return false;
        else if(mTechType != other.mTechType)
                return false;
        else if(mUpgradeType != other.mUpgradeType)
                return false;
        else
                return true;
}
void Requirement::reserve ( int  frameTime)
bool Requirement::unitRequirement ( ) [inline]

Member Data Documentation

int Requirement::mAmount [private]

Definition at line 65 of file Requirement.h.

Referenced by earliestTime(), operator<(), operator==(), and reserve().

const int Requirement::maxTime = std::numeric_limits<int>::max() [static]
int Requirement::mDelay [private]

Definition at line 73 of file Requirement.h.

Referenced by earliestUnitTime(), getDelay(), operator<(), and operator==().

int Requirement::mDuration [private]

Definition at line 72 of file Requirement.h.

Referenced by earliestUnitTime(), operator<(), operator==(), and reserve().

Definition at line 70 of file Requirement.h.

Referenced by earliestUnitTime(), operator<(), and operator==().

int Requirement::mPriority [private]

Definition at line 71 of file Requirement.h.

Referenced by earliestUnitTime(), operator<(), operator==(), and reserve().

Definition at line 75 of file Requirement.h.

Referenced by earliestTime(), operator<(), and operator==().

Definition at line 78 of file Requirement.h.

Referenced by earliestTime(), operator<(), and operator==().

Definition at line 68 of file Requirement.h.

Referenced by earliestUnitTime(), getUnit(), operator<(), operator==(), and reserve().

Definition at line 67 of file Requirement.h.

Referenced by earliestUnitTime(), operator<(), and operator==().

Definition at line 77 of file Requirement.h.

Referenced by earliestTime(), operator<(), and operator==().

Definition at line 79 of file Requirement.h.

Referenced by earliestTime(), operator<(), and operator==().


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