FuzzyHelper.cpp 7.2 KB

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