DefenceBehavior.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * DefenceBehavior.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. #include "StdInc.h"
  11. #include "DefenceBehavior.h"
  12. #include "../VCAI.h"
  13. #include "../Engine/Nullkiller.h"
  14. #include "../AIhelper.h"
  15. #include "../AIUtility.h"
  16. #include "../Goals/BuyArmy.h"
  17. #include "../Goals/VisitTile.h"
  18. #include "../Goals/ExecuteHeroChain.h"
  19. #include "../Goals/ExchangeSwapTownHeroes.h"
  20. #include "lib/mapping/CMap.h" //for victory conditions
  21. #include "lib/CPathfinder.h"
  22. extern boost::thread_specific_ptr<CCallback> cb;
  23. extern boost::thread_specific_ptr<VCAI> ai;
  24. extern FuzzyHelper * fh;
  25. using namespace Goals;
  26. std::string DefenceBehavior::toString() const
  27. {
  28. return "Defend towns";
  29. }
  30. Goals::TGoalVec DefenceBehavior::getTasks()
  31. {
  32. Goals::TGoalVec tasks;
  33. auto heroes = cb->getHeroesInfo();
  34. if(heroes.size())
  35. {
  36. for(auto town : cb->getTownsInfo())
  37. {
  38. evaluateDefence(tasks, town);
  39. }
  40. }
  41. return tasks;
  42. }
  43. uint64_t townArmyIncome(const CGTownInstance * town)
  44. {
  45. uint64_t result = 0;
  46. for(auto creatureInfo : town->creatures)
  47. {
  48. if(creatureInfo.second.empty())
  49. continue;
  50. auto creature = creatureInfo.second.back().toCreature();
  51. result += creature->AIValue * town->getGrowthInfo(creature->level).totalGrowth();
  52. }
  53. return result;
  54. }
  55. void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInstance * town)
  56. {
  57. auto basicPriority = 0.3f + std::sqrt(townArmyIncome(town) / 20000.0f)
  58. + town->dailyIncome()[Res::GOLD] / 10000.0f;
  59. logAi->debug("Evaluating defence for %s, basic priority %f", town->name, basicPriority);
  60. auto treatNode = ai->nullkiller->dangerHitMap->getObjectTreat(town);
  61. auto treats = { treatNode.fastestDanger, treatNode.maximumDanger };
  62. if(!treatNode.fastestDanger.hero)
  63. {
  64. logAi->debug("No treat found for town %s", town->name);
  65. return;
  66. }
  67. int dayOfWeek = cb->getDate(Date::DAY_OF_WEEK);
  68. if(town->garrisonHero)
  69. {
  70. if(!ai->nullkiller->isHeroLocked(town->garrisonHero.get()))
  71. {
  72. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, nullptr).setpriority(5)));
  73. return;
  74. }
  75. logAi->debug(
  76. "Hero %s in garrison of town %s is suposed to defend the town",
  77. town->garrisonHero->name,
  78. town->name);
  79. return;
  80. }
  81. uint64_t reinforcement = ai->ah->howManyReinforcementsCanBuy(town->getUpperArmy(), town);
  82. if(reinforcement)
  83. {
  84. logAi->debug("Town %s can buy defence army %lld", town->name, reinforcement);
  85. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(0.5f)));
  86. }
  87. auto paths = ai->ah->getPathsToTile(town->visitablePos());
  88. if(paths.empty())
  89. {
  90. logAi->debug("No ways to defend town %s", town->name);
  91. return;
  92. }
  93. for(auto & treat : treats)
  94. {
  95. logAi->debug(
  96. "Town %s has treat %lld in %s turns, hero: %s",
  97. town->name,
  98. treat.danger,
  99. std::to_string(treat.turn),
  100. treat.hero->name);
  101. bool treatIsUnderControl = false;
  102. for(AIPath & path : paths)
  103. {
  104. if(path.getHeroStrength() > treat.danger)
  105. {
  106. if(dayOfWeek + treat.turn < 6 && isSafeToVisit(path.targetHero, path.heroArmy, treat.danger)
  107. || path.exchangeCount == 1 && path.turn() < treat.turn
  108. || path.turn() < treat.turn - 1)
  109. {
  110. logAi->debug(
  111. "Hero %s can eliminate danger for town %s using path %s.",
  112. path.targetHero->name,
  113. town->name,
  114. path.toString());
  115. treatIsUnderControl = true;
  116. break;
  117. }
  118. }
  119. }
  120. if(treatIsUnderControl)
  121. continue;
  122. if(ai->canRecruitAnyHero(town))
  123. {
  124. auto heroesInTavern = cb->getAvailableHeroes(town);
  125. for(auto hero : heroesInTavern)
  126. {
  127. if(hero->getTotalStrength() > treat.danger)
  128. {
  129. tasks.push_back(Goals::sptr(Goals::RecruitHero().settown(town).setobjid(hero->id.getNum()).setpriority(1)));
  130. continue;
  131. }
  132. }
  133. }
  134. for(AIPath & path : paths)
  135. {
  136. #if AI_TRACE_LEVEL >= 1
  137. logAi->trace(
  138. "Hero %s can defend town with force %lld in %s turns, cost: %f, path: %s",
  139. path.targetHero->name,
  140. path.getHeroStrength(),
  141. std::to_string(path.turn()),
  142. path.movementCost(),
  143. path.toString());
  144. #endif
  145. float priority = basicPriority
  146. + std::min(SAFE_ATTACK_CONSTANT, (float)path.getHeroStrength() / treat.danger) / (treat.turn + 1);
  147. if(path.targetHero == town->visitingHero && path.exchangeCount == 1)
  148. {
  149. #if AI_TRACE_LEVEL >= 1
  150. logAi->trace("Put %s to garrison of town %s with priority %f",
  151. path.targetHero->name,
  152. town->name,
  153. priority);
  154. #endif
  155. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, town->visitingHero.get()).setpriority(priority)));
  156. continue;
  157. }
  158. if(path.turn() <= treat.turn && path.getHeroStrength() * SAFE_ATTACK_CONSTANT >= treat.danger)
  159. {
  160. #if AI_TRACE_LEVEL >= 1
  161. logAi->trace("Move %s to defend town %s with priority %f",
  162. path.targetHero->name,
  163. town->name,
  164. priority);
  165. #endif
  166. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, town).setpriority(priority)));
  167. continue;
  168. }
  169. }
  170. }
  171. logAi->debug("Found %d tasks", tasks.size());
  172. /*for(auto & treat : treats)
  173. {
  174. auto paths = ai->ah->getPathsToTile(treat.hero->visitablePos());
  175. for(AIPath & path : paths)
  176. {
  177. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path)));
  178. }
  179. }*/
  180. }