FuzzyHelper.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "VCAI.h"
  14. FuzzyHelper * fh;
  15. extern boost::thread_specific_ptr<VCAI> ai;
  16. Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
  17. {
  18. if(vec.empty()) //no possibilities found
  19. return sptr(Goals::Invalid());
  20. ai->cachedSectorMaps.clear();
  21. //a trick to switch between heroes less often - calculatePaths is costly
  22. auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  23. {
  24. return lhs->hero.h < rhs->hero.h;
  25. };
  26. boost::sort(vec, sortByHeroes);
  27. for(auto g : vec)
  28. {
  29. setPriority(g);
  30. }
  31. auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  32. {
  33. return lhs->priority < rhs->priority;
  34. };
  35. return *boost::max_element(vec, compareGoals);
  36. }
  37. ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
  38. {
  39. //this one is not fuzzy anymore, just calculate weighted average
  40. auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
  41. CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
  42. ui64 totalStrength = 0;
  43. ui8 totalChance = 0;
  44. for(auto config : bankInfo->getPossibleGuards())
  45. {
  46. totalStrength += config.second.totalStrength * config.first;
  47. totalChance += config.first;
  48. }
  49. return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
  50. }
  51. float FuzzyHelper::evaluate(Goals::VisitTile & g)
  52. {
  53. return visitTileEngine.evaluate(g);
  54. }
  55. float FuzzyHelper::evaluate(Goals::VisitObj & g)
  56. {
  57. return visitObjEngine.evaluate(g);
  58. }
  59. float FuzzyHelper::evaluate(Goals::VisitHero & g)
  60. {
  61. auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
  62. if(!obj)
  63. return -100; //hero died in the meantime
  64. //TODO: consider direct copy (constructor?)
  65. g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
  66. return g.priority;
  67. }
  68. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  69. {
  70. //the more army we need, the more important goal
  71. //the more army we lack, the less important goal
  72. float army = g.hero->getArmyStrength();
  73. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  74. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  75. }
  76. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  77. {
  78. if (!g.hero.h)
  79. return 0; //lowest priority
  80. int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
  81. if(t.valid())
  82. {
  83. if(isSafeToVisit(g.hero, t))
  84. {
  85. g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
  86. }
  87. else
  88. {
  89. g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
  90. sethero(g.hero).setisAbstract(true).accept(this));
  91. }
  92. return g.priority;
  93. }
  94. else
  95. return -1;
  96. }
  97. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  98. {
  99. return g.priority; //TODO
  100. }
  101. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  102. {
  103. return 0;
  104. }
  105. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  106. {
  107. return g.priority; //handled by ResourceManager
  108. }
  109. float FuzzyHelper::evaluate(Goals::Build & g)
  110. {
  111. return 0;
  112. }
  113. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  114. {
  115. return g.priority;
  116. }
  117. float FuzzyHelper::evaluate(Goals::Explore & g)
  118. {
  119. return 1;
  120. }
  121. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  122. {
  123. return 1;
  124. }
  125. float FuzzyHelper::evaluate(Goals::Invalid & g)
  126. {
  127. return -1e10;
  128. }
  129. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  130. {
  131. logAi->warn("Cannot evaluate goal %s", g.name());
  132. return g.priority;
  133. }
  134. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  135. {
  136. g->setpriority(g->accept(this)); //this enforces returned value is set
  137. }