Goals.cpp 46 KB

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