FuzzyHelper.cpp 8.3 KB

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