AttackPossibility.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "StackWithBonuses.h"
  13. #define BATTLE_TRACE_LEVEL 0
  14. class DamageCache
  15. {
  16. private:
  17. std::unordered_map<uint32_t, std::unordered_map<uint32_t, float>> damageCache;
  18. std::map<BattleHex, std::unordered_map<uint32_t, int64_t>> obstacleDamage;
  19. DamageCache * parent;
  20. void buildObstacleDamageCache(std::shared_ptr<HypotheticBattle> hb, BattleSide side);
  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 getObstacleDamage(const BattleHex & hex, const battle::Unit * defender);
  27. int64_t getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
  28. void buildDamageCache(std::shared_ptr<HypotheticBattle> hb, BattleSide side);
  29. };
  30. /// <summary>
  31. /// Evaluate attack value of one particular attack taking into account various effects like
  32. /// retaliation, 2-hex breath, collateral damage, shooters blocked damage
  33. /// </summary>
  34. class AttackPossibility
  35. {
  36. public:
  37. BattleHex from; //tile from which we attack
  38. BattleHex dest; //tile which we attack
  39. BattleAttackInfo attack;
  40. std::shared_ptr<battle::CUnitState> attackerState;
  41. std::vector<std::shared_ptr<battle::CUnitState>> affectedUnits;
  42. float defenderDamageReduce = 0;
  43. float attackerDamageReduce = 0; //usually by counter-attack
  44. float collateralDamageReduce = 0; // friendly fire (usually by two-hex attacks)
  45. int64_t shootersBlockedDmg = 0;
  46. bool defenderDead = false;
  47. AttackPossibility(const BattleHex & from, const BattleHex & dest, const BattleAttackInfo & attack_);
  48. float damageDiff() const;
  49. float attackValue() const;
  50. float damageDiff(float positiveEffectMultiplier, float negativeEffectMultiplier) const;
  51. static AttackPossibility evaluate(
  52. const BattleAttackInfo & attackInfo,
  53. BattleHex hex,
  54. DamageCache & damageCache,
  55. std::shared_ptr<CBattleInfoCallback> state);
  56. static float calculateDamageReduce(
  57. const battle::Unit * attacker,
  58. const battle::Unit * defender,
  59. uint64_t damageDealt,
  60. DamageCache & damageCache,
  61. std::shared_ptr<CBattleInfoCallback> cb);
  62. private:
  63. static int64_t evaluateBlockedShootersDmg(
  64. const BattleAttackInfo & attackInfo,
  65. BattleHex hex,
  66. DamageCache & damageCache,
  67. std::shared_ptr<CBattleInfoCallback> state);
  68. };