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