CGeniusAI.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef __CGENIUSAI_H__
  2. #define __CGENIUSAI_H__
  3. #include "Common.h"
  4. #include "BattleLogic.h"
  5. #include "GeneralAI.h"
  6. #include "../../lib/CondSh.h"
  7. #include <set>
  8. #include <list>
  9. #include <queue>
  10. class CBuilding;
  11. namespace GeniusAI {
  12. enum BattleState
  13. {
  14. NO_BATTLE,
  15. UPCOMING_BATTLE,
  16. ONGOING_BATTLE,
  17. ENDING_BATTLE
  18. };
  19. class CGeniusAI : public CGlobalAI
  20. {
  21. private:
  22. ICallback* m_cb;
  23. GeniusAI::BattleAI::CBattleLogic* m_battleLogic;
  24. GeniusAI::GeneralAI::CGeneralAI m_generalAI;
  25. CondSh<BattleState> m_state; //are we engaged into battle?
  26. struct AIObjectContainer
  27. {
  28. AIObjectContainer(const CGObjectInstance * o):o(o){}
  29. const CGObjectInstance * o;
  30. bool operator<(const AIObjectContainer& b)const;
  31. };
  32. class HypotheticalGameState
  33. {
  34. public:
  35. struct HeroModel
  36. {
  37. HeroModel(){}
  38. HeroModel(const CGHeroInstance * h);
  39. int3 pos;
  40. int3 interestingPos;
  41. bool finished;
  42. int remainingMovement;
  43. const CGHeroInstance * h;
  44. };
  45. struct TownModel
  46. {
  47. TownModel(const CGTownInstance *t);
  48. const CGTownInstance *t;
  49. std::vector<std::pair<ui32, std::vector<ui32> > > creaturesToRecruit;
  50. CCreatureSet creaturesInGarrison; //type, num
  51. bool hasBuilt;
  52. };
  53. HypotheticalGameState(){}
  54. HypotheticalGameState(CGeniusAI & AI);
  55. void update(CGeniusAI & AI);
  56. std::vector<const CGHeroInstance *> AvailableHeroesToBuy;
  57. std::vector<int> resourceAmounts;
  58. std::vector<HeroModel> heroModels;
  59. std::vector<TownModel> townModels;
  60. std::set< AIObjectContainer > knownVisitableObjects;
  61. };
  62. struct CostModel
  63. {
  64. CostModel(vector<int> &resourceCosts,const CGHeroInstance * moved,int distOutOfTheWay);
  65. CostModel():moved(NULL){}
  66. vector<int> resourceCosts;
  67. const CGHeroInstance * moved;
  68. int distOutOfTheWay;
  69. float getCost();
  70. };
  71. class AIObjective
  72. {
  73. public:
  74. enum Type
  75. {
  76. //hero objectives
  77. visit, //done
  78. attack, //done
  79. flee,
  80. dismissUnits,
  81. dismissYourself,
  82. finishTurn, //done //uses up remaining motion to get somewhere nice.
  83. //town objectives
  84. recruitHero, //done
  85. buildBuilding, //done
  86. recruitCreatures, //done
  87. upgradeCreatures //done
  88. };
  89. CostModel cost;
  90. CGeniusAI * AI;
  91. Type type;
  92. virtual void fulfill(CGeniusAI &,HypotheticalGameState & hgs)=0;
  93. virtual HypotheticalGameState pretend(const HypotheticalGameState &) =0;
  94. virtual void print() const=0;
  95. virtual float getValue() const=0; //how much is it worth to the AI to achieve
  96. };
  97. class HeroObjective: public AIObjective
  98. {
  99. public:
  100. int3 pos;
  101. const CGObjectInstance * object;
  102. std::vector<HypotheticalGameState::HeroModel *> whoCanAchieve;
  103. HeroObjective(){}
  104. HeroObjective(Type t):object(NULL){type = t;}
  105. HeroObjective(Type t,const CGObjectInstance * object,HypotheticalGameState::HeroModel *h,CGeniusAI * AI);
  106. bool operator < (const HeroObjective &other)const;
  107. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  108. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  109. float getValue() const;
  110. void print() const;
  111. private:
  112. mutable float _value;
  113. };
  114. //town objectives
  115. //recruitHero
  116. //buildBuilding
  117. //recruitCreatures
  118. //upgradeCreatures
  119. class TownObjective: public AIObjective
  120. {
  121. public:
  122. HypotheticalGameState::TownModel * whichTown;
  123. int which; //which hero, which building, which creature,
  124. TownObjective(Type t,HypotheticalGameState::TownModel * tn,int Which,CGeniusAI * AI);
  125. bool operator < (const TownObjective &other)const;
  126. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  127. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  128. float getValue() const;
  129. void print() const;
  130. private:
  131. mutable float _value;
  132. };
  133. class AIObjectivePtrCont
  134. {
  135. public:
  136. AIObjectivePtrCont():obj(NULL){}
  137. AIObjectivePtrCont(AIObjective * obj):obj(obj){};
  138. AIObjective * obj;
  139. bool operator < (const AIObjectivePtrCont & other) const{return obj->getValue()<other.obj->getValue();}
  140. };
  141. HypotheticalGameState trueGameState;
  142. AIObjective * getBestObjective();
  143. void addHeroObjectives(HypotheticalGameState::HeroModel &h, HypotheticalGameState & hgs);
  144. void addTownObjectives(HypotheticalGameState::TownModel &h, HypotheticalGameState & hgs);
  145. void fillObjectiveQueue(HypotheticalGameState & hgs);
  146. void reportResources();
  147. void startFirstTurn();
  148. std::map<int,bool> isHeroStrong;//hero
  149. std::set< AIObjectContainer > knownVisitableObjects;
  150. std::set<HeroObjective> currentHeroObjectives; //can be fulfilled right now
  151. std::set<TownObjective> currentTownObjectives;
  152. std::vector<AIObjectivePtrCont> objectiveQueue;
  153. public:
  154. CGeniusAI();
  155. virtual ~CGeniusAI();
  156. virtual void init(ICallback * CB);
  157. virtual void yourTurn();
  158. virtual void heroKilled(const CGHeroInstance *);
  159. virtual void heroCreated(const CGHeroInstance *);
  160. virtual void heroMoved(const TryMoveHero &);
  161. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) {};
  162. virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID){};
  163. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel); //Show a dialog, player must take decision. If selection then he has to choose between one of given components, if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  164. virtual void tileRevealed(int3 pos);
  165. virtual void tileHidden(int3 pos);
  166. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  167. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd);
  168. virtual void playerBlocked(int reason);
  169. virtual void objectRemoved(const CGObjectInstance *obj); //eg. collected resource, picked artifact, beaten hero
  170. virtual void newObject(const CGObjectInstance * obj); //eg. ship built in shipyard
  171. // battle
  172. virtual void actionFinished(const BattleAction *action);//occurs AFTER every action taken by any stack or by the hero
  173. virtual void actionStarted(const BattleAction *action);//occurs BEFORE every action taken by any stack or by the hero
  174. virtual void battleAttack(BattleAttack *ba); //called when stack is performing attack
  175. virtual void battleStacksAttacked(std::set<BattleStackAttacked> & bsa); //called when stack receives damage (after battleAttack())
  176. virtual void battleEnd(BattleResult *br);
  177. virtual void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  178. virtual void battleStackMoved(int ID, int dest, int distance, bool end);
  179. virtual void battleSpellCast(SpellCast *sc);
  180. virtual void battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side); //called by engine when battle starts; side=0 - left, side=1 - right
  181. virtual void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  182. //
  183. virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  184. virtual void battleStackAttacking(int ID, int dest);
  185. virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
  186. virtual BattleAction activeStack(int stackID);
  187. void battleResultsApplied();
  188. };
  189. }
  190. #endif // __CGENIUSAI_H__