FuzzyHelper.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "Goals/Goals.h"
  13. #include "VCAI.h"
  14. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  15. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  16. #include "../../lib/mapObjectConstructors/CBankInstanceConstructor.h"
  17. #include "../../lib/mapObjects/CGCreature.h"
  18. #include "../../lib/mapObjects/CGDwelling.h"
  19. #include "../../lib/gameState/InfoAboutArmy.h"
  20. FuzzyHelper * fh;
  21. Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
  22. {
  23. if(vec.empty())
  24. {
  25. logAi->debug("FuzzyHelper found no goals. Returning Goals::Invalid.");
  26. //no possibilities found
  27. return sptr(Goals::Invalid());
  28. }
  29. //a trick to switch between heroes less often - calculatePaths is costly
  30. auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  31. {
  32. return lhs->hero.h < rhs->hero.h;
  33. };
  34. std::sort(vec.begin(), vec.end(), sortByHeroes);
  35. for(auto g : vec)
  36. {
  37. setPriority(g);
  38. }
  39. auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  40. {
  41. return lhs->priority < rhs->priority;
  42. };
  43. for(auto goal : vec)
  44. {
  45. logAi->trace("FuzzyHelper evaluated goal %s, priority=%.4f", goal->name(), goal->priority);
  46. }
  47. Goals::TSubgoal result = *std::max_element(vec.begin(), vec.end(), compareGoals);
  48. logAi->debug("FuzzyHelper returned goal %s, priority=%.4f", result->name(), result->priority);
  49. return result;
  50. }
  51. float FuzzyHelper::evaluate(Goals::VisitTile & g)
  52. {
  53. if(g.parent)
  54. {
  55. g.parent->accept(this);
  56. }
  57. return visitTileEngine.evaluate(g);
  58. }
  59. float FuzzyHelper::evaluate(Goals::BuildBoat & g)
  60. {
  61. const float buildBoatPenalty = 0.25;
  62. if(!g.parent)
  63. {
  64. return 0;
  65. }
  66. return g.parent->accept(this) - buildBoatPenalty;
  67. }
  68. float FuzzyHelper::evaluate(Goals::AdventureSpellCast & g)
  69. {
  70. if(!g.parent)
  71. {
  72. return 0;
  73. }
  74. const CSpell * spell = g.getSpell();
  75. const float spellCastPenalty = (float)g.hero->getSpellCost(spell) / g.hero->mana;
  76. return g.parent->accept(this) - spellCastPenalty;
  77. }
  78. float FuzzyHelper::evaluate(Goals::CompleteQuest & g)
  79. {
  80. // TODO: How to evaluate quest complexity?
  81. const float questPenalty = 0.2f;
  82. if(!g.parent)
  83. {
  84. return 0;
  85. }
  86. return g.parent->accept(this) * questPenalty;
  87. }
  88. float FuzzyHelper::evaluate(Goals::VisitObj & g)
  89. {
  90. if(g.parent)
  91. {
  92. g.parent->accept(this);
  93. }
  94. return visitObjEngine.evaluate(g);
  95. }
  96. float FuzzyHelper::evaluate(Goals::VisitHero & g)
  97. {
  98. auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
  99. if(!obj)
  100. {
  101. return -100; //hero died in the meantime
  102. }
  103. else
  104. {
  105. g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).accept(this));
  106. }
  107. return g.priority;
  108. }
  109. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  110. {
  111. //the more army we need, the more important goal
  112. //the more army we lack, the less important goal
  113. float army = static_cast<float>(g.hero->getArmyStrength());
  114. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  115. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  116. }
  117. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  118. {
  119. if (!g.hero.h)
  120. return 0; //lowest priority
  121. return g.whatToDoToAchieve()->accept(this);
  122. }
  123. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  124. {
  125. return g.priority; //TODO
  126. }
  127. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  128. {
  129. return 0;
  130. }
  131. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  132. {
  133. return g.priority; //handled by ResourceManager
  134. }
  135. float FuzzyHelper::evaluate(Goals::Build & g)
  136. {
  137. return 0;
  138. }
  139. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  140. {
  141. return g.priority;
  142. }
  143. float FuzzyHelper::evaluate(Goals::Explore & g)
  144. {
  145. return 1;
  146. }
  147. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  148. {
  149. return 1;
  150. }
  151. float FuzzyHelper::evaluate(Goals::Invalid & g)
  152. {
  153. return -1e10;
  154. }
  155. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  156. {
  157. logAi->warn("Cannot evaluate goal %s", g.name());
  158. return g.priority;
  159. }
  160. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  161. {
  162. g->setpriority(g->accept(this)); //this enforces returned value is set
  163. }
  164. ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor)
  165. {
  166. return evaluateDanger(tile, visitor, ai);
  167. }
  168. ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai)
  169. {
  170. auto cb = ai->myCb;
  171. const TerrainTile * t = cb->getTile(tile, false);
  172. if(!t) //we can know about guard but can't check its tile (the edge of fow)
  173. return 190000000; //MUCH
  174. ui64 objectDanger = 0;
  175. ui64 guardDanger = 0;
  176. auto visitableObjects = cb->getVisitableObjs(tile);
  177. // in some scenarios hero happens to be "under" the object (eg town). Then we consider ONLY the hero.
  178. if(vstd::contains_if(visitableObjects, objWithID<Obj::HERO>))
  179. {
  180. vstd::erase_if(visitableObjects, [](const CGObjectInstance * obj)
  181. {
  182. return !objWithID<Obj::HERO>(obj);
  183. });
  184. }
  185. if(const CGObjectInstance * dangerousObject = vstd::backOrNull(visitableObjects))
  186. {
  187. objectDanger = evaluateDanger(dangerousObject, ai); //unguarded objects can also be dangerous or unhandled
  188. if(objectDanger)
  189. {
  190. //TODO: don't downcast objects AI shouldn't know about!
  191. auto armedObj = dynamic_cast<const CArmedInstance *>(dangerousObject);
  192. if(armedObj)
  193. {
  194. float tacticalAdvantage = tacticalAdvantageEngine.getTacticalAdvantage(visitor, armedObj);
  195. objectDanger = static_cast<ui64>(objectDanger * tacticalAdvantage); //this line tends to go infinite for allied towns (?)
  196. }
  197. }
  198. if(dangerousObject->ID == Obj::SUBTERRANEAN_GATE)
  199. {
  200. //check guard on the other side of the gate
  201. auto it = ai->knownSubterraneanGates.find(dangerousObject);
  202. if(it != ai->knownSubterraneanGates.end())
  203. {
  204. auto guards = cb->getGuardingCreatures(it->second->visitablePos());
  205. for(auto cre : guards)
  206. {
  207. float tacticalAdvantage = tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre));
  208. vstd::amax(guardDanger, evaluateDanger(cre, ai) * tacticalAdvantage);
  209. }
  210. }
  211. }
  212. }
  213. auto guards = cb->getGuardingCreatures(tile);
  214. for(auto cre : guards)
  215. {
  216. float tacticalAdvantage = tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre));
  217. vstd::amax(guardDanger, evaluateDanger(cre, ai) * tacticalAdvantage); //we are interested in strongest monster around
  218. }
  219. //TODO mozna odwiedzic blockvis nie ruszajac straznika
  220. return std::max(objectDanger, guardDanger);
  221. }
  222. ui64 FuzzyHelper::evaluateDanger(const CGObjectInstance * obj, const VCAI * ai)
  223. {
  224. auto cb = ai->myCb;
  225. if(obj->tempOwner.isValidPlayer() && cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES) //owned or allied objects don't pose any threat
  226. return 0;
  227. switch(obj->ID)
  228. {
  229. case Obj::HERO:
  230. {
  231. InfoAboutHero iah;
  232. cb->getHeroInfo(obj, iah);
  233. return iah.army.getStrength();
  234. }
  235. case Obj::TOWN:
  236. case Obj::GARRISON:
  237. case Obj::GARRISON2:
  238. {
  239. InfoAboutTown iat;
  240. cb->getTownInfo(obj, iat);
  241. return iat.army.getStrength();
  242. }
  243. default:
  244. {
  245. const CArmedInstance * a = dynamic_cast<const CArmedInstance *>(obj);
  246. if (a)
  247. return a->getArmyStrength();
  248. else
  249. return 0;
  250. }
  251. }
  252. }