CGeniusAI.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Priorities;
  20. class CGeniusAI : public CGlobalAI
  21. {
  22. private:
  23. ICallback* m_cb;
  24. GeniusAI::BattleAI::CBattleLogic* m_battleLogic;
  25. GeniusAI::GeneralAI::CGeneralAI m_generalAI;
  26. GeniusAI::Priorities * m_priorities;
  27. CondSh<BattleState> m_state; //are we engaged into battle?
  28. struct AIObjectContainer
  29. {
  30. AIObjectContainer(const CGObjectInstance * o):o(o){}
  31. const CGObjectInstance * o;
  32. bool operator<(const AIObjectContainer& b)const;
  33. };
  34. class HypotheticalGameState
  35. {
  36. public:
  37. struct HeroModel
  38. {
  39. HeroModel(){}
  40. HeroModel(const CGHeroInstance * h);
  41. int3 pos;
  42. int3 previouslyVisited_pos;
  43. int3 interestingPos;
  44. bool finished;
  45. int remainingMovement;
  46. const CGHeroInstance * h;
  47. };
  48. struct TownModel
  49. {
  50. TownModel(const CGTownInstance *t);
  51. const CGTownInstance *t;
  52. std::vector<std::pair<ui32, std::vector<ui32> > > creaturesToRecruit;
  53. CCreatureSet creaturesInGarrison; //type, num
  54. bool hasBuilt;
  55. };
  56. HypotheticalGameState(){}
  57. HypotheticalGameState(CGeniusAI & ai);
  58. void update(CGeniusAI & ai);
  59. CGeniusAI * AI;
  60. std::vector<const CGHeroInstance *> AvailableHeroesToBuy;
  61. std::vector<int> resourceAmounts;
  62. std::vector<HeroModel> heroModels;
  63. std::vector<TownModel> townModels;
  64. std::set< AIObjectContainer > knownVisitableObjects;
  65. };
  66. class AIObjective
  67. {
  68. public:
  69. enum Type
  70. {
  71. //hero objectives
  72. visit, //done TODO: upon visit friendly hero, trade
  73. attack, //done
  74. //flee,
  75. dismissUnits,
  76. dismissYourself,
  77. rearangeTroops,
  78. finishTurn, //done //uses up remaining motion to get somewhere interesting.
  79. //town objectives
  80. recruitHero, //done
  81. buildBuilding, //done
  82. recruitCreatures, //done
  83. upgradeCreatures //done
  84. };
  85. CGeniusAI * AI;
  86. Type type;
  87. virtual void fulfill(CGeniusAI &,HypotheticalGameState & hgs)=0;
  88. virtual HypotheticalGameState pretend(const HypotheticalGameState &) =0;
  89. virtual void print() const=0;
  90. virtual float getValue() const=0; //how much is it worth to the AI to achieve
  91. };
  92. class HeroObjective: public AIObjective
  93. {
  94. public:
  95. HypotheticalGameState hgs;
  96. int3 pos;
  97. const CGObjectInstance * object;
  98. mutable std::vector<HypotheticalGameState::HeroModel *> whoCanAchieve;
  99. //HeroObjective(){}
  100. //HeroObjective(Type t):object(NULL){type = t;}
  101. HeroObjective(const HypotheticalGameState &hgs,Type t,const CGObjectInstance * object,HypotheticalGameState::HeroModel *h,CGeniusAI * AI);
  102. bool operator < (const HeroObjective &other)const;
  103. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  104. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  105. float getValue() const;
  106. void print() const;
  107. private:
  108. mutable float _value;
  109. mutable float _cost;
  110. };
  111. //town objectives
  112. //recruitHero
  113. //buildBuilding
  114. //recruitCreatures
  115. //upgradeCreatures
  116. class TownObjective: public AIObjective
  117. {
  118. public:
  119. HypotheticalGameState hgs;
  120. HypotheticalGameState::TownModel * whichTown;
  121. int which; //which hero, which building, which creature,
  122. TownObjective(const HypotheticalGameState &hgs,Type t,HypotheticalGameState::TownModel * tn,int Which,CGeniusAI * AI);
  123. bool operator < (const TownObjective &other)const;
  124. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  125. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  126. float getValue() const;
  127. void print() const;
  128. private:
  129. mutable float _value;
  130. mutable float _cost;
  131. };
  132. class AIObjectivePtrCont
  133. {
  134. public:
  135. AIObjectivePtrCont():obj(NULL){}
  136. AIObjectivePtrCont(AIObjective * obj):obj(obj){};
  137. AIObjective * obj;
  138. bool operator < (const AIObjectivePtrCont & other) const{return obj->getValue()<other.obj->getValue();}
  139. };
  140. HypotheticalGameState trueGameState;
  141. AIObjective * getBestObjective();
  142. void addHeroObjectives(HypotheticalGameState::HeroModel &h, HypotheticalGameState & hgs);
  143. void addTownObjectives(HypotheticalGameState::TownModel &h, HypotheticalGameState & hgs);
  144. void fillObjectiveQueue(HypotheticalGameState & hgs);
  145. void reportResources();
  146. void startFirstTurn();
  147. std::map<int,bool> isHeroStrong;//hero
  148. std::set< AIObjectContainer > knownVisitableObjects;
  149. std::set<HeroObjective> currentHeroObjectives; //can be fulfilled right now
  150. std::set<TownObjective> currentTownObjectives;
  151. std::vector<AIObjectivePtrCont> objectiveQueue;
  152. public:
  153. CGeniusAI();
  154. virtual ~CGeniusAI();
  155. virtual void init(ICallback * CB);
  156. virtual void yourTurn();
  157. virtual void heroKilled(const CGHeroInstance *);
  158. virtual void heroCreated(const CGHeroInstance *);
  159. virtual void heroMoved(const TryMoveHero &);
  160. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) {};
  161. virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID){};
  162. 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.
  163. virtual void tileRevealed(int3 pos);
  164. virtual void tileHidden(int3 pos);
  165. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  166. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd);
  167. virtual void playerBlocked(int reason);
  168. virtual void objectRemoved(const CGObjectInstance *obj); //eg. collected resource, picked artifact, beaten hero
  169. virtual void newObject(const CGObjectInstance * obj); //eg. ship built in shipyard
  170. // battle
  171. virtual void actionFinished(const BattleAction *action);//occurs AFTER every action taken by any stack or by the hero
  172. virtual void actionStarted(const BattleAction *action);//occurs BEFORE every action taken by any stack or by the hero
  173. virtual void battleAttack(BattleAttack *ba); //called when stack is performing attack
  174. virtual void battleStacksAttacked(std::set<BattleStackAttacked> & bsa); //called when stack receives damage (after battleAttack())
  175. virtual void battleEnd(BattleResult *br);
  176. 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
  177. virtual void battleStackMoved(int ID, int dest, int distance, bool end);
  178. virtual void battleSpellCast(SpellCast *sc);
  179. 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
  180. virtual void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  181. //
  182. virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  183. virtual void battleStackAttacking(int ID, int dest);
  184. virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
  185. virtual BattleAction activeStack(int stackID);
  186. void battleResultsApplied();
  187. friend class Priorities;
  188. };
  189. }
  190. #endif // __CGENIUSAI_H__