BWAPI
Skynet/Skynet/Condition.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "Interface.h"
00004 
00005 #include <boost/scoped_ptr.hpp>
00006 
00007 #include "OperatorType.h"
00008 #include "TypeSafeEnum.h"
00009 
00010 struct ConditionTestDef
00011 {
00012         enum type
00013         {
00014                 None,
00015                 enemyUnitCountGreaterEqualThan,
00016                 enemyDoesntHasUnitLessThan,
00017                 enemyHasResearched,
00018                 enemyHasUpgraded,
00019                 enemyHasExpanded,
00020                 isResearching,
00021                 isResearched,
00022                 isUpgrading,
00023                 isUpgraded,
00024                 myPlannedUnitTotalGreaterEqualThan,
00025                 myPlannedUnitTotalLessThan,
00026                 myUnitTotalBuildCountGreaterEqualThan,
00027                 myUnitTotalBuildCountLessThan,
00028                 isEnemyZerg,
00029                 isEnemyProtoss,
00030                 isEnemyTerran,
00031                 isContained,
00032                 canForgeExpand,
00033                 mapHasIsland,
00034                 mapHasMineralOnlyNatural,
00035                 distanceBetweenBotAndEnemy,
00036                 minDistanceBetweenMainsLessThan,
00037                 minDistanceBetweenMainsGreaterThan,
00038                 mapSize,
00039                 numberOfEnemies,
00040                 enemyMainSize,
00041                 mapIs
00042         };
00043 };
00044 typedef SafeEnum<ConditionTestDef> ConditionTest;
00045 
00046 struct Condition
00047 {
00048 public:
00049         Condition() : mType(ConditionTest::None), mOperatorType(OperatorType::None), mNeededValue(false) {}
00050         Condition(ConditionTest type, bool expectedValue = true);
00051         Condition(ConditionTest type, int extraValue);
00052         Condition(ConditionTest type, bool expectedValue, int extraValue);
00053         Condition(ConditionTest type, double extraValue);
00054         Condition(ConditionTest type, bool expectedValue, double extraValue);
00055         Condition(ConditionTest type, BWAPI::UnitType unitType, int count = 1);
00056         Condition(ConditionTest type, BWAPI::TechType tech);
00057         Condition(ConditionTest type, BWAPI::UpgradeType upgrade, int level = 1);
00058         Condition(ConditionTest type, std::string string);
00059 
00060         Condition(const Condition &other)
00061                 : mType(other.mType)
00062                 , mOperatorType(other.mOperatorType)
00063                 , mLCondition(other.mLCondition ? new Condition(*other.mLCondition) : NULL)
00064                 , mRCondition(other.mRCondition ? new Condition(*other.mRCondition) : NULL)
00065                 , mNeededValue(other.mNeededValue)
00066                 , mExtraInt(other.mExtraInt)
00067                 , mExtraDouble(other.mExtraDouble)
00068                 , mUnitType(other.mUnitType)
00069                 , mTech(other.mTech)
00070                 , mUpgrade(other.mUpgrade)
00071                 , mString(other.mString)
00072         {
00073         }
00074 
00075         Condition &Condition::operator=(const Condition &other)
00076         {
00077                 mType = other.mType;
00078                 mOperatorType = other.mOperatorType;
00079                 mNeededValue = other.mNeededValue;
00080                 mExtraInt = other.mExtraInt;
00081                 mExtraDouble = other.mExtraDouble;
00082                 mUnitType = other.mUnitType;
00083                 mTech = other.mTech;
00084                 mUpgrade = other.mUpgrade;
00085                 mString = other.mString;
00086 
00087                 mLCondition.reset(other.mLCondition ? new Condition(*other.mLCondition) : NULL);
00088                 mRCondition.reset(other.mRCondition ? new Condition(*other.mRCondition) : NULL);
00089 
00090                 return *this;
00091         }
00092 
00093         bool evauluate() const
00094         {
00095                 if(mOperatorType == OperatorType::None)
00096                         return passesValue();
00097                 else if(mOperatorType == OperatorType::And)
00098                         return mLCondition->evauluate() && mRCondition->evauluate();
00099                 else if(mOperatorType == OperatorType::Or)
00100                         return mLCondition->evauluate() || mRCondition->evauluate();
00101 
00102                 return false;
00103         }
00104 
00105         Condition operator&&(const Condition &other) const
00106         {
00107                 return Condition(*this, OperatorType::And, other);
00108         }
00109 
00110         Condition operator||(const Condition &other) const
00111         {
00112                 return Condition(*this, OperatorType::Or, other);
00113         }
00114 
00115 private:
00116         ConditionTest mType;
00117 
00118         OperatorType mOperatorType;
00119         boost::scoped_ptr<Condition> mLCondition;
00120         boost::scoped_ptr<Condition> mRCondition;
00121 
00122         bool mNeededValue;
00123         int mExtraInt;
00124         double mExtraDouble;
00125 
00126         BWAPI::UnitType mUnitType;
00127         BWAPI::TechType mTech;
00128         BWAPI::UpgradeType mUpgrade;
00129         std::string mString;
00130 
00131         bool passesValue() const;
00132 
00133         Condition(const Condition &left, OperatorType::type opType, const Condition &right)
00134                 : mType(ConditionTest::None)
00135                 , mOperatorType(opType)
00136                 , mLCondition(new Condition(left))
00137                 , mRCondition(new Condition(right))
00138                 , mNeededValue(false)
00139                 , mExtraInt(0)
00140                 , mExtraDouble(0)
00141                 , mUnitType(BWAPI::UnitTypes::None)
00142                 , mTech(BWAPI::TechTypes::None)
00143                 , mUpgrade(BWAPI::UpgradeTypes::None)
00144                 , mString()
00145         {}
00146 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines