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 "StackWithBonuses.h"
  14. #define BATTLE_TRACE_LEVEL 0
  15. class DamageCache
  16. {
  17. private:
  18. std::unordered_map<uint32_t, std::unordered_map<uint32_t, float>> damageCache;
  19. DamageCache * parent;
  20. public:
  21. DamageCache() : parent(nullptr) {}
  22. DamageCache(DamageCache * parent) : parent(parent) {}
  23. void cacheDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  24. int64_t getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  25. int64_t getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  26. void buildDamageCache(std::shared_ptr<HypotheticBattle> hb, BattleSide side);
  27. };
  28. /// <summary>
  29. /// Evaluate attack value of one particular attack taking into account various effects like
  30. /// retaliation, 2-hex breath, collateral damage, shooters blocked damage
  31. /// </summary>
  32. class AttackPossibility
  33. {
  34. public:
  35. BattleHex from; //tile from which we attack
  36. BattleHex dest; //tile which we attack
  37. BattleAttackInfo attack;
  38. std::shared_ptr<battle::CUnitState> attackerState;
  39. std::vector<std::shared_ptr<battle::CUnitState>> affectedUnits;
  40. float defenderDamageReduce = 0;
  41. float attackerDamageReduce = 0; //usually by counter-attack
  42. float collateralDamageReduce = 0; // friendly fire (usually by two-hex attacks)
  43. int64_t shootersBlockedDmg = 0;
  44. bool defenderDead = false;
  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. };