Fuzzy.cpp 18 KB

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