BattleLogic.cpp 23 KB

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