CGeniusAI.h 7.9 KB

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