CGeniusAI.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. #include "CGeniusAI.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <boost/lexical_cast.hpp>
  6. #include <boost/lambda/lambda.hpp>
  7. #include <boost/lambda/bind.hpp>
  8. #include <boost/lambda/if.hpp>
  9. #include <boost/algorithm/string/split.hpp>
  10. #include <boost/algorithm/string/classification.hpp>
  11. #include <boost/algorithm/string.hpp>
  12. using namespace boost::lambda;
  13. using namespace std;
  14. using namespace GeniusAI;
  15. #if defined (_MSC_VER) && (_MSC_VER >= 1020) && (__MINGW32__)
  16. #include <windows.h>
  17. #endif
  18. void MsgBox(const char *msg, bool messageBox = false)
  19. {
  20. #if defined _DEBUG
  21. # if defined (_MSC_VER) && (_MSC_VER >= 1020)
  22. if (messageBox)
  23. {
  24. MessageBoxA(NULL, msg, "Debug message", MB_OK | MB_ICONASTERISK);
  25. }
  26. # endif
  27. std::cout << msg << std::endl;
  28. #endif
  29. }
  30. void CGeniusAI::init(ICallback *CB)
  31. {
  32. m_cb = CB;
  33. human = false;
  34. playerID = m_cb->getMyColor();
  35. serialID = m_cb->getMySerial();
  36. std::string info = std::string("GeniusAI initialized for player ") + boost::lexical_cast<std::string>(playerID);
  37. m_battleLogic = NULL;
  38. MsgBox(info.c_str());
  39. }
  40. void CGeniusAI::yourTurn()
  41. {
  42. m_cb->endTurn();
  43. }
  44. void CGeniusAI::heroKilled(const CGHeroInstance *)
  45. {
  46. }
  47. void CGeniusAI::heroCreated(const CGHeroInstance *)
  48. {
  49. }
  50. void CGeniusAI::heroMoved(const HeroMoveDetails &)
  51. {
  52. }
  53. void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  54. {
  55. callback(rand() % skills.size());
  56. }
  57. /**
  58. * occurs AFTER every action taken by any stack or by the hero
  59. */
  60. void CGeniusAI::actionFinished(const BattleAction *action)
  61. {
  62. std::string message("\t\tCGeniusAI::actionFinished - type(");
  63. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  64. message += "), side(";
  65. message += boost::lexical_cast<std::string>((unsigned)action->side);
  66. message += ")";
  67. MsgBox(message.c_str());
  68. }
  69. /**
  70. * occurs BEFORE every action taken by any stack or by the hero
  71. */
  72. void CGeniusAI::actionStarted(const BattleAction *action)
  73. {
  74. std::string message("\t\tCGeniusAI::actionStarted - type(");
  75. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  76. message += "), side(";
  77. message += boost::lexical_cast<std::string>((unsigned)action->side);
  78. message += ")";
  79. MsgBox(message.c_str());
  80. }
  81. /**
  82. * called when stack is performing attack
  83. */
  84. void CGeniusAI::battleAttack(BattleAttack *ba)
  85. {
  86. MsgBox("\t\t\tCGeniusAI::battleAttack");
  87. }
  88. /**
  89. * called when stack receives damage (after battleAttack())
  90. */
  91. void CGeniusAI::battleStackAttacked(BattleStackAttacked * bsa)
  92. {
  93. MsgBox("\t\t\tCGeniusAI::battleStackAttacked");
  94. }
  95. /**
  96. * called by engine when battle starts; side=0 - left, side=1 - right
  97. */
  98. void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  99. {
  100. assert(!m_battleLogic);
  101. m_battleLogic = new CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
  102. assert(m_battleLogic);
  103. MsgBox("** CGeniusAI::battleStart **");
  104. }
  105. /**
  106. *
  107. */
  108. void CGeniusAI::battleEnd(BattleResult *br)
  109. {
  110. delete m_battleLogic;
  111. m_battleLogic = NULL;
  112. MsgBox("** CGeniusAI::battleEnd **");
  113. }
  114. /**
  115. * called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  116. */
  117. void CGeniusAI::battleNewRound(int round)
  118. {
  119. std::string message("\tCGeniusAI::battleNewRound - ");
  120. message += boost::lexical_cast<std::string>(round);
  121. MsgBox(message.c_str());
  122. m_battleLogic->SetCurrentTurn(round);
  123. }
  124. /**
  125. *
  126. */
  127. void CGeniusAI::battleStackMoved(int ID, int dest)
  128. {
  129. std::string message("\t\t\tCGeniusAI::battleStackMoved ID(");
  130. message += boost::lexical_cast<std::string>(ID);
  131. message += "), dest(";
  132. message += boost::lexical_cast<std::string>(dest);
  133. message += ")";
  134. MsgBox(message.c_str());
  135. }
  136. /**
  137. *
  138. */
  139. void CGeniusAI::battleSpellCasted(SpellCasted *sc)
  140. {
  141. MsgBox("\t\t\tCGeniusAI::battleSpellCasted");
  142. }
  143. /**
  144. * called when battlefield is prepared, prior the battle beginning
  145. */
  146. void CGeniusAI::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles)
  147. {
  148. MsgBox("CGeniusAI::battlefieldPrepared");
  149. }
  150. /**
  151. *
  152. */
  153. void CGeniusAI::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  154. {
  155. MsgBox("\t\t\tCGeniusAI::battleStackMoved");
  156. }
  157. /**
  158. *
  159. */
  160. void CGeniusAI::battleStackAttacking(int ID, int dest)
  161. {
  162. MsgBox("\t\t\tCGeniusAI::battleStackAttacking");
  163. }
  164. /**
  165. *
  166. */
  167. void CGeniusAI::battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  168. {
  169. MsgBox("\t\t\tCGeniusAI::battleStackIsAttacked");
  170. }
  171. /**
  172. * called when it's turn of that stack
  173. */
  174. BattleAction CGeniusAI::activeStack(int stackID)
  175. {
  176. std::string message("\t\t\tCGeniusAI::activeStack stackID(");
  177. message += boost::lexical_cast<std::string>(stackID);
  178. message += ")";
  179. MsgBox(message.c_str());
  180. return m_battleLogic->MakeDecision(stackID);
  181. };
  182. /*
  183. ui8 side; //who made this action: false - left, true - right player
  184. ui32 stackNumber;//stack ID, -1 left hero, -2 right hero,
  185. ui8 actionType; //
  186. 0 = Cancel BattleAction
  187. 1 = Hero cast a spell
  188. 2 = Walk
  189. 3 = Defend
  190. 4 = Retreat from the battle
  191. 5 = Surrender
  192. 6 = Walk and Attack
  193. 7 = Shoot
  194. 8 = Wait
  195. 9 = Catapult
  196. 10 = Monster casts a spell (i.e. Faerie Dragons)
  197. ui16 destinationTile;
  198. si32 additionalInfo; // e.g. spell number if type is 1 || 10; tile to attack if type is 6
  199. */
  200. /**
  201. * Implementation of CBattleHelper class.
  202. */
  203. CBattleHelper::CBattleHelper():
  204. InfiniteDistance(0xffff),
  205. BattlefieldHeight(11),
  206. BattlefieldWidth(15),
  207. m_voteForDistance(10),
  208. m_voteForDistanceFromShooters(20),
  209. m_voteForHitPoints(10),
  210. m_voteForMaxDamage(10),
  211. m_voteForMaxSpeed(10),
  212. m_voteForMinDamage(10)
  213. {
  214. // loads votes
  215. std::fstream f;
  216. f.open("AI\\CBattleHelper.txt", std::ios::in);
  217. if (f)
  218. {
  219. //char c_line[512];
  220. std::string line;
  221. while (std::getline(f, line, '\n'))
  222. {
  223. //f.getline(c_line, sizeof(c_line), '\n');
  224. //std::string line(c_line);
  225. //std::getline(f, line);
  226. std::vector<std::string> parts;
  227. boost::algorithm::split(parts, line, boost::algorithm::is_any_of("="));
  228. if (parts.size() >= 2)
  229. {
  230. boost::algorithm::trim(parts[0]);
  231. boost::algorithm::trim(parts[1]);
  232. if (parts[0].compare("m_voteForDistance") == 0)
  233. {
  234. try
  235. {
  236. m_voteForDistance = boost::lexical_cast<int>(parts[1]);
  237. }
  238. catch (boost::bad_lexical_cast &)
  239. {}
  240. }
  241. else if (parts[0].compare("m_voteForDistanceFromShooters") == 0)
  242. {
  243. try
  244. {
  245. m_voteForDistanceFromShooters = boost::lexical_cast<int>(parts[1]);
  246. }
  247. catch (boost::bad_lexical_cast &)
  248. {}
  249. }
  250. else if (parts[0].compare("m_voteForHitPoints") == 0)
  251. {
  252. try
  253. {
  254. m_voteForHitPoints = boost::lexical_cast<int>(parts[1]);
  255. }
  256. catch (boost::bad_lexical_cast &)
  257. {}
  258. }
  259. else if (parts[0].compare("m_voteForMaxDamage") == 0)
  260. {
  261. try
  262. {
  263. m_voteForMaxDamage = boost::lexical_cast<int>(parts[1]);
  264. }
  265. catch (boost::bad_lexical_cast &)
  266. {}
  267. }
  268. else if (parts[0].compare("m_voteForMaxSpeed") == 0)
  269. {
  270. try
  271. {
  272. m_voteForMaxSpeed = boost::lexical_cast<int>(parts[1]);
  273. }
  274. catch (boost::bad_lexical_cast &)
  275. {}
  276. }
  277. else if (parts[0].compare("m_voteForMinDamage") == 0)
  278. {
  279. try
  280. {
  281. m_voteForMinDamage = boost::lexical_cast<int>(parts[1]);
  282. }
  283. catch (boost::bad_lexical_cast &)
  284. {}
  285. }
  286. }
  287. }
  288. f.close();
  289. }
  290. }
  291. CBattleHelper::~CBattleHelper()
  292. {}
  293. int CBattleHelper::GetBattleFieldPosition(int x, int y)
  294. {
  295. return x + 17 * (y - 1);
  296. }
  297. int CBattleHelper::DecodeXPosition(int battleFieldPosition)
  298. {
  299. int pos = battleFieldPosition - (DecodeYPosition(battleFieldPosition) - 1) * 17;
  300. assert(pos > 0 && pos < 16);
  301. return pos;
  302. }
  303. int CBattleHelper::DecodeYPosition(int battleFieldPosition)
  304. {
  305. double y = (double)battleFieldPosition / 17.0;
  306. if (y - (int)y > 0.0)
  307. {
  308. return (int)y + 1;
  309. }
  310. assert((int)y > 0 && (int)y <= 11);
  311. return (int)y;
  312. }
  313. int CBattleHelper::GetShortestDistance(int pointA, int pointB)
  314. {
  315. /**
  316. * TODO: function hasn't been checked!
  317. */
  318. int x1 = DecodeXPosition(pointA);
  319. int y1 = DecodeYPosition(pointA);
  320. //
  321. int x2 = DecodeXPosition(pointB);
  322. //x2 += (x2 % 2)? 0 : 1;
  323. int y2 = DecodeYPosition(pointB);
  324. //
  325. double dx = x1 - x2;
  326. double dy = y1 - y2;
  327. return (int)sqrt(dx * dx + dy * dy);
  328. }
  329. int CBattleHelper::GetDistanceWithObstacles(int pointA, int pointB)
  330. {
  331. // TODO - implement this!
  332. return GetShortestDistance(pointA, pointB);
  333. }
  334. /**
  335. * Implementation of CBattleLogic class.
  336. */
  337. CBattleLogic::CBattleLogic(ICallback *cb, CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) :
  338. m_cb(cb),
  339. m_iCurrentTurn(-2),
  340. m_bIsAttacker(false),
  341. m_army1(army1),
  342. m_army2(army2),
  343. m_tile(tile),
  344. m_hero1(hero1),
  345. m_hero2(hero2),
  346. m_side(side)
  347. {
  348. const int max_enemy_creatures = 12;
  349. m_statMaxDamage.reserve(max_enemy_creatures);
  350. m_statMinDamage.reserve(max_enemy_creatures);
  351. m_statMaxSpeed.reserve(max_enemy_creatures);
  352. m_statDistance.reserve(max_enemy_creatures);
  353. m_statDistanceFromShooters.reserve(max_enemy_creatures);
  354. m_statHitPoints.reserve(max_enemy_creatures);
  355. }
  356. CBattleLogic::~CBattleLogic()
  357. {}
  358. void CBattleLogic::SetCurrentTurn(int turn)
  359. {
  360. m_iCurrentTurn = turn;
  361. }
  362. void CBattleLogic::MakeStatistics(int currentCreatureId)
  363. {
  364. typedef std::map<int, CStack> map_stacks;
  365. map_stacks allStacks = m_cb->battleGetStacks();
  366. const CStack *currentStack = m_cb->battleGetStackByID(currentCreatureId);
  367. /*
  368. // find all creatures belong to the enemy
  369. std::for_each(allStacks.begin(), allStacks.end(),
  370. if_(bind<ui8>(&CStack::attackerOwned, bind<CStack>(&map_stacks::value_type::second, _1)) == m_bIsAttacker)
  371. [
  372. var(enemy)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  373. ret<CStack>(bind<CStack>(&map_stacks::value_type::second, _1))
  374. ]
  375. );
  376. // fill other containers
  377. // max damage
  378. std::for_each(enemy.begin(), enemy.end(),
  379. var(m_statMaxDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  380. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  381. bind<CStack>(&map_stacks::value_type::second, _1))))
  382. );
  383. // min damage
  384. std::for_each(enemy.begin(), enemy.end(),
  385. var(m_statMinDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  386. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  387. bind<CStack>(&map_stacks::value_type::second, _1))))
  388. );
  389. */
  390. m_statMaxDamage.clear();
  391. m_statMinDamage.clear();
  392. m_statHitPoints.clear();
  393. m_statMaxSpeed.clear();
  394. m_statDistanceFromShooters.clear();
  395. m_statDistance.clear();
  396. m_statDistance.clear();
  397. m_statCasualties.clear();
  398. int totalEnemyDamage = 0;
  399. int totalEnemyHitPoints = 0;
  400. int totalDamage = 0;
  401. int totalHitPoints = 0;
  402. for (map_stacks::const_iterator it = allStacks.begin(); it != allStacks.end(); ++it)
  403. {
  404. const CStack *st = &it->second;
  405. if ((it->second.attackerOwned != 0) != m_bIsAttacker)
  406. {
  407. int id = it->first;
  408. if (st->amount < 1)
  409. {
  410. continue;
  411. }
  412. // make stats
  413. int hitPoints = st->amount * st->creature->hitPoints - (st->creature->hitPoints - st->firstHPleft);
  414. m_statMaxDamage.push_back(std::pair<int, int>(id, st->creature->damageMax * st->amount));
  415. m_statMinDamage.push_back(std::pair<int, int>(id, st->creature->damageMin * st->amount));
  416. m_statHitPoints.push_back(std::pair<int, int>(id, hitPoints));
  417. m_statMaxSpeed.push_back(std::pair<int, int>(id, st->creature->speed));
  418. totalEnemyDamage += (st->creature->damageMax + st->creature->damageMin) * st->amount / 2;
  419. totalEnemyHitPoints += hitPoints;
  420. // calculate casualties
  421. SCreatureCasualties cs;
  422. // hp * amount - damage * ( (att - def)>=0 )
  423. // hit poionts
  424. assert(hitPoints >= 0 && "CGeniusAI - creature cannot have hit points less than zero");
  425. CGHeroInstance *attackerHero = (m_side)? m_hero1 : m_hero2;
  426. CGHeroInstance *defendingHero = (m_side)? m_hero2 : m_hero1;
  427. int attackDefenseBonus = currentStack->creature->attack + (attackerHero ? attackerHero->getPrimSkillLevel(0) : 0) - (st->creature->defence + (defendingHero ? defendingHero->getPrimSkillLevel(1) : 0));
  428. float damageFactor = 1.0f;
  429. if(attackDefenseBonus < 0) //decreasing dmg
  430. {
  431. if(0.02f * (-attackDefenseBonus) > 0.3f)
  432. {
  433. damageFactor += -0.3f;
  434. }
  435. else
  436. {
  437. damageFactor += 0.02f * attackDefenseBonus;
  438. }
  439. }
  440. else //increasing dmg
  441. {
  442. if(0.05f * attackDefenseBonus > 4.0f)
  443. {
  444. damageFactor += 4.0f;
  445. }
  446. else
  447. {
  448. damageFactor += 0.05f * attackDefenseBonus;
  449. }
  450. }
  451. cs.damage_max = (int)(currentStack->creature->damageMax * currentStack->amount * damageFactor);
  452. if (cs.damage_max > hitPoints)
  453. {
  454. cs.damage_max = hitPoints;
  455. }
  456. cs.damage_min = (int)(currentStack->creature->damageMin * currentStack->amount * damageFactor);
  457. if (cs.damage_min > hitPoints)
  458. {
  459. cs.damage_min = hitPoints;
  460. }
  461. cs.amount_max = cs.damage_max / st->creature->hitPoints;
  462. cs.amount_min = cs.damage_min / st->creature->hitPoints;
  463. cs.leftHitPoints_for_max = (hitPoints - cs.damage_max) % st->creature->hitPoints;
  464. cs.leftHitPoint_for_min = (hitPoints - cs.damage_min) % st->creature->hitPoints;
  465. m_statCasualties.push_back(std::pair<int, SCreatureCasualties>(id, cs));
  466. if (st->creature->isShooting() && st->shots > 0)
  467. {
  468. m_statDistanceFromShooters.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  469. }
  470. if (currentStack->creature->isFlying() || (currentStack->creature->isShooting() && currentStack->shots > 0))
  471. {
  472. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  473. }
  474. else
  475. {
  476. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetDistanceWithObstacles(currentStack->position, st->position)));
  477. }
  478. }
  479. else
  480. {
  481. if (st->amount < 1)
  482. {
  483. continue;
  484. }
  485. int hitPoints = st->amount * st->creature->hitPoints - (st->creature->hitPoints - st->firstHPleft);
  486. totalDamage += (st->creature->damageMax + st->creature->damageMin) * st->amount / 2;
  487. totalHitPoints += hitPoints;
  488. }
  489. }
  490. if ((float)totalDamage / (float)totalEnemyDamage < 0.5f &&
  491. (float)totalHitPoints / (float)totalEnemyHitPoints < 0.5f)
  492. {
  493. m_bEnemyDominates = true;
  494. MsgBox("** EnemyDominates!");
  495. }
  496. else
  497. {
  498. m_bEnemyDominates = false;
  499. }
  500. // sort max damage
  501. std::sort(m_statMaxDamage.begin(), m_statMaxDamage.end(),
  502. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  503. // sort min damage
  504. std::sort(m_statMinDamage.begin(), m_statMinDamage.end(),
  505. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  506. // sort max speed
  507. std::sort(m_statMaxSpeed.begin(), m_statMaxSpeed.end(),
  508. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  509. // sort distance
  510. std::sort(m_statDistance.begin(), m_statDistance.end(),
  511. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  512. // sort distance from shooters
  513. std::sort(m_statDistanceFromShooters.begin(), m_statDistanceFromShooters.end(),
  514. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  515. // sort hit points
  516. std::sort(m_statHitPoints.begin(), m_statHitPoints.end(),
  517. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  518. // sort casualties
  519. std::sort(m_statCasualties.begin(), m_statCasualties.end(),
  520. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _1))
  521. >
  522. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _2)));
  523. }
  524. BattleAction CBattleLogic::MakeDecision(int stackID)
  525. {
  526. MakeStatistics(stackID);
  527. list<int> creatures;
  528. int additionalInfo;
  529. if (m_bEnemyDominates)
  530. {
  531. creatures = PerformBerserkAttack(stackID, additionalInfo);
  532. }
  533. else
  534. {
  535. creatures = PerformDefaultAction(stackID, additionalInfo);
  536. }
  537. /*
  538. std::string message("Creature will be attacked - ");
  539. message += boost::lexical_cast<std::string>(creature_to_attack);
  540. MsgBox(message.c_str());
  541. */
  542. if (additionalInfo == -1 || creatures.empty())
  543. {
  544. // defend
  545. return MakeDefend(stackID);
  546. }
  547. else if (additionalInfo == -2)
  548. {
  549. return MakeWait(stackID);
  550. }
  551. list<int>::iterator it, eit;
  552. eit = creatures.end();
  553. for (it = creatures.begin(); it != eit; ++it)
  554. {
  555. BattleAction ba = MakeAttack(stackID, *it);
  556. if (ba.actionType != action_walk_and_attack)
  557. {
  558. continue;
  559. }
  560. else
  561. {
  562. PrintBattleAction(ba);
  563. return ba;
  564. }
  565. }
  566. BattleAction ba = MakeAttack(stackID, *creatures.begin());
  567. PrintBattleAction(ba);
  568. return ba;
  569. }
  570. std::vector<int> CBattleLogic::GetAvailableHexesForAttacker(CStack *defender, CStack *attacker)
  571. {
  572. int x = m_battleHelper.DecodeXPosition(defender->position);
  573. int y = m_battleHelper.DecodeYPosition(defender->position);
  574. bool defenderIsDW = defender->creature->isDoubleWide();
  575. bool attackerIsDW = attacker->creature->isDoubleWide();
  576. // TOTO: should be std::vector<int> but for debug purpose std::pair is used
  577. typedef std::pair<int, int> hexPoint;
  578. std::list<hexPoint> candidates;
  579. std::vector<int> fields;
  580. if (defenderIsDW)
  581. {
  582. if (defender->attackerOwned)
  583. {
  584. // from left side
  585. if (!(y % 2))
  586. {
  587. // up
  588. candidates.push_back(hexPoint(x - 2, y - 1));
  589. candidates.push_back(hexPoint(x - 1, y - 1));
  590. candidates.push_back(hexPoint(x, y - 1));
  591. // down
  592. candidates.push_back(hexPoint(x - 2, y + 1));
  593. candidates.push_back(hexPoint(x - 1, y + 1));
  594. candidates.push_back(hexPoint(x, y + 1));
  595. }
  596. else
  597. {
  598. // up
  599. candidates.push_back(hexPoint(x - 1, y - 1));
  600. candidates.push_back(hexPoint(x, y - 1));
  601. candidates.push_back(hexPoint(x + 1, y - 1));
  602. // down
  603. candidates.push_back(hexPoint(x - 1, y + 1));
  604. candidates.push_back(hexPoint(x, y + 1));
  605. candidates.push_back(hexPoint(x + 1, y + 1));
  606. }
  607. candidates.push_back(hexPoint(x - 2, y));
  608. candidates.push_back(hexPoint(x + 1, y));
  609. }
  610. else
  611. {
  612. // from right
  613. if (!(y % 2))
  614. {
  615. // up
  616. candidates.push_back(hexPoint(x - 1, y - 1));
  617. candidates.push_back(hexPoint(x, y - 1));
  618. candidates.push_back(hexPoint(x + 1, y - 1));
  619. // down
  620. candidates.push_back(hexPoint(x - 1, y + 1));
  621. candidates.push_back(hexPoint(x, y + 1));
  622. candidates.push_back(hexPoint(x + 1, y + 1));
  623. }
  624. else
  625. {
  626. // up
  627. candidates.push_back(hexPoint(x, y - 1));
  628. candidates.push_back(hexPoint(x + 1, y - 1));
  629. candidates.push_back(hexPoint(x + 2, y - 1));
  630. // down
  631. candidates.push_back(hexPoint(x, y + 1));
  632. candidates.push_back(hexPoint(x + 1, y + 1));
  633. candidates.push_back(hexPoint(x + 2, y + 1));
  634. }
  635. candidates.push_back(hexPoint(x - 1, y));
  636. candidates.push_back(hexPoint(x + 2, y));
  637. }
  638. }
  639. else
  640. {
  641. if (!(y % 2)) // even line
  642. {
  643. // up
  644. candidates.push_back(hexPoint(x - 1, y - 1));
  645. candidates.push_back(hexPoint(x, y - 1));
  646. // down
  647. candidates.push_back(hexPoint(x - 1, y + 1));
  648. candidates.push_back(hexPoint(x, y + 1));
  649. }
  650. else // odd line
  651. {
  652. // up
  653. candidates.push_back(hexPoint(x, y - 1));
  654. candidates.push_back(hexPoint(x + 1, y - 1));
  655. // down
  656. candidates.push_back(hexPoint(x, y + 1));
  657. candidates.push_back(hexPoint(x + 1, y + 1));
  658. }
  659. candidates.push_back(hexPoint(x + 1, y));
  660. candidates.push_back(hexPoint(x - 1, y));
  661. }
  662. // remove fields which are out of bounds or obstacles
  663. for (std::list<hexPoint>::iterator it = candidates.begin(); it != candidates.end(); ++it)
  664. {
  665. if (it->first < 1 || it->first > m_battleHelper.BattlefieldWidth ||
  666. it->second < 1 || it->second > m_battleHelper.BattlefieldHeight)
  667. {
  668. // field is out of bounds
  669. //it = candidates.erase(it);
  670. continue;
  671. }
  672. int new_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  673. CStack *st = m_cb->battleGetStackByPos(new_pos);
  674. if (st == NULL || st->amount < 1)
  675. {
  676. if (attackerIsDW)
  677. {
  678. int tail_pos = -1;
  679. if (attacker->attackerOwned) // left side
  680. {
  681. int tail_pos_x = it->first - 1;
  682. if (tail_pos_x < 1)
  683. {
  684. continue;
  685. }
  686. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  687. }
  688. else // right side
  689. {
  690. int tail_pos_x = it->first + 1;
  691. if (tail_pos_x > m_battleHelper.BattlefieldWidth)
  692. {
  693. continue;
  694. }
  695. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  696. }
  697. assert(tail_pos >= 0 && "Error during calculation position of double wide creature");
  698. CStack *tailStack = m_cb->battleGetStackByPos(tail_pos);
  699. if (st != NULL && st->amount >= 1)
  700. {
  701. continue;
  702. }
  703. }
  704. fields.push_back(new_pos);
  705. }
  706. else if (attacker)
  707. {
  708. if (attacker->ID == st->ID)
  709. {
  710. fields.push_back(new_pos);
  711. }
  712. }
  713. //
  714. //++it;
  715. }
  716. return fields;
  717. }
  718. BattleAction CBattleLogic::MakeDefend(int stackID)
  719. {
  720. BattleAction ba;
  721. ba.side = 1;
  722. ba.actionType = action_defend;
  723. ba.stackNumber = stackID;
  724. ba.additionalInfo = -1;
  725. return ba;
  726. }
  727. BattleAction CBattleLogic::MakeWait(int stackID)
  728. {
  729. BattleAction ba;
  730. ba.side = 1;
  731. ba.actionType = action_wait;
  732. ba.stackNumber = stackID;
  733. ba.additionalInfo = -1;
  734. return ba;
  735. }
  736. BattleAction CBattleLogic::MakeAttack(int attackerID, int destinationID)
  737. {
  738. if (m_cb->battleCanShoot(attackerID, m_cb->battleGetPos(destinationID)))
  739. {
  740. // shoot
  741. BattleAction ba;
  742. ba.side = 1;
  743. ba.additionalInfo = -1;
  744. ba.actionType = action_shoot; // shoot
  745. ba.stackNumber = attackerID;
  746. ba.destinationTile = (ui16)m_cb->battleGetPos(destinationID);
  747. return ba;
  748. }
  749. else
  750. {
  751. // go or go&attack
  752. int dest_tile = -1;
  753. std::vector<int> av_tiles = GetAvailableHexesForAttacker(m_cb->battleGetStackByID(destinationID), m_cb->battleGetStackByID(attackerID));
  754. if (av_tiles.size() < 1)
  755. {
  756. return MakeDefend(attackerID);
  757. }
  758. // get the best tile - now the nearest
  759. int prev_distance = m_battleHelper.InfiniteDistance;
  760. int currentPos = m_cb->battleGetPos(attackerID);
  761. for (std::vector<int>::iterator it = av_tiles.begin(); it != av_tiles.end(); ++it)
  762. {
  763. int dist = m_battleHelper.GetDistanceWithObstacles(*it, m_cb->battleGetPos(attackerID));
  764. if (dist < prev_distance)
  765. {
  766. prev_distance = dist;
  767. dest_tile = *it;
  768. }
  769. if (*it == currentPos)
  770. {
  771. dest_tile = currentPos;
  772. break;
  773. }
  774. }
  775. std::vector<int> fields = m_cb->battleGetAvailableHexes(attackerID, false);
  776. BattleAction ba;
  777. ba.side = 1;
  778. //ba.actionType = 6; // go and attack
  779. ba.stackNumber = attackerID;
  780. ba.destinationTile = (ui16)dest_tile;
  781. ba.additionalInfo = m_cb->battleGetPos(destinationID);
  782. int nearest_dist = m_battleHelper.InfiniteDistance;
  783. int nearest_pos = -1;
  784. // if double wide calculate tail
  785. CStack *attackerStack = m_cb->battleGetStackByID(attackerID);
  786. assert(attackerStack != NULL);
  787. int tail_pos = -1;
  788. if (attackerStack->creature->isDoubleWide())
  789. {
  790. int x_pos = m_battleHelper.DecodeXPosition(attackerStack->position);
  791. int y_pos = m_battleHelper.DecodeYPosition(attackerStack->position);
  792. if (attackerStack->attackerOwned)
  793. {
  794. x_pos -= 1;
  795. }
  796. else
  797. {
  798. x_pos += 1;
  799. }
  800. // if creature can perform attack without movement - do it!
  801. tail_pos = m_battleHelper.GetBattleFieldPosition(x_pos, y_pos);
  802. if (dest_tile == tail_pos)
  803. {
  804. ba.additionalInfo = dest_tile;
  805. ba.actionType = action_walk_and_attack;
  806. PrintBattleAction(ba);
  807. return ba;
  808. }
  809. }
  810. for (std::vector<int>::const_iterator it = fields.begin(); it != fields.end(); ++it)
  811. {
  812. if (*it == dest_tile)
  813. {
  814. // attack!
  815. ba.actionType = action_walk_and_attack;
  816. #if defined _DEBUG
  817. PrintBattleAction(ba);
  818. #endif
  819. return ba;
  820. }
  821. int d = m_battleHelper.GetDistanceWithObstacles(dest_tile, *it);
  822. if (d < nearest_dist)
  823. {
  824. nearest_dist = d;
  825. nearest_pos = *it;
  826. }
  827. }
  828. string message;
  829. message = "Attacker position X=";
  830. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(nearest_pos)) + ", Y=";
  831. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(nearest_pos));
  832. MsgBox(message.c_str());
  833. ba.actionType = action_walk;
  834. ba.destinationTile = (ui16)nearest_pos;
  835. ba.additionalInfo = -1;
  836. #if defined _DEBUG
  837. PrintBattleAction(ba);
  838. #endif
  839. return ba;
  840. }
  841. }
  842. /**
  843. * The main idea is to perform maximum casualties.
  844. */
  845. list<int> CBattleLogic::PerformBerserkAttack(int stackID, int &additionalInfo)
  846. {
  847. CCreature c = m_cb->battleGetCreature(stackID);
  848. // attack to make biggest damage
  849. list<int> creatures;
  850. if (!m_statCasualties.empty())
  851. {
  852. //creature_to_attack = m_statCasualties.begin()->first;
  853. creature_stat_casualties::iterator it = m_statCasualties.begin();
  854. for (; it != m_statCasualties.end(); ++it)
  855. {
  856. if (it->second.amount_min <= 0)
  857. {
  858. creatures.push_back(it->first);
  859. continue;
  860. }
  861. for (creature_stat::const_iterator it2 = m_statDistance.begin(); it2 != m_statDistance.end(); ++it2)
  862. {
  863. if (it2->first == it->first && it2->second - 1 <= c.speed)
  864. {
  865. creatures.push_front(it->first);
  866. }
  867. }
  868. }
  869. creatures.push_back(m_statCasualties.begin()->first);
  870. }
  871. return creatures;
  872. }
  873. list<int> CBattleLogic::PerformDefaultAction(int stackID, int &additionalInfo)
  874. {
  875. // first approach based on the statistics and weights
  876. // if this solution was fine we would develop this idea
  877. //
  878. std::map<int, int> votes;
  879. for (creature_stat::iterator it = m_statMaxDamage.begin(); it != m_statMaxDamage.end(); ++it)
  880. {
  881. votes[it->first] = 0;
  882. }
  883. votes[m_statMaxDamage.begin()->first] += m_battleHelper.GetVoteForMaxDamage();
  884. votes[m_statMinDamage.begin()->first] += m_battleHelper.GetVoteForMinDamage();
  885. if (m_statDistanceFromShooters.size())
  886. {
  887. votes[m_statDistanceFromShooters.begin()->first] += m_battleHelper.GetVoteForDistanceFromShooters();
  888. }
  889. votes[m_statDistance.begin()->first] += m_battleHelper.GetVoteForDistance();
  890. votes[m_statHitPoints.begin()->first] += m_battleHelper.GetVoteForHitPoints();
  891. votes[m_statMaxSpeed.begin()->first] += m_battleHelper.GetVoteForMaxSpeed();
  892. // get creature to attack
  893. int max_vote = 0;
  894. list<int> creatures;
  895. for (std::map<int, int>::iterator it = votes.begin(); it != votes.end(); ++it)
  896. {
  897. if (it->second > max_vote)
  898. {
  899. max_vote = it->second;
  900. creatures.push_front(it->first);
  901. }
  902. }
  903. additionalInfo = 0; // list contains creatures which shoud be attacked
  904. return creatures;
  905. }
  906. void CBattleLogic::PrintBattleAction(const BattleAction &action) // for debug purpose
  907. {
  908. std::string message("Battle action \n");
  909. message += "\taction type - ";
  910. switch (action.actionType)
  911. {
  912. case 0:
  913. message += "Cancel BattleAction\n";
  914. break;
  915. case 1:
  916. message += "Hero cast a spell\n";
  917. break;
  918. case 2:
  919. message += "Walk\n";
  920. break;
  921. case 3:
  922. message += "Defend\n";
  923. break;
  924. case 4:
  925. message += "Retreat from the battle\n";
  926. break;
  927. case 5:
  928. message += "Surrender\n";
  929. break;
  930. case 6:
  931. message += "Walk and Attack\n";
  932. break;
  933. case 7:
  934. message += "Shoot\n";
  935. break;
  936. case 8:
  937. message += "Wait\n";
  938. break;
  939. case 9:
  940. message += "Catapult\n";
  941. break;
  942. case 10:
  943. message += "Monster casts a spell\n";
  944. break;
  945. }
  946. message += "\tDestination tile: X = ";
  947. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.destinationTile));
  948. message += ", Y = " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.destinationTile));
  949. message += "\nAdditional info: ";
  950. if (action.actionType == 6)// || action.actionType == 7)
  951. {
  952. message += "stack - " + boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.additionalInfo));
  953. message += ", " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.additionalInfo));
  954. message += ", creature - ";
  955. CStack *c = m_cb->battleGetStackByPos(action.additionalInfo);
  956. if (c && c->creature)
  957. {
  958. message += c->creature->nameRef;
  959. }
  960. else
  961. {
  962. message += "NULL";
  963. }
  964. }
  965. else
  966. {
  967. message += boost::lexical_cast<std::string>(action.additionalInfo);
  968. }
  969. #ifdef _WIN32
  970. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  971. CONSOLE_SCREEN_BUFFER_INFO csbi;
  972. GetConsoleScreenBufferInfo(hConsole, &csbi);
  973. SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  974. #else
  975. std::string color;
  976. color = "\x1b[1;40;32m";
  977. std::cout << color;
  978. #endif
  979. std::cout << message.c_str() << std::flush;
  980. #ifdef _WIN32
  981. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  982. #else
  983. color = "\x1b[0m";
  984. std::cout << color;
  985. #endif
  986. }