|
|
@@ -89,19 +89,6 @@ armyStructure evaluateArmyStructure(const CArmedInstance * army)
|
|
|
return as;
|
|
|
}
|
|
|
|
|
|
-float HeroMovementGoalEngineBase::calculateTurnDistanceInputValue(const Goals::AbstractGoal & goal) const
|
|
|
-{
|
|
|
- if(goal.evaluationContext.movementCost > 0)
|
|
|
- {
|
|
|
- return goal.evaluationContext.movementCost;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- auto pathInfo = ai->myCb->getPathsInfo(goal.hero.h)->getPathInfo(goal.tile);
|
|
|
- return pathInfo->cost;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
TacticalAdvantageEngine::TacticalAdvantageEngine()
|
|
|
{
|
|
|
try
|
|
|
@@ -254,206 +241,3 @@ float TacticalAdvantageEngine::getTacticalAdvantage(const CArmedInstance * we, c
|
|
|
|
|
|
return output;
|
|
|
}
|
|
|
-
|
|
|
-//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec)
|
|
|
-
|
|
|
-HeroMovementGoalEngineBase::HeroMovementGoalEngineBase()
|
|
|
-{
|
|
|
- try
|
|
|
- {
|
|
|
- strengthRatio = new fl::InputVariable("strengthRatio"); //hero must be strong enough to defeat guards
|
|
|
- heroStrengthVariable = new fl::InputVariable("heroStrengthVariable"); //we want to use weakest possible hero
|
|
|
- turnDistanceVariable = new fl::InputVariable("turnDistanceVariable"); //we want to use hero who is near
|
|
|
- missionImportance = new fl::InputVariable("lockedMissionImportance"); //we may want to preempt hero with low-priority mission
|
|
|
- value = new fl::OutputVariable("Value");
|
|
|
- value->setMinimum(0);
|
|
|
- value->setMaximum(5);
|
|
|
-
|
|
|
- std::vector<fl::InputVariable *> helper = { strengthRatio, heroStrengthVariable, turnDistanceVariable, missionImportance };
|
|
|
- for(auto val : helper)
|
|
|
- {
|
|
|
- engine.addInputVariable(val);
|
|
|
- }
|
|
|
- engine.addOutputVariable(value);
|
|
|
-
|
|
|
- strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0));
|
|
|
- strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3));
|
|
|
- strengthRatio->setRange(0, SAFE_ATTACK_CONSTANT * 3);
|
|
|
-
|
|
|
- //strength compared to our main hero
|
|
|
- heroStrengthVariable->addTerm(new fl::Ramp("LOW", 0.5, 0));
|
|
|
- heroStrengthVariable->addTerm(new fl::Triangle("MEDIUM", 0.2, 0.8));
|
|
|
- heroStrengthVariable->addTerm(new fl::Ramp("HIGH", 0.5, 1));
|
|
|
- heroStrengthVariable->setRange(0.0, 1.0);
|
|
|
-
|
|
|
- turnDistanceVariable->addTerm(new fl::Ramp("SHORT", 0.5, 0));
|
|
|
- turnDistanceVariable->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
|
|
|
- turnDistanceVariable->addTerm(new fl::Ramp("LONG", 0.5, 10));
|
|
|
- turnDistanceVariable->setRange(0.0, 10.0);
|
|
|
-
|
|
|
- missionImportance->addTerm(new fl::Ramp("LOW", 2.5, 0));
|
|
|
- missionImportance->addTerm(new fl::Triangle("MEDIUM", 2, 3));
|
|
|
- missionImportance->addTerm(new fl::Ramp("HIGH", 2.5, 5));
|
|
|
- missionImportance->setRange(0.0, 5.0);
|
|
|
-
|
|
|
- //an issue: in 99% cases this outputs center of mass (2.5) regardless of actual input :/
|
|
|
- //should be same as "mission Importance" to keep consistency
|
|
|
- value->addTerm(new fl::Ramp("LOW", 2.5, 0));
|
|
|
- value->addTerm(new fl::Triangle("MEDIUM", 2, 3)); //can't be center of mass :/
|
|
|
- value->addTerm(new fl::Ramp("HIGH", 2.5, 5));
|
|
|
- value->setRange(0.0, 5.0);
|
|
|
-
|
|
|
- //use unarmed scouts if possible
|
|
|
- addRule("if strengthRatio is HIGH and heroStrengthVariable is LOW then Value is HIGH");
|
|
|
- //we may want to use secondary hero(es) rather than main hero
|
|
|
- addRule("if strengthRatio is HIGH and heroStrengthVariable is MEDIUM then Value is MEDIUM");
|
|
|
- addRule("if strengthRatio is HIGH and heroStrengthVariable is HIGH then Value is LOW");
|
|
|
- //don't assign targets to heroes who are too weak, but prefer targets of our main hero (in case we need to gather army)
|
|
|
- addRule("if strengthRatio is LOW and heroStrengthVariable is LOW then Value is LOW");
|
|
|
- //attempt to arm secondary heroes is not stupid
|
|
|
- addRule("if strengthRatio is LOW and heroStrengthVariable is MEDIUM then Value is HIGH");
|
|
|
- addRule("if strengthRatio is LOW and heroStrengthVariable is HIGH then Value is LOW");
|
|
|
-
|
|
|
- //do not cancel important goals
|
|
|
- addRule("if lockedMissionImportance is HIGH then Value is LOW");
|
|
|
- addRule("if lockedMissionImportance is MEDIUM then Value is MEDIUM");
|
|
|
- addRule("if lockedMissionImportance is LOW then Value is HIGH");
|
|
|
- //pick nearby objects if it's easy, avoid long walks
|
|
|
- addRule("if turnDistanceVariable is SHORT then Value is HIGH");
|
|
|
- addRule("if turnDistanceVariable is MEDIUM then Value is MEDIUM");
|
|
|
- addRule("if turnDistanceVariable is LONG then Value is LOW");
|
|
|
- }
|
|
|
- catch(fl::Exception & fe)
|
|
|
- {
|
|
|
- logAi->error("HeroMovementGoalEngineBase: %s", fe.getWhat());
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void HeroMovementGoalEngineBase::setSharedFuzzyVariables(Goals::AbstractGoal & goal)
|
|
|
-{
|
|
|
- float turns = calculateTurnDistanceInputValue(goal);
|
|
|
- float missionImportanceData = 0;
|
|
|
-
|
|
|
- if(vstd::contains(ai->lockedHeroes, goal.hero))
|
|
|
- {
|
|
|
- missionImportanceData = ai->lockedHeroes[goal.hero]->priority;
|
|
|
- }
|
|
|
- else if(goal.parent)
|
|
|
- {
|
|
|
- missionImportanceData = goal.parent->priority;
|
|
|
- }
|
|
|
-
|
|
|
- float strengthRatioData = 10.0f; //we are much stronger than enemy
|
|
|
- ui64 danger = fh->evaluateDanger(goal.tile, goal.hero.h);
|
|
|
- if(danger)
|
|
|
- strengthRatioData = (fl::scalar)goal.hero.h->getTotalStrength() / danger;
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- strengthRatio->setValue(strengthRatioData);
|
|
|
- heroStrengthVariable->setValue((fl::scalar)goal.hero->getTotalStrength() / ai->primaryHero()->getTotalStrength());
|
|
|
- turnDistanceVariable->setValue(turns);
|
|
|
- missionImportance->setValue(missionImportanceData);
|
|
|
- }
|
|
|
- catch(fl::Exception & fe)
|
|
|
- {
|
|
|
- logAi->error("HeroMovementGoalEngineBase::setSharedFuzzyVariables: %s", fe.getWhat());
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-VisitObjEngine::VisitObjEngine()
|
|
|
-{
|
|
|
- try
|
|
|
- {
|
|
|
- objectValue = new fl::InputVariable("objectValue"); //value of that object type known by AI
|
|
|
-
|
|
|
- engine.addInputVariable(objectValue);
|
|
|
-
|
|
|
- //objectValue ranges are based on checking RMG priorities of some objects and checking LOW/MID/HIGH proportions for various values in QtFuzzyLite
|
|
|
- objectValue->addTerm(new fl::Ramp("LOW", 3500.0, 0.0));
|
|
|
- objectValue->addTerm(new fl::Triangle("MEDIUM", 0.0, 8500.0));
|
|
|
- std::vector<fl::Discrete::Pair> multiRamp = { fl::Discrete::Pair(5000.0, 0.0), fl::Discrete::Pair(10000.0, 0.75), fl::Discrete::Pair(20000.0, 1.0) };
|
|
|
- objectValue->addTerm(new fl::Discrete("HIGH", multiRamp));
|
|
|
- objectValue->setRange(0.0, 20000.0); //relic artifact value is border value by design, even better things are scaled down.
|
|
|
-
|
|
|
- addRule("if objectValue is HIGH then Value is HIGH");
|
|
|
- addRule("if objectValue is MEDIUM then Value is MEDIUM");
|
|
|
- addRule("if objectValue is LOW then Value is LOW");
|
|
|
- }
|
|
|
- catch(fl::Exception & fe)
|
|
|
- {
|
|
|
- logAi->error("FindWanderTarget: %s", fe.getWhat());
|
|
|
- }
|
|
|
- configure();
|
|
|
-}
|
|
|
-
|
|
|
-float VisitObjEngine::evaluate(Goals::VisitObj & goal)
|
|
|
-{
|
|
|
- if(!goal.hero)
|
|
|
- return 0;
|
|
|
-
|
|
|
- auto obj = ai->myCb->getObj(ObjectInstanceID(goal.objid));
|
|
|
- if(!obj)
|
|
|
- {
|
|
|
- logAi->error("Goals::VisitObj objid " + std::to_string(goal.objid) + " no longer visible, probably goal used for something it's not intended");
|
|
|
- return -100; // FIXME: Added check when goal was used for hero instead of VisitHero, but crashes are bad anyway
|
|
|
- }
|
|
|
-
|
|
|
- boost::optional<int> objValueKnownByAI = MapObjectsEvaluator::getInstance().getObjectValue(obj);
|
|
|
- int objValue = 0;
|
|
|
-
|
|
|
- if(objValueKnownByAI != boost::none) //consider adding value manipulation based on object instances on map
|
|
|
- {
|
|
|
- objValue = std::min(std::max(objValueKnownByAI.get(), 0), 20000);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- MapObjectsEvaluator::getInstance().addObjectData(obj->ID, obj->subID, 0);
|
|
|
- logGlobal->error("AI met object type it doesn't know - ID: " + std::to_string(obj->ID) + ", subID: " + std::to_string(obj->subID) + " - adding to database with value " + std::to_string(objValue));
|
|
|
- }
|
|
|
-
|
|
|
- setSharedFuzzyVariables(goal);
|
|
|
-
|
|
|
- float output = -1.0f;
|
|
|
- try
|
|
|
- {
|
|
|
- objectValue->setValue(objValue);
|
|
|
- engine.process();
|
|
|
- output = value->getValue();
|
|
|
- }
|
|
|
- catch(fl::Exception & fe)
|
|
|
- {
|
|
|
- logAi->error("evaluate getWanderTargetObjectValue: %s", fe.getWhat());
|
|
|
- }
|
|
|
- assert(output >= 0.0f);
|
|
|
- return output;
|
|
|
-}
|
|
|
-
|
|
|
-VisitTileEngine::VisitTileEngine() //so far no VisitTile-specific variables that are not shared with HeroMovementGoalEngineBase
|
|
|
-{
|
|
|
- configure();
|
|
|
-}
|
|
|
-
|
|
|
-float VisitTileEngine::evaluate(Goals::VisitTile & goal)
|
|
|
-{
|
|
|
- //we assume that hero is already set and we want to choose most suitable one for the mission
|
|
|
- if(!goal.hero)
|
|
|
- return 0;
|
|
|
-
|
|
|
- //assert(cb->isInTheMap(g.tile));
|
|
|
-
|
|
|
- setSharedFuzzyVariables(goal);
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- engine.process();
|
|
|
-
|
|
|
- goal.priority = value->getValue();
|
|
|
- }
|
|
|
- catch(fl::Exception & fe)
|
|
|
- {
|
|
|
- logAi->error("evaluate VisitTile: %s", fe.getWhat());
|
|
|
- }
|
|
|
- assert(goal.priority >= 0);
|
|
|
- return goal.priority;
|
|
|
-}
|