Fuzzy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Fuzzy.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Fuzzy.h"
  12. #include <limits>
  13. #include "../../lib/mapObjects/MapObjects.h"
  14. #include "../../lib/mapObjects/CommonConstructors.h"
  15. #include "../../lib/CCreatureHandler.h"
  16. #include "../../lib/CPathfinder.h"
  17. #include "../../lib/CGameStateFwd.h"
  18. #include "../../lib/VCMI_Lib.h"
  19. #include "../../CCallback.h"
  20. #include "VCAI.h"
  21. #define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
  22. #define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
  23. struct BankConfig;
  24. class CBankInfo;
  25. class Engine;
  26. class InputVariable;
  27. class CGTownInstance;
  28. FuzzyHelper * fh;
  29. extern boost::thread_specific_ptr<CCallback> cb;
  30. extern boost::thread_specific_ptr<VCAI> ai;
  31. engineBase::engineBase()
  32. {
  33. engine.addRuleBlock(&rules);
  34. }
  35. void engineBase::configure()
  36. {
  37. engine.configure("Minimum", "Maximum", "Minimum", "AlgebraicSum", "Centroid", "General");
  38. logAi->info(engine.toString());
  39. }
  40. void engineBase::addRule(const std::string & txt)
  41. {
  42. rules.addRule(fl::Rule::parse(txt, &engine));
  43. }
  44. struct armyStructure
  45. {
  46. float walkers, shooters, flyers;
  47. ui32 maxSpeed;
  48. };
  49. armyStructure evaluateArmyStructure(const CArmedInstance * army)
  50. {
  51. ui64 totalStrenght = army->getArmyStrength();
  52. double walkersStrenght = 0;
  53. double flyersStrenght = 0;
  54. double shootersStrenght = 0;
  55. ui32 maxSpeed = 0;
  56. for(auto s : army->Slots())
  57. {
  58. bool walker = true;
  59. if(s.second->type->hasBonusOfType(Bonus::SHOOTER))
  60. {
  61. shootersStrenght += s.second->getPower();
  62. walker = false;
  63. }
  64. if(s.second->type->hasBonusOfType(Bonus::FLYING))
  65. {
  66. flyersStrenght += s.second->getPower();
  67. walker = false;
  68. }
  69. if(walker)
  70. walkersStrenght += s.second->getPower();
  71. vstd::amax(maxSpeed, s.second->type->valOfBonuses(Bonus::STACKS_SPEED));
  72. }
  73. armyStructure as;
  74. as.walkers = walkersStrenght / totalStrenght;
  75. as.shooters = shootersStrenght / totalStrenght;
  76. as.flyers = flyersStrenght / totalStrenght;
  77. as.maxSpeed = maxSpeed;
  78. assert(as.walkers || as.flyers || as.shooters);
  79. return as;
  80. }
  81. FuzzyHelper::FuzzyHelper()
  82. {
  83. initTacticalAdvantage();
  84. ta.configure();
  85. initVisitTile();
  86. vt.configure();
  87. }
  88. void FuzzyHelper::initTacticalAdvantage()
  89. {
  90. try
  91. {
  92. ta.ourShooters = new fl::InputVariable("OurShooters");
  93. ta.ourWalkers = new fl::InputVariable("OurWalkers");
  94. ta.ourFlyers = new fl::InputVariable("OurFlyers");
  95. ta.enemyShooters = new fl::InputVariable("EnemyShooters");
  96. ta.enemyWalkers = new fl::InputVariable("EnemyWalkers");
  97. ta.enemyFlyers = new fl::InputVariable("EnemyFlyers");
  98. //Tactical advantage calculation
  99. std::vector<fl::InputVariable *> helper =
  100. {
  101. ta.ourShooters, ta.ourWalkers, ta.ourFlyers, ta.enemyShooters, ta.enemyWalkers, ta.enemyFlyers
  102. };
  103. for(auto val : helper)
  104. {
  105. ta.engine.addInputVariable(val);
  106. val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
  107. val->addTerm(new fl::Ramp("MANY", 0.4, 1));
  108. val->setRange(0.0, 1.0);
  109. }
  110. ta.ourSpeed = new fl::InputVariable("OurSpeed");
  111. ta.enemySpeed = new fl::InputVariable("EnemySpeed");
  112. helper = {ta.ourSpeed, ta.enemySpeed};
  113. for(auto val : helper)
  114. {
  115. ta.engine.addInputVariable(val);
  116. val->addTerm(new fl::Ramp("LOW", 6.5, 3));
  117. val->addTerm(new fl::Triangle("MEDIUM", 5.5, 10.5));
  118. val->addTerm(new fl::Ramp("HIGH", 8.5, 16));
  119. val->setRange(0, 25);
  120. }
  121. ta.castleWalls = new fl::InputVariable("CastleWalls");
  122. ta.engine.addInputVariable(ta.castleWalls);
  123. {
  124. fl::Rectangle * none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f);
  125. ta.castleWalls->addTerm(none);
  126. fl::Trapezoid * medium = new fl::Trapezoid("MEDIUM", (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f, CGTownInstance::FORT,
  127. CGTownInstance::CITADEL, CGTownInstance::CITADEL + (CGTownInstance::CASTLE - CGTownInstance::CITADEL) * 0.5f);
  128. ta.castleWalls->addTerm(medium);
  129. fl::Ramp * high = new fl::Ramp("HIGH", CGTownInstance::CITADEL - 0.1, CGTownInstance::CASTLE);
  130. ta.castleWalls->addTerm(high);
  131. ta.castleWalls->setRange(CGTownInstance::NONE, CGTownInstance::CASTLE);
  132. }
  133. ta.bankPresent = new fl::InputVariable("Bank");
  134. ta.engine.addInputVariable(ta.bankPresent);
  135. {
  136. fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
  137. ta.bankPresent->addTerm(termFalse);
  138. fl::Rectangle * termTrue = new fl::Rectangle("TRUE", 0.5f, 1);
  139. ta.bankPresent->addTerm(termTrue);
  140. ta.bankPresent->setRange(0, 1);
  141. }
  142. ta.threat = new fl::OutputVariable("Threat");
  143. ta.engine.addOutputVariable(ta.threat);
  144. ta.threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGHT));
  145. ta.threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
  146. ta.threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
  147. ta.threat->setRange(MIN_AI_STRENGHT, 1.5);
  148. ta.addRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is LOW");
  149. ta.addRule("if OurShooters is MANY and EnemyShooters is FEW then Threat is LOW");
  150. ta.addRule("if OurSpeed is LOW and EnemyShooters is MANY then Threat is HIGH");
  151. ta.addRule("if OurSpeed is HIGH and EnemyShooters is MANY then Threat is LOW");
  152. ta.addRule("if OurWalkers is FEW and EnemyShooters is MANY then Threat is somewhat LOW");
  153. ta.addRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH");
  154. //just to cover all cases
  155. ta.addRule("if OurShooters is FEW and EnemySpeed is HIGH then Threat is MEDIUM");
  156. ta.addRule("if EnemySpeed is MEDIUM then Threat is MEDIUM");
  157. ta.addRule("if EnemySpeed is LOW and OurShooters is FEW then Threat is MEDIUM");
  158. ta.addRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH");
  159. ta.addRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW");
  160. ta.addRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH");
  161. ta.addRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM");
  162. ta.addRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW");
  163. }
  164. catch(fl::Exception & pe)
  165. {
  166. logAi->error("initTacticalAdvantage: %s", pe.getWhat());
  167. }
  168. }
  169. ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
  170. {
  171. //this one is not fuzzy anymore, just calculate weighted average
  172. auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
  173. CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
  174. ui64 totalStrength = 0;
  175. ui8 totalChance = 0;
  176. for(auto config : bankInfo->getPossibleGuards())
  177. {
  178. totalStrength += config.second.totalStrength * config.first;
  179. totalChance += config.first;
  180. }
  181. return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
  182. }
  183. float FuzzyHelper::getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy)
  184. {
  185. float output = 1;
  186. try
  187. {
  188. armyStructure ourStructure = evaluateArmyStructure(we);
  189. armyStructure enemyStructure = evaluateArmyStructure(enemy);
  190. ta.ourWalkers->setValue(ourStructure.walkers);
  191. ta.ourShooters->setValue(ourStructure.shooters);
  192. ta.ourFlyers->setValue(ourStructure.flyers);
  193. ta.ourSpeed->setValue(ourStructure.maxSpeed);
  194. ta.enemyWalkers->setValue(enemyStructure.walkers);
  195. ta.enemyShooters->setValue(enemyStructure.shooters);
  196. ta.enemyFlyers->setValue(enemyStructure.flyers);
  197. ta.enemySpeed->setValue(enemyStructure.maxSpeed);
  198. bool bank = dynamic_cast<const CBank *>(enemy);
  199. if(bank)
  200. ta.bankPresent->setValue(1);
  201. else
  202. ta.bankPresent->setValue(0);
  203. const CGTownInstance * fort = dynamic_cast<const CGTownInstance *>(enemy);
  204. if(fort)
  205. ta.castleWalls->setValue(fort->fortLevel());
  206. else
  207. ta.castleWalls->setValue(0);
  208. //engine.process(TACTICAL_ADVANTAGE);//TODO: Process only Tactical_Advantage
  209. ta.engine.process();
  210. output = ta.threat->getValue();
  211. }
  212. catch(fl::Exception & fe)
  213. {
  214. logAi->error("getTacticalAdvantage: %s ", fe.getWhat());
  215. }
  216. if(output < 0 || (output != output))
  217. {
  218. fl::InputVariable * tab[] = {ta.bankPresent, ta.castleWalls, ta.ourWalkers, ta.ourShooters, ta.ourFlyers, ta.ourSpeed, ta.enemyWalkers, ta.enemyShooters, ta.enemyFlyers, ta.enemySpeed};
  219. std::string names[] = {"bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" };
  220. std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: ");
  221. for(int i = 0; i < boost::size(tab); i++)
  222. log << names[i] << ": " << tab[i]->getValue() << " ";
  223. logAi->error(log.str());
  224. assert(false);
  225. }
  226. return output;
  227. }
  228. FuzzyHelper::TacticalAdvantage::~TacticalAdvantage()
  229. {
  230. //TODO: smart pointers?
  231. delete ourWalkers;
  232. delete ourShooters;
  233. delete ourFlyers;
  234. delete enemyWalkers;
  235. delete enemyShooters;
  236. delete enemyFlyers;
  237. delete ourSpeed;
  238. delete enemySpeed;
  239. delete bankPresent;
  240. delete castleWalls;
  241. delete threat;
  242. }
  243. //std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec)
  244. Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
  245. {
  246. if(vec.empty()) //no possibilities found
  247. return sptr(Goals::Invalid());
  248. ai->cachedSectorMaps.clear();
  249. //a trick to switch between heroes less often - calculatePaths is costly
  250. auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  251. {
  252. return lhs->hero.h < rhs->hero.h;
  253. };
  254. boost::sort(vec, sortByHeroes);
  255. for(auto g : vec)
  256. {
  257. setPriority(g);
  258. }
  259. auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
  260. {
  261. return lhs->priority < rhs->priority;
  262. };
  263. return *boost::max_element(vec, compareGoals);
  264. }
  265. float FuzzyHelper::evaluate(Goals::Explore & g)
  266. {
  267. return 1;
  268. }
  269. float FuzzyHelper::evaluate(Goals::RecruitHero & g)
  270. {
  271. return 1;
  272. }
  273. FuzzyHelper::EvalVisitTile::~EvalVisitTile()
  274. {
  275. delete strengthRatio;
  276. delete heroStrength;
  277. delete turnDistance;
  278. delete missionImportance;
  279. delete estimatedReward;
  280. }
  281. void FuzzyHelper::initVisitTile()
  282. {
  283. try
  284. {
  285. vt.strengthRatio = new fl::InputVariable("strengthRatio"); //hero must be strong enough to defeat guards
  286. vt.heroStrength = new fl::InputVariable("heroStrength"); //we want to use weakest possible hero
  287. vt.turnDistance = new fl::InputVariable("turnDistance"); //we want to use hero who is near
  288. vt.missionImportance = new fl::InputVariable("lockedMissionImportance"); //we may want to preempt hero with low-priority mission
  289. vt.estimatedReward = new fl::InputVariable("estimatedReward"); //indicate AI that content of the file is important or it is probably bad
  290. vt.value = new fl::OutputVariable("Value");
  291. vt.value->setMinimum(0);
  292. vt.value->setMaximum(5);
  293. std::vector<fl::InputVariable *> helper = {vt.strengthRatio, vt.heroStrength, vt.turnDistance, vt.missionImportance, vt.estimatedReward};
  294. for(auto val : helper)
  295. {
  296. vt.engine.addInputVariable(val);
  297. }
  298. vt.engine.addOutputVariable(vt.value);
  299. vt.strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0));
  300. vt.strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3));
  301. vt.strengthRatio->setRange(0, SAFE_ATTACK_CONSTANT * 3);
  302. //strength compared to our main hero
  303. vt.heroStrength->addTerm(new fl::Ramp("LOW", 0.2, 0));
  304. vt.heroStrength->addTerm(new fl::Triangle("MEDIUM", 0.2, 0.8));
  305. vt.heroStrength->addTerm(new fl::Ramp("HIGH", 0.5, 1));
  306. vt.heroStrength->setRange(0.0, 1.0);
  307. vt.turnDistance->addTerm(new fl::Ramp("SMALL", 0.5, 0));
  308. vt.turnDistance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
  309. vt.turnDistance->addTerm(new fl::Ramp("LONG", 0.5, 3));
  310. vt.turnDistance->setRange(0.0, 3.0);
  311. vt.missionImportance->addTerm(new fl::Ramp("LOW", 2.5, 0));
  312. vt.missionImportance->addTerm(new fl::Triangle("MEDIUM", 2, 3));
  313. vt.missionImportance->addTerm(new fl::Ramp("HIGH", 2.5, 5));
  314. vt.missionImportance->setRange(0.0, 5.0);
  315. vt.estimatedReward->addTerm(new fl::Ramp("LOW", 2.5, 0));
  316. vt.estimatedReward->addTerm(new fl::Ramp("HIGH", 2.5, 5));
  317. vt.estimatedReward->setRange(0.0, 5.0);
  318. //an issue: in 99% cases this outputs center of mass (2.5) regardless of actual input :/
  319. //should be same as "mission Importance" to keep consistency
  320. vt.value->addTerm(new fl::Ramp("LOW", 2.5, 0));
  321. vt.value->addTerm(new fl::Triangle("MEDIUM", 2, 3)); //can't be center of mass :/
  322. vt.value->addTerm(new fl::Ramp("HIGH", 2.5, 5));
  323. vt.value->setRange(0.0, 5.0);
  324. //use unarmed scouts if possible
  325. vt.addRule("if strengthRatio is HIGH and heroStrength is LOW then Value is very HIGH");
  326. //we may want to use secondary hero(es) rather than main hero
  327. vt.addRule("if strengthRatio is HIGH and heroStrength is MEDIUM then Value is somewhat HIGH");
  328. vt.addRule("if strengthRatio is HIGH and heroStrength is HIGH then Value is somewhat LOW");
  329. //don't assign targets to heroes who are too weak, but prefer targets of our main hero (in case we need to gather army)
  330. vt.addRule("if strengthRatio is LOW and heroStrength is LOW then Value is very LOW");
  331. //attempt to arm secondary heroes is not stupid
  332. vt.addRule("if strengthRatio is LOW and heroStrength is MEDIUM then Value is somewhat HIGH");
  333. vt.addRule("if strengthRatio is LOW and heroStrength is HIGH then Value is LOW");
  334. //do not cancel important goals
  335. vt.addRule("if lockedMissionImportance is HIGH then Value is very LOW");
  336. vt.addRule("if lockedMissionImportance is MEDIUM then Value is somewhat LOW");
  337. vt.addRule("if lockedMissionImportance is LOW then Value is HIGH");
  338. //pick nearby objects if it's easy, avoid long walks
  339. vt.addRule("if turnDistance is SMALL then Value is HIGH");
  340. vt.addRule("if turnDistance is MEDIUM then Value is MEDIUM");
  341. vt.addRule("if turnDistance is LONG then Value is LOW");
  342. //some goals are more rewarding by definition f.e. capturing town is more important than collecting resource - experimental
  343. vt.addRule("if estimatedReward is HIGH then Value is very HIGH");
  344. vt.addRule("if estimatedReward is LOW then Value is somewhat LOW");
  345. }
  346. catch(fl::Exception & fe)
  347. {
  348. logAi->error("visitTile: %s", fe.getWhat());
  349. }
  350. }
  351. float FuzzyHelper::evaluate(Goals::VisitTile & g)
  352. {
  353. //we assume that hero is already set and we want to choose most suitable one for the mission
  354. if(!g.hero)
  355. return 0;
  356. //assert(cb->isInTheMap(g.tile));
  357. float turns = 0;
  358. float distance = CPathfinderHelper::getMovementCost(g.hero.h, g.tile);
  359. if(!distance) //we stand on that tile
  360. {
  361. turns = 0;
  362. }
  363. else
  364. {
  365. if(distance < g.hero->movement) //we can move there within one turn
  366. turns = (fl::scalar)distance / g.hero->movement;
  367. else
  368. turns = 1 + (fl::scalar)(distance - g.hero->movement) / g.hero->maxMovePoints(true); //bool on land?
  369. }
  370. float missionImportance = 0;
  371. if(vstd::contains(ai->lockedHeroes, g.hero))
  372. missionImportance = ai->lockedHeroes[g.hero]->priority;
  373. float strengthRatio = 10.0f; //we are much stronger than enemy
  374. ui64 danger = evaluateDanger(g.tile, g.hero.h);
  375. if(danger)
  376. strengthRatio = (fl::scalar)g.hero.h->getTotalStrength() / danger;
  377. float tilePriority = 0;
  378. if(g.objid == -1)
  379. {
  380. vt.estimatedReward->setEnabled(false);
  381. }
  382. else if(g.objid == Obj::TOWN) //TODO: move to getObj eventually and add appropiate logic there
  383. {
  384. vt.estimatedReward->setEnabled(true);
  385. tilePriority = 5;
  386. }
  387. try
  388. {
  389. vt.strengthRatio->setValue(strengthRatio);
  390. vt.heroStrength->setValue((fl::scalar)g.hero->getTotalStrength() / ai->primaryHero()->getTotalStrength());
  391. vt.turnDistance->setValue(turns);
  392. vt.missionImportance->setValue(missionImportance);
  393. vt.estimatedReward->setValue(tilePriority);
  394. vt.engine.process();
  395. //engine.process(VISIT_TILE); //TODO: Process only Visit_Tile
  396. g.priority = vt.value->getValue();
  397. }
  398. catch(fl::Exception & fe)
  399. {
  400. logAi->error("evaluate VisitTile: %s", fe.getWhat());
  401. }
  402. assert(g.priority >= 0);
  403. return g.priority;
  404. }
  405. float FuzzyHelper::evaluate(Goals::VisitHero & g)
  406. {
  407. auto obj = cb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
  408. if(!obj)
  409. return -100; //hero died in the meantime
  410. //TODO: consider direct copy (constructor?)
  411. g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
  412. return g.priority;
  413. }
  414. float FuzzyHelper::evaluate(Goals::GatherArmy & g)
  415. {
  416. //the more army we need, the more important goal
  417. //the more army we lack, the less important goal
  418. float army = g.hero->getArmyStrength();
  419. float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
  420. return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
  421. }
  422. float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
  423. {
  424. if(!g.hero.h)
  425. throw cannotFulfillGoalException("ClearWayTo called without hero!");
  426. int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
  427. if(t.valid())
  428. {
  429. if(isSafeToVisit(g.hero, t))
  430. {
  431. g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
  432. }
  433. else
  434. {
  435. g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
  436. sethero(g.hero).setisAbstract(true).accept(this));
  437. }
  438. return g.priority;
  439. }
  440. else
  441. return -1;
  442. }
  443. float FuzzyHelper::evaluate(Goals::BuildThis & g)
  444. {
  445. return g.priority; //TODO
  446. }
  447. float FuzzyHelper::evaluate(Goals::DigAtTile & g)
  448. {
  449. return 0;
  450. }
  451. float FuzzyHelper::evaluate(Goals::CollectRes & g)
  452. {
  453. return g.priority; //handled by ResourceManager
  454. }
  455. float FuzzyHelper::evaluate(Goals::Build & g)
  456. {
  457. return 0;
  458. }
  459. float FuzzyHelper::evaluate(Goals::BuyArmy & g)
  460. {
  461. return g.priority;
  462. }
  463. float FuzzyHelper::evaluate(Goals::Invalid & g)
  464. {
  465. return -1e10;
  466. }
  467. float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
  468. {
  469. logAi->warn("Cannot evaluate goal %s", g.name());
  470. return g.priority;
  471. }
  472. void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
  473. {
  474. g->setpriority(g->accept(this)); //this enforces returned value is set
  475. }