FuzzyHelper.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. else
  65. {
  66. auto dummyGoal = Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract);
  67. g.setpriority(dummyGoal.accept(this));
  68. }
  69. return g.priority;
  70. }
  71. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  72. {
  73. //the more army we need, the more important goal
  74. //the more army we lack, the less important goal
  75. float army = g.hero->getArmyStrength();
  76. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  77. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  78. }
  79. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  80. {
  81. if (!g.hero.h)
  82. return 0; //lowest priority
  83. int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
  84. if(t.valid())
  85. {
  86. if(isSafeToVisit(g.hero, t))
  87. {
  88. g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
  89. }
  90. else
  91. {
  92. g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
  93. sethero(g.hero).setisAbstract(true).accept(this));
  94. }
  95. return g.priority;
  96. }
  97. else
  98. return -1;
  99. }
  100. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  101. {
  102. return g.priority; //TODO
  103. }
  104. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  105. {
  106. return 0;
  107. }
  108. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  109. {
  110. return g.priority; //handled by ResourceManager
  111. }
  112. float FuzzyHelper::evaluate(Goals::Build & g)
  113. {
  114. return 0;
  115. }
  116. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  117. {
  118. return g.priority;
  119. }
  120. float FuzzyHelper::evaluate(Goals::Explore & g)
  121. {
  122. return 1;
  123. }
  124. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  125. {
  126. return 1;
  127. }
  128. float FuzzyHelper::evaluate(Goals::Invalid & g)
  129. {
  130. return -1e10;
  131. }
  132. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  133. {
  134. logAi->warn("Cannot evaluate goal %s", g.name());
  135. return g.priority;
  136. }
  137. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  138. {
  139. g->setpriority(g->accept(this)); //this enforces returned value is set
  140. }