BattleLogic.cpp 22 KB

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