mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
Merge pull request #474 from vcmi/FuzzyHelperRework
FuzzyHelper refactoring + Ai Map Object Evaluation improvements
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "AIUtility.h"
|
#include "AIUtility.h"
|
||||||
#include "VCAI.h"
|
#include "VCAI.h"
|
||||||
#include "Fuzzy.h"
|
#include "FuzzyHelper.h"
|
||||||
|
|
||||||
#include "../../lib/UnlockGuard.h"
|
#include "../../lib/UnlockGuard.h"
|
||||||
#include "../../lib/CConfigHandler.h"
|
#include "../../lib/CConfigHandler.h"
|
||||||
@@ -245,7 +245,7 @@ ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor)
|
|||||||
auto armedObj = dynamic_cast<const CArmedInstance *>(dangerousObject);
|
auto armedObj = dynamic_cast<const CArmedInstance *>(dangerousObject);
|
||||||
if(armedObj)
|
if(armedObj)
|
||||||
{
|
{
|
||||||
float tacticalAdvantage = fh->getTacticalAdvantage(visitor, armedObj);
|
float tacticalAdvantage = fh->tacticalAdvantageEngine.getTacticalAdvantage(visitor, armedObj);
|
||||||
objectDanger *= tacticalAdvantage; //this line tends to go infinite for allied towns (?)
|
objectDanger *= tacticalAdvantage; //this line tends to go infinite for allied towns (?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,7 @@ ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor)
|
|||||||
auto guards = cb->getGuardingCreatures(it->second->visitablePos());
|
auto guards = cb->getGuardingCreatures(it->second->visitablePos());
|
||||||
for(auto cre : guards)
|
for(auto cre : guards)
|
||||||
{
|
{
|
||||||
vstd::amax(guardDanger, evaluateDanger(cre) * fh->getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre)));
|
vstd::amax(guardDanger, evaluateDanger(cre) * fh->tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -267,7 +267,7 @@ ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor)
|
|||||||
auto guards = cb->getGuardingCreatures(tile);
|
auto guards = cb->getGuardingCreatures(tile);
|
||||||
for(auto cre : guards)
|
for(auto cre : guards)
|
||||||
{
|
{
|
||||||
vstd::amax(guardDanger, evaluateDanger(cre) * fh->getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre))); //we are interested in strongest monster around
|
vstd::amax(guardDanger, evaluateDanger(cre) * fh->tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance *>(cre))); //we are interested in strongest monster around
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO mozna odwiedzic blockvis nie ruszajac straznika
|
//TODO mozna odwiedzic blockvis nie ruszajac straznika
|
||||||
|
@@ -15,7 +15,8 @@ set(VCAI_SRCS
|
|||||||
SectorMap.cpp
|
SectorMap.cpp
|
||||||
BuildingManager.cpp
|
BuildingManager.cpp
|
||||||
MapObjectsEvaluator.cpp
|
MapObjectsEvaluator.cpp
|
||||||
Fuzzy.cpp
|
FuzzyEngines.cpp
|
||||||
|
FuzzyHelper.cpp
|
||||||
Goals.cpp
|
Goals.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
VCAI.cpp
|
VCAI.cpp
|
||||||
@@ -31,7 +32,8 @@ set(VCAI_HEADERS
|
|||||||
SectorMap.h
|
SectorMap.h
|
||||||
BuildingManager.h
|
BuildingManager.h
|
||||||
MapObjectsEvaluator.h
|
MapObjectsEvaluator.h
|
||||||
Fuzzy.h
|
FuzzyEngines.h
|
||||||
|
FuzzyHelper.h
|
||||||
Goals.h
|
Goals.h
|
||||||
VCAI.h
|
VCAI.h
|
||||||
)
|
)
|
||||||
|
@@ -1,643 +0,0 @@
|
|||||||
/*
|
|
||||||
* Fuzzy.cpp, part of VCMI engine
|
|
||||||
*
|
|
||||||
* Authors: listed in file AUTHORS in main folder
|
|
||||||
*
|
|
||||||
* License: GNU General Public License v2.0 or later
|
|
||||||
* Full text of license available in license.txt file, in main folder
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#include "StdInc.h"
|
|
||||||
#include "Fuzzy.h"
|
|
||||||
#include <limits>
|
|
||||||
|
|
||||||
#include "../../lib/mapObjects/MapObjects.h"
|
|
||||||
#include "../../lib/mapObjects/CommonConstructors.h"
|
|
||||||
#include "../../lib/CCreatureHandler.h"
|
|
||||||
#include "../../lib/CPathfinder.h"
|
|
||||||
#include "../../lib/CGameStateFwd.h"
|
|
||||||
#include "../../lib/VCMI_Lib.h"
|
|
||||||
#include "../../CCallback.h"
|
|
||||||
#include "VCAI.h"
|
|
||||||
#include "MapObjectsEvaluator.h"
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
||||||
struct BankConfig;
|
|
||||||
class CBankInfo;
|
|
||||||
class Engine;
|
|
||||||
class InputVariable;
|
|
||||||
class CGTownInstance;
|
|
||||||
|
|
||||||
FuzzyHelper * fh;
|
|
||||||
|
|
||||||
extern boost::thread_specific_ptr<CCallback> cb;
|
|
||||||
extern boost::thread_specific_ptr<VCAI> ai;
|
|
||||||
|
|
||||||
engineBase::engineBase()
|
|
||||||
{
|
|
||||||
engine.addRuleBlock(&rules);
|
|
||||||
}
|
|
||||||
|
|
||||||
void engineBase::configure()
|
|
||||||
{
|
|
||||||
engine.configure("Minimum", "Maximum", "Minimum", "AlgebraicSum", "Centroid", "General");
|
|
||||||
logAi->info(engine.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
void engineBase::addRule(const std::string & txt)
|
|
||||||
{
|
|
||||||
rules.addRule(fl::Rule::parse(txt, &engine));
|
|
||||||
}
|
|
||||||
|
|
||||||
struct armyStructure
|
|
||||||
{
|
|
||||||
float walkers, shooters, flyers;
|
|
||||||
ui32 maxSpeed;
|
|
||||||
};
|
|
||||||
|
|
||||||
armyStructure evaluateArmyStructure(const CArmedInstance * army)
|
|
||||||
{
|
|
||||||
ui64 totalStrenght = army->getArmyStrength();
|
|
||||||
double walkersStrenght = 0;
|
|
||||||
double flyersStrenght = 0;
|
|
||||||
double shootersStrenght = 0;
|
|
||||||
ui32 maxSpeed = 0;
|
|
||||||
|
|
||||||
for(auto s : army->Slots())
|
|
||||||
{
|
|
||||||
bool walker = true;
|
|
||||||
if(s.second->type->hasBonusOfType(Bonus::SHOOTER))
|
|
||||||
{
|
|
||||||
shootersStrenght += s.second->getPower();
|
|
||||||
walker = false;
|
|
||||||
}
|
|
||||||
if(s.second->type->hasBonusOfType(Bonus::FLYING))
|
|
||||||
{
|
|
||||||
flyersStrenght += s.second->getPower();
|
|
||||||
walker = false;
|
|
||||||
}
|
|
||||||
if(walker)
|
|
||||||
walkersStrenght += s.second->getPower();
|
|
||||||
|
|
||||||
vstd::amax(maxSpeed, s.second->type->valOfBonuses(Bonus::STACKS_SPEED));
|
|
||||||
}
|
|
||||||
armyStructure as;
|
|
||||||
as.walkers = walkersStrenght / totalStrenght;
|
|
||||||
as.shooters = shootersStrenght / totalStrenght;
|
|
||||||
as.flyers = flyersStrenght / totalStrenght;
|
|
||||||
as.maxSpeed = maxSpeed;
|
|
||||||
assert(as.walkers || as.flyers || as.shooters);
|
|
||||||
return as;
|
|
||||||
}
|
|
||||||
|
|
||||||
FuzzyHelper::FuzzyHelper()
|
|
||||||
{
|
|
||||||
initTacticalAdvantage();
|
|
||||||
ta.configure();
|
|
||||||
initVisitTile();
|
|
||||||
vt.configure();
|
|
||||||
initWanderTarget();
|
|
||||||
wanderTarget.configure();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void FuzzyHelper::initTacticalAdvantage()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ta.ourShooters = new fl::InputVariable("OurShooters");
|
|
||||||
ta.ourWalkers = new fl::InputVariable("OurWalkers");
|
|
||||||
ta.ourFlyers = new fl::InputVariable("OurFlyers");
|
|
||||||
ta.enemyShooters = new fl::InputVariable("EnemyShooters");
|
|
||||||
ta.enemyWalkers = new fl::InputVariable("EnemyWalkers");
|
|
||||||
ta.enemyFlyers = new fl::InputVariable("EnemyFlyers");
|
|
||||||
|
|
||||||
//Tactical advantage calculation
|
|
||||||
std::vector<fl::InputVariable *> helper =
|
|
||||||
{
|
|
||||||
ta.ourShooters, ta.ourWalkers, ta.ourFlyers, ta.enemyShooters, ta.enemyWalkers, ta.enemyFlyers
|
|
||||||
};
|
|
||||||
|
|
||||||
for(auto val : helper)
|
|
||||||
{
|
|
||||||
ta.engine.addInputVariable(val);
|
|
||||||
val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
|
|
||||||
val->addTerm(new fl::Ramp("MANY", 0.4, 1));
|
|
||||||
val->setRange(0.0, 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
ta.ourSpeed = new fl::InputVariable("OurSpeed");
|
|
||||||
ta.enemySpeed = new fl::InputVariable("EnemySpeed");
|
|
||||||
|
|
||||||
helper = {ta.ourSpeed, ta.enemySpeed};
|
|
||||||
|
|
||||||
for(auto val : helper)
|
|
||||||
{
|
|
||||||
ta.engine.addInputVariable(val);
|
|
||||||
val->addTerm(new fl::Ramp("LOW", 6.5, 3));
|
|
||||||
val->addTerm(new fl::Triangle("MEDIUM", 5.5, 10.5));
|
|
||||||
val->addTerm(new fl::Ramp("HIGH", 8.5, 16));
|
|
||||||
val->setRange(0, 25);
|
|
||||||
}
|
|
||||||
|
|
||||||
ta.castleWalls = new fl::InputVariable("CastleWalls");
|
|
||||||
ta.engine.addInputVariable(ta.castleWalls);
|
|
||||||
{
|
|
||||||
fl::Rectangle * none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f);
|
|
||||||
ta.castleWalls->addTerm(none);
|
|
||||||
|
|
||||||
fl::Trapezoid * medium = new fl::Trapezoid("MEDIUM", (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f, CGTownInstance::FORT,
|
|
||||||
CGTownInstance::CITADEL, CGTownInstance::CITADEL + (CGTownInstance::CASTLE - CGTownInstance::CITADEL) * 0.5f);
|
|
||||||
ta.castleWalls->addTerm(medium);
|
|
||||||
|
|
||||||
fl::Ramp * high = new fl::Ramp("HIGH", CGTownInstance::CITADEL - 0.1, CGTownInstance::CASTLE);
|
|
||||||
ta.castleWalls->addTerm(high);
|
|
||||||
|
|
||||||
ta.castleWalls->setRange(CGTownInstance::NONE, CGTownInstance::CASTLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ta.bankPresent = new fl::InputVariable("Bank");
|
|
||||||
ta.engine.addInputVariable(ta.bankPresent);
|
|
||||||
{
|
|
||||||
fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
|
|
||||||
ta.bankPresent->addTerm(termFalse);
|
|
||||||
fl::Rectangle * termTrue = new fl::Rectangle("TRUE", 0.5f, 1);
|
|
||||||
ta.bankPresent->addTerm(termTrue);
|
|
||||||
ta.bankPresent->setRange(0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
ta.threat = new fl::OutputVariable("Threat");
|
|
||||||
ta.engine.addOutputVariable(ta.threat);
|
|
||||||
ta.threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGHT));
|
|
||||||
ta.threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
|
|
||||||
ta.threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
|
|
||||||
ta.threat->setRange(MIN_AI_STRENGHT, 1.5);
|
|
||||||
|
|
||||||
ta.addRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is LOW");
|
|
||||||
ta.addRule("if OurShooters is MANY and EnemyShooters is FEW then Threat is LOW");
|
|
||||||
ta.addRule("if OurSpeed is LOW and EnemyShooters is MANY then Threat is HIGH");
|
|
||||||
ta.addRule("if OurSpeed is HIGH and EnemyShooters is MANY then Threat is LOW");
|
|
||||||
|
|
||||||
ta.addRule("if OurWalkers is FEW and EnemyShooters is MANY then Threat is somewhat LOW");
|
|
||||||
ta.addRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH");
|
|
||||||
//just to cover all cases
|
|
||||||
ta.addRule("if OurShooters is FEW and EnemySpeed is HIGH then Threat is MEDIUM");
|
|
||||||
ta.addRule("if EnemySpeed is MEDIUM then Threat is MEDIUM");
|
|
||||||
ta.addRule("if EnemySpeed is LOW and OurShooters is FEW then Threat is MEDIUM");
|
|
||||||
|
|
||||||
ta.addRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH");
|
|
||||||
ta.addRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW");
|
|
||||||
|
|
||||||
ta.addRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH");
|
|
||||||
ta.addRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM");
|
|
||||||
ta.addRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW");
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(fl::Exception & pe)
|
|
||||||
{
|
|
||||||
logAi->error("initTacticalAdvantage: %s", pe.getWhat());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const
|
|
||||||
{
|
|
||||||
float turns = 0.0f;
|
|
||||||
float distance = CPathfinderHelper::getMovementCost(h, tile);
|
|
||||||
if(distance)
|
|
||||||
{
|
|
||||||
if(distance < h->movement) //we can move there within one turn
|
|
||||||
turns = (fl::scalar)distance / h->movement;
|
|
||||||
else
|
|
||||||
turns = 1 + (fl::scalar)(distance - h->movement) / h->maxMovePoints(true); //bool on land?
|
|
||||||
}
|
|
||||||
return turns;
|
|
||||||
}
|
|
||||||
|
|
||||||
ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
|
|
||||||
{
|
|
||||||
//this one is not fuzzy anymore, just calculate weighted average
|
|
||||||
|
|
||||||
auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
|
|
||||||
|
|
||||||
CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
|
|
||||||
|
|
||||||
ui64 totalStrength = 0;
|
|
||||||
ui8 totalChance = 0;
|
|
||||||
for(auto config : bankInfo->getPossibleGuards())
|
|
||||||
{
|
|
||||||
totalStrength += config.second.totalStrength * config.first;
|
|
||||||
totalChance += config.first;
|
|
||||||
}
|
|
||||||
return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy)
|
|
||||||
{
|
|
||||||
float output = 1;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
armyStructure ourStructure = evaluateArmyStructure(we);
|
|
||||||
armyStructure enemyStructure = evaluateArmyStructure(enemy);
|
|
||||||
|
|
||||||
ta.ourWalkers->setValue(ourStructure.walkers);
|
|
||||||
ta.ourShooters->setValue(ourStructure.shooters);
|
|
||||||
ta.ourFlyers->setValue(ourStructure.flyers);
|
|
||||||
ta.ourSpeed->setValue(ourStructure.maxSpeed);
|
|
||||||
|
|
||||||
ta.enemyWalkers->setValue(enemyStructure.walkers);
|
|
||||||
ta.enemyShooters->setValue(enemyStructure.shooters);
|
|
||||||
ta.enemyFlyers->setValue(enemyStructure.flyers);
|
|
||||||
ta.enemySpeed->setValue(enemyStructure.maxSpeed);
|
|
||||||
|
|
||||||
bool bank = dynamic_cast<const CBank *>(enemy);
|
|
||||||
if(bank)
|
|
||||||
ta.bankPresent->setValue(1);
|
|
||||||
else
|
|
||||||
ta.bankPresent->setValue(0);
|
|
||||||
|
|
||||||
const CGTownInstance * fort = dynamic_cast<const CGTownInstance *>(enemy);
|
|
||||||
if(fort)
|
|
||||||
ta.castleWalls->setValue(fort->fortLevel());
|
|
||||||
else
|
|
||||||
ta.castleWalls->setValue(0);
|
|
||||||
|
|
||||||
//engine.process(TACTICAL_ADVANTAGE);//TODO: Process only Tactical_Advantage
|
|
||||||
ta.engine.process();
|
|
||||||
output = ta.threat->getValue();
|
|
||||||
}
|
|
||||||
catch(fl::Exception & fe)
|
|
||||||
{
|
|
||||||
logAi->error("getTacticalAdvantage: %s ", fe.getWhat());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(output < 0 || (output != output))
|
|
||||||
{
|
|
||||||
fl::InputVariable * tab[] = {ta.bankPresent, ta.castleWalls, ta.ourWalkers, ta.ourShooters, ta.ourFlyers, ta.ourSpeed, ta.enemyWalkers, ta.enemyShooters, ta.enemyFlyers, ta.enemySpeed};
|
|
||||||
std::string names[] = {"bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" };
|
|
||||||
std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: ");
|
|
||||||
|
|
||||||
for(int i = 0; i < boost::size(tab); i++)
|
|
||||||
log << names[i] << ": " << tab[i]->getValue() << " ";
|
|
||||||
logAi->error(log.str());
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::getWanderTargetObjectValue(const CGHeroInstance & h, const ObjectIdRef & obj)
|
|
||||||
{
|
|
||||||
float distFromObject = calculateTurnDistanceInputValue(&h, obj->pos);
|
|
||||||
boost::optional<int> objValueKnownByAI = MapObjectsEvaluator::getInstance().getObjectValue(obj->ID, obj->subID);
|
|
||||||
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->warn("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));
|
|
||||||
}
|
|
||||||
|
|
||||||
float output = -1.0f;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
wanderTarget.distance->setValue(distFromObject);
|
|
||||||
wanderTarget.objectValue->setValue(objValue);
|
|
||||||
wanderTarget.engine.process();
|
|
||||||
output = wanderTarget.visitGain->getValue();
|
|
||||||
}
|
|
||||||
catch (fl::Exception & fe)
|
|
||||||
{
|
|
||||||
logAi->error("evaluate getWanderTargetObjectValue: %s", fe.getWhat());
|
|
||||||
}
|
|
||||||
assert(output >= 0.0f);
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
FuzzyHelper::TacticalAdvantage::~TacticalAdvantage()
|
|
||||||
{
|
|
||||||
//TODO: smart pointers?
|
|
||||||
delete ourWalkers;
|
|
||||||
delete ourShooters;
|
|
||||||
delete ourFlyers;
|
|
||||||
delete enemyWalkers;
|
|
||||||
delete enemyShooters;
|
|
||||||
delete enemyFlyers;
|
|
||||||
delete ourSpeed;
|
|
||||||
delete enemySpeed;
|
|
||||||
delete bankPresent;
|
|
||||||
delete castleWalls;
|
|
||||||
delete threat;
|
|
||||||
}
|
|
||||||
|
|
||||||
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec)
|
|
||||||
|
|
||||||
Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
|
|
||||||
{
|
|
||||||
if(vec.empty()) //no possibilities found
|
|
||||||
return sptr(Goals::Invalid());
|
|
||||||
|
|
||||||
ai->cachedSectorMaps.clear();
|
|
||||||
|
|
||||||
//a trick to switch between heroes less often - calculatePaths is costly
|
|
||||||
auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
|
||||||
{
|
|
||||||
return lhs->hero.h < rhs->hero.h;
|
|
||||||
};
|
|
||||||
boost::sort(vec, sortByHeroes);
|
|
||||||
|
|
||||||
for(auto g : vec)
|
|
||||||
{
|
|
||||||
setPriority(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
|
||||||
{
|
|
||||||
return lhs->priority < rhs->priority;
|
|
||||||
};
|
|
||||||
return *boost::max_element(vec, compareGoals);
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::evaluate(Goals::Explore & g)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::RecruitHero & g)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
FuzzyHelper::EvalVisitTile::~EvalVisitTile()
|
|
||||||
{
|
|
||||||
delete strengthRatio;
|
|
||||||
delete heroStrength;
|
|
||||||
delete turnDistance;
|
|
||||||
delete missionImportance;
|
|
||||||
delete estimatedReward;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FuzzyHelper::initVisitTile()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
vt.strengthRatio = new fl::InputVariable("strengthRatio"); //hero must be strong enough to defeat guards
|
|
||||||
vt.heroStrength = new fl::InputVariable("heroStrength"); //we want to use weakest possible hero
|
|
||||||
vt.turnDistance = new fl::InputVariable("turnDistance"); //we want to use hero who is near
|
|
||||||
vt.missionImportance = new fl::InputVariable("lockedMissionImportance"); //we may want to preempt hero with low-priority mission
|
|
||||||
vt.estimatedReward = new fl::InputVariable("estimatedReward"); //indicate AI that content of the file is important or it is probably bad
|
|
||||||
vt.value = new fl::OutputVariable("Value");
|
|
||||||
vt.value->setMinimum(0);
|
|
||||||
vt.value->setMaximum(5);
|
|
||||||
|
|
||||||
std::vector<fl::InputVariable *> helper = {vt.strengthRatio, vt.heroStrength, vt.turnDistance, vt.missionImportance, vt.estimatedReward};
|
|
||||||
for(auto val : helper)
|
|
||||||
{
|
|
||||||
vt.engine.addInputVariable(val);
|
|
||||||
}
|
|
||||||
vt.engine.addOutputVariable(vt.value);
|
|
||||||
|
|
||||||
vt.strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0));
|
|
||||||
vt.strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3));
|
|
||||||
vt.strengthRatio->setRange(0, SAFE_ATTACK_CONSTANT * 3);
|
|
||||||
|
|
||||||
//strength compared to our main hero
|
|
||||||
vt.heroStrength->addTerm(new fl::Ramp("LOW", 0.2, 0));
|
|
||||||
vt.heroStrength->addTerm(new fl::Triangle("MEDIUM", 0.2, 0.8));
|
|
||||||
vt.heroStrength->addTerm(new fl::Ramp("HIGH", 0.5, 1));
|
|
||||||
vt.heroStrength->setRange(0.0, 1.0);
|
|
||||||
|
|
||||||
vt.turnDistance->addTerm(new fl::Ramp("SMALL", 0.5, 0));
|
|
||||||
vt.turnDistance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
|
|
||||||
vt.turnDistance->addTerm(new fl::Ramp("LONG", 0.5, 3));
|
|
||||||
vt.turnDistance->setRange(0.0, 3.0);
|
|
||||||
|
|
||||||
vt.missionImportance->addTerm(new fl::Ramp("LOW", 2.5, 0));
|
|
||||||
vt.missionImportance->addTerm(new fl::Triangle("MEDIUM", 2, 3));
|
|
||||||
vt.missionImportance->addTerm(new fl::Ramp("HIGH", 2.5, 5));
|
|
||||||
vt.missionImportance->setRange(0.0, 5.0);
|
|
||||||
|
|
||||||
vt.estimatedReward->addTerm(new fl::Ramp("LOW", 2.5, 0));
|
|
||||||
vt.estimatedReward->addTerm(new fl::Ramp("HIGH", 2.5, 5));
|
|
||||||
vt.estimatedReward->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
|
|
||||||
vt.value->addTerm(new fl::Ramp("LOW", 2.5, 0));
|
|
||||||
vt.value->addTerm(new fl::Triangle("MEDIUM", 2, 3)); //can't be center of mass :/
|
|
||||||
vt.value->addTerm(new fl::Ramp("HIGH", 2.5, 5));
|
|
||||||
vt.value->setRange(0.0, 5.0);
|
|
||||||
|
|
||||||
//use unarmed scouts if possible
|
|
||||||
vt.addRule("if strengthRatio is HIGH and heroStrength is LOW then Value is very HIGH");
|
|
||||||
//we may want to use secondary hero(es) rather than main hero
|
|
||||||
vt.addRule("if strengthRatio is HIGH and heroStrength is MEDIUM then Value is somewhat HIGH");
|
|
||||||
vt.addRule("if strengthRatio is HIGH and heroStrength is HIGH then Value is somewhat 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)
|
|
||||||
vt.addRule("if strengthRatio is LOW and heroStrength is LOW then Value is very LOW");
|
|
||||||
//attempt to arm secondary heroes is not stupid
|
|
||||||
vt.addRule("if strengthRatio is LOW and heroStrength is MEDIUM then Value is somewhat HIGH");
|
|
||||||
vt.addRule("if strengthRatio is LOW and heroStrength is HIGH then Value is LOW");
|
|
||||||
|
|
||||||
//do not cancel important goals
|
|
||||||
vt.addRule("if lockedMissionImportance is HIGH then Value is very LOW");
|
|
||||||
vt.addRule("if lockedMissionImportance is MEDIUM then Value is somewhat LOW");
|
|
||||||
vt.addRule("if lockedMissionImportance is LOW then Value is HIGH");
|
|
||||||
//pick nearby objects if it's easy, avoid long walks
|
|
||||||
vt.addRule("if turnDistance is SMALL then Value is HIGH");
|
|
||||||
vt.addRule("if turnDistance is MEDIUM then Value is MEDIUM");
|
|
||||||
vt.addRule("if turnDistance is LONG then Value is LOW");
|
|
||||||
//some goals are more rewarding by definition f.e. capturing town is more important than collecting resource - experimental
|
|
||||||
vt.addRule("if estimatedReward is HIGH then Value is very HIGH");
|
|
||||||
vt.addRule("if estimatedReward is LOW then Value is somewhat LOW");
|
|
||||||
}
|
|
||||||
catch(fl::Exception & fe)
|
|
||||||
{
|
|
||||||
logAi->error("visitTile: %s", fe.getWhat());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FuzzyHelper::initWanderTarget()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
wanderTarget.distance = new fl::InputVariable("distance"); //distance on map from object
|
|
||||||
wanderTarget.objectValue = new fl::InputVariable("objectValue"); //value of that object type known by AI
|
|
||||||
wanderTarget.visitGain = new fl::OutputVariable("visitGain");
|
|
||||||
wanderTarget.visitGain->setMinimum(0);
|
|
||||||
wanderTarget.visitGain->setMaximum(10);
|
|
||||||
|
|
||||||
wanderTarget.engine.addInputVariable(wanderTarget.distance);
|
|
||||||
wanderTarget.engine.addInputVariable(wanderTarget.objectValue);
|
|
||||||
wanderTarget.engine.addOutputVariable(wanderTarget.visitGain);
|
|
||||||
|
|
||||||
//for now distance variable same as in as VisitTile
|
|
||||||
wanderTarget.distance->addTerm(new fl::Ramp("SHORT", 0.5, 0));
|
|
||||||
wanderTarget.distance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
|
|
||||||
wanderTarget.distance->addTerm(new fl::Ramp("LONG", 0.5, 3));
|
|
||||||
wanderTarget.distance->setRange(0, 3.0);
|
|
||||||
|
|
||||||
//objectValue ranges are based on checking RMG priorities of some objects and trying to guess sane value ranges
|
|
||||||
wanderTarget.objectValue->addTerm(new fl::Ramp("LOW", 3000, 0)); //I have feeling that concave shape might work well instead of ramp for objectValue FL terms
|
|
||||||
wanderTarget.objectValue->addTerm(new fl::Triangle("MEDIUM", 2500, 6000));
|
|
||||||
wanderTarget.objectValue->addTerm(new fl::Ramp("HIGH", 5000, 20000));
|
|
||||||
wanderTarget.objectValue->setRange(0, 20000); //relic artifact value is border value by design, even better things are scaled down.
|
|
||||||
|
|
||||||
wanderTarget.visitGain->addTerm(new fl::Ramp("LOW", 5, 0));
|
|
||||||
wanderTarget.visitGain->addTerm(new fl::Triangle("MEDIUM", 4, 6));
|
|
||||||
wanderTarget.visitGain->addTerm(new fl::Ramp("HIGH", 5, 10));
|
|
||||||
wanderTarget.visitGain->setRange(0, 10);
|
|
||||||
|
|
||||||
wanderTarget.addRule("if distance is LONG and objectValue is HIGH then visitGain is MEDIUM");
|
|
||||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is HIGH then visitGain is somewhat HIGH");
|
|
||||||
wanderTarget.addRule("if distance is SHORT and objectValue is HIGH then visitGain is HIGH");
|
|
||||||
|
|
||||||
wanderTarget.addRule("if distance is LONG and objectValue is MEDIUM then visitGain is somewhat LOW");
|
|
||||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is MEDIUM then visitGain is MEDIUM");
|
|
||||||
wanderTarget.addRule("if distance is SHORT and objectValue is MEDIUM then visitGain is somewhat HIGH");
|
|
||||||
|
|
||||||
wanderTarget.addRule("if distance is LONG and objectValue is LOW then visitGain is very LOW");
|
|
||||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is LOW then visitGain is LOW");
|
|
||||||
wanderTarget.addRule("if distance is SHORT and objectValue is LOW then visitGain is MEDIUM");
|
|
||||||
}
|
|
||||||
catch(fl::Exception & fe)
|
|
||||||
{
|
|
||||||
logAi->error("FindWanderTarget: %s", fe.getWhat());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::evaluate(Goals::VisitTile & g)
|
|
||||||
{
|
|
||||||
//we assume that hero is already set and we want to choose most suitable one for the mission
|
|
||||||
if(!g.hero)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
//assert(cb->isInTheMap(g.tile));
|
|
||||||
float turns = calculateTurnDistanceInputValue(g.hero.h, g.tile);
|
|
||||||
float missionImportance = 0;
|
|
||||||
if(vstd::contains(ai->lockedHeroes, g.hero))
|
|
||||||
missionImportance = ai->lockedHeroes[g.hero]->priority;
|
|
||||||
|
|
||||||
float strengthRatio = 10.0f; //we are much stronger than enemy
|
|
||||||
ui64 danger = evaluateDanger(g.tile, g.hero.h);
|
|
||||||
if(danger)
|
|
||||||
strengthRatio = (fl::scalar)g.hero.h->getTotalStrength() / danger;
|
|
||||||
|
|
||||||
float tilePriority = 0;
|
|
||||||
if(g.objid == -1)
|
|
||||||
{
|
|
||||||
vt.estimatedReward->setEnabled(false);
|
|
||||||
}
|
|
||||||
else if(g.objid == Obj::TOWN) //TODO: move to getObj eventually and add appropiate logic there
|
|
||||||
{
|
|
||||||
vt.estimatedReward->setEnabled(true);
|
|
||||||
tilePriority = 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
vt.strengthRatio->setValue(strengthRatio);
|
|
||||||
vt.heroStrength->setValue((fl::scalar)g.hero->getTotalStrength() / ai->primaryHero()->getTotalStrength());
|
|
||||||
vt.turnDistance->setValue(turns);
|
|
||||||
vt.missionImportance->setValue(missionImportance);
|
|
||||||
vt.estimatedReward->setValue(tilePriority);
|
|
||||||
|
|
||||||
vt.engine.process();
|
|
||||||
//engine.process(VISIT_TILE); //TODO: Process only Visit_Tile
|
|
||||||
g.priority = vt.value->getValue();
|
|
||||||
}
|
|
||||||
catch(fl::Exception & fe)
|
|
||||||
{
|
|
||||||
logAi->error("evaluate VisitTile: %s", fe.getWhat());
|
|
||||||
}
|
|
||||||
assert(g.priority >= 0);
|
|
||||||
return g.priority;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::VisitHero & g)
|
|
||||||
{
|
|
||||||
auto obj = cb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
|
|
||||||
if(!obj)
|
|
||||||
return -100; //hero died in the meantime
|
|
||||||
//TODO: consider direct copy (constructor?)
|
|
||||||
g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
|
||||||
return g.priority;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::GatherArmy & g)
|
|
||||||
{
|
|
||||||
//the more army we need, the more important goal
|
|
||||||
//the more army we lack, the less important goal
|
|
||||||
float army = g.hero->getArmyStrength();
|
|
||||||
float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
|
|
||||||
return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
|
|
||||||
{
|
|
||||||
if(!g.hero.h)
|
|
||||||
throw cannotFulfillGoalException("ClearWayTo called without hero!");
|
|
||||||
|
|
||||||
int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
|
|
||||||
|
|
||||||
if(t.valid())
|
|
||||||
{
|
|
||||||
if(isSafeToVisit(g.hero, t))
|
|
||||||
{
|
|
||||||
g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
|
|
||||||
sethero(g.hero).setisAbstract(true).accept(this));
|
|
||||||
}
|
|
||||||
return g.priority;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
float FuzzyHelper::evaluate(Goals::BuildThis & g)
|
|
||||||
{
|
|
||||||
return g.priority; //TODO
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::DigAtTile & g)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::CollectRes & g)
|
|
||||||
{
|
|
||||||
return g.priority; //handled by ResourceManager
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::Build & g)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::BuyArmy & g)
|
|
||||||
{
|
|
||||||
return g.priority;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::Invalid & g)
|
|
||||||
{
|
|
||||||
return -1e10;
|
|
||||||
}
|
|
||||||
float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
|
|
||||||
{
|
|
||||||
logAi->warn("Cannot evaluate goal %s", g.name());
|
|
||||||
return g.priority;
|
|
||||||
}
|
|
||||||
void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
|
|
||||||
{
|
|
||||||
g->setpriority(g->accept(this)); //this enforces returned value is set
|
|
||||||
}
|
|
||||||
|
|
||||||
FuzzyHelper::EvalWanderTargetObject::~EvalWanderTargetObject()
|
|
||||||
{
|
|
||||||
delete distance;
|
|
||||||
delete objectValue;
|
|
||||||
delete visitGain;
|
|
||||||
}
|
|
100
AI/VCAI/Fuzzy.h
100
AI/VCAI/Fuzzy.h
@@ -1,100 +0,0 @@
|
|||||||
/*
|
|
||||||
* Fuzzy.h, part of VCMI engine
|
|
||||||
*
|
|
||||||
* Authors: listed in file AUTHORS in main folder
|
|
||||||
*
|
|
||||||
* License: GNU General Public License v2.0 or later
|
|
||||||
* Full text of license available in license.txt file, in main folder
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
#include "fl/Headers.h"
|
|
||||||
#include "Goals.h"
|
|
||||||
|
|
||||||
class VCAI;
|
|
||||||
class CArmedInstance;
|
|
||||||
class CBank;
|
|
||||||
struct SectorMap;
|
|
||||||
|
|
||||||
class engineBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
fl::Engine engine;
|
|
||||||
fl::RuleBlock rules;
|
|
||||||
|
|
||||||
engineBase();
|
|
||||||
void configure();
|
|
||||||
void addRule(const std::string & txt);
|
|
||||||
};
|
|
||||||
|
|
||||||
class FuzzyHelper
|
|
||||||
{
|
|
||||||
friend class VCAI;
|
|
||||||
|
|
||||||
class TacticalAdvantage : public engineBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
fl::InputVariable * ourWalkers, * ourShooters, * ourFlyers, * enemyWalkers, * enemyShooters, * enemyFlyers;
|
|
||||||
fl::InputVariable * ourSpeed, * enemySpeed;
|
|
||||||
fl::InputVariable * bankPresent;
|
|
||||||
fl::InputVariable * castleWalls;
|
|
||||||
fl::OutputVariable * threat;
|
|
||||||
~TacticalAdvantage();
|
|
||||||
} ta;
|
|
||||||
|
|
||||||
class EvalVisitTile : public engineBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
fl::InputVariable * strengthRatio;
|
|
||||||
fl::InputVariable * heroStrength;
|
|
||||||
fl::InputVariable * turnDistance;
|
|
||||||
fl::InputVariable * missionImportance;
|
|
||||||
fl::InputVariable * estimatedReward;
|
|
||||||
fl::OutputVariable * value;
|
|
||||||
fl::RuleBlock rules;
|
|
||||||
~EvalVisitTile();
|
|
||||||
} vt;
|
|
||||||
|
|
||||||
class EvalWanderTargetObject : public engineBase //designed for use with VCAI::wander()
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
fl::InputVariable * distance;
|
|
||||||
fl::InputVariable * objectValue;
|
|
||||||
fl::OutputVariable * visitGain;
|
|
||||||
~EvalWanderTargetObject();
|
|
||||||
} wanderTarget;
|
|
||||||
|
|
||||||
private:
|
|
||||||
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
|
|
||||||
|
|
||||||
public:
|
|
||||||
enum RuleBlocks {BANK_DANGER, TACTICAL_ADVANTAGE, VISIT_TILE};
|
|
||||||
//blocks should be initialized in this order, which may be confusing :/
|
|
||||||
|
|
||||||
FuzzyHelper();
|
|
||||||
void initTacticalAdvantage();
|
|
||||||
void initVisitTile();
|
|
||||||
void initWanderTarget();
|
|
||||||
|
|
||||||
float evaluate(Goals::Explore & g);
|
|
||||||
float evaluate(Goals::RecruitHero & g);
|
|
||||||
float evaluate(Goals::VisitTile & g);
|
|
||||||
float evaluate(Goals::VisitHero & g);
|
|
||||||
float evaluate(Goals::BuildThis & g);
|
|
||||||
float evaluate(Goals::DigAtTile & g);
|
|
||||||
float evaluate(Goals::CollectRes & g);
|
|
||||||
float evaluate(Goals::Build & g);
|
|
||||||
float evaluate(Goals::BuyArmy & g);
|
|
||||||
float evaluate(Goals::GatherArmy & g);
|
|
||||||
float evaluate(Goals::ClearWayTo & g);
|
|
||||||
float evaluate(Goals::Invalid & g);
|
|
||||||
float evaluate(Goals::AbstractGoal & g);
|
|
||||||
void setPriority(Goals::TSubgoal & g);
|
|
||||||
|
|
||||||
ui64 estimateBankDanger(const CBank * bank);
|
|
||||||
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
|
|
||||||
float getWanderTargetObjectValue(const CGHeroInstance & h, const ObjectIdRef & obj);
|
|
||||||
|
|
||||||
Goals::TSubgoal chooseSolution(Goals::TGoalVec vec);
|
|
||||||
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec);
|
|
||||||
};
|
|
434
AI/VCAI/FuzzyEngines.cpp
Normal file
434
AI/VCAI/FuzzyEngines.cpp
Normal file
@@ -0,0 +1,434 @@
|
|||||||
|
/*
|
||||||
|
* FuzzyEngines.cpp, part of VCMI engine
|
||||||
|
*
|
||||||
|
* Authors: listed in file AUTHORS in main folder
|
||||||
|
*
|
||||||
|
* License: GNU General Public License v2.0 or later
|
||||||
|
* Full text of license available in license.txt file, in main folder
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "StdInc.h"
|
||||||
|
#include "FuzzyEngines.h"
|
||||||
|
|
||||||
|
#include "../../lib/mapObjects/MapObjects.h"
|
||||||
|
#include "VCAI.h"
|
||||||
|
#include "MapObjectsEvaluator.h"
|
||||||
|
|
||||||
|
#define MIN_AI_STRENGTH (0.5f) //lower when combat AI gets smarter
|
||||||
|
#define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
|
||||||
|
|
||||||
|
extern boost::thread_specific_ptr<VCAI> ai;
|
||||||
|
|
||||||
|
engineBase::engineBase()
|
||||||
|
{
|
||||||
|
engine.addRuleBlock(&rules);
|
||||||
|
}
|
||||||
|
|
||||||
|
void engineBase::configure()
|
||||||
|
{
|
||||||
|
engine.configure("Minimum", "Maximum", "Minimum", "AlgebraicSum", "Centroid", "General");
|
||||||
|
logAi->info(engine.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void engineBase::addRule(const std::string & txt)
|
||||||
|
{
|
||||||
|
rules.addRule(fl::Rule::parse(txt, &engine));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct armyStructure
|
||||||
|
{
|
||||||
|
float walkers, shooters, flyers;
|
||||||
|
ui32 maxSpeed;
|
||||||
|
};
|
||||||
|
|
||||||
|
armyStructure evaluateArmyStructure(const CArmedInstance * army)
|
||||||
|
{
|
||||||
|
ui64 totalStrenght = army->getArmyStrength();
|
||||||
|
double walkersStrenght = 0;
|
||||||
|
double flyersStrenght = 0;
|
||||||
|
double shootersStrenght = 0;
|
||||||
|
ui32 maxSpeed = 0;
|
||||||
|
|
||||||
|
for(auto s : army->Slots())
|
||||||
|
{
|
||||||
|
bool walker = true;
|
||||||
|
if(s.second->type->hasBonusOfType(Bonus::SHOOTER))
|
||||||
|
{
|
||||||
|
shootersStrenght += s.second->getPower();
|
||||||
|
walker = false;
|
||||||
|
}
|
||||||
|
if(s.second->type->hasBonusOfType(Bonus::FLYING))
|
||||||
|
{
|
||||||
|
flyersStrenght += s.second->getPower();
|
||||||
|
walker = false;
|
||||||
|
}
|
||||||
|
if(walker)
|
||||||
|
walkersStrenght += s.second->getPower();
|
||||||
|
|
||||||
|
vstd::amax(maxSpeed, s.second->type->valOfBonuses(Bonus::STACKS_SPEED));
|
||||||
|
}
|
||||||
|
armyStructure as;
|
||||||
|
as.walkers = walkersStrenght / totalStrenght;
|
||||||
|
as.shooters = shootersStrenght / totalStrenght;
|
||||||
|
as.flyers = flyersStrenght / totalStrenght;
|
||||||
|
as.maxSpeed = maxSpeed;
|
||||||
|
assert(as.walkers || as.flyers || as.shooters);
|
||||||
|
return as;
|
||||||
|
}
|
||||||
|
|
||||||
|
float HeroMovementGoalEngineBase::calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const
|
||||||
|
{
|
||||||
|
float turns = 0.0f;
|
||||||
|
float distance = CPathfinderHelper::getMovementCost(h, tile);
|
||||||
|
if(distance)
|
||||||
|
{
|
||||||
|
if(distance < h->movement) //we can move there within one turn
|
||||||
|
turns = (fl::scalar)distance / h->movement;
|
||||||
|
else
|
||||||
|
turns = 1 + (fl::scalar)(distance - h->movement) / h->maxMovePoints(true); //bool on land?
|
||||||
|
}
|
||||||
|
return turns;
|
||||||
|
}
|
||||||
|
|
||||||
|
TacticalAdvantageEngine::TacticalAdvantageEngine()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ourShooters = new fl::InputVariable("OurShooters");
|
||||||
|
ourWalkers = new fl::InputVariable("OurWalkers");
|
||||||
|
ourFlyers = new fl::InputVariable("OurFlyers");
|
||||||
|
enemyShooters = new fl::InputVariable("EnemyShooters");
|
||||||
|
enemyWalkers = new fl::InputVariable("EnemyWalkers");
|
||||||
|
enemyFlyers = new fl::InputVariable("EnemyFlyers");
|
||||||
|
|
||||||
|
//Tactical advantage calculation
|
||||||
|
std::vector<fl::InputVariable *> helper =
|
||||||
|
{
|
||||||
|
ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers
|
||||||
|
};
|
||||||
|
|
||||||
|
for(auto val : helper)
|
||||||
|
{
|
||||||
|
engine.addInputVariable(val);
|
||||||
|
val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
|
||||||
|
val->addTerm(new fl::Ramp("MANY", 0.4, 1));
|
||||||
|
val->setRange(0.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ourSpeed = new fl::InputVariable("OurSpeed");
|
||||||
|
enemySpeed = new fl::InputVariable("EnemySpeed");
|
||||||
|
|
||||||
|
helper = { ourSpeed, enemySpeed };
|
||||||
|
|
||||||
|
for(auto val : helper)
|
||||||
|
{
|
||||||
|
engine.addInputVariable(val);
|
||||||
|
val->addTerm(new fl::Ramp("LOW", 6.5, 3));
|
||||||
|
val->addTerm(new fl::Triangle("MEDIUM", 5.5, 10.5));
|
||||||
|
val->addTerm(new fl::Ramp("HIGH", 8.5, 16));
|
||||||
|
val->setRange(0, 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
castleWalls = new fl::InputVariable("CastleWalls");
|
||||||
|
engine.addInputVariable(castleWalls);
|
||||||
|
{
|
||||||
|
fl::Rectangle * none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f);
|
||||||
|
castleWalls->addTerm(none);
|
||||||
|
|
||||||
|
fl::Trapezoid * medium = new fl::Trapezoid("MEDIUM", (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f, CGTownInstance::FORT,
|
||||||
|
CGTownInstance::CITADEL, CGTownInstance::CITADEL + (CGTownInstance::CASTLE - CGTownInstance::CITADEL) * 0.5f);
|
||||||
|
castleWalls->addTerm(medium);
|
||||||
|
|
||||||
|
fl::Ramp * high = new fl::Ramp("HIGH", CGTownInstance::CITADEL - 0.1, CGTownInstance::CASTLE);
|
||||||
|
castleWalls->addTerm(high);
|
||||||
|
|
||||||
|
castleWalls->setRange(CGTownInstance::NONE, CGTownInstance::CASTLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bankPresent = new fl::InputVariable("Bank");
|
||||||
|
engine.addInputVariable(bankPresent);
|
||||||
|
{
|
||||||
|
fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
|
||||||
|
bankPresent->addTerm(termFalse);
|
||||||
|
fl::Rectangle * termTrue = new fl::Rectangle("TRUE", 0.5f, 1);
|
||||||
|
bankPresent->addTerm(termTrue);
|
||||||
|
bankPresent->setRange(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
threat = new fl::OutputVariable("Threat");
|
||||||
|
engine.addOutputVariable(threat);
|
||||||
|
threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGTH));
|
||||||
|
threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
|
||||||
|
threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
|
||||||
|
threat->setRange(MIN_AI_STRENGTH, 1.5);
|
||||||
|
|
||||||
|
addRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is LOW");
|
||||||
|
addRule("if OurShooters is MANY and EnemyShooters is FEW then Threat is LOW");
|
||||||
|
addRule("if OurSpeed is LOW and EnemyShooters is MANY then Threat is HIGH");
|
||||||
|
addRule("if OurSpeed is HIGH and EnemyShooters is MANY then Threat is LOW");
|
||||||
|
|
||||||
|
addRule("if OurWalkers is FEW and EnemyShooters is MANY then Threat is somewhat LOW");
|
||||||
|
addRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH");
|
||||||
|
//just to cover all cases
|
||||||
|
addRule("if OurShooters is FEW and EnemySpeed is HIGH then Threat is MEDIUM");
|
||||||
|
addRule("if EnemySpeed is MEDIUM then Threat is MEDIUM");
|
||||||
|
addRule("if EnemySpeed is LOW and OurShooters is FEW then Threat is MEDIUM");
|
||||||
|
|
||||||
|
addRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH");
|
||||||
|
addRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW");
|
||||||
|
|
||||||
|
addRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH");
|
||||||
|
addRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM");
|
||||||
|
addRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(fl::Exception & pe)
|
||||||
|
{
|
||||||
|
logAi->error("initTacticalAdvantage: %s", pe.getWhat());
|
||||||
|
}
|
||||||
|
configure();
|
||||||
|
}
|
||||||
|
|
||||||
|
float TacticalAdvantageEngine::getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy)
|
||||||
|
{
|
||||||
|
float output = 1;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
armyStructure ourStructure = evaluateArmyStructure(we);
|
||||||
|
armyStructure enemyStructure = evaluateArmyStructure(enemy);
|
||||||
|
|
||||||
|
ourWalkers->setValue(ourStructure.walkers);
|
||||||
|
ourShooters->setValue(ourStructure.shooters);
|
||||||
|
ourFlyers->setValue(ourStructure.flyers);
|
||||||
|
ourSpeed->setValue(ourStructure.maxSpeed);
|
||||||
|
|
||||||
|
enemyWalkers->setValue(enemyStructure.walkers);
|
||||||
|
enemyShooters->setValue(enemyStructure.shooters);
|
||||||
|
enemyFlyers->setValue(enemyStructure.flyers);
|
||||||
|
enemySpeed->setValue(enemyStructure.maxSpeed);
|
||||||
|
|
||||||
|
bool bank = dynamic_cast<const CBank *>(enemy);
|
||||||
|
if(bank)
|
||||||
|
bankPresent->setValue(1);
|
||||||
|
else
|
||||||
|
bankPresent->setValue(0);
|
||||||
|
|
||||||
|
const CGTownInstance * fort = dynamic_cast<const CGTownInstance *>(enemy);
|
||||||
|
if(fort)
|
||||||
|
castleWalls->setValue(fort->fortLevel());
|
||||||
|
else
|
||||||
|
castleWalls->setValue(0);
|
||||||
|
|
||||||
|
engine.process();
|
||||||
|
output = threat->getValue();
|
||||||
|
}
|
||||||
|
catch(fl::Exception & fe)
|
||||||
|
{
|
||||||
|
logAi->error("getTacticalAdvantage: %s ", fe.getWhat());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(output < 0 || (output != output))
|
||||||
|
{
|
||||||
|
fl::InputVariable * tab[] = { bankPresent, castleWalls, ourWalkers, ourShooters, ourFlyers, ourSpeed, enemyWalkers, enemyShooters, enemyFlyers, enemySpeed };
|
||||||
|
std::string names[] = { "bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" };
|
||||||
|
std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: ");
|
||||||
|
|
||||||
|
for(int i = 0; i < boost::size(tab); i++)
|
||||||
|
log << names[i] << ": " << tab[i]->getValue() << " ";
|
||||||
|
logAi->error(log.str());
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
heroStrength = new fl::InputVariable("heroStrength"); //we want to use weakest possible hero
|
||||||
|
turnDistance = new fl::InputVariable("turnDistance"); //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, heroStrength, turnDistance, 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
|
||||||
|
heroStrength->addTerm(new fl::Ramp("LOW", 0.2, 0));
|
||||||
|
heroStrength->addTerm(new fl::Triangle("MEDIUM", 0.2, 0.8));
|
||||||
|
heroStrength->addTerm(new fl::Ramp("HIGH", 0.5, 1));
|
||||||
|
heroStrength->setRange(0.0, 1.0);
|
||||||
|
|
||||||
|
turnDistance->addTerm(new fl::Ramp("SHORT", 0.5, 0));
|
||||||
|
turnDistance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
|
||||||
|
turnDistance->addTerm(new fl::Ramp("LONG", 0.5, 3));
|
||||||
|
turnDistance->setRange(0.0, 3.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 heroStrength is LOW then Value is very HIGH");
|
||||||
|
//we may want to use secondary hero(es) rather than main hero
|
||||||
|
addRule("if strengthRatio is HIGH and heroStrength is MEDIUM then Value is somewhat HIGH");
|
||||||
|
addRule("if strengthRatio is HIGH and heroStrength is HIGH then Value is somewhat 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 heroStrength is LOW then Value is very LOW");
|
||||||
|
//attempt to arm secondary heroes is not stupid
|
||||||
|
addRule("if strengthRatio is LOW and heroStrength is MEDIUM then Value is somewhat HIGH");
|
||||||
|
addRule("if strengthRatio is LOW and heroStrength is HIGH then Value is LOW");
|
||||||
|
|
||||||
|
//do not cancel important goals
|
||||||
|
addRule("if lockedMissionImportance is HIGH then Value is very LOW");
|
||||||
|
addRule("if lockedMissionImportance is MEDIUM then Value is somewhat LOW");
|
||||||
|
addRule("if lockedMissionImportance is LOW then Value is HIGH");
|
||||||
|
//pick nearby objects if it's easy, avoid long walks
|
||||||
|
addRule("if turnDistance is SHORT then Value is HIGH");
|
||||||
|
addRule("if turnDistance is MEDIUM then Value is MEDIUM");
|
||||||
|
addRule("if turnDistance 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.hero.h, goal.tile);
|
||||||
|
float missionImportanceData = 0;
|
||||||
|
if(vstd::contains(ai->lockedHeroes, goal.hero))
|
||||||
|
missionImportanceData = ai->lockedHeroes[goal.hero]->priority;
|
||||||
|
|
||||||
|
float strengthRatioData = 10.0f; //we are much stronger than enemy
|
||||||
|
ui64 danger = evaluateDanger(goal.tile, goal.hero.h);
|
||||||
|
if(danger)
|
||||||
|
strengthRatioData = (fl::scalar)goal.hero.h->getTotalStrength() / danger;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
strengthRatio->setValue(strengthRatioData);
|
||||||
|
heroStrength->setValue((fl::scalar)goal.hero->getTotalStrength() / ai->primaryHero()->getTotalStrength());
|
||||||
|
turnDistance->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 trying to guess sane value ranges
|
||||||
|
objectValue->addTerm(new fl::Ramp("LOW", 3000, 0)); //I have feeling that concave shape might work well instead of ramp for objectValue FL terms
|
||||||
|
objectValue->addTerm(new fl::Triangle("MEDIUM", 2500, 6000));
|
||||||
|
objectValue->addTerm(new fl::Ramp("HIGH", 5000, 20000));
|
||||||
|
objectValue->setRange(0, 20000); //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));
|
||||||
|
|
||||||
|
|
||||||
|
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->warn("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;
|
||||||
|
}
|
72
AI/VCAI/FuzzyEngines.h
Normal file
72
AI/VCAI/FuzzyEngines.h
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* FuzzyEngines.h, part of VCMI engine
|
||||||
|
*
|
||||||
|
* Authors: listed in file AUTHORS in main folder
|
||||||
|
*
|
||||||
|
* License: GNU General Public License v2.0 or later
|
||||||
|
* Full text of license available in license.txt file, in main folder
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "fl/Headers.h"
|
||||||
|
#include "Goals.h"
|
||||||
|
|
||||||
|
class CArmedInstance;
|
||||||
|
|
||||||
|
class engineBase //subclasses create fuzzylite variables with "new" that are not freed - this is desired as fl::Engine wants to destroy these...
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
fl::Engine engine;
|
||||||
|
fl::RuleBlock rules;
|
||||||
|
virtual void configure();
|
||||||
|
void addRule(const std::string & txt);
|
||||||
|
public:
|
||||||
|
engineBase();
|
||||||
|
};
|
||||||
|
|
||||||
|
class TacticalAdvantageEngine : public engineBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TacticalAdvantageEngine();
|
||||||
|
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
|
||||||
|
private:
|
||||||
|
fl::InputVariable * ourWalkers, *ourShooters, *ourFlyers, *enemyWalkers, *enemyShooters, *enemyFlyers;
|
||||||
|
fl::InputVariable * ourSpeed, *enemySpeed;
|
||||||
|
fl::InputVariable * bankPresent;
|
||||||
|
fl::InputVariable * castleWalls;
|
||||||
|
fl::OutputVariable * threat;
|
||||||
|
};
|
||||||
|
|
||||||
|
class HeroMovementGoalEngineBase : public engineBase //in future - maybe derive from some (GoalEngineBase : public engineBase) class for handling non-movement goals with common utility for goal engines
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HeroMovementGoalEngineBase();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void setSharedFuzzyVariables(Goals::AbstractGoal & goal);
|
||||||
|
|
||||||
|
fl::InputVariable * strengthRatio;
|
||||||
|
fl::InputVariable * heroStrength;
|
||||||
|
fl::InputVariable * turnDistance;
|
||||||
|
fl::InputVariable * missionImportance;
|
||||||
|
fl::OutputVariable * value;
|
||||||
|
|
||||||
|
private:
|
||||||
|
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class VisitTileEngine : public HeroMovementGoalEngineBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VisitTileEngine();
|
||||||
|
float evaluate(Goals::VisitTile & goal);
|
||||||
|
};
|
||||||
|
|
||||||
|
class VisitObjEngine : public HeroMovementGoalEngineBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VisitObjEngine();
|
||||||
|
float evaluate(Goals::VisitObj & goal);
|
||||||
|
protected:
|
||||||
|
fl::InputVariable * objectValue;
|
||||||
|
};
|
156
AI/VCAI/FuzzyHelper.cpp
Normal file
156
AI/VCAI/FuzzyHelper.cpp
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* FuzzyHelper.cpp, part of VCMI engine
|
||||||
|
*
|
||||||
|
* Authors: listed in file AUTHORS in main folder
|
||||||
|
*
|
||||||
|
* License: GNU General Public License v2.0 or later
|
||||||
|
* Full text of license available in license.txt file, in main folder
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "StdInc.h"
|
||||||
|
#include "FuzzyHelper.h"
|
||||||
|
|
||||||
|
#include "../../lib/mapObjects/CommonConstructors.h"
|
||||||
|
#include "VCAI.h"
|
||||||
|
|
||||||
|
FuzzyHelper * fh;
|
||||||
|
|
||||||
|
extern boost::thread_specific_ptr<VCAI> ai;
|
||||||
|
|
||||||
|
Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
|
||||||
|
{
|
||||||
|
if(vec.empty()) //no possibilities found
|
||||||
|
return sptr(Goals::Invalid());
|
||||||
|
|
||||||
|
ai->cachedSectorMaps.clear();
|
||||||
|
|
||||||
|
//a trick to switch between heroes less often - calculatePaths is costly
|
||||||
|
auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
||||||
|
{
|
||||||
|
return lhs->hero.h < rhs->hero.h;
|
||||||
|
};
|
||||||
|
boost::sort(vec, sortByHeroes);
|
||||||
|
|
||||||
|
for(auto g : vec)
|
||||||
|
{
|
||||||
|
setPriority(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
||||||
|
{
|
||||||
|
return lhs->priority < rhs->priority;
|
||||||
|
};
|
||||||
|
return *boost::max_element(vec, compareGoals);
|
||||||
|
}
|
||||||
|
|
||||||
|
ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
|
||||||
|
{
|
||||||
|
//this one is not fuzzy anymore, just calculate weighted average
|
||||||
|
|
||||||
|
auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
|
||||||
|
|
||||||
|
CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
|
||||||
|
|
||||||
|
ui64 totalStrength = 0;
|
||||||
|
ui8 totalChance = 0;
|
||||||
|
for(auto config : bankInfo->getPossibleGuards())
|
||||||
|
{
|
||||||
|
totalStrength += config.second.totalStrength * config.first;
|
||||||
|
totalChance += config.first;
|
||||||
|
}
|
||||||
|
return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float FuzzyHelper::evaluate(Goals::VisitTile & g)
|
||||||
|
{
|
||||||
|
return visitTileEngine.evaluate(g);
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::VisitObj & g)
|
||||||
|
{
|
||||||
|
return visitObjEngine.evaluate(g);
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::VisitHero & g)
|
||||||
|
{
|
||||||
|
auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
|
||||||
|
if(!obj)
|
||||||
|
return -100; //hero died in the meantime
|
||||||
|
//TODO: consider direct copy (constructor?)
|
||||||
|
g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
||||||
|
return g.priority;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::GatherArmy & g)
|
||||||
|
{
|
||||||
|
//the more army we need, the more important goal
|
||||||
|
//the more army we lack, the less important goal
|
||||||
|
float army = g.hero->getArmyStrength();
|
||||||
|
float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
|
||||||
|
return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
|
||||||
|
}
|
||||||
|
|
||||||
|
float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
|
||||||
|
{
|
||||||
|
if (!g.hero.h)
|
||||||
|
return 0; //lowest priority
|
||||||
|
|
||||||
|
int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
|
||||||
|
|
||||||
|
if(t.valid())
|
||||||
|
{
|
||||||
|
if(isSafeToVisit(g.hero, t))
|
||||||
|
{
|
||||||
|
g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
|
||||||
|
sethero(g.hero).setisAbstract(true).accept(this));
|
||||||
|
}
|
||||||
|
return g.priority;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float FuzzyHelper::evaluate(Goals::BuildThis & g)
|
||||||
|
{
|
||||||
|
return g.priority; //TODO
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::DigAtTile & g)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::CollectRes & g)
|
||||||
|
{
|
||||||
|
return g.priority; //handled by ResourceManager
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::Build & g)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::BuyArmy & g)
|
||||||
|
{
|
||||||
|
return g.priority;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::Explore & g)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::RecruitHero & g)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::Invalid & g)
|
||||||
|
{
|
||||||
|
return -1e10;
|
||||||
|
}
|
||||||
|
float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
|
||||||
|
{
|
||||||
|
logAi->warn("Cannot evaluate goal %s", g.name());
|
||||||
|
return g.priority;
|
||||||
|
}
|
||||||
|
void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
|
||||||
|
{
|
||||||
|
g->setpriority(g->accept(this)); //this enforces returned value is set
|
||||||
|
}
|
42
AI/VCAI/FuzzyHelper.h
Normal file
42
AI/VCAI/FuzzyHelper.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* FuzzyHelper.h, part of VCMI engine
|
||||||
|
*
|
||||||
|
* Authors: listed in file AUTHORS in main folder
|
||||||
|
*
|
||||||
|
* License: GNU General Public License v2.0 or later
|
||||||
|
* Full text of license available in license.txt file, in main folder
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "FuzzyEngines.h"
|
||||||
|
|
||||||
|
class CBank;
|
||||||
|
|
||||||
|
class FuzzyHelper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TacticalAdvantageEngine tacticalAdvantageEngine;
|
||||||
|
VisitTileEngine visitTileEngine;
|
||||||
|
VisitObjEngine visitObjEngine;
|
||||||
|
|
||||||
|
float evaluate(Goals::Explore & g);
|
||||||
|
float evaluate(Goals::RecruitHero & g);
|
||||||
|
float evaluate(Goals::VisitTile & g);
|
||||||
|
float evaluate(Goals::VisitObj & g);
|
||||||
|
float evaluate(Goals::VisitHero & g);
|
||||||
|
float evaluate(Goals::BuildThis & g);
|
||||||
|
float evaluate(Goals::DigAtTile & g);
|
||||||
|
float evaluate(Goals::CollectRes & g);
|
||||||
|
float evaluate(Goals::Build & g);
|
||||||
|
float evaluate(Goals::BuyArmy & g);
|
||||||
|
float evaluate(Goals::GatherArmy & g);
|
||||||
|
float evaluate(Goals::ClearWayTo & g);
|
||||||
|
float evaluate(Goals::Invalid & g);
|
||||||
|
float evaluate(Goals::AbstractGoal & g);
|
||||||
|
void setPriority(Goals::TSubgoal & g);
|
||||||
|
|
||||||
|
ui64 estimateBankDanger(const CBank * bank); //TODO: move to another class?
|
||||||
|
|
||||||
|
Goals::TSubgoal chooseSolution(Goals::TGoalVec vec);
|
||||||
|
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec);
|
||||||
|
};
|
@@ -10,7 +10,7 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "Goals.h"
|
#include "Goals.h"
|
||||||
#include "VCAI.h"
|
#include "VCAI.h"
|
||||||
#include "Fuzzy.h"
|
#include "FuzzyHelper.h"
|
||||||
#include "ResourceManager.h"
|
#include "ResourceManager.h"
|
||||||
#include "BuildingManager.h"
|
#include "BuildingManager.h"
|
||||||
#include "../../lib/mapping/CMap.h" //for victory conditions
|
#include "../../lib/mapping/CMap.h" //for victory conditions
|
||||||
@@ -77,7 +77,7 @@ std::string Goals::AbstractGoal::name() const //TODO: virtualize
|
|||||||
case GATHER_TROOPS:
|
case GATHER_TROOPS:
|
||||||
desc = "GATHER TROOPS";
|
desc = "GATHER TROOPS";
|
||||||
break;
|
break;
|
||||||
case GET_OBJ:
|
case VISIT_OBJ:
|
||||||
{
|
{
|
||||||
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
|
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
|
||||||
if(obj)
|
if(obj)
|
||||||
@@ -153,7 +153,7 @@ bool Goals::AbstractGoal::operator==(AbstractGoal & g)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
//assigned hero and object
|
//assigned hero and object
|
||||||
case GET_OBJ:
|
case VISIT_OBJ:
|
||||||
case FIND_OBJ: //TODO: use subtype?
|
case FIND_OBJ: //TODO: use subtype?
|
||||||
case VISIT_HERO:
|
case VISIT_HERO:
|
||||||
case GET_ART_TYPE:
|
case GET_ART_TYPE:
|
||||||
@@ -350,7 +350,7 @@ TSubgoal Win::whatToDoToAchieve()
|
|||||||
return sptr(Goals::Conquer());
|
return sptr(Goals::Conquer());
|
||||||
|
|
||||||
|
|
||||||
return sptr(Goals::GetObj(goal.object->id.getNum()));
|
return sptr(Goals::VisitObj(goal.object->id.getNum()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -404,7 +404,7 @@ TSubgoal Win::whatToDoToAchieve()
|
|||||||
return sptr(Goals::DigAtTile(grailPos));
|
return sptr(Goals::DigAtTile(grailPos));
|
||||||
} //TODO: use FIND_OBJ
|
} //TODO: use FIND_OBJ
|
||||||
else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
|
else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
|
||||||
return sptr(Goals::GetObj(obj->id.getNum()));
|
return sptr(Goals::VisitObj(obj->id.getNum()));
|
||||||
else
|
else
|
||||||
return sptr(Goals::Explore());
|
return sptr(Goals::Explore());
|
||||||
}
|
}
|
||||||
@@ -414,7 +414,7 @@ TSubgoal Win::whatToDoToAchieve()
|
|||||||
{
|
{
|
||||||
if(goal.object)
|
if(goal.object)
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(goal.object->id.getNum()));
|
return sptr(Goals::VisitObj(goal.object->id.getNum()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -489,7 +489,7 @@ TSubgoal FindObj::whatToDoToAchieve()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(o && ai->isAccessible(o->pos)) //we don't use isAccessibleForHero as we don't know which hero it is
|
if(o && ai->isAccessible(o->pos)) //we don't use isAccessibleForHero as we don't know which hero it is
|
||||||
return sptr(Goals::GetObj(o->id.getNum()));
|
return sptr(Goals::VisitObj(o->id.getNum()));
|
||||||
else
|
else
|
||||||
return sptr(Goals::Explore());
|
return sptr(Goals::Explore());
|
||||||
}
|
}
|
||||||
@@ -507,50 +507,86 @@ bool Goals::FindObj::fulfillsMe(TSubgoal goal)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetObj::completeMessage() const
|
std::string VisitObj::completeMessage() const
|
||||||
{
|
{
|
||||||
return "hero " + hero.get()->name + " captured Object ID = " + boost::lexical_cast<std::string>(objid);
|
return "hero " + hero.get()->name + " captured Object ID = " + boost::lexical_cast<std::string>(objid);
|
||||||
}
|
}
|
||||||
|
|
||||||
TSubgoal GetObj::whatToDoToAchieve()
|
TGoalVec VisitObj::getAllPossibleSubgoals()
|
||||||
{
|
{
|
||||||
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
|
TGoalVec goalList;
|
||||||
|
const CGObjectInstance * obj = cb->getObjInstance(ObjectInstanceID(objid));
|
||||||
if(!obj)
|
if(!obj)
|
||||||
return sptr(Goals::Explore());
|
{
|
||||||
if(obj->tempOwner == ai->playerID) //we can't capture our own object -> move to Win codition
|
throw cannotFulfillGoalException("Object is missing - goal is invalid now!");
|
||||||
throw cannotFulfillGoalException("Cannot capture my own object " + obj->getObjectName());
|
}
|
||||||
|
|
||||||
int3 pos = obj->visitablePos();
|
int3 pos = obj->visitablePos();
|
||||||
if(hero)
|
if(hero)
|
||||||
{
|
{
|
||||||
if(ai->isAccessibleForHero(pos, hero))
|
if(ai->isAccessibleForHero(pos, hero))
|
||||||
return sptr(Goals::VisitTile(pos).sethero(hero));
|
{
|
||||||
|
if(isSafeToVisit(hero, pos))
|
||||||
|
goalList.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero)));
|
||||||
|
else
|
||||||
|
goalList.push_back(sptr(Goals::GatherArmy(evaluateDanger(pos, hero.h) * SAFE_ATTACK_CONSTANT).sethero(hero).setisAbstract(true)));
|
||||||
|
|
||||||
|
return goalList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for(auto h : cb->getHeroesInfo())
|
for(auto potentialVisitor : cb->getHeroesInfo())
|
||||||
{
|
{
|
||||||
if(ai->isAccessibleForHero(pos, h))
|
if(ai->isAccessibleForHero(pos, potentialVisitor))
|
||||||
return sptr(Goals::VisitTile(pos).sethero(h)); //we must visit object with same hero, if any
|
{
|
||||||
|
if(isSafeToVisit(potentialVisitor, pos))
|
||||||
|
goalList.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(potentialVisitor)));
|
||||||
|
else
|
||||||
|
goalList.push_back(sptr(Goals::GatherArmy(evaluateDanger(pos, potentialVisitor) * SAFE_ATTACK_CONSTANT).sethero(potentialVisitor).setisAbstract(true)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!goalList.empty())
|
||||||
|
{
|
||||||
|
return goalList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sptr(Goals::ClearWayTo(pos).sethero(hero));
|
|
||||||
|
goalList.push_back(sptr(Goals::ClearWayTo(pos)));
|
||||||
|
return goalList;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Goals::GetObj::operator==(AbstractGoal & g)
|
TSubgoal VisitObj::whatToDoToAchieve()
|
||||||
|
{
|
||||||
|
auto bestGoal = fh->chooseSolution(getAllPossibleSubgoals());
|
||||||
|
|
||||||
|
if(bestGoal->goalType == Goals::VISIT_OBJ && bestGoal->hero)
|
||||||
|
bestGoal->setisElementar(true);
|
||||||
|
|
||||||
|
return bestGoal;
|
||||||
|
}
|
||||||
|
|
||||||
|
Goals::VisitObj::VisitObj(int Objid) : CGoal(Goals::VISIT_OBJ)
|
||||||
|
{
|
||||||
|
objid = Objid;
|
||||||
|
tile = ai->myCb->getObjInstance(ObjectInstanceID(objid))->visitablePos();
|
||||||
|
priority = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Goals::VisitObj::operator==(AbstractGoal & g)
|
||||||
{
|
{
|
||||||
if (g.goalType != goalType)
|
if (g.goalType != goalType)
|
||||||
return false;
|
return false;
|
||||||
return g.objid == objid;
|
return g.objid == objid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetObj::fulfillsMe(TSubgoal goal)
|
bool VisitObj::fulfillsMe(TSubgoal goal)
|
||||||
{
|
{
|
||||||
if(goal->goalType == Goals::VISIT_TILE) //visiting tile visits object at same time
|
if(goal->goalType == Goals::VISIT_TILE)
|
||||||
{
|
{
|
||||||
if (!hero || hero == goal->hero)
|
if (!hero || hero == goal->hero)
|
||||||
{
|
{
|
||||||
auto obj = cb->getObj(ObjectInstanceID(objid));
|
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
|
||||||
if (obj && obj->visitablePos() == goal->tile) //object could be removed
|
if (obj && obj->visitablePos() == goal->tile) //object could be removed
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -695,7 +731,7 @@ TGoalVec ClearWayTo::getAllPossibleSubgoals()
|
|||||||
if(shouldVisit(h, topObj))
|
if(shouldVisit(h, topObj))
|
||||||
{
|
{
|
||||||
//do NOT use VISIT_TILE, as tile with quets guard can't be visited
|
//do NOT use VISIT_TILE, as tile with quets guard can't be visited
|
||||||
ret.push_back(sptr(Goals::GetObj(topObj->id.getNum()).sethero(h)));
|
ret.push_back(sptr(Goals::VisitObj(topObj->id.getNum()).sethero(h))); //TODO: Recheck this code - object visit became elementar goal
|
||||||
continue; //do not try to visit tile or gather army
|
continue; //do not try to visit tile or gather army
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -912,8 +948,6 @@ TSubgoal VisitTile::whatToDoToAchieve()
|
|||||||
{
|
{
|
||||||
if(isSafeToVisit(ret->hero, tile) && ai->isAccessibleForHero(tile, ret->hero))
|
if(isSafeToVisit(ret->hero, tile) && ai->isAccessibleForHero(tile, ret->hero))
|
||||||
{
|
{
|
||||||
if(cb->getTile(tile)->topVisitableId().num == Obj::TOWN) //if target is town, fuzzy system will use additional "estimatedReward" variable to increase priority a bit
|
|
||||||
ret->objid = Obj::TOWN; //TODO: move to getObj eventually and add appropiate logic there
|
|
||||||
ret->setisElementar(true);
|
ret->setisElementar(true);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -1103,22 +1137,10 @@ TGoalVec Goals::CollectRes::getAllPossibleSubgoals()
|
|||||||
}
|
}
|
||||||
for (auto obj : ourObjs)
|
for (auto obj : ourObjs)
|
||||||
{
|
{
|
||||||
int3 dest = obj->visitablePos();
|
if (ai->isAccessibleForHero(obj->visitablePos(), h))
|
||||||
auto t = sm->firstTileToGet(h, dest); //we assume that no more than one tile on the way is guarded
|
|
||||||
if (t.valid()) //we know any path at all
|
|
||||||
{
|
{
|
||||||
if (ai->isTileNotReserved(h, t)) //no other hero wants to conquer that tile
|
//further decomposition and evaluation will be handled by VisitObj
|
||||||
{
|
ret.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(h).setisAbstract(true)));
|
||||||
if (isSafeToVisit(h, dest))
|
|
||||||
{
|
|
||||||
if (dest != t) //there is something blocking our way
|
|
||||||
ret.push_back(sptr(Goals::ClearWayTo(dest, h).setisAbstract(true)));
|
|
||||||
else
|
|
||||||
ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setisAbstract(true)));
|
|
||||||
}
|
|
||||||
else //we need to get army in order to pick that object
|
|
||||||
ret.push_back(sptr(Goals::GatherArmy(evaluateDanger(dest, h) * SAFE_ATTACK_CONSTANT).sethero(h).setisAbstract(true)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1200,7 +1222,7 @@ TSubgoal Goals::CollectRes::whatToDoToTrade()
|
|||||||
auto objid = m->o->id.getNum();
|
auto objid = m->o->id.getNum();
|
||||||
if (backObj->tempOwner != ai->playerID) //top object not owned
|
if (backObj->tempOwner != ai->playerID) //top object not owned
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(objid)); //just go there
|
return sptr(Goals::VisitObj(objid)); //just go there
|
||||||
}
|
}
|
||||||
else //either it's our town, or we have hero there
|
else //either it's our town, or we have hero there
|
||||||
{
|
{
|
||||||
@@ -1306,7 +1328,7 @@ TSubgoal GatherTroops::whatToDoToAchieve()
|
|||||||
if(!nearest)
|
if(!nearest)
|
||||||
throw cannotFulfillGoalException("Cannot find nearest dwelling!");
|
throw cannotFulfillGoalException("Cannot find nearest dwelling!");
|
||||||
|
|
||||||
return sptr(Goals::GetObj(nearest->id.getNum()));
|
return sptr(Goals::VisitObj(nearest->id.getNum()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return sptr(Goals::Explore());
|
return sptr(Goals::Explore());
|
||||||
@@ -1386,13 +1408,9 @@ TGoalVec Conquer::getAllPossibleSubgoals()
|
|||||||
{
|
{
|
||||||
ret.push_back(sptr(Goals::VisitHero(obj->id.getNum()).sethero(h).setisAbstract(true)));
|
ret.push_back(sptr(Goals::VisitHero(obj->id.getNum()).sethero(h).setisAbstract(true)));
|
||||||
}
|
}
|
||||||
else //just visit that tile
|
else //just get that object
|
||||||
{
|
{
|
||||||
if(obj->ID.num == Obj::TOWN)
|
ret.push_back(sptr(Goals::VisitObj(obj->id.getNum()).sethero(h).setisAbstract(true)));
|
||||||
//if target is town, fuzzy system will use additional "estimatedReward" variable to increase priority a bit
|
|
||||||
ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setobjid(obj->ID.num).setisAbstract(true))); //TODO: change to getObj eventually and and move appropiate logic there
|
|
||||||
else
|
|
||||||
ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setisAbstract(true)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1612,7 +1630,7 @@ TGoalVec GatherArmy::getAllPossibleSubgoals()
|
|||||||
TSubgoal AbstractGoal::goVisitOrLookFor(const CGObjectInstance * obj)
|
TSubgoal AbstractGoal::goVisitOrLookFor(const CGObjectInstance * obj)
|
||||||
{
|
{
|
||||||
if(obj)
|
if(obj)
|
||||||
return sptr(Goals::GetObj(obj->id.getNum()));
|
return sptr(Goals::VisitObj(obj->id.getNum()));
|
||||||
else
|
else
|
||||||
return sptr(Goals::Explore());
|
return sptr(Goals::Explore());
|
||||||
}
|
}
|
||||||
|
@@ -46,7 +46,7 @@ enum EGoals
|
|||||||
GATHER_TROOPS, // val of creatures with objid
|
GATHER_TROOPS, // val of creatures with objid
|
||||||
|
|
||||||
OBJECT_GOALS_BEGIN,
|
OBJECT_GOALS_BEGIN,
|
||||||
GET_OBJ, //visit or defeat or collect the object
|
VISIT_OBJ, //visit or defeat or collect the object
|
||||||
FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
|
FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
|
||||||
VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
|
VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
|
||||||
|
|
||||||
@@ -461,21 +461,13 @@ public:
|
|||||||
bool fulfillsMe(TSubgoal goal) override;
|
bool fulfillsMe(TSubgoal goal) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DLL_EXPORT GetObj : public CGoal<GetObj>
|
class DLL_EXPORT VisitObj : public CGoal<VisitObj> //this goal was previously known as GetObj
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GetObj() {} // empty constructor not allowed
|
VisitObj() = delete; // empty constructor not allowed
|
||||||
|
VisitObj(int Objid);
|
||||||
|
|
||||||
GetObj(int Objid)
|
TGoalVec getAllPossibleSubgoals() override;
|
||||||
: CGoal(Goals::GET_OBJ)
|
|
||||||
{
|
|
||||||
objid = Objid;
|
|
||||||
priority = 3;
|
|
||||||
}
|
|
||||||
TGoalVec getAllPossibleSubgoals() override
|
|
||||||
{
|
|
||||||
return TGoalVec();
|
|
||||||
}
|
|
||||||
TSubgoal whatToDoToAchieve() override;
|
TSubgoal whatToDoToAchieve() override;
|
||||||
bool operator==(AbstractGoal & g) override;
|
bool operator==(AbstractGoal & g) override;
|
||||||
bool fulfillsMe(TSubgoal goal) override;
|
bool fulfillsMe(TSubgoal goal) override;
|
||||||
|
@@ -2,6 +2,9 @@
|
|||||||
#include "MapObjectsEvaluator.h"
|
#include "MapObjectsEvaluator.h"
|
||||||
#include "../../lib/GameConstants.h"
|
#include "../../lib/GameConstants.h"
|
||||||
#include "../../lib/VCMI_Lib.h"
|
#include "../../lib/VCMI_Lib.h"
|
||||||
|
#include "../../lib/CCreatureHandler.h"
|
||||||
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
|
|
||||||
MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
|
MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
|
||||||
{
|
{
|
||||||
@@ -29,9 +32,9 @@ MapObjectsEvaluator::MapObjectsEvaluator()
|
|||||||
{
|
{
|
||||||
objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
|
objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
|
||||||
}
|
}
|
||||||
else
|
else //some default handling when aiValue not found
|
||||||
{
|
{
|
||||||
objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0; //some default handling when aiValue not found
|
objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,6 +52,26 @@ boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int seco
|
|||||||
return boost::optional<int>();
|
return boost::optional<int>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boost::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance * obj) const
|
||||||
|
{
|
||||||
|
if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
|
||||||
|
{
|
||||||
|
auto dwelling = dynamic_cast<const CGDwelling *>(obj);
|
||||||
|
int aiValue = 0;
|
||||||
|
for(auto & creLevel : dwelling->creatures)
|
||||||
|
{
|
||||||
|
for(auto & creatureID : creLevel.second)
|
||||||
|
{
|
||||||
|
auto creature = VLC->creh->creatures[creatureID];
|
||||||
|
aiValue += (creature->AIValue * creature->growth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return aiValue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return getObjectValue(obj->ID, obj->subID);
|
||||||
|
}
|
||||||
|
|
||||||
void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
|
void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
|
||||||
{
|
{
|
||||||
CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
|
CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
|
||||||
|
@@ -19,6 +19,7 @@ public:
|
|||||||
MapObjectsEvaluator();
|
MapObjectsEvaluator();
|
||||||
static MapObjectsEvaluator & getInstance();
|
static MapObjectsEvaluator & getInstance();
|
||||||
boost::optional<int> getObjectValue(int primaryID, int secondaryID) const;
|
boost::optional<int> getObjectValue(int primaryID, int secondaryID) const;
|
||||||
|
boost::optional<int> getObjectValue(const CGObjectInstance * obj) const;
|
||||||
void addObjectData(int primaryID, int secondaryID, int value);
|
void addObjectData(int primaryID, int secondaryID, int value);
|
||||||
void removeObjectData(int primaryID, int secondaryID);
|
void removeObjectData(int primaryID, int secondaryID);
|
||||||
};
|
};
|
||||||
|
106
AI/VCAI/VCAI.cpp
106
AI/VCAI/VCAI.cpp
@@ -9,7 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "VCAI.h"
|
#include "VCAI.h"
|
||||||
#include "Fuzzy.h"
|
#include "FuzzyHelper.h"
|
||||||
#include "ResourceManager.h"
|
#include "ResourceManager.h"
|
||||||
#include "BuildingManager.h"
|
#include "BuildingManager.h"
|
||||||
|
|
||||||
@@ -268,7 +268,7 @@ void VCAI::heroVisit(const CGHeroInstance * visitor, const CGObjectInstance * vi
|
|||||||
{
|
{
|
||||||
markObjectVisited(visitedObj);
|
markObjectVisited(visitedObj);
|
||||||
unreserveObject(visitor, visitedObj);
|
unreserveObject(visitor, visitedObj);
|
||||||
completeGoal(sptr(Goals::GetObj(visitedObj->id.getNum()).sethero(visitor))); //we don't need to visit it anymore
|
completeGoal(sptr(Goals::VisitObj(visitedObj->id.getNum()).sethero(visitor))); //we don't need to visit it anymore
|
||||||
//TODO: what if we visited one-time visitable object that was reserved by another hero (shouldn't, but..)
|
//TODO: what if we visited one-time visitable object that was reserved by another hero (shouldn't, but..)
|
||||||
if (visitedObj->ID == Obj::HERO)
|
if (visitedObj->ID == Obj::HERO)
|
||||||
{
|
{
|
||||||
@@ -416,6 +416,38 @@ void VCAI::objectRemoved(const CGObjectInstance * obj)
|
|||||||
for(auto h : cb->getHeroesInfo())
|
for(auto h : cb->getHeroesInfo())
|
||||||
unreserveObject(h, obj);
|
unreserveObject(h, obj);
|
||||||
|
|
||||||
|
|
||||||
|
vstd::erase_if(lockedHeroes, [&](const std::pair<HeroPtr, Goals::TSubgoal> & x) -> bool
|
||||||
|
{
|
||||||
|
if((x.second->goalType == Goals::VISIT_OBJ) && (x.second->objid == obj->id.getNum()))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
vstd::erase_if(ultimateGoalsFromBasic, [&](const std::pair<Goals::TSubgoal, Goals::TGoalVec> & x) -> bool
|
||||||
|
{
|
||||||
|
if((x.first->goalType == Goals::VISIT_OBJ) && (x.first->objid == obj->id.getNum()))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
auto goalErasePredicate = [&](const Goals::TSubgoal & x) ->bool
|
||||||
|
{
|
||||||
|
if((x->goalType == Goals::VISIT_OBJ) && (x->objid == obj->id.getNum()))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
vstd::erase_if(basicGoals, goalErasePredicate);
|
||||||
|
vstd::erase_if(goalsToAdd, goalErasePredicate);
|
||||||
|
vstd::erase_if(goalsToRemove, goalErasePredicate);
|
||||||
|
|
||||||
|
for(auto goal : ultimateGoalsFromBasic)
|
||||||
|
vstd::erase_if(goal.second, goalErasePredicate);
|
||||||
|
|
||||||
//TODO: Find better way to handle hero boat removal
|
//TODO: Find better way to handle hero boat removal
|
||||||
if(auto hero = dynamic_cast<const CGHeroInstance *>(obj))
|
if(auto hero = dynamic_cast<const CGHeroInstance *>(obj))
|
||||||
{
|
{
|
||||||
@@ -1015,7 +1047,7 @@ void VCAI::performObjectInteraction(const CGObjectInstance * obj, HeroPtr h)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
completeGoal(sptr(Goals::GetObj(obj->id.getNum()).sethero(h)));
|
completeGoal(sptr(Goals::VisitObj(obj->id.getNum()).sethero(h)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void VCAI::moveCreaturesToHero(const CGTownInstance * t)
|
void VCAI::moveCreaturesToHero(const CGTownInstance * t)
|
||||||
@@ -1324,7 +1356,6 @@ bool VCAI::canRecruitAnyHero(const CGTownInstance * t) const
|
|||||||
|
|
||||||
void VCAI::wander(HeroPtr h)
|
void VCAI::wander(HeroPtr h)
|
||||||
{
|
{
|
||||||
|
|
||||||
auto visitTownIfAny = [this](HeroPtr h) -> bool
|
auto visitTownIfAny = [this](HeroPtr h) -> bool
|
||||||
{
|
{
|
||||||
if (h->visitedTown)
|
if (h->visitedTown)
|
||||||
@@ -1333,6 +1364,7 @@ void VCAI::wander(HeroPtr h)
|
|||||||
buildArmyIn(h->visitedTown);
|
buildArmyIn(h->visitedTown);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
//unclaim objects that are now dangerous for us
|
//unclaim objects that are now dangerous for us
|
||||||
@@ -1462,30 +1494,26 @@ void VCAI::wander(HeroPtr h)
|
|||||||
//end of objs empty
|
//end of objs empty
|
||||||
|
|
||||||
if(dests.size()) //performance improvement
|
if(dests.size()) //performance improvement
|
||||||
{
|
{
|
||||||
auto fuzzyLogicSorter = [h](const ObjectIdRef & l, const ObjectIdRef & r) -> bool //TODO: create elementar GetObj goal usable for goal decomposition and Wander based on VisitTile logic and object value on top of it
|
Goals::TGoalVec targetObjectGoals;
|
||||||
|
for(auto destination : dests)
|
||||||
{
|
{
|
||||||
return fh->getWanderTargetObjectValue( *h.get(), l) < fh->getWanderTargetObjectValue(*h.get(), r);
|
targetObjectGoals.push_back(sptr(Goals::VisitObj(destination.id.getNum()).sethero(h).setisAbstract(true)));
|
||||||
};
|
}
|
||||||
|
auto bestObjectGoal = fh->chooseSolution(targetObjectGoals);
|
||||||
const ObjectIdRef & dest = *boost::max_element(dests, fuzzyLogicSorter); //find best object to visit based on fuzzy logic evaluation, TODO: use elementar version of GetObj here in future
|
decomposeGoal(bestObjectGoal)->accept(this);
|
||||||
|
|
||||||
//wander should not cause heroes to be reserved - they are always considered free
|
//wander should not cause heroes to be reserved - they are always considered free
|
||||||
logAi->debug("Of all %d destinations, object oid=%d seems nice", dests.size(), dest.id.getNum());
|
if(bestObjectGoal->goalType == Goals::VISIT_OBJ)
|
||||||
if (!goVisitObj(dest, h))
|
|
||||||
{
|
{
|
||||||
if (!dest)
|
auto chosenObject = cb->getObjInstance(ObjectInstanceID(bestObjectGoal->objid));
|
||||||
{
|
if(chosenObject != nullptr)
|
||||||
logAi->debug("Visit attempt made the object (id=%d) gone...", dest.id.getNum());
|
logAi->debug("Of all %d destinations, object %s at pos=%s seems nice", dests.size(), chosenObject->getObjectName(), chosenObject->pos.toString());
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logAi->debug("Hero %s apparently used all MPs (%d left)", h->name, h->movement);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else //we reached our destination
|
else
|
||||||
visitTownIfAny(h);
|
logAi->debug("Trying to realize goal of type %d as part of wandering.", bestObjectGoal->goalType);
|
||||||
|
|
||||||
|
visitTownIfAny(h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
visitTownIfAny(h); //in case hero is just sitting in town
|
visitTownIfAny(h); //in case hero is just sitting in town
|
||||||
@@ -2008,6 +2036,22 @@ void VCAI::tryRealize(Goals::VisitTile & g)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VCAI::tryRealize(Goals::VisitObj & g)
|
||||||
|
{
|
||||||
|
auto position = g.tile;
|
||||||
|
if(!g.hero->movement)
|
||||||
|
throw cannotFulfillGoalException("Cannot visit object: hero is out of MPs!");
|
||||||
|
if(position == g.hero->visitablePos() && cb->getVisitableObjs(g.hero->visitablePos()).size() < 2)
|
||||||
|
{
|
||||||
|
logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", g.hero->name, g.tile.toString());
|
||||||
|
throw goalFulfilledException(sptr(g));
|
||||||
|
}
|
||||||
|
if(ai->moveHeroToTile(position, g.hero.get()))
|
||||||
|
{
|
||||||
|
throw goalFulfilledException(sptr(g));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void VCAI::tryRealize(Goals::VisitHero & g)
|
void VCAI::tryRealize(Goals::VisitHero & g)
|
||||||
{
|
{
|
||||||
if(!g.hero->movement)
|
if(!g.hero->movement)
|
||||||
@@ -2364,7 +2408,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(hero))
|
if (q.quest->checkQuest(hero))
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()).sethero(hero));
|
return sptr(Goals::VisitObj(q.obj->id.getNum()).sethero(hero));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto art : q.quest->m5arts)
|
for (auto art : q.quest->m5arts)
|
||||||
@@ -2380,7 +2424,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(hero))
|
if (q.quest->checkQuest(hero))
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()).sethero(hero));
|
return sptr(Goals::VisitObj(q.obj->id.getNum()).sethero(hero));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sptr(Goals::FindObj(Obj::PRISON)); //rule of a thumb - quest heroes usually are locked in prisons
|
return sptr(Goals::FindObj(Obj::PRISON)); //rule of a thumb - quest heroes usually are locked in prisons
|
||||||
@@ -2393,7 +2437,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(hero)) //very bad info - stacks can be split between multiple heroes :(
|
if (q.quest->checkQuest(hero)) //very bad info - stacks can be split between multiple heroes :(
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()).sethero(hero));
|
return sptr(Goals::VisitObj(q.obj->id.getNum()).sethero(hero));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto creature : q.quest->m6creatures)
|
for (auto creature : q.quest->m6creatures)
|
||||||
@@ -2410,7 +2454,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
|
if (q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()));
|
return sptr(Goals::VisitObj(q.obj->id.getNum()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2430,9 +2474,9 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
auto obj = cb->getObjByQuestIdentifier(q.quest->m13489val);
|
auto obj = cb->getObjByQuestIdentifier(q.quest->m13489val);
|
||||||
if (obj)
|
if (obj)
|
||||||
return sptr(Goals::GetObj(obj->id.getNum()));
|
return sptr(Goals::VisitObj(obj->id.getNum()));
|
||||||
else
|
else
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum())); //visit seer hut
|
return sptr(Goals::VisitObj(q.obj->id.getNum())); //visit seer hut
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CQuest::MISSION_PRIMARY_STAT:
|
case CQuest::MISSION_PRIMARY_STAT:
|
||||||
@@ -2442,7 +2486,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(hero))
|
if (q.quest->checkQuest(hero))
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()).sethero(hero));
|
return sptr(Goals::VisitObj(q.obj->id.getNum()).sethero(hero));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < q.quest->m2stats.size(); ++i)
|
for (int i = 0; i < q.quest->m2stats.size(); ++i)
|
||||||
@@ -2458,7 +2502,7 @@ Goals::TSubgoal VCAI::questToGoal(const QuestInfo & q)
|
|||||||
{
|
{
|
||||||
if (q.quest->checkQuest(hero))
|
if (q.quest->checkQuest(hero))
|
||||||
{
|
{
|
||||||
return sptr(Goals::GetObj(q.obj->id.getNum()).sethero(hero)); //TODO: causes infinite loop :/
|
return sptr(Goals::VisitObj(q.obj->id.getNum()).sethero(hero)); //TODO: causes infinite loop :/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logAi->debug("Don't know how to reach hero level %d", q.quest->m13489val);
|
logAi->debug("Don't know how to reach hero level %d", q.quest->m13489val);
|
||||||
|
@@ -121,6 +121,7 @@ public:
|
|||||||
void tryRealize(Goals::Explore & g);
|
void tryRealize(Goals::Explore & g);
|
||||||
void tryRealize(Goals::RecruitHero & g);
|
void tryRealize(Goals::RecruitHero & g);
|
||||||
void tryRealize(Goals::VisitTile & g);
|
void tryRealize(Goals::VisitTile & g);
|
||||||
|
void tryRealize(Goals::VisitObj & g);
|
||||||
void tryRealize(Goals::VisitHero & g);
|
void tryRealize(Goals::VisitHero & g);
|
||||||
void tryRealize(Goals::BuildThis & g);
|
void tryRealize(Goals::BuildThis & g);
|
||||||
void tryRealize(Goals::DigAtTile & g);
|
void tryRealize(Goals::DigAtTile & g);
|
||||||
@@ -284,7 +285,7 @@ public:
|
|||||||
h.template registerType<Goals::AbstractGoal, Goals::GatherArmy>();
|
h.template registerType<Goals::AbstractGoal, Goals::GatherArmy>();
|
||||||
h.template registerType<Goals::AbstractGoal, Goals::GatherTroops>();
|
h.template registerType<Goals::AbstractGoal, Goals::GatherTroops>();
|
||||||
h.template registerType<Goals::AbstractGoal, Goals::GetArtOfType>();
|
h.template registerType<Goals::AbstractGoal, Goals::GetArtOfType>();
|
||||||
h.template registerType<Goals::AbstractGoal, Goals::GetObj>();
|
h.template registerType<Goals::AbstractGoal, Goals::VisitObj>();
|
||||||
h.template registerType<Goals::AbstractGoal, Goals::Invalid>();
|
h.template registerType<Goals::AbstractGoal, Goals::Invalid>();
|
||||||
//h.template registerType<Goals::AbstractGoal, Goals::NotLose>();
|
//h.template registerType<Goals::AbstractGoal, Goals::NotLose>();
|
||||||
h.template registerType<Goals::AbstractGoal, Goals::RecruitHero>();
|
h.template registerType<Goals::AbstractGoal, Goals::RecruitHero>();
|
||||||
|
Reference in New Issue
Block a user