CGeniusAI.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "../../lib/VCMI_Lib.h"
  8. #include <set>
  9. #include <list>
  10. #include <queue>
  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. class AIObjectContainer
  26. {
  27. public:
  28. AIObjectContainer(const CGObjectInstance * o):o(o){}
  29. const CGObjectInstance * o;
  30. bool operator<(const AIObjectContainer& b)const
  31. {
  32. if (o->pos!=b.o->pos)
  33. return o->pos<b.o->pos;
  34. return o->id<b.o->id;
  35. }
  36. };
  37. std::set< AIObjectContainer > knownVisitableObjects;
  38. class HypotheticalGameState
  39. {
  40. public:
  41. HypotheticalGameState(CGeniusAI & AI)
  42. :knownVisitableObjects(AI.knownVisitableObjects)
  43. {
  44. std::vector < const CGHeroInstance *> heroes = AI.m_cb->getHeroesInfo();
  45. for(std::vector < const CGHeroInstance *>::iterator i = heroes.begin(); i != heroes.end(); i++)
  46. heroModels.push_back(HeroModel(*i));
  47. std::vector < const CGTownInstance *> towns = AI.m_cb->getTownsInfo();
  48. for(std::vector < const CGTownInstance *>::iterator i = towns.begin(); i != towns.end(); i++)
  49. townModels.push_back(TownModel(*i));
  50. if(AI.m_cb->howManyTowns()!=0)
  51. AvailableHeroesToBuy = AI.m_cb->getAvailableHeroes(AI.m_cb->getTownInfo(0,0));
  52. }
  53. class TownModel
  54. {
  55. public:
  56. TownModel(const CGTownInstance *t):t(t){visitingHero=(t->visitingHero!=NULL);}
  57. const CGTownInstance *t;
  58. bool visitingHero;
  59. };
  60. class HeroModel
  61. {
  62. public:
  63. HeroModel(const CGHeroInstance * h):h(h){
  64. pos = h->getPosition(false);remainingMovement = h->movement;
  65. }
  66. int3 pos;
  67. int remainingMovement;
  68. const CGHeroInstance * h;
  69. };
  70. std::vector<const CGHeroInstance *> AvailableHeroesToBuy;
  71. std::vector<HeroModel> heroModels;
  72. std::vector<TownModel> townModels;
  73. std::set< AIObjectContainer > knownVisitableObjects;
  74. };
  75. void addHeroObjectives(HypotheticalGameState::HeroModel &h, HypotheticalGameState & hgs);
  76. void addTownObjectives(HypotheticalGameState::TownModel &h, HypotheticalGameState & hgs);
  77. void getObjectives(HypotheticalGameState & hgs);
  78. void reportResources();
  79. int turn;
  80. CondSh<BattleState> m_state; //are we engaged into battle?
  81. bool firstTurn;
  82. class AIObjective
  83. {
  84. public:
  85. enum Type
  86. {
  87. //hero objectives
  88. visit,
  89. attack,
  90. flee,
  91. dismissUnits,
  92. dismissYourself,
  93. finishTurn, //uses up remaining motion to get somewhere nice.
  94. //town objectives
  95. recruitHero,
  96. recruitCreatures,
  97. upgradeCreatures,
  98. buildBuilding
  99. };
  100. Type type;
  101. //virtual bool operator < (const AIObjective &)const=0;
  102. //virtual bool stillPossible(const HypotheticalGameState &)const = 0;
  103. virtual void fulfill(CGeniusAI &,HypotheticalGameState & hgs)=0;
  104. virtual float getValue() const=0; //how much is it worth to the AI to achieve
  105. };
  106. class HeroObjective: public AIObjective
  107. {
  108. public:
  109. int3 pos;
  110. const CGObjectInstance * object;
  111. std::vector<HypotheticalGameState::HeroModel *> whoCanAchieve;
  112. HeroObjective(){}
  113. HeroObjective(Type t):object(NULL){type = t;}
  114. HeroObjective(Type t,const CGObjectInstance * object,HypotheticalGameState::HeroModel *h):object(object)
  115. {
  116. pos = object->pos;
  117. type = t;
  118. whoCanAchieve.push_back(h);
  119. _value = rand();
  120. }
  121. bool operator < (const HeroObjective &other)const
  122. {
  123. if(type != other.type)
  124. return type<other.type;
  125. if(pos!=other.pos)
  126. return pos < other.pos;
  127. if(object->id!=other.object->id)
  128. return object->id < other.object->id;
  129. return false;
  130. }
  131. //bool stillPossible(const HypotheticalGameState &) const;
  132. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  133. float getValue() const{return _value;}
  134. private:
  135. float _value;
  136. };
  137. //town objectives
  138. //recruitHero,
  139. //recruitCreatures,
  140. //upgradeCreatures,
  141. //buildBuilding
  142. class TownObjective: public AIObjective
  143. {
  144. public:
  145. HypotheticalGameState::TownModel * whichTown;
  146. int which; //which hero, which building, which creature,
  147. TownObjective(Type t,HypotheticalGameState::TownModel * tn,int which):whichTown(tn),which(which){type = t;_value = rand();}
  148. bool operator < (const TownObjective &other)const
  149. {
  150. if(type != other.type)
  151. return type<other.type;
  152. if(whichTown->t->id!=other.whichTown->t->id)
  153. return whichTown->t->id < other.whichTown->t->id;
  154. return false;
  155. }
  156. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  157. float getValue() const {return _value;}
  158. private:
  159. float _value;
  160. };
  161. class AIObjectivePtrCont
  162. {
  163. public:
  164. AIObjectivePtrCont():obj(NULL){}
  165. AIObjectivePtrCont(AIObjective * obj):obj(obj){};
  166. AIObjective * obj;
  167. bool operator < (const AIObjectivePtrCont & other) const
  168. {return obj->getValue()<other.obj->getValue();}
  169. };
  170. std::set<HeroObjective> currentHeroObjectives; //can be fulfilled right now
  171. std::set<TownObjective> currentTownObjectives;
  172. public:
  173. CGeniusAI();
  174. virtual ~CGeniusAI();
  175. virtual void init(ICallback * CB);
  176. virtual void yourTurn();
  177. virtual void heroKilled(const CGHeroInstance *);
  178. virtual void heroCreated(const CGHeroInstance *);
  179. virtual void heroMoved(const TryMoveHero &);
  180. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val) {};
  181. virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID){};
  182. 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.
  183. virtual void tileRevealed(int3 pos);
  184. virtual void tileHidden(int3 pos);
  185. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  186. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd);
  187. virtual void playerBlocked(int reason);
  188. // battle
  189. virtual void actionFinished(const BattleAction *action);//occurs AFTER every action taken by any stack or by the hero
  190. virtual void actionStarted(const BattleAction *action);//occurs BEFORE every action taken by any stack or by the hero
  191. virtual void battleAttack(BattleAttack *ba); //called when stack is performing attack
  192. virtual void battleStacksAttacked(std::set<BattleStackAttacked> & bsa); //called when stack receives damage (after battleAttack())
  193. virtual void battleEnd(BattleResult *br);
  194. 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
  195. virtual void battleStackMoved(int ID, int dest, int distance, bool end);
  196. virtual void battleSpellCast(SpellCast *sc);
  197. 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
  198. virtual void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  199. //
  200. virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  201. virtual void battleStackAttacking(int ID, int dest);
  202. virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
  203. virtual BattleAction activeStack(int stackID);
  204. void battleResultsApplied();
  205. };
  206. }
  207. #endif // __CGENIUSAI_H__