FuzzyHelper.cpp 8.4 KB

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