2
0

ThreatMap.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * ThreatMap.cpp, 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. /*
  11. #include "ThreatMap.h"
  12. #include "StdInc.h"
  13. template <typename Container, typename Pred>
  14. auto sum(const Container & c, Pred p) -> decltype(p(*std::begin(c)))
  15. {
  16. double ret = 0;
  17. for(const auto &element : c)
  18. {
  19. ret += p(element);
  20. }
  21. return ret;
  22. }
  23. ThreatMap::ThreatMap(const CStack *Endangered) : endangered(Endangered)
  24. {
  25. sufferedDamage.fill(0);
  26. for(const CStack *enemy : getCbc()->battleGetStacks())
  27. {
  28. //Consider only stacks of different owner
  29. if(enemy->attackerOwned == endangered->attackerOwned)
  30. continue;
  31. //Look-up which tiles can be melee-attacked
  32. std::array<bool, GameConstants::BFIELD_SIZE> meleeAttackable;
  33. meleeAttackable.fill(false);
  34. auto enemyReachability = getCbc()->getReachability(enemy);
  35. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  36. {
  37. if(enemyReachability.isReachable(i))
  38. {
  39. meleeAttackable[i] = true;
  40. for(auto n : BattleHex(i).neighbouringTiles())
  41. meleeAttackable[n] = true;
  42. }
  43. }
  44. //Gather possible assaults
  45. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  46. {
  47. if(getCbc()->battleCanShoot(enemy, i))
  48. threatMap[i].push_back(BattleAttackInfo(enemy, endangered, true));
  49. else if(meleeAttackable[i])
  50. {
  51. BattleAttackInfo bai(enemy, endangered, false);
  52. bai.chargedFields = std::max(BattleHex::getDistance(enemy->position, i) - 1, 0); //TODO check real distance (BFS), not just metric
  53. threatMap[i].push_back(BattleAttackInfo(bai));
  54. }
  55. }
  56. }
  57. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  58. {
  59. sufferedDamage[i] = sum(threatMap[i], [](const BattleAttackInfo &bai) -> int
  60. {
  61. auto dmg = getCbc()->calculateDmgRange(bai);
  62. return (dmg.first + dmg.second)/2;
  63. });
  64. }
  65. }
  66. */ // These lines may be usefull but they are't used in the code.