DefenceBehavior.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/DismissHero.h"
  20. #include "../Goals/ExchangeSwapTownHeroes.h"
  21. #include "lib/mapping/CMap.h" //for victory conditions
  22. #include "lib/CPathfinder.h"
  23. extern boost::thread_specific_ptr<CCallback> cb;
  24. extern boost::thread_specific_ptr<VCAI> ai;
  25. extern FuzzyHelper * fh;
  26. using namespace Goals;
  27. std::string DefenceBehavior::toString() const
  28. {
  29. return "Defend towns";
  30. }
  31. Goals::TGoalVec DefenceBehavior::getTasks()
  32. {
  33. Goals::TGoalVec tasks;
  34. auto heroes = cb->getHeroesInfo();
  35. if(heroes.size())
  36. {
  37. for(auto town : cb->getTownsInfo())
  38. {
  39. evaluateDefence(tasks, town);
  40. }
  41. }
  42. return tasks;
  43. }
  44. uint64_t townArmyIncome(const CGTownInstance * town)
  45. {
  46. uint64_t result = 0;
  47. for(auto creatureInfo : town->creatures)
  48. {
  49. if(creatureInfo.second.empty())
  50. continue;
  51. auto creature = creatureInfo.second.back().toCreature();
  52. result += creature->AIValue * town->getGrowthInfo(creature->level).totalGrowth();
  53. }
  54. return result;
  55. }
  56. void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInstance * town)
  57. {
  58. auto basicPriority = 0.3f + std::sqrt(townArmyIncome(town) / 20000.0f)
  59. + town->dailyIncome()[Res::GOLD] / 10000.0f;
  60. logAi->debug("Evaluating defence for %s, basic priority %f", town->name, basicPriority);
  61. auto treatNode = ai->nullkiller->dangerHitMap->getObjectTreat(town);
  62. auto treats = { treatNode.fastestDanger, treatNode.maximumDanger };
  63. if(!treatNode.fastestDanger.hero)
  64. {
  65. logAi->debug("No treat found for town %s", town->name);
  66. return;
  67. }
  68. int dayOfWeek = cb->getDate(Date::DAY_OF_WEEK);
  69. if(town->garrisonHero)
  70. {
  71. if(!ai->nullkiller->isHeroLocked(town->garrisonHero.get()))
  72. {
  73. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, nullptr).setpriority(5)));
  74. return;
  75. }
  76. logAi->debug(
  77. "Hero %s in garrison of town %s is suposed to defend the town",
  78. town->garrisonHero->name,
  79. town->name);
  80. return;
  81. }
  82. uint64_t reinforcement = ai->ah->howManyReinforcementsCanBuy(town->getUpperArmy(), town);
  83. if(reinforcement)
  84. {
  85. logAi->debug("Town %s can buy defence army %lld", town->name, reinforcement);
  86. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(0.5f)));
  87. }
  88. auto paths = ai->ah->getPathsToTile(town->visitablePos());
  89. for(auto & treat : treats)
  90. {
  91. logAi->debug(
  92. "Town %s has treat %lld in %s turns, hero: %s",
  93. town->name,
  94. treat.danger,
  95. std::to_string(treat.turn),
  96. treat.hero->name);
  97. bool treatIsUnderControl = false;
  98. for(AIPath & path : paths)
  99. {
  100. if(path.getHeroStrength() > treat.danger)
  101. {
  102. if(path.turn() <= treat.turn && dayOfWeek + treat.turn < 6 && isSafeToVisit(path.targetHero, path.heroArmy, treat.danger)
  103. || path.exchangeCount == 1 && path.turn() < treat.turn
  104. || path.turn() < treat.turn - 1)
  105. {
  106. logAi->debug(
  107. "Hero %s can eliminate danger for town %s using path %s.",
  108. path.targetHero->name,
  109. town->name,
  110. path.toString());
  111. treatIsUnderControl = true;
  112. break;
  113. }
  114. }
  115. }
  116. if(treatIsUnderControl)
  117. continue;
  118. if(cb->getResourceAmount(Res::GOLD) > GameConstants::HERO_GOLD_COST)
  119. {
  120. auto heroesInTavern = cb->getAvailableHeroes(town);
  121. for(auto hero : heroesInTavern)
  122. {
  123. if(hero->getTotalStrength() > treat.danger)
  124. {
  125. auto myHeroes = cb->getHeroesInfo();
  126. if(cb->getHeroesInfo().size() < ALLOWED_ROAMING_HEROES)
  127. {
  128. logAi->debug("Hero %s can be recruited to defend %s", hero->name, town->name);
  129. tasks.push_back(Goals::sptr(Goals::RecruitHero().settown(town).setobjid(hero->id.getNum()).setpriority(1)));
  130. continue;
  131. }
  132. else
  133. {
  134. const CGHeroInstance * weakestHero = nullptr;
  135. for(auto existingHero : myHeroes)
  136. {
  137. if(ai->nullkiller->isHeroLocked(existingHero)
  138. || existingHero->getArmyStrength() > hero->getArmyStrength()
  139. || ai->ah->getHeroRole(existingHero) == HeroRole::MAIN
  140. || existingHero->movement
  141. || existingHero->artifactsWorn.size() > (existingHero->hasSpellbook() ? 2 : 1))
  142. continue;
  143. if(!weakestHero || weakestHero->getFightingStrength() > existingHero->getFightingStrength())
  144. {
  145. weakestHero = existingHero;
  146. }
  147. if(weakestHero)
  148. {
  149. tasks.push_back(Goals::sptr(Goals::DismissHero(weakestHero)));
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. if(paths.empty())
  157. {
  158. logAi->debug("No ways to defend town %s", town->name);
  159. continue;
  160. }
  161. for(AIPath & path : paths)
  162. {
  163. #if AI_TRACE_LEVEL >= 1
  164. logAi->trace(
  165. "Hero %s can defend town with force %lld in %s turns, cost: %f, path: %s",
  166. path.targetHero->name,
  167. path.getHeroStrength(),
  168. std::to_string(path.turn()),
  169. path.movementCost(),
  170. path.toString());
  171. #endif
  172. if(path.turn() <= treat.turn - 2)
  173. {
  174. logAi->trace("Deffer defence of %s by %s because he has enough time to rich the town next trun",
  175. town->name,
  176. path.targetHero->name);
  177. continue;
  178. }
  179. float priority = basicPriority
  180. + std::min(SAFE_ATTACK_CONSTANT, (float)path.getHeroStrength() / treat.danger) / (treat.turn + 1);
  181. if(path.targetHero == town->visitingHero && path.exchangeCount == 1)
  182. {
  183. #if AI_TRACE_LEVEL >= 1
  184. logAi->trace("Put %s to garrison of town %s with priority %f",
  185. path.targetHero->name,
  186. town->name,
  187. priority);
  188. #endif
  189. tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, town->visitingHero.get(), HeroLockedReason::DEFENCE).setpriority(priority)));
  190. continue;
  191. }
  192. if(path.turn() <= treat.turn && path.getHeroStrength() * SAFE_ATTACK_CONSTANT >= treat.danger)
  193. {
  194. if(ai->nullkiller->arePathHeroesLocked(path))
  195. {
  196. #if AI_TRACE_LEVEL >= 1
  197. logAi->trace("Can not move %s to defend town %s with priority %f. Path is locked.",
  198. path.targetHero->name,
  199. town->name,
  200. priority);
  201. #endif
  202. continue;
  203. }
  204. #if AI_TRACE_LEVEL >= 1
  205. logAi->trace("Move %s to defend town %s with priority %f",
  206. path.targetHero->name,
  207. town->name,
  208. priority);
  209. #endif
  210. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, town).setpriority(priority)));
  211. }
  212. }
  213. }
  214. logAi->debug("Found %d tasks", tasks.size());
  215. /*for(auto & treat : treats)
  216. {
  217. auto paths = ai->ah->getPathsToTile(treat.hero->visitablePos());
  218. for(AIPath & path : paths)
  219. {
  220. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path)));
  221. }
  222. }*/
  223. }