CGeniusAI.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "../../global.h"
  9. //#include "../../client/CGameInfo.h"
  10. #include <set>
  11. #include <list>
  12. #include <queue>
  13. class CBuilding;
  14. namespace GeniusAI {
  15. enum BattleState
  16. {
  17. NO_BATTLE,
  18. UPCOMING_BATTLE,
  19. ONGOING_BATTLE,
  20. ENDING_BATTLE
  21. };
  22. class CGeniusAI : public CGlobalAI
  23. {
  24. private:
  25. ICallback* m_cb;
  26. GeniusAI::BattleAI::CBattleLogic* m_battleLogic;
  27. GeniusAI::GeneralAI::CGeneralAI m_generalAI;
  28. CondSh<BattleState> m_state; //are we engaged into battle?
  29. class AIObjectContainer
  30. {
  31. public:
  32. AIObjectContainer(const CGObjectInstance * o):o(o){}
  33. const CGObjectInstance * o;
  34. bool operator<(const AIObjectContainer& b)const
  35. {
  36. if (o->pos!=b.o->pos)
  37. return o->pos<b.o->pos;
  38. return o->id<b.o->id;
  39. }
  40. };
  41. class HypotheticalGameState
  42. {
  43. public:
  44. HypotheticalGameState(){}
  45. HypotheticalGameState(CGeniusAI & AI)
  46. :knownVisitableObjects(AI.knownVisitableObjects)
  47. {
  48. std::vector < const CGHeroInstance *> heroes = AI.m_cb->getHeroesInfo();
  49. for(std::vector < const CGHeroInstance *>::iterator i = heroes.begin(); i != heroes.end(); i++)
  50. heroModels.push_back(HeroModel(*i));
  51. std::vector < const CGTownInstance *> towns = AI.m_cb->getTownsInfo();
  52. for(std::vector < const CGTownInstance *>::iterator i = towns.begin(); i != towns.end(); i++)
  53. townModels.push_back(TownModel(*i));
  54. if(AI.m_cb->howManyTowns()!=0)
  55. AvailableHeroesToBuy = AI.m_cb->getAvailableHeroes(AI.m_cb->getTownInfo(0,0));
  56. for(int i = 0; i < 7;i++)resourceAmounts.push_back(AI.m_cb->getResourceAmount(i));
  57. }
  58. class TownModel
  59. {
  60. public:
  61. TownModel(const CGTownInstance *t):t(t){visitingHero=(t->visitingHero!=NULL);}
  62. const CGTownInstance *t;
  63. bool visitingHero;
  64. };
  65. class HeroModel
  66. {
  67. public:
  68. HeroModel(const CGHeroInstance * h):h(h){
  69. pos = h->getPosition(false);remainingMovement = h->movement;
  70. }
  71. int3 pos;
  72. int remainingMovement;
  73. const CGHeroInstance * h;
  74. };
  75. std::vector<const CGHeroInstance *> AvailableHeroesToBuy;
  76. std::vector<int> resourceAmounts;
  77. std::vector<HeroModel> heroModels;
  78. std::vector<TownModel> townModels;
  79. std::set< AIObjectContainer > knownVisitableObjects;
  80. };
  81. class AIObjective
  82. {
  83. public:
  84. enum Type
  85. {
  86. //hero objectives
  87. visit,
  88. attack,
  89. flee,
  90. dismissUnits,
  91. dismissYourself,
  92. finishTurn, //uses up remaining motion to get somewhere nice.
  93. //town objectives
  94. recruitHero,
  95. recruitCreatures,
  96. upgradeCreatures,
  97. buildBuilding
  98. };
  99. Type type;
  100. //virtual bool operator < (const AIObjective &)const=0;
  101. //virtual bool stillPossible(const HypotheticalGameState &)const = 0;
  102. virtual void fulfill(CGeniusAI &,HypotheticalGameState & hgs)=0;
  103. virtual HypotheticalGameState pretend(const HypotheticalGameState &) =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. void fulfill(CGeniusAI &,HypotheticalGameState & hgs);
  132. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return 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. HypotheticalGameState pretend(const HypotheticalGameState &hgs){return hgs;};
  158. float getValue() const {return _value;}
  159. private:
  160. float _value;
  161. };
  162. class AIObjectivePtrCont
  163. {
  164. public:
  165. AIObjectivePtrCont():obj(NULL){}
  166. AIObjectivePtrCont(AIObjective * obj):obj(obj){};
  167. AIObjective * obj;
  168. bool operator < (const AIObjectivePtrCont & other) const
  169. {return obj->getValue()<other.obj->getValue();}
  170. };
  171. HypotheticalGameState trueGameState;
  172. AIObjective * getBestObjective();
  173. void addHeroObjectives(HypotheticalGameState::HeroModel &h, HypotheticalGameState & hgs);
  174. void addTownObjectives(HypotheticalGameState::TownModel &h, HypotheticalGameState & hgs);
  175. void fillObjectiveQueue(HypotheticalGameState & hgs);
  176. void reportResources();
  177. int turn;
  178. bool firstTurn;
  179. std::set< AIObjectContainer > knownVisitableObjects;
  180. std::set<HeroObjective> currentHeroObjectives; //can be fulfilled right now
  181. std::set<TownObjective> currentTownObjectives;
  182. std::vector<AIObjectivePtrCont> objectiveQueue;
  183. public:
  184. CGeniusAI();
  185. virtual ~CGeniusAI();
  186. virtual void init(ICallback * CB);
  187. virtual void yourTurn();
  188. virtual void heroKilled(const CGHeroInstance *);
  189. virtual void heroCreated(const CGHeroInstance *);
  190. virtual void heroMoved(const TryMoveHero &);
  191. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val) {};
  192. virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID){};
  193. 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.
  194. virtual void tileRevealed(int3 pos);
  195. virtual void tileHidden(int3 pos);
  196. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  197. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd);
  198. virtual void playerBlocked(int reason);
  199. virtual void objectRemoved(const CGObjectInstance *obj); //eg. collected resource, picked artifact, beaten hero
  200. virtual void newObject(const CGObjectInstance * obj); //eg. ship built in shipyard
  201. // battle
  202. virtual void actionFinished(const BattleAction *action);//occurs AFTER every action taken by any stack or by the hero
  203. virtual void actionStarted(const BattleAction *action);//occurs BEFORE every action taken by any stack or by the hero
  204. virtual void battleAttack(BattleAttack *ba); //called when stack is performing attack
  205. virtual void battleStacksAttacked(std::set<BattleStackAttacked> & bsa); //called when stack receives damage (after battleAttack())
  206. virtual void battleEnd(BattleResult *br);
  207. 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
  208. virtual void battleStackMoved(int ID, int dest, int distance, bool end);
  209. virtual void battleSpellCast(SpellCast *sc);
  210. 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
  211. virtual void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  212. //
  213. virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  214. virtual void battleStackAttacking(int ID, int dest);
  215. virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
  216. virtual BattleAction activeStack(int stackID);
  217. void battleResultsApplied();
  218. };
  219. }
  220. #endif // __CGENIUSAI_H__