Goals.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Goals.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 "Goals.h"
  12. #include "VCAI.h"
  13. #include "Fuzzy.h"
  14. #include "ResourceManager.h"
  15. #include "../../lib/mapping/CMap.h" //for victory conditions
  16. #include "../../lib/CPathfinder.h"
  17. #include "../../lib/StringConstants.h"
  18. #include "AIhelper.h"
  19. extern boost::thread_specific_ptr<CCallback> cb;
  20. extern boost::thread_specific_ptr<VCAI> ai;
  21. extern boost::thread_specific_ptr<AIhelper> ah;
  22. extern FuzzyHelper * fh;
  23. using namespace Goals;
  24. TSubgoal Goals::sptr(const AbstractGoal & tmp)
  25. {
  26. TSubgoal ptr;
  27. ptr.reset(tmp.clone());
  28. return ptr;
  29. }
  30. std::string Goals::AbstractGoal::name() const //TODO: virtualize
  31. {
  32. std::string desc;
  33. switch(goalType)
  34. {
  35. case INVALID:
  36. return "INVALID";
  37. case WIN:
  38. return "WIN";
  39. case DO_NOT_LOSE:
  40. return "DO NOT LOOSE";
  41. case CONQUER:
  42. return "CONQUER";
  43. case BUILD:
  44. return "BUILD";
  45. case EXPLORE:
  46. desc = "EXPLORE";
  47. break;
  48. case GATHER_ARMY:
  49. desc = "GATHER ARMY";
  50. break;
  51. case BUY_ARMY:
  52. return "BUY ARMY";
  53. break;
  54. case BOOST_HERO:
  55. desc = "BOOST_HERO (unsupported)";
  56. break;
  57. case RECRUIT_HERO:
  58. return "RECRUIT HERO";
  59. case BUILD_STRUCTURE:
  60. return "BUILD STRUCTURE";
  61. case COLLECT_RES:
  62. desc = "COLLECT RESOURCE " + GameConstants::RESOURCE_NAMES[resID] + " (" + boost::lexical_cast<std::string>(value) + ")";
  63. break;
  64. case TRADE:
  65. {
  66. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  67. if (obj)
  68. desc = (boost::format("TRADE %d of %s at %s") % value % GameConstants::RESOURCE_NAMES[resID] % obj->getObjectName()).str();
  69. }
  70. break;
  71. case GATHER_TROOPS:
  72. desc = "GATHER TROOPS";
  73. break;
  74. case GET_OBJ:
  75. {
  76. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  77. if(obj)
  78. desc = "GET OBJ " + obj->getObjectName();
  79. }
  80. break;
  81. case FIND_OBJ:
  82. desc = "FIND OBJ " + boost::lexical_cast<std::string>(objid);
  83. break;
  84. case VISIT_HERO:
  85. {
  86. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  87. if(obj)
  88. desc = "VISIT HERO " + obj->getObjectName();
  89. }
  90. break;
  91. case GET_ART_TYPE:
  92. desc = "GET ARTIFACT OF TYPE " + VLC->arth->artifacts[aid]->Name();
  93. break;
  94. case ISSUE_COMMAND:
  95. return "ISSUE COMMAND (unsupported)";
  96. case VISIT_TILE:
  97. desc = "VISIT TILE " + tile.toString();
  98. break;
  99. case CLEAR_WAY_TO:
  100. desc = "CLEAR WAY TO " + tile.toString();
  101. break;
  102. case DIG_AT_TILE:
  103. desc = "DIG AT TILE " + tile.toString();
  104. break;
  105. default:
  106. return boost::lexical_cast<std::string>(goalType);
  107. }
  108. if(hero.get(true)) //FIXME: used to crash when we lost hero and failed goal
  109. desc += " (" + hero->name + ")";
  110. return desc;
  111. }
  112. //TODO: virtualize if code gets complex?
  113. bool Goals::AbstractGoal::operator==(AbstractGoal & g)
  114. {
  115. if(g.goalType != goalType)
  116. return false;
  117. if(g.isElementar != isElementar) //elementar goals fulfill long term non-elementar goals (VisitTile)
  118. return false;
  119. switch(goalType)
  120. {
  121. //no parameters
  122. case INVALID:
  123. case WIN:
  124. case DO_NOT_LOSE:
  125. case RECRUIT_HERO: //overloaded
  126. return true;
  127. break;
  128. //assigned to hero, no parameters
  129. case CONQUER:
  130. case EXPLORE:
  131. case BOOST_HERO:
  132. return g.hero.h == hero.h; //how comes HeroPtrs are equal for different heroes?
  133. break;
  134. case GATHER_ARMY: //actual value is indifferent
  135. return (g.hero.h == hero.h || town == g.town); //TODO: gather army for town maybe?
  136. break;
  137. //assigned hero and tile
  138. case VISIT_TILE:
  139. case CLEAR_WAY_TO:
  140. case DIG_AT_TILE:
  141. return (g.hero.h == hero.h && g.tile == tile);
  142. break;
  143. //assigned hero and object
  144. case GET_OBJ:
  145. case FIND_OBJ: //TODO: use subtype?
  146. case VISIT_HERO:
  147. case GET_ART_TYPE:
  148. return (g.hero.h == hero.h && g.objid == objid);
  149. break;
  150. case BUILD_STRUCTURE:
  151. return (town == g.town && bid == g.bid); //build specific structure in specific town
  152. break;
  153. //no check atm
  154. case COLLECT_RES:
  155. case TRADE: //TODO
  156. return (resID == g.resID); //every hero may collect resources
  157. break;
  158. case GATHER_TROOPS:
  159. case ISSUE_COMMAND:
  160. case BUILD: //TODO: should be decomposed to build specific structures
  161. default:
  162. return false;
  163. }
  164. }
  165. //TODO: find out why the following are not generated automatically on MVS?
  166. namespace Goals
  167. {
  168. template<>
  169. void CGoal<Win>::accept(VCAI * ai)
  170. {
  171. ai->tryRealize(static_cast<Win &>(*this));
  172. }
  173. template<>
  174. void CGoal<Build>::accept(VCAI * ai)
  175. {
  176. ai->tryRealize(static_cast<Build &>(*this));
  177. }
  178. template<>
  179. float CGoal<Win>::accept(FuzzyHelper * f)
  180. {
  181. return f->evaluate(static_cast<Win &>(*this));
  182. }
  183. template<>
  184. float CGoal<Build>::accept(FuzzyHelper * f)
  185. {
  186. return f->evaluate(static_cast<Build &>(*this));
  187. }
  188. bool TSubgoal::operator==(const TSubgoal & rhs) const
  189. {
  190. return *get() == *rhs.get(); //comparison for Goals is overloaded, so they don't need to be identical to match
  191. }
  192. bool BuyArmy::operator==(BuyArmy & g)
  193. {
  194. //if (hero && hero != g.hero)
  195. // return false;
  196. return town == g.town;
  197. }
  198. bool BuyArmy::fulfillsMe(TSubgoal goal)
  199. {
  200. //if (hero && hero != goal->hero)
  201. // return false;
  202. return town == goal->town && goal->value >= value; //can always buy more army
  203. }
  204. TSubgoal BuyArmy::whatToDoToAchieve()
  205. {
  206. //TODO: calculate the actual cost of units instead
  207. TResources price;
  208. price[Res::GOLD] = value * 0.4f; //some approximate value
  209. return ah->whatToDo(price, iAmElementar()); //buy right now or gather resources
  210. }
  211. std::string BuyArmy::completeMessage() const
  212. {
  213. return boost::format("Bought army of value %d in town of %s") % value, town->name;
  214. }
  215. }
  216. TSubgoal Trade::whatToDoToAchieve()
  217. {
  218. return iAmElementar();
  219. }
  220. bool Trade::operator==(CollectRes & g)
  221. {
  222. if (g.resID == resID)
  223. if (g.value == value) //TODO: not sure if that logic is consitent
  224. return true;
  225. return false;
  226. }
  227. //TSubgoal AbstractGoal::whatToDoToAchieve()
  228. //{
  229. // logAi->debug("Decomposing goal of type %s",name());
  230. // return sptr (Goals::Explore());
  231. //}
  232. TSubgoal Win::whatToDoToAchieve()
  233. {
  234. auto toBool = [=](const EventCondition &)
  235. {
  236. // TODO: proper implementation
  237. // Right now even already fulfilled goals will be included into generated list
  238. // Proper check should test if event condition is already fulfilled
  239. // Easiest way to do this is to call CGameState::checkForVictory but this function should not be
  240. // used on client side or in AI code
  241. return false;
  242. };
  243. std::vector<EventCondition> goals;
  244. for(const TriggeredEvent & event : cb->getMapHeader()->triggeredEvents)
  245. {
  246. //TODO: try to eliminate human player(s) using loss conditions that have isHuman element
  247. if(event.effect.type == EventEffect::VICTORY)
  248. {
  249. boost::range::copy(event.trigger.getFulfillmentCandidates(toBool), std::back_inserter(goals));
  250. }
  251. }
  252. //TODO: instead of returning first encountered goal AI should generate list of possible subgoals
  253. for(const EventCondition & goal : goals)
  254. {
  255. switch(goal.condition)
  256. {
  257. case EventCondition::HAVE_ARTIFACT:
  258. return sptr(Goals::GetArtOfType(goal.objectType));
  259. case EventCondition::DESTROY:
  260. {
  261. if(goal.object)
  262. {
  263. auto obj = cb->getObj(goal.object->id);
  264. if(obj)
  265. if(obj->getOwner() == ai->playerID) //we can't capture our own object
  266. return sptr(Goals::Conquer());
  267. return sptr(Goals::GetObj(goal.object->id.getNum()));
  268. }
  269. else
  270. {
  271. // TODO: destroy all objects of type goal.objectType
  272. // This situation represents "kill all creatures" condition from H3
  273. break;
  274. }
  275. }
  276. case EventCondition::HAVE_BUILDING:
  277. {
  278. // TODO build other buildings apart from Grail
  279. // goal.objectType = buidingID to build
  280. // goal.object = optional, town in which building should be built
  281. // Represents "Improve town" condition from H3 (but unlike H3 it consists from 2 separate conditions)
  282. if(goal.objectType == BuildingID::GRAIL)
  283. {
  284. if(auto h = ai->getHeroWithGrail())
  285. {
  286. //hero is in a town that can host Grail
  287. if(h->visitedTown && !vstd::contains(h->visitedTown->forbiddenBuildings, BuildingID::GRAIL))
  288. {
  289. const CGTownInstance * t = h->visitedTown;
  290. return sptr(Goals::BuildThis(BuildingID::GRAIL, t).setpriority(10));
  291. }
  292. else
  293. {
  294. auto towns = cb->getTownsInfo();
  295. towns.erase(boost::remove_if(towns,
  296. [](const CGTownInstance * t) -> bool
  297. {
  298. return vstd::contains(t->forbiddenBuildings, BuildingID::GRAIL);
  299. }),
  300. towns.end());
  301. boost::sort(towns, CDistanceSorter(h.get()));
  302. if(towns.size())
  303. {
  304. return sptr(Goals::VisitTile(towns.front()->visitablePos()).sethero(h));
  305. }
  306. }
  307. }
  308. double ratio = 0;
  309. // maybe make this check a bit more complex? For example:
  310. // 0.75 -> dig randomly within 3 tiles radius
  311. // 0.85 -> radius now 2 tiles
  312. // 0.95 -> 1 tile radius, position is fully known
  313. // AFAIK H3 AI does something like this
  314. int3 grailPos = cb->getGrailPos(&ratio);
  315. if(ratio > 0.99)
  316. {
  317. return sptr(Goals::DigAtTile(grailPos));
  318. } //TODO: use FIND_OBJ
  319. else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
  320. return sptr(Goals::GetObj(obj->id.getNum()));
  321. else
  322. return sptr(Goals::Explore());
  323. }
  324. break;
  325. }
  326. case EventCondition::CONTROL:
  327. {
  328. if(goal.object)
  329. {
  330. return sptr(Goals::GetObj(goal.object->id.getNum()));
  331. }
  332. else
  333. {
  334. //TODO: control all objects of type "goal.objectType"
  335. // Represents H3 condition "Flag all mines"
  336. break;
  337. }
  338. }
  339. case EventCondition::HAVE_RESOURCES:
  340. //TODO mines? piles? marketplace?
  341. //save?
  342. return sptr(Goals::CollectRes(static_cast<Res::ERes>(goal.objectType), goal.value));
  343. case EventCondition::HAVE_CREATURES:
  344. return sptr(Goals::GatherTroops(goal.objectType, goal.value));
  345. case EventCondition::TRANSPORT:
  346. {
  347. //TODO. merge with bring Grail to town? So AI will first dig grail, then transport it using this goal and builds it
  348. // Represents "transport artifact" condition:
  349. // goal.objectType = type of artifact
  350. // goal.object = destination-town where artifact should be transported
  351. break;
  352. }
  353. case EventCondition::STANDARD_WIN:
  354. return sptr(Goals::Conquer());
  355. // Conditions that likely don't need any implementation
  356. case EventCondition::DAYS_PASSED:
  357. break; // goal.value = number of days for condition to trigger
  358. case EventCondition::DAYS_WITHOUT_TOWN:
  359. break; // goal.value = number of days to trigger this
  360. case EventCondition::IS_HUMAN:
  361. break; // Should be only used in calculation of candidates (see toBool lambda)
  362. case EventCondition::CONST_VALUE:
  363. break;
  364. case EventCondition::HAVE_0:
  365. case EventCondition::HAVE_BUILDING_0:
  366. case EventCondition::DESTROY_0:
  367. //TODO: support new condition format
  368. return sptr(Goals::Conquer());
  369. default:
  370. assert(0);
  371. }
  372. }
  373. return sptr(Goals::Invalid());
  374. }
  375. TSubgoal FindObj::whatToDoToAchieve()
  376. {
  377. const CGObjectInstance * o = nullptr;
  378. if(resID > -1) //specified
  379. {
  380. for(const CGObjectInstance * obj : ai->visitableObjs)
  381. {
  382. if(obj->ID == objid && obj->subID == resID)
  383. {
  384. o = obj;
  385. break; //TODO: consider multiple objects and choose best
  386. }
  387. }
  388. }
  389. else
  390. {
  391. for(const CGObjectInstance * obj : ai->visitableObjs)
  392. {
  393. if(obj->ID == objid)
  394. {
  395. o = obj;
  396. break; //TODO: consider multiple objects and choose best
  397. }
  398. }
  399. }
  400. if(o && ai->isAccessible(o->pos)) //we don't use isAccessibleForHero as we don't know which hero it is
  401. return sptr(Goals::GetObj(o->id.getNum()));
  402. else
  403. return sptr(Goals::Explore());
  404. }
  405. bool Goals::FindObj::fulfillsMe(TSubgoal goal)
  406. {
  407. if (goal->goalType == Goals::VISIT_TILE) //visiting tile visits object at same time
  408. {
  409. if (!hero || hero == goal->hero)
  410. for (auto obj : cb->getVisitableObjs(goal->tile)) //check if any object on that tile matches criteria
  411. if (obj->visitablePos() == goal->tile) //object could be removed
  412. if (obj->ID == objid && obj->subID == resID) //same type and subtype
  413. return true;
  414. }
  415. return false;
  416. }
  417. std::string GetObj::completeMessage() const
  418. {
  419. return "hero " + hero.get()->name + " captured Object ID = " + boost::lexical_cast<std::string>(objid);
  420. }
  421. TSubgoal GetObj::whatToDoToAchieve()
  422. {
  423. const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
  424. if(!obj)
  425. return sptr(Goals::Explore());
  426. if(obj->tempOwner == ai->playerID) //we can't capture our own object -> move to Win codition
  427. throw cannotFulfillGoalException("Cannot capture my own object " + obj->getObjectName());
  428. int3 pos = obj->visitablePos();
  429. if(hero)
  430. {
  431. if(ai->isAccessibleForHero(pos, hero))
  432. return sptr(Goals::VisitTile(pos).sethero(hero));
  433. }
  434. else
  435. {
  436. for(auto h : cb->getHeroesInfo())
  437. {
  438. if(ai->isAccessibleForHero(pos, h))
  439. return sptr(Goals::VisitTile(pos).sethero(h)); //we must visit object with same hero, if any
  440. }
  441. }
  442. return sptr(Goals::ClearWayTo(pos).sethero(hero));
  443. }
  444. bool GetObj::fulfillsMe(TSubgoal goal)
  445. {
  446. if(goal->goalType == Goals::VISIT_TILE) //visiting tile visits object at same time
  447. {
  448. if (!hero || hero == goal->hero)
  449. {
  450. auto obj = cb->getObj(ObjectInstanceID(objid));
  451. if (obj && obj->visitablePos() == goal->tile) //object could be removed
  452. return true;
  453. }
  454. }
  455. return false;
  456. }
  457. std::string VisitHero::completeMessage() const
  458. {
  459. return "hero " + hero.get()->name + " visited hero " + boost::lexical_cast<std::string>(objid);
  460. }
  461. TSubgoal VisitHero::whatToDoToAchieve()
  462. {
  463. const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
  464. if(!obj)
  465. return sptr(Goals::Explore());
  466. int3 pos = obj->visitablePos();
  467. if(hero && ai->isAccessibleForHero(pos, hero, true) && isSafeToVisit(hero, pos)) //enemy heroes can get reinforcements
  468. {
  469. if(hero->pos == pos)
  470. logAi->error("Hero %s tries to visit himself.", hero.name);
  471. else
  472. {
  473. //can't use VISIT_TILE here as tile appears blocked by target hero
  474. //FIXME: elementar goal should not be abstract
  475. return sptr(Goals::VisitHero(objid).sethero(hero).settile(pos).setisElementar(true));
  476. }
  477. }
  478. return sptr(Goals::Invalid());
  479. }
  480. bool VisitHero::fulfillsMe(TSubgoal goal)
  481. {
  482. //TODO: VisitObj shoudl not be used for heroes, but...
  483. if(goal->goalType == Goals::VISIT_TILE)
  484. {
  485. auto obj = cb->getObj(ObjectInstanceID(objid));
  486. if (!obj)
  487. {
  488. logAi->error("Hero %s: VisitHero::fulfillsMe at %s: object %d not found", hero.name, goal->tile.toString(), objid);
  489. return false;
  490. }
  491. return obj->visitablePos() == goal->tile;
  492. }
  493. return false;
  494. }
  495. TSubgoal GetArtOfType::whatToDoToAchieve()
  496. {
  497. TSubgoal alternativeWay = CGoal::lookForArtSmart(aid); //TODO: use
  498. if(alternativeWay->invalid())
  499. return sptr(Goals::FindObj(Obj::ARTIFACT, aid));
  500. return sptr(Goals::Invalid());
  501. }
  502. TSubgoal ClearWayTo::whatToDoToAchieve()
  503. {
  504. assert(cb->isInTheMap(tile)); //set tile
  505. if(!cb->isVisible(tile))
  506. {
  507. logAi->error("Clear way should be used with visible tiles!");
  508. return sptr(Goals::Explore());
  509. }
  510. return (fh->chooseSolution(getAllPossibleSubgoals()));
  511. }
  512. bool Goals::ClearWayTo::fulfillsMe(TSubgoal goal)
  513. {
  514. if (goal->goalType == Goals::VISIT_TILE)
  515. {
  516. if (!hero || hero == goal->hero)
  517. return tile == goal->tile;
  518. }
  519. return false;
  520. }
  521. TGoalVec ClearWayTo::getAllPossibleSubgoals()
  522. {
  523. TGoalVec ret;
  524. std::vector<const CGHeroInstance *> heroes;
  525. if(hero)
  526. heroes.push_back(hero.h);
  527. else
  528. heroes = cb->getHeroesInfo();
  529. for(auto h : heroes)
  530. {
  531. //TODO: handle clearing way to allied heroes that are blocked
  532. //if ((hero && hero->visitablePos() == tile && hero == *h) || //we can't free the way ourselves
  533. // h->visitablePos() == tile) //we are already on that tile! what does it mean?
  534. // continue;
  535. //if our hero is trapped, make sure we request clearing the way from OUR perspective
  536. auto sm = ai->getCachedSectorMap(h);
  537. int3 tileToHit = sm->firstTileToGet(h, tile);
  538. if(!tileToHit.valid())
  539. continue;
  540. if(isBlockedBorderGate(tileToHit))
  541. {
  542. //FIXME: this way we'll not visit gate and activate quest :?
  543. ret.push_back(sptr(Goals::FindObj(Obj::KEYMASTER, cb->getTile(tileToHit)->visitableObjects.back()->subID)));
  544. }
  545. auto topObj = cb->getTopObj(tileToHit);
  546. if(topObj)
  547. {
  548. if(vstd::contains(ai->reservedObjs, topObj) && !vstd::contains(ai->reservedHeroesMap[h], topObj))
  549. {
  550. throw goalFulfilledException(sptr(Goals::ClearWayTo(tile, h)));
  551. continue; //do not capure object reserved by other hero
  552. }
  553. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(h->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
  554. {
  555. if(topObj != hero.get(true)) //the hero we want to free
  556. logAi->error("%s stands in the way of %s", topObj->getObjectName(), h->getObjectName());
  557. }
  558. if(topObj->ID == Obj::QUEST_GUARD || topObj->ID == Obj::BORDERGUARD)
  559. {
  560. if(shouldVisit(h, topObj))
  561. {
  562. //do NOT use VISIT_TILE, as tile with quets guard can't be visited
  563. ret.push_back(sptr(Goals::GetObj(topObj->id.getNum()).sethero(h)));
  564. continue; //do not try to visit tile or gather army
  565. }
  566. else
  567. {
  568. //TODO: we should be able to return apriopriate quest here (VCAI::striveToQuest)
  569. logAi->debug("Quest guard blocks the way to %s", tile.toString());
  570. continue; //do not access quets guard if we can't complete the quest
  571. }
  572. }
  573. }
  574. if(isSafeToVisit(h, tileToHit)) //this makes sense only if tile is guarded, but there i no quest object
  575. {
  576. ret.push_back(sptr(Goals::VisitTile(tileToHit).sethero(h)));
  577. }
  578. else
  579. {
  580. ret.push_back(sptr(Goals::GatherArmy(evaluateDanger(tileToHit, h) * SAFE_ATTACK_CONSTANT).
  581. sethero(h).setisAbstract(true)));
  582. }
  583. }
  584. if(ai->canRecruitAnyHero())
  585. ret.push_back(sptr(Goals::RecruitHero()));
  586. if(ret.empty())
  587. {
  588. logAi->warn("There is no known way to clear the way to tile %s", tile.toString());
  589. throw goalFulfilledException(sptr(Goals::ClearWayTo(tile))); //make sure asigned hero gets unlocked
  590. }
  591. return ret;
  592. }
  593. std::string Explore::completeMessage() const
  594. {
  595. return "Hero " + hero.get()->name + " completed exploration";
  596. }
  597. TSubgoal Explore::whatToDoToAchieve()
  598. {
  599. auto ret = fh->chooseSolution(getAllPossibleSubgoals());
  600. if(hero) //use best step for this hero
  601. {
  602. return ret;
  603. }
  604. else
  605. {
  606. if(ret->hero.get(true))
  607. return sptr(sethero(ret->hero.h).setisAbstract(true)); //choose this hero and then continue with him
  608. else
  609. return ret; //other solutions, like buying hero from tavern
  610. }
  611. }
  612. TGoalVec Explore::getAllPossibleSubgoals()
  613. {
  614. TGoalVec ret;
  615. std::vector<const CGHeroInstance *> heroes;
  616. if(hero)
  617. {
  618. heroes.push_back(hero.h);
  619. }
  620. else
  621. {
  622. //heroes = ai->getUnblockedHeroes();
  623. heroes = cb->getHeroesInfo();
  624. vstd::erase_if(heroes, [](const HeroPtr h)
  625. {
  626. if(ai->getGoal(h)->goalType == Goals::EXPLORE) //do not reassign hero who is already explorer
  627. return true;
  628. if(!ai->isAbleToExplore(h))
  629. return true;
  630. return !h->movement; //saves time, immobile heroes are useless anyway
  631. });
  632. }
  633. //try to use buildings that uncover map
  634. std::vector<const CGObjectInstance *> objs;
  635. for(auto obj : ai->visitableObjs)
  636. {
  637. if(!vstd::contains(ai->alreadyVisited, obj))
  638. {
  639. switch(obj->ID.num)
  640. {
  641. case Obj::REDWOOD_OBSERVATORY:
  642. case Obj::PILLAR_OF_FIRE:
  643. case Obj::CARTOGRAPHER:
  644. objs.push_back(obj);
  645. break;
  646. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  647. case Obj::MONOLITH_TWO_WAY:
  648. case Obj::SUBTERRANEAN_GATE:
  649. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  650. assert(ai->knownTeleportChannels.find(tObj->channel) != ai->knownTeleportChannels.end());
  651. if(TeleportChannel::IMPASSABLE != ai->knownTeleportChannels[tObj->channel]->passability)
  652. objs.push_back(obj);
  653. break;
  654. }
  655. }
  656. else
  657. {
  658. switch(obj->ID.num)
  659. {
  660. case Obj::MONOLITH_TWO_WAY:
  661. case Obj::SUBTERRANEAN_GATE:
  662. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  663. if(TeleportChannel::IMPASSABLE == ai->knownTeleportChannels[tObj->channel]->passability)
  664. break;
  665. for(auto exit : ai->knownTeleportChannels[tObj->channel]->exits)
  666. {
  667. if(!cb->getObj(exit))
  668. { // Always attempt to visit two-way teleports if one of channel exits is not visible
  669. objs.push_back(obj);
  670. break;
  671. }
  672. }
  673. break;
  674. }
  675. }
  676. }
  677. auto primaryHero = ai->primaryHero().h;
  678. for(auto h : heroes)
  679. {
  680. auto sm = ai->getCachedSectorMap(h);
  681. for(auto obj : objs) //double loop, performance risk?
  682. {
  683. auto t = sm->firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
  684. if(ai->isTileNotReserved(h, t))
  685. ret.push_back(sptr(Goals::ClearWayTo(obj->visitablePos(), h).setisAbstract(true)));
  686. }
  687. int3 t = whereToExplore(h);
  688. if(t.valid())
  689. {
  690. ret.push_back(sptr(Goals::VisitTile(t).sethero(h)));
  691. }
  692. else
  693. {
  694. //FIXME: possible issues when gathering army to break
  695. if(hero.h == h || //exporation is assigned to this hero
  696. (!hero && h == primaryHero)) //not assigned to specific hero, let our main hero do the job
  697. {
  698. t = ai->explorationDesperate(h); //check this only ONCE, high cost
  699. if (t.valid()) //don't waste time if we are completely blocked
  700. {
  701. ret.push_back(sptr(Goals::ClearWayTo(t, h).setisAbstract(true)));
  702. continue;
  703. }
  704. }
  705. ai->markHeroUnableToExplore(h); //there is no freely accessible tile, do not poll this hero anymore
  706. }
  707. }
  708. //we either don't have hero yet or none of heroes can explore
  709. if((!hero || ret.empty()) && ai->canRecruitAnyHero())
  710. ret.push_back(sptr(Goals::RecruitHero()));
  711. if(ret.empty())
  712. {
  713. throw goalFulfilledException(sptr(Goals::Explore().sethero(hero)));
  714. }
  715. //throw cannotFulfillGoalException("Cannot explore - no possible ways found!");
  716. return ret;
  717. }
  718. bool Explore::fulfillsMe(TSubgoal goal)
  719. {
  720. if(goal->goalType == Goals::EXPLORE)
  721. {
  722. if(goal->hero)
  723. return hero == goal->hero;
  724. else
  725. return true; //cancel ALL exploration
  726. }
  727. return false;
  728. }
  729. TSubgoal RecruitHero::whatToDoToAchieve()
  730. {
  731. const CGTownInstance * t = ai->findTownWithTavern();
  732. if(!t)
  733. return sptr(Goals::BuildThis(BuildingID::TAVERN).setpriority(2));
  734. TResources res;
  735. res[Res::GOLD] = GameConstants::HERO_GOLD_COST;
  736. return ah->whatToDo(res, iAmElementar()); //either buy immediately, or collect res
  737. }
  738. bool Goals::RecruitHero::operator==(RecruitHero & g)
  739. {
  740. //TODO: check town and hero
  741. return true; //for now, recruiting any hero will do
  742. }
  743. std::string VisitTile::completeMessage() const
  744. {
  745. return "Hero " + hero.get()->name + " visited tile " + tile.toString();
  746. }
  747. TSubgoal VisitTile::whatToDoToAchieve()
  748. {
  749. auto ret = fh->chooseSolution(getAllPossibleSubgoals());
  750. if(ret->hero)
  751. {
  752. if(isSafeToVisit(ret->hero, tile) && ai->isAccessibleForHero(tile, ret->hero))
  753. {
  754. if(cb->getTile(tile)->topVisitableId().num == Obj::TOWN) //if target is town, fuzzy system will use additional "estimatedReward" variable to increase priority a bit
  755. ret->objid = Obj::TOWN; //TODO: move to getObj eventually and add appropiate logic there
  756. ret->setisElementar(true);
  757. return ret;
  758. }
  759. else
  760. {
  761. return sptr(Goals::GatherArmy(evaluateDanger(tile, *ret->hero) * SAFE_ATTACK_CONSTANT)
  762. .sethero(ret->hero).setisAbstract(true));
  763. }
  764. }
  765. return ret;
  766. }
  767. TGoalVec VisitTile::getAllPossibleSubgoals()
  768. {
  769. assert(cb->isInTheMap(tile));
  770. TGoalVec ret;
  771. if(!cb->isVisible(tile))
  772. ret.push_back(sptr(Goals::Explore())); //what sense does it make?
  773. else
  774. {
  775. std::vector<const CGHeroInstance *> heroes;
  776. if(hero)
  777. heroes.push_back(hero.h); //use assigned hero if any
  778. else
  779. heroes = cb->getHeroesInfo(); //use most convenient hero
  780. for(auto h : heroes)
  781. {
  782. if(ai->isAccessibleForHero(tile, h))
  783. ret.push_back(sptr(Goals::VisitTile(tile).sethero(h)));
  784. }
  785. if(ai->canRecruitAnyHero())
  786. ret.push_back(sptr(Goals::RecruitHero()));
  787. }
  788. if(ret.empty())
  789. {
  790. auto obj = vstd::frontOrNull(cb->getVisitableObjs(tile));
  791. if(obj && obj->ID == Obj::HERO && obj->tempOwner == ai->playerID) //our own hero stands on that tile
  792. {
  793. if(hero.get(true) && hero->id == obj->id) //if it's assigned hero, visit tile. If it's different hero, we can't visit tile now
  794. ret.push_back(sptr(Goals::VisitTile(tile).sethero(dynamic_cast<const CGHeroInstance *>(obj)).setisElementar(true)));
  795. else
  796. throw cannotFulfillGoalException("Tile is already occupied by another hero "); //FIXME: we should give up this tile earlier
  797. }
  798. else
  799. ret.push_back(sptr(Goals::ClearWayTo(tile)));
  800. }
  801. //important - at least one sub-goal must handle case which is impossible to fulfill (unreachable tile)
  802. return ret;
  803. }
  804. TSubgoal DigAtTile::whatToDoToAchieve()
  805. {
  806. const CGObjectInstance * firstObj = vstd::frontOrNull(cb->getVisitableObjs(tile));
  807. if(firstObj && firstObj->ID == Obj::HERO && firstObj->tempOwner == ai->playerID) //we have hero at dest
  808. {
  809. const CGHeroInstance * h = dynamic_cast<const CGHeroInstance *>(firstObj);
  810. sethero(h).setisElementar(true);
  811. return sptr(*this);
  812. }
  813. return sptr(Goals::VisitTile(tile));
  814. }
  815. TSubgoal BuildThis::whatToDoToAchieve()
  816. {
  817. auto b = BuildingID(bid);
  818. // find town if not set
  819. if (!town && hero)
  820. town = hero->visitedTown;
  821. if (!town)
  822. {
  823. for (const CGTownInstance * t : cb->getTownsInfo())
  824. {
  825. switch (cb->canBuildStructure(town, b))
  826. {
  827. case EBuildingState::ALLOWED:
  828. town = t;
  829. break; //TODO: look for prerequisites? this is not our reponsibility
  830. default:
  831. continue;
  832. }
  833. }
  834. }
  835. if (town) //we have specific town to build this
  836. {
  837. auto res = town->town->buildings.at(BuildingID(bid))->resources;
  838. return ah->whatToDo(res, iAmElementar()); //realize immediately or gather resources
  839. }
  840. else
  841. throw cannotFulfillGoalException("Cannot find town to build this");
  842. }
  843. TGoalVec Goals::CollectRes::getAllPossibleSubgoals()
  844. {
  845. TGoalVec ret;
  846. auto givesResource = [this](const CGObjectInstance * obj) -> bool
  847. {
  848. //TODO: move this logic to object side
  849. //TODO: remember mithril exists
  850. //TODO: water objects
  851. //TODO: Creature banks
  852. //return false first from once-visitable, before checking if they were even visited
  853. switch (obj->ID.num)
  854. {
  855. case Obj::TREASURE_CHEST:
  856. return resID == Res::GOLD;
  857. break;
  858. case Obj::RESOURCE:
  859. return obj->subID == resID;
  860. break;
  861. case Obj::MINE:
  862. return (obj->subID == resID &&
  863. (cb->getPlayerRelations(obj->tempOwner, ai->playerID) == PlayerRelations::ENEMIES)); //don't capture our mines
  864. break;
  865. case Obj::CAMPFIRE:
  866. return true; //contains all resources
  867. break;
  868. case Obj::WINDMILL:
  869. switch (resID)
  870. {
  871. case Res::GOLD:
  872. case Res::WOOD:
  873. return false;
  874. }
  875. break;
  876. case Obj::WATER_WHEEL:
  877. if (resID != Res::GOLD)
  878. return false;
  879. break;
  880. case Obj::MYSTICAL_GARDEN:
  881. if ((resID != Res::GOLD) && (resID != Res::GEMS))
  882. return false;
  883. break;
  884. case Obj::LEAN_TO:
  885. case Obj::WAGON:
  886. if (resID != Res::GOLD)
  887. return false;
  888. break;
  889. default:
  890. return false;
  891. break;
  892. }
  893. return !vstd::contains(ai->alreadyVisited, obj); //for weekly / once visitable
  894. };
  895. std::vector<const CGObjectInstance *> objs;
  896. for (auto obj : ai->visitableObjs)
  897. {
  898. if (givesResource(obj))
  899. objs.push_back(obj);
  900. }
  901. for (auto h : cb->getHeroesInfo())
  902. {
  903. auto sm = ai->getCachedSectorMap(h);
  904. std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
  905. for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
  906. {
  907. if (givesResource(obj))
  908. ourObjs.push_back(obj);
  909. }
  910. for (auto obj : ourObjs)
  911. {
  912. int3 dest = obj->visitablePos();
  913. auto t = sm->firstTileToGet(h, dest); //we assume that no more than one tile on the way is guarded
  914. if (t.valid()) //we know any path at all
  915. {
  916. if (ai->isTileNotReserved(h, t)) //no other hero wants to conquer that tile
  917. {
  918. if (isSafeToVisit(h, dest))
  919. {
  920. if (dest != t) //there is something blocking our way
  921. ret.push_back(sptr(Goals::ClearWayTo(dest, h).setisAbstract(true)));
  922. else
  923. ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setisAbstract(true)));
  924. }
  925. else //we need to get army in order to pick that object
  926. ret.push_back(sptr(Goals::GatherArmy(evaluateDanger(dest, h) * SAFE_ATTACK_CONSTANT).sethero(h).setisAbstract(true)));
  927. }
  928. }
  929. }
  930. }
  931. return ret;
  932. }
  933. TSubgoal CollectRes::whatToDoToAchieve()
  934. {
  935. auto goals = getAllPossibleSubgoals();
  936. auto trade = whatToDoToTrade();
  937. if (!trade->invalid())
  938. goals.push_back(trade);
  939. if (goals.empty())
  940. return sptr(Goals::Explore()); //we can always do that
  941. else
  942. return fh->chooseSolution(goals); //TODO: evaluate trading
  943. }
  944. TSubgoal Goals::CollectRes::whatToDoToTrade()
  945. {
  946. std::vector<const IMarket *> markets;
  947. std::vector<const CGObjectInstance *> visObjs;
  948. ai->retrieveVisitableObjs(visObjs, true);
  949. for (const CGObjectInstance * obj : visObjs)
  950. {
  951. if (const IMarket * m = IMarket::castFrom(obj, false))
  952. {
  953. if (obj->ID == Obj::TOWN && obj->tempOwner == ai->playerID && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
  954. markets.push_back(m);
  955. else if (obj->ID == Obj::TRADING_POST)
  956. markets.push_back(m);
  957. }
  958. }
  959. boost::sort(markets, [](const IMarket * m1, const IMarket * m2) -> bool
  960. {
  961. return m1->getMarketEfficiency() < m2->getMarketEfficiency();
  962. });
  963. markets.erase(boost::remove_if(markets, [](const IMarket * market) -> bool
  964. {
  965. if (!(market->o->ID == Obj::TOWN && market->o->tempOwner == ai->playerID))
  966. {
  967. if (!ai->isAccessible(market->o->visitablePos()))
  968. return true;
  969. }
  970. return false;
  971. }), markets.end());
  972. if (!markets.size())
  973. {
  974. for (const CGTownInstance * t : cb->getTownsInfo())
  975. {
  976. if (cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
  977. return sptr(Goals::BuildThis(BuildingID::MARKETPLACE, t).setpriority(2));
  978. }
  979. }
  980. else
  981. {
  982. const IMarket * m = markets.back();
  983. //attempt trade at back (best prices)
  984. int howManyCanWeBuy = 0;
  985. for (Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  986. {
  987. if (i == resID)
  988. continue;
  989. int toGive = -1, toReceive = -1;
  990. m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
  991. assert(toGive > 0 && toReceive > 0);
  992. howManyCanWeBuy += toReceive * (cb->getResourceAmount(i) / toGive);
  993. }
  994. if (howManyCanWeBuy >= value)
  995. {
  996. auto backObj = cb->getTopObj(m->o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
  997. assert(backObj);
  998. auto objid = m->o->id.getNum();
  999. if (backObj->tempOwner != ai->playerID) //top object not owned
  1000. {
  1001. return sptr(Goals::GetObj(objid)); //just go there
  1002. }
  1003. else //either it's our town, or we have hero there
  1004. {
  1005. Goals::Trade trade(resID, value, objid);
  1006. return sptr(trade.setisElementar(true)); //we can do this immediately - highest priority
  1007. }
  1008. }
  1009. }
  1010. return sptr(Goals::Invalid()); //cannot trade
  1011. //TODO: separate goal to execute trade?
  1012. //return sptr(setisElementar(true)); //not sure why we are here
  1013. }
  1014. bool CollectRes::fulfillsMe(TSubgoal goal)
  1015. {
  1016. if (goal->resID == resID)
  1017. if (goal->value >= value)
  1018. return true;
  1019. return false;
  1020. }
  1021. bool Goals::CollectRes::operator==(CollectRes & g)
  1022. {
  1023. if (g.resID == resID)
  1024. if (g.value == value) //TODO: not sure if that logic is consitent
  1025. return true;
  1026. return false;
  1027. }
  1028. TSubgoal GatherTroops::whatToDoToAchieve()
  1029. {
  1030. std::vector<const CGDwelling *> dwellings;
  1031. for(const CGTownInstance * t : cb->getTownsInfo())
  1032. {
  1033. auto creature = VLC->creh->creatures[objid];
  1034. if(t->subID == creature->faction) //TODO: how to force AI to build unupgraded creatures? :O
  1035. {
  1036. auto creatures = vstd::tryAt(t->town->creatures, creature->level - 1);
  1037. if(!creatures)
  1038. continue;
  1039. int upgradeNumber = vstd::find_pos(*creatures, creature->idNumber);
  1040. if(upgradeNumber < 0)
  1041. continue;
  1042. BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
  1043. if(t->hasBuilt(bid)) //this assumes only creatures with dwellings are assigned to faction
  1044. {
  1045. dwellings.push_back(t);
  1046. }
  1047. else
  1048. {
  1049. return sptr(Goals::BuildThis(bid, t).setpriority(priority));
  1050. }
  1051. }
  1052. }
  1053. for(auto obj : ai->visitableObjs)
  1054. {
  1055. if(obj->ID != Obj::CREATURE_GENERATOR1) //TODO: what with other creature generators?
  1056. continue;
  1057. auto d = dynamic_cast<const CGDwelling *>(obj);
  1058. for(auto creature : d->creatures)
  1059. {
  1060. if(creature.first) //there are more than 0 creatures avaliabe
  1061. {
  1062. for(auto type : creature.second)
  1063. {
  1064. if(type == objid && ah->freeResources().canAfford(VLC->creh->creatures[type]->cost))
  1065. dwellings.push_back(d);
  1066. }
  1067. }
  1068. }
  1069. }
  1070. if(dwellings.size())
  1071. {
  1072. typedef std::map<const CGHeroInstance *, const CGDwelling *> TDwellMap;
  1073. // sorted helper
  1074. auto comparator = [](const TDwellMap::value_type & a, const TDwellMap::value_type & b) -> bool
  1075. {
  1076. const CGPathNode * ln = ai->myCb->getPathsInfo(a.first)->getPathInfo(a.second->visitablePos());
  1077. const CGPathNode * rn = ai->myCb->getPathsInfo(b.first)->getPathInfo(b.second->visitablePos());
  1078. if(ln->turns != rn->turns)
  1079. return ln->turns < rn->turns;
  1080. return (ln->moveRemains > rn->moveRemains);
  1081. };
  1082. // for all owned heroes generate map <hero -> nearest dwelling>
  1083. TDwellMap nearestDwellings;
  1084. for(const CGHeroInstance * hero : cb->getHeroesInfo(true))
  1085. {
  1086. nearestDwellings[hero] = *boost::range::min_element(dwellings, CDistanceSorter(hero));
  1087. }
  1088. if(nearestDwellings.size())
  1089. {
  1090. // find hero who is nearest to a dwelling
  1091. const CGDwelling * nearest = boost::range::min_element(nearestDwellings, comparator)->second;
  1092. if(!nearest)
  1093. throw cannotFulfillGoalException("Cannot find nearest dwelling!");
  1094. return sptr(Goals::GetObj(nearest->id.getNum()));
  1095. }
  1096. else
  1097. return sptr(Goals::Explore());
  1098. }
  1099. else
  1100. {
  1101. return sptr(Goals::Explore());
  1102. }
  1103. //TODO: exchange troops between heroes
  1104. }
  1105. bool Goals::GatherTroops::fulfillsMe(TSubgoal goal)
  1106. {
  1107. if (!hero || hero == goal->hero) //we got army for desired hero or any hero
  1108. if (goal->objid == objid) //same creature type //TODO: consider upgrades?
  1109. if (goal->value >= value) //notify every time we get resources?
  1110. return true;
  1111. return false;
  1112. }
  1113. TSubgoal Conquer::whatToDoToAchieve()
  1114. {
  1115. return fh->chooseSolution(getAllPossibleSubgoals());
  1116. }
  1117. TGoalVec Conquer::getAllPossibleSubgoals()
  1118. {
  1119. TGoalVec ret;
  1120. auto conquerable = [](const CGObjectInstance * obj) -> bool
  1121. {
  1122. if(cb->getPlayerRelations(ai->playerID, obj->tempOwner) == PlayerRelations::ENEMIES)
  1123. {
  1124. switch(obj->ID.num)
  1125. {
  1126. case Obj::TOWN:
  1127. case Obj::HERO:
  1128. case Obj::CREATURE_GENERATOR1:
  1129. case Obj::MINE: //TODO: check ai->knownSubterraneanGates
  1130. return true;
  1131. }
  1132. }
  1133. return false;
  1134. };
  1135. std::vector<const CGObjectInstance *> objs;
  1136. for(auto obj : ai->visitableObjs)
  1137. {
  1138. if(conquerable(obj))
  1139. objs.push_back(obj);
  1140. }
  1141. for(auto h : cb->getHeroesInfo())
  1142. {
  1143. auto sm = ai->getCachedSectorMap(h);
  1144. std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
  1145. for(auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
  1146. {
  1147. if(conquerable(obj))
  1148. ourObjs.push_back(obj);
  1149. }
  1150. for(auto obj : ourObjs)
  1151. {
  1152. int3 dest = obj->visitablePos();
  1153. auto t = sm->firstTileToGet(h, dest); //we assume that no more than one tile on the way is guarded
  1154. if(t.valid()) //we know any path at all
  1155. {
  1156. if(ai->isTileNotReserved(h, t)) //no other hero wants to conquer that tile
  1157. {
  1158. if(isSafeToVisit(h, dest))
  1159. {
  1160. if(dest != t) //there is something blocking our way
  1161. ret.push_back(sptr(Goals::ClearWayTo(dest, h).setisAbstract(true)));
  1162. else
  1163. {
  1164. if(obj->ID.num == Obj::HERO) //enemy hero may move to other position
  1165. {
  1166. ret.push_back(sptr(Goals::VisitHero(obj->id.getNum()).sethero(h).setisAbstract(true)));
  1167. }
  1168. else //just visit that tile
  1169. {
  1170. if(obj->ID.num == Obj::TOWN)
  1171. //if target is town, fuzzy system will use additional "estimatedReward" variable to increase priority a bit
  1172. 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
  1173. else
  1174. ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setisAbstract(true)));
  1175. }
  1176. }
  1177. }
  1178. else //we need to get army in order to conquer that place
  1179. ret.push_back(sptr(Goals::GatherArmy(evaluateDanger(dest, h) * SAFE_ATTACK_CONSTANT).sethero(h).setisAbstract(true)));
  1180. }
  1181. }
  1182. }
  1183. }
  1184. if(!objs.empty() && ai->canRecruitAnyHero()) //probably no point to recruit hero if we see no objects to capture
  1185. ret.push_back(sptr(Goals::RecruitHero()));
  1186. if(ret.empty())
  1187. ret.push_back(sptr(Goals::Explore())); //we need to find an enemy
  1188. return ret;
  1189. }
  1190. TSubgoal Build::whatToDoToAchieve()
  1191. {
  1192. return iAmElementar();
  1193. }
  1194. bool Goals::Build::fulfillsMe(TSubgoal goal)
  1195. {
  1196. if (goal->goalType == Goals::BUILD || goal->goalType == Goals::BUILD_STRUCTURE)
  1197. return (!town || town == goal->town); //building anything will do, in this town if set
  1198. else
  1199. return false;
  1200. }
  1201. TSubgoal Invalid::whatToDoToAchieve()
  1202. {
  1203. return iAmElementar();
  1204. }
  1205. std::string GatherArmy::completeMessage() const
  1206. {
  1207. return "Hero " + hero.get()->name + " gathered army of value " + boost::lexical_cast<std::string>(value);
  1208. }
  1209. TSubgoal GatherArmy::whatToDoToAchieve()
  1210. {
  1211. //TODO: find hero if none set
  1212. assert(hero.h);
  1213. return fh->chooseSolution(getAllPossibleSubgoals()); //find dwelling. use current hero to prevent him from doing nothing.
  1214. }
  1215. static const BuildingID unitsSource[] = { BuildingID::DWELL_LVL_1, BuildingID::DWELL_LVL_2, BuildingID::DWELL_LVL_3,
  1216. BuildingID::DWELL_LVL_4, BuildingID::DWELL_LVL_5, BuildingID::DWELL_LVL_6, BuildingID::DWELL_LVL_7};
  1217. TGoalVec GatherArmy::getAllPossibleSubgoals()
  1218. {
  1219. //get all possible towns, heroes and dwellings we may use
  1220. TGoalVec ret;
  1221. //TODO: include evaluation of monsters gather in calculation
  1222. for(auto t : cb->getTownsInfo())
  1223. {
  1224. auto pos = t->visitablePos();
  1225. if(ai->isAccessibleForHero(pos, hero))
  1226. {
  1227. //grab army from town
  1228. if(!t->visitingHero && howManyReinforcementsCanGet(hero, t))
  1229. {
  1230. if(!vstd::contains(ai->townVisitsThisWeek[hero], t))
  1231. ret.push_back(sptr(Goals::VisitTile(pos).sethero(hero)));
  1232. }
  1233. //buy army in town
  1234. if (!t->visitingHero || t->visitingHero != hero.get(true))
  1235. {
  1236. ui32 val = std::min<ui32>(value, howManyReinforcementsCanBuy(hero, t));
  1237. if (val)
  1238. {
  1239. auto goal = sptr(Goals::BuyArmy(t, val).sethero(hero));
  1240. if (!ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
  1241. ret.push_back(goal);
  1242. }
  1243. }
  1244. //build dwelling
  1245. auto bid = ai->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK));
  1246. if (bid != BuildingID::NONE)
  1247. {
  1248. auto goal = sptr(BuildThis(bid, t).setpriority(priority));
  1249. if (!ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
  1250. ret.push_back(goal);
  1251. }
  1252. }
  1253. }
  1254. auto otherHeroes = cb->getHeroesInfo();
  1255. auto heroDummy = hero;
  1256. vstd::erase_if(otherHeroes, [heroDummy](const CGHeroInstance * h)
  1257. {
  1258. if(h == heroDummy.h)
  1259. return true;
  1260. else if(!ai->isAccessibleForHero(heroDummy->visitablePos(), h, true))
  1261. return true;
  1262. else if(!ai->canGetArmy(heroDummy.h, h))
  1263. return true;
  1264. else if(ai->getGoal(h)->goalType == Goals::GATHER_ARMY)
  1265. return true;
  1266. else
  1267. return false;
  1268. });
  1269. for(auto h : otherHeroes)
  1270. {
  1271. // Go to the other hero if we are faster
  1272. ret.push_back(sptr(Goals::VisitHero(h->id.getNum()).setisAbstract(true).sethero(hero)));
  1273. // Let the other hero come to us
  1274. ret.push_back(sptr(Goals::VisitHero(hero->id.getNum()).setisAbstract(true).sethero(h)));
  1275. }
  1276. std::vector<const CGObjectInstance *> objs;
  1277. for(auto obj : ai->visitableObjs)
  1278. {
  1279. if(obj->ID == Obj::CREATURE_GENERATOR1)
  1280. {
  1281. auto relationToOwner = cb->getPlayerRelations(obj->getOwner(), ai->playerID);
  1282. //Use flagged dwellings only when there are available creatures that we can afford
  1283. if(relationToOwner == PlayerRelations::SAME_PLAYER)
  1284. {
  1285. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  1286. for(auto & creLevel : dwelling->creatures)
  1287. {
  1288. if(creLevel.first)
  1289. {
  1290. for(auto & creatureID : creLevel.second)
  1291. {
  1292. auto creature = VLC->creh->creatures[creatureID];
  1293. if(ah->freeResources().canAfford(creature->cost))
  1294. objs.push_back(obj);
  1295. }
  1296. }
  1297. }
  1298. }
  1299. }
  1300. }
  1301. for(auto h : cb->getHeroesInfo())
  1302. {
  1303. auto sm = ai->getCachedSectorMap(h);
  1304. for(auto obj : objs)
  1305. {
  1306. //find safe dwelling
  1307. auto pos = obj->visitablePos();
  1308. if(ai->isGoodForVisit(obj, h, *sm))
  1309. ret.push_back(sptr(Goals::VisitTile(pos).sethero(h)));
  1310. }
  1311. }
  1312. if(ai->canRecruitAnyHero() && ah->freeGold() > GameConstants::HERO_GOLD_COST) //this is not stupid in early phase of game
  1313. {
  1314. if(auto t = ai->findTownWithTavern())
  1315. {
  1316. for(auto h : cb->getAvailableHeroes(t)) //we assume that all towns have same set of heroes
  1317. {
  1318. if(h && h->getTotalStrength() > 500) //do not buy heroes with single creatures for GatherArmy
  1319. {
  1320. ret.push_back(sptr(Goals::RecruitHero()));
  1321. break;
  1322. }
  1323. }
  1324. }
  1325. }
  1326. if(ret.empty())
  1327. {
  1328. if(hero == ai->primaryHero() || value >= 1.1f) // FIXME: check PR388
  1329. ret.push_back(sptr(Goals::Explore()));
  1330. else //workaround to break loop - seemingly there are no ways to explore left
  1331. throw goalFulfilledException(sptr(Goals::GatherArmy(0).sethero(hero)));
  1332. }
  1333. return ret;
  1334. }
  1335. //TSubgoal AbstractGoal::whatToDoToAchieve()
  1336. //{
  1337. // logAi->debug("Decomposing goal of type %s",name());
  1338. // return sptr (Goals::Explore());
  1339. //}
  1340. TSubgoal AbstractGoal::goVisitOrLookFor(const CGObjectInstance * obj)
  1341. {
  1342. if(obj)
  1343. return sptr(Goals::GetObj(obj->id.getNum()));
  1344. else
  1345. return sptr(Goals::Explore());
  1346. }
  1347. TSubgoal AbstractGoal::lookForArtSmart(int aid)
  1348. {
  1349. return sptr(Goals::Invalid());
  1350. }
  1351. bool AbstractGoal::invalid() const
  1352. {
  1353. return goalType == INVALID;
  1354. }
  1355. void AbstractGoal::accept(VCAI * ai)
  1356. {
  1357. ai->tryRealize(*this);
  1358. }
  1359. template<typename T>
  1360. void CGoal<T>::accept(VCAI * ai)
  1361. {
  1362. ai->tryRealize(static_cast<T &>(*this)); //casting enforces template instantiation
  1363. }
  1364. float AbstractGoal::accept(FuzzyHelper * f)
  1365. {
  1366. return f->evaluate(*this);
  1367. }
  1368. template<typename T>
  1369. float CGoal<T>::accept(FuzzyHelper * f)
  1370. {
  1371. return f->evaluate(static_cast<T &>(*this)); //casting enforces template instantiation
  1372. }