AttackPossibility.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * AttackPossibility.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/battle/CUnitState.h"
  12. #include "../../CCallback.h"
  13. #include "common.h"
  14. #include "StackWithBonuses.h"
  15. #define BATTLE_TRACE_LEVEL 0
  16. class DamageCache
  17. {
  18. private:
  19. std::unordered_map<uint32_t, std::unordered_map<uint32_t, float>> damageCache;
  20. DamageCache * parent;
  21. public:
  22. DamageCache() : parent(nullptr) {}
  23. DamageCache(DamageCache * parent) : parent(parent) {}
  24. void cacheDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  25. int64_t getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  26. int64_t getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  27. void buildDamageCache(std::shared_ptr<HypotheticBattle> hb, int side);
  28. };
  29. /// <summary>
  30. /// Evaluate attack value of one particular attack taking into account various effects like
  31. /// retaliation, 2-hex breath, collateral damage, shooters blocked damage
  32. /// </summary>
  33. class AttackPossibility
  34. {
  35. public:
  36. BattleHex from; //tile from which we attack
  37. BattleHex dest; //tile which we attack
  38. BattleAttackInfo attack;
  39. std::shared_ptr<battle::CUnitState> attackerState;
  40. std::vector<std::shared_ptr<battle::CUnitState>> affectedUnits;
  41. float defenderDamageReduce = 0;
  42. float attackerDamageReduce = 0; //usually by counter-attack
  43. float collateralDamageReduce = 0; // friendly fire (usually by two-hex attacks)
  44. int64_t shootersBlockedDmg = 0;
  45. AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack_);
  46. float damageDiff() const;
  47. float attackValue() const;
  48. float damageDiff(float positiveEffectMultiplier, float negativeEffectMultiplier) const;
  49. static AttackPossibility evaluate(
  50. const BattleAttackInfo & attackInfo,
  51. BattleHex hex,
  52. DamageCache & damageCache,
  53. std::shared_ptr<CBattleInfoCallback> state);
  54. static float calculateDamageReduce(
  55. const battle::Unit * attacker,
  56. const battle::Unit * defender,
  57. uint64_t damageDealt,
  58. DamageCache & damageCache,
  59. std::shared_ptr<CBattleInfoCallback> cb);
  60. private:
  61. static int64_t evaluateBlockedShootersDmg(
  62. const BattleAttackInfo & attackInfo,
  63. BattleHex hex,
  64. DamageCache & damageCache,
  65. std::shared_ptr<CBattleInfoCallback> state);
  66. };