HeroManager.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * HeroManager.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 "../Engine/Nullkiller.h"
  12. #include "../../../lib/mapObjects/MapObjects.h"
  13. #include "../../../lib/CHeroHandler.h"
  14. namespace NKAI
  15. {
  16. SecondarySkillEvaluator HeroManager::wariorSkillsScores = SecondarySkillEvaluator(
  17. {
  18. std::make_shared<SecondarySkillScoreMap>(
  19. std::map<SecondarySkill, float>
  20. {
  21. {SecondarySkill::DIPLOMACY, 2},
  22. {SecondarySkill::LOGISTICS, 2},
  23. {SecondarySkill::EARTH_MAGIC, 2},
  24. {SecondarySkill::ARMORER, 2},
  25. {SecondarySkill::OFFENCE, 2},
  26. {SecondarySkill::AIR_MAGIC, 1},
  27. {SecondarySkill::WISDOM, 1},
  28. {SecondarySkill::LEADERSHIP, 1},
  29. {SecondarySkill::INTELLIGENCE, 1},
  30. {SecondarySkill::RESISTANCE, 1},
  31. {SecondarySkill::MYSTICISM, -1},
  32. {SecondarySkill::SORCERY, -1},
  33. {SecondarySkill::ESTATES, -1},
  34. {SecondarySkill::FIRST_AID, -1},
  35. {SecondarySkill::LEARNING, -1},
  36. {SecondarySkill::SCHOLAR, -1},
  37. {SecondarySkill::EAGLE_EYE, -1},
  38. {SecondarySkill::NAVIGATION, -1}
  39. }),
  40. std::make_shared<ExistingSkillRule>(),
  41. std::make_shared<WisdomRule>(),
  42. std::make_shared<AtLeastOneMagicRule>()
  43. });
  44. SecondarySkillEvaluator HeroManager::scountSkillsScores = SecondarySkillEvaluator(
  45. {
  46. std::make_shared<SecondarySkillScoreMap>(
  47. std::map<SecondarySkill, float>
  48. {
  49. {SecondarySkill::LOGISTICS, 2},
  50. {SecondarySkill::ESTATES, 2},
  51. {SecondarySkill::PATHFINDING, 1},
  52. {SecondarySkill::SCHOLAR, 1}
  53. }),
  54. std::make_shared<ExistingSkillRule>()
  55. });
  56. float HeroManager::evaluateSecSkill(SecondarySkill skill, const CGHeroInstance * hero) const
  57. {
  58. auto role = getHeroRole(hero);
  59. if(role == HeroRole::MAIN)
  60. return wariorSkillsScores.evaluateSecSkill(hero, skill);
  61. return scountSkillsScores.evaluateSecSkill(hero, skill);
  62. }
  63. float HeroManager::evaluateSpeciality(const CGHeroInstance * hero) const
  64. {
  65. auto heroSpecial = Selector::source(Bonus::HERO_SPECIAL, hero->type->ID.getNum());
  66. auto secondarySkillBonus = Selector::type()(Bonus::SECONDARY_SKILL_PREMY);
  67. auto specialSecondarySkillBonuses = hero->getBonuses(heroSpecial.And(secondarySkillBonus));
  68. float specialityScore = 0.0f;
  69. for(auto bonus : *specialSecondarySkillBonuses)
  70. {
  71. SecondarySkill bonusSkill = SecondarySkill(bonus->subtype);
  72. float bonusScore = wariorSkillsScores.evaluateSecSkill(hero, bonusSkill);
  73. if(bonusScore > 0)
  74. specialityScore += bonusScore * bonusScore * bonusScore;
  75. }
  76. return specialityScore;
  77. }
  78. float HeroManager::evaluateFightingStrength(const CGHeroInstance * hero) const
  79. {
  80. return evaluateSpeciality(hero) + wariorSkillsScores.evaluateSecSkills(hero) + hero->level * 1.5f;
  81. }
  82. std::vector<std::vector<const CGHeroInstance *>> clusterizeHeroes(CCallback * cb, std::vector<const CGHeroInstance *> heroes)
  83. {
  84. std::vector<std::vector<const CGHeroInstance *>> clusters;
  85. for(auto hero : heroes)
  86. {
  87. auto paths = cb->getPathsInfo(hero);
  88. std::vector<const CGHeroInstance *> newCluster = {hero};
  89. for(auto cluster = clusters.begin(); cluster != clusters.end();)
  90. {
  91. auto hero = std::find_if(cluster->begin(), cluster->end(), [&](const CGHeroInstance * h) -> bool
  92. {
  93. return paths->getNode(h->visitablePos())->turns < SCOUT_TURN_DISTANCE_LIMIT;
  94. });
  95. if(hero != cluster->end())
  96. {
  97. vstd::concatenate(newCluster, *cluster);
  98. clusters.erase(cluster);
  99. }
  100. else
  101. cluster++;
  102. }
  103. clusters.push_back(newCluster);
  104. }
  105. return clusters;
  106. }
  107. void HeroManager::update()
  108. {
  109. logAi->trace("Start analysing our heroes");
  110. std::map<const CGHeroInstance *, float> scores;
  111. auto myHeroes = cb->getHeroesInfo();
  112. for(auto & hero : myHeroes)
  113. {
  114. scores[hero] = evaluateFightingStrength(hero);
  115. }
  116. auto scoreSort = [&](const CGHeroInstance * h1, const CGHeroInstance * h2) -> bool
  117. {
  118. return scores.at(h1) > scores.at(h2);
  119. };
  120. int globalMainCount = std::min(((int)myHeroes.size() + 2) / 3, cb->getMapSize().x / 100 + 1);
  121. std::sort(myHeroes.begin(), myHeroes.end(), scoreSort);
  122. for(auto hero : myHeroes)
  123. {
  124. heroRoles[hero] = (globalMainCount--) > 0 ? HeroRole::MAIN : HeroRole::SCOUT;
  125. }
  126. for(auto cluster : clusterizeHeroes(cb, myHeroes))
  127. {
  128. std::sort(cluster.begin(), cluster.end(), scoreSort);
  129. auto localMainCountMax = (cluster.size() + 2) / 3;
  130. for(auto hero : cluster)
  131. {
  132. if(heroRoles[hero] != HeroRole::MAIN)
  133. {
  134. heroRoles[hero] = HeroRole::MAIN;
  135. break;
  136. }
  137. localMainCountMax--;
  138. if(localMainCountMax == 0)
  139. break;
  140. }
  141. }
  142. for(auto hero : myHeroes)
  143. {
  144. logAi->trace("Hero %s has role %s", hero->name, heroRoles[hero] == HeroRole::MAIN ? "main" : "scout");
  145. }
  146. }
  147. HeroRole HeroManager::getHeroRole(const HeroPtr & hero) const
  148. {
  149. return heroRoles.at(hero);
  150. }
  151. const std::map<HeroPtr, HeroRole> & HeroManager::getHeroRoles() const
  152. {
  153. return heroRoles;
  154. }
  155. int HeroManager::selectBestSkill(const HeroPtr & hero, const std::vector<SecondarySkill> & skills) const
  156. {
  157. auto role = getHeroRole(hero);
  158. auto & evaluator = role == HeroRole::MAIN ? wariorSkillsScores : scountSkillsScores;
  159. int result = 0;
  160. float resultScore = -100;
  161. for(int i = 0; i < skills.size(); i++)
  162. {
  163. auto score = evaluator.evaluateSecSkill(hero.get(), skills[i]);
  164. if(score > resultScore)
  165. {
  166. resultScore = score;
  167. result = i;
  168. }
  169. logAi->trace(
  170. "Hero %s is proposed to learn %d with score %f",
  171. hero.name,
  172. skills[i].toEnum(),
  173. score);
  174. }
  175. return result;
  176. }
  177. float HeroManager::evaluateHero(const CGHeroInstance * hero) const
  178. {
  179. return evaluateFightingStrength(hero);
  180. }
  181. SecondarySkillScoreMap::SecondarySkillScoreMap(std::map<SecondarySkill, float> scoreMap)
  182. :scoreMap(scoreMap)
  183. {
  184. }
  185. void SecondarySkillScoreMap::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  186. {
  187. auto it = scoreMap.find(skill);
  188. if(it != scoreMap.end())
  189. {
  190. score = it->second;
  191. }
  192. }
  193. void ExistingSkillRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  194. {
  195. int upgradesLeft = 0;
  196. for(auto & heroSkill : hero->secSkills)
  197. {
  198. if(heroSkill.first == skill)
  199. return;
  200. upgradesLeft += SecSkillLevel::EXPERT - heroSkill.second;
  201. }
  202. if(score >= 2 || (score >= 1 && upgradesLeft <= 1))
  203. score += 1.5;
  204. }
  205. void WisdomRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  206. {
  207. if(skill != SecondarySkill::WISDOM)
  208. return;
  209. auto wisdomLevel = hero->getSecSkillLevel(SecondarySkill::WISDOM);
  210. if(hero->level > 10 && wisdomLevel == SecSkillLevel::NONE)
  211. score += 1.5;
  212. }
  213. std::vector<SecondarySkill> AtLeastOneMagicRule::magicSchools = {
  214. SecondarySkill::AIR_MAGIC,
  215. SecondarySkill::EARTH_MAGIC,
  216. SecondarySkill::FIRE_MAGIC,
  217. SecondarySkill::WATER_MAGIC
  218. };
  219. void AtLeastOneMagicRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  220. {
  221. if(!vstd::contains(magicSchools, skill))
  222. return;
  223. bool heroHasAnyMagic = vstd::contains_if(magicSchools, [&](SecondarySkill skill) -> bool
  224. {
  225. return hero->getSecSkillLevel(skill) > SecSkillLevel::NONE;
  226. });
  227. if(!heroHasAnyMagic)
  228. score += 1;
  229. }
  230. SecondarySkillEvaluator::SecondarySkillEvaluator(std::vector<std::shared_ptr<ISecondarySkillRule>> evaluationRules)
  231. : evaluationRules(evaluationRules)
  232. {
  233. }
  234. float SecondarySkillEvaluator::evaluateSecSkills(const CGHeroInstance * hero) const
  235. {
  236. float totalScore = 0;
  237. for(auto skill : hero->secSkills)
  238. {
  239. totalScore += skill.second * evaluateSecSkill(hero, skill.first);
  240. }
  241. return totalScore;
  242. }
  243. float SecondarySkillEvaluator::evaluateSecSkill(const CGHeroInstance * hero, SecondarySkill skill) const
  244. {
  245. float score = 0;
  246. for(auto rule : evaluationRules)
  247. rule->evaluateScore(hero, skill, score);
  248. return score;
  249. }
  250. }