DefenceBehavior.cpp 6.8 KB

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