BWAPI
Skynet/Skynet/UnitFilter.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 "TypeSafeEnum.h"
00008 
00009 #include "OperatorType.h"
00010 
00011 namespace UnitFilterFlags
00012 {
00013         enum type
00014         {
00015                 None = 0,
00016                 IsComplete = 1,
00017                 HasAddon = (1 << 1),
00018                 IsLifted = (1 << 2),
00019                 IsWorker = (1 << 3),
00020                 IsArmyUnit = (1 << 4),
00021                 CanAttackGround = (1 << 5),
00022                 CanAttackAir = (1 << 6),
00023                 IsSpellCaster = (1 << 7),
00024                 All = -1
00025         };
00026 }
00027 
00028 struct UnitFilter
00029 {
00030 public:
00031         UnitFilter();
00032         UnitFilter(BWAPI::UnitType type);
00033         UnitFilter(UnitFilterFlags::type flags);
00034 
00035         UnitFilter(const UnitFilter &other)
00036                 : mType(other.mType)
00037                 , mOperatorType(other.mOperatorType)
00038                 , mLFilter(other.mLFilter ? new UnitFilter(*other.mLFilter) : NULL)
00039                 , mRFilter(other.mRFilter ? new UnitFilter(*other.mRFilter) : NULL)
00040                 , mExpectedValue(other.mExpectedValue)
00041                 , mUnitType(other.mUnitType)
00042                 , mUnitFlags(other.mUnitFlags)
00043         {}
00044 
00045         UnitFilter &UnitFilter::operator=(const UnitFilter &other)
00046         {
00047                 mType = other.mType;
00048                 mOperatorType = other.mOperatorType;
00049 
00050                 mExpectedValue = other.mExpectedValue;
00051 
00052                 mUnitType = other.mUnitType;
00053                 mUnitFlags = other.mUnitFlags;
00054 
00055                 mLFilter.reset(other.mLFilter ? new UnitFilter(*other.mLFilter) : NULL);
00056                 mRFilter.reset(other.mRFilter ? new UnitFilter(*other.mRFilter) : NULL);
00057 
00058                 return *this;
00059         }
00060 
00061         UnitFilter operator||(const UnitFilter& other) const
00062         {
00063                 return UnitFilter(*this, OperatorType::Or, other);
00064         }
00065         UnitFilter operator&&(const UnitFilter& other) const
00066         {
00067                 return UnitFilter(*this, OperatorType::And, other);
00068         }
00069 
00070         bool passesFilter(const Unit &unit) const
00071         {
00072                 if(mOperatorType == OperatorType::None)
00073                         return filter(unit);
00074                 else if(mOperatorType == OperatorType::And)
00075                         return mLFilter->passesFilter(unit) == mExpectedValue && mRFilter->passesFilter(unit) == mExpectedValue;
00076                 else if(mOperatorType == OperatorType::Or)
00077                         return mLFilter->passesFilter(unit) == mExpectedValue || mRFilter->passesFilter(unit) == mExpectedValue;
00078 
00079                 return false;
00080         }
00081 
00082         UnitFilter &operator!();
00083         bool operator!=(const UnitFilter& other) const;
00084         bool operator==(const UnitFilter& other) const;
00085         bool operator<(const UnitFilter& other) const;
00086 
00087 private:
00088         struct UnitFilterTypeDef
00089         {
00090                 enum type
00091                 {
00092                         None,
00093                         UnitOfType,
00094                         PassesFlags
00095                 };
00096         };
00097         typedef SafeEnum<UnitFilterTypeDef> UnitFilterType;
00098 
00099         UnitFilterType mType;
00100 
00101         OperatorType mOperatorType;
00102         boost::scoped_ptr<UnitFilter> mLFilter;
00103         boost::scoped_ptr<UnitFilter> mRFilter;
00104 
00105         bool mExpectedValue;
00106 
00107         BWAPI::UnitType mUnitType;
00108         UnitFilterFlags::type mUnitFlags;
00109 
00110         bool filter(const Unit &unit) const;
00111 
00112         UnitFilter(const UnitFilter &left, OperatorType::type opType, const UnitFilter &right)
00113                 : mType(UnitFilterType::None)
00114                 , mOperatorType(opType)
00115                 , mLFilter(new UnitFilter(left))
00116                 , mRFilter(new UnitFilter(right))
00117                 , mExpectedValue(true)
00118                 , mUnitType(BWAPI::UnitTypes::None)
00119                 , mUnitFlags(UnitFilterFlags::None)
00120         {}
00121 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines