BattleLogic.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. #include "BattleLogic.h"
  2. #include "../../lib/BattleState.h"
  3. #include <math.h>
  4. #include <boost/lexical_cast.hpp>
  5. #include <boost/lambda/lambda.hpp>
  6. #include <boost/lambda/bind.hpp>
  7. #include <boost/lambda/if.hpp>
  8. #include <boost/foreach.hpp>
  9. #ifdef _WIN32
  10. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  11. #include <windows.h>
  12. HANDLE handleIn;
  13. HANDLE handleOut;
  14. #endif
  15. using namespace geniusai::BattleAI;
  16. using namespace boost::lambda;
  17. using namespace std;
  18. #if _MSC_VER >= 1600
  19. #define bind boost::lambda::bind
  20. #endif
  21. /*
  22. ui8 side; //who made this action: false - left, true - right player
  23. ui32 stackNumber;//stack ID, -1 left hero, -2 right hero,
  24. ui8 actionType; //
  25. 0 = Cancel BattleAction
  26. 1 = Hero cast a spell
  27. 2 = Walk
  28. 3 = Defend
  29. 4 = Retreat from the battle
  30. 5 = Surrender
  31. 6 = Walk and Attack
  32. 7 = Shoot
  33. 8 = Wait
  34. 9 = Catapult
  35. 10 = Monster casts a spell (i.e. Faerie Dragons)
  36. ui16 destinationTile;
  37. si32 additionalInfo; // e.g. spell number if type is 1 || 10; tile to attack if type is 6
  38. */
  39. /**
  40. * Implementation of CBattleLogic class.
  41. */
  42. CBattleLogic::CBattleLogic(CCallback *cb, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) :
  43. m_iCurrentTurn(-2),
  44. m_bIsAttacker(!side),
  45. m_cb(cb),
  46. m_army1(army1),
  47. m_army2(army2),
  48. m_tile(tile),
  49. m_hero1(hero1),
  50. m_hero2(hero2),
  51. m_side(side)
  52. {
  53. const int max_enemy_creatures = 12;
  54. m_statMaxDamage.reserve(max_enemy_creatures);
  55. m_statMinDamage.reserve(max_enemy_creatures);
  56. m_statMaxSpeed.reserve(max_enemy_creatures);
  57. m_statDistance.reserve(max_enemy_creatures);
  58. m_statDistanceFromShooters.reserve(max_enemy_creatures);
  59. m_statHitPoints.reserve(max_enemy_creatures);
  60. }
  61. CBattleLogic::~CBattleLogic()
  62. {}
  63. void CBattleLogic::SetCurrentTurn(int turn)
  64. {
  65. m_iCurrentTurn = turn;
  66. }
  67. void CBattleLogic::MakeStatistics(int currentCreatureId)
  68. {
  69. typedef std::vector<const CStack*> vector_stacks;
  70. vector_stacks allStacks = m_cb->battleGetStacks();
  71. const CStack *currentStack = m_cb->battleGetStackByID(currentCreatureId);
  72. if(currentStack->position < 0) //turret
  73. {
  74. return;
  75. }
  76. /*
  77. // find all creatures belong to the enemy
  78. std::for_each(allStacks.begin(), allStacks.end(),
  79. if_(bind<ui8>(&CStack::attackerOwned, bind<CStack>(&map_stacks::value_type::second, _1)) == m_bIsAttacker)
  80. [
  81. var(enemy)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  82. ret<CStack>(bind<CStack>(&map_stacks::value_type::second, _1))
  83. ]
  84. );
  85. // fill other containers
  86. // max damage
  87. std::for_each(enemy.begin(), enemy.end(),
  88. var(m_statMaxDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  89. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  90. bind<CStack>(&map_stacks::value_type::second, _1))))
  91. );
  92. // min damage
  93. std::for_each(enemy.begin(), enemy.end(),
  94. var(m_statMinDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  95. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  96. bind<CStack>(&map_stacks::value_type::second, _1))))
  97. );
  98. */
  99. m_statMaxDamage.clear();
  100. m_statMinDamage.clear();
  101. m_statHitPoints.clear();
  102. m_statMaxSpeed.clear();
  103. m_statDistanceFromShooters.clear();
  104. m_statDistance.clear();
  105. m_statDistance.clear();
  106. m_statCasualties.clear();
  107. int totalEnemyDamage = 0;
  108. int totalEnemyHitPoints = 0;
  109. int totalDamage = 0;
  110. int totalHitPoints = 0;
  111. BOOST_FOREACH(const CStack *st, allStacks)
  112. {
  113. const int stackHP = st->valOfBonuses(Bonus::STACK_HEALTH);
  114. if ((st->attackerOwned != 0) != m_bIsAttacker)
  115. {
  116. int id = st->ID;
  117. if (st->count < 1)
  118. {
  119. continue;
  120. }
  121. // make stats
  122. int hitPoints = st->count * stackHP - (stackHP - st->firstHPleft);
  123. m_statMaxDamage.push_back(std::pair<int, int>(id, st->getMaxDamage() * st->count));
  124. m_statMinDamage.push_back(std::pair<int, int>(id, st->getMinDamage() * st->count));
  125. m_statHitPoints.push_back(std::pair<int, int>(id, hitPoints));
  126. m_statMaxSpeed.push_back(std::pair<int, int>(id, stackHP));
  127. totalEnemyDamage += (st->getCreature()->damageMax + st->getCreature()->damageMin) * st->count / 2;
  128. totalEnemyHitPoints += hitPoints;
  129. // calculate casualties
  130. SCreatureCasualties cs;
  131. // hp * amount - damage * ( (att - def)>=0 )
  132. // hit poionts
  133. assert(hitPoints >= 0 && "CGeniusAI - creature cannot have hit points less than zero");
  134. //CGHeroInstance *attackerHero = (m_side)? m_hero1 : m_hero2;
  135. //CGHeroInstance *defendingHero = (m_side)? m_hero2 : m_hero1;
  136. int attackDefenseBonus = currentStack->Attack() - st->Defense();
  137. float damageFactor = 1.0f;
  138. if(attackDefenseBonus < 0) //decreasing dmg
  139. {
  140. if(0.02f * (-attackDefenseBonus) > 0.3f)
  141. {
  142. damageFactor += -0.3f;
  143. }
  144. else
  145. {
  146. damageFactor += 0.02f * attackDefenseBonus;
  147. }
  148. }
  149. else //increasing dmg
  150. {
  151. if(0.05f * attackDefenseBonus > 4.0f)
  152. {
  153. damageFactor += 4.0f;
  154. }
  155. else
  156. {
  157. damageFactor += 0.05f * attackDefenseBonus;
  158. }
  159. }
  160. cs.damage_max = (int)(currentStack->getMaxDamage() * currentStack->count * damageFactor);
  161. if (cs.damage_max > hitPoints)
  162. {
  163. cs.damage_max = hitPoints;
  164. }
  165. cs.damage_min = (int)(currentStack->getMinDamage() * currentStack->count * damageFactor);
  166. if (cs.damage_min > hitPoints)
  167. {
  168. cs.damage_min = hitPoints;
  169. }
  170. cs.amount_max = cs.damage_max / stackHP;
  171. cs.amount_min = cs.damage_min / stackHP;
  172. cs.leftHitPoints_for_max = (hitPoints - cs.damage_max) % stackHP;
  173. cs.leftHitPoint_for_min = (hitPoints - cs.damage_min) % stackHP;
  174. m_statCasualties.push_back(std::pair<int, SCreatureCasualties>(id, cs));
  175. if (st->getCreature()->isShooting() && st->shots > 0)
  176. {
  177. m_statDistanceFromShooters.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  178. }
  179. if (currentStack->hasBonusOfType(Bonus::FLYING) || (currentStack->getCreature()->isShooting() && currentStack->shots > 0))
  180. {
  181. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  182. }
  183. else
  184. {
  185. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetDistanceWithObstacles(currentStack->position, st->position)));
  186. }
  187. }
  188. else
  189. {
  190. if (st->count < 1)
  191. {
  192. continue;
  193. }
  194. int hitPoints = st->count * stackHP - (stackHP - st->firstHPleft);
  195. totalDamage += (st->getMaxDamage() + st->getMinDamage()) * st->count / 2;
  196. totalHitPoints += hitPoints;
  197. }
  198. }
  199. if ((float)totalDamage / (float)totalEnemyDamage < 0.5f &&
  200. (float)totalHitPoints / (float)totalEnemyHitPoints < 0.5f)
  201. {
  202. m_bEnemyDominates = true;
  203. DbgBox("** EnemyDominates!");
  204. }
  205. else
  206. {
  207. m_bEnemyDominates = false;
  208. }
  209. //boost compile hack
  210. typedef int const std::pair<int, int>::* IntPtr;
  211. typedef int const SCreatureCasualties::* CreaPtr;
  212. typedef SCreatureCasualties const std::pair<int, SCreatureCasualties>::* CreaPairPtr;
  213. // sort max damage
  214. std::sort(m_statMaxDamage.begin(), m_statMaxDamage.end(),
  215. bind((IntPtr)&creature_stat::value_type::second, _1) > bind((IntPtr)&creature_stat::value_type::second, _2));
  216. // sort min damage
  217. std::sort(m_statMinDamage.begin(), m_statMinDamage.end(),
  218. bind((IntPtr)&creature_stat::value_type::second, _1) > bind((IntPtr)&creature_stat::value_type::second, _2));
  219. // sort max speed
  220. std::sort(m_statMaxSpeed.begin(), m_statMaxSpeed.end(),
  221. bind((IntPtr)&creature_stat::value_type::second, _1) > bind((IntPtr)&creature_stat::value_type::second, _2));
  222. // sort distance
  223. std::sort(m_statDistance.begin(), m_statDistance.end(),
  224. bind((IntPtr)&creature_stat::value_type::second, _1) < bind((IntPtr)&creature_stat::value_type::second, _2));
  225. // sort distance from shooters
  226. std::sort(m_statDistanceFromShooters.begin(), m_statDistanceFromShooters.end(),
  227. bind((IntPtr)&creature_stat::value_type::second, _1) < bind((IntPtr)&creature_stat::value_type::second, _2));
  228. // sort hit points
  229. std::sort(m_statHitPoints.begin(), m_statHitPoints.end(),
  230. bind((IntPtr)&creature_stat::value_type::second, _1) > bind((IntPtr)&creature_stat::value_type::second, _2));
  231. // sort casualties
  232. std::sort(m_statCasualties.begin(), m_statCasualties.end(),
  233. bind((CreaPtr)&creature_stat_casualties::value_type::second_type::damage_max,
  234. bind((CreaPairPtr)&creature_stat_casualties::value_type::second, _1))
  235. >
  236. bind((CreaPtr)&creature_stat_casualties::value_type::second_type::damage_max,
  237. bind((CreaPairPtr)&creature_stat_casualties::value_type::second, _2)));
  238. }
  239. BattleAction CBattleLogic::MakeDecision(int stackID)
  240. {
  241. const CStack *currentStack = m_cb->battleGetStackByID(stackID);
  242. if(currentStack->position < 0 || currentStack->getCreature()->idNumber == 147) //turret or first aid kit
  243. {
  244. return BattleAction::makeDefend(currentStack);
  245. }
  246. MakeStatistics(stackID);
  247. list<int> creatures;
  248. int additionalInfo = 0; //?
  249. if (m_bEnemyDominates)
  250. {
  251. creatures = PerformBerserkAttack(stackID, additionalInfo);
  252. }
  253. else
  254. {
  255. creatures = PerformDefaultAction(stackID, additionalInfo);
  256. }
  257. /*std::string message("Creature will be attacked - ");
  258. message += boost::lexical_cast<std::string>(creature_to_attack);
  259. DbgBox(message.c_str());*/
  260. if (additionalInfo == -1 || creatures.empty())
  261. {
  262. // defend
  263. return BattleAction::makeDefend(currentStack);
  264. }
  265. else if (additionalInfo == -2)
  266. {
  267. return BattleAction::makeWait(currentStack);
  268. }
  269. list<int>::iterator it, eit;
  270. eit = creatures.end();
  271. for (it = creatures.begin(); it != eit; ++it)
  272. {
  273. BattleAction ba = MakeAttack(stackID, *it);
  274. if (ba.actionType != action_walk_and_attack)
  275. {
  276. continue;
  277. }
  278. else
  279. {
  280. #if defined PRINT_DEBUG
  281. PrintBattleAction(ba);
  282. #endif
  283. return ba;
  284. }
  285. }
  286. BattleAction ba = MakeAttack(stackID, *creatures.begin());
  287. return ba;
  288. }
  289. std::vector<int> CBattleLogic::GetAvailableHexesForAttacker(const CStack *defender, const CStack *attacker)
  290. {
  291. int x = m_battleHelper.DecodeXPosition(defender->position);
  292. int y = m_battleHelper.DecodeYPosition(defender->position);
  293. bool defenderIsDW = defender->doubleWide();
  294. bool attackerIsDW = attacker->doubleWide();
  295. // TOTO: should be std::vector<int> but for debug purpose std::pair is used
  296. typedef std::pair<int, int> hexPoint;
  297. std::list<hexPoint> candidates;
  298. std::vector<int> fields;
  299. if (defenderIsDW)
  300. {
  301. if (defender->attackerOwned)
  302. {
  303. // from left side
  304. if (!(y % 2))
  305. {
  306. // up
  307. candidates.push_back(hexPoint(x - 2, y - 1));
  308. candidates.push_back(hexPoint(x - 1, y - 1));
  309. candidates.push_back(hexPoint(x, y - 1));
  310. // down
  311. candidates.push_back(hexPoint(x - 2, y + 1));
  312. candidates.push_back(hexPoint(x - 1, y + 1));
  313. candidates.push_back(hexPoint(x, y + 1));
  314. }
  315. else
  316. {
  317. // up
  318. candidates.push_back(hexPoint(x - 1, y - 1));
  319. candidates.push_back(hexPoint(x, y - 1));
  320. candidates.push_back(hexPoint(x + 1, y - 1));
  321. // down
  322. candidates.push_back(hexPoint(x - 1, y + 1));
  323. candidates.push_back(hexPoint(x, y + 1));
  324. candidates.push_back(hexPoint(x + 1, y + 1));
  325. }
  326. candidates.push_back(hexPoint(x - 2, y));
  327. candidates.push_back(hexPoint(x + 1, y));
  328. }
  329. else
  330. {
  331. // from right
  332. if (!(y % 2))
  333. {
  334. // up
  335. candidates.push_back(hexPoint(x - 1, y - 1));
  336. candidates.push_back(hexPoint(x, y - 1));
  337. candidates.push_back(hexPoint(x + 1, y - 1));
  338. // down
  339. candidates.push_back(hexPoint(x - 1, y + 1));
  340. candidates.push_back(hexPoint(x, y + 1));
  341. candidates.push_back(hexPoint(x + 1, y + 1));
  342. }
  343. else
  344. {
  345. // up
  346. candidates.push_back(hexPoint(x, y - 1));
  347. candidates.push_back(hexPoint(x + 1, y - 1));
  348. candidates.push_back(hexPoint(x + 2, y - 1));
  349. // down
  350. candidates.push_back(hexPoint(x, y + 1));
  351. candidates.push_back(hexPoint(x + 1, y + 1));
  352. candidates.push_back(hexPoint(x + 2, y + 1));
  353. }
  354. candidates.push_back(hexPoint(x - 1, y));
  355. candidates.push_back(hexPoint(x + 2, y));
  356. }
  357. }
  358. else
  359. {
  360. if (!(y % 2)) // even line
  361. {
  362. // up
  363. candidates.push_back(hexPoint(x - 1, y - 1));
  364. candidates.push_back(hexPoint(x, y - 1));
  365. // down
  366. candidates.push_back(hexPoint(x - 1, y + 1));
  367. candidates.push_back(hexPoint(x, y + 1));
  368. }
  369. else // odd line
  370. {
  371. // up
  372. candidates.push_back(hexPoint(x, y - 1));
  373. candidates.push_back(hexPoint(x + 1, y - 1));
  374. // down
  375. candidates.push_back(hexPoint(x, y + 1));
  376. candidates.push_back(hexPoint(x + 1, y + 1));
  377. }
  378. candidates.push_back(hexPoint(x + 1, y));
  379. candidates.push_back(hexPoint(x - 1, y));
  380. }
  381. // remove fields which are out of bounds or obstacles
  382. for (std::list<hexPoint>::iterator it = candidates.begin(); it != candidates.end(); ++it)
  383. {
  384. if (it->first < 1 || it->first > m_battleHelper.BattlefieldWidth ||
  385. it->second < 1 || it->second > m_battleHelper.BattlefieldHeight)
  386. {
  387. // field is out of bounds
  388. //it = candidates.erase(it);
  389. continue;
  390. }
  391. int new_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  392. const CStack *st = m_cb->battleGetStackByPos(new_pos);
  393. if (st == NULL || st->count < 1)
  394. {
  395. if (attackerIsDW)
  396. {
  397. int tail_pos = -1;
  398. if (attacker->attackerOwned) // left side
  399. {
  400. int tail_pos_x = it->first - 1;
  401. if (tail_pos_x < 1)
  402. {
  403. continue;
  404. }
  405. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  406. }
  407. else // right side
  408. {
  409. int tail_pos_x = it->first + 1;
  410. if (tail_pos_x > m_battleHelper.BattlefieldWidth)
  411. {
  412. continue;
  413. }
  414. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  415. }
  416. assert(tail_pos >= 0 && "Error during calculation position of double wide creature");
  417. //CStack *tailStack = m_cb->battleGetStackByPos(tail_pos);
  418. if (st != NULL && st->count >= 1)
  419. {
  420. continue;
  421. }
  422. }
  423. fields.push_back(new_pos);
  424. }
  425. else if (attacker)
  426. {
  427. if (attacker->ID == st->ID)
  428. {
  429. fields.push_back(new_pos);
  430. }
  431. }
  432. //
  433. //++it;
  434. }
  435. return fields;
  436. }
  437. BattleAction CBattleLogic::MakeAttack(int attackerID, int destinationID)
  438. {
  439. const CStack *attackerStack = m_cb->battleGetStackByID(attackerID),
  440. *destinationStack = m_cb->battleGetStackByID(destinationID);
  441. assert(attackerStack && destinationStack);
  442. //don't attack ourselves
  443. if(destinationStack->attackerOwned == !m_side)
  444. {
  445. return BattleAction::makeDefend(attackerStack);
  446. }
  447. if (m_cb->battleCanShoot(attackerStack, destinationStack->position)) // shoot
  448. {
  449. return BattleAction::makeShotAttack(attackerStack, destinationStack);
  450. }
  451. else
  452. {
  453. // go or go&attack
  454. int dest_tile = -1;
  455. std::vector<int> av_tiles = GetAvailableHexesForAttacker(m_cb->battleGetStackByID(destinationID), m_cb->battleGetStackByID(attackerID));
  456. if (av_tiles.size() < 1)
  457. {
  458. return BattleAction::makeDefend(attackerStack);
  459. }
  460. // get the best tile - now the nearest
  461. int prev_distance = m_battleHelper.InfiniteDistance;
  462. int currentPos = m_cb->battleGetPos(attackerID);
  463. for (std::vector<int>::iterator it = av_tiles.begin(); it != av_tiles.end(); ++it)
  464. {
  465. int dist = m_battleHelper.GetDistanceWithObstacles(*it, m_cb->battleGetPos(attackerID));
  466. if (dist < prev_distance)
  467. {
  468. prev_distance = dist;
  469. dest_tile = *it;
  470. }
  471. if (*it == currentPos)
  472. {
  473. dest_tile = currentPos;
  474. break;
  475. }
  476. }
  477. std::vector<THex> fields = m_cb->battleGetAvailableHexes(m_cb->battleGetStackByID(attackerID), false);
  478. if(fields.size() == 0)
  479. {
  480. return BattleAction::makeDefend(attackerStack);
  481. }
  482. BattleAction ba;
  483. ba.side = m_side;
  484. //ba.actionType = 6; // go and attack
  485. ba.stackNumber = attackerID;
  486. ba.destinationTile = static_cast<ui16>(dest_tile);
  487. //simplified checking for possibility of attack (previous was too simplified)
  488. int destStackPos = m_cb->battleGetPos(destinationID);
  489. if(THex::mutualPosition(dest_tile, destStackPos) != -1)
  490. ba.additionalInfo = destStackPos;
  491. else if(THex::mutualPosition(dest_tile, destStackPos+1) != -1)
  492. ba.additionalInfo = destStackPos+1;
  493. else if(THex::mutualPosition(dest_tile, destStackPos-1) != -1)
  494. ba.additionalInfo = destStackPos-1;
  495. else
  496. return BattleAction::makeDefend(attackerStack);
  497. int nearest_dist = m_battleHelper.InfiniteDistance;
  498. int nearest_pos = -1;
  499. // if double wide calculate tail
  500. int tail_pos = -1;
  501. if (attackerStack->doubleWide())
  502. {
  503. int x_pos = m_battleHelper.DecodeXPosition(attackerStack->position);
  504. int y_pos = m_battleHelper.DecodeYPosition(attackerStack->position);
  505. if (attackerStack->attackerOwned)
  506. {
  507. x_pos -= 1;
  508. }
  509. else
  510. {
  511. x_pos += 1;
  512. }
  513. // if creature can perform attack without movement - do it!
  514. tail_pos = m_battleHelper.GetBattleFieldPosition(x_pos, y_pos);
  515. if (dest_tile == tail_pos)
  516. {
  517. ba.additionalInfo = dest_tile;
  518. ba.actionType = action_walk_and_attack;
  519. #if defined PRINT_DEBUG
  520. PrintBattleAction(ba);
  521. #endif
  522. assert(m_cb->battleGetStackByPos(ba.additionalInfo, false)); //if action is action_walk_and_attack additional info must point on enemy stack
  523. assert(m_cb->battleGetStackByPos(ba.additionalInfo, false) != attackerStack); //don't attack ourselve
  524. return ba;
  525. }
  526. }
  527. for (std::vector<THex>::const_iterator it = fields.begin(); it != fields.end(); ++it)
  528. {
  529. if (*it == dest_tile)
  530. {
  531. // attack!
  532. ba.actionType = action_walk_and_attack;
  533. #if defined PRINT_DEBUG
  534. PrintBattleAction(ba);
  535. #endif
  536. assert(m_cb->battleGetStackByPos(ba.additionalInfo)); //if action is action_walk_and_attack additional info must point on enemy stack
  537. assert(m_cb->battleGetStackByPos(ba.additionalInfo) != attackerStack); //don't attack ourselve
  538. return ba;
  539. }
  540. int d = m_battleHelper.GetDistanceWithObstacles(dest_tile, *it);
  541. if (d < nearest_dist)
  542. {
  543. nearest_dist = d;
  544. nearest_pos = *it;
  545. }
  546. }
  547. string message;
  548. message = "Attacker position X=";
  549. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(nearest_pos)) + ", Y=";
  550. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(nearest_pos));
  551. DbgBox(message.c_str());
  552. ba.actionType = action_walk;
  553. ba.destinationTile = (ui16)nearest_pos;
  554. ba.additionalInfo = -1;
  555. #if defined PRINT_DEBUG
  556. PrintBattleAction(ba);
  557. #endif
  558. return ba;
  559. }
  560. }
  561. /**
  562. * The main idea is to perform maximum casualties.
  563. */
  564. list<int> CBattleLogic::PerformBerserkAttack(int stackID, int &additionalInfo)
  565. {
  566. const CStack * c = m_cb->battleGetStackByID(stackID);
  567. // attack to make biggest damage
  568. list<int> creatures;
  569. if (!m_statCasualties.empty())
  570. {
  571. //creature_to_attack = m_statCasualties.begin()->first;
  572. creature_stat_casualties::iterator it = m_statCasualties.begin();
  573. for (; it != m_statCasualties.end(); ++it)
  574. {
  575. if (it->second.amount_min <= 0)
  576. {
  577. creatures.push_back(it->first);
  578. continue;
  579. }
  580. for (creature_stat::const_iterator it2 = m_statDistance.begin(); it2 != m_statDistance.end(); ++it2)
  581. {
  582. if (it2->first == it->first && it2->second - 1 <= c->getCreature()->valOfBonuses(Bonus::STACKS_SPEED))
  583. {
  584. creatures.push_front(it->first);
  585. }
  586. }
  587. }
  588. creatures.push_back(m_statCasualties.begin()->first);
  589. }
  590. return creatures;
  591. }
  592. list<int> CBattleLogic::PerformDefaultAction(int stackID, int &additionalInfo)
  593. {
  594. // first approach based on the statistics and weights
  595. // if this solution was fine we would develop this idea
  596. //
  597. std::map<int, int> votes;
  598. for (creature_stat::iterator it = m_statMaxDamage.begin(); it != m_statMaxDamage.end(); ++it)
  599. {
  600. votes[it->first] = 0;
  601. }
  602. votes[m_statMaxDamage.begin()->first] += m_battleHelper.GetVoteForMaxDamage();
  603. votes[m_statMinDamage.begin()->first] += m_battleHelper.GetVoteForMinDamage();
  604. if (m_statDistanceFromShooters.size())
  605. {
  606. votes[m_statDistanceFromShooters.begin()->first] += m_battleHelper.GetVoteForDistanceFromShooters();
  607. }
  608. votes[m_statDistance.begin()->first] += m_battleHelper.GetVoteForDistance();
  609. votes[m_statHitPoints.begin()->first] += m_battleHelper.GetVoteForHitPoints();
  610. votes[m_statMaxSpeed.begin()->first] += m_battleHelper.GetVoteForMaxSpeed();
  611. // get creature to attack
  612. int max_vote = 0;
  613. list<int> creatures;
  614. for (std::map<int, int>::iterator it = votes.begin(); it != votes.end(); ++it)
  615. {
  616. if (bool(m_cb->battleGetStackByID(it->first)->attackerOwned) == m_side //it's hostile stack
  617. && it->second > max_vote)
  618. {
  619. max_vote = it->second;
  620. creatures.push_front(it->first);
  621. }
  622. }
  623. additionalInfo = 0; // list contains creatures which shoud be attacked
  624. return creatures;
  625. }
  626. void CBattleLogic::PrintBattleAction(const BattleAction &action) // for debug purpose
  627. {
  628. std::string message("Battle action \n");
  629. message += "\taction type - ";
  630. switch (action.actionType)
  631. {
  632. case 0:
  633. message += "Cancel BattleAction\n";
  634. break;
  635. case 1:
  636. message += "Hero cast a spell\n";
  637. break;
  638. case 2:
  639. message += "Walk\n";
  640. break;
  641. case 3:
  642. message += "Defend\n";
  643. break;
  644. case 4:
  645. message += "Retreat from the battle\n";
  646. break;
  647. case 5:
  648. message += "Surrender\n";
  649. break;
  650. case 6:
  651. message += "Walk and Attack\n";
  652. break;
  653. case 7:
  654. message += "Shoot\n";
  655. break;
  656. case 8:
  657. message += "Wait\n";
  658. break;
  659. case 9:
  660. message += "Catapult\n";
  661. break;
  662. case 10:
  663. message += "Monster casts a spell\n";
  664. break;
  665. }
  666. message += "\tDestination tile: X = ";
  667. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.destinationTile));
  668. message += ", Y = " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.destinationTile));
  669. message += "\nAdditional info: ";
  670. if (action.actionType == 6)// || action.actionType == 7)
  671. {
  672. message += "stack - " + boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.additionalInfo));
  673. message += ", " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.additionalInfo));
  674. message += ", creature - ";
  675. const CStack *c = m_cb->battleGetStackByPos(action.additionalInfo);
  676. if (c && c->getCreature())
  677. {
  678. message += c->getCreature()->nameRef;
  679. }
  680. else
  681. {
  682. message += "NULL";
  683. }
  684. }
  685. else
  686. {
  687. message += boost::lexical_cast<std::string>(action.additionalInfo);
  688. }
  689. #ifdef _WIN32
  690. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  691. CONSOLE_SCREEN_BUFFER_INFO csbi;
  692. GetConsoleScreenBufferInfo(hConsole, &csbi);
  693. SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  694. #else
  695. std::string color;
  696. color = "\x1b[1;40;32m";
  697. std::cout << color;
  698. #endif
  699. std::cout << message.c_str() << std::endl;
  700. #ifdef _WIN32
  701. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  702. #else
  703. color = "\x1b[0m";
  704. std::cout << color;
  705. #endif
  706. }