PriorityEvaluator.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * PriorityEvaluator.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 "PriorityEvaluator.h"
  12. #include <limits>
  13. #include "../../../lib/mapObjects/MapObjects.h"
  14. #include "../../../lib/mapObjects/CommonConstructors.h"
  15. #include "../../../lib/CCreatureHandler.h"
  16. #include "../../../lib/CPathfinder.h"
  17. #include "../../../lib/CGameStateFwd.h"
  18. #include "../../../lib/VCMI_Lib.h"
  19. #include "../../../CCallback.h"
  20. #include "../../../lib/filesystem/Filesystem.h"
  21. #include "../VCAI.h"
  22. #include "../AIhelper.h"
  23. #include "../Engine/Nullkiller.h"
  24. #include "../Goals/ExecuteHeroChain.h"
  25. #define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
  26. #define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
  27. struct BankConfig;
  28. class CBankInfo;
  29. class Engine;
  30. class InputVariable;
  31. class CGTownInstance;
  32. extern boost::thread_specific_ptr<CCallback> cb;
  33. extern boost::thread_specific_ptr<VCAI> ai;
  34. EvaluationContext::EvaluationContext()
  35. : movementCost(0.0),
  36. manaCost(0),
  37. danger(0),
  38. closestWayRatio(1),
  39. movementCostByRole(),
  40. skillReward(0),
  41. goldReward(0),
  42. goldCost(0),
  43. armyReward(0),
  44. armyLossPersentage(0),
  45. heroRole(HeroRole::SCOUT),
  46. turn(0),
  47. strategicalValue(0)
  48. {
  49. }
  50. PriorityEvaluator::~PriorityEvaluator()
  51. {
  52. delete engine;
  53. }
  54. void PriorityEvaluator::initVisitTile()
  55. {
  56. auto file = CResourceHandler::get()->load(ResourceID("config/ai/object-priorities.txt"))->readAll();
  57. std::string str = std::string((char *)file.first.get(), file.second);
  58. engine = fl::FllImporter().fromString(str);
  59. armyLossPersentageVariable = engine->getInputVariable("armyLoss");
  60. heroRoleVariable = engine->getInputVariable("heroRole");
  61. dangerVariable = engine->getInputVariable("danger");
  62. turnVariable = engine->getInputVariable("turn");
  63. mainTurnDistanceVariable = engine->getInputVariable("mainTurnDistance");
  64. scoutTurnDistanceVariable = engine->getInputVariable("scoutTurnDistance");
  65. goldRewardVariable = engine->getInputVariable("goldReward");
  66. armyRewardVariable = engine->getInputVariable("armyReward");
  67. skillRewardVariable = engine->getInputVariable("skillReward");
  68. rewardTypeVariable = engine->getInputVariable("rewardType");
  69. closestHeroRatioVariable = engine->getInputVariable("closestHeroRatio");
  70. strategicalValueVariable = engine->getInputVariable("strategicalValue");
  71. goldPreasureVariable = engine->getInputVariable("goldPreasure");
  72. goldCostVariable = engine->getInputVariable("goldCost");
  73. value = engine->getOutputVariable("Value");
  74. }
  75. int32_t estimateTownIncome(const CGObjectInstance * target, const CGHeroInstance * hero)
  76. {
  77. auto relations = cb->getPlayerRelations(hero->tempOwner, target->tempOwner);
  78. if(relations != PlayerRelations::ENEMIES)
  79. return 0; // if we already own it, no additional reward will be received by just visiting it
  80. auto town = cb->getTown(target->id);
  81. auto isNeutral = target->tempOwner == PlayerColor::NEUTRAL;
  82. auto isProbablyDeveloped = !isNeutral && town->hasFort();
  83. return isProbablyDeveloped ? 1500 : 500;
  84. }
  85. TResources getCreatureBankResources(const CGObjectInstance * target, const CGHeroInstance * hero)
  86. {
  87. auto objectInfo = VLC->objtypeh->getHandlerFor(target->ID, target->subID)->getObjectInfo(target->appearance);
  88. CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
  89. auto resources = bankInfo->getPossibleResourcesReward();
  90. return resources;
  91. }
  92. uint64_t getCreatureBankArmyReward(const CGObjectInstance * target, const CGHeroInstance * hero)
  93. {
  94. auto objectInfo = VLC->objtypeh->getHandlerFor(target->ID, target->subID)->getObjectInfo(target->appearance);
  95. CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
  96. auto creatures = bankInfo->getPossibleCreaturesReward();
  97. uint64_t result = 0;
  98. for(auto c : creatures)
  99. {
  100. result += c.type->AIValue * c.count;
  101. }
  102. return result;
  103. }
  104. uint64_t getDwellingScore(const CGObjectInstance * target, bool checkGold)
  105. {
  106. auto dwelling = dynamic_cast<const CGDwelling *>(target);
  107. uint64_t score = 0;
  108. for(auto & creLevel : dwelling->creatures)
  109. {
  110. if(creLevel.first && creLevel.second.size())
  111. {
  112. auto creature = creLevel.second.back().toCreature();
  113. auto creaturesAreFree = creature->level == 1;
  114. if(!creaturesAreFree && checkGold && !cb->getResourceAmount().canAfford(creature->cost * creLevel.first))
  115. continue;
  116. score += creature->AIValue * creLevel.first;
  117. }
  118. }
  119. return score;
  120. }
  121. int getDwellingArmyCost(const CGObjectInstance * target)
  122. {
  123. auto dwelling = dynamic_cast<const CGDwelling *>(target);
  124. int cost = 0;
  125. for(auto & creLevel : dwelling->creatures)
  126. {
  127. if(creLevel.first && creLevel.second.size())
  128. {
  129. auto creature = creLevel.second.back().toCreature();
  130. auto creaturesAreFree = creature->level == 1;
  131. if(!creaturesAreFree)
  132. cost += creature->cost[Res::GOLD] * creLevel.first;
  133. }
  134. }
  135. return cost;
  136. }
  137. uint64_t evaluateArtifactArmyValue(CArtifactInstance * art)
  138. {
  139. if(art->artType->id == ArtifactID::SPELL_SCROLL)
  140. return 1500;
  141. auto statsValue =
  142. 4 * art->valOfBonuses(Bonus::LAND_MOVEMENT)
  143. + 700 * art->valOfBonuses(Bonus::MORALE)
  144. + 700 * art->getAttack(false)
  145. + 700 * art->getDefence(false)
  146. + 700 * art->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::KNOWLEDGE)
  147. + 700 * art->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::SPELL_POWER)
  148. + 700 * art->getDefence(false)
  149. + 500 * art->valOfBonuses(Bonus::LUCK);
  150. auto classValue = 0;
  151. switch(art->artType->aClass)
  152. {
  153. case CArtifact::EartClass::ART_MINOR:
  154. classValue = 1000;
  155. break;
  156. case CArtifact::EartClass::ART_MAJOR:
  157. classValue = 3000;
  158. break;
  159. case CArtifact::EartClass::ART_RELIC:
  160. case CArtifact::EartClass::ART_SPECIAL:
  161. classValue = 8000;
  162. break;
  163. }
  164. return statsValue > classValue ? statsValue : classValue;
  165. }
  166. uint64_t getArmyReward(const CGObjectInstance * target, const CGHeroInstance * hero, const CCreatureSet * army, bool checkGold)
  167. {
  168. const float enemyArmyEliminationRewardRatio = 0.5f;
  169. if(!target)
  170. return 0;
  171. switch(target->ID)
  172. {
  173. case Obj::TOWN:
  174. return target->tempOwner == PlayerColor::NEUTRAL ? 1000 : 10000;
  175. case Obj::HILL_FORT:
  176. return ai->ah->calculateCreateresUpgrade(army, target, cb->getResourceAmount()).upgradeValue;
  177. case Obj::CREATURE_BANK:
  178. return getCreatureBankArmyReward(target, hero);
  179. case Obj::CREATURE_GENERATOR1:
  180. case Obj::CREATURE_GENERATOR2:
  181. case Obj::CREATURE_GENERATOR3:
  182. case Obj::CREATURE_GENERATOR4:
  183. return getDwellingScore(target, checkGold);
  184. case Obj::CRYPT:
  185. case Obj::SHIPWRECK:
  186. case Obj::SHIPWRECK_SURVIVOR:
  187. case Obj::WARRIORS_TOMB:
  188. return 1000;
  189. case Obj::ARTIFACT:
  190. return evaluateArtifactArmyValue(dynamic_cast<const CGArtifact *>(target)->storedArtifact);
  191. case Obj::DRAGON_UTOPIA:
  192. return 10000;
  193. case Obj::HERO:
  194. return cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
  195. ? enemyArmyEliminationRewardRatio * dynamic_cast<const CGHeroInstance *>(target)->getArmyStrength()
  196. : 0;
  197. default:
  198. return 0;
  199. }
  200. }
  201. int getGoldCost(const CGObjectInstance * target, const CGHeroInstance * hero, const CCreatureSet * army)
  202. {
  203. if(!target)
  204. return 0;
  205. switch(target->ID)
  206. {
  207. case Obj::HILL_FORT:
  208. return ai->ah->calculateCreateresUpgrade(army, target, cb->getResourceAmount()).upgradeCost[Res::GOLD];
  209. case Obj::SCHOOL_OF_MAGIC:
  210. case Obj::SCHOOL_OF_WAR:
  211. return 1000;
  212. case Obj::UNIVERSITY:
  213. return 2000;
  214. case Obj::CREATURE_GENERATOR1:
  215. case Obj::CREATURE_GENERATOR2:
  216. case Obj::CREATURE_GENERATOR3:
  217. case Obj::CREATURE_GENERATOR4:
  218. return getDwellingArmyCost(target);
  219. default:
  220. return 0;
  221. }
  222. }
  223. float getStrategicalValue(const CGObjectInstance * target);
  224. float getEnemyHeroStrategicalValue(const CGHeroInstance * enemy)
  225. {
  226. auto objectsUnderTreat = ai->nullkiller->dangerHitMap->getOneTurnAccessibleObjects(enemy);
  227. float objectValue = 0;
  228. for(auto obj : objectsUnderTreat)
  229. {
  230. vstd::amax(objectValue, getStrategicalValue(obj));
  231. }
  232. return objectValue / 2.0f + enemy->level / 15.0f;
  233. }
  234. float getResourceRequirementStrength(int resType)
  235. {
  236. TResources requiredResources = ai->nullkiller->buildAnalyzer->getResourcesRequiredNow();
  237. TResources dailyIncome = ai->nullkiller->buildAnalyzer->getDailyIncome();
  238. if(requiredResources[resType] == 0)
  239. return 0;
  240. if(dailyIncome[resType] == 0)
  241. return 1.0f;
  242. float ratio = (float)requiredResources[resType] / dailyIncome[resType] / 2;
  243. return std::min(ratio, 1.0f);
  244. }
  245. float getTotalResourceRequirementStrength(int resType)
  246. {
  247. TResources requiredResources = ai->nullkiller->buildAnalyzer->getTotalResourcesRequired();
  248. TResources dailyIncome = ai->nullkiller->buildAnalyzer->getDailyIncome();
  249. if(requiredResources[resType] == 0)
  250. return 0;
  251. float ratio = dailyIncome[resType] == 0
  252. ? requiredResources[resType] / 50
  253. : (float)requiredResources[resType] / dailyIncome[resType] / 50;
  254. return std::min(ratio, 1.0f);
  255. }
  256. float getStrategicalValue(const CGObjectInstance * target)
  257. {
  258. if(!target)
  259. return 0;
  260. switch(target->ID)
  261. {
  262. case Obj::MINE:
  263. return target->subID == Res::GOLD ? 0.5f : 0.05f * getTotalResourceRequirementStrength(target->subID) + 0.05f * getResourceRequirementStrength(target->subID);
  264. case Obj::RESOURCE:
  265. return target->subID == Res::GOLD ? 0 : 0.3f * getResourceRequirementStrength(target->subID);
  266. case Obj::TOWN:
  267. return dynamic_cast<const CGTownInstance *>(target)->hasFort()
  268. ? (target->tempOwner == PlayerColor::NEUTRAL ? 0.8f : 1.0f)
  269. : 0.5f;
  270. case Obj::HERO:
  271. return cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
  272. ? getEnemyHeroStrategicalValue(dynamic_cast<const CGHeroInstance *>(target))
  273. : 0;
  274. default:
  275. return 0;
  276. }
  277. }
  278. float evaluateWitchHutSkillScore(const CGWitchHut * hut, const CGHeroInstance * hero, HeroRole role)
  279. {
  280. if(!hut->wasVisited(hero->tempOwner))
  281. return role == HeroRole::SCOUT ? 2 : 0;
  282. auto skill = SecondarySkill(hut->ability);
  283. if(hero->getSecSkillLevel(skill) != SecSkillLevel::NONE
  284. || hero->secSkills.size() >= GameConstants::SKILL_PER_HERO)
  285. return 0;
  286. auto score = ai->ah->evaluateSecSkill(skill, hero);
  287. return score >= 2 ? (role == HeroRole::MAIN ? 10 : 4) : score;
  288. }
  289. float getSkillReward(const CGObjectInstance * target, const CGHeroInstance * hero, HeroRole role)
  290. {
  291. const float enemyHeroEliminationSkillRewardRatio = 0.5f;
  292. if(!target)
  293. return 0;
  294. switch(target->ID)
  295. {
  296. case Obj::STAR_AXIS:
  297. case Obj::SCHOLAR:
  298. case Obj::SCHOOL_OF_MAGIC:
  299. case Obj::SCHOOL_OF_WAR:
  300. case Obj::GARDEN_OF_REVELATION:
  301. case Obj::MARLETTO_TOWER:
  302. case Obj::MERCENARY_CAMP:
  303. case Obj::SHRINE_OF_MAGIC_GESTURE:
  304. case Obj::SHRINE_OF_MAGIC_INCANTATION:
  305. case Obj::TREE_OF_KNOWLEDGE:
  306. return 1;
  307. case Obj::LEARNING_STONE:
  308. return 1.0f / std::sqrt(hero->level);
  309. case Obj::ARENA:
  310. case Obj::SHRINE_OF_MAGIC_THOUGHT:
  311. return 2;
  312. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  313. return 8;
  314. case Obj::WITCH_HUT:
  315. return evaluateWitchHutSkillScore(dynamic_cast<const CGWitchHut *>(target), hero, role);
  316. case Obj::HERO:
  317. return cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
  318. ? enemyHeroEliminationSkillRewardRatio * dynamic_cast<const CGHeroInstance *>(target)->level
  319. : 0;
  320. default:
  321. return 0;
  322. }
  323. }
  324. int32_t getArmyCost(const CArmedInstance * army)
  325. {
  326. int32_t value = 0;
  327. for(auto stack : army->Slots())
  328. {
  329. value += stack.second->getCreatureID().toCreature()->cost[Res::GOLD] * stack.second->count;
  330. }
  331. return value;
  332. }
  333. /// Gets aproximated reward in gold. Daily income is multiplied by 5
  334. int32_t getGoldReward(const CGObjectInstance * target, const CGHeroInstance * hero)
  335. {
  336. if(!target)
  337. return 0;
  338. const int dailyIncomeMultiplier = 5;
  339. const float enemyArmyEliminationGoldRewardRatio = 0.2f;
  340. const int32_t heroEliminationBonus = GameConstants::HERO_GOLD_COST / 2;
  341. auto isGold = target->subID == Res::GOLD; // TODO: other resorces could be sold but need to evaluate market power
  342. switch(target->ID)
  343. {
  344. case Obj::RESOURCE:
  345. return isGold ? 600 : 100;
  346. case Obj::TREASURE_CHEST:
  347. return 1500;
  348. case Obj::WATER_WHEEL:
  349. return 1000;
  350. case Obj::TOWN:
  351. return dailyIncomeMultiplier * estimateTownIncome(target, hero);
  352. case Obj::MINE:
  353. case Obj::ABANDONED_MINE:
  354. return dailyIncomeMultiplier * (isGold ? 1000 : 75);
  355. case Obj::MYSTICAL_GARDEN:
  356. case Obj::WINDMILL:
  357. return 100;
  358. case Obj::CAMPFIRE:
  359. return 800;
  360. case Obj::WAGON:
  361. return 100;
  362. case Obj::CREATURE_BANK:
  363. return getCreatureBankResources(target, hero)[Res::GOLD];
  364. case Obj::CRYPT:
  365. case Obj::DERELICT_SHIP:
  366. return 3000;
  367. case Obj::DRAGON_UTOPIA:
  368. return 10000;
  369. case Obj::SEA_CHEST:
  370. return 1500;
  371. case Obj::HERO:
  372. return cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
  373. ? heroEliminationBonus + enemyArmyEliminationGoldRewardRatio * getArmyCost(dynamic_cast<const CGHeroInstance *>(target))
  374. : 0;
  375. default:
  376. return 0;
  377. }
  378. }
  379. class ExecuteHeroChainEvaluationContextBuilder : public IEvaluationContextBuilder
  380. {
  381. public:
  382. virtual void buildEvaluationContext(EvaluationContext & evaluationContext, Goals::TSubgoal task) const override
  383. {
  384. if(task->goalType != Goals::EXECUTE_HERO_CHAIN)
  385. return;
  386. Goals::ExecuteHeroChain & chain = dynamic_cast<Goals::ExecuteHeroChain &>(*task);
  387. const AIPath & path = chain.getPath();
  388. vstd::amax(evaluationContext.danger, path.getTotalDanger());
  389. evaluationContext.movementCost += path.movementCost();
  390. evaluationContext.closestWayRatio = chain.closestWayRatio;
  391. for(auto & node : path.nodes)
  392. {
  393. auto role = ai->ah->getHeroRole(node.targetHero);
  394. evaluationContext.movementCostByRole[role] += node.cost;
  395. }
  396. auto heroPtr = task->hero;
  397. const CGObjectInstance * target = cb->getObj((ObjectInstanceID)task->objid, false);
  398. auto day = cb->getDate(Date::DAY);
  399. auto hero = heroPtr.get();
  400. bool checkGold = evaluationContext.danger == 0;
  401. auto army = path.heroArmy;
  402. vstd::amax(evaluationContext.armyLossPersentage, path.getTotalArmyLoss() / (double)path.getHeroStrength());
  403. vstd::amax(evaluationContext.heroRole, ai->ah->getHeroRole(heroPtr));
  404. evaluationContext.goldReward += getGoldReward(target, hero);
  405. evaluationContext.armyReward += getArmyReward(target, hero, army, checkGold);
  406. evaluationContext.skillReward += getSkillReward(target, hero, evaluationContext.heroRole);
  407. evaluationContext.strategicalValue += getStrategicalValue(target);
  408. evaluationContext.goldCost += getGoldCost(target, hero, army);
  409. vstd::amax(evaluationContext.turn, path.turn());
  410. }
  411. };
  412. class BuildThisEvaluationContextBuilder : public IEvaluationContextBuilder
  413. {
  414. public:
  415. virtual void buildEvaluationContext(EvaluationContext & evaluationContext, Goals::TSubgoal task) const override
  416. {
  417. if(task->goalType != Goals::BUILD_STRUCTURE)
  418. return;
  419. Goals::BuildThis & buildThis = dynamic_cast<Goals::BuildThis &>(*task);
  420. auto & bi = buildThis.buildingInfo;
  421. evaluationContext.goldReward += 7 * bi.dailyIncome[Res::GOLD] / 2; // 7 day income but half we already have
  422. evaluationContext.heroRole = HeroRole::MAIN;
  423. evaluationContext.movementCostByRole[evaluationContext.heroRole] += bi.prerequisitesCount;
  424. evaluationContext.strategicalValue += buildThis.townInfo.armyScore / 50000.0;
  425. evaluationContext.goldCost += bi.buildCostWithPrerequisits[Res::GOLD];
  426. if(bi.creatureID != CreatureID::NONE)
  427. {
  428. evaluationContext.strategicalValue += (0.5f + 0.1f * bi.creatureLevel) / (float)bi.prerequisitesCount;
  429. if(bi.baseCreatureID == bi.creatureID)
  430. {
  431. evaluationContext.armyReward += ai->ah->evaluateStackPower(bi.creatureID.toCreature(), bi.creatureGrows);
  432. }
  433. else
  434. {
  435. auto creaturesToUpgrade = ai->ah->getTotalCreaturesAvailable(bi.baseCreatureID);
  436. auto upgradedPower = ai->ah->evaluateStackPower(bi.creatureID.toCreature(), creaturesToUpgrade.count);
  437. evaluationContext.armyReward += upgradedPower - creaturesToUpgrade.power;
  438. }
  439. }
  440. else
  441. {
  442. evaluationContext.strategicalValue += ai->nullkiller->buildAnalyzer->getGoldPreasure() * evaluationContext.goldReward / 2200.0f;
  443. }
  444. }
  445. };
  446. PriorityEvaluator::PriorityEvaluator()
  447. {
  448. initVisitTile();
  449. evaluationContextBuilders.push_back(std::make_shared<ExecuteHeroChainEvaluationContextBuilder>());
  450. evaluationContextBuilders.push_back(std::make_shared<BuildThisEvaluationContextBuilder>());
  451. }
  452. EvaluationContext PriorityEvaluator::buildEvaluationContext(Goals::TSubgoal goal) const
  453. {
  454. Goals::TGoalVec parts;
  455. EvaluationContext context;
  456. if(goal->goalType == Goals::COMPOSITION)
  457. {
  458. parts = goal->decompose();
  459. }
  460. else
  461. {
  462. parts.push_back(goal);
  463. }
  464. for(auto goal : parts)
  465. {
  466. context.strategicalValue += goal->strategicalValue;
  467. context.goldCost += goal->goldCost;
  468. for(auto builder : evaluationContextBuilders)
  469. {
  470. builder->buildEvaluationContext(context, goal);
  471. }
  472. }
  473. return context;
  474. }
  475. /// distance
  476. /// nearest hero?
  477. /// gold income
  478. /// army income
  479. /// hero strength - hero skills
  480. /// danger
  481. /// importance
  482. float PriorityEvaluator::evaluate(Goals::TSubgoal task)
  483. {
  484. auto evaluationContext = buildEvaluationContext(task);
  485. int rewardType = (evaluationContext.goldReward > 0 ? 1 : 0)
  486. + (evaluationContext.armyReward > 0 ? 1 : 0)
  487. + (evaluationContext.skillReward > 0 ? 1 : 0)
  488. + (evaluationContext.strategicalValue > 0 ? 1 : 0);
  489. double result = 0;
  490. try
  491. {
  492. armyLossPersentageVariable->setValue(evaluationContext.armyLossPersentage);
  493. heroRoleVariable->setValue(evaluationContext.heroRole);
  494. mainTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::MAIN]);
  495. scoutTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::SCOUT]);
  496. goldRewardVariable->setValue(evaluationContext.goldReward);
  497. armyRewardVariable->setValue(evaluationContext.armyReward);
  498. skillRewardVariable->setValue(evaluationContext.skillReward);
  499. dangerVariable->setValue(evaluationContext.danger);
  500. rewardTypeVariable->setValue(rewardType);
  501. closestHeroRatioVariable->setValue(evaluationContext.closestWayRatio);
  502. strategicalValueVariable->setValue(evaluationContext.strategicalValue);
  503. goldPreasureVariable->setValue(ai->nullkiller->buildAnalyzer->getGoldPreasure());
  504. goldCostVariable->setValue(evaluationContext.goldCost / ((float)cb->getResourceAmount(Res::GOLD) + (float)ai->nullkiller->buildAnalyzer->getDailyIncome()[Res::GOLD] + 1.0f));
  505. turnVariable->setValue(evaluationContext.turn);
  506. engine->process();
  507. //engine.process(VISIT_TILE); //TODO: Process only Visit_Tile
  508. result = value->getValue();
  509. }
  510. catch(fl::Exception & fe)
  511. {
  512. logAi->error("evaluate VisitTile: %s", fe.getWhat());
  513. }
  514. assert(result >= 0);
  515. #ifdef VCMI_TRACE_PATHFINDER
  516. logAi->trace("Evaluated %s, loss: %f, turns main: %f, scout: %f, gold: %d, cost: %d, army gain: %d, danger: %d, role: %s, strategical value: %f, cwr: %f, result %f",
  517. task->toString(),
  518. evaluationContext.armyLossPersentage,
  519. evaluationContext.movementCostByRole[HeroRole::MAIN],
  520. evaluationContext.movementCostByRole[HeroRole::SCOUT],
  521. evaluationContext.goldReward,
  522. evaluationContext.goldCost,
  523. evaluationContext.armyReward,
  524. evaluationContext.danger,
  525. evaluationContext.heroRole ? "scout" : "main",
  526. evaluationContext.strategicalValue,
  527. evaluationContext.closestWayRatio,
  528. result);
  529. #endif
  530. return result;
  531. }