CGeniusAI.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. #include "CGeniusAI.h"
  2. #include "AIPriorities.h"
  3. #include <iostream>
  4. #include "../../hch/CBuildingHandler.h"
  5. #include "../../hch/CHeroHandler.h"
  6. #include "../../lib/VCMI_Lib.h"
  7. #include "../../lib/NetPacks.h"
  8. #include <boost/lexical_cast.hpp>
  9. using namespace std;
  10. using namespace GeniusAI;
  11. #if defined (_MSC_VER) && (_MSC_VER >= 1020) || (__MINGW32__)
  12. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  13. #include <windows.h>
  14. #endif
  15. void DbgBox(const char *msg, bool messageBox)
  16. {
  17. #if defined PRINT_DEBUG
  18. #if defined _DEBUG
  19. //#if 0
  20. # if defined (_MSC_VER) && (_MSC_VER >= 1020)
  21. if (messageBox)
  22. {
  23. MessageBoxA(NULL, msg, "Debug message", MB_OK | MB_ICONASTERISK);
  24. }
  25. # endif
  26. std::cout << msg << std::endl;
  27. #endif
  28. #endif
  29. }
  30. bool CGeniusAI::AIObjectContainer::operator<(const AIObjectContainer& b)const
  31. {
  32. if (o->pos!=b.o->pos)
  33. return o->pos<b.o->pos;
  34. return o->id<b.o->id;
  35. }
  36. CGeniusAI::HypotheticalGameState::HeroModel::HeroModel(const CGHeroInstance * h)
  37. :h(h),finished(false)
  38. {
  39. pos = h->getPosition(false);remainingMovement = h->movement;
  40. }
  41. CGeniusAI::HypotheticalGameState::TownModel::TownModel(const CGTownInstance *t):t(t)
  42. {
  43. hasBuilt = t->builded;
  44. creaturesToRecruit = t->creatures;
  45. creaturesInGarrison = t->army;
  46. }
  47. CGeniusAI::HypotheticalGameState::HypotheticalGameState(CGeniusAI & ai)
  48. :knownVisitableObjects(ai.knownVisitableObjects)
  49. {
  50. AI = &ai;
  51. std::vector < const CGHeroInstance *> heroes = ai.m_cb->getHeroesInfo();
  52. for(std::vector < const CGHeroInstance *>::iterator i = heroes.begin(); i != heroes.end(); i++)
  53. heroModels.push_back(HeroModel(*i));
  54. std::vector < const CGTownInstance *> towns = ai.m_cb->getTownsInfo();
  55. for(std::vector < const CGTownInstance *>::iterator i = towns.begin(); i != towns.end(); i++)
  56. if((*i)->tempOwner==ai.m_cb->getMyColor())
  57. townModels.push_back(TownModel(*i));
  58. if(ai.m_cb->howManyTowns()!=0)
  59. AvailableHeroesToBuy = ai.m_cb->getAvailableHeroes(ai.m_cb->getTownInfo(0,0));
  60. for(int i = 0; i < 8;i++)resourceAmounts.push_back(ai.m_cb->getResourceAmount(i));
  61. }
  62. void CGeniusAI::HypotheticalGameState::update(CGeniusAI & ai)
  63. {
  64. AI = &ai;
  65. knownVisitableObjects = ai.knownVisitableObjects;
  66. std::vector<HeroModel> oldModels = heroModels;
  67. heroModels.clear();
  68. std::vector < const CGHeroInstance *> heroes = ai.m_cb->getHeroesInfo();
  69. for(std::vector < const CGHeroInstance *>::iterator i = heroes.begin(); i != heroes.end(); i++)
  70. heroModels.push_back(HeroModel(*i));
  71. for(int i = 0; i < oldModels.size();i++)
  72. for(int ii = 0; ii < heroModels.size();ii++)
  73. if(oldModels[i].h->subID==heroModels[ii].h->subID)
  74. {
  75. heroModels[ii].finished = oldModels[i].finished;
  76. heroModels[ii].previouslyVisited_pos=oldModels[i].previouslyVisited_pos;
  77. }
  78. townModels.clear();
  79. std::vector < const CGTownInstance *> towns = ai.m_cb->getTownsInfo();
  80. for(std::vector < const CGTownInstance *>::iterator i = towns.begin(); i != towns.end(); i++)
  81. if((*i)->tempOwner==ai.m_cb->getMyColor())
  82. townModels.push_back(TownModel(*i));
  83. if(ai.m_cb->howManyTowns()!=0)
  84. AvailableHeroesToBuy = ai.m_cb->getAvailableHeroes(ai.m_cb->getTownInfo(0,0));
  85. resourceAmounts.clear();
  86. for(int i = 0; i < 8;i++)resourceAmounts.push_back(ai.m_cb->getResourceAmount(i));
  87. }
  88. CGeniusAI::HeroObjective::HeroObjective(const HypotheticalGameState &hgs,Type t,const CGObjectInstance * object,HypotheticalGameState::HeroModel *h,CGeniusAI * ai):object(object),hgs(hgs)
  89. {
  90. AI = ai;
  91. pos = object->pos;
  92. type = t;
  93. whoCanAchieve.push_back(h);
  94. _value = -1;
  95. }
  96. float CGeniusAI::HeroObjective::getValue() const
  97. {
  98. if(_value>=0)
  99. return _value-_cost;
  100. vector<int> resourceCosts; //TODO: each object should have an associated cost to visit IE (tree of knowledge 1000 gold/10 gems)
  101. for(int i = 0; i < 8;i++)
  102. resourceCosts.push_back(0);
  103. if(object->ID==47) //school of magic
  104. resourceCosts[6]+=1000;
  105. float bestCost = 9e9;
  106. HypotheticalGameState::HeroModel * bestHero = NULL;
  107. if(type !=AIObjective::finishTurn)
  108. {
  109. for(int i = 0; i < whoCanAchieve.size();i++)
  110. {
  111. int distOutOfTheWay = 0;
  112. CPath path3;
  113. //from hero to object
  114. if(AI->m_cb->getPath(whoCanAchieve[i]->pos,pos,whoCanAchieve[i]->h,path3))
  115. distOutOfTheWay+=path3.nodes[0].dist;
  116. //from object to goal
  117. if(AI->m_cb->getPath(pos,whoCanAchieve[i]->interestingPos,whoCanAchieve[i]->h,path3))
  118. {
  119. distOutOfTheWay+=path3.nodes[0].dist;
  120. //from hero directly to goal
  121. if(AI->m_cb->getPath(whoCanAchieve[i]->pos,whoCanAchieve[i]->interestingPos,whoCanAchieve[i]->h,path3))
  122. distOutOfTheWay-=path3.nodes[0].dist;
  123. }
  124. float cost = AI->m_priorities->getCost(resourceCosts,whoCanAchieve[i]->h,distOutOfTheWay);
  125. if(cost < bestCost)
  126. {
  127. bestCost = cost;
  128. bestHero = whoCanAchieve[i];
  129. }
  130. }
  131. }
  132. else bestCost = 0;
  133. if(bestHero)
  134. {
  135. whoCanAchieve.clear();
  136. whoCanAchieve.push_back(bestHero);
  137. }
  138. _value = AI->m_priorities->getValue(*this);
  139. _cost=bestCost;
  140. return _value-_cost;
  141. }
  142. bool CGeniusAI::HeroObjective::operator < (const HeroObjective &other)const
  143. {
  144. if(type != other.type)
  145. return type<other.type;
  146. if(pos!=other.pos)
  147. return pos < other.pos;
  148. if(object->id!=other.object->id)
  149. return object->id < other.object->id;
  150. if(dynamic_cast<const CGVisitableOPH *> (object))
  151. if(whoCanAchieve.front()->h->id!=other.whoCanAchieve.front()->h->id)
  152. return whoCanAchieve.front()->h->id<other.whoCanAchieve.front()->h->id;
  153. return false;
  154. }
  155. void CGeniusAI::HeroObjective::print() const
  156. {
  157. switch(type)
  158. {
  159. case visit:
  160. cout << "visit " << object->hoverName << " at (" <<object->pos.x << ","<<object->pos.y << ")" ;
  161. break;
  162. case attack:
  163. cout << "attack " << object->hoverName;
  164. break;
  165. case finishTurn:
  166. cout << "finish turn";
  167. }
  168. if(whoCanAchieve.size()==1)
  169. cout << " with " << whoCanAchieve.front()->h->hoverName;
  170. }
  171. CGeniusAI::TownObjective::TownObjective(const HypotheticalGameState &hgs,Type t,HypotheticalGameState::TownModel * tn,int Which,CGeniusAI * ai)
  172. :whichTown(tn),which(Which),hgs(hgs)
  173. {
  174. AI=ai;
  175. type = t;
  176. _value = -1;
  177. }
  178. float CGeniusAI::TownObjective::getValue() const
  179. {
  180. if(_value>=0)
  181. return _value-_cost;
  182. float cost;
  183. vector<int> resourceCosts(8,0);
  184. CBuilding * b;
  185. CCreature * creature;
  186. int ID,newID, howMany;
  187. switch(type)
  188. {
  189. case recruitHero:
  190. resourceCosts[6]=2500;
  191. break;
  192. case buildBuilding:
  193. b = VLC->buildh->buildings[whichTown->t->subID][which];
  194. for(int i = 0; b && i < b->resources.size();i++)
  195. resourceCosts[i]=b->resources[i];
  196. break;
  197. case recruitCreatures:
  198. ID = whichTown->creaturesToRecruit[which].second.back(); //buy upgraded if possible
  199. creature = &VLC->creh->creatures[ID];
  200. howMany = whichTown->creaturesToRecruit[which].first;
  201. for(int i = 0; i < creature->cost.size();i++)
  202. amin(howMany,creature->cost[i]?hgs.resourceAmounts[i]/creature->cost[i]:INT_MAX);
  203. for(int i = 0; creature && i < creature->cost.size();i++)
  204. resourceCosts[i]=creature->cost[i]*howMany;
  205. break;
  206. case upgradeCreatures:
  207. UpgradeInfo ui = AI->m_cb->getUpgradeInfo(whichTown->t,which);
  208. ID = whichTown->creaturesInGarrison.slots[which].first;
  209. howMany = whichTown->creaturesInGarrison.slots[which].second;
  210. newID = ui.newID.back();
  211. int upgrade_serial = ui.newID.size()-1;
  212. for (std::set<std::pair<int,int> >::iterator j=ui.cost[upgrade_serial].begin(); j!=ui.cost[upgrade_serial].end(); j++)
  213. resourceCosts[j->first] = j->second*howMany;
  214. break;
  215. }
  216. _cost = AI->m_priorities->getCost(resourceCosts,NULL,0);
  217. _value = AI->m_priorities->getValue(*this);
  218. return _value-_cost;
  219. }
  220. bool CGeniusAI::TownObjective::operator < (const TownObjective &other)const
  221. {
  222. if(type != other.type)
  223. return type<other.type;
  224. if(which!=other.which)
  225. return which<other.which;
  226. if(whichTown->t->id!=other.whichTown->t->id)
  227. return whichTown->t->id < other.whichTown->t->id;
  228. return false;
  229. }
  230. void CGeniusAI::TownObjective::print() const
  231. {
  232. CBuilding * b;
  233. const CCreature *creature;
  234. HypotheticalGameState::HeroModel hm;
  235. int ID, howMany, newID, hSlot;
  236. switch(type)
  237. {
  238. case recruitHero:
  239. cout << "recruit hero.";break;
  240. case buildBuilding:
  241. b = VLC->buildh->buildings[whichTown->t->subID][which];
  242. cout << "build " << b->Name() << " cost = ";
  243. if(b->resources.size()!=0)
  244. {
  245. if(b->resources[0]!=0)cout << b->resources[0] << " wood. ";
  246. if(b->resources[1]!=0)cout << b->resources[1] << " mercury. ";
  247. if(b->resources[2]!=0)cout << b->resources[2] << " ore. ";
  248. if(b->resources[3]!=0)cout << b->resources[3] << " sulfur. ";
  249. if(b->resources[4]!=0)cout << b->resources[4] << " cristal. ";
  250. if(b->resources[5]!=0)cout << b->resources[5] << " gems. ";
  251. if(b->resources[6]!=0)cout << b->resources[6] << " gold. ";
  252. }
  253. break;
  254. case recruitCreatures:
  255. ID = whichTown->creaturesToRecruit[which].second.back(); //buy upgraded if possible
  256. creature = &VLC->creh->creatures[ID];
  257. howMany = whichTown->creaturesToRecruit[which].first;
  258. for(int i = 0; i < creature->cost.size();i++)
  259. amin(howMany,creature->cost[i]?AI->m_cb->getResourceAmount(i)/creature->cost[i]:INT_MAX);
  260. cout << "recruit " << howMany << " "<< creature->namePl << " (Total AI Strength " << creature->AIValue*howMany << "). cost = ";
  261. if(creature->cost.size()!=0)
  262. {
  263. if(creature->cost[0]!=0)cout << creature->cost[0]*howMany << " wood. ";
  264. if(creature->cost[1]!=0)cout << creature->cost[1]*howMany << " mercury. ";
  265. if(creature->cost[2]!=0)cout << creature->cost[2]*howMany << " ore. ";
  266. if(creature->cost[3]!=0)cout << creature->cost[3]*howMany << " sulfur. ";
  267. if(creature->cost[4]!=0)cout << creature->cost[4]*howMany << " cristal. ";
  268. if(creature->cost[5]!=0)cout << creature->cost[5]*howMany << " gems. ";
  269. if(creature->cost[6]!=0)cout << creature->cost[6]*howMany << " gold. ";
  270. }
  271. break;
  272. case upgradeCreatures:
  273. UpgradeInfo ui = AI->m_cb->getUpgradeInfo(whichTown->t,which);
  274. ID = whichTown->creaturesInGarrison.slots[which].first;
  275. cout << "upgrade " << VLC->creh->creatures[ID].namePl;
  276. //ui.cost
  277. break;
  278. }
  279. }
  280. CGeniusAI::CGeniusAI()
  281. : m_generalAI(), m_state(NO_BATTLE)
  282. {
  283. m_priorities = new Priorities("AI/GeniusAI.brain");
  284. }
  285. CGeniusAI::~CGeniusAI()
  286. {
  287. delete m_priorities;
  288. }
  289. void CGeniusAI::init(ICallback *CB)
  290. {
  291. m_cb = CB;
  292. m_generalAI.init(CB);
  293. human = false;
  294. playerID = m_cb->getMyColor();
  295. serialID = m_cb->getMySerial();
  296. std::string info = std::string("GeniusAI initialized for player ") + boost::lexical_cast<std::string>(playerID);
  297. m_battleLogic = NULL;
  298. DbgBox(info.c_str());
  299. }
  300. void CGeniusAI::reportResources()
  301. {
  302. cout << "Day " << m_cb->getDate() << ": ";
  303. cout << "AI Player " <<m_cb->getMySerial()<< " with " << m_cb->howManyHeroes(true) << " heroes. " << endl;
  304. cout << m_cb->getResourceAmount(0) << " wood. ";
  305. cout << m_cb->getResourceAmount(1) << " mercury. ";
  306. cout << m_cb->getResourceAmount(2) << " ore. ";
  307. cout << m_cb->getResourceAmount(3) << " sulfur. ";
  308. cout << m_cb->getResourceAmount(4) << " cristal. ";
  309. cout << m_cb->getResourceAmount(5) << " gems. ";
  310. cout << m_cb->getResourceAmount(6) << " gold.";
  311. cout << endl;
  312. }
  313. void CGeniusAI::addHeroObjectives(CGeniusAI::HypotheticalGameState::HeroModel &h, CGeniusAI::HypotheticalGameState &hgs)
  314. {
  315. int3 hpos, destination;
  316. CPath path;
  317. hpos = h.pos;
  318. int movement = h.remainingMovement;
  319. int3 interestingPos;
  320. int maxInteresting=0;
  321. AIObjective::Type tp = AIObjective::visit;
  322. if(h.finished) return;
  323. for(std::set<AIObjectContainer>::const_iterator i = hgs.knownVisitableObjects.begin(); i != hgs.knownVisitableObjects.end();i++)
  324. {
  325. tp = AIObjective::visit;
  326. if( h.previouslyVisited_pos==i->o->getSightCenter())
  327. continue;
  328. //TODO: what would the hero actually visit if he went to that spot
  329. // maybe the hero wants to visit a seemingly unguarded enemy town, but there is a hero on top of it.
  330. //if(i->o->)
  331. if(i->o->ID!=HEROI_TYPE) //unless you are trying to visit a hero
  332. {
  333. bool heroThere = false;
  334. for(int ii = 0; ii < hgs.heroModels.size();ii++)
  335. if(hgs.heroModels[ii].pos==i->o->getSightCenter())
  336. heroThere = true;
  337. if(heroThere) //it won't work if there is already someone visiting that spot.
  338. continue;
  339. }
  340. if(i->o->ID==HEROI_TYPE&&i->o->getOwner()==m_cb->getMyColor())//visiting friendly heroes not yet supported
  341. continue;
  342. if(i->o->id==h.h->id) //don't visit yourself (should be caught by above)
  343. continue;
  344. if(i->o->ID==53&&i->o->getOwner()==m_cb->getMyColor())//don't visit a mine if you own, there's almost no point(maybe to leave guards or because the hero's trapped).
  345. continue;
  346. if(i->o->getOwner()!=m_cb->getMyColor())
  347. {
  348. int enemyStrength = 0; //TODO: I feel like the AI shouldn't have access to this information.
  349. // We must get an approximation based on few, many, ... zounds etc.
  350. if(dynamic_cast<const CArmedInstance *> (i->o))
  351. enemyStrength = (dynamic_cast<const CArmedInstance *> (i->o))->getArmyStrength();//TODO: should be virtual maybe, Army strength should be comparable across objects
  352. if(dynamic_cast<const CGHeroInstance *> (i->o))
  353. enemyStrength = (dynamic_cast<const CGHeroInstance *> (i->o))->getTotalStrength();
  354. if(dynamic_cast<const CGTownInstance *> (i->o))
  355. enemyStrength = (dynamic_cast<const CGTownInstance *> (i->o))->getArmyStrength()*1.2;
  356. float heroStrength = h.h->getTotalStrength();
  357. if(enemyStrength*2.5 > heroStrength) //TODO: ballence these numbers using objective cost formula.
  358. continue; // it would be nice to do a battle sim
  359. if(enemyStrength>0)tp = AIObjective::attack;
  360. }
  361. if(dynamic_cast<const CGVisitableOPW *> (i->o)&&dynamic_cast<const CGVisitableOPW *> (i->o)->visited)//don't visit things that have already been visited this week.
  362. continue;
  363. if(dynamic_cast<const CGVisitableOPH *> (i->o)&&vstd::contains(dynamic_cast<const CGVisitableOPH *> (i->o)->visitors,h.h->id))//don't visit things that you have already visited OPH
  364. continue;
  365. if(i->o->ID==88||i->o->ID==89||i->o->ID==90)
  366. {
  367. //TODO: if no spell book continue
  368. //TODO: if the shrine's spell is identified, and the hero already has it, continue
  369. }
  370. destination = i->o->getSightCenter();
  371. if(hpos.z==destination.z) //don't try to take a path from the underworld to the top or vice versa
  372. { //TODO: fix get path so that it doesn't return a path unless z's are the same, or path goes through sub gate
  373. if(m_cb->getPath(hpos,destination,h.h,path))
  374. {
  375. path.convert(0);
  376. if(path.nodes[0].dist<movement)
  377. {
  378. HeroObjective ho(hgs,tp,i->o,&h,this);
  379. std::set<HeroObjective>::iterator found = currentHeroObjectives.find(ho);
  380. if(found==currentHeroObjectives.end())
  381. currentHeroObjectives.insert(ho);
  382. else {
  383. HeroObjective *objective = (HeroObjective *)&(*found);
  384. objective->whoCanAchieve.push_back(&h);
  385. }
  386. }
  387. // find the most interesting object that is eventually reachable, and set that position to the ultimate goal position
  388. int hi = rand(); //TODO: replace random numbers with some sort of ranking system
  389. if(hi>maxInteresting)
  390. {
  391. maxInteresting = hi;
  392. interestingPos = destination;
  393. }
  394. }
  395. }
  396. }
  397. h.interestingPos = interestingPos;
  398. // if(h.remainingMovement>0&&m_cb->getPath(hpos,interestingPos,h.h,path)) // there ought to be a path
  399. currentHeroObjectives.insert(HeroObjective(hgs,HeroObjective::finishTurn,h.h,&h,this));
  400. }
  401. void CGeniusAI::HeroObjective::fulfill(CGeniusAI & cg,HypotheticalGameState & hgs)
  402. {
  403. cg.m_cb->waitTillRealize = true;
  404. HypotheticalGameState::HeroModel * h;
  405. int3 hpos, destination;
  406. CPath path;
  407. CPath path2;
  408. int3 bestPos,currentPos,checkPos;
  409. int howGood;
  410. switch(type)
  411. {
  412. case finishTurn:
  413. h = whoCanAchieve.front();
  414. h->finished=true;
  415. hpos = h->pos;
  416. destination = h->interestingPos;
  417. if(!cg.m_cb->getPath(hpos,destination,h->h,path)) {cout << "AI error: invalid destination" << endl; return;}
  418. destination = h->pos;
  419. for(int i = path.nodes.size()-2;i>=0;i--) //find closest coord that we can get to
  420. if(cg.m_cb->getPath(hpos,path.nodes[i].coord,h->h,path2)&&path2.nodes[0].dist<=h->remainingMovement)
  421. destination = path.nodes[i].coord;
  422. if(destination == h->interestingPos) break;
  423. ///////// Find close pos with the most neighboring empty squares. We don't want to get in the way. ///////////////////
  424. bestPos = destination;
  425. howGood=0;
  426. for(int x = -3;x <= 3;x++)
  427. for(int y = -3;y <= 3;y++)
  428. {
  429. currentPos=destination+int3(x,y,0);
  430. if(cg.m_cb->getVisitableObjs(currentPos).size()!=0) //there better not be anything there
  431. continue;
  432. if(!cg.m_cb->getPath(hpos,currentPos,h->h,path)||path.nodes[0].dist>h->remainingMovement) //it better be reachable from the hero
  433. continue;
  434. int count = 0;
  435. for(int xx = -1;xx <= 1;xx++)
  436. for(int yy = -1;yy <= 1;yy++)
  437. {
  438. checkPos = currentPos+int3(xx,yy,0);
  439. if(cg.m_cb->getPath(currentPos,checkPos,h->h,path))
  440. count++;
  441. }
  442. if(count > howGood)
  443. {
  444. howGood = count;
  445. bestPos = currentPos;
  446. }
  447. }
  448. destination = bestPos;
  449. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  450. cg.m_cb->getPath(hpos,destination,h->h,path);
  451. path.convert(0);
  452. break;
  453. case visit:case attack:
  454. h = whoCanAchieve.front(); //lowest cost hero
  455. h->previouslyVisited_pos=object->getSightCenter();
  456. hpos = h->pos;
  457. destination = object->getSightCenter();
  458. break;
  459. }
  460. if(type == visit||type == finishTurn||type == attack)
  461. if(cg.m_cb->getPath(hpos,destination,h->h,path))
  462. {
  463. path.convert(0);
  464. if(cg.m_state.get() != NO_BATTLE)
  465. cg.m_state.waitUntil(NO_BATTLE);//wait for battle end
  466. //wait over, battle over too. hero might be killed. check.
  467. for(int i = path.nodes.size()-2;i>=0&&(cg.m_cb->getHeroSerial(h->h) >= 0);i--)
  468. {
  469. cg.m_cb->moveHero(h->h,path.nodes[i].coord);
  470. if(cg.m_state.get() != NO_BATTLE)
  471. cg.m_state.waitUntil(NO_BATTLE);//wait for battle end
  472. }
  473. h->remainingMovement-=path.nodes[0].dist;
  474. if(object->blockVisit)
  475. h->pos = path.nodes[1].coord;
  476. else
  477. h->pos=destination;
  478. std::set<AIObjectContainer>::iterator i = hgs.knownVisitableObjects.find(AIObjectContainer(object));
  479. if(i!=hgs.knownVisitableObjects.end())
  480. hgs.knownVisitableObjects.erase(i);
  481. }
  482. const CGTownInstance * town = dynamic_cast<const CGTownInstance *> (object);
  483. if(town&&object->getOwner()==cg.m_cb->getMyColor())
  484. {
  485. //upgrade hero's units
  486. cout << "visiting town" << endl;
  487. CCreatureSet hcreatures = h->h->army;
  488. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = hcreatures.slots.begin();i!=hcreatures.slots.end();i++) // for each hero slot
  489. {
  490. UpgradeInfo ui = cg.m_cb->getUpgradeInfo(h->h,i->first);
  491. bool canUpgrade = false;
  492. if(ui.newID.size()!=0) //does this stack need upgrading?
  493. {
  494. canUpgrade = true;
  495. for(int ii=0;ii<ui.cost.size();ii++)//can afford the upgrade?
  496. for (std::set<std::pair<int,int> >::iterator j=ui.cost[ii].begin(); j!=ui.cost[ii].end(); j++)
  497. if(hgs.resourceAmounts[j->first] < j->second*i->second.second)
  498. canUpgrade = false;
  499. }
  500. if(canUpgrade)
  501. {
  502. cg.m_cb->upgradeCreature(h->h,i->first,ui.newID.back());
  503. cout << "upgrading hero's " << VLC->creh->creatures[i->second.first].namePl << endl;
  504. }
  505. }
  506. //give town's units to hero
  507. CCreatureSet tcreatures = town->army;
  508. int weakestCreatureStack;
  509. int weakestCreatureAIValue=99999;
  510. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = tcreatures.slots.begin();i!=tcreatures.slots.end();i++)
  511. if(VLC->creh->creatures[i->second.first].AIValue<weakestCreatureAIValue)
  512. {
  513. weakestCreatureAIValue = VLC->creh->creatures[i->second.first].AIValue;
  514. weakestCreatureStack = i->first;
  515. }
  516. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = tcreatures.slots.begin();i!=tcreatures.slots.end();i++) // for each town slot
  517. {
  518. hcreatures = h->h->army;
  519. int hSlot = hcreatures.getSlotFor(i->second.first);
  520. if(hSlot == -1) continue;
  521. cout << "giving hero " << VLC->creh->creatures[i->second.first].namePl << endl;
  522. if(hcreatures.slots.find(hSlot)!=hcreatures.slots.end())
  523. {
  524. if(i->first==weakestCreatureStack&&town->garrisonHero!=NULL)//can't take garrisonHero's last unit
  525. cg.m_cb->splitStack(town,h->h,i->first,hSlot,i->second.second-1);
  526. else
  527. cg.m_cb->mergeStacks(town,h->h,i->first,hSlot); //TODO: the comment says that this code is not safe for the AI.
  528. }
  529. else
  530. {
  531. cg.m_cb->swapCreatures(town,h->h,i->first,hSlot);
  532. }
  533. }
  534. }
  535. }
  536. void CGeniusAI::addTownObjectives(HypotheticalGameState::TownModel &t, HypotheticalGameState & hgs)
  537. {
  538. //recruitHero
  539. //buildBuilding
  540. //recruitCreatures
  541. //upgradeCreatures
  542. if(hgs.heroModels.size()<3&&hgs.resourceAmounts[6]>=2500) //recruitHero
  543. {
  544. bool heroAtTown = false;
  545. for(int i = 0; i < hgs.heroModels.size();i++)
  546. if(hgs.heroModels[i].pos==t.t->getSightCenter())
  547. heroAtTown = true;
  548. if(!heroAtTown && vstd::contains(t.t->builtBuildings, 5)) //no visiting hero and built tavern
  549. {
  550. for(int i =0; i < hgs.AvailableHeroesToBuy.size();i++)
  551. if(hgs.AvailableHeroesToBuy[i]!=NULL&&(t.t->subID==(hgs.AvailableHeroesToBuy[i]->type->heroType/2)))
  552. {
  553. TownObjective to(hgs,AIObjective::recruitHero,&t,0,this);
  554. currentTownObjectives.insert(to);
  555. }
  556. }
  557. }
  558. //buildBuilding
  559. if(!t.hasBuilt)
  560. {
  561. std::map<int, CBuilding *> thisTownsBuildings = VLC->buildh->buildings[t.t->subID];// m_cb->getCBuildingsByID(t.t);
  562. for(std::map<int, CBuilding *>::iterator i = thisTownsBuildings.begin(); i != thisTownsBuildings.end();i++)
  563. {
  564. if(m_cb->canBuildStructure(t.t,i->first)==7)
  565. {
  566. TownObjective to(hgs,AIObjective::buildBuilding,&t,i->first,this);
  567. currentTownObjectives.insert(to);
  568. }
  569. }
  570. }
  571. //recruitCreatures
  572. for(int i = 0; i < t.creaturesToRecruit.size() ;i++)
  573. {
  574. if(t.creaturesToRecruit[i].first==0||t.creaturesToRecruit[i].second.empty()) continue;
  575. int ID = t.creaturesToRecruit[i].second.back();
  576. const CCreature *creature = &VLC->creh->creatures[ID];//m_cb->getCCreatureByID(ID);
  577. bool canAfford = true;
  578. for(int ii = 0;ii<creature->cost.size();ii++)
  579. if(creature->cost[ii]>hgs.resourceAmounts[ii])
  580. canAfford = false; // can we afford at least one creature?
  581. if(!canAfford) continue;
  582. //cout << "town has " << t.t->creatures[i].first << " "<< creature->namePl << " (AI Strength " << creature->AIValue << ")." << endl;
  583. TownObjective to(hgs,AIObjective::recruitCreatures,&t,i,this);
  584. currentTownObjectives.insert(to);
  585. }
  586. //upgradeCreatures
  587. for(std::map<si32,std::pair<ui32,si32> >::iterator i = t.creaturesInGarrison.slots.begin();i!=t.creaturesInGarrison.slots.end();i++)
  588. {
  589. UpgradeInfo ui = m_cb->getUpgradeInfo(t.t,i->first);
  590. if(ui.newID.size()!=0)
  591. {
  592. bool canAfford = true;
  593. int upgrade_serial = ui.newID.size()-1;
  594. for (std::set<std::pair<int,int> >::iterator j=ui.cost[upgrade_serial].begin(); j!=ui.cost[upgrade_serial].end(); j++)
  595. if(hgs.resourceAmounts[j->first] < j->second*i->second.second)
  596. canAfford = false;
  597. if(canAfford)
  598. {
  599. TownObjective to(hgs,AIObjective::upgradeCreatures,&t,i->first,this);
  600. currentTownObjectives.insert(to);
  601. }
  602. }
  603. }
  604. }
  605. void CGeniusAI::TownObjective::fulfill(CGeniusAI & cg,HypotheticalGameState &hgs)
  606. {
  607. cg.m_cb->waitTillRealize = true;
  608. CBuilding * b;
  609. const CCreature *creature;
  610. HypotheticalGameState::HeroModel hm;
  611. int ID, howMany, newID, hSlot;
  612. switch(type)
  613. {
  614. case recruitHero:
  615. cg.m_cb->recruitHero(whichTown->t,hgs.AvailableHeroesToBuy[which]);
  616. hm = HypotheticalGameState::HeroModel(hgs.AvailableHeroesToBuy[which]);
  617. hm.pos = whichTown->t->getSightCenter();
  618. hm.remainingMovement = hm.h->maxMovePoints(true);
  619. hgs.heroModels.push_back(hm);
  620. hgs.resourceAmounts[6]-=2500;
  621. break;
  622. case buildBuilding:
  623. b = VLC->buildh->buildings[whichTown->t->subID][which];
  624. if(cg.m_cb->canBuildStructure(whichTown->t,which)==7)
  625. {
  626. cout << "built " << b->Name() << "." << endl;
  627. if(!cg.m_cb->buildBuilding(whichTown->t,which)) cout << "really tried to build unbuildable building" <<endl;
  628. for(int i = 0; b && i < b->resources.size();i++)
  629. hgs.resourceAmounts[i]-=b->resources[i];
  630. }
  631. else cout << "trying to build a structure we cannot build" << endl;
  632. whichTown->hasBuilt=true;
  633. break;
  634. case recruitCreatures:
  635. ID = whichTown->creaturesToRecruit[which].second.back(); //buy upgraded if possible
  636. creature = &VLC->creh->creatures[ID];
  637. howMany = whichTown->creaturesToRecruit[which].first;
  638. for(int i = 0; i < creature->cost.size();i++)
  639. amin(howMany,creature->cost[i]?hgs.resourceAmounts[i]/creature->cost[i]:INT_MAX);
  640. if(howMany == 0) cout << "tried to recruit without enough money.";
  641. cout << "recruiting " << howMany << " "<< creature->namePl << " (Total AI Strength " << creature->AIValue*howMany << ")." << endl;
  642. cg.m_cb->recruitCreatures(whichTown->t,ID,howMany);
  643. break;
  644. case upgradeCreatures:
  645. UpgradeInfo ui = cg.m_cb->getUpgradeInfo(whichTown->t,which);
  646. ID = whichTown->creaturesInGarrison.slots[which].first;
  647. newID = ui.newID.back();
  648. cg.m_cb->upgradeCreature(whichTown->t,which,newID);//TODO: reduce resources in hgs
  649. cout << "upgrading " << VLC->creh->creatures[ID].namePl << endl;
  650. break;
  651. }
  652. }
  653. void CGeniusAI::fillObjectiveQueue(HypotheticalGameState & hgs)
  654. {
  655. objectiveQueue.clear();
  656. currentHeroObjectives.clear();
  657. currentTownObjectives.clear();
  658. for(std::vector <CGeniusAI::HypotheticalGameState::HeroModel>::iterator i = hgs.heroModels.begin(); i != hgs.heroModels.end(); i++)
  659. addHeroObjectives(*i,hgs);
  660. for(std::vector <CGeniusAI::HypotheticalGameState::TownModel>::iterator i = hgs.townModels.begin(); i != hgs.townModels.end(); i++)
  661. addTownObjectives(*i,hgs);
  662. for(std::set<CGeniusAI::HeroObjective>::iterator i = currentHeroObjectives.begin(); i != currentHeroObjectives.end(); i++)
  663. objectiveQueue.push_back(AIObjectivePtrCont((CGeniusAI::HeroObjective *)&(*i)));
  664. for(std::set<CGeniusAI::TownObjective>::iterator i = currentTownObjectives.begin(); i != currentTownObjectives.end(); i++)
  665. objectiveQueue.push_back(AIObjectivePtrCont((CGeniusAI::TownObjective *)&(*i)));
  666. }
  667. CGeniusAI::AIObjective * CGeniusAI::getBestObjective()
  668. {
  669. trueGameState.update(*this);
  670. fillObjectiveQueue(trueGameState);
  671. // if(!objectiveQueue.empty())
  672. // return max_element(objectiveQueue.begin(),objectiveQueue.end())->obj;
  673. m_priorities->fillFeatures(trueGameState);
  674. if(objectiveQueue.empty()) return NULL;
  675. // sort(objectiveQueue.begin(),objectiveQueue.end());
  676. // reverse(objectiveQueue.begin(),objectiveQueue.end());
  677. int num= 1;
  678. // for(std::vector<AIObjectivePtrCont> ::iterator i = objectiveQueue.begin(); i < objectiveQueue.end();i++)
  679. // {
  680. // if(!dynamic_cast<HeroObjective*>(i->obj))continue;
  681. // cout << num++ << ": ";
  682. // i->obj->print();
  683. // cout << " value: " << i->obj->getValue();
  684. // cout << endl;
  685. // }
  686. // int choice = 0;
  687. // cout << "which would you do? (enter 0 for none): ";
  688. // cin >> choice;
  689. cout << "doing best of " << objectiveQueue.size() << " ";
  690. CGeniusAI::AIObjective* best = max_element(objectiveQueue.begin(),objectiveQueue.end())->obj;
  691. best->print();
  692. cout << " value = " << best->getValue() << endl;
  693. if(!objectiveQueue.empty())
  694. return best;
  695. return objectiveQueue.front().obj;
  696. }
  697. void CGeniusAI::yourTurn()
  698. {
  699. static boost::mutex mutex;
  700. boost::mutex::scoped_lock lock(mutex);
  701. m_cb->waitTillRealize = true;
  702. static int seed = rand();
  703. srand(seed);
  704. if(m_cb->getDate()==1)
  705. {
  706. // startFirstTurn();
  707. // m_cb->endTurn();
  708. // return;
  709. }
  710. //////////////TODO: replace with updates. Also add suspected objects list./////////
  711. knownVisitableObjects.clear();
  712. int3 pos = m_cb->getMapSize();
  713. for(int x = 0;x<pos.x;x++)
  714. for(int y = 0;y<pos.y;y++)
  715. for(int z = 0;z<pos.z;z++)
  716. tileRevealed(int3(x,y,z));
  717. ///////////////////////////////////////////////////////////////////////////////////
  718. reportResources();
  719. trueGameState = HypotheticalGameState(*this);
  720. AIObjective * objective;
  721. while((objective = getBestObjective())!=NULL)
  722. objective->fulfill(*this,trueGameState);
  723. seed = rand();
  724. m_cb->endTurn();
  725. m_cb->waitTillRealize = false;
  726. }
  727. /*
  728. void CGeniusAI::startFirstTurn()
  729. {
  730. HypotheticalGameState hgs(*this);
  731. const CGTownInstance * town = m_cb->getTownInfo(0,0);
  732. const CGHeroInstance * heroInst = m_cb->getHeroInfo(0,0);
  733. TownObjective(hgs,AIObjective::recruitHero,&hgs.townModels.front(),0,this).fulfill(*this,hgs);
  734. m_cb->swapGarrisonHero(town);
  735. hgs.update(*this);
  736. for(int i = 0; i < hgs.townModels.front().creaturesToRecruit.size() ;i++)
  737. {
  738. if(hgs.townModels.front().creaturesToRecruit[i].first==0) continue;
  739. int ID = hgs.townModels.front().creaturesToRecruit[i].second.back();
  740. const CCreature *creature = &VLC->creh->creatures[ID];
  741. bool canAfford = true;
  742. for(int ii = 0;ii<creature->cost.size();ii++)
  743. if(creature->cost[ii]>hgs.resourceAmounts[ii])
  744. canAfford = false; // can we afford at least one creature?
  745. if(!canAfford) continue;
  746. TownObjective(hgs,AIObjective::recruitCreatures,&hgs.townModels.front(),i,this).fulfill(*this,hgs);
  747. }
  748. hgs.update(*this);
  749. HypotheticalGameState::HeroModel *hero;
  750. for(int i = 0; i < hgs.heroModels.size();i++)
  751. if(hgs.heroModels[i].h->id==heroInst->id)
  752. HeroObjective(hgs,AIObjective::visit,town,hero=&hgs.heroModels[i],this).fulfill(*this,hgs);
  753. hgs.update(*this);
  754. // m_cb->swapGarrisonHero(town);
  755. //TODO: choose the strongest hero.
  756. }
  757. */
  758. void CGeniusAI::heroKilled(const CGHeroInstance * hero)
  759. {
  760. }
  761. void CGeniusAI::heroCreated(const CGHeroInstance *hero)
  762. {
  763. }
  764. void CGeniusAI::tileRevealed(int3 pos)
  765. {
  766. std::vector < const CGObjectInstance * > objects = m_cb->getVisitableObjs(pos);
  767. for(std::vector < const CGObjectInstance * >::iterator o = objects.begin();o!=objects.end();o++)
  768. if((*o)->id!=-1)
  769. knownVisitableObjects.insert(*o);
  770. objects = m_cb->getFlaggableObjects(pos);
  771. for(std::vector < const CGObjectInstance * >::iterator o = objects.begin();o!=objects.end();o++)
  772. if((*o)->id!=-1)
  773. knownVisitableObjects.insert(*o);
  774. }
  775. void CGeniusAI::newObject(const CGObjectInstance * obj) //eg. ship built in shipyard
  776. {
  777. knownVisitableObjects.insert(obj);
  778. }
  779. void CGeniusAI::objectRemoved(const CGObjectInstance *obj) //eg. collected resource, picked artifact, beaten hero
  780. {
  781. std::set <AIObjectContainer>::iterator o = knownVisitableObjects.find(obj);
  782. if(o!=knownVisitableObjects.end())
  783. knownVisitableObjects.erase(o);
  784. }
  785. void CGeniusAI::tileHidden(int3 pos)
  786. {
  787. }
  788. void CGeniusAI::heroMoved(const TryMoveHero &TMH)
  789. {
  790. //DbgBox("** CGeniusAI::heroMoved **");
  791. }
  792. void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  793. {
  794. callback(rand() % skills.size());
  795. }
  796. void GeniusAI::CGeniusAI::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd )
  797. {
  798. onEnd();
  799. }
  800. void GeniusAI::CGeniusAI::playerBlocked( int reason )
  801. {
  802. if(reason == 0) //battle is coming...
  803. {
  804. m_state.setn(UPCOMING_BATTLE);
  805. }
  806. }
  807. void GeniusAI::CGeniusAI::battleResultsApplied()
  808. {
  809. assert(m_state.get() == ENDING_BATTLE);
  810. m_state.setn(NO_BATTLE);
  811. }
  812. void CGeniusAI::showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel)
  813. {
  814. m_cb->selectionMade(cancel ? 0 : 1, askID);
  815. }
  816. /**
  817. * occurs AFTER every action taken by any stack or by the hero
  818. */
  819. void CGeniusAI::actionFinished(const BattleAction *action)
  820. {
  821. std::string message("\t\tCGeniusAI::actionFinished - type(");
  822. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  823. message += "), side(";
  824. message += boost::lexical_cast<std::string>((unsigned)action->side);
  825. message += ")";
  826. DbgBox(message.c_str());
  827. }
  828. /**
  829. * occurs BEFORE every action taken by any stack or by the hero
  830. */
  831. void CGeniusAI::actionStarted(const BattleAction *action)
  832. {
  833. std::string message("\t\tCGeniusAI::actionStarted - type(");
  834. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  835. message += "), side(";
  836. message += boost::lexical_cast<std::string>((unsigned)action->side);
  837. message += ")";
  838. DbgBox(message.c_str());
  839. }
  840. /**
  841. * called when stack is performing attack
  842. */
  843. void CGeniusAI::battleAttack(BattleAttack *ba)
  844. {
  845. DbgBox("\t\t\tCGeniusAI::battleAttack");
  846. }
  847. /**
  848. * called when stack receives damage (after battleAttack())
  849. */
  850. void CGeniusAI::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  851. {
  852. DbgBox("\t\t\tCGeniusAI::battleStacksAttacked");
  853. }
  854. /**
  855. * called by engine when battle starts; side=0 - left, side=1 - right
  856. */
  857. void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  858. {
  859. assert(!m_battleLogic);
  860. assert(playerID > PLAYER_LIMIT || m_state.get() == UPCOMING_BATTLE); //we have been informed that battle will start (or we are neutral AI)
  861. m_state.setn(ONGOING_BATTLE);
  862. m_battleLogic = new BattleAI::CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
  863. DbgBox("** CGeniusAI::battleStart **");
  864. }
  865. /**
  866. *
  867. */
  868. void CGeniusAI::battleEnd(BattleResult *br)
  869. {
  870. switch(br->winner)
  871. {
  872. case 0: std::cout << "The winner is the attacker." << std::endl;break;
  873. case 1: std::cout << "The winner is the defender." << std::endl;break;
  874. case 2: std::cout << "It's a draw." << std::endl;break;
  875. };
  876. cout << "lost ";
  877. for(std::set<std::pair<ui32,si32> >::iterator i = br->casualties[0].begin(); i !=br->casualties[0].end();i++)
  878. cout << i->second << " " << VLC->creh->creatures[i->first].namePl << endl;
  879. delete m_battleLogic;
  880. m_battleLogic = NULL;
  881. assert(m_state.get() == ONGOING_BATTLE);
  882. m_state.setn(ENDING_BATTLE);
  883. DbgBox("** CGeniusAI::battleEnd **");
  884. }
  885. /**
  886. * called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  887. */
  888. void CGeniusAI::battleNewRound(int round)
  889. {
  890. std::string message("\tCGeniusAI::battleNewRound - ");
  891. message += boost::lexical_cast<std::string>(round);
  892. DbgBox(message.c_str());
  893. m_battleLogic->SetCurrentTurn(round);
  894. }
  895. /**
  896. *
  897. */
  898. void CGeniusAI::battleStackMoved(int ID, int dest, int distance, bool end)
  899. {
  900. std::string message("\t\t\tCGeniusAI::battleStackMoved ID(");
  901. message += boost::lexical_cast<std::string>(ID);
  902. message += "), dest(";
  903. message += boost::lexical_cast<std::string>(dest);
  904. message += ")";
  905. DbgBox(message.c_str());
  906. }
  907. /**
  908. *
  909. */
  910. void CGeniusAI::battleSpellCast(SpellCast *sc)
  911. {
  912. DbgBox("\t\t\tCGeniusAI::battleSpellCast");
  913. }
  914. /**
  915. * called when battlefield is prepared, prior the battle beginning
  916. */
  917. void CGeniusAI::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles)
  918. {
  919. DbgBox("CGeniusAI::battlefieldPrepared");
  920. }
  921. /**
  922. *
  923. */
  924. void CGeniusAI::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  925. {
  926. DbgBox("\t\t\tCGeniusAI::battleStackMoved");
  927. }
  928. /**
  929. *
  930. */
  931. void CGeniusAI::battleStackAttacking(int ID, int dest)
  932. {
  933. DbgBox("\t\t\tCGeniusAI::battleStackAttacking");
  934. }
  935. /**
  936. *
  937. */
  938. void CGeniusAI::battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  939. {
  940. DbgBox("\t\t\tCGeniusAI::battleStackIsAttacked");
  941. }
  942. /**
  943. * called when it's turn of that stack
  944. */
  945. BattleAction CGeniusAI::activeStack(int stackID)
  946. {
  947. std::string message("\t\t\tCGeniusAI::activeStack stackID(");
  948. message += boost::lexical_cast<std::string>(stackID);
  949. message += ")";
  950. DbgBox(message.c_str());
  951. BattleAction bact = m_battleLogic->MakeDecision(stackID);
  952. assert(m_cb->battleGetStackByID(bact.stackNumber));
  953. return bact;
  954. };