AttackPossibility.h 2.8 KB

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