FuzzyHelper.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * FuzzyHelper.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 "FuzzyHelper.h"
  12. #include "../../lib/mapObjects/CommonConstructors.h"
  13. #include "Goals/Goals.h"
  14. #include "VCAI.h"
  15. FuzzyHelper * fh;
  16. extern boost::thread_specific_ptr<VCAI> ai;
  17. Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
  18. {
  19. if(vec.empty())
  20. {
  21. logAi->debug("FuzzyHelper found no goals. Returning Goals::Invalid.");
  22. //no possibilities found
  23. return sptr(Goals::Invalid());
  24. }
  25. //a trick to switch between heroes less often - calculatePaths is costly
  26. auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  27. {
  28. return lhs->hero.h < rhs->hero.h;
  29. };
  30. boost::sort(vec, sortByHeroes);
  31. for(auto g : vec)
  32. {
  33. setPriority(g);
  34. }
  35. auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  36. {
  37. return lhs->priority < rhs->priority;
  38. };
  39. for(auto goal : vec)
  40. {
  41. logAi->trace("FuzzyHelper evaluated goal %s, priority=%.4f", goal->name(), goal->priority);
  42. }
  43. Goals::TSubgoal result = *boost::max_element(vec, compareGoals);
  44. logAi->debug("FuzzyHelper returned goal %s, priority=%.4f", result->name(), result->priority);
  45. return result;
  46. }
  47. ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
  48. {
  49. //this one is not fuzzy anymore, just calculate weighted average
  50. auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
  51. CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
  52. ui64 totalStrength = 0;
  53. ui8 totalChance = 0;
  54. for(auto config : bankInfo->getPossibleGuards())
  55. {
  56. totalStrength += config.second.totalStrength * config.first;
  57. totalChance += config.first;
  58. }
  59. return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
  60. }
  61. float FuzzyHelper::evaluate(Goals::VisitTile & g)
  62. {
  63. if(g.parent)
  64. {
  65. g.parent->accept(this);
  66. }
  67. return visitTileEngine.evaluate(g);
  68. }
  69. float FuzzyHelper::evaluate(Goals::BuildBoat & g)
  70. {
  71. const float buildBoatPenalty = 0.25;
  72. if(!g.parent)
  73. {
  74. return 0;
  75. }
  76. return g.parent->accept(this) - buildBoatPenalty;
  77. }
  78. float FuzzyHelper::evaluate(Goals::AdventureSpellCast & g)
  79. {
  80. if(!g.parent)
  81. {
  82. return 0;
  83. }
  84. const CSpell * spell = g.getSpell();
  85. const float spellCastPenalty = (float)g.hero->getSpellCost(spell) / g.hero->mana;
  86. return g.parent->accept(this) - spellCastPenalty;
  87. }
  88. float FuzzyHelper::evaluate(Goals::CompleteQuest & g)
  89. {
  90. // TODO: How to evaluate quest complexity?
  91. const float questPenalty = 0.2;
  92. if(!g.parent)
  93. {
  94. return 0;
  95. }
  96. return g.parent->accept(this) * questPenalty;
  97. }
  98. float FuzzyHelper::evaluate(Goals::VisitObj & g)
  99. {
  100. if(g.parent)
  101. {
  102. g.parent->accept(this);
  103. }
  104. return visitObjEngine.evaluate(g);
  105. }
  106. float FuzzyHelper::evaluate(Goals::VisitHero & g)
  107. {
  108. auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
  109. if(!obj)
  110. return -100; //hero died in the meantime
  111. else
  112. {
  113. g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).accept(this));
  114. }
  115. return g.priority;
  116. }
  117. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  118. {
  119. //the more army we need, the more important goal
  120. //the more army we lack, the less important goal
  121. float army = g.hero->getArmyStrength();
  122. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  123. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  124. }
  125. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  126. {
  127. if (!g.hero.h)
  128. return 0; //lowest priority
  129. return g.whatToDoToAchieve()->accept(this);
  130. }
  131. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  132. {
  133. return g.priority; //TODO
  134. }
  135. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  136. {
  137. return 0;
  138. }
  139. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  140. {
  141. return g.priority; //handled by ResourceManager
  142. }
  143. float FuzzyHelper::evaluate(Goals::Build & g)
  144. {
  145. return 0;
  146. }
  147. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  148. {
  149. return g.priority;
  150. }
  151. float FuzzyHelper::evaluate(Goals::Explore & g)
  152. {
  153. return 1;
  154. }
  155. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  156. {
  157. return 1;
  158. }
  159. float FuzzyHelper::evaluate(Goals::Invalid & g)
  160. {
  161. return -1e10;
  162. }
  163. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  164. {
  165. logAi->warn("Cannot evaluate goal %s", g.name());
  166. return g.priority;
  167. }
  168. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  169. {
  170. g->setpriority(g->accept(this)); //this enforces returned value is set
  171. }