|
|
@@ -15,6 +15,8 @@
|
|
|
#include "../../../lib/mapObjectConstructors/CObjectClassesHandler.h"
|
|
|
#include "../../../lib/mapObjectConstructors/CBankInstanceConstructor.h"
|
|
|
#include "../../../lib/mapObjects/MapObjects.h"
|
|
|
+#include "../../../lib/mapping/CMapDefines.h"
|
|
|
+#include "../../../lib/RoadHandler.h"
|
|
|
#include "../../../lib/CCreatureHandler.h"
|
|
|
#include "../../../lib/VCMI_Lib.h"
|
|
|
#include "../../../lib/StartInfo.h"
|
|
|
@@ -33,11 +35,9 @@
|
|
|
namespace NKAI
|
|
|
{
|
|
|
|
|
|
-#define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
|
|
|
-#define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
|
|
|
-const float MIN_CRITICAL_VALUE = 2.0f;
|
|
|
+constexpr float MIN_CRITICAL_VALUE = 2.0f;
|
|
|
|
|
|
-EvaluationContext::EvaluationContext(const Nullkiller * ai)
|
|
|
+EvaluationContext::EvaluationContext(const Nullkiller* ai)
|
|
|
: movementCost(0.0),
|
|
|
manaCost(0),
|
|
|
danger(0),
|
|
|
@@ -51,9 +51,22 @@ EvaluationContext::EvaluationContext(const Nullkiller * ai)
|
|
|
heroRole(HeroRole::SCOUT),
|
|
|
turn(0),
|
|
|
strategicalValue(0),
|
|
|
+ conquestValue(0),
|
|
|
evaluator(ai),
|
|
|
enemyHeroDangerRatio(0),
|
|
|
- armyGrowth(0)
|
|
|
+ threat(0),
|
|
|
+ armyGrowth(0),
|
|
|
+ armyInvolvement(0),
|
|
|
+ defenseValue(0),
|
|
|
+ isDefend(false),
|
|
|
+ threatTurns(INT_MAX),
|
|
|
+ involvesSailing(false),
|
|
|
+ isTradeBuilding(false),
|
|
|
+ isExchange(false),
|
|
|
+ isArmyUpgrade(false),
|
|
|
+ isHero(false),
|
|
|
+ isEnemy(false),
|
|
|
+ explorePriority(0)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
@@ -118,35 +131,17 @@ int32_t estimateTownIncome(CCallback * cb, const CGObjectInstance * target, cons
|
|
|
return booster * (town->hasFort() && town->tempOwner != PlayerColor::NEUTRAL ? booster * 500 : 250);
|
|
|
}
|
|
|
|
|
|
-TResources getCreatureBankResources(const CGObjectInstance * target, const CGHeroInstance * hero)
|
|
|
+int32_t getResourcesGoldReward(const TResources & res)
|
|
|
{
|
|
|
- //Fixme: unused variable hero
|
|
|
+ int32_t result = 0;
|
|
|
|
|
|
- auto objectInfo = target->getObjectHandler()->getObjectInfo(target->appearance);
|
|
|
- CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
|
|
|
- auto resources = bankInfo->getPossibleResourcesReward();
|
|
|
- TResources result = TResources();
|
|
|
- int sum = 0;
|
|
|
-
|
|
|
- for(auto & reward : resources)
|
|
|
+ for(auto r : GameResID::ALL_RESOURCES())
|
|
|
{
|
|
|
- result += reward.data * reward.chance;
|
|
|
- sum += reward.chance;
|
|
|
+ if(res[r] > 0)
|
|
|
+ result += r == EGameResID::GOLD ? res[r] : res[r] * 100;
|
|
|
}
|
|
|
|
|
|
- return sum > 1 ? result / sum : result;
|
|
|
-}
|
|
|
-
|
|
|
-uint64_t getResourcesGoldReward(const TResources & res)
|
|
|
-{
|
|
|
- int nonGoldResources = res[EGameResID::GEMS]
|
|
|
- + res[EGameResID::SULFUR]
|
|
|
- + res[EGameResID::WOOD]
|
|
|
- + res[EGameResID::ORE]
|
|
|
- + res[EGameResID::CRYSTAL]
|
|
|
- + res[EGameResID::MERCURY];
|
|
|
-
|
|
|
- return res[EGameResID::GOLD] + 100 * nonGoldResources;
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
uint64_t getCreatureBankArmyReward(const CGObjectInstance * target, const CGHeroInstance * hero)
|
|
|
@@ -173,10 +168,10 @@ uint64_t getCreatureBankArmyReward(const CGObjectInstance * target, const CGHero
|
|
|
for (auto c : creatures)
|
|
|
{
|
|
|
//Only if hero has slot for this creature in the army
|
|
|
- auto ccre = dynamic_cast<const CCreature*>(c.data.type);
|
|
|
+ auto ccre = dynamic_cast<const CCreature*>(c.data.getType());
|
|
|
if (hero->getSlotFor(ccre).validSlot() || duplicatingSlots > 0)
|
|
|
{
|
|
|
- result += (c.data.type->getAIValue() * c.data.count) * c.chance;
|
|
|
+ result += (c.data.getType()->getAIValue() * c.data.count) * c.chance;
|
|
|
}
|
|
|
/*else
|
|
|
{
|
|
|
@@ -243,16 +238,16 @@ int getDwellingArmyCost(const CGObjectInstance * target)
|
|
|
auto creature = creLevel.second.back().toCreature();
|
|
|
auto creaturesAreFree = creature->getLevel() == 1;
|
|
|
if(!creaturesAreFree)
|
|
|
- cost += creature->getRecruitCost(EGameResID::GOLD) * creLevel.first;
|
|
|
+ cost += creature->getFullRecruitCost().marketValue() * creLevel.first;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return cost;
|
|
|
}
|
|
|
|
|
|
-static uint64_t evaluateArtifactArmyValue(const CArtifactInstance * art)
|
|
|
+static uint64_t evaluateArtifactArmyValue(const CArtifact * art)
|
|
|
{
|
|
|
- if(art->artType->getId() == ArtifactID::SPELL_SCROLL)
|
|
|
+ if(art->getId() == ArtifactID::SPELL_SCROLL)
|
|
|
return 1500;
|
|
|
|
|
|
auto statsValue =
|
|
|
@@ -267,8 +262,10 @@ static uint64_t evaluateArtifactArmyValue(const CArtifactInstance * art)
|
|
|
|
|
|
auto classValue = 0;
|
|
|
|
|
|
- switch(art->artType->aClass)
|
|
|
+ switch(art->aClass)
|
|
|
{
|
|
|
+ case CArtifact::EartClass::ART_TREASURE:
|
|
|
+ //FALL_THROUGH
|
|
|
case CArtifact::EartClass::ART_MINOR:
|
|
|
classValue = 1000;
|
|
|
break;
|
|
|
@@ -302,22 +299,15 @@ uint64_t RewardEvaluator::getArmyReward(
|
|
|
{
|
|
|
case Obj::HILL_FORT:
|
|
|
return ai->armyManager->calculateCreaturesUpgrade(army, target, ai->cb->getResourceAmount()).upgradeValue;
|
|
|
- case Obj::CREATURE_BANK:
|
|
|
- return getCreatureBankArmyReward(target, hero);
|
|
|
case Obj::CREATURE_GENERATOR1:
|
|
|
case Obj::CREATURE_GENERATOR2:
|
|
|
case Obj::CREATURE_GENERATOR3:
|
|
|
case Obj::CREATURE_GENERATOR4:
|
|
|
return getDwellingArmyValue(ai->cb.get(), target, checkGold);
|
|
|
- case Obj::CRYPT:
|
|
|
- case Obj::SHIPWRECK:
|
|
|
- case Obj::SHIPWRECK_SURVIVOR:
|
|
|
- case Obj::WARRIORS_TOMB:
|
|
|
- return 1000;
|
|
|
+ case Obj::SPELL_SCROLL:
|
|
|
+ //FALL_THROUGH
|
|
|
case Obj::ARTIFACT:
|
|
|
- return evaluateArtifactArmyValue(dynamic_cast<const CGArtifact *>(target)->storedArtifact);
|
|
|
- case Obj::DRAGON_UTOPIA:
|
|
|
- return 10000;
|
|
|
+ return evaluateArtifactArmyValue(dynamic_cast<const CGArtifact *>(target)->storedArtifact->getType());
|
|
|
case Obj::HERO:
|
|
|
return relations == PlayerRelations::ENEMIES
|
|
|
? enemyArmyEliminationRewardRatio * dynamic_cast<const CGHeroInstance *>(target)->getArmyStrength()
|
|
|
@@ -328,8 +318,46 @@ uint64_t RewardEvaluator::getArmyReward(
|
|
|
case Obj::MAGIC_SPRING:
|
|
|
return getManaRecoveryArmyReward(hero);
|
|
|
default:
|
|
|
- return 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto rewardable = dynamic_cast<const Rewardable::Interface *>(target);
|
|
|
+
|
|
|
+ if(rewardable)
|
|
|
+ {
|
|
|
+ auto totalValue = 0;
|
|
|
+
|
|
|
+ for(int index : rewardable->getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT))
|
|
|
+ {
|
|
|
+ auto & info = rewardable->configuration.info[index];
|
|
|
+
|
|
|
+ auto rewardValue = 0;
|
|
|
+
|
|
|
+ if(!info.reward.artifacts.empty())
|
|
|
+ {
|
|
|
+ for(auto artID : info.reward.artifacts)
|
|
|
+ {
|
|
|
+ const auto * art = dynamic_cast<const CArtifact *>(VLC->artifacts()->getById(artID));
|
|
|
+
|
|
|
+ rewardValue += evaluateArtifactArmyValue(art);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!info.reward.creatures.empty())
|
|
|
+ {
|
|
|
+ for(const auto & stackInfo : info.reward.creatures)
|
|
|
+ {
|
|
|
+ rewardValue += stackInfo.getType()->getAIValue() * stackInfo.getCount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ totalValue += rewardValue > 0 ? rewardValue / (info.reward.artifacts.size() + info.reward.creatures.size()) : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalValue;
|
|
|
}
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
uint64_t RewardEvaluator::getArmyGrowth(
|
|
|
@@ -468,12 +496,29 @@ uint64_t RewardEvaluator::townArmyGrowth(const CGTownInstance * town) const
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-uint64_t RewardEvaluator::getManaRecoveryArmyReward(const CGHeroInstance * hero) const
|
|
|
+float RewardEvaluator::getManaRecoveryArmyReward(const CGHeroInstance * hero) const
|
|
|
{
|
|
|
return ai->heroManager->getMagicStrength(hero) * 10000 * (1.0f - std::sqrt(static_cast<float>(hero->mana) / hero->manaLimit()));
|
|
|
}
|
|
|
|
|
|
-float RewardEvaluator::getStrategicalValue(const CGObjectInstance * target) const
|
|
|
+float RewardEvaluator::getResourceRequirementStrength(const TResources & res) const
|
|
|
+{
|
|
|
+ float sum = 0.0f;
|
|
|
+
|
|
|
+ for(TResources::nziterator it(res); it.valid(); it++)
|
|
|
+ {
|
|
|
+ //Evaluate resources used for construction. Gold is evaluated separately.
|
|
|
+ if(it->resType != EGameResID::GOLD)
|
|
|
+ {
|
|
|
+ sum += 0.1f * it->resVal * getResourceRequirementStrength(it->resType)
|
|
|
+ + 0.05f * it->resVal * getTotalResourceRequirementStrength(it->resType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return sum;
|
|
|
+}
|
|
|
+
|
|
|
+float RewardEvaluator::getStrategicalValue(const CGObjectInstance * target, const CGHeroInstance * hero) const
|
|
|
{
|
|
|
if(!target)
|
|
|
return 0;
|
|
|
@@ -491,24 +536,10 @@ float RewardEvaluator::getStrategicalValue(const CGObjectInstance * target) cons
|
|
|
case Obj::RESOURCE:
|
|
|
{
|
|
|
auto resource = dynamic_cast<const CGResource *>(target);
|
|
|
- return resource->resourceID() == EGameResID::GOLD
|
|
|
- ? 0
|
|
|
- : 0.2f * getTotalResourceRequirementStrength(resource->resourceID()) + 0.4f * getResourceRequirementStrength(resource->resourceID());
|
|
|
- }
|
|
|
-
|
|
|
- case Obj::CREATURE_BANK:
|
|
|
- {
|
|
|
- auto resourceReward = getCreatureBankResources(target, nullptr);
|
|
|
- float sum = 0.0f;
|
|
|
- for (TResources::nziterator it (resourceReward); it.valid(); it++)
|
|
|
- {
|
|
|
- //Evaluate resources used for construction. Gold is evaluated separately.
|
|
|
- if (it->resType != EGameResID::GOLD)
|
|
|
- {
|
|
|
- sum += 0.1f * it->resVal * getResourceRequirementStrength(it->resType);
|
|
|
- }
|
|
|
- }
|
|
|
- return sum;
|
|
|
+ TResources res;
|
|
|
+ res[resource->resourceID()] = resource->amount;
|
|
|
+
|
|
|
+ return getResourceRequirementStrength(res);
|
|
|
}
|
|
|
|
|
|
case Obj::TOWN:
|
|
|
@@ -546,6 +577,70 @@ float RewardEvaluator::getStrategicalValue(const CGObjectInstance * target) cons
|
|
|
case Obj::KEYMASTER:
|
|
|
return 0.6f;
|
|
|
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto rewardable = dynamic_cast<const Rewardable::Interface *>(target);
|
|
|
+
|
|
|
+ if(rewardable && hero)
|
|
|
+ {
|
|
|
+ auto resourceReward = 0.0f;
|
|
|
+
|
|
|
+ for(int index : rewardable->getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT))
|
|
|
+ {
|
|
|
+ resourceReward += getResourceRequirementStrength(rewardable->configuration.info[index].reward.resources);
|
|
|
+ }
|
|
|
+
|
|
|
+ return resourceReward;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+float RewardEvaluator::getConquestValue(const CGObjectInstance* target) const
|
|
|
+{
|
|
|
+ if (!target)
|
|
|
+ return 0;
|
|
|
+ if (target->getOwner() == ai->playerID)
|
|
|
+ return 0;
|
|
|
+ switch (target->ID)
|
|
|
+ {
|
|
|
+ case Obj::TOWN:
|
|
|
+ {
|
|
|
+ if (ai->buildAnalyzer->getDevelopmentInfo().empty())
|
|
|
+ return 10.0f;
|
|
|
+
|
|
|
+ auto town = dynamic_cast<const CGTownInstance*>(target);
|
|
|
+
|
|
|
+ if (town->getOwner() == ai->playerID)
|
|
|
+ {
|
|
|
+ auto armyIncome = townArmyGrowth(town);
|
|
|
+ auto dailyIncome = town->dailyIncome()[EGameResID::GOLD];
|
|
|
+
|
|
|
+ return std::min(1.0f, std::sqrt(armyIncome / 40000.0f)) + std::min(0.3f, dailyIncome / 10000.0f);
|
|
|
+ }
|
|
|
+
|
|
|
+ auto fortLevel = town->fortLevel();
|
|
|
+ auto booster = 1.0f;
|
|
|
+
|
|
|
+ if (town->hasCapitol())
|
|
|
+ return booster * 1.5;
|
|
|
+
|
|
|
+ if (fortLevel < CGTownInstance::CITADEL)
|
|
|
+ return booster * (town->hasFort() ? 1.0 : 0.8);
|
|
|
+ else
|
|
|
+ return booster * (fortLevel == CGTownInstance::CASTLE ? 1.4 : 1.2);
|
|
|
+ }
|
|
|
+
|
|
|
+ case Obj::HERO:
|
|
|
+ return ai->cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
|
|
|
+ ? getEnemyHeroStrategicalValue(dynamic_cast<const CGHeroInstance*>(target))
|
|
|
+ : 0;
|
|
|
+
|
|
|
+ case Obj::KEYMASTER:
|
|
|
+ return 0.6f;
|
|
|
+
|
|
|
default:
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -593,11 +688,11 @@ float RewardEvaluator::getSkillReward(const CGObjectInstance * target, const CGH
|
|
|
case Obj::ARENA:
|
|
|
return 2;
|
|
|
case Obj::SHRINE_OF_MAGIC_INCANTATION:
|
|
|
- return 0.2f;
|
|
|
+ return 0.25f;
|
|
|
case Obj::SHRINE_OF_MAGIC_GESTURE:
|
|
|
- return 0.3f;
|
|
|
+ return 1.0f;
|
|
|
case Obj::SHRINE_OF_MAGIC_THOUGHT:
|
|
|
- return 0.5f;
|
|
|
+ return 2.0f;
|
|
|
case Obj::LIBRARY_OF_ENLIGHTENMENT:
|
|
|
return 8;
|
|
|
case Obj::WITCH_HUT:
|
|
|
@@ -605,15 +700,55 @@ float RewardEvaluator::getSkillReward(const CGObjectInstance * target, const CGH
|
|
|
case Obj::PANDORAS_BOX:
|
|
|
//Can contains experience, spells, or skills (only on custom maps)
|
|
|
return 2.5f;
|
|
|
- case Obj::PYRAMID:
|
|
|
- return 3.0f;
|
|
|
case Obj::HERO:
|
|
|
return ai->cb->getPlayerRelations(target->tempOwner, ai->playerID) == PlayerRelations::ENEMIES
|
|
|
? enemyHeroEliminationSkillRewardRatio * dynamic_cast<const CGHeroInstance *>(target)->level
|
|
|
: 0;
|
|
|
+
|
|
|
default:
|
|
|
- return 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto rewardable = dynamic_cast<const Rewardable::Interface *>(target);
|
|
|
+
|
|
|
+ if(rewardable)
|
|
|
+ {
|
|
|
+ auto totalValue = 0.0f;
|
|
|
+
|
|
|
+ for(int index : rewardable->getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT))
|
|
|
+ {
|
|
|
+ auto & info = rewardable->configuration.info[index];
|
|
|
+
|
|
|
+ auto rewardValue = 0.0f;
|
|
|
+
|
|
|
+ if(!info.reward.spells.empty())
|
|
|
+ {
|
|
|
+ for(auto spellID : info.reward.spells)
|
|
|
+ {
|
|
|
+ const spells::Spell * spell = VLC->spells()->getById(spellID);
|
|
|
+
|
|
|
+ if(hero->canLearnSpell(spell) && !hero->spellbookContainsSpell(spellID))
|
|
|
+ {
|
|
|
+ rewardValue += std::sqrt(spell->getLevel()) / 4.0f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ totalValue += rewardValue / info.reward.spells.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!info.reward.primary.empty())
|
|
|
+ {
|
|
|
+ for(auto value : info.reward.primary)
|
|
|
+ {
|
|
|
+ totalValue += value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalValue;
|
|
|
}
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
const HitMapInfo & RewardEvaluator::getEnemyHeroDanger(const int3 & tile, uint8_t turn) const
|
|
|
@@ -635,7 +770,7 @@ int32_t getArmyCost(const CArmedInstance * army)
|
|
|
|
|
|
for(auto stack : army->Slots())
|
|
|
{
|
|
|
- value += stack.second->getCreatureID().toCreature()->getRecruitCost(EGameResID::GOLD) * stack.second->count;
|
|
|
+ value += stack.second->getCreatureID().toCreature()->getFullRecruitCost().marketValue() * stack.second->count;
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
@@ -671,22 +806,6 @@ int32_t RewardEvaluator::getGoldReward(const CGObjectInstance * target, const CG
|
|
|
auto * mine = dynamic_cast<const CGMine*>(target);
|
|
|
return dailyIncomeMultiplier * (mine->producedResource == GameResID::GOLD ? 1000 : 75);
|
|
|
}
|
|
|
- case Obj::MYSTICAL_GARDEN:
|
|
|
- case Obj::WINDMILL:
|
|
|
- return 100;
|
|
|
- case Obj::CAMPFIRE:
|
|
|
- return 800;
|
|
|
- case Obj::WAGON:
|
|
|
- return 100;
|
|
|
- case Obj::CREATURE_BANK:
|
|
|
- return getResourcesGoldReward(getCreatureBankResources(target, hero));
|
|
|
- case Obj::CRYPT:
|
|
|
- case Obj::DERELICT_SHIP:
|
|
|
- return 3000;
|
|
|
- case Obj::DRAGON_UTOPIA:
|
|
|
- return 10000;
|
|
|
- case Obj::SEA_CHEST:
|
|
|
- return 1500;
|
|
|
case Obj::PANDORAS_BOX:
|
|
|
return 2500;
|
|
|
case Obj::PRISON:
|
|
|
@@ -697,8 +816,26 @@ int32_t RewardEvaluator::getGoldReward(const CGObjectInstance * target, const CG
|
|
|
? heroEliminationBonus + enemyArmyEliminationGoldRewardRatio * getArmyCost(dynamic_cast<const CGHeroInstance *>(target))
|
|
|
: 0;
|
|
|
default:
|
|
|
- return 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto rewardable = dynamic_cast<const Rewardable::Interface *>(target);
|
|
|
+
|
|
|
+ if(rewardable)
|
|
|
+ {
|
|
|
+ auto goldReward = 0;
|
|
|
+
|
|
|
+ for(int index : rewardable->getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT))
|
|
|
+ {
|
|
|
+ auto & info = rewardable->configuration.info[index];
|
|
|
+
|
|
|
+ goldReward += getResourcesGoldReward(info.reward.resources);
|
|
|
+ }
|
|
|
+
|
|
|
+ return goldReward;
|
|
|
}
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
class HeroExchangeEvaluator : public IEvaluationContextBuilder
|
|
|
@@ -714,7 +851,9 @@ public:
|
|
|
uint64_t armyStrength = heroExchange.getReinforcementArmyStrength(evaluationContext.evaluator.ai);
|
|
|
|
|
|
evaluationContext.addNonCriticalStrategicalValue(2.0f * armyStrength / (float)heroExchange.hero->getArmyStrength());
|
|
|
+ evaluationContext.conquestValue += 2.0f * armyStrength / (float)heroExchange.hero->getArmyStrength();
|
|
|
evaluationContext.heroRole = evaluationContext.evaluator.ai->heroManager->getHeroRole(heroExchange.hero);
|
|
|
+ evaluationContext.isExchange = true;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -732,6 +871,7 @@ public:
|
|
|
|
|
|
evaluationContext.armyReward += upgradeValue;
|
|
|
evaluationContext.addNonCriticalStrategicalValue(upgradeValue / (float)armyUpgrade.hero->getArmyStrength());
|
|
|
+ evaluationContext.isArmyUpgrade = true;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -746,22 +886,46 @@ public:
|
|
|
int tilesDiscovered = task->value;
|
|
|
|
|
|
evaluationContext.addNonCriticalStrategicalValue(0.03f * tilesDiscovered);
|
|
|
+ for (auto obj : evaluationContext.evaluator.ai->cb->getVisitableObjs(task->tile))
|
|
|
+ {
|
|
|
+ switch (obj->ID.num)
|
|
|
+ {
|
|
|
+ case Obj::MONOLITH_ONE_WAY_ENTRANCE:
|
|
|
+ case Obj::MONOLITH_TWO_WAY:
|
|
|
+ case Obj::SUBTERRANEAN_GATE:
|
|
|
+ evaluationContext.explorePriority = 1;
|
|
|
+ break;
|
|
|
+ case Obj::REDWOOD_OBSERVATORY:
|
|
|
+ case Obj::PILLAR_OF_FIRE:
|
|
|
+ evaluationContext.explorePriority = 2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(evaluationContext.evaluator.ai->cb->getTile(task->tile)->roadType != RoadId::NO_ROAD)
|
|
|
+ evaluationContext.explorePriority = 1;
|
|
|
+ if (evaluationContext.explorePriority == 0)
|
|
|
+ evaluationContext.explorePriority = 3;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
class StayAtTownManaRecoveryEvaluator : public IEvaluationContextBuilder
|
|
|
{
|
|
|
public:
|
|
|
- void buildEvaluationContext(EvaluationContext & evaluationContext, Goals::TSubgoal task) const override
|
|
|
+ void buildEvaluationContext(EvaluationContext& evaluationContext, Goals::TSubgoal task) const override
|
|
|
{
|
|
|
- if(task->goalType != Goals::STAY_AT_TOWN)
|
|
|
+ if (task->goalType != Goals::STAY_AT_TOWN)
|
|
|
return;
|
|
|
|
|
|
- Goals::StayAtTown & stayAtTown = dynamic_cast<Goals::StayAtTown &>(*task);
|
|
|
+ Goals::StayAtTown& stayAtTown = dynamic_cast<Goals::StayAtTown&>(*task);
|
|
|
|
|
|
evaluationContext.armyReward += evaluationContext.evaluator.getManaRecoveryArmyReward(stayAtTown.getHero());
|
|
|
- evaluationContext.movementCostByRole[evaluationContext.heroRole] += stayAtTown.getMovementWasted();
|
|
|
- evaluationContext.movementCost += stayAtTown.getMovementWasted();
|
|
|
+ if (evaluationContext.armyReward == 0)
|
|
|
+ evaluationContext.isDefend = true;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ evaluationContext.movementCost += stayAtTown.getMovementWasted();
|
|
|
+ evaluationContext.movementCostByRole[evaluationContext.heroRole] += stayAtTown.getMovementWasted();
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -772,15 +936,8 @@ void addTileDanger(EvaluationContext & evaluationContext, const int3 & tile, uin
|
|
|
if(enemyDanger.danger)
|
|
|
{
|
|
|
auto dangerRatio = enemyDanger.danger / (double)ourStrength;
|
|
|
- auto enemyHero = evaluationContext.evaluator.ai->cb->getObj(enemyDanger.hero.hid, false);
|
|
|
- bool isAI = enemyHero && isAnotherAi(enemyHero, *evaluationContext.evaluator.ai->cb);
|
|
|
-
|
|
|
- if(isAI)
|
|
|
- {
|
|
|
- dangerRatio *= 1.5; // lets make AI bit more afraid of other AI.
|
|
|
- }
|
|
|
-
|
|
|
vstd::amax(evaluationContext.enemyHeroDangerRatio, dangerRatio);
|
|
|
+ vstd::amax(evaluationContext.threat, enemyDanger.threat);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -824,6 +981,10 @@ public:
|
|
|
else
|
|
|
evaluationContext.addNonCriticalStrategicalValue(1.7f * multiplier * strategicalValue);
|
|
|
|
|
|
+ evaluationContext.defenseValue = town->fortLevel();
|
|
|
+ evaluationContext.isDefend = true;
|
|
|
+ evaluationContext.threatTurns = treat.turn;
|
|
|
+
|
|
|
vstd::amax(evaluationContext.danger, defendTown.getTreat().danger);
|
|
|
addTileDanger(evaluationContext, town->visitablePos(), defendTown.getTurn(), defendTown.getDefenceStrength());
|
|
|
}
|
|
|
@@ -845,6 +1006,9 @@ public:
|
|
|
Goals::ExecuteHeroChain & chain = dynamic_cast<Goals::ExecuteHeroChain &>(*task);
|
|
|
const AIPath & path = chain.getPath();
|
|
|
|
|
|
+ if (vstd::isAlmostZero(path.movementCost()))
|
|
|
+ return;
|
|
|
+
|
|
|
vstd::amax(evaluationContext.danger, path.getTotalDanger());
|
|
|
evaluationContext.movementCost += path.movementCost();
|
|
|
evaluationContext.closestWayRatio = chain.closestWayRatio;
|
|
|
@@ -854,14 +1018,24 @@ public:
|
|
|
for(auto & node : path.nodes)
|
|
|
{
|
|
|
vstd::amax(costsPerHero[node.targetHero], node.cost);
|
|
|
+ if (node.layer == EPathfindingLayer::SAIL)
|
|
|
+ evaluationContext.involvesSailing = true;
|
|
|
}
|
|
|
|
|
|
+ float highestCostForSingleHero = 0;
|
|
|
for(auto pair : costsPerHero)
|
|
|
{
|
|
|
auto role = evaluationContext.evaluator.ai->heroManager->getHeroRole(pair.first);
|
|
|
-
|
|
|
evaluationContext.movementCostByRole[role] += pair.second;
|
|
|
+ if (pair.second > highestCostForSingleHero)
|
|
|
+ highestCostForSingleHero = pair.second;
|
|
|
+ }
|
|
|
+ if (highestCostForSingleHero > 1 && costsPerHero.size() > 1)
|
|
|
+ {
|
|
|
+ //Chains that involve more than 1 hero doing something for more than a turn are too expensive in my book. They often involved heroes doing nothing just standing there waiting to fulfill their part of the chain.
|
|
|
+ return;
|
|
|
}
|
|
|
+ evaluationContext.movementCost *= costsPerHero.size(); //further deincentivise chaining as it often involves bringing back the army afterwards
|
|
|
|
|
|
auto hero = task->hero;
|
|
|
bool checkGold = evaluationContext.danger == 0;
|
|
|
@@ -880,10 +1054,18 @@ public:
|
|
|
evaluationContext.armyGrowth += evaluationContext.evaluator.getArmyGrowth(target, hero, army);
|
|
|
evaluationContext.skillReward += evaluationContext.evaluator.getSkillReward(target, hero, heroRole);
|
|
|
evaluationContext.addNonCriticalStrategicalValue(evaluationContext.evaluator.getStrategicalValue(target));
|
|
|
+ evaluationContext.conquestValue += evaluationContext.evaluator.getConquestValue(target);
|
|
|
+ if (target->ID == Obj::HERO)
|
|
|
+ evaluationContext.isHero = true;
|
|
|
+ if (target->getOwner().isValidPlayer() && ai->cb->getPlayerRelations(ai->playerID, target->getOwner()) == PlayerRelations::ENEMIES)
|
|
|
+ evaluationContext.isEnemy = true;
|
|
|
evaluationContext.goldCost += evaluationContext.evaluator.getGoldCost(target, hero, army);
|
|
|
+ if(evaluationContext.danger > 0)
|
|
|
+ evaluationContext.skillReward += (float)evaluationContext.danger / (float)hero->getArmyStrength();
|
|
|
}
|
|
|
+ evaluationContext.armyInvolvement += army->getArmyCost();
|
|
|
|
|
|
- vstd::amax(evaluationContext.armyLossPersentage, path.getTotalArmyLoss() / (double)path.getHeroStrength());
|
|
|
+ vstd::amax(evaluationContext.armyLossPersentage, (float)path.getTotalArmyLoss() / (float)army->getArmyStrength());
|
|
|
addTileDanger(evaluationContext, path.targetTile(), path.turn(), path.getHeroStrength());
|
|
|
vstd::amax(evaluationContext.turn, path.turn());
|
|
|
}
|
|
|
@@ -924,6 +1106,7 @@ public:
|
|
|
evaluationContext.armyReward += evaluationContext.evaluator.getArmyReward(target, hero, army, checkGold) / boost;
|
|
|
evaluationContext.skillReward += evaluationContext.evaluator.getSkillReward(target, hero, role) / boost;
|
|
|
evaluationContext.addNonCriticalStrategicalValue(evaluationContext.evaluator.getStrategicalValue(target) / boost);
|
|
|
+ evaluationContext.conquestValue += evaluationContext.evaluator.getConquestValue(target);
|
|
|
evaluationContext.goldCost += evaluationContext.evaluator.getGoldCost(target, hero, army) / boost;
|
|
|
evaluationContext.movementCostByRole[role] += objInfo.second.movementCost / boost;
|
|
|
evaluationContext.movementCost += objInfo.second.movementCost / boost;
|
|
|
@@ -949,6 +1132,14 @@ public:
|
|
|
Goals::ExchangeSwapTownHeroes & swapCommand = dynamic_cast<Goals::ExchangeSwapTownHeroes &>(*task);
|
|
|
const CGHeroInstance * garrisonHero = swapCommand.getGarrisonHero();
|
|
|
|
|
|
+ logAi->trace("buildEvaluationContext ExchangeSwapTownHeroesContextBuilder %s affected objects: %d", swapCommand.toString(), swapCommand.getAffectedObjects().size());
|
|
|
+ for (auto obj : swapCommand.getAffectedObjects())
|
|
|
+ {
|
|
|
+ logAi->trace("affected object: %s", evaluationContext.evaluator.ai->cb->getObj(obj)->getObjectName());
|
|
|
+ }
|
|
|
+ if (garrisonHero)
|
|
|
+ logAi->debug("with %s and %d", garrisonHero->getNameTranslated(), int(swapCommand.getLockingReason()));
|
|
|
+
|
|
|
if(garrisonHero && swapCommand.getLockingReason() == HeroLockedReason::DEFENCE)
|
|
|
{
|
|
|
auto defenderRole = evaluationContext.evaluator.ai->heroManager->getHeroRole(garrisonHero);
|
|
|
@@ -957,6 +1148,9 @@ public:
|
|
|
evaluationContext.movementCost += mpLeft;
|
|
|
evaluationContext.movementCostByRole[defenderRole] += mpLeft;
|
|
|
evaluationContext.heroRole = defenderRole;
|
|
|
+ evaluationContext.isDefend = true;
|
|
|
+ evaluationContext.armyInvolvement = garrisonHero->getArmyStrength();
|
|
|
+ logAi->debug("evaluationContext.isDefend: %d", evaluationContext.isDefend);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
@@ -1000,8 +1194,14 @@ public:
|
|
|
evaluationContext.goldReward += 7 * bi.dailyIncome[EGameResID::GOLD] / 2; // 7 day income but half we already have
|
|
|
evaluationContext.heroRole = HeroRole::MAIN;
|
|
|
evaluationContext.movementCostByRole[evaluationContext.heroRole] += bi.prerequisitesCount;
|
|
|
- evaluationContext.goldCost += bi.buildCostWithPrerequisits[EGameResID::GOLD];
|
|
|
+ int32_t cost = bi.buildCost[EGameResID::GOLD];
|
|
|
+ evaluationContext.goldCost += cost;
|
|
|
evaluationContext.closestWayRatio = 1;
|
|
|
+ evaluationContext.buildingCost += bi.buildCostWithPrerequisites;
|
|
|
+ if (bi.id == BuildingID::MARKETPLACE || bi.dailyIncome[EGameResID::WOOD] > 0)
|
|
|
+ evaluationContext.isTradeBuilding = true;
|
|
|
+
|
|
|
+ logAi->trace("Building costs for %s : %s MarketValue: %d",bi.toString(), evaluationContext.buildingCost.toString(), evaluationContext.buildingCost.marketValue());
|
|
|
|
|
|
if(bi.creatureID != CreatureID::NONE)
|
|
|
{
|
|
|
@@ -1028,7 +1228,18 @@ public:
|
|
|
else if(bi.id >= BuildingID::MAGES_GUILD_1 && bi.id <= BuildingID::MAGES_GUILD_5)
|
|
|
{
|
|
|
evaluationContext.skillReward += 2 * (bi.id - BuildingID::MAGES_GUILD_1);
|
|
|
+ for (auto hero : evaluationContext.evaluator.ai->cb->getHeroesInfo())
|
|
|
+ {
|
|
|
+ evaluationContext.armyInvolvement += hero->getArmyCost();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int sameTownBonus = 0;
|
|
|
+ for (auto town : evaluationContext.evaluator.ai->cb->getTownsInfo())
|
|
|
+ {
|
|
|
+ if (buildThis.town->getFaction() == town->getFaction())
|
|
|
+ sameTownBonus += town->getTownLevel();
|
|
|
}
|
|
|
+ evaluationContext.armyReward *= sameTownBonus;
|
|
|
|
|
|
if(evaluationContext.goldReward)
|
|
|
{
|
|
|
@@ -1048,7 +1259,7 @@ public:
|
|
|
|
|
|
uint64_t RewardEvaluator::getUpgradeArmyReward(const CGTownInstance * town, const BuildingInfo & bi) const
|
|
|
{
|
|
|
- if(ai->buildAnalyzer->hasAnyBuilding(town->getFaction(), bi.id))
|
|
|
+ if(ai->buildAnalyzer->hasAnyBuilding(town->getFactionID(), bi.id))
|
|
|
return 0;
|
|
|
|
|
|
auto creaturesToUpgrade = ai->armyManager->getTotalCreaturesAvailable(bi.baseCreatureID);
|
|
|
@@ -1090,6 +1301,7 @@ EvaluationContext PriorityEvaluator::buildEvaluationContext(Goals::TSubgoal goal
|
|
|
for(auto subgoal : parts)
|
|
|
{
|
|
|
context.goldCost += subgoal->goldCost;
|
|
|
+ context.buildingCost += subgoal->buildingCost;
|
|
|
|
|
|
for(auto builder : evaluationContextBuilders)
|
|
|
{
|
|
|
@@ -1100,7 +1312,7 @@ EvaluationContext PriorityEvaluator::buildEvaluationContext(Goals::TSubgoal goal
|
|
|
return context;
|
|
|
}
|
|
|
|
|
|
-float PriorityEvaluator::evaluate(Goals::TSubgoal task)
|
|
|
+float PriorityEvaluator::evaluate(Goals::TSubgoal task, int priorityTier)
|
|
|
{
|
|
|
auto evaluationContext = buildEvaluationContext(task);
|
|
|
|
|
|
@@ -1113,47 +1325,284 @@ float PriorityEvaluator::evaluate(Goals::TSubgoal task)
|
|
|
|
|
|
double result = 0;
|
|
|
|
|
|
- try
|
|
|
+ if (ai->settings->isUseFuzzy())
|
|
|
{
|
|
|
- armyLossPersentageVariable->setValue(evaluationContext.armyLossPersentage);
|
|
|
- heroRoleVariable->setValue(evaluationContext.heroRole);
|
|
|
- mainTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::MAIN]);
|
|
|
- scoutTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::SCOUT]);
|
|
|
- goldRewardVariable->setValue(goldRewardPerTurn);
|
|
|
- armyRewardVariable->setValue(evaluationContext.armyReward);
|
|
|
- armyGrowthVariable->setValue(evaluationContext.armyGrowth);
|
|
|
- skillRewardVariable->setValue(evaluationContext.skillReward);
|
|
|
- dangerVariable->setValue(evaluationContext.danger);
|
|
|
- rewardTypeVariable->setValue(rewardType);
|
|
|
- closestHeroRatioVariable->setValue(evaluationContext.closestWayRatio);
|
|
|
- strategicalValueVariable->setValue(evaluationContext.strategicalValue);
|
|
|
- goldPressureVariable->setValue(ai->buildAnalyzer->getGoldPressure());
|
|
|
- goldCostVariable->setValue(evaluationContext.goldCost / ((float)ai->getFreeResources()[EGameResID::GOLD] + (float)ai->buildAnalyzer->getDailyIncome()[EGameResID::GOLD] + 1.0f));
|
|
|
- turnVariable->setValue(evaluationContext.turn);
|
|
|
- fearVariable->setValue(evaluationContext.enemyHeroDangerRatio);
|
|
|
-
|
|
|
- engine->process();
|
|
|
-
|
|
|
- result = value->getValue();
|
|
|
+ float fuzzyResult = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ armyLossPersentageVariable->setValue(evaluationContext.armyLossPersentage);
|
|
|
+ heroRoleVariable->setValue(evaluationContext.heroRole);
|
|
|
+ mainTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::MAIN]);
|
|
|
+ scoutTurnDistanceVariable->setValue(evaluationContext.movementCostByRole[HeroRole::SCOUT]);
|
|
|
+ goldRewardVariable->setValue(goldRewardPerTurn);
|
|
|
+ armyRewardVariable->setValue(evaluationContext.armyReward);
|
|
|
+ armyGrowthVariable->setValue(evaluationContext.armyGrowth);
|
|
|
+ skillRewardVariable->setValue(evaluationContext.skillReward);
|
|
|
+ dangerVariable->setValue(evaluationContext.danger);
|
|
|
+ rewardTypeVariable->setValue(rewardType);
|
|
|
+ closestHeroRatioVariable->setValue(evaluationContext.closestWayRatio);
|
|
|
+ strategicalValueVariable->setValue(evaluationContext.strategicalValue);
|
|
|
+ goldPressureVariable->setValue(ai->buildAnalyzer->getGoldPressure());
|
|
|
+ goldCostVariable->setValue(evaluationContext.goldCost / ((float)ai->getFreeResources()[EGameResID::GOLD] + (float)ai->buildAnalyzer->getDailyIncome()[EGameResID::GOLD] + 1.0f));
|
|
|
+ turnVariable->setValue(evaluationContext.turn);
|
|
|
+ fearVariable->setValue(evaluationContext.enemyHeroDangerRatio);
|
|
|
+
|
|
|
+ engine->process();
|
|
|
+
|
|
|
+ fuzzyResult = value->getValue();
|
|
|
+ }
|
|
|
+ catch (fl::Exception& fe)
|
|
|
+ {
|
|
|
+ logAi->error("evaluate VisitTile: %s", fe.getWhat());
|
|
|
+ }
|
|
|
+ result = fuzzyResult;
|
|
|
}
|
|
|
- catch(fl::Exception & fe)
|
|
|
+ else
|
|
|
{
|
|
|
- logAi->error("evaluate VisitTile: %s", fe.getWhat());
|
|
|
+ float score = 0;
|
|
|
+ const bool amIInDanger = ai->cb->getTownsInfo().empty() || (evaluationContext.isDefend && evaluationContext.threatTurns == 0);
|
|
|
+ const float maxWillingToLose = amIInDanger ? 1 : ai->settings->getMaxArmyLossTarget();
|
|
|
+
|
|
|
+ bool arriveNextWeek = false;
|
|
|
+ if (ai->cb->getDate(Date::DAY_OF_WEEK) + evaluationContext.turn > 7 && priorityTier < PriorityTier::FAR_KILL)
|
|
|
+ arriveNextWeek = true;
|
|
|
+
|
|
|
+#if NKAI_TRACE_LEVEL >= 2
|
|
|
+ logAi->trace("BEFORE: priorityTier %d, Evaluated %s, loss: %f, turn: %d, turns main: %f, scout: %f, army-involvement: %f, gold: %f, cost: %d, army gain: %f, army growth: %f skill: %f danger: %d, threatTurns: %d, threat: %d, role: %s, strategical value: %f, conquest value: %f cwr: %f, fear: %f, explorePriority: %d isDefend: %d isEnemy: %d arriveNextWeek: %d",
|
|
|
+ priorityTier,
|
|
|
+ task->toString(),
|
|
|
+ evaluationContext.armyLossPersentage,
|
|
|
+ (int)evaluationContext.turn,
|
|
|
+ evaluationContext.movementCostByRole[HeroRole::MAIN],
|
|
|
+ evaluationContext.movementCostByRole[HeroRole::SCOUT],
|
|
|
+ evaluationContext.armyInvolvement,
|
|
|
+ goldRewardPerTurn,
|
|
|
+ evaluationContext.goldCost,
|
|
|
+ evaluationContext.armyReward,
|
|
|
+ evaluationContext.armyGrowth,
|
|
|
+ evaluationContext.skillReward,
|
|
|
+ evaluationContext.danger,
|
|
|
+ evaluationContext.threatTurns,
|
|
|
+ evaluationContext.threat,
|
|
|
+ evaluationContext.heroRole == HeroRole::MAIN ? "main" : "scout",
|
|
|
+ evaluationContext.strategicalValue,
|
|
|
+ evaluationContext.conquestValue,
|
|
|
+ evaluationContext.closestWayRatio,
|
|
|
+ evaluationContext.enemyHeroDangerRatio,
|
|
|
+ evaluationContext.explorePriority,
|
|
|
+ evaluationContext.isDefend,
|
|
|
+ evaluationContext.isEnemy,
|
|
|
+ arriveNextWeek);
|
|
|
+#endif
|
|
|
+
|
|
|
+ switch (priorityTier)
|
|
|
+ {
|
|
|
+ case PriorityTier::INSTAKILL: //Take towns / kill heroes in immediate reach
|
|
|
+ {
|
|
|
+ if (evaluationContext.turn > 0)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.movementCost >= 1)
|
|
|
+ return 0;
|
|
|
+ if(evaluationContext.conquestValue > 0)
|
|
|
+ score = evaluationContext.armyInvolvement;
|
|
|
+ if (vstd::isAlmostZero(score) || (evaluationContext.enemyHeroDangerRatio > 1 && (evaluationContext.turn > 0 || evaluationContext.isExchange) && !ai->cb->getTownsInfo().empty()))
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::INSTADEFEND: //Defend immediately threatened towns
|
|
|
+ {
|
|
|
+ if (evaluationContext.isDefend && evaluationContext.threatTurns == 0 && evaluationContext.turn == 0)
|
|
|
+ score = evaluationContext.armyInvolvement;
|
|
|
+ if (evaluationContext.isEnemy && maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::KILL: //Take towns / kill heroes that are further away
|
|
|
+ //FALL_THROUGH
|
|
|
+ case PriorityTier::FAR_KILL:
|
|
|
+ {
|
|
|
+ if (evaluationContext.turn > 0 && evaluationContext.isHero)
|
|
|
+ return 0;
|
|
|
+ if (arriveNextWeek && evaluationContext.isEnemy)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.conquestValue > 0)
|
|
|
+ score = evaluationContext.armyInvolvement;
|
|
|
+ if (vstd::isAlmostZero(score) || (evaluationContext.enemyHeroDangerRatio > 1 && (evaluationContext.turn > 0 || evaluationContext.isExchange) && !ai->cb->getTownsInfo().empty()))
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ score *= evaluationContext.closestWayRatio;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::UPGRADE:
|
|
|
+ {
|
|
|
+ if (!evaluationContext.isArmyUpgrade)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1)
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ if (vstd::isAlmostZero(evaluationContext.armyLossPersentage) && evaluationContext.closestWayRatio < 1.0)
|
|
|
+ return 0;
|
|
|
+ score = 1000;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::HIGH_PRIO_EXPLORE:
|
|
|
+ {
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.explorePriority != 1)
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ if (vstd::isAlmostZero(evaluationContext.armyLossPersentage) && evaluationContext.closestWayRatio < 1.0)
|
|
|
+ return 0;
|
|
|
+ score = 1000;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::HUNTER_GATHER: //Collect guarded stuff
|
|
|
+ //FALL_THROUGH
|
|
|
+ case PriorityTier::FAR_HUNTER_GATHER:
|
|
|
+ {
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1 && !evaluationContext.isDefend)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.buildingCost.marketValue() > 0)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.isDefend && (evaluationContext.enemyHeroDangerRatio < 1 || evaluationContext.threatTurns > 0 || evaluationContext.turn > 0))
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.explorePriority == 3)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.isArmyUpgrade)
|
|
|
+ return 0;
|
|
|
+ if ((evaluationContext.enemyHeroDangerRatio > 0 && arriveNextWeek) || evaluationContext.enemyHeroDangerRatio > 1)
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ if (vstd::isAlmostZero(evaluationContext.armyLossPersentage) && evaluationContext.closestWayRatio < 1.0)
|
|
|
+ return 0;
|
|
|
+ score += evaluationContext.strategicalValue * 1000;
|
|
|
+ score += evaluationContext.goldReward;
|
|
|
+ score += evaluationContext.skillReward * evaluationContext.armyInvolvement * (1 - evaluationContext.armyLossPersentage) * 0.05;
|
|
|
+ score += evaluationContext.armyReward;
|
|
|
+ score += evaluationContext.armyGrowth;
|
|
|
+ score -= evaluationContext.goldCost;
|
|
|
+ score -= evaluationContext.armyInvolvement * evaluationContext.armyLossPersentage;
|
|
|
+ if (score > 0)
|
|
|
+ {
|
|
|
+ score = 1000;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::LOW_PRIO_EXPLORE:
|
|
|
+ {
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.explorePriority != 3)
|
|
|
+ return 0;
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.closestWayRatio < 1.0)
|
|
|
+ return 0;
|
|
|
+ score = 1000;
|
|
|
+ if (evaluationContext.movementCost > 0)
|
|
|
+ score /= evaluationContext.movementCost;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::DEFEND: //Defend whatever if nothing else is to do
|
|
|
+ {
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1 && evaluationContext.isExchange)
|
|
|
+ return 0;
|
|
|
+ if (evaluationContext.isDefend || evaluationContext.isArmyUpgrade)
|
|
|
+ score = evaluationContext.armyInvolvement;
|
|
|
+ score /= (evaluationContext.turn + 1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PriorityTier::BUILDINGS: //For buildings and buying army
|
|
|
+ {
|
|
|
+ if (maxWillingToLose - evaluationContext.armyLossPersentage < 0)
|
|
|
+ return 0;
|
|
|
+ //If we already have locked resources, we don't look at other buildings
|
|
|
+ if (ai->getLockedResources().marketValue() > 0)
|
|
|
+ return 0;
|
|
|
+ score += evaluationContext.conquestValue * 1000;
|
|
|
+ score += evaluationContext.strategicalValue * 1000;
|
|
|
+ score += evaluationContext.goldReward;
|
|
|
+ score += evaluationContext.skillReward * evaluationContext.armyInvolvement * (1 - evaluationContext.armyLossPersentage) * 0.05;
|
|
|
+ score += evaluationContext.armyReward;
|
|
|
+ score += evaluationContext.armyGrowth;
|
|
|
+ if (evaluationContext.buildingCost.marketValue() > 0)
|
|
|
+ {
|
|
|
+ if (!evaluationContext.isTradeBuilding && ai->getFreeResources()[EGameResID::WOOD] - evaluationContext.buildingCost[EGameResID::WOOD] < 5 && ai->buildAnalyzer->getDailyIncome()[EGameResID::WOOD] < 1)
|
|
|
+ {
|
|
|
+ logAi->trace("Should make sure to build market-place instead of %s", task->toString());
|
|
|
+ for (auto town : ai->cb->getTownsInfo())
|
|
|
+ {
|
|
|
+ if (!town->hasBuiltSomeTradeBuilding())
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ score += 1000;
|
|
|
+ auto resourcesAvailable = evaluationContext.evaluator.ai->getFreeResources();
|
|
|
+ auto income = ai->buildAnalyzer->getDailyIncome();
|
|
|
+ if(ai->buildAnalyzer->isGoldPressureHigh())
|
|
|
+ score /= evaluationContext.buildingCost.marketValue();
|
|
|
+ if (!resourcesAvailable.canAfford(evaluationContext.buildingCost))
|
|
|
+ {
|
|
|
+ TResources needed = evaluationContext.buildingCost - resourcesAvailable;
|
|
|
+ needed.positive();
|
|
|
+ int turnsTo = needed.maxPurchasableCount(income);
|
|
|
+ if (turnsTo == INT_MAX)
|
|
|
+ return 0;
|
|
|
+ else
|
|
|
+ score /= turnsTo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (evaluationContext.enemyHeroDangerRatio > 1 && !evaluationContext.isDefend && vstd::isAlmostZero(evaluationContext.conquestValue))
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result = score;
|
|
|
+ //TODO: Figure out the root cause for why evaluationContext.closestWayRatio has become -nan(ind).
|
|
|
+ if (std::isnan(result))
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
|
|
- logAi->trace("Evaluated %s, loss: %f, turn: %d, turns main: %f, scout: %f, gold: %f, cost: %d, army gain: %f, danger: %d, role: %s, strategical value: %f, cwr: %f, fear: %f, result %f",
|
|
|
+ logAi->trace("priorityTier %d, Evaluated %s, loss: %f, turn: %d, turns main: %f, scout: %f, army-involvement: %f, gold: %f, cost: %d, army gain: %f, army growth: %f skill: %f danger: %d, threatTurns: %d, threat: %d, role: %s, strategical value: %f, conquest value: %f cwr: %f, fear: %f, result %f",
|
|
|
+ priorityTier,
|
|
|
task->toString(),
|
|
|
evaluationContext.armyLossPersentage,
|
|
|
(int)evaluationContext.turn,
|
|
|
evaluationContext.movementCostByRole[HeroRole::MAIN],
|
|
|
evaluationContext.movementCostByRole[HeroRole::SCOUT],
|
|
|
+ evaluationContext.armyInvolvement,
|
|
|
goldRewardPerTurn,
|
|
|
evaluationContext.goldCost,
|
|
|
evaluationContext.armyReward,
|
|
|
+ evaluationContext.armyGrowth,
|
|
|
+ evaluationContext.skillReward,
|
|
|
evaluationContext.danger,
|
|
|
+ evaluationContext.threatTurns,
|
|
|
+ evaluationContext.threat,
|
|
|
evaluationContext.heroRole == HeroRole::MAIN ? "main" : "scout",
|
|
|
evaluationContext.strategicalValue,
|
|
|
+ evaluationContext.conquestValue,
|
|
|
evaluationContext.closestWayRatio,
|
|
|
evaluationContext.enemyHeroDangerRatio,
|
|
|
result);
|