1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Implemented rule-based evaluation of tactical advantage between armies.

AI will creep more agressively and smarter.
This commit is contained in:
DjWarmonger 2012-03-04 10:43:46 +00:00
parent c5007f8bef
commit cd0ed39fbf
6 changed files with 620 additions and 388 deletions

View File

@ -41,12 +41,24 @@
*
*/
#define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
class BankConfig;
class FuzzyEngine;
class InputLVar;
class CGTownInstance;
using namespace boost::assign;
using namespace vstd;
FuzzyHelper fh;
struct armyStructure
{
float walkers, shooters, flyers;
ui32 maxSpeed;
};
ui64 evaluateBankConfig (BankConfig * bc)
{
ui64 danger = 0;
@ -57,33 +69,156 @@ ui64 evaluateBankConfig (BankConfig * bc)
return danger;
}
armyStructure evaluateArmyStructure (const CArmedInstance * army)
{
ui64 totalStrenght = army->getArmyStrength();
double walkersStrenght = 0;
double flyersStrenght = 0;
double shootersStrenght = 0;
ui32 maxSpeed = 0;
BOOST_FOREACH(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();
amax (maxSpeed, s.second->type->speed);
}
armyStructure as;
as.walkers = walkersStrenght / totalStrenght;
as.shooters = shootersStrenght / totalStrenght;
as.flyers = flyersStrenght / totalStrenght;
as.maxSpeed = maxSpeed;
return as;
}
FuzzyHelper::FuzzyHelper()
{
initBank();
initTacticalAdvantage();
}
void FuzzyHelper::initBank()
{
try
{
//Trivial bank estimation
bankInput = new fl::InputLVar("BankInput");
bankDanger = new fl::OutputLVar("BankDanger");
bankInput->addTerm(new fl::SingletonTerm ("SET"));
engine.addRuleBlock (&ruleBlock); //have to be added before the rules are parsed
engine.addRuleBlock (&bankBlock); //have to be added before the rules are parsed
engine.addInputLVar (bankInput);
engine.addOutputLVar (bankDanger);
for (int i = 0; i < 4; ++i)
{
bankDanger->addTerm(new fl::TriangularTerm ("Bank" + boost::lexical_cast<std::string>(i), 0, 1));
ruleBlock.addRule(new fl::MamdaniRule("if BankInput is SET then BankDanger is Bank" + boost::lexical_cast<std::string>(i), engine));
bankBlock.addRule(new fl::MamdaniRule("if BankInput is SET then BankDanger is Bank" + boost::lexical_cast<std::string>(i), engine));
}
}
catch (fl::FuzzyException fe)
{
tlog1 << fe.name() << ": " << fe.message() << '\n';
tlog1 << "initBank " << fe.name() << ": " << fe.message() << '\n';
}
}
void FuzzyHelper::initTacticalAdvantage()
{
try
{
//Tactical advantage calculation
std::vector<fl::InputLVar*> helper;
ourShooters = new fl::InputLVar("OurShooters");
ourWalkers = new fl::InputLVar("OurWalkers");
ourFlyers = new fl::InputLVar("OurFlyers");
enemyShooters = new fl::InputLVar("EnemyShooters");
enemyWalkers = new fl::InputLVar("EnemyWalkers");
enemyFlyers = new fl::InputLVar("EnemyFlyers");
helper += ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers;
BOOST_FOREACH (auto val, helper)
{
val->addTerm (new fl::ShoulderTerm("FEW", 0, 0.75, true));
val->addTerm (new fl::ShoulderTerm("MANY", 0.25, 1, false));
engine.addInputLVar(val);
}
helper.clear();
ourSpeed = new fl::InputLVar("OurSpeed");
enemySpeed = new fl::InputLVar("EnemySpeed");
helper += ourSpeed, enemySpeed;
BOOST_FOREACH (auto val, helper)
{
val->addTerm (new fl::ShoulderTerm("LOW", 3, 9, true));
val->addTerm (new fl::TriangularTerm("MEDIUM", 7, 13));
val->addTerm (new fl::ShoulderTerm("HIGH", 11, 16, false));
engine.addInputLVar(val);
}
castleWalls = new fl::InputLVar("CastleWalls");
castleWalls->addTerm(new fl::SingletonTerm("NONE", CGTownInstance::NONE));
castleWalls->addTerm(new fl::TrapezoidalTerm("MEDIUM", CGTownInstance::FORT, 2.5));
castleWalls->addTerm(new fl::ShoulderTerm("HIGH", CGTownInstance::CITADEL, CGTownInstance::CASTLE));
engine.addInputLVar(castleWalls);
bankPresent = new fl::InputLVar("Bank");
bankPresent->addTerm(new fl::SingletonTerm("FALSE", 0));
bankPresent->addTerm(new fl::SingletonTerm("TRUE", 1));
engine.addInputLVar(bankPresent);
threat = new fl::OutputLVar("Threat");
threat->addTerm (new fl::TriangularTerm("LOW", MIN_AI_STRENGHT, 1));
threat->addTerm (new fl::TriangularTerm("MEDIUM", 0.8, 1.2));
threat->addTerm (new fl::ShoulderTerm("HIGH", 1, 1.5, false));
engine.addOutputLVar(threat);
engine.hedgeSet().add(new fl::HedgeSomewhat());
engine.hedgeSet().add(new fl::HedgeVery());
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is very LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurSpeed is LOW and OurShooters is FEW and EnemyShooters is MANY then Threat is very HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if (OurShooters is MANY or OurFlyers is MANY) and EnemyShooters is MANY then Threat is LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH", engine));
//tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemyShooters is MANY then Threat is MEDIUM", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM", engine));
tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW", engine));
engine.addRuleBlock (&tacticalAdvantage);
}
catch(fl::ParsingException pe)
{
tlog1 << "initTacticalAdvantage " << pe.name() << ": " << pe.message() << '\n';
}
catch (fl::FuzzyException fe)
{
tlog1 << "initTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
}
}
ui64 FuzzyHelper::estimateBankDanger (int ID)
{
std::vector <ConstTransitivePtr<BankConfig>> & configs = VLC->objh->banksInfo[ID];
ui64 val = INFINITY;;
ui64 val = INFINITY;
try
{
switch (configs.size())
{
case 4:
@ -100,7 +235,7 @@ ui64 FuzzyHelper::estimateBankDanger (int ID)
//int averageValue = (evaluateBankConfig (VLC->objh->banksInfo[ID][0]) + evaluateBankConfig (VLC->objh->banksInfo[ID][3])) * 0.5;
dynamic_cast<fl::SingletonTerm*>(bankInput->term("SET"))->setValue(0.5);
bankInput->setInput (0.5);
engine.process();
engine.process (BANK_DANGER);
val = bankDanger->output().defuzzify(); //some expected value of this bank
}
catch (fl::FuzzyException fe)
@ -114,6 +249,53 @@ ui64 FuzzyHelper::estimateBankDanger (int ID)
default:
tlog3 << ("Uhnandled bank config!\n");
}
}
catch (fl::FuzzyException fe)
{
tlog1 << "estimateBankDanger " << fe.name() << ": " << fe.message() << '\n';
}
return val;
}
float FuzzyHelper::getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy)
{
float output = 1;
try
{
armyStructure ourStructure = evaluateArmyStructure(we);
armyStructure enemyStructure = evaluateArmyStructure(enemy);
ourWalkers->setInput(ourStructure.walkers);
ourShooters->setInput(ourStructure.shooters);
ourFlyers->setInput(ourStructure.flyers);
ourSpeed->setInput(ourStructure.maxSpeed);
enemyWalkers->setInput(enemyStructure.walkers);
enemyShooters->setInput(enemyStructure.shooters);
enemyFlyers->setInput(enemyStructure.flyers);
enemySpeed->setInput(enemyStructure.maxSpeed);
bool bank = dynamic_cast<const CBank*>(enemy);
if (bank)
bankPresent->setInput(1);
else
bankPresent->setInput(0);
const CGTownInstance * fort = dynamic_cast<const CGTownInstance*>(enemy);
if (fort)
{
castleWalls->setInput (fort->fortLevel());
}
else
castleWalls->setInput(0);
engine.process (TACTICAL_ADVANTAGE);
output = threat->output().defuzzify();
}
catch (fl::FuzzyException fe)
{
tlog1 << "getTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
}
return output;
}

View File

@ -13,6 +13,7 @@
*/
class VCAI;
class CArmedInstance;
template <typename T> bool isinf (T val)
{
@ -24,11 +25,25 @@ class FuzzyHelper
friend class VCAI;
fl::FuzzyEngine engine;
fl::InputLVar* bankInput;
fl::OutputLVar* bankDanger;
fl::RuleBlock ruleBlock;
fl::RuleBlock bankBlock;
fl::InputLVar * ourWalkers, * ourShooters, * ourFlyers, * enemyWalkers, * enemyShooters, * enemyFlyers;
fl::InputLVar * ourSpeed, * enemySpeed;
fl::InputLVar * bankPresent;
fl::InputLVar * castleWalls;
fl::OutputLVar * threat;
fl::RuleBlock tacticalAdvantage;
public:
enum RuleBlocks {BANK_DANGER, TACTICAL_ADVANTAGE};
FuzzyHelper();
void initBank();
void initTacticalAdvantage();
ui64 estimateBankDanger (int ID);
float getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy); //returns factor how many times enemy is stronger than us
};

View File

@ -11,7 +11,7 @@ extern FuzzyHelper fh;
const int ACTUAL_RESOURCE_COUNT = 7;
const double SAFE_ATTACK_CONSTANT = 2.5;
const double SAFE_ATTACK_CONSTANT = 1.5;
using namespace vstd;
//one thread may be turn of AI and another will be handling a side effect for AI2
@ -297,6 +297,39 @@ ui64 evaluateDanger(crint3 tile)
return 0;
}
ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor)
{
const TerrainTile *t = cb->getTile(tile, false);
if(!t) //we can know about guard but can't check its tile (the edge of fow)
return 190000000; //MUCH
ui64 objectDanger = 0, guardDanger = 0;
CArmedInstance * dangerousObject;
if(t->visitable)
{
dangerousObject = dynamic_cast<CArmedInstance*>(t->visitableObjects.back());
if (dangerousObject)
{
objectDanger = evaluateDanger(dangerousObject);
objectDanger *= fh.getTacticalAdvantage (visitor, dangerousObject);
}
else
{
objectDanger = 0;
}
}
int3 guardPos = cb->guardingCreaturePosition(tile);
if(guardPos.x >= 0 && guardPos != tile)
guardDanger = evaluateDanger(guardPos, visitor);
//TODO mozna odwiedzic blockvis nie ruszajac straznika
return std::max(objectDanger, guardDanger);
return 0;
}
ui64 evaluateDanger(const CGObjectInstance *obj)
{
if(obj->tempOwner < GameConstants::PLAYER_LIMIT && cb->getPlayerRelations(obj->tempOwner, ai->playerID)) //owned or allied objects don't pose any threat
@ -945,7 +978,7 @@ void VCAI::buildStructure(const CGTownInstance * t)
bool isSafeToVisit(const CGHeroInstance *h, crint3 tile)
{
const ui64 heroStrength = h->getTotalStrength(),
dangerStrength = evaluateDanger(tile);
dangerStrength = evaluateDanger(tile, h);
if(dangerStrength)
{
if(heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength)

View File

@ -1820,15 +1820,15 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
break;
}
}
int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
{
if((builtBuildings.find(9))!=builtBuildings.end())
return 3;
return CASTLE;
if((builtBuildings.find(8))!=builtBuildings.end())
return 2;
return CITADEL;
if((builtBuildings.find(7))!=builtBuildings.end())
return 1;
return 0;
return FORT;
return NONE;
}
int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol

View File

@ -544,6 +544,8 @@ struct DLL_LINKAGE GrowthInfo
class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
{
public:
enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
CTownAndVisitingHero townAndVis;
CTown * town;
std::string name; // name of town
@ -605,7 +607,7 @@ public:
//////////////////////////////////////////////////////////////////////////
bool needsLastStack() const;
int fortLevel() const; //0 - none, 1 - fort, 2 - citadel, 3 - castle
CGTownInstance::EFortLevel fortLevel() const;
int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
bool creatureDwelling(const int & level, bool upgraded=false) const;