DefenceBehavior.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/ExecuteHeroChain.h"
  18. #include "../Goals/DismissHero.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::decompose() const
  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) const
  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. if(!town->visitingHero)
  73. {
  74. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, nullptr).setpriority(5)));
  75. }
  76. return;
  77. }
  78. logAi->debug(
  79. "Hero %s in garrison of town %s is suposed to defend the town",
  80. town->garrisonHero->name,
  81. town->name);
  82. return;
  83. }
  84. uint64_t reinforcement = ai->ah->howManyReinforcementsCanBuy(town->getUpperArmy(), town);
  85. if(reinforcement)
  86. {
  87. logAi->debug("Town %s can buy defence army %lld", town->name, reinforcement);
  88. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(0.5f)));
  89. }
  90. auto paths = ai->ah->getPathsToTile(town->visitablePos());
  91. for(auto & treat : treats)
  92. {
  93. logAi->debug(
  94. "Town %s has treat %lld in %s turns, hero: %s",
  95. town->name,
  96. treat.danger,
  97. std::to_string(treat.turn),
  98. treat.hero->name);
  99. bool treatIsUnderControl = false;
  100. for(AIPath & path : paths)
  101. {
  102. if(path.getHeroStrength() > treat.danger)
  103. {
  104. if(path.turn() <= treat.turn && dayOfWeek + treat.turn < 6 && isSafeToVisit(path.targetHero, path.heroArmy, treat.danger)
  105. || path.exchangeCount == 1 && path.turn() < treat.turn
  106. || path.turn() < treat.turn - 1)
  107. {
  108. logAi->debug(
  109. "Hero %s can eliminate danger for town %s using path %s.",
  110. path.targetHero->name,
  111. town->name,
  112. path.toString());
  113. treatIsUnderControl = true;
  114. break;
  115. }
  116. }
  117. }
  118. if(treatIsUnderControl)
  119. continue;
  120. if(!town->visitingHero
  121. && town->hasBuilt(BuildingID::TAVERN)
  122. && cb->getResourceAmount(Res::GOLD) > GameConstants::HERO_GOLD_COST)
  123. {
  124. auto heroesInTavern = cb->getAvailableHeroes(town);
  125. for(auto hero : heroesInTavern)
  126. {
  127. if(hero->getTotalStrength() > treat.danger)
  128. {
  129. auto myHeroes = cb->getHeroesInfo();
  130. if(cb->getHeroesInfo().size() < ALLOWED_ROAMING_HEROES)
  131. {
  132. logAi->debug("Hero %s can be recruited to defend %s", hero->name, town->name);
  133. tasks.push_back(Goals::sptr(Goals::RecruitHero(town, hero).setpriority(1)));
  134. continue;
  135. }
  136. else
  137. {
  138. const CGHeroInstance * weakestHero = nullptr;
  139. for(auto existingHero : myHeroes)
  140. {
  141. if(ai->nullkiller->isHeroLocked(existingHero)
  142. || existingHero->getArmyStrength() > hero->getArmyStrength()
  143. || ai->ah->getHeroRole(existingHero) == HeroRole::MAIN
  144. || existingHero->movement
  145. || existingHero->artifactsWorn.size() > (existingHero->hasSpellbook() ? 2 : 1))
  146. continue;
  147. if(!weakestHero || weakestHero->getFightingStrength() > existingHero->getFightingStrength())
  148. {
  149. weakestHero = existingHero;
  150. }
  151. if(weakestHero)
  152. {
  153. tasks.push_back(Goals::sptr(Goals::DismissHero(weakestHero)));
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if(paths.empty())
  161. {
  162. logAi->debug("No ways to defend town %s", town->name);
  163. continue;
  164. }
  165. for(AIPath & path : paths)
  166. {
  167. #if AI_TRACE_LEVEL >= 1
  168. logAi->trace(
  169. "Hero %s can defend town with force %lld in %s turns, cost: %f, path: %s",
  170. path.targetHero->name,
  171. path.getHeroStrength(),
  172. std::to_string(path.turn()),
  173. path.movementCost(),
  174. path.toString());
  175. #endif
  176. if(path.turn() <= treat.turn - 2)
  177. {
  178. logAi->trace("Deffer defence of %s by %s because he has enough time to rich the town next trun",
  179. town->name,
  180. path.targetHero->name);
  181. continue;
  182. }
  183. float priority = basicPriority
  184. + std::min(SAFE_ATTACK_CONSTANT, (float)path.getHeroStrength() / treat.danger) / (treat.turn + 1);
  185. if(path.targetHero == town->visitingHero && path.exchangeCount == 1)
  186. {
  187. #if AI_TRACE_LEVEL >= 1
  188. logAi->trace("Put %s to garrison of town %s with priority %f",
  189. path.targetHero->name,
  190. town->name,
  191. priority);
  192. #endif
  193. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, town->visitingHero.get(), HeroLockedReason::DEFENCE).setpriority(priority)));
  194. continue;
  195. }
  196. if(path.turn() <= treat.turn && path.getHeroStrength() * SAFE_ATTACK_CONSTANT >= treat.danger)
  197. {
  198. if(ai->nullkiller->arePathHeroesLocked(path))
  199. {
  200. #if AI_TRACE_LEVEL >= 1
  201. logAi->trace("Can not move %s to defend town %s with priority %f. Path is locked.",
  202. path.targetHero->name,
  203. town->name,
  204. priority);
  205. #endif
  206. continue;
  207. }
  208. #if AI_TRACE_LEVEL >= 1
  209. logAi->trace("Move %s to defend town %s with priority %f",
  210. path.targetHero->name,
  211. town->name,
  212. priority);
  213. #endif
  214. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, town).setpriority(priority)));
  215. }
  216. }
  217. }
  218. logAi->debug("Found %d tasks", tasks.size());
  219. /*for(auto & treat : treats)
  220. {
  221. auto paths = ai->ah->getPathsToTile(treat.hero->visitablePos());
  222. for(AIPath & path : paths)
  223. {
  224. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path)));
  225. }
  226. }*/
  227. }