2
0

FuzzyHelper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.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->debug("FuzzyHelper evaluated goal %s, priority=%i", goal->name(), goal->priority);
  42. }
  43. Goals::TSubgoal result = *boost::max_element(vec, compareGoals);
  44. logAi->debug("FuzzyHelper returned goal %s, priority=%i", 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. return visitTileEngine.evaluate(g);
  64. }
  65. float FuzzyHelper::evaluate(Goals::BuildBoat & g)
  66. {
  67. const float buildBoatPenalty = 0.25;
  68. if(!g.parent)
  69. {
  70. return 0;
  71. }
  72. return g.parent->accept(this) - buildBoatPenalty;
  73. }
  74. float FuzzyHelper::evaluate(Goals::VisitObj & g)
  75. {
  76. return visitObjEngine.evaluate(g);
  77. }
  78. float FuzzyHelper::evaluate(Goals::VisitHero & g)
  79. {
  80. auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
  81. if(!obj)
  82. return -100; //hero died in the meantime
  83. else
  84. {
  85. g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).accept(this));
  86. }
  87. return g.priority;
  88. }
  89. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  90. {
  91. //the more army we need, the more important goal
  92. //the more army we lack, the less important goal
  93. float army = g.hero->getArmyStrength();
  94. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  95. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  96. }
  97. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  98. {
  99. if (!g.hero.h)
  100. return 0; //lowest priority
  101. return g.whatToDoToAchieve()->accept(this);
  102. }
  103. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  104. {
  105. return g.priority; //TODO
  106. }
  107. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  108. {
  109. return 0;
  110. }
  111. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  112. {
  113. return g.priority; //handled by ResourceManager
  114. }
  115. float FuzzyHelper::evaluate(Goals::Build & g)
  116. {
  117. return 0;
  118. }
  119. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  120. {
  121. return g.priority;
  122. }
  123. float FuzzyHelper::evaluate(Goals::Explore & g)
  124. {
  125. return 1;
  126. }
  127. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  128. {
  129. return 1;
  130. }
  131. float FuzzyHelper::evaluate(Goals::Invalid & g)
  132. {
  133. return -1e10;
  134. }
  135. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  136. {
  137. logAi->warn("Cannot evaluate goal %s", g.name());
  138. return g.priority;
  139. }
  140. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  141. {
  142. g->setpriority(g->accept(this)); //this enforces returned value is set
  143. }