VCAI.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  1. #include "StdInc.h"
  2. #include "VCAI.h"
  3. #include "../../lib/UnlockGuard.h"
  4. #include "Fuzzy.h"
  5. #include "../../lib/CObjectHandler.h"
  6. #define I_AM_ELEMENTAR return CGoal(*this).setisElementar(true)
  7. CLogger &aiLogger = tlog6;
  8. extern FuzzyHelper *fh;
  9. class CGVisitableOPW;
  10. const int ACTUAL_RESOURCE_COUNT = 7;
  11. const double SAFE_ATTACK_CONSTANT = 1.5;
  12. using namespace vstd;
  13. //one thread may be turn of AI and another will be handling a side effect for AI2
  14. boost::thread_specific_ptr<CCallback> cb;
  15. boost::thread_specific_ptr<VCAI> ai;
  16. // CCallback *cb;
  17. // VCAI *ai;
  18. //helper RAII to manage global ai/cb ptrs
  19. struct SetGlobalState
  20. {
  21. SetGlobalState(VCAI * AI)
  22. {
  23. assert(!ai.get());
  24. assert(!cb.get());
  25. ai.reset(AI);
  26. cb.reset(AI->myCb);
  27. }
  28. ~SetGlobalState()
  29. {
  30. ai.release();
  31. cb.release();
  32. }
  33. };
  34. #define SET_GLOBAL_STATE(ai) SetGlobalState _hlpSetState(ai);
  35. #define NET_EVENT_HANDLER SET_GLOBAL_STATE(this)
  36. #define MAKING_TURN SET_GLOBAL_STATE(this)
  37. const int GOLD_RESERVE = 10000; //when buying creatures we want to keep at least this much gold (10000 so at least we'll be able to reach capitol)
  38. const int HERO_GOLD_COST = 2500;
  39. const int ALLOWED_ROAMING_HEROES = 8;
  40. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  41. std::string goalName(EGoals goalType)
  42. {
  43. switch (goalType)
  44. {
  45. case INVALID:
  46. return "INVALID";
  47. case WIN:
  48. return "WIN";
  49. case CONQUER:
  50. return "CONQUER";
  51. case EXPLORE:
  52. return "EXPLORE";
  53. case GATHER_ARMY:
  54. return "GATHER ARMY";
  55. case VISIT_TILE:
  56. return "VISIT TILE";
  57. case CLEAR_WAY_TO:
  58. return "CLEAR WAY TO";
  59. default:
  60. return boost::lexical_cast<std::string>(goalType);
  61. }
  62. }
  63. bool compareHeroStrength(const CGHeroInstance *h1, const CGHeroInstance *h2)
  64. {
  65. return h1->getTotalStrength() < h2->getTotalStrength();
  66. }
  67. bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2)
  68. {
  69. return a1->getArmyStrength() < a2->getArmyStrength();
  70. }
  71. static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  72. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  73. struct AILogger
  74. {
  75. AILogger()
  76. {
  77. lvl = 0;
  78. }
  79. int lvl;
  80. struct Tab
  81. {
  82. Tab();
  83. ~Tab();
  84. };
  85. } logger;
  86. AILogger::Tab::Tab()
  87. {
  88. logger.lvl++;
  89. }
  90. AILogger::Tab::~Tab()
  91. {
  92. logger.lvl--;
  93. }
  94. struct TimeCheck
  95. {
  96. CStopWatch time;
  97. std::string txt;
  98. TimeCheck(crstring TXT) : txt(TXT)
  99. {
  100. }
  101. ~TimeCheck()
  102. {
  103. BNLOG("Time of %s was %d ms.", txt % time.getDiff());
  104. }
  105. };
  106. template<typename T>
  107. void removeDuplicates(std::vector<T> &vec)
  108. {
  109. boost::sort(vec);
  110. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  111. }
  112. template<typename Range, typename Predicate>
  113. void erase_if(Range &vec, Predicate pred)
  114. {
  115. vec.erase(boost::remove_if(vec, pred),vec.end());
  116. }
  117. struct AtScopeExit
  118. {
  119. boost::function<void()> foo;
  120. AtScopeExit(const boost::function<void()> &FOO) : foo(FOO)
  121. {}
  122. ~AtScopeExit()
  123. {
  124. foo();
  125. }
  126. };
  127. void foreach_tile_pos(boost::function<void(const int3& pos)> foo)
  128. {
  129. for(int i = 0; i < cb->getMapSize().x; i++)
  130. for(int j = 0; j < cb->getMapSize().y; j++)
  131. for(int k = 0; k < cb->getMapSize().z; k++)
  132. foo(int3(i,j,k));
  133. }
  134. void foreach_neighbour(const int3 &pos, boost::function<void(const int3& pos)> foo)
  135. {
  136. BOOST_FOREACH(const int3 &dir, dirs)
  137. {
  138. const int3 n = pos + dir;
  139. if(cb->isInTheMap(n))
  140. foo(pos+dir);
  141. }
  142. }
  143. unsigned char &retreiveTileN(std::vector< std::vector< std::vector<unsigned char> > > &vectors, const int3 &pos)
  144. {
  145. return vectors[pos.x][pos.y][pos.z];
  146. }
  147. const unsigned char &retreiveTileN(const std::vector< std::vector< std::vector<unsigned char> > > &vectors, const int3 &pos)
  148. {
  149. return vectors[pos.x][pos.y][pos.z];
  150. }
  151. void foreach_tile(std::vector< std::vector< std::vector<unsigned char> > > &vectors, boost::function<void(unsigned char &in)> foo)
  152. {
  153. for(auto i = vectors.begin(); i != vectors.end(); i++)
  154. for(auto j = i->begin(); j != i->end(); j++)
  155. for(auto z = j->begin(); z != j->end(); z++)
  156. foo(*z);
  157. }
  158. struct ObjInfo
  159. {
  160. int3 pos;
  161. std::string name;
  162. ObjInfo(){}
  163. ObjInfo(const CGObjectInstance *obj)
  164. {
  165. pos = obj->pos;
  166. name = obj->getHoverText();
  167. }
  168. };
  169. std::map<const CGObjectInstance *, ObjInfo> helperObjInfo;
  170. template <typename Container, typename Item>
  171. bool remove_if_present(Container &c, const Item &item)
  172. {
  173. auto i = std::find(c.begin(), c.end(), item);
  174. if (i != c.end())
  175. {
  176. c.erase(i);
  177. return true;
  178. }
  179. return false;
  180. }
  181. template <typename V, typename Item, typename Item2>
  182. bool remove_if_present(std::map<Item,V> & c, const Item2 &item)
  183. {
  184. auto i = c.find(item);
  185. if (i != c.end())
  186. {
  187. c.erase(i);
  188. return true;
  189. }
  190. return false;
  191. }
  192. template <typename Container, typename Pred>
  193. void erase(Container &c, Pred pred)
  194. {
  195. c.erase(boost::remove_if(c, pred), c.end());
  196. }
  197. bool isReachable(const CGObjectInstance *obj)
  198. {
  199. return cb->getPathInfo(obj->visitablePos())->turns < 255;
  200. }
  201. ui64 howManyReinforcementsCanGet(const CGHeroInstance *h, const CGTownInstance *t)
  202. {
  203. ui64 ret = 0;
  204. int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount();
  205. std::vector<const CStackInstance *> toMove;
  206. BOOST_FOREACH(auto const slot, t->Slots())
  207. {
  208. //can be merged woth another stack?
  209. TSlot dst = h->getSlotFor(slot.second->getCreatureID());
  210. if(h->hasStackAtSlot(dst))
  211. ret += t->getPower(slot.first);
  212. else
  213. toMove.push_back(slot.second);
  214. }
  215. boost::sort(toMove, [](const CStackInstance *lhs, const CStackInstance *rhs)
  216. {
  217. return lhs->getPower() < rhs->getPower();
  218. });
  219. BOOST_REVERSE_FOREACH(const CStackInstance *stack, toMove)
  220. {
  221. if(freeHeroSlots)
  222. {
  223. ret += stack->getPower();
  224. freeHeroSlots--;
  225. }
  226. else
  227. break;
  228. }
  229. return ret;
  230. }
  231. std::string strFromInt3(int3 pos)
  232. {
  233. std::ostringstream oss;
  234. oss << pos;
  235. return oss.str();
  236. }
  237. bool isCloser(const CGObjectInstance *lhs, const CGObjectInstance *rhs)
  238. {
  239. const CGPathNode *ln = cb->getPathInfo(lhs->visitablePos()), *rn = cb->getPathInfo(rhs->visitablePos());
  240. if(ln->turns != rn->turns)
  241. return ln->turns < rn->turns;
  242. return (ln->moveRemains > rn->moveRemains);
  243. };
  244. ui64 evaluateDanger(const CGObjectInstance *obj);
  245. ui64 evaluateDanger(crint3 tile)
  246. {
  247. const TerrainTile *t = cb->getTile(tile, false);
  248. if(!t) //we can know about guard but can't check its tile (the edge of fow)
  249. return 190000000; //MUCH
  250. ui64 objectDanger = 0, guardDanger = 0;
  251. if(t->visitable)
  252. objectDanger = evaluateDanger(t->visitableObjects.back());
  253. int3 guardPos = cb->guardingCreaturePosition(tile);
  254. if(guardPos.x >= 0 && guardPos != tile)
  255. guardDanger = evaluateDanger(guardPos);
  256. //TODO mozna odwiedzic blockvis nie ruszajac straznika
  257. return std::max(objectDanger, guardDanger);
  258. return 0;
  259. }
  260. ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor)
  261. {
  262. const TerrainTile *t = cb->getTile(tile, false);
  263. if(!t) //we can know about guard but can't check its tile (the edge of fow)
  264. return 190000000; //MUCH
  265. ui64 objectDanger = 0, guardDanger = 0;
  266. CArmedInstance * dangerousObject;
  267. if(t->visitable)
  268. {
  269. dangerousObject = dynamic_cast<CArmedInstance*>(t->visitableObjects.back());
  270. objectDanger = evaluateDanger(t->visitableObjects.back()); //unguarded objects can also be dangerous or unhandled
  271. if (dangerousObject)
  272. {
  273. objectDanger *= fh->getTacticalAdvantage (visitor, dangerousObject);
  274. }
  275. }
  276. int3 guardPos = cb->guardingCreaturePosition(tile);
  277. if(guardPos.x >= 0 && guardPos != tile)
  278. guardDanger = evaluateDanger(guardPos, visitor);
  279. //TODO mozna odwiedzic blockvis nie ruszajac straznika
  280. return std::max(objectDanger, guardDanger);
  281. return 0;
  282. }
  283. ui64 evaluateDanger(const CGObjectInstance *obj)
  284. {
  285. if(obj->tempOwner < GameConstants::PLAYER_LIMIT && cb->getPlayerRelations(obj->tempOwner, ai->playerID)) //owned or allied objects don't pose any threat
  286. return 0;
  287. switch(obj->ID)
  288. {
  289. case GameConstants::HEROI_TYPE:
  290. {
  291. InfoAboutHero iah;
  292. cb->getHeroInfo(obj, iah);
  293. return iah.army.getStrength();
  294. }
  295. case GameConstants::TOWNI_TYPE:
  296. case Obj::GARRISON: case Obj::GARRISON2: //garrison
  297. {
  298. InfoAboutTown iat;
  299. cb->getTownInfo(obj, iat);
  300. return iat.army.getStrength();
  301. }
  302. case GameConstants::CREI_TYPE:
  303. {
  304. //TODO!!!!!!!!
  305. const CGCreature *cre = dynamic_cast<const CGCreature*>(obj);
  306. return cre->getArmyStrength();
  307. }
  308. case Obj::CREATURE_GENERATOR1:
  309. {
  310. const CGDwelling *d = dynamic_cast<const CGDwelling*>(obj);
  311. return d->getArmyStrength();
  312. }
  313. case Obj::CRYPT: //crypt
  314. case Obj::CREATURE_BANK: //crebank
  315. case Obj::DRAGON_UTOPIA:
  316. case Obj::SHIPWRECK: //shipwreck
  317. case Obj::DERELICT_SHIP: //derelict ship
  318. case Obj::PYRAMID:
  319. return fh->estimateBankDanger (VLC->objh->bankObjToIndex(obj));
  320. default:
  321. return 0;
  322. }
  323. }
  324. bool compareDanger(const CGObjectInstance *lhs, const CGObjectInstance *rhs)
  325. {
  326. return evaluateDanger(lhs) < evaluateDanger(rhs);
  327. }
  328. VCAI::VCAI(void)
  329. {
  330. LOG_ENTRY;
  331. myCb = NULL;
  332. battleAIName = "StupidAI";
  333. makingTurn = NULL;
  334. }
  335. VCAI::~VCAI(void)
  336. {
  337. LOG_ENTRY;
  338. }
  339. void VCAI::availableCreaturesChanged(const CGDwelling *town)
  340. {
  341. NET_EVENT_HANDLER;
  342. LOG_ENTRY;
  343. }
  344. void VCAI::heroMoved(const TryMoveHero & details)
  345. {
  346. NET_EVENT_HANDLER;
  347. LOG_ENTRY;
  348. if(details.result == TryMoveHero::TELEPORTATION)
  349. {
  350. const TerrainTile *t1 = cb->getTile(CGHeroInstance::convertPosition(details.start, false), false),
  351. *t2 = cb->getTile(CGHeroInstance::convertPosition(details.end, false), false);
  352. if(!t1 || !t2) //enemy may have teleported to a tile we don't see
  353. return;
  354. if(t1->visitable && t2->visitable)
  355. {
  356. const CGObjectInstance *o1 = t1->visitableObjects.front(),
  357. *o2 = t2->visitableObjects.front();
  358. if(o1->ID == Obj::SUBTERRANEAN_GATE && o2->ID == Obj::SUBTERRANEAN_GATE)
  359. {
  360. knownSubterraneanGates[o1] = o2;
  361. knownSubterraneanGates[o2] = o1;
  362. }
  363. }
  364. }
  365. }
  366. void VCAI::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
  367. {
  368. NET_EVENT_HANDLER;
  369. LOG_ENTRY;
  370. }
  371. void VCAI::heroInGarrisonChange(const CGTownInstance *town)
  372. {
  373. NET_EVENT_HANDLER;
  374. LOG_ENTRY;
  375. }
  376. void VCAI::centerView(int3 pos, int focusTime)
  377. {
  378. NET_EVENT_HANDLER;
  379. LOG_ENTRY;
  380. }
  381. void VCAI::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  382. {
  383. NET_EVENT_HANDLER;
  384. LOG_ENTRY;
  385. }
  386. void VCAI::artifactAssembled(const ArtifactLocation &al)
  387. {
  388. NET_EVENT_HANDLER;
  389. LOG_ENTRY;
  390. }
  391. void VCAI::showTavernWindow(const CGObjectInstance *townOrTavern)
  392. {
  393. NET_EVENT_HANDLER;
  394. LOG_ENTRY;
  395. }
  396. void VCAI::showThievesGuildWindow (const CGObjectInstance * obj)
  397. {
  398. NET_EVENT_HANDLER;
  399. LOG_ENTRY;
  400. }
  401. void VCAI::playerBlocked(int reason)
  402. {
  403. NET_EVENT_HANDLER;
  404. LOG_ENTRY;
  405. if (reason == PlayerBlocked::UPCOMING_BATTLE)
  406. status.setBattle(UPCOMING_BATTLE);
  407. }
  408. void VCAI::showPuzzleMap()
  409. {
  410. NET_EVENT_HANDLER;
  411. LOG_ENTRY;
  412. }
  413. void VCAI::showShipyardDialog(const IShipyard *obj)
  414. {
  415. NET_EVENT_HANDLER;
  416. LOG_ENTRY;
  417. }
  418. void VCAI::gameOver(ui8 player, bool victory)
  419. {
  420. NET_EVENT_HANDLER;
  421. LOG_ENTRY;
  422. BNLOG("Player %d: I heard that player %d %s.", playerID % (int)player % (victory ? "won" : "lost"));
  423. if(player == playerID)
  424. {
  425. if(victory)
  426. {
  427. tlog0 << "VCAI: I won! Incredible!\n";
  428. tlog0 << "Turn nr " << myCb->getDate() << std::endl;
  429. }
  430. else
  431. {
  432. tlog0 << "VCAI: Player " << (int)player << " lost. It's me. What a disappointment! :(\n";
  433. }
  434. // //let's make Impossible difficulty finally standing to its name :>
  435. // if(myCb->getStartInfo()->difficulty == 4 && !victory)
  436. // {
  437. // //play dirty: crash the whole engine to avoid lose
  438. // //that way AI is unbeatable!
  439. // *(int*)NULL = 666;
  440. // }
  441. // TODO - at least write some insults on stdout
  442. finish();
  443. }
  444. }
  445. void VCAI::artifactPut(const ArtifactLocation &al)
  446. {
  447. NET_EVENT_HANDLER;
  448. LOG_ENTRY;
  449. }
  450. void VCAI::artifactRemoved(const ArtifactLocation &al)
  451. {
  452. NET_EVENT_HANDLER;
  453. LOG_ENTRY;
  454. }
  455. void VCAI::stacksErased(const StackLocation &location)
  456. {
  457. NET_EVENT_HANDLER;
  458. LOG_ENTRY;
  459. }
  460. void VCAI::artifactDisassembled(const ArtifactLocation &al)
  461. {
  462. NET_EVENT_HANDLER;
  463. LOG_ENTRY;
  464. }
  465. void VCAI::heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start)
  466. {
  467. NET_EVENT_HANDLER;
  468. LOG_ENTRY;
  469. if (start)
  470. {
  471. visitedObject = const_cast<CGObjectInstance *>(visitedObj); // remember the object and wait for return
  472. markObjectVisited (visitedObj);
  473. }
  474. }
  475. void VCAI::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
  476. {
  477. NET_EVENT_HANDLER;
  478. LOG_ENTRY;
  479. }
  480. void VCAI::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  481. {
  482. NET_EVENT_HANDLER;
  483. LOG_ENTRY;
  484. //buildArmyIn(town);
  485. //moveCreaturesToHero(town);
  486. }
  487. void VCAI::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
  488. {
  489. NET_EVENT_HANDLER;
  490. LOG_ENTRY;
  491. // BOOST_FOREACH(int3 tile, pos)
  492. // BOOST_FOREACH(const CGObjectInstance *obj, cb->getVisitableObjs(tile))
  493. // remove_if_present(visitableObjs, obj);
  494. visitableObjs.erase(boost::remove_if(visitableObjs, [&](const CGObjectInstance *obj){return !myCb->getObj(obj->id);}), visitableObjs.end());
  495. }
  496. void VCAI::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
  497. {
  498. NET_EVENT_HANDLER;
  499. LOG_ENTRY;
  500. BOOST_FOREACH(int3 tile, pos)
  501. BOOST_FOREACH(const CGObjectInstance *obj, myCb->getVisitableObjs(tile))
  502. addVisitableObj(obj);
  503. }
  504. void VCAI::heroExchangeStarted(si32 hero1, si32 hero2)
  505. {
  506. NET_EVENT_HANDLER;
  507. LOG_ENTRY;
  508. }
  509. void VCAI::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
  510. {
  511. NET_EVENT_HANDLER;
  512. LOG_ENTRY;
  513. }
  514. void VCAI::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
  515. {
  516. NET_EVENT_HANDLER;
  517. LOG_ENTRY;
  518. }
  519. void VCAI::heroMovePointsChanged(const CGHeroInstance * hero)
  520. {
  521. NET_EVENT_HANDLER;
  522. LOG_ENTRY;
  523. }
  524. void VCAI::stackChangedType(const StackLocation &location, const CCreature &newType)
  525. {
  526. NET_EVENT_HANDLER;
  527. LOG_ENTRY;
  528. }
  529. void VCAI::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
  530. {
  531. NET_EVENT_HANDLER;
  532. LOG_ENTRY;
  533. }
  534. void VCAI::newObject(const CGObjectInstance * obj)
  535. {
  536. NET_EVENT_HANDLER;
  537. LOG_ENTRY;
  538. if(obj->isVisitable())
  539. addVisitableObj(obj);
  540. }
  541. void VCAI::objectRemoved(const CGObjectInstance *obj)
  542. {
  543. NET_EVENT_HANDLER;
  544. LOG_ENTRY;
  545. if(remove_if_present(visitableObjs, obj))
  546. assert(obj->isVisitable());
  547. }
  548. void VCAI::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
  549. {
  550. NET_EVENT_HANDLER;
  551. LOG_ENTRY;
  552. }
  553. void VCAI::playerBonusChanged(const Bonus &bonus, bool gain)
  554. {
  555. NET_EVENT_HANDLER;
  556. LOG_ENTRY;
  557. }
  558. void VCAI::newStackInserted(const StackLocation &location, const CStackInstance &stack)
  559. {
  560. NET_EVENT_HANDLER;
  561. LOG_ENTRY;
  562. }
  563. void VCAI::heroCreated(const CGHeroInstance*)
  564. {
  565. NET_EVENT_HANDLER;
  566. LOG_ENTRY;
  567. }
  568. void VCAI::advmapSpellCast(const CGHeroInstance * caster, int spellID)
  569. {
  570. NET_EVENT_HANDLER;
  571. LOG_ENTRY;
  572. }
  573. void VCAI::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
  574. {
  575. NET_EVENT_HANDLER;
  576. LOG_ENTRY;
  577. }
  578. void VCAI::requestRealized(PackageApplied *pa)
  579. {
  580. NET_EVENT_HANDLER;
  581. LOG_ENTRY;
  582. if(status.haveTurn())
  583. {
  584. if(pa->packType == typeList.getTypeID<EndTurn>())
  585. if(pa->result)
  586. status.madeTurn();
  587. }
  588. if(pa->packType == typeList.getTypeID<QueryReply>())
  589. {
  590. status.removeQuery();
  591. }
  592. }
  593. void VCAI::receivedResource(int type, int val)
  594. {
  595. NET_EVENT_HANDLER;
  596. LOG_ENTRY;
  597. }
  598. void VCAI::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
  599. {
  600. NET_EVENT_HANDLER;
  601. LOG_ENTRY;
  602. }
  603. void VCAI::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
  604. {
  605. NET_EVENT_HANDLER;
  606. LOG_ENTRY;
  607. }
  608. void VCAI::heroManaPointsChanged(const CGHeroInstance * hero)
  609. {
  610. NET_EVENT_HANDLER;
  611. LOG_ENTRY;
  612. }
  613. void VCAI::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
  614. {
  615. NET_EVENT_HANDLER;
  616. LOG_ENTRY;
  617. }
  618. void VCAI::battleResultsApplied()
  619. {
  620. NET_EVENT_HANDLER;
  621. LOG_ENTRY;
  622. assert(status.getBattle() == ENDING_BATTLE);
  623. status.setBattle(NO_BATTLE);
  624. }
  625. void VCAI::objectPropertyChanged(const SetObjectProperty * sop)
  626. {
  627. NET_EVENT_HANDLER;
  628. LOG_ENTRY;
  629. if(sop->what == ObjProperty::OWNER)
  630. {
  631. if(sop->val == playerID)
  632. remove_if_present(visitableObjs, myCb->getObj(sop->id));
  633. //TODO restore lost obj
  634. }
  635. }
  636. void VCAI::buildChanged(const CGTownInstance *town, int buildingID, int what)
  637. {
  638. NET_EVENT_HANDLER;
  639. LOG_ENTRY;
  640. }
  641. void VCAI::heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain)
  642. {
  643. NET_EVENT_HANDLER;
  644. LOG_ENTRY;
  645. }
  646. void VCAI::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
  647. {
  648. NET_EVENT_HANDLER;
  649. LOG_ENTRY;
  650. }
  651. void VCAI::init(CCallback * CB)
  652. {
  653. myCb = CB;
  654. cbc = CB;
  655. NET_EVENT_HANDLER;
  656. LOG_ENTRY;
  657. playerID = myCb->getMyColor();
  658. myCb->waitTillRealize = true;
  659. myCb->unlockGsWhenWaiting = true;
  660. if(!fh)
  661. fh = new FuzzyHelper();
  662. retreiveVisitableObjs(visitableObjs);
  663. }
  664. void VCAI::yourTurn()
  665. {
  666. NET_EVENT_HANDLER;
  667. LOG_ENTRY;
  668. status.startedTurn();
  669. makingTurn = new boost::thread(&VCAI::makeTurn, this);
  670. }
  671. void VCAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  672. {
  673. NET_EVENT_HANDLER;
  674. LOG_ENTRY;
  675. status.addQuery();
  676. callback(0);
  677. }
  678. void VCAI::showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel)
  679. {
  680. NET_EVENT_HANDLER;
  681. LOG_ENTRY;
  682. int sel = 0;
  683. status.addQuery();
  684. if(selection) //select from multiple components -> take the last one (they're indexed [1-size])
  685. sel = components.size();
  686. if(!selection && cancel) //yes&no -> always answer yes, we are a brave AI :)
  687. sel = 1;
  688. cb->selectionMade(sel, askID);
  689. }
  690. void VCAI::showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd)
  691. {
  692. NET_EVENT_HANDLER;
  693. LOG_ENTRY;
  694. status.addQuery();
  695. //you can't request action from action-response thread
  696. //pickBestCreatures (down, up);
  697. onEnd();
  698. }
  699. void VCAI::serialize(COSer<CSaveFile> &h, const int version)
  700. {
  701. NET_EVENT_HANDLER;
  702. LOG_ENTRY;
  703. }
  704. void VCAI::serialize(CISer<CLoadFile> &h, const int version)
  705. {
  706. NET_EVENT_HANDLER;
  707. LOG_ENTRY;
  708. }
  709. void makePossibleUpgrades(const CArmedInstance *obj)
  710. {
  711. if(!obj)
  712. return;
  713. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  714. {
  715. if(const CStackInstance *s = obj->getStackPtr(i))
  716. {
  717. UpgradeInfo ui;
  718. cb->getUpgradeInfo(obj, i, ui);
  719. if(ui.oldID >= 0 && cb->getResourceAmount().canAfford(ui.cost[0] * s->count))
  720. {
  721. cb->upgradeCreature(obj, i, ui.newID[0]);
  722. }
  723. }
  724. }
  725. }
  726. void VCAI::makeTurn()
  727. {
  728. MAKING_TURN;
  729. boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
  730. setThreadName(-1, "VCAI::makeTurn");
  731. BNLOG("Player %d starting turn", playerID);
  732. INDENT;
  733. switch(cb->getDate(1))
  734. {
  735. case 1:
  736. {
  737. townVisitsThisWeek.clear();
  738. std::vector<const CGObjectInstance *> objs;
  739. retreiveVisitableObjs(objs, true);
  740. BOOST_FOREACH(const CGObjectInstance *obj, objs)
  741. {
  742. if (isWeeklyRevisitable(obj))
  743. {
  744. if (!vstd::contains(visitableObjs, obj))
  745. visitableObjs.push_back(obj);
  746. auto o = std::find (alreadyVisited.begin(), alreadyVisited.end(), obj);
  747. if (o != alreadyVisited.end())
  748. alreadyVisited.erase(o);
  749. }
  750. }
  751. }
  752. break;
  753. case 7: //reconsider strategy
  754. {
  755. const CGHeroInstance * h = primaryHero();
  756. if (h) //check if our primary hero can ahndle danger
  757. {
  758. ui64 totalDanger = 0;
  759. int dangerousObjects = 0;
  760. std::vector<const CGObjectInstance *> objs;
  761. retreiveVisitableObjs(objs, false);
  762. BOOST_FOREACH (auto obj, objs)
  763. {
  764. if (evaluateDanger(obj)) //potentilaly dnagerous
  765. {
  766. totalDanger += evaluateDanger (obj->visitablePos(), h);
  767. ++dangerousObjects;
  768. }
  769. }
  770. if (dangerousObjects && totalDanger / dangerousObjects > h->getHeroStrength())
  771. {
  772. setGoal (h, CGoal(GATHER_ARMY).sethero(h));
  773. }
  774. }
  775. }
  776. break;
  777. }
  778. if(cb->getSelectedHero())
  779. cb->recalculatePaths();
  780. makeTurnInternal();
  781. vstd::clear_pointer(makingTurn);
  782. return;
  783. }
  784. void VCAI::makeTurnInternal()
  785. {
  786. saving = 0;
  787. //it looks messy here, but it's better to have armed heroes before attempting realizing goals
  788. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  789. moveCreaturesToHero(t);
  790. try
  791. {
  792. striveToGoal(CGoal(WIN));
  793. for (auto hg = lockedHeroes.begin(); hg != lockedHeroes.end(); hg++) //continue our goals
  794. {
  795. striveToGoal (hg->second);
  796. }
  797. striveToGoal(CGoal(BUILD)); //TODO: smarter building management
  798. }
  799. catch(boost::thread_interrupted &e)
  800. {
  801. tlog0 << "Making turn thread has been interrupted. We'll end without calling endTurn.\n";
  802. return;
  803. }
  804. catch(std::exception &e)
  805. {
  806. tlog0 << "Making turn thread has caught an exception: " << e.what() << "\n";
  807. }
  808. endTurn();
  809. }
  810. bool VCAI::goVisitObj(const CGObjectInstance * obj, const CGHeroInstance * h)
  811. {
  812. int3 dst = obj->visitablePos();
  813. BNLOG("%s will try to visit %s at (%s)", h->name % obj->hoverName % strFromInt3(dst));
  814. return moveHeroToTile(dst, h);
  815. }
  816. void VCAI::performObjectInteraction(const CGObjectInstance * obj, const CGHeroInstance * h)
  817. {
  818. switch (obj->ID)
  819. {
  820. case Obj::CREATURE_GENERATOR1:
  821. recruitCreatures(dynamic_cast<const CGDwelling *>(obj));
  822. break;
  823. }
  824. }
  825. void VCAI::moveCreaturesToHero(const CGTownInstance * t)
  826. {
  827. if(t->visitingHero && t->armedGarrison())
  828. {
  829. pickBestCreatures (t->visitingHero, t);
  830. }
  831. }
  832. void VCAI::pickBestCreatures(const CArmedInstance * army, const CArmedInstance * source)
  833. {
  834. //TODO - what if source is a hero (the last stack problem) -> it'd good to create a single stack of weakest cre
  835. const CArmedInstance *armies[] = {army, source};
  836. //we calculate total strength for each creature type available in armies
  837. std::map<const CCreature*, int> creToPower;
  838. BOOST_FOREACH(auto armyPtr, armies)
  839. BOOST_FOREACH(auto &i, armyPtr->Slots())
  840. creToPower[i.second->type] += i.second->getPower();
  841. //TODO - consider more than just power (ie morale penalty, hero specialty in certain stacks, etc)
  842. std::vector<const CCreature *> bestArmy; //types that'll be in final dst army
  843. for (int i = 0; i < GameConstants::ARMY_SIZE; i++) //pick the creatures from which we can get most power, as many as dest can fit
  844. {
  845. typedef const std::pair<const CCreature*, int> &CrePowerPair;
  846. auto creIt = boost::max_element(creToPower, [](CrePowerPair lhs, CrePowerPair rhs)
  847. {
  848. return lhs.second < rhs.second;
  849. });
  850. bestArmy.push_back(creIt->first);
  851. creToPower.erase(creIt);
  852. if(creToPower.empty())
  853. break;
  854. }
  855. //foreach best type -> iterate over slots in both armies and if it's the appropriate type, send it to the slot where it belongs
  856. for (int i = 0; i < bestArmy.size(); i++) //i-th strongest creature type will go to i-th slot
  857. BOOST_FOREACH(auto armyPtr, armies)
  858. for (int j = 0; j < GameConstants::ARMY_SIZE; j++)
  859. if(armyPtr->getCreature(j) == bestArmy[i] && (i != j || armyPtr != army)) //it's a searched creature not in dst slot
  860. cb->mergeOrSwapStacks(armyPtr, army, j, i);
  861. //TODO - having now strongest possible army, we may want to think about arranging stacks
  862. }
  863. void VCAI::recruitCreatures(const CGDwelling * d)
  864. {
  865. for(int i = 0; i < d->creatures.size(); i++)
  866. {
  867. if(!d->creatures[i].second.size())
  868. continue;
  869. int count = d->creatures[i].first;
  870. int creID = d->creatures[i].second.back();
  871. // const CCreature *c = VLC->creh->creatures[creID];
  872. // if(containsSavedRes(c->cost))
  873. // continue;
  874. TResources myRes = cb->getResourceAmount();
  875. myRes[Res::GOLD] -= GOLD_RESERVE;
  876. amin(count, myRes / VLC->creh->creatures[creID]->cost);
  877. if(count > 0)
  878. cb->recruitCreatures(d, creID, count, i);
  879. }
  880. }
  881. void VCAI::buildStructure(const CGTownInstance * t)
  882. {
  883. //TODO make *real* town development system
  884. const int buildings[] = {5, 11, 14, 16, 0, 12, 7, 8, 9, 13, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  885. 39, 40, 41, 42, 43, 1, 2, 3, 4, 17, 18, 19, 21, 22, 23};
  886. for(int i = 0; i < ARRAY_COUNT(buildings); i++)
  887. {
  888. if(t->hasBuilt(buildings[i]))
  889. continue;
  890. const CBuilding *b = VLC->buildh->buildings[t->subID][buildings[i]];
  891. int canBuild = cb->canBuildStructure(t, buildings[i]);
  892. if(canBuild == EBuildingState::ALLOWED)
  893. {
  894. if(!containsSavedRes(b->resources))
  895. {
  896. BNLOG("Player %d will build %s in town of %s at %s", playerID % b->Name() % t->name % t->pos);
  897. cb->buildBuilding(t, buildings[i]);
  898. }
  899. break;
  900. }
  901. else if(canBuild == EBuildingState::NO_RESOURCES)
  902. {
  903. TResources mine = cb->getResourceAmount(), cost = VLC->buildh->buildings[t->subID][buildings[i]]->resources,
  904. income = estimateIncome();
  905. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  906. {
  907. int diff = mine[i] - cost[i] + income[i];
  908. if(diff < 0)
  909. saving[i] = 1;
  910. }
  911. continue;
  912. }
  913. }
  914. }
  915. bool isSafeToVisit(const CGHeroInstance *h, crint3 tile)
  916. {
  917. const ui64 heroStrength = h->getTotalStrength(),
  918. dangerStrength = evaluateDanger(tile, h);
  919. if(dangerStrength)
  920. {
  921. if(heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength)
  922. {
  923. BNLOG("It's, safe for %s to visit tile %s", h->name % tile);
  924. return true;
  925. }
  926. else
  927. return false;
  928. }
  929. return true; //there's no danger
  930. }
  931. std::vector<const CGObjectInstance *> VCAI::getPossibleDestinations(const CGHeroInstance *h)
  932. {
  933. validateVisitableObjs();
  934. std::vector<const CGObjectInstance *> possibleDestinations;
  935. BOOST_FOREACH(const CGObjectInstance *obj, visitableObjs)
  936. {
  937. if(cb->getPathInfo(obj->visitablePos())->reachable() && !obj->wasVisited(playerID) &&
  938. (obj->tempOwner != playerID || isWeeklyRevisitable(obj))) //flag or get weekly resources / creatures
  939. possibleDestinations.push_back(obj);
  940. }
  941. boost::sort(possibleDestinations, isCloser);
  942. possibleDestinations.erase(boost::remove_if(possibleDestinations, [&](const CGObjectInstance *obj) -> bool
  943. {
  944. if(vstd::contains(alreadyVisited, obj))
  945. return true;
  946. if(!isSafeToVisit(h, obj->visitablePos()))
  947. return true;
  948. if (!shouldVisit(h, obj))
  949. return true;
  950. return false;
  951. }),possibleDestinations.end());
  952. return possibleDestinations;
  953. }
  954. void VCAI::wander(const CGHeroInstance * h)
  955. {
  956. while(1)
  957. {
  958. auto dests = getPossibleDestinations(h);
  959. if(!dests.size())
  960. {
  961. PNLOG("Nowhere more to go...\n");
  962. setGoal (h, INVALID);
  963. break;
  964. }
  965. const CGObjectInstance * obj = dests.front();
  966. if(!goVisitObj(obj, h))
  967. {
  968. BNLOG("Hero %s apparently used all MPs (%d left)\n", h->name % h->movement);
  969. markObjectVisited(obj); //reserve that object - we predict it will be reached soon
  970. setGoal(h, CGoal(VISIT_TILE).sethero(h).settile(obj->visitablePos()));
  971. break;
  972. }
  973. if(h->visitedTown)
  974. {
  975. townVisitsThisWeek[h].push_back(h->visitedTown);
  976. buildArmyIn(h->visitedTown);
  977. break;
  978. }
  979. }
  980. }
  981. void VCAI::setGoal (const CGHeroInstance *h, const CGoal goal)
  982. { //TODO: check for presence?
  983. lockedHeroes[h] = goal;
  984. }
  985. void VCAI::setGoal (const CGHeroInstance *h, EGoals goalType)
  986. {
  987. lockedHeroes[h] = CGoal(goalType);
  988. }
  989. void VCAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  990. {
  991. assert(playerID > GameConstants::PLAYER_LIMIT || status.getBattle() == UPCOMING_BATTLE);
  992. status.setBattle(ONGOING_BATTLE);
  993. const TerrainTile *t = myCb->getTile(tile); //may be NULL in some very are cases -> eg. visited monolith and fighting with an enemy at the FoW covered exit
  994. battlename = boost::str(boost::format("battle of %s attacking %s at %s") % (hero1 ? hero1->name : "a army") % (t ? t->visitableObjects.back()->hoverName : "unknown enemy") % tile);
  995. CAdventureAI::battleStart(army1, army2, tile, hero1, hero2, side);
  996. }
  997. void VCAI::battleEnd(const BattleResult *br)
  998. {
  999. assert(status.getBattle() == ONGOING_BATTLE);
  1000. status.setBattle(ENDING_BATTLE);
  1001. bool won = br->winner == myCb->battleGetMySide();
  1002. BNLOG("Player %d: I %s the %s!", playerID % (won ? "won" : "lost") % battlename);
  1003. battlename.clear();
  1004. CAdventureAI::battleEnd(br);
  1005. }
  1006. void VCAI::waitTillFree()
  1007. {
  1008. auto unlock = vstd::makeUnlockSharedGuard(cb->getGsMutex());
  1009. status.waitTillFree();
  1010. }
  1011. void VCAI::markObjectVisited (const CGObjectInstance *obj)
  1012. {
  1013. if(dynamic_cast<const CGVisitableOPH *>(obj) || //we may want to wisit it with another hero
  1014. dynamic_cast<const CGBonusingObject *>(obj) || //or another time
  1015. (obj->ID == Obj::MONSTER))
  1016. return;
  1017. alreadyVisited.push_back(obj);
  1018. }
  1019. void VCAI::validateVisitableObjs()
  1020. {
  1021. std::vector<const CGObjectInstance *> hlp;
  1022. retreiveVisitableObjs(hlp, true);
  1023. start:
  1024. BOOST_FOREACH(const CGObjectInstance *obj, visitableObjs)
  1025. if(!vstd::contains(hlp, obj))
  1026. {
  1027. tlog1 << helperObjInfo[obj].name << " at " << helperObjInfo[obj].pos << " shouldn't be on list!\n";
  1028. remove_if_present(visitableObjs, obj);
  1029. goto start;
  1030. }
  1031. }
  1032. void VCAI::retreiveVisitableObjs(std::vector<const CGObjectInstance *> &out, bool includeOwned /*= false*/) const
  1033. {
  1034. for(int i = 0; i < cb->getMapSize().x; i++)
  1035. for(int j = 0; j < cb->getMapSize().y; j++)
  1036. for(int k = 0; k < cb->getMapSize().z; k++)
  1037. if(const TerrainTile *t = cb->getTile(int3(i,j,k), false))
  1038. {
  1039. BOOST_FOREACH(const CGObjectInstance *obj, t->visitableObjects)
  1040. {
  1041. if(includeOwned || obj->tempOwner != playerID)
  1042. out.push_back(obj);
  1043. }
  1044. }
  1045. }
  1046. std::vector<const CGObjectInstance *> VCAI::getFlaggedObjects() const
  1047. {
  1048. std::vector<const CGObjectInstance *> ret;
  1049. retreiveVisitableObjs(ret, true);
  1050. erase_if(ret, [](const CGObjectInstance *obj)
  1051. {
  1052. return obj->tempOwner != ai->playerID;
  1053. });
  1054. return ret;
  1055. }
  1056. void VCAI::addVisitableObj(const CGObjectInstance *obj)
  1057. {
  1058. visitableObjs.push_back(obj);
  1059. helperObjInfo[obj] = ObjInfo(obj);
  1060. }
  1061. const CGObjectInstance * VCAI::lookForArt(int aid) const
  1062. {
  1063. BOOST_FOREACH(const CGObjectInstance *obj, ai->visitableObjs)
  1064. {
  1065. if(obj->ID == 5 && obj->subID == aid)
  1066. return obj;
  1067. }
  1068. return NULL;
  1069. //TODO what if more than one artifact is available? return them all or some slection criteria
  1070. }
  1071. bool VCAI::isAccessible(const int3 &pos)
  1072. {
  1073. //TODO precalculate for speed
  1074. BOOST_FOREACH(const CGHeroInstance *h, cb->getHeroesInfo())
  1075. {
  1076. if(isAccessibleForHero(pos, h))
  1077. return true;
  1078. }
  1079. return false;
  1080. }
  1081. const CGHeroInstance * VCAI::getHeroWithGrail() const
  1082. {
  1083. BOOST_FOREACH(const CGHeroInstance *h, cb->getHeroesInfo())
  1084. if(h->hasArt(2)) //grail
  1085. return h;
  1086. return NULL;
  1087. }
  1088. const CGObjectInstance * VCAI::getUnvisitedObj(const boost::function<bool(const CGObjectInstance *)> &predicate)
  1089. {
  1090. //TODO smarter definition of unvisited
  1091. BOOST_FOREACH(const CGObjectInstance *obj, visitableObjs)
  1092. if(predicate(obj) && !vstd::contains(alreadyVisited, obj))
  1093. return obj;
  1094. return NULL;
  1095. }
  1096. bool VCAI::isAccessibleForHero(const int3 & pos, const CGHeroInstance * h) const
  1097. {
  1098. cb->setSelection(h);
  1099. return cb->getPathInfo(pos)->reachable();
  1100. }
  1101. class cannotFulfillGoalException : public std::exception
  1102. {
  1103. std::string msg;
  1104. public:
  1105. explicit cannotFulfillGoalException(crstring _Message) : msg(_Message)
  1106. {
  1107. }
  1108. virtual ~cannotFulfillGoalException() throw ()
  1109. {
  1110. };
  1111. const char *what() const throw () OVERRIDE
  1112. {
  1113. return msg.c_str();
  1114. }
  1115. };
  1116. bool VCAI::moveHeroToTile(int3 dst, const CGHeroInstance * h)
  1117. {
  1118. visitedObject = NULL;
  1119. int3 startHpos = h->visitablePos();
  1120. bool ret = false;
  1121. if(startHpos == dst)
  1122. {
  1123. assert(cb->getTile(dst)->visitableObjects.size() > 1); //there's no point in revisiting tile where there is no visitable object
  1124. cb->moveHero(h,CGHeroInstance::convertPosition(dst, true));
  1125. waitTillFree(); //movement may cause battle or blocking dialog
  1126. ret = true;
  1127. }
  1128. else
  1129. {
  1130. CGPath path;
  1131. cb->getPath2(dst, path);
  1132. if(path.nodes.empty())
  1133. {
  1134. tlog1 << "Hero " << h->name << " cannot reach " << dst << std::endl;
  1135. throw std::runtime_error("Wrong move order!");
  1136. }
  1137. int i=path.nodes.size()-1;
  1138. for(; i>0; i--)
  1139. {
  1140. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  1141. if(path.nodes[i-1].turns)
  1142. {
  1143. //blockedHeroes.insert(h); //to avoid attempts of moving heroes with very little MPs
  1144. break;
  1145. }
  1146. int3 endpos = path.nodes[i-1].coord;
  1147. if(endpos == h->visitablePos())
  1148. continue;
  1149. // if(i > 1)
  1150. // {
  1151. // int3 afterEndPos = path.nodes[i-2].coord;
  1152. // if(afterEndPos.z != endpos.z)
  1153. //
  1154. // }
  1155. //tlog0 << "Moving " << h->name << " from " << h->getPosition() << " to " << endpos << std::endl;
  1156. cb->moveHero(h,CGHeroInstance::convertPosition(endpos, true));
  1157. waitTillFree(); //movement may cause battle or blocking dialog
  1158. boost::this_thread::interruption_point();
  1159. if(h->tempOwner != playerID) //we lost hero
  1160. {
  1161. remove_if_present(lockedHeroes, h);
  1162. break;
  1163. }
  1164. }
  1165. ret = !i;
  1166. }
  1167. if (visitedObject) //we step into something interesting
  1168. performObjectInteraction (visitedObject, h);
  1169. if(h->tempOwner == playerID) //lost hero after last move
  1170. cb->recalculatePaths();
  1171. BNLOG("Hero %s moved from %s to %s", h->name % startHpos % h->visitablePos());
  1172. return ret;
  1173. }
  1174. int howManyTilesWillBeDiscovered(const int3 &pos, int radious)
  1175. {
  1176. int ret = 0;
  1177. for(int x = pos.x - radious; x <= pos.x + radious; x++)
  1178. {
  1179. for(int y = pos.y - radious; y <= pos.y + radious; y++)
  1180. {
  1181. int3 npos = int3(x,y,pos.z);
  1182. if(cb->isInTheMap(npos) && pos.dist2d(npos) - 0.5 < radious && !cb->isVisible(npos))
  1183. {
  1184. ret++;
  1185. }
  1186. }
  1187. }
  1188. return ret;
  1189. }
  1190. int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir)
  1191. {
  1192. return howManyTilesWillBeDiscovered(pos + dir, radious);
  1193. }
  1194. void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out)
  1195. {
  1196. BOOST_FOREACH(const int3 &tile, tiles)
  1197. {
  1198. foreach_neighbour(tile, [&](int3 neighbour)
  1199. {
  1200. if(cb->isVisible(neighbour))
  1201. out.push_back(neighbour);
  1202. });
  1203. }
  1204. }
  1205. void VCAI::tryRealize(CGoal g)
  1206. {
  1207. BNLOG("Attempting realizing goal with code %d", g.goalType);
  1208. switch(g.goalType)
  1209. {
  1210. case EXPLORE:
  1211. {
  1212. assert(0); //this goal is not elementar!
  1213. }
  1214. break;
  1215. case RECRUIT_HERO:
  1216. {
  1217. if(const CGTownInstance *t = findTownWithTavern())
  1218. {
  1219. //TODO co jesli nie ma dostepnego bohatera?
  1220. //TODO jezeli miasto jest zablokowane, sprobowac oczyscic wejscie
  1221. cb->recruitHero(t, cb->getAvailableHeroes(t)[0]);
  1222. }
  1223. //TODO karkolomna alternatywa - tawerna na mapie przygod lub wiezienie (nie wiem, czy warto?)
  1224. }
  1225. break;
  1226. case VISIT_TILE:
  1227. {
  1228. if(!g.hero->movement)
  1229. throw cannotFulfillGoalException("Cannot visit tile: hero is out of MPs!");
  1230. if(!g.isBlockedBorderGate(g.tile))
  1231. {
  1232. if (ai->moveHeroToTile(g.tile, g.hero));
  1233. setGoal (g.hero, INVALID); //this hero reached target and no goal
  1234. }
  1235. else
  1236. throw cannotFulfillGoalException("There's a blocked gate!");
  1237. }
  1238. break;
  1239. case BUILD_STRUCTURE:
  1240. {
  1241. const CGTownInstance *t = g.town;
  1242. if(!t && g.hero)
  1243. t = g.hero->visitedTown;
  1244. if(!t)
  1245. {
  1246. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1247. {
  1248. switch(cb->canBuildStructure(t, g.bid))
  1249. {
  1250. case EBuildingState::ALLOWED:
  1251. cb->buildBuilding(t, g.bid);
  1252. return;
  1253. default:
  1254. break;
  1255. }
  1256. }
  1257. }
  1258. else if(cb->canBuildStructure(t, g.bid) == EBuildingState::ALLOWED)
  1259. {
  1260. cb->buildBuilding(t, g.bid);
  1261. return;
  1262. }
  1263. throw cannotFulfillGoalException("Cannot build a given structure!");
  1264. }
  1265. break;
  1266. case DIG_AT_TILE:
  1267. {
  1268. assert(g.hero->visitablePos() == g.tile);
  1269. if (g.hero->diggingStatus() == CGHeroInstance::CAN_DIG)
  1270. {
  1271. cb->dig (g.hero);
  1272. setGoal (g.hero, INVALID); // finished digging
  1273. }
  1274. else
  1275. {
  1276. ai->lockedHeroes[g.hero] = g; //hero who tries to dig shouldn't do anything else
  1277. throw cannotFulfillGoalException("A hero can't dig!\n");
  1278. }
  1279. }
  1280. break;
  1281. case COLLECT_RES:
  1282. if(const CGObjectInstance *obj = cb->getObj(g.objid, false))
  1283. {
  1284. if(const IMarket *m = IMarket::castFrom(obj, false))
  1285. {
  1286. for (int i = 0; i < ACTUAL_RESOURCE_COUNT; i++)
  1287. {
  1288. if(i == g.resID) continue;
  1289. int toGive, toGet;
  1290. m->getOffer(i, g.resID, toGive, toGet, EMarketMode::RESOURCE_RESOURCE);
  1291. toGive = toGive * (cb->getResourceAmount(i) / toGive);
  1292. cb->trade(obj, EMarketMode::RESOURCE_RESOURCE, i, g.resID, toGive);
  1293. if(cb->getResourceAmount(g.resID) >= g.value)
  1294. return;
  1295. }
  1296. }
  1297. else
  1298. {
  1299. throw cannotFulfillGoalException("I don't know how to use this object to raise resources!");
  1300. }
  1301. }
  1302. else
  1303. {
  1304. saving[g.resID] = 1;
  1305. throw cannotFulfillGoalException("No object that could be used to raise resources!");
  1306. }
  1307. case CONQUER:
  1308. case GATHER_ARMY:
  1309. case BOOST_HERO:
  1310. // TODO: conquer??
  1311. throw cannotFulfillGoalException("I don't know how to fulfill this!");
  1312. case BUILD:
  1313. performTypicalActions();
  1314. throw cannotFulfillGoalException("BUILD has been realized as much as possible.");
  1315. case INVALID:
  1316. throw cannotFulfillGoalException("I don't know how to fulfill this!");
  1317. default:
  1318. throw cannotFulfillGoalException("Unknown type of goal !");
  1319. }
  1320. }
  1321. const CGTownInstance * VCAI::findTownWithTavern() const
  1322. {
  1323. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1324. if(vstd::contains(t->builtBuildings, EBuilding::TAVERN) && !t->visitingHero)
  1325. return t;
  1326. return NULL;
  1327. }
  1328. std::vector<const CGHeroInstance *> VCAI::getUnblockedHeroes() const
  1329. {
  1330. std::vector<const CGHeroInstance *> ret = cb->getHeroesInfo();
  1331. BOOST_FOREACH(auto h, lockedHeroes)
  1332. {
  1333. if (!h.second.invalid()) //we can use heroes without valid goal
  1334. remove_if_present(ret, h.first);
  1335. }
  1336. return ret;
  1337. }
  1338. const CGHeroInstance * VCAI::primaryHero() const
  1339. {
  1340. auto hs = cb->getHeroesInfo();
  1341. boost::sort(hs, compareHeroStrength);
  1342. if(hs.empty())
  1343. return NULL;
  1344. return hs.back();
  1345. }
  1346. void VCAI::endTurn()
  1347. {
  1348. tlog4 << "Player " << playerID << " ends turn\n";
  1349. if(!status.haveTurn())
  1350. {
  1351. tlog1 << "Not having turn at the end of turn???\n";
  1352. }
  1353. do
  1354. {
  1355. cb->endTurn();
  1356. } while(status.haveTurn()); //for some reasons, our request may fail -> stop requesting end of turn only after we've received a confirmation that it's over
  1357. tlog4 << "Player " << playerID << " ended turn\n";
  1358. }
  1359. void VCAI::striveToGoal(const CGoal &ultimateGoal)
  1360. {
  1361. if (ultimateGoal.invalid())
  1362. return;
  1363. while(1)
  1364. {
  1365. CGoal goal = ultimateGoal;
  1366. BNLOG("Striving to goal of type %d", ultimateGoal.goalType);
  1367. int maxGoals = 100; //preventing deadlock for mutually dependent goals
  1368. while(!goal.isElementar && maxGoals)
  1369. {
  1370. INDENT;
  1371. BNLOG("Considering goal %s", goalName(goal.goalType));
  1372. try
  1373. {
  1374. boost::this_thread::interruption_point();
  1375. goal = goal.whatToDoToAchieve();
  1376. --maxGoals;
  1377. }
  1378. catch(std::exception &e)
  1379. {
  1380. BNLOG("Goal %s decomposition failed: %s", goalName(goal.goalType) % e.what());
  1381. return;
  1382. }
  1383. }
  1384. try
  1385. {
  1386. boost::this_thread::interruption_point();
  1387. if (goal.hero) //lock this hero to fulfill ultimate goal
  1388. {
  1389. if (maxGoals)
  1390. {
  1391. setGoal (goal.hero, goal);
  1392. }
  1393. else
  1394. {
  1395. setGoal (goal.hero, INVALID); // we seemingly don't know what to do with hero
  1396. }
  1397. }
  1398. tryRealize(goal);
  1399. boost::this_thread::interruption_point();
  1400. }
  1401. catch(boost::thread_interrupted &e)
  1402. {
  1403. BNLOG("Player %d: Making turn thread received an interruption!", playerID);
  1404. throw; //rethrow, we want to truly end this thread
  1405. }
  1406. catch(std::exception &e)
  1407. {
  1408. BNLOG("Failed to realize subgoal of type %d (greater goal type was %d), I will stop.", goal.goalType % ultimateGoal.goalType);
  1409. BNLOG("The error message was: %s", e.what());
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. void VCAI::performTypicalActions()
  1415. {
  1416. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1417. {
  1418. BNLOG("Looking into %s", t->name);
  1419. buildStructure(t);
  1420. buildArmyIn(t);
  1421. if(!ai->primaryHero() ||
  1422. (t->getArmyStrength() > ai->primaryHero()->getArmyStrength() * 2 && !isAccessibleForHero(t->visitablePos(), ai->primaryHero())))
  1423. {
  1424. recruitHero(t);
  1425. buildArmyIn(t);
  1426. }
  1427. }
  1428. BOOST_FOREACH(const CGHeroInstance *h, getUnblockedHeroes())
  1429. {
  1430. BNLOG("Looking into %s, MP=%d", h->name.c_str() % h->movement);
  1431. INDENT;
  1432. makePossibleUpgrades(h);
  1433. cb->setSelection(h);
  1434. wander(h);
  1435. }
  1436. }
  1437. void VCAI::buildArmyIn(const CGTownInstance * t)
  1438. {
  1439. makePossibleUpgrades(t->visitingHero);
  1440. makePossibleUpgrades(t);
  1441. recruitCreatures(t);
  1442. moveCreaturesToHero(t);
  1443. }
  1444. int3 VCAI::explorationBestNeighbour(int3 hpos, int radius, const CGHeroInstance * h)
  1445. {
  1446. TimeCheck tc("looking for best exploration neighbour");
  1447. std::map<int3, int> dstToRevealedTiles;
  1448. BOOST_FOREACH(crint3 dir, dirs)
  1449. if(cb->isInTheMap(hpos+dir))
  1450. dstToRevealedTiles[hpos + dir] = howManyTilesWillBeDiscovered(radius, hpos, dir) * isSafeToVisit(h, hpos + dir);
  1451. auto best = dstToRevealedTiles.begin();
  1452. best->second *= cb->getPathInfo(best->first)->reachable();
  1453. best->second *= cb->getPathInfo(best->first)->accessible == CGPathNode::ACCESSIBLE;
  1454. for(auto i = dstToRevealedTiles.begin(); i != dstToRevealedTiles.end(); i++)
  1455. {
  1456. const CGPathNode *pn = cb->getPathInfo(i->first);
  1457. //const TerrainTile *t = cb->getTile(i->first);
  1458. if(best->second < i->second && i->second && pn->reachable() && pn->accessible == CGPathNode::ACCESSIBLE)
  1459. best = i;
  1460. }
  1461. if(best->second)
  1462. return best->first;
  1463. throw cannotFulfillGoalException("No neighbour will bring new discoveries!");
  1464. }
  1465. int3 VCAI::explorationNewPoint(int radius, const CGHeroInstance * h, std::vector<std::vector<int3> > &tiles)
  1466. {
  1467. TimeCheck tc("looking for new exploration point");
  1468. tlog0 << "Looking for an another place for exploration...\n";
  1469. tiles.resize(radius);
  1470. foreach_tile_pos([&](const int3 &pos)
  1471. {
  1472. if(!cb->isVisible(pos))
  1473. tiles[0].push_back(pos);
  1474. });
  1475. for (int i = 1; i < radius; i++)
  1476. {
  1477. getVisibleNeighbours(tiles[i-1], tiles[i]);
  1478. removeDuplicates(tiles[i]);
  1479. BOOST_FOREACH(const int3 &tile, tiles[i])
  1480. {
  1481. if(cb->getPathInfo(tile)->reachable() && isSafeToVisit(h, tile) && howManyTilesWillBeDiscovered(tile, radius))
  1482. {
  1483. return tile;
  1484. }
  1485. }
  1486. }
  1487. throw cannotFulfillGoalException("No accessible tile will bring discoveries!");
  1488. }
  1489. TResources VCAI::estimateIncome() const
  1490. {
  1491. TResources ret;
  1492. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1493. {
  1494. ret[Res::GOLD] += t->dailyIncome();
  1495. //TODO duplikuje newturn
  1496. if(t->hasBuilt(EBuilding::RESOURCE_SILO)) //there is resource silo
  1497. {
  1498. if(t->town->primaryRes == 127) //we'll give wood and ore
  1499. {
  1500. ret[Res::WOOD] ++;
  1501. ret[Res::ORE] ++;
  1502. }
  1503. else
  1504. {
  1505. ret[t->town->primaryRes] ++;
  1506. }
  1507. }
  1508. }
  1509. BOOST_FOREACH(const CGObjectInstance *obj, getFlaggedObjects())
  1510. {
  1511. if(obj->ID == Obj::MINE)
  1512. {
  1513. switch(obj->subID)
  1514. {
  1515. case Res::WOOD:
  1516. case Res::ORE:
  1517. ret[obj->subID] += WOOD_ORE_MINE_PRODUCTION;
  1518. break;
  1519. case Res::GOLD:
  1520. case 7: //abandoned mine -> also gold
  1521. ret[Res::GOLD] += GOLD_MINE_PRODUCTION;
  1522. break;
  1523. default:
  1524. ret[obj->subID] += RESOURCE_MINE_PRODUCTION;
  1525. break;
  1526. }
  1527. }
  1528. }
  1529. return ret;
  1530. }
  1531. bool VCAI::containsSavedRes(const TResources &cost) const
  1532. {
  1533. for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  1534. {
  1535. if(saving[i] && cost[i])
  1536. return true;
  1537. }
  1538. return false;
  1539. }
  1540. void VCAI::recruitHero(const CGTownInstance * t)
  1541. {
  1542. BNLOG("Trying to recruit a hero in %s at %s", t->name % t->visitablePos())
  1543. cb->recruitHero(t, cb->getAvailableHeroes(t).front());
  1544. }
  1545. void VCAI::finish()
  1546. {
  1547. if(makingTurn)
  1548. makingTurn->interrupt();
  1549. }
  1550. AIStatus::AIStatus()
  1551. {
  1552. battle = NO_BATTLE;
  1553. remainingQueries = 0;
  1554. havingTurn = false;
  1555. }
  1556. AIStatus::~AIStatus()
  1557. {
  1558. }
  1559. void AIStatus::setBattle(BattleState BS)
  1560. {
  1561. boost::unique_lock<boost::mutex> lock(mx);
  1562. battle = BS;
  1563. cv.notify_all();
  1564. }
  1565. BattleState AIStatus::getBattle()
  1566. {
  1567. boost::unique_lock<boost::mutex> lock(mx);
  1568. return battle;
  1569. }
  1570. void AIStatus::addQueries(int val)
  1571. {
  1572. boost::unique_lock<boost::mutex> lock(mx);
  1573. remainingQueries += val;
  1574. BNLOG("Changing count of queries by %d, to a total of %d", val % remainingQueries);
  1575. assert(remainingQueries >= 0);
  1576. cv.notify_all();
  1577. }
  1578. void AIStatus::addQuery()
  1579. {
  1580. addQueries(1);
  1581. }
  1582. void AIStatus::removeQuery()
  1583. {
  1584. addQueries(-1);
  1585. }
  1586. int AIStatus::getQueriesCount()
  1587. {
  1588. boost::unique_lock<boost::mutex> lock(mx);
  1589. return remainingQueries;
  1590. }
  1591. void AIStatus::startedTurn()
  1592. {
  1593. boost::unique_lock<boost::mutex> lock(mx);
  1594. havingTurn = true;
  1595. cv.notify_all();
  1596. }
  1597. void AIStatus::madeTurn()
  1598. {
  1599. boost::unique_lock<boost::mutex> lock(mx);
  1600. havingTurn = false;
  1601. cv.notify_all();
  1602. }
  1603. void AIStatus::waitTillFree()
  1604. {
  1605. boost::unique_lock<boost::mutex> lock(mx);
  1606. while(battle != NO_BATTLE || remainingQueries)
  1607. cv.wait(lock);
  1608. }
  1609. bool AIStatus::haveTurn()
  1610. {
  1611. boost::unique_lock<boost::mutex> lock(mx);
  1612. return havingTurn;
  1613. }
  1614. int3 whereToExplore(const CGHeroInstance *h)
  1615. {
  1616. //TODO it's stupid and ineffective, write sth better
  1617. cb->setSelection(h);
  1618. int radius = h->getSightRadious();
  1619. int3 hpos = h->visitablePos();
  1620. //look for nearby objs -> visit them if they're close enouh
  1621. const int DIST_LIMIT = 3;
  1622. std::vector<const CGObjectInstance *> nearbyVisitableObjs;
  1623. BOOST_FOREACH(const CGObjectInstance *obj, ai->getPossibleDestinations(h))
  1624. {
  1625. int3 op = obj->visitablePos();
  1626. CGPath p;
  1627. cb->getPath2(op, p);
  1628. if(p.nodes.size() && p.endPos() == op && p.nodes.size() <= DIST_LIMIT)
  1629. nearbyVisitableObjs.push_back(obj);
  1630. }
  1631. boost::sort(nearbyVisitableObjs, isCloser);
  1632. if(nearbyVisitableObjs.size())
  1633. return nearbyVisitableObjs.back()->visitablePos();
  1634. try
  1635. {
  1636. return ai->explorationBestNeighbour(hpos, radius, h);
  1637. }
  1638. catch(cannotFulfillGoalException &e)
  1639. {
  1640. std::vector<std::vector<int3> > tiles; //tiles[distance_to_fow], metryka taksówkowa
  1641. try
  1642. {
  1643. return ai->explorationNewPoint(radius, h, tiles);
  1644. }
  1645. catch(cannotFulfillGoalException &e)
  1646. {
  1647. std::map<int, std::vector<int3> > profits;
  1648. {
  1649. TimeCheck tc("Evaluating exploration possibilities");
  1650. tiles[0].clear(); //we can't reach FoW anyway
  1651. BOOST_FOREACH(auto &vt, tiles)
  1652. BOOST_FOREACH(auto &tile, vt)
  1653. profits[howManyTilesWillBeDiscovered(tile, radius)].push_back(tile);
  1654. }
  1655. if(profits.empty())
  1656. throw cannotFulfillGoalException("Cannot explore - no possible ways found!");
  1657. auto bestDest = profits.end();
  1658. bestDest--;
  1659. return bestDest->second.front(); //TODO which is the real best tile?
  1660. }
  1661. }
  1662. }
  1663. TSubgoal CGoal::whatToDoToAchieve()
  1664. {
  1665. switch(goalType)
  1666. {
  1667. case WIN:
  1668. {
  1669. const CVictoryCondition &vc = cb->getMapHeader()->victoryCondition;
  1670. EVictoryConditionType::EVictoryConditionType cond = vc.condition;
  1671. if(!vc.appliesToAI)
  1672. {
  1673. //TODO deduce victory from human loss condition
  1674. cond = EVictoryConditionType::WINSTANDARD;
  1675. }
  1676. switch(cond)
  1677. {
  1678. case EVictoryConditionType::ARTIFACT:
  1679. return CGoal(GET_ART_TYPE).setaid(vc.ID);
  1680. case EVictoryConditionType::BEATHERO:
  1681. return CGoal(GET_OBJ).setobjid(vc.ID);
  1682. case EVictoryConditionType::BEATMONSTER:
  1683. return CGoal(GET_OBJ).setobjid(vc.ID);
  1684. case EVictoryConditionType::BUILDCITY:
  1685. //TODO build castle/capitol
  1686. break;
  1687. case EVictoryConditionType::BUILDGRAIL:
  1688. {
  1689. if(const CGHeroInstance *h = ai->getHeroWithGrail())
  1690. {
  1691. //hero is in a town that can host Grail
  1692. if(h->visitedTown && !vstd::contains(h->visitedTown->forbiddenBuildings, EBuilding::GRAIL))
  1693. {
  1694. const CGTownInstance *t = h->visitedTown;
  1695. return CGoal(BUILD_STRUCTURE).setbid(EBuilding::GRAIL).settown(t);
  1696. }
  1697. else
  1698. {
  1699. auto towns = cb->getTownsInfo();
  1700. towns.erase(boost::remove_if(towns,
  1701. [](const CGTownInstance *t) -> bool
  1702. {
  1703. return vstd::contains(t->forbiddenBuildings, EBuilding::GRAIL);
  1704. }),
  1705. towns.end());
  1706. boost::sort(towns, isCloser);
  1707. if(towns.size())
  1708. {
  1709. return CGoal(VISIT_TILE).sethero(h).settile(towns.front()->visitablePos());
  1710. }
  1711. }
  1712. }
  1713. double ratio = 0;
  1714. int3 grailPos = cb->getGrailPos(ratio);
  1715. if(ratio > 0.99)
  1716. {
  1717. return CGoal(DIG_AT_TILE).settile(grailPos);
  1718. }
  1719. else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
  1720. {
  1721. return CGoal(GET_OBJ).setobjid(obj->id);
  1722. }
  1723. else
  1724. return CGoal(EXPLORE);
  1725. }
  1726. break;
  1727. case EVictoryConditionType::CAPTURECITY:
  1728. return CGoal(GET_OBJ).setobjid(vc.ID);
  1729. case EVictoryConditionType::GATHERRESOURCE:
  1730. return CGoal(COLLECT_RES).setresID(vc.ID).setvalue(vc.count);
  1731. //TODO mines? piles? marketplace?
  1732. //save?
  1733. break;
  1734. case EVictoryConditionType::GATHERTROOP:
  1735. break;
  1736. case EVictoryConditionType::TAKEDWELLINGS:
  1737. break;
  1738. case EVictoryConditionType::TAKEMINES:
  1739. break;
  1740. case EVictoryConditionType::TRANSPORTITEM:
  1741. break;
  1742. case EVictoryConditionType::WINSTANDARD:
  1743. return CGoal(CONQUER);
  1744. default:
  1745. assert(0);
  1746. }
  1747. }
  1748. break;
  1749. case GET_OBJ:
  1750. {
  1751. const CGObjectInstance * obj = cb->getObj(objid);
  1752. if(!obj)
  1753. return CGoal(EXPLORE);
  1754. int3 pos = cb->getObj(objid)->visitablePos();
  1755. return CGoal(VISIT_TILE).settile(pos);
  1756. }
  1757. break;
  1758. case GET_ART_TYPE:
  1759. {
  1760. const CGObjectInstance *artInst = ai->lookForArt(aid);
  1761. if(!artInst)
  1762. {
  1763. TSubgoal alternativeWay = CGoal::lookForArtSmart(aid);
  1764. if(alternativeWay.invalid())
  1765. return CGoal(EXPLORE);
  1766. else
  1767. return alternativeWay;
  1768. }
  1769. else
  1770. return CGoal(GET_OBJ).setobjid(artInst->id);
  1771. }
  1772. break;
  1773. case CLEAR_WAY_TO:
  1774. {
  1775. assert(tile.x >= 0); //set tile
  1776. if(!cb->isVisible(tile))
  1777. {
  1778. tlog1 << "Clear way should be used with visible tiles!\n";
  1779. return CGoal(EXPLORE);
  1780. }
  1781. const CGHeroInstance *h = hero ? hero : ai->primaryHero();
  1782. if(!h)
  1783. return CGoal(RECRUIT_HERO);
  1784. cb->setSelection(h);
  1785. SectorMap sm;
  1786. bool dropToFile = false;
  1787. if(dropToFile) //for debug purposes
  1788. sm.write("test.txt");
  1789. int3 tileToHit = sm.firstTileToGet(h, tile);
  1790. //if(isSafeToVisit(h, tileToHit))
  1791. if(isBlockedBorderGate(tileToHit))
  1792. throw cannotFulfillGoalException("There's blocked border gate!");
  1793. if(tileToHit == tile)
  1794. {
  1795. tlog1 << boost::format("Very strange, tile to hit is %s and tile is also %s, while hero %s is at %s\n")
  1796. % tileToHit % tile % h->name % h->visitablePos();
  1797. throw cannotFulfillGoalException("Retreiving first tile to hit failed (probably)!");
  1798. }
  1799. return CGoal(VISIT_TILE).settile(tileToHit).sethero(h);
  1800. //TODO czy istnieje lepsza droga?
  1801. }
  1802. throw cannotFulfillGoalException("Cannot reach given tile!");
  1803. //return CGoal(EXPLORE); // TODO improve
  1804. case EXPLORE:
  1805. {
  1806. auto hs = cb->getHeroesInfo();
  1807. int howManyHeroes = hs.size();
  1808. erase(hs, [](const CGHeroInstance *h)
  1809. {
  1810. return contains(ai->lockedHeroes, h);
  1811. });
  1812. if(hs.empty()) //all heroes are busy. buy new one
  1813. {
  1814. if (howManyHeroes < 3 && ai->findTownWithTavern()) //we may want to recruit second hero. TODO: make it smart finally
  1815. return CGoal(RECRUIT_HERO);
  1816. else //find mobile hero with weakest army
  1817. {
  1818. hs = cb->getHeroesInfo();
  1819. erase_if(hs, [](const CGHeroInstance *h)
  1820. {
  1821. return !h->movement; //only hero with movement are of interest for us
  1822. });
  1823. if (hs.empty())
  1824. {
  1825. if (howManyHeroes < GameConstants::MAX_HEROES_PER_PLAYER)
  1826. return CGoal(RECRUIT_HERO);
  1827. else
  1828. throw cannotFulfillGoalException("No heroes with remaining MPs for exploring!\n");
  1829. }
  1830. boost::sort(hs, compareHeroStrength);
  1831. }
  1832. }
  1833. const CGHeroInstance *h = hs.front();
  1834. CGoal ret(VISIT_TILE);
  1835. ret.sethero(h);
  1836. return ret.settile(whereToExplore(h));
  1837. }
  1838. I_AM_ELEMENTAR;
  1839. case RECRUIT_HERO:
  1840. {
  1841. const CGTownInstance *t = ai->findTownWithTavern();
  1842. if(!t)
  1843. return CGoal(BUILD_STRUCTURE).setbid(EBuilding::TAVERN);
  1844. if(cb->getResourceAmount(Res::GOLD) < HERO_GOLD_COST)
  1845. return CGoal(COLLECT_RES).setresID(Res::GOLD).setvalue(HERO_GOLD_COST);
  1846. I_AM_ELEMENTAR;
  1847. }
  1848. break;
  1849. case VISIT_TILE:
  1850. {
  1851. if(!cb->isVisible(tile))
  1852. return CGoal(EXPLORE);
  1853. if(hero && !ai->isAccessibleForHero(tile, hero))
  1854. hero = NULL;
  1855. if(!hero)
  1856. {
  1857. if(cb->getHeroesInfo().empty())
  1858. return CGoal(RECRUIT_HERO);
  1859. BOOST_FOREACH(const CGHeroInstance *h, cb->getHeroesInfo())
  1860. {
  1861. if(ai->isAccessibleForHero(tile, h))
  1862. {
  1863. hero = h;
  1864. break;
  1865. }
  1866. }
  1867. }
  1868. if(hero)
  1869. {
  1870. if(isSafeToVisit(hero, tile))
  1871. return CGoal(*this).setisElementar(true);
  1872. else
  1873. return CGoal(GATHER_ARMY).sethero(hero);
  1874. }
  1875. else //inaccessible for all heroes
  1876. return CGoal(CLEAR_WAY_TO).settile(tile);
  1877. }
  1878. break;
  1879. case DIG_AT_TILE:
  1880. {
  1881. auto objs = cb->getTile(tile)->visitableObjects;
  1882. if(objs.size() && objs.front()->ID == GameConstants::HEROI_TYPE && objs.front()->tempOwner == ai->playerID) //we have hero at dest
  1883. {
  1884. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(objs.front());
  1885. return CGoal(*this).sethero(h).setisElementar(true);
  1886. }
  1887. return CGoal(VISIT_TILE).settile(tile);
  1888. }
  1889. break;
  1890. case BUILD_STRUCTURE:
  1891. //TODO check res
  1892. //look for town
  1893. //prerequisites?
  1894. I_AM_ELEMENTAR;
  1895. case COLLECT_RES:
  1896. {
  1897. std::vector<const IMarket*> markets;
  1898. std::vector<const CGObjectInstance*> visObjs;
  1899. ai->retreiveVisitableObjs(visObjs, true);
  1900. BOOST_FOREACH(const CGObjectInstance *obj, visObjs)
  1901. {
  1902. if(const IMarket *m = IMarket::castFrom(obj, false))
  1903. {
  1904. if(obj->ID == GameConstants::TOWNI_TYPE && obj->tempOwner == ai->playerID && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
  1905. markets.push_back(m);
  1906. else if(obj->ID == Obj::TRADING_POST) //TODO a moze po prostu test na pozwalanie handlu?
  1907. markets.push_back(m);
  1908. }
  1909. }
  1910. boost::sort(markets, [](const IMarket *m1, const IMarket *m2) -> bool
  1911. {
  1912. return m1->getMarketEfficiency() < m2->getMarketEfficiency();
  1913. });
  1914. markets.erase(boost::remove_if(markets, [](const IMarket *market) -> bool
  1915. {
  1916. return !(market->o->ID == GameConstants::TOWNI_TYPE && market->o->tempOwner == ai->playerID)
  1917. && !ai->isAccessible(market->o->visitablePos());
  1918. }),markets.end());
  1919. if(!markets.size())
  1920. {
  1921. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1922. {
  1923. if(cb->canBuildStructure(t, EBuilding::MARKETPLACE) == EBuildingState::ALLOWED)
  1924. return CGoal(BUILD_STRUCTURE).settown(t).setbid(EBuilding::MARKETPLACE);
  1925. }
  1926. }
  1927. else
  1928. {
  1929. const IMarket *m = markets.back();
  1930. //attempt trade at back (best prices)
  1931. int howManyCanWeBuy = 0;
  1932. for(int i = 0; i < ACTUAL_RESOURCE_COUNT; i++)
  1933. {
  1934. if(i == resID) continue;
  1935. int toGive = -1, toReceive = -1;
  1936. m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
  1937. assert(toGive > 0 && toReceive > 0);
  1938. howManyCanWeBuy += toReceive * (cb->getResourceAmount(i) / toGive);
  1939. }
  1940. if(howManyCanWeBuy + cb->getResourceAmount(resID) >= value)
  1941. {
  1942. if(cb->getTile(m->o->visitablePos())->visitableObjects.back()->tempOwner != ai->playerID)
  1943. return CGoal(GET_OBJ).setobjid(m->o->id);
  1944. return setobjid(m->o->id).setisElementar(true);
  1945. }
  1946. }
  1947. }
  1948. return CGoal(INVALID);
  1949. case CONQUER:
  1950. {
  1951. //TODO make use from many heroes
  1952. std::vector<const CGHeroInstance *> heroes = cb->getHeroesInfo();
  1953. erase_if(heroes, [](const CGHeroInstance *h)
  1954. {
  1955. return vstd::contains(ai->lockedHeroes, h) || !h->movement;
  1956. });
  1957. boost::sort(heroes, compareHeroStrength);
  1958. if(heroes.empty())
  1959. I_AM_ELEMENTAR;
  1960. const CGHeroInstance *h = heroes.back();
  1961. cb->setSelection(h);
  1962. std::vector<const CGObjectInstance *> objs; //here we'll gather enemy towns and heroes
  1963. ai->retreiveVisitableObjs(objs);
  1964. erase_if(objs, [&](const CGObjectInstance *obj)
  1965. {
  1966. return (obj->ID != GameConstants::TOWNI_TYPE && obj->ID != GameConstants::HEROI_TYPE) //not town/hero
  1967. || cb->getPlayerRelations(ai->playerID, obj->tempOwner) != 0; //not enemy
  1968. });
  1969. if(objs.empty())
  1970. return CGoal(EXPLORE); //we need to find an enemy
  1971. erase_if(objs, [&](const CGObjectInstance *obj)
  1972. {
  1973. return !isSafeToVisit(h, obj->visitablePos());
  1974. });
  1975. if(objs.empty())
  1976. I_AM_ELEMENTAR;
  1977. boost::sort(objs, isCloser);
  1978. BOOST_FOREACH(const CGObjectInstance *obj, objs)
  1979. {
  1980. if(ai->isAccessibleForHero(obj->visitablePos(), h))
  1981. return CGoal(VISIT_TILE).sethero(h).settile(obj->visitablePos());
  1982. }
  1983. return CGoal(EXPLORE); //enemy is inaccessible
  1984. }
  1985. break;
  1986. case BUILD:
  1987. I_AM_ELEMENTAR;
  1988. case INVALID:
  1989. I_AM_ELEMENTAR;
  1990. case GATHER_ARMY:
  1991. {
  1992. const CGHeroInstance *h = hero;
  1993. auto compareReinforcements = [h](const CGTownInstance *lhs, const CGTownInstance *rhs) -> bool
  1994. {
  1995. return howManyReinforcementsCanGet(h, lhs) < howManyReinforcementsCanGet(h, rhs);
  1996. };
  1997. std::vector<const CGTownInstance *> townsReachable;
  1998. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  1999. {
  2000. if(!t->visitingHero && howManyReinforcementsCanGet(h,t))
  2001. {
  2002. if(isReachable(t))
  2003. townsReachable.push_back(t);
  2004. }
  2005. }
  2006. if(townsReachable.size()) //try towns first
  2007. {
  2008. boost::sort(townsReachable, compareReinforcements);
  2009. return CGoal(VISIT_TILE).sethero(hero).settile(townsReachable.back()->visitablePos());
  2010. }
  2011. else
  2012. {
  2013. std::vector<const CGObjectInstance *> objs; //here we'll gather all dwellings
  2014. ai->retreiveVisitableObjs(objs);
  2015. erase_if(objs, [&](const CGObjectInstance *obj)
  2016. {
  2017. return (obj->ID != Obj::CREATURE_GENERATOR1); //not town/ dwelling
  2018. });
  2019. if(objs.empty()) //no possible objects, we did eveyrthing already
  2020. return CGoal(EXPLORE).sethero(hero);
  2021. //TODO: check if we can recruit any creatures there, evaluate army
  2022. boost::sort(objs, isCloser);
  2023. BOOST_FOREACH(const CGObjectInstance *obj, objs)
  2024. { //find safe dwelling
  2025. if (isSafeToVisit(hero, obj->visitablePos())) //TODO: make use of multiple heroes
  2026. return CGoal(VISIT_TILE).sethero(hero).settile(obj->visitablePos());
  2027. }
  2028. }
  2029. return CGoal(EXPLORE); //find dwelling
  2030. }
  2031. break;
  2032. default:
  2033. assert(0);
  2034. }
  2035. return CGoal(EXPLORE);
  2036. }
  2037. TSubgoal CGoal::goVisitOrLookFor(const CGObjectInstance *obj)
  2038. {
  2039. if(obj)
  2040. return CGoal(GET_OBJ).setobjid(obj->id);
  2041. else
  2042. return CGoal(EXPLORE);
  2043. }
  2044. TSubgoal CGoal::lookForArtSmart(int aid)
  2045. {
  2046. return CGoal(INVALID);
  2047. }
  2048. bool CGoal::invalid() const
  2049. {
  2050. return goalType == INVALID;
  2051. }
  2052. bool CGoal::isBlockedBorderGate(int3 tileToHit)
  2053. {
  2054. return cb->getTile(tileToHit)->topVisitableID() == Obj::BORDER_GATE
  2055. && cb->getPathInfo(tileToHit)->accessible != CGPathNode::ACCESSIBLE;
  2056. }
  2057. SectorMap::SectorMap()
  2058. {
  2059. // int3 sizes = cb->getMapSize();
  2060. // sector.resize(sizes.x);
  2061. // BOOST_FOREACH(auto &i, sector)
  2062. // i.resize(sizes.y);
  2063. //
  2064. // BOOST_FOREACH(auto &i, sector)
  2065. // BOOST_FOREACH(auto &j, i)
  2066. // j.resize(sizes.z, 0);
  2067. update();
  2068. }
  2069. bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
  2070. {
  2071. if(t->blocked && !t->visitable)
  2072. {
  2073. sec = NOT_AVAILABLE;
  2074. return true;
  2075. }
  2076. return false;
  2077. }
  2078. bool markIfBlocked(ui8 &sec, crint3 pos)
  2079. {
  2080. return markIfBlocked(sec, pos, cb->getTile(pos));
  2081. }
  2082. void SectorMap::update()
  2083. {
  2084. clear();
  2085. int curSector = 3; //0 is invisible, 1 is not explored
  2086. foreach_tile_pos([&](crint3 pos)
  2087. {
  2088. if(retreiveTile(pos) == NOT_CHECKED)
  2089. {
  2090. if(!markIfBlocked(retreiveTile(pos), pos))
  2091. exploreNewSector(pos, curSector++);
  2092. }
  2093. });
  2094. valid = true;
  2095. }
  2096. void SectorMap::clear()
  2097. {
  2098. sector = cb->getVisibilityMap();
  2099. valid = false;
  2100. }
  2101. bool canBeEmbarkmentPoint(const TerrainTile *t)
  2102. {
  2103. //tile must be free of with unoccupied boat
  2104. return !t->blocked
  2105. || (t->visitableObjects.size() == 1 && t->topVisitableID() == Obj::BOAT);
  2106. }
  2107. void SectorMap::exploreNewSector(crint3 pos, int num)
  2108. {
  2109. Sector &s = infoOnSectors[num];
  2110. s.id = num;
  2111. s.water = cb->getTile(pos)->isWater();
  2112. std::queue<int3> toVisit;
  2113. toVisit.push(pos);
  2114. while(toVisit.size())
  2115. {
  2116. int3 curPos = toVisit.front();
  2117. toVisit.pop();
  2118. ui8 &sec = retreiveTile(curPos);
  2119. if(sec == NOT_CHECKED)
  2120. {
  2121. const TerrainTile *t = cb->getTile(curPos);
  2122. if(!markIfBlocked(sec, curPos, t))
  2123. {
  2124. if(t->isWater() == s.water) //sector is only-water or only-land
  2125. {
  2126. sec = num;
  2127. s.tiles.push_back(curPos);
  2128. foreach_neighbour(curPos, [&](crint3 neighPos)
  2129. {
  2130. if(retreiveTile(neighPos) == NOT_CHECKED)
  2131. {
  2132. toVisit.push(neighPos);
  2133. //parent[neighPos] = curPos;
  2134. }
  2135. const TerrainTile *nt = cb->getTile(neighPos, false);
  2136. if(nt && nt->isWater() != s.water && canBeEmbarkmentPoint(nt))
  2137. {
  2138. s.embarkmentPoints.push_back(neighPos);
  2139. }
  2140. });
  2141. if(t->visitable && vstd::contains(ai->knownSubterraneanGates, t->visitableObjects.front()))
  2142. toVisit.push(ai->knownSubterraneanGates[t->visitableObjects.front()]->pos);
  2143. }
  2144. }
  2145. }
  2146. }
  2147. removeDuplicates(s.embarkmentPoints);
  2148. }
  2149. void SectorMap::write(crstring fname)
  2150. {
  2151. std::ofstream out(fname);
  2152. for(int k = 0; k < cb->getMapSize().z; k++)
  2153. {
  2154. for(int j = 0; j < cb->getMapSize().y; j++)
  2155. {
  2156. for(int i = 0; i < cb->getMapSize().x; i++)
  2157. {
  2158. out << (int)sector[i][j][k] << '\t';
  2159. }
  2160. out << std::endl;
  2161. }
  2162. out << std::endl;
  2163. }
  2164. }
  2165. bool isWeeklyRevisitable (const CGObjectInstance * obj)
  2166. { //TODO: allow polling of remaining creatures in dwelling
  2167. if (dynamic_cast<const CGVisitableOPW *>(obj) || //ensures future compatibility, unlike IDs
  2168. dynamic_cast<const CGDwelling *>(obj) ||
  2169. dynamic_cast<const CBank *>(obj)) //banks tend to respawn often in mods
  2170. return true;
  2171. switch (obj->ID)
  2172. {
  2173. case Obj::STABLES: //any other potential visitable objects?
  2174. return true;
  2175. }
  2176. return false;
  2177. }
  2178. bool shouldVisit (const CGHeroInstance * h, const CGObjectInstance * obj)
  2179. {
  2180. if (obj->wasVisited(h))
  2181. return false;
  2182. switch (obj->ID)
  2183. {
  2184. case Obj::CREATURE_GENERATOR1:
  2185. {
  2186. bool canRecruitCreatures = false;
  2187. const CGDwelling * d = dynamic_cast<const CGDwelling *>(obj);
  2188. BOOST_FOREACH(auto level, d->creatures)
  2189. {
  2190. BOOST_FOREACH(auto c, level.second)
  2191. {
  2192. if (h->getSlotFor(c) != -1)
  2193. canRecruitCreatures = true;
  2194. }
  2195. }
  2196. return canRecruitCreatures;
  2197. }
  2198. case Obj::MONOLITH1:
  2199. case Obj::MONOLITH2:
  2200. case Obj::MONOLITH3:
  2201. case Obj::WHIRLPOOL:
  2202. //TODO: mehcanism for handling monoliths
  2203. return false;
  2204. }
  2205. return true;
  2206. }
  2207. int3 SectorMap::firstTileToGet(const CGHeroInstance *h, crint3 dst)
  2208. {
  2209. int sourceSector = retreiveTile(h->visitablePos()),
  2210. destinationSector = retreiveTile(dst);
  2211. if(sourceSector != destinationSector)
  2212. {
  2213. const Sector *src = &infoOnSectors[sourceSector],
  2214. *dst = &infoOnSectors[destinationSector];
  2215. std::map<const Sector*, const Sector*> preds;
  2216. std::queue<const Sector *> sq;
  2217. sq.push(src);
  2218. while(!sq.empty())
  2219. {
  2220. const Sector *s = sq.front();
  2221. sq.pop();
  2222. BOOST_FOREACH(int3 ep, s->embarkmentPoints)
  2223. {
  2224. Sector *neigh = &infoOnSectors[retreiveTile(ep)];
  2225. //preds[s].push_back(neigh);
  2226. if(!preds[neigh])
  2227. {
  2228. preds[neigh] = s;
  2229. sq.push(neigh);
  2230. }
  2231. }
  2232. //TODO consider other types of connections between sectors?
  2233. }
  2234. if(!preds[dst])
  2235. {
  2236. write("test.txt");
  2237. throw cannotFulfillGoalException(str(format("Cannot found connection between sectors %d and %d") % src->id % dst->id));
  2238. }
  2239. std::vector<const Sector*> toTraverse;
  2240. toTraverse.push_back(dst);
  2241. while(toTraverse.back() != src)
  2242. {
  2243. toTraverse.push_back(preds[toTraverse.back()]);
  2244. }
  2245. if(preds[dst])
  2246. {
  2247. const Sector *sectorToReach = toTraverse.at(toTraverse.size() - 2);
  2248. if(!src->water && sectorToReach->water) //embark
  2249. {
  2250. //embark on ship -> look for an EP with a boat
  2251. auto firstEP = boost::find_if(src->embarkmentPoints, [=](crint3 pos) -> bool
  2252. {
  2253. const TerrainTile *t = cb->getTile(pos);
  2254. return t && t->visitableObjects.size() == 1 && t->topVisitableID() == Obj::BOAT
  2255. && retreiveTile(pos) == sectorToReach->id;
  2256. });
  2257. if(firstEP != src->embarkmentPoints.end())
  2258. {
  2259. return *firstEP;
  2260. }
  2261. else
  2262. {
  2263. //we need to find a shipyard with an access to the desired sector's EP
  2264. //TODO what about Summon Boat spell?
  2265. std::vector<const IShipyard *> shipyards;
  2266. BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
  2267. {
  2268. if(t->hasBuilt(EBuilding::SHIPYARD))
  2269. shipyards.push_back(t);
  2270. }
  2271. std::vector<const CGObjectInstance*> visObjs;
  2272. ai->retreiveVisitableObjs(visObjs, true);
  2273. BOOST_FOREACH(const CGObjectInstance *obj, visObjs)
  2274. {
  2275. if(obj->ID != GameConstants::TOWNI_TYPE) //towns were handled in the previous loop
  2276. if(const IShipyard *shipyard = IShipyard::castFrom(obj))
  2277. shipyards.push_back(shipyard);
  2278. }
  2279. shipyards.erase(boost::remove_if(shipyards, [=](const IShipyard *shipyard) -> bool
  2280. {
  2281. return shipyard->state() != 0 || retreiveTile(shipyard->bestLocation()) != sectorToReach->id;
  2282. }),shipyards.end());
  2283. if(!shipyards.size())
  2284. {
  2285. //TODO consider possibility of building shipyard in a town
  2286. throw cannotFulfillGoalException("There is no known shipyard!");
  2287. }
  2288. //we have only shipyards that possibly can build ships onto the appropriate EP
  2289. auto ownedGoodShipyard = boost::find_if(shipyards, [](const IShipyard *s) -> bool
  2290. {
  2291. return s->o->tempOwner == ai->playerID;
  2292. });
  2293. if(ownedGoodShipyard != shipyards.end())
  2294. {
  2295. const IShipyard *s = *ownedGoodShipyard;
  2296. TResources shipCost;
  2297. s->getBoatCost(shipCost);
  2298. if(cb->getResourceAmount().canAfford(shipCost))
  2299. {
  2300. int3 ret = s->bestLocation();
  2301. cb->buildBoat(s);
  2302. return ret;
  2303. }
  2304. else
  2305. {
  2306. //TODO gather res
  2307. throw cannotFulfillGoalException("Not enough resources to build a boat");
  2308. }
  2309. }
  2310. else
  2311. {
  2312. //TODO pick best shipyard to take over
  2313. return shipyards.front()->o->pos;
  2314. }
  2315. }
  2316. }
  2317. else if(src->water && !sectorToReach->water)
  2318. {
  2319. //TODO
  2320. //disembark
  2321. }
  2322. else
  2323. {
  2324. //TODO
  2325. //transition between two land/water sectors. Monolith? Whirlpool? ...
  2326. throw cannotFulfillGoalException("Land-land and water-water inter-sector transitions are not implemented!");
  2327. }
  2328. }
  2329. else
  2330. {
  2331. throw cannotFulfillGoalException("Inter-sector route detection failed: not connected sectors?");
  2332. }
  2333. }
  2334. else
  2335. {
  2336. makeParentBFS(h->visitablePos());
  2337. int3 curtile = dst;
  2338. while(curtile != h->visitablePos())
  2339. {
  2340. if(cb->getPathInfo(curtile)->reachable())
  2341. {
  2342. return curtile;
  2343. }
  2344. else
  2345. {
  2346. auto i = parent.find(curtile);
  2347. if(i != parent.end())
  2348. {
  2349. assert(curtile != i->second);
  2350. curtile = i->second;
  2351. }
  2352. else
  2353. throw cannotFulfillGoalException("Unreachable tile in sector? Should not happen!");
  2354. }
  2355. }
  2356. }
  2357. throw cannotFulfillGoalException("Impossible happened.");
  2358. }
  2359. void SectorMap::makeParentBFS(crint3 source)
  2360. {
  2361. parent.clear();
  2362. int mySector = retreiveTile(source);
  2363. std::queue<int3> toVisit;
  2364. toVisit.push(source);
  2365. while(toVisit.size())
  2366. {
  2367. int3 curPos = toVisit.front();
  2368. toVisit.pop();
  2369. ui8 &sec = retreiveTile(curPos);
  2370. assert(sec == mySector); //consider only tiles from the same sector
  2371. //const TerrainTile *t = cb->getTile(curPos);
  2372. foreach_neighbour(curPos, [&](crint3 neighPos)
  2373. {
  2374. if(retreiveTile(neighPos) == mySector && !vstd::contains(parent, neighPos))
  2375. {
  2376. toVisit.push(neighPos);
  2377. parent[neighPos] = curPos;
  2378. }
  2379. });
  2380. }
  2381. }
  2382. unsigned char & SectorMap::retreiveTile(crint3 pos)
  2383. {
  2384. return retreiveTileN(sector, pos);
  2385. }