CGeniusAI.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #pragma once
  2. #include "Common.h"
  3. #include "BattleLogic.h"
  4. #include "GeneralAI.h"
  5. #include "../../lib/CondSh.h"
  6. class CBuilding;
  7. namespace geniusai {
  8. enum BattleState
  9. {
  10. NO_BATTLE,
  11. UPCOMING_BATTLE,
  12. ONGOING_BATTLE,
  13. ENDING_BATTLE
  14. };
  15. class Priorities;
  16. class CGeniusAI : public CGlobalAI
  17. {
  18. private:
  19. // TODO: cb... come back, croach busters!?
  20. CCallback* m_cb;
  21. geniusai::BattleAI::CBattleLogic* m_battleLogic;
  22. geniusai::GeneralAI::CGeneralAI m_generalAI;
  23. geniusai::Priorities* m_priorities;
  24. CondSh<BattleState> m_state; //are we engaged into battle?
  25. struct AIObjectContainer
  26. {
  27. AIObjectContainer(const CGObjectInstance * o):o(o){}
  28. const CGObjectInstance * o;
  29. bool operator<(const AIObjectContainer& b)const;
  30. };
  31. class HypotheticalGameState
  32. {
  33. public:
  34. struct HeroModel
  35. {
  36. HeroModel(){}
  37. HeroModel(const CGHeroInstance * h);
  38. int3 pos;
  39. int3 previouslyVisited_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. CGeniusAI * AI;
  57. std::vector<const CGHeroInstance *> AvailableHeroesToBuy;
  58. std::vector<ui32> resourceAmounts;
  59. std::vector<HeroModel> heroModels;
  60. std::vector<TownModel> townModels;
  61. std::set< AIObjectContainer > knownVisitableObjects;
  62. };
  63. class AIObjective
  64. {
  65. public:
  66. enum Type
  67. {
  68. //hero objectives
  69. visit, //done TODO: upon visit friendly hero, trade
  70. attack, //done
  71. //flee,
  72. dismissUnits,
  73. dismissYourself,
  74. rearangeTroops,
  75. finishTurn, //done //uses up remaining motion to get somewhere interesting.
  76. //town objectives
  77. recruitHero, //done
  78. buildBuilding, //done
  79. recruitCreatures, //done
  80. upgradeCreatures //done
  81. };
  82. CGeniusAI * AI;
  83. Type type;
  84. virtual void fulfill(CGeniusAI &,HypotheticalGameState & hgs)=0;
  85. virtual HypotheticalGameState pretend(const HypotheticalGameState&) =0;
  86. virtual void print() const=0;
  87. virtual double getValue() const=0; //how much is it worth to the AI to achieve
  88. };
  89. class HeroObjective: public AIObjective
  90. {
  91. public:
  92. HypotheticalGameState hgs;
  93. int3 pos;
  94. const CGObjectInstance * object; //interactive object
  95. mutable std::vector<HypotheticalGameState::HeroModel*> whoCanAchieve;
  96. //HeroObjective(){}
  97. //HeroObjective(Type t):object(NULL){type = t;}
  98. HeroObjective(const HypotheticalGameState &hgs,
  99. Type t,
  100. const CGObjectInstance* object,
  101. HypotheticalGameState::HeroModel* h,
  102. CGeniusAI* AI);
  103. bool operator< (const HeroObjective &other) const;
  104. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  105. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  106. double getValue() const;
  107. void print() const;
  108. private:
  109. mutable double _value;
  110. mutable double _cost;
  111. };
  112. //town objectives
  113. //recruitHero
  114. //buildBuilding
  115. //recruitCreatures
  116. //upgradeCreatures
  117. class TownObjective: public AIObjective
  118. {
  119. public:
  120. HypotheticalGameState hgs;
  121. HypotheticalGameState::TownModel * whichTown;
  122. int which; //which hero, which building, which creature,
  123. TownObjective(const HypotheticalGameState &hgs,Type t,HypotheticalGameState::TownModel * tn,int Which,CGeniusAI * AI);
  124. bool operator < (const TownObjective &other)const;
  125. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  126. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  127. double getValue() const;
  128. void print() const;
  129. private:
  130. mutable double _value;
  131. mutable double _cost;
  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(CCallback * 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, bool removableUnits, 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(const BattleAttack *ba); //called when stack is performing attack
  175. virtual void battleStacksAttacked(const std::set<BattleStackAttacked> & bsa); //called when stack receives damage (after battleAttack())
  176. virtual void battleEnd(const 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, std::vector<SBattleHex> dest, int distance);
  179. virtual void battleSpellCast(const BattleSpellCast *sc);
  180. 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
  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(const CStack * stack);
  187. void battleResultsApplied();
  188. friend class Priorities;
  189. };
  190. }