HeroManager.cpp 7.6 KB

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