BWAPI
SnippyHolloW-BroodwarBotQ-f01ab56/src/Micro/Units/BayesianUnit.h
Go to the documentation of this file.
00001 #pragma once
00002 #include <PrecompiledHeader.h>
00003 #include <BWAPI.h>
00004 #include <BWTA.h>
00005 #include <math.h>
00006 #include "Utils/Vec.h"
00007 #include "BattleUnit.h"
00008 #include "Regions/MapManager.h"
00009 //#include "Micro/UnitsGroup.h"
00010 #include <map>
00011 #include <set>
00012 #include <boost/archive/text_oarchive.hpp>
00013 #include <boost/archive/text_iarchive.hpp>
00014 #include <boost/serialization/vector.hpp>
00015 #include <boost/serialization/map.hpp>
00016 #include <boost/shared_ptr.hpp>
00017 #include <fstream>
00018 
00019 class BayesianUnit;
00020 typedef boost::shared_ptr<BayesianUnit> pBayesianUnit;
00021 
00022 // #define PROBT 1
00023 //#define __WITH_FLOCKING__
00024 // #define __HEIGHTS_ATTRACTION__
00025 // #define __WITH_OCCUPATION__
00026 
00027 // TODO, this class has to be derived to take Flying/Ground/Special Units 
00028 // (templars, tanks, lurkers, etc.) into account
00029 
00030 class UnitsGroup;
00031 
00032 enum occupation_type {
00033     OCCUP_NO,
00034     OCCUP_EUNIT,
00035     OCCUP_UNIT,
00036     OCCUP_BLOCKING,
00037     OCCUP_BUILDING
00038 };
00039 
00040 enum unit_mode {
00041     MODE_INPOS,
00042     MODE_FIGHT_G,   // ground
00043     MODE_FIGHT_A,   // air
00044     MODE_SCOUT,
00045 #ifdef __WITH_FLOCKING__
00046     MODE_FLOCK,
00047 #endif
00048     MODE_MOVE
00049 }; 
00050 
00051 enum repulse_value {
00052     REPULSE_NO,
00053     REPULSE_LOW,
00054     REPULSE_HIGH
00055 };
00056 
00057 enum damage_value {
00058     DAMAGE_NO,
00059     DAMAGE_LOW,
00060     DAMAGE_MED,
00061     DAMAGE_HIGH
00062 };
00063 
00064 struct ProbTablesData
00065 {
00066         friend class boost::serialization::access;
00067 #ifdef __LEARNING_PROB_TABLES__
00068         int version;
00069         int score;
00070         void fillProbTables();
00071 #endif
00072     std::vector<double> _damageProb;
00073     std::vector<double> _repulseProb;
00074     std::map<int, double> _defaultProb; // int == occupation_type, for s13n
00075 #ifdef __HEIGHTS_ATTRACTION__
00076     std::vector<double> _heightProb;
00077 #endif
00078         ProbTablesData();
00079         ProbTablesData(int utID, std::vector<double>damageP, 
00080                 std::vector<double>repulseP
00081                 , std::map<int, double> defaultP // int == occupation_type, for s13n
00082 #ifdef __HEIGHTS_ATTRACTION__
00083                 , std::vector<double> heightP
00084 #endif
00085                 );
00086         template<class Archive>
00087         void serialize(Archive & ar, const unsigned int version);
00088 };
00089 
00090 BOOST_CLASS_TRACKING(ProbTablesData, boost::serialization::track_never)
00091 
00092 struct ProbTables
00093 {
00094         int unitTypeID; // -1 for ground, -2 for flying, -3 for special
00095         ProbTablesData probTablesData;
00096         ProbTables(int utID);
00097         ~ProbTables();
00098 };
00099 
00100 class BayesianUnit : public BattleUnit
00101 {
00102 protected:
00103     std::vector<Vec> _dirv;
00104     int _maxDimension, _minDimension;
00105     int _lastAttackFrame;
00106     int _lastMoveFrame;
00107     int _lastClickFrame;
00108     int _lastRefreshPathRequest;
00109     double _maxDiag;
00110     int _maxWeaponsRange;
00111     BWAPI::Position _lastRightClick, _posAtMost13FramesAgo, _posAtMost23FramesAgo;
00112     bool _iThinkImBlocked;
00113     int _lastTotalHP;
00114     std::list<int> _HPLosts;
00115     int _sumLostHP;
00116     const int _refreshPathFramerate; // should be static TODO
00117     int _maxDistWhileRefreshingPath; // not static because dependent on the speed
00118     int _maxDistInOneClick; // not static because dependent on the speed
00119     Position _inPos;
00120     bool _fleeing;
00121     bool _fightMoving;
00122     //std::multimap<BWAPI::Position, attractor_type> _prox;
00123 #ifdef __WITH_OCCUPATION__
00124     std::vector<occupation_type> _occupation;
00125 #endif
00126     // dirv[attractor] = direction relative to an attractor
00127     /* Our Unit is in 12. Here are the indices of the Vec()s.
00128     ________________
00129     | 0| 5|10|15|20|
00130     | 1| 6|11|16|21|
00131     | 2| 7|12|17|22|
00132     | 3| 8|13|18|23|
00133     | 4| 9|14|19|24|
00134     ----------------
00135     This grid because there are only 16 possible directions in Broodwar */
00136     MapManager* mapManager;
00137     std::vector<repulse_value> _repulseValues;
00138 #ifdef __WITH_FLOCKING__
00139     static std::vector<repulse_value> _flockValues;
00140 #endif
00141     std::vector<damage_value> _damageValues;
00142         const ProbTables* _probTables;
00143     UnitsGroup* _unitsGroup;
00144     std::multimap<double, BWAPI::Unit*> _rangeEnemies;
00145     std::multimap<double, Vec> _dirvProb;
00146 
00147     inline void computeRepulseValues();
00148 #ifdef __WITH_FLOCKING__
00149     inline void computeFlockValues();
00150 #endif
00151     inline void computeDamageValues();
00152     void straightLine(std::vector<BWAPI::Position>& ppath, 
00153         const BWAPI::Position& p_start, 
00154         const BWAPI::Position& p_end, 
00155         bool quick=true);
00156 
00157     inline void updateDirV();
00158     inline void testIfBlocked();
00159     inline void resumeFromBlocked();
00160     void updateRangeEnemies();
00161     void clearDamages();
00162     void updateTargetEnemy();
00163     inline void setTargetEnemy(BWAPI::Unit* u);
00164     int computeDmg(BWAPI::Unit* u);
00165     bool inRange(BWAPI::Unit* u);
00166     bool outRanges(BWAPI::Unit* u);
00167         bool outRanges(const std::set<Unit*>& units);
00168         bool isOutrangingMe(BWAPI::Unit* u);
00169     void drawDirV();
00170     void updateObj();
00171     void drawObj(int number=0);
00172     void updateDir();
00173     void drawDir();
00174     inline void clickDir();
00175     inline void clickTarget();
00176         inline void aMoveTarget();
00177         inline void moveClick(BWAPI::Position p);
00178     void flee();
00179     int fightMove();
00180     void drawArrow(Vec& v);
00181     inline void updateAttractors();
00182     void drawAttractors();
00183     void drawRepulseValues();
00184 #ifdef __WITH_FLOCKING__
00185     void drawFlockValues();
00186 #endif
00187 #ifdef __WITH_OCCUPATION__
00188     void drawOccupation(int number);
00189 #endif
00190     // TODO:
00191     // goal direction
00192     // range enemies
00193     inline double computeProb(unsigned int i);
00194     inline void computeProbs();
00195     inline void selectDir(const Vec& criterium);
00196     virtual int addRangeGround();
00197     virtual int addRangeAir();
00198     virtual bool decideToFlee(); // sets _fleeing
00199     void simpleFlee();
00200     bool dodgeStorm();
00201     bool dragScarab();
00202     bool dragMine();
00203     void drawProbs(std::multimap<double, Vec>& probs, int number=0);
00204     unit_mode _mode;
00205     std::set<Unit*> _targetingMe;
00206     void updateTargetingMe();
00207 public:
00208     int _fleeingDmg; // number of DPS we have to take in to decide to flee, default 20
00209         static pBayesianUnit newBayesianUnit(BWAPI::Unit* u);
00210 
00211         bool isFighting();
00212     void move(BWAPI::Position p); // debug purposes
00213         void switchMode(unit_mode um);
00214         void setUnitsGroup(UnitsGroup* ug);
00215         void dettachGroup();
00216     unit_mode getMode();
00217     int getMaxDimension();
00218         void newPath();
00219     void updatePPath();
00220     Vec dir, obj; // dir=current direction, obj=pathfinder's direction
00221     BayesianUnit(BWAPI::Unit* u, const ProbTables* probTables);
00222     virtual ~BayesianUnit();
00223     BWAPI::UnitType getType();
00224 
00226     void update();
00227 
00228     virtual void attack(const BWAPI::Position& p);
00229     void attackEnemyUnit(BWAPI::Unit* u);
00230 
00231     virtual void micro() = 0;
00232     virtual void check() = 0;
00233     virtual int getAttackDuration() = 0;
00234     virtual std::set<BWAPI::UnitType> getSetPrio() = 0;
00235 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines