BattleLogic.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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(ICallback *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. // sort max damage
  210. std::sort(m_statMaxDamage.begin(), m_statMaxDamage.end(),
  211. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  212. // sort min damage
  213. std::sort(m_statMinDamage.begin(), m_statMinDamage.end(),
  214. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  215. // sort max speed
  216. std::sort(m_statMaxSpeed.begin(), m_statMaxSpeed.end(),
  217. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  218. // sort distance
  219. std::sort(m_statDistance.begin(), m_statDistance.end(),
  220. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  221. // sort distance from shooters
  222. std::sort(m_statDistanceFromShooters.begin(), m_statDistanceFromShooters.end(),
  223. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  224. // sort hit points
  225. std::sort(m_statHitPoints.begin(), m_statHitPoints.end(),
  226. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  227. // sort casualties
  228. std::sort(m_statCasualties.begin(), m_statCasualties.end(),
  229. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _1))
  230. >
  231. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _2)));
  232. }
  233. BattleAction CBattleLogic::MakeDecision(int stackID)
  234. {
  235. const CStack *currentStack = m_cb->battleGetStackByID(stackID);
  236. if(currentStack->position < 0 || currentStack->getCreature()->idNumber == 147) //turret or first aid kit
  237. {
  238. return BattleAction::makeDefend(currentStack);
  239. }
  240. MakeStatistics(stackID);
  241. list<int> creatures;
  242. int additionalInfo = 0; //?
  243. if (m_bEnemyDominates)
  244. {
  245. creatures = PerformBerserkAttack(stackID, additionalInfo);
  246. }
  247. else
  248. {
  249. creatures = PerformDefaultAction(stackID, additionalInfo);
  250. }
  251. /*std::string message("Creature will be attacked - ");
  252. message += boost::lexical_cast<std::string>(creature_to_attack);
  253. DbgBox(message.c_str());*/
  254. if (additionalInfo == -1 || creatures.empty())
  255. {
  256. // defend
  257. return BattleAction::makeDefend(currentStack);
  258. }
  259. else if (additionalInfo == -2)
  260. {
  261. return BattleAction::makeWait(currentStack);
  262. }
  263. list<int>::iterator it, eit;
  264. eit = creatures.end();
  265. for (it = creatures.begin(); it != eit; ++it)
  266. {
  267. BattleAction ba = MakeAttack(stackID, *it);
  268. if (ba.actionType != action_walk_and_attack)
  269. {
  270. continue;
  271. }
  272. else
  273. {
  274. #if defined PRINT_DEBUG
  275. PrintBattleAction(ba);
  276. #endif
  277. return ba;
  278. }
  279. }
  280. BattleAction ba = MakeAttack(stackID, *creatures.begin());
  281. return ba;
  282. }
  283. std::vector<int> CBattleLogic::GetAvailableHexesForAttacker(const CStack *defender, const CStack *attacker)
  284. {
  285. int x = m_battleHelper.DecodeXPosition(defender->position);
  286. int y = m_battleHelper.DecodeYPosition(defender->position);
  287. bool defenderIsDW = defender->doubleWide();
  288. bool attackerIsDW = attacker->doubleWide();
  289. // TOTO: should be std::vector<int> but for debug purpose std::pair is used
  290. typedef std::pair<int, int> hexPoint;
  291. std::list<hexPoint> candidates;
  292. std::vector<int> fields;
  293. if (defenderIsDW)
  294. {
  295. if (defender->attackerOwned)
  296. {
  297. // from left side
  298. if (!(y % 2))
  299. {
  300. // up
  301. candidates.push_back(hexPoint(x - 2, y - 1));
  302. candidates.push_back(hexPoint(x - 1, y - 1));
  303. candidates.push_back(hexPoint(x, y - 1));
  304. // down
  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. }
  309. else
  310. {
  311. // up
  312. candidates.push_back(hexPoint(x - 1, y - 1));
  313. candidates.push_back(hexPoint(x, y - 1));
  314. candidates.push_back(hexPoint(x + 1, y - 1));
  315. // down
  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. }
  320. candidates.push_back(hexPoint(x - 2, y));
  321. candidates.push_back(hexPoint(x + 1, y));
  322. }
  323. else
  324. {
  325. // from right
  326. if (!(y % 2))
  327. {
  328. // up
  329. candidates.push_back(hexPoint(x - 1, y - 1));
  330. candidates.push_back(hexPoint(x, y - 1));
  331. candidates.push_back(hexPoint(x + 1, y - 1));
  332. // down
  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. }
  337. else
  338. {
  339. // up
  340. candidates.push_back(hexPoint(x, y - 1));
  341. candidates.push_back(hexPoint(x + 1, y - 1));
  342. candidates.push_back(hexPoint(x + 2, y - 1));
  343. // down
  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. }
  348. candidates.push_back(hexPoint(x - 1, y));
  349. candidates.push_back(hexPoint(x + 2, y));
  350. }
  351. }
  352. else
  353. {
  354. if (!(y % 2)) // even line
  355. {
  356. // up
  357. candidates.push_back(hexPoint(x - 1, y - 1));
  358. candidates.push_back(hexPoint(x, y - 1));
  359. // down
  360. candidates.push_back(hexPoint(x - 1, y + 1));
  361. candidates.push_back(hexPoint(x, y + 1));
  362. }
  363. else // odd line
  364. {
  365. // up
  366. candidates.push_back(hexPoint(x, y - 1));
  367. candidates.push_back(hexPoint(x + 1, y - 1));
  368. // down
  369. candidates.push_back(hexPoint(x, y + 1));
  370. candidates.push_back(hexPoint(x + 1, y + 1));
  371. }
  372. candidates.push_back(hexPoint(x + 1, y));
  373. candidates.push_back(hexPoint(x - 1, y));
  374. }
  375. // remove fields which are out of bounds or obstacles
  376. for (std::list<hexPoint>::iterator it = candidates.begin(); it != candidates.end(); ++it)
  377. {
  378. if (it->first < 1 || it->first > m_battleHelper.BattlefieldWidth ||
  379. it->second < 1 || it->second > m_battleHelper.BattlefieldHeight)
  380. {
  381. // field is out of bounds
  382. //it = candidates.erase(it);
  383. continue;
  384. }
  385. int new_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  386. const CStack *st = m_cb->battleGetStackByPos(new_pos);
  387. if (st == NULL || st->count < 1)
  388. {
  389. if (attackerIsDW)
  390. {
  391. int tail_pos = -1;
  392. if (attacker->attackerOwned) // left side
  393. {
  394. int tail_pos_x = it->first - 1;
  395. if (tail_pos_x < 1)
  396. {
  397. continue;
  398. }
  399. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  400. }
  401. else // right side
  402. {
  403. int tail_pos_x = it->first + 1;
  404. if (tail_pos_x > m_battleHelper.BattlefieldWidth)
  405. {
  406. continue;
  407. }
  408. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  409. }
  410. assert(tail_pos >= 0 && "Error during calculation position of double wide creature");
  411. //CStack *tailStack = m_cb->battleGetStackByPos(tail_pos);
  412. if (st != NULL && st->count >= 1)
  413. {
  414. continue;
  415. }
  416. }
  417. fields.push_back(new_pos);
  418. }
  419. else if (attacker)
  420. {
  421. if (attacker->ID == st->ID)
  422. {
  423. fields.push_back(new_pos);
  424. }
  425. }
  426. //
  427. //++it;
  428. }
  429. return fields;
  430. }
  431. BattleAction CBattleLogic::MakeAttack(int attackerID, int destinationID)
  432. {
  433. const CStack *attackerStack = m_cb->battleGetStackByID(attackerID),
  434. *destinationStack = m_cb->battleGetStackByID(destinationID);
  435. assert(attackerStack && destinationStack);
  436. //don't attack ourselves
  437. if(destinationStack->attackerOwned == !m_side)
  438. {
  439. return BattleAction::makeDefend(attackerStack);
  440. }
  441. if (m_cb->battleCanShoot(attackerID, m_cb->battleGetPos(destinationID))) // shoot
  442. {
  443. return BattleAction::makeShotAttack(attackerStack, destinationStack);
  444. }
  445. else
  446. {
  447. // go or go&attack
  448. int dest_tile = -1;
  449. std::vector<int> av_tiles = GetAvailableHexesForAttacker(m_cb->battleGetStackByID(destinationID), m_cb->battleGetStackByID(attackerID));
  450. if (av_tiles.size() < 1)
  451. {
  452. return BattleAction::makeDefend(attackerStack);
  453. }
  454. // get the best tile - now the nearest
  455. int prev_distance = m_battleHelper.InfiniteDistance;
  456. int currentPos = m_cb->battleGetPos(attackerID);
  457. for (std::vector<int>::iterator it = av_tiles.begin(); it != av_tiles.end(); ++it)
  458. {
  459. int dist = m_battleHelper.GetDistanceWithObstacles(*it, m_cb->battleGetPos(attackerID));
  460. if (dist < prev_distance)
  461. {
  462. prev_distance = dist;
  463. dest_tile = *it;
  464. }
  465. if (*it == currentPos)
  466. {
  467. dest_tile = currentPos;
  468. break;
  469. }
  470. }
  471. std::vector<int> fields = m_cb->battleGetAvailableHexes(attackerID, false);
  472. if(fields.size() == 0)
  473. {
  474. return BattleAction::makeDefend(attackerStack);
  475. }
  476. BattleAction ba;
  477. ba.side = m_side;
  478. //ba.actionType = 6; // go and attack
  479. ba.stackNumber = attackerID;
  480. ba.destinationTile = static_cast<ui16>(dest_tile);
  481. //simplified checking for possibility of attack (previous was too simplified)
  482. int destStackPos = m_cb->battleGetPos(destinationID);
  483. if(BattleInfo::mutualPosition(dest_tile, destStackPos) != -1)
  484. ba.additionalInfo = destStackPos;
  485. else if(BattleInfo::mutualPosition(dest_tile, destStackPos+1) != -1)
  486. ba.additionalInfo = destStackPos+1;
  487. else if(BattleInfo::mutualPosition(dest_tile, destStackPos-1) != -1)
  488. ba.additionalInfo = destStackPos-1;
  489. else
  490. return BattleAction::makeDefend(attackerStack);
  491. int nearest_dist = m_battleHelper.InfiniteDistance;
  492. int nearest_pos = -1;
  493. // if double wide calculate tail
  494. int tail_pos = -1;
  495. if (attackerStack->doubleWide())
  496. {
  497. int x_pos = m_battleHelper.DecodeXPosition(attackerStack->position);
  498. int y_pos = m_battleHelper.DecodeYPosition(attackerStack->position);
  499. if (attackerStack->attackerOwned)
  500. {
  501. x_pos -= 1;
  502. }
  503. else
  504. {
  505. x_pos += 1;
  506. }
  507. // if creature can perform attack without movement - do it!
  508. tail_pos = m_battleHelper.GetBattleFieldPosition(x_pos, y_pos);
  509. if (dest_tile == tail_pos)
  510. {
  511. ba.additionalInfo = dest_tile;
  512. ba.actionType = action_walk_and_attack;
  513. #if defined PRINT_DEBUG
  514. PrintBattleAction(ba);
  515. #endif
  516. assert(m_cb->battleGetStackByPos(ba.additionalInfo, false)); //if action is action_walk_and_attack additional info must point on enemy stack
  517. assert(m_cb->battleGetStackByPos(ba.additionalInfo, false) != attackerStack); //don't attack ourselve
  518. return ba;
  519. }
  520. }
  521. for (std::vector<int>::const_iterator it = fields.begin(); it != fields.end(); ++it)
  522. {
  523. if (*it == dest_tile)
  524. {
  525. // attack!
  526. ba.actionType = action_walk_and_attack;
  527. #if defined PRINT_DEBUG
  528. PrintBattleAction(ba);
  529. #endif
  530. assert(m_cb->battleGetStackByPos(ba.additionalInfo)); //if action is action_walk_and_attack additional info must point on enemy stack
  531. assert(m_cb->battleGetStackByPos(ba.additionalInfo) != attackerStack); //don't attack ourselve
  532. return ba;
  533. }
  534. int d = m_battleHelper.GetDistanceWithObstacles(dest_tile, *it);
  535. if (d < nearest_dist)
  536. {
  537. nearest_dist = d;
  538. nearest_pos = *it;
  539. }
  540. }
  541. string message;
  542. message = "Attacker position X=";
  543. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(nearest_pos)) + ", Y=";
  544. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(nearest_pos));
  545. DbgBox(message.c_str());
  546. ba.actionType = action_walk;
  547. ba.destinationTile = (ui16)nearest_pos;
  548. ba.additionalInfo = -1;
  549. #if defined PRINT_DEBUG
  550. PrintBattleAction(ba);
  551. #endif
  552. return ba;
  553. }
  554. }
  555. /**
  556. * The main idea is to perform maximum casualties.
  557. */
  558. list<int> CBattleLogic::PerformBerserkAttack(int stackID, int &additionalInfo)
  559. {
  560. const CStack * c = m_cb->battleGetStackByID(stackID);
  561. // attack to make biggest damage
  562. list<int> creatures;
  563. if (!m_statCasualties.empty())
  564. {
  565. //creature_to_attack = m_statCasualties.begin()->first;
  566. creature_stat_casualties::iterator it = m_statCasualties.begin();
  567. for (; it != m_statCasualties.end(); ++it)
  568. {
  569. if (it->second.amount_min <= 0)
  570. {
  571. creatures.push_back(it->first);
  572. continue;
  573. }
  574. for (creature_stat::const_iterator it2 = m_statDistance.begin(); it2 != m_statDistance.end(); ++it2)
  575. {
  576. if (it2->first == it->first && it2->second - 1 <= c->getCreature()->valOfBonuses(Bonus::STACKS_SPEED))
  577. {
  578. creatures.push_front(it->first);
  579. }
  580. }
  581. }
  582. creatures.push_back(m_statCasualties.begin()->first);
  583. }
  584. return creatures;
  585. }
  586. list<int> CBattleLogic::PerformDefaultAction(int stackID, int &additionalInfo)
  587. {
  588. // first approach based on the statistics and weights
  589. // if this solution was fine we would develop this idea
  590. //
  591. std::map<int, int> votes;
  592. for (creature_stat::iterator it = m_statMaxDamage.begin(); it != m_statMaxDamage.end(); ++it)
  593. {
  594. votes[it->first] = 0;
  595. }
  596. votes[m_statMaxDamage.begin()->first] += m_battleHelper.GetVoteForMaxDamage();
  597. votes[m_statMinDamage.begin()->first] += m_battleHelper.GetVoteForMinDamage();
  598. if (m_statDistanceFromShooters.size())
  599. {
  600. votes[m_statDistanceFromShooters.begin()->first] += m_battleHelper.GetVoteForDistanceFromShooters();
  601. }
  602. votes[m_statDistance.begin()->first] += m_battleHelper.GetVoteForDistance();
  603. votes[m_statHitPoints.begin()->first] += m_battleHelper.GetVoteForHitPoints();
  604. votes[m_statMaxSpeed.begin()->first] += m_battleHelper.GetVoteForMaxSpeed();
  605. // get creature to attack
  606. int max_vote = 0;
  607. list<int> creatures;
  608. for (std::map<int, int>::iterator it = votes.begin(); it != votes.end(); ++it)
  609. {
  610. if (bool(m_cb->battleGetStackByID(it->first)->attackerOwned) == m_side //it's hostile stack
  611. && it->second > max_vote)
  612. {
  613. max_vote = it->second;
  614. creatures.push_front(it->first);
  615. }
  616. }
  617. additionalInfo = 0; // list contains creatures which shoud be attacked
  618. return creatures;
  619. }
  620. void CBattleLogic::PrintBattleAction(const BattleAction &action) // for debug purpose
  621. {
  622. std::string message("Battle action \n");
  623. message += "\taction type - ";
  624. switch (action.actionType)
  625. {
  626. case 0:
  627. message += "Cancel BattleAction\n";
  628. break;
  629. case 1:
  630. message += "Hero cast a spell\n";
  631. break;
  632. case 2:
  633. message += "Walk\n";
  634. break;
  635. case 3:
  636. message += "Defend\n";
  637. break;
  638. case 4:
  639. message += "Retreat from the battle\n";
  640. break;
  641. case 5:
  642. message += "Surrender\n";
  643. break;
  644. case 6:
  645. message += "Walk and Attack\n";
  646. break;
  647. case 7:
  648. message += "Shoot\n";
  649. break;
  650. case 8:
  651. message += "Wait\n";
  652. break;
  653. case 9:
  654. message += "Catapult\n";
  655. break;
  656. case 10:
  657. message += "Monster casts a spell\n";
  658. break;
  659. }
  660. message += "\tDestination tile: X = ";
  661. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.destinationTile));
  662. message += ", Y = " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.destinationTile));
  663. message += "\nAdditional info: ";
  664. if (action.actionType == 6)// || action.actionType == 7)
  665. {
  666. message += "stack - " + boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.additionalInfo));
  667. message += ", " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.additionalInfo));
  668. message += ", creature - ";
  669. const CStack *c = m_cb->battleGetStackByPos(action.additionalInfo);
  670. if (c && c->getCreature())
  671. {
  672. message += c->getCreature()->nameRef;
  673. }
  674. else
  675. {
  676. message += "NULL";
  677. }
  678. }
  679. else
  680. {
  681. message += boost::lexical_cast<std::string>(action.additionalInfo);
  682. }
  683. #ifdef _WIN32
  684. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  685. CONSOLE_SCREEN_BUFFER_INFO csbi;
  686. GetConsoleScreenBufferInfo(hConsole, &csbi);
  687. SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  688. #else
  689. std::string color;
  690. color = "\x1b[1;40;32m";
  691. std::cout << color;
  692. #endif
  693. std::cout << message.c_str() << std::endl;
  694. #ifdef _WIN32
  695. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  696. #else
  697. color = "\x1b[0m";
  698. std::cout << color;
  699. #endif
  700. }