CGeniusAI.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. m_cb->endTurn();
  703. return;
  704. static int seed = rand();
  705. srand(seed);
  706. if(m_cb->getDate()==1)
  707. {
  708. // startFirstTurn();
  709. // m_cb->endTurn();
  710. // return;
  711. }
  712. //////////////TODO: replace with updates. Also add suspected objects list./////////
  713. knownVisitableObjects.clear();
  714. int3 pos = m_cb->getMapSize();
  715. for(int x = 0;x<pos.x;x++)
  716. for(int y = 0;y<pos.y;y++)
  717. for(int z = 0;z<pos.z;z++)
  718. tileRevealed(int3(x,y,z));
  719. ///////////////////////////////////////////////////////////////////////////////////
  720. reportResources();
  721. trueGameState = HypotheticalGameState(*this);
  722. AIObjective * objective;
  723. while((objective = getBestObjective())!=NULL)
  724. objective->fulfill(*this,trueGameState);
  725. seed = rand();
  726. m_cb->endTurn();
  727. m_cb->waitTillRealize = false;
  728. }
  729. /*
  730. void CGeniusAI::startFirstTurn()
  731. {
  732. HypotheticalGameState hgs(*this);
  733. const CGTownInstance * town = m_cb->getTownInfo(0,0);
  734. const CGHeroInstance * heroInst = m_cb->getHeroInfo(0,0);
  735. TownObjective(hgs,AIObjective::recruitHero,&hgs.townModels.front(),0,this).fulfill(*this,hgs);
  736. m_cb->swapGarrisonHero(town);
  737. hgs.update(*this);
  738. for(int i = 0; i < hgs.townModels.front().creaturesToRecruit.size() ;i++)
  739. {
  740. if(hgs.townModels.front().creaturesToRecruit[i].first==0) continue;
  741. int ID = hgs.townModels.front().creaturesToRecruit[i].second.back();
  742. const CCreature *creature = &VLC->creh->creatures[ID];
  743. bool canAfford = true;
  744. for(int ii = 0;ii<creature->cost.size();ii++)
  745. if(creature->cost[ii]>hgs.resourceAmounts[ii])
  746. canAfford = false; // can we afford at least one creature?
  747. if(!canAfford) continue;
  748. TownObjective(hgs,AIObjective::recruitCreatures,&hgs.townModels.front(),i,this).fulfill(*this,hgs);
  749. }
  750. hgs.update(*this);
  751. HypotheticalGameState::HeroModel *hero;
  752. for(int i = 0; i < hgs.heroModels.size();i++)
  753. if(hgs.heroModels[i].h->id==heroInst->id)
  754. HeroObjective(hgs,AIObjective::visit,town,hero=&hgs.heroModels[i],this).fulfill(*this,hgs);
  755. hgs.update(*this);
  756. // m_cb->swapGarrisonHero(town);
  757. //TODO: choose the strongest hero.
  758. }
  759. */
  760. void CGeniusAI::heroKilled(const CGHeroInstance * hero)
  761. {
  762. }
  763. void CGeniusAI::heroCreated(const CGHeroInstance *hero)
  764. {
  765. }
  766. void CGeniusAI::tileRevealed(int3 pos)
  767. {
  768. std::vector < const CGObjectInstance * > objects = m_cb->getVisitableObjs(pos);
  769. for(std::vector < const CGObjectInstance * >::iterator o = objects.begin();o!=objects.end();o++)
  770. if((*o)->id!=-1)
  771. knownVisitableObjects.insert(*o);
  772. objects = m_cb->getFlaggableObjects(pos);
  773. for(std::vector < const CGObjectInstance * >::iterator o = objects.begin();o!=objects.end();o++)
  774. if((*o)->id!=-1)
  775. knownVisitableObjects.insert(*o);
  776. }
  777. void CGeniusAI::newObject(const CGObjectInstance * obj) //eg. ship built in shipyard
  778. {
  779. knownVisitableObjects.insert(obj);
  780. }
  781. void CGeniusAI::objectRemoved(const CGObjectInstance *obj) //eg. collected resource, picked artifact, beaten hero
  782. {
  783. std::set <AIObjectContainer>::iterator o = knownVisitableObjects.find(obj);
  784. if(o!=knownVisitableObjects.end())
  785. knownVisitableObjects.erase(o);
  786. }
  787. void CGeniusAI::tileHidden(int3 pos)
  788. {
  789. }
  790. void CGeniusAI::heroMoved(const TryMoveHero &TMH)
  791. {
  792. //DbgBox("** CGeniusAI::heroMoved **");
  793. }
  794. void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  795. {
  796. callback(rand() % skills.size());
  797. }
  798. void GeniusAI::CGeniusAI::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd )
  799. {
  800. onEnd();
  801. }
  802. void GeniusAI::CGeniusAI::playerBlocked( int reason )
  803. {
  804. if(reason == 0) //battle is coming...
  805. {
  806. m_state.setn(UPCOMING_BATTLE);
  807. }
  808. }
  809. void GeniusAI::CGeniusAI::battleResultsApplied()
  810. {
  811. assert(m_state.get() == ENDING_BATTLE);
  812. m_state.setn(NO_BATTLE);
  813. }
  814. void CGeniusAI::showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel)
  815. {
  816. m_cb->selectionMade(cancel ? 0 : 1, askID);
  817. }
  818. /**
  819. * occurs AFTER every action taken by any stack or by the hero
  820. */
  821. void CGeniusAI::actionFinished(const BattleAction *action)
  822. {
  823. std::string message("\t\tCGeniusAI::actionFinished - type(");
  824. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  825. message += "), side(";
  826. message += boost::lexical_cast<std::string>((unsigned)action->side);
  827. message += ")";
  828. DbgBox(message.c_str());
  829. }
  830. /**
  831. * occurs BEFORE every action taken by any stack or by the hero
  832. */
  833. void CGeniusAI::actionStarted(const BattleAction *action)
  834. {
  835. std::string message("\t\tCGeniusAI::actionStarted - type(");
  836. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  837. message += "), side(";
  838. message += boost::lexical_cast<std::string>((unsigned)action->side);
  839. message += ")";
  840. DbgBox(message.c_str());
  841. }
  842. /**
  843. * called when stack is performing attack
  844. */
  845. void CGeniusAI::battleAttack(BattleAttack *ba)
  846. {
  847. DbgBox("\t\t\tCGeniusAI::battleAttack");
  848. }
  849. /**
  850. * called when stack receives damage (after battleAttack())
  851. */
  852. void CGeniusAI::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  853. {
  854. DbgBox("\t\t\tCGeniusAI::battleStacksAttacked");
  855. }
  856. /**
  857. * called by engine when battle starts; side=0 - left, side=1 - right
  858. */
  859. void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  860. {
  861. assert(!m_battleLogic);
  862. assert(playerID > PLAYER_LIMIT || m_state.get() == UPCOMING_BATTLE); //we have been informed that battle will start (or we are neutral AI)
  863. m_state.setn(ONGOING_BATTLE);
  864. m_battleLogic = new BattleAI::CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
  865. DbgBox("** CGeniusAI::battleStart **");
  866. }
  867. /**
  868. *
  869. */
  870. void CGeniusAI::battleEnd(BattleResult *br)
  871. {
  872. switch(br->winner)
  873. {
  874. case 0: std::cout << "The winner is the attacker." << std::endl;break;
  875. case 1: std::cout << "The winner is the defender." << std::endl;break;
  876. case 2: std::cout << "It's a draw." << std::endl;break;
  877. };
  878. cout << "lost ";
  879. for(std::map<ui32,si32>::iterator i = br->casualties[0].begin(); i !=br->casualties[0].end();i++)
  880. cout << i->second << " " << VLC->creh->creatures[i->first].namePl << endl;
  881. delete m_battleLogic;
  882. m_battleLogic = NULL;
  883. assert(m_state.get() == ONGOING_BATTLE);
  884. m_state.setn(ENDING_BATTLE);
  885. DbgBox("** CGeniusAI::battleEnd **");
  886. }
  887. /**
  888. * called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  889. */
  890. void CGeniusAI::battleNewRound(int round)
  891. {
  892. std::string message("\tCGeniusAI::battleNewRound - ");
  893. message += boost::lexical_cast<std::string>(round);
  894. DbgBox(message.c_str());
  895. m_battleLogic->SetCurrentTurn(round);
  896. }
  897. /**
  898. *
  899. */
  900. void CGeniusAI::battleStackMoved(int ID, int dest, int distance, bool end)
  901. {
  902. std::string message("\t\t\tCGeniusAI::battleStackMoved ID(");
  903. message += boost::lexical_cast<std::string>(ID);
  904. message += "), dest(";
  905. message += boost::lexical_cast<std::string>(dest);
  906. message += ")";
  907. DbgBox(message.c_str());
  908. }
  909. /**
  910. *
  911. */
  912. void CGeniusAI::battleSpellCast(SpellCast *sc)
  913. {
  914. DbgBox("\t\t\tCGeniusAI::battleSpellCast");
  915. }
  916. /**
  917. * called when battlefield is prepared, prior the battle beginning
  918. */
  919. void CGeniusAI::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles)
  920. {
  921. DbgBox("CGeniusAI::battlefieldPrepared");
  922. }
  923. /**
  924. *
  925. */
  926. void CGeniusAI::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  927. {
  928. DbgBox("\t\t\tCGeniusAI::battleStackMoved");
  929. }
  930. /**
  931. *
  932. */
  933. void CGeniusAI::battleStackAttacking(int ID, int dest)
  934. {
  935. DbgBox("\t\t\tCGeniusAI::battleStackAttacking");
  936. }
  937. /**
  938. *
  939. */
  940. void CGeniusAI::battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  941. {
  942. DbgBox("\t\t\tCGeniusAI::battleStackIsAttacked");
  943. }
  944. /**
  945. * called when it's turn of that stack
  946. */
  947. BattleAction CGeniusAI::activeStack(int stackID)
  948. {
  949. std::string message("\t\t\tCGeniusAI::activeStack stackID(");
  950. message += boost::lexical_cast<std::string>(stackID);
  951. message += ")";
  952. DbgBox(message.c_str());
  953. BattleAction bact = m_battleLogic->MakeDecision(stackID);
  954. assert(m_cb->battleGetStackByID(bact.stackNumber));
  955. return bact;
  956. };