CGeniusAI.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef __GENIUS_AI__
  2. #define __GENIUS_AI__
  3. #define VCMI_DLL
  4. #pragma warning (disable: 4100 4251 4245 4018 4081)
  5. #include "../../AI_Base.h"
  6. #include "../../CCallback.h"
  7. #include "../../hch/CCreatureHandler.h"
  8. #pragma warning (default: 4100 4251 4245 4018 4081)
  9. namespace GeniusAI {
  10. class CBattleHelper
  11. {
  12. public:
  13. CBattleHelper();
  14. ~CBattleHelper();
  15. int GetBattleFieldPosition(int x, int y);
  16. int DecodeXPosition(int battleFieldPosition);
  17. int DecodeYPosition(int battleFieldPosition);
  18. int GetShortestDistance(int pointA, int pointB);
  19. int GetDistanceWithObstacles(int pointA, int pointB);
  20. int GetVoteForMaxDamage() const { return m_voteForMaxDamage; }
  21. int GetVoteForMinDamage() const { return m_voteForMinDamage; }
  22. int GetVoteForMaxSpeed() const { return m_voteForMaxSpeed; }
  23. int GetVoteForDistance() const { return m_voteForDistance; }
  24. int GetVoteForDistanceFromShooters() const { return m_voteForDistanceFromShooters; }
  25. int GetVoteForHitPoints() const { return m_voteForHitPoints; }
  26. const int InfiniteDistance;
  27. const int BattlefieldWidth;
  28. const int BattlefieldHeight;
  29. private:
  30. int m_voteForMaxDamage;
  31. int m_voteForMinDamage;
  32. int m_voteForMaxSpeed;
  33. int m_voteForDistance;
  34. int m_voteForDistanceFromShooters;
  35. int m_voteForHitPoints;
  36. };
  37. /**
  38. * Maybe it is a additional proxy, but i've separated the game IA for transparent.
  39. */
  40. class CBattleLogic
  41. {
  42. private:
  43. enum EMainStrategyType
  44. {
  45. strategy_super_aggresive,
  46. strategy_aggresive,
  47. strategy_neutral,
  48. strategy_defensive,
  49. strategy_super_defensive,
  50. strategy_stupid_attack /** only for debug and test purpose */
  51. };
  52. enum ECreatureRoleInBattle
  53. {
  54. creature_role_shooter,
  55. creature_role_defenser,
  56. creature_role_fast_attacker,
  57. creature_role_attacker
  58. };
  59. enum EActionType
  60. {
  61. action_cancel = 0, // Cancel BattleAction
  62. action_cast_spell = 1, // Hero cast a spell
  63. action_walk = 2, // Walk
  64. action_defend = 3, // Defend
  65. action_retreat = 4, // Retreat from the battle
  66. action_surrender = 5, // Surrender
  67. action_walk_and_attack = 6, // Walk and Attack
  68. action_shoot = 7, // Shoot
  69. action_wait = 8, // Wait
  70. action_catapult = 9, // Catapult
  71. action_monster_casts_spell = 10 // Monster casts a spell (i.e. Faerie Dragons)
  72. };
  73. public:
  74. CBattleLogic(ICallback *cb);
  75. ~CBattleLogic();
  76. void SetCurrentTurn(int turn);
  77. BattleAction MakeDecision(int stackID);
  78. private:
  79. CBattleHelper m_battleHelper;
  80. int m_iCurrentTurn;
  81. bool m_bIsAttacker;
  82. ICallback *m_cb;
  83. void MakeStatistics(int currentCreatureId); // before decision we have to make some calculation and simulation
  84. // statistics
  85. typedef std::vector<std::pair<int, int> > creature_stat; // first - creature id, second - value
  86. creature_stat m_statMaxDamage;
  87. creature_stat m_statMinDamage;
  88. //
  89. creature_stat m_statMaxSpeed;
  90. creature_stat m_statDistance;
  91. creature_stat m_statDistanceFromShooters;
  92. creature_stat m_statHitPoints;
  93. std::vector<int> GetAvailableHexesForAttacker(CStack *defender);
  94. BattleAction MakeDefend(int stackID);
  95. };
  96. class CGeniusAI : public CGlobalAI
  97. {
  98. private:
  99. ICallback *m_cb;
  100. CBattleLogic *m_battleLogic;
  101. public:
  102. virtual void init(ICallback * CB);
  103. virtual void yourTurn();
  104. virtual void heroKilled(const CGHeroInstance *);
  105. virtual void heroCreated(const CGHeroInstance *);
  106. virtual void heroMoved(const HeroMoveDetails &);
  107. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val) {};
  108. virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID){};
  109. virtual void tileRevealed(int3 pos){};
  110. virtual void tileHidden(int3 pos){};
  111. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback);
  112. // battle
  113. virtual void actionFinished(const BattleAction *action);//occurs AFTER every action taken by any stack or by the hero
  114. virtual void actionStarted(const BattleAction *action);//occurs BEFORE every action taken by any stack or by the hero
  115. virtual void battleAttack(BattleAttack *ba); //called when stack is performing attack
  116. virtual void battleStackAttacked(BattleStackAttacked * bsa); //called when stack receives damage (after battleAttack())
  117. virtual void battleEnd(BattleResult *br);
  118. 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
  119. virtual void battleStackMoved(int ID, int dest);
  120. virtual void battleSpellCasted(SpellCasted *sc);
  121. 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
  122. virtual void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
  123. //
  124. virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
  125. virtual void battleStackAttacking(int ID, int dest);
  126. virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting);
  127. virtual BattleAction activeStack(int stackID);
  128. };
  129. }
  130. #endif/*__GENIUS_AI__*/