BattleLogic.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "Common.h"
  3. #include "BattleHelper.h"
  4. #pragma warning (disable: 4100 4251 4245 4018 4081)
  5. #include "../../CCallback.h"
  6. #include "../../lib/CCreatureHandler.h"
  7. #include "../../lib/CObjectHandler.h"
  8. #pragma warning (default: 4100 4251 4245 4018 4081)
  9. #pragma warning (disable: 4100)
  10. using namespace std;
  11. namespace geniusai { namespace BattleAI {
  12. /**
  13. * Class is responsible for making decision during the battle.
  14. */
  15. class CBattleLogic
  16. {
  17. private:
  18. enum EMainStrategyType
  19. {
  20. strategy_super_aggresive,
  21. strategy_aggresive,
  22. strategy_neutral,
  23. strategy_defensive,
  24. strategy_super_defensive,
  25. strategy_berserk_attack /** cause max damage, usually when creatures fight against overwhelming army*/
  26. };
  27. enum ECreatureRoleInBattle
  28. {
  29. creature_role_shooter,
  30. creature_role_defenser,
  31. creature_role_fast_attacker,
  32. creature_role_attacker
  33. };
  34. enum EActionType
  35. {
  36. action_cancel = 0, // Cancel BattleAction
  37. action_cast_spell = 1, // Hero cast a spell
  38. action_walk = 2, // Walk
  39. action_defend = 3, // Defend
  40. action_retreat = 4, // Retreat from the battle
  41. action_surrender = 5, // Surrender
  42. action_walk_and_attack = 6, // Walk and Attack
  43. action_shoot = 7, // Shoot
  44. action_wait = 8, // Wait
  45. action_catapult = 9, // Catapult
  46. action_monster_casts_spell = 10 // Monster casts a spell (i.e. Faerie Dragons)
  47. };
  48. struct SCreatureCasualties
  49. {
  50. int amount_max; // amount of creatures that will be dead
  51. int amount_min;
  52. int damage_max; // number of hit points that creature will lost
  53. int damage_min;
  54. int leftHitPoints_for_max; // number of hit points that will remain (for last unity)
  55. int leftHitPoint_for_min; // scenario
  56. };
  57. public:
  58. CBattleLogic(CCallback *cb, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side);
  59. ~CBattleLogic();
  60. void SetCurrentTurn(int turn);
  61. /**
  62. * Get the final decision.
  63. */
  64. BattleAction MakeDecision(int stackID);
  65. private:
  66. CBattleHelper m_battleHelper;
  67. //BattleInfo m_battleInfo;
  68. int m_iCurrentTurn;
  69. bool m_bIsAttacker;
  70. CCallback *m_cb;
  71. const CCreatureSet *m_army1;
  72. const CCreatureSet *m_army2;
  73. int3 m_tile;
  74. const CGHeroInstance *m_hero1;
  75. const CGHeroInstance *m_hero2;
  76. bool m_side;
  77. // statistics
  78. typedef std::vector<std::pair<int, int> > creature_stat; // first - creature id, second - value
  79. creature_stat m_statMaxDamage;
  80. creature_stat m_statMinDamage;
  81. //
  82. creature_stat m_statMaxSpeed;
  83. creature_stat m_statDistance;
  84. creature_stat m_statDistanceFromShooters;
  85. creature_stat m_statHitPoints;
  86. typedef std::vector<std::pair<int, SCreatureCasualties> > creature_stat_casualties;
  87. creature_stat_casualties m_statCasualties;
  88. bool m_bEnemyDominates;
  89. /**
  90. * Before decision we have to make some calculation and simulation.
  91. */
  92. void MakeStatistics(int currentCreatureId);
  93. /**
  94. * Helper function. It's used for performing an attack action.
  95. */
  96. std::vector<int> GetAvailableHexesForAttacker(const CStack *defender, const CStack *attacker = NULL);
  97. /**
  98. * Make an attack action if it's possible.
  99. * If it's not possible then function returns defend action.
  100. */
  101. BattleAction MakeAttack(int attackerID, int destinationID);
  102. /**
  103. * Berserk mode - do maximum casualties.
  104. * Return vector wiht IDs of creatures to attack,
  105. * additional info: -2 means wait, -1 - defend, 0 - make attack
  106. */
  107. list<int> PerformBerserkAttack(int stackID, int &additionalInfo);
  108. /**
  109. * Normal mode - equilibrium between casualties and yields.
  110. * Return vector wiht IDs of creatures to attack,
  111. * additional info: -2 means wait, -1 - defend, 0 - make attack
  112. */
  113. list<int> PerformDefaultAction(int stackID, int &additionalInfo);
  114. /**
  115. * Only for debug purpose.
  116. */
  117. void PrintBattleAction(const BattleAction &action);
  118. };
  119. }}