BattleLogic.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 = m_side;
  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 = m_side;
  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. CStack *attackerStack = m_cb->battleGetStackByID(attackerID),
  437. *destinationStack = m_cb->battleGetStackByID(destinationID);
  438. assert(attackerStack && destinationStack);
  439. //don't attack ourselves
  440. if(destinationStack->attackerOwned == !m_side)
  441. {
  442. return MakeDefend(attackerID);
  443. }
  444. if (m_cb->battleCanShoot(attackerID, m_cb->battleGetPos(destinationID)))
  445. {
  446. // shoot
  447. BattleAction ba;
  448. ba.side = m_side;
  449. ba.additionalInfo = -1;
  450. ba.actionType = action_shoot; // shoot
  451. ba.stackNumber = attackerID;
  452. ba.destinationTile = (ui16)m_cb->battleGetPos(destinationID);
  453. return ba;
  454. }
  455. else
  456. {
  457. // go or go&attack
  458. int dest_tile = -1;
  459. std::vector<int> av_tiles = GetAvailableHexesForAttacker(m_cb->battleGetStackByID(destinationID), m_cb->battleGetStackByID(attackerID));
  460. if (av_tiles.size() < 1)
  461. {
  462. return MakeDefend(attackerID);
  463. }
  464. // get the best tile - now the nearest
  465. int prev_distance = m_battleHelper.InfiniteDistance;
  466. int currentPos = m_cb->battleGetPos(attackerID);
  467. for (std::vector<int>::iterator it = av_tiles.begin(); it != av_tiles.end(); ++it)
  468. {
  469. int dist = m_battleHelper.GetDistanceWithObstacles(*it, m_cb->battleGetPos(attackerID));
  470. if (dist < prev_distance)
  471. {
  472. prev_distance = dist;
  473. dest_tile = *it;
  474. }
  475. if (*it == currentPos)
  476. {
  477. dest_tile = currentPos;
  478. break;
  479. }
  480. }
  481. std::vector<int> fields = m_cb->battleGetAvailableHexes(attackerID, false);
  482. if(fields.size() == 0)
  483. {
  484. return MakeDefend(attackerID);
  485. }
  486. BattleAction ba;
  487. ba.side = m_side;
  488. //ba.actionType = 6; // go and attack
  489. ba.stackNumber = attackerID;
  490. ba.destinationTile = static_cast<ui16>(dest_tile);
  491. //simplified checking for possibility of attack (previous was too simplified)
  492. int destStackPos = m_cb->battleGetPos(destinationID);
  493. if(BattleInfo::mutualPosition(dest_tile, destStackPos) != -1)
  494. ba.additionalInfo = destStackPos;
  495. else if(BattleInfo::mutualPosition(dest_tile, destStackPos+1) != -1)
  496. ba.additionalInfo = destStackPos+1;
  497. else if(BattleInfo::mutualPosition(dest_tile, destStackPos-1) != -1)
  498. ba.additionalInfo = destStackPos-1;
  499. else
  500. MakeDefend(attackerID);
  501. int nearest_dist = m_battleHelper.InfiniteDistance;
  502. int nearest_pos = -1;
  503. // if double wide calculate tail
  504. int tail_pos = -1;
  505. if (attackerStack->creature->isDoubleWide())
  506. {
  507. int x_pos = m_battleHelper.DecodeXPosition(attackerStack->position);
  508. int y_pos = m_battleHelper.DecodeYPosition(attackerStack->position);
  509. if (attackerStack->attackerOwned)
  510. {
  511. x_pos -= 1;
  512. }
  513. else
  514. {
  515. x_pos += 1;
  516. }
  517. // if creature can perform attack without movement - do it!
  518. tail_pos = m_battleHelper.GetBattleFieldPosition(x_pos, y_pos);
  519. if (dest_tile == tail_pos)
  520. {
  521. ba.additionalInfo = dest_tile;
  522. ba.actionType = action_walk_and_attack;
  523. #if defined PRINT_DEBUG
  524. PrintBattleAction(ba);
  525. #endif
  526. assert(m_cb->battleGetStackByPos(ba.additionalInfo)); //if action is action_walk_and_attack additional info must point on enemy stack
  527. assert(m_cb->battleGetStackByPos(ba.additionalInfo) != attackerStack); //don't attack ourselve
  528. return ba;
  529. }
  530. }
  531. for (std::vector<int>::const_iterator it = fields.begin(); it != fields.end(); ++it)
  532. {
  533. if (*it == dest_tile)
  534. {
  535. // attack!
  536. ba.actionType = action_walk_and_attack;
  537. #if defined PRINT_DEBUG
  538. PrintBattleAction(ba);
  539. #endif
  540. assert(m_cb->battleGetStackByPos(ba.additionalInfo)); //if action is action_walk_and_attack additional info must point on enemy stack
  541. assert(m_cb->battleGetStackByPos(ba.additionalInfo) != attackerStack); //don't attack ourselve
  542. return ba;
  543. }
  544. int d = m_battleHelper.GetDistanceWithObstacles(dest_tile, *it);
  545. if (d < nearest_dist)
  546. {
  547. nearest_dist = d;
  548. nearest_pos = *it;
  549. }
  550. }
  551. string message;
  552. message = "Attacker position X=";
  553. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(nearest_pos)) + ", Y=";
  554. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(nearest_pos));
  555. DbgBox(message.c_str());
  556. ba.actionType = action_walk;
  557. ba.destinationTile = (ui16)nearest_pos;
  558. ba.additionalInfo = -1;
  559. #if defined PRINT_DEBUG
  560. PrintBattleAction(ba);
  561. #endif
  562. return ba;
  563. }
  564. }
  565. /**
  566. * The main idea is to perform maximum casualties.
  567. */
  568. list<int> CBattleLogic::PerformBerserkAttack(int stackID, int &additionalInfo)
  569. {
  570. CCreature c = m_cb->battleGetCreature(stackID);
  571. // attack to make biggest damage
  572. list<int> creatures;
  573. if (!m_statCasualties.empty())
  574. {
  575. //creature_to_attack = m_statCasualties.begin()->first;
  576. creature_stat_casualties::iterator it = m_statCasualties.begin();
  577. for (; it != m_statCasualties.end(); ++it)
  578. {
  579. if (it->second.amount_min <= 0)
  580. {
  581. creatures.push_back(it->first);
  582. continue;
  583. }
  584. for (creature_stat::const_iterator it2 = m_statDistance.begin(); it2 != m_statDistance.end(); ++it2)
  585. {
  586. if (it2->first == it->first && it2->second - 1 <= c.speed)
  587. {
  588. creatures.push_front(it->first);
  589. }
  590. }
  591. }
  592. creatures.push_back(m_statCasualties.begin()->first);
  593. }
  594. return creatures;
  595. }
  596. list<int> CBattleLogic::PerformDefaultAction(int stackID, int &additionalInfo)
  597. {
  598. // first approach based on the statistics and weights
  599. // if this solution was fine we would develop this idea
  600. //
  601. std::map<int, int> votes;
  602. for (creature_stat::iterator it = m_statMaxDamage.begin(); it != m_statMaxDamage.end(); ++it)
  603. {
  604. votes[it->first] = 0;
  605. }
  606. votes[m_statMaxDamage.begin()->first] += m_battleHelper.GetVoteForMaxDamage();
  607. votes[m_statMinDamage.begin()->first] += m_battleHelper.GetVoteForMinDamage();
  608. if (m_statDistanceFromShooters.size())
  609. {
  610. votes[m_statDistanceFromShooters.begin()->first] += m_battleHelper.GetVoteForDistanceFromShooters();
  611. }
  612. votes[m_statDistance.begin()->first] += m_battleHelper.GetVoteForDistance();
  613. votes[m_statHitPoints.begin()->first] += m_battleHelper.GetVoteForHitPoints();
  614. votes[m_statMaxSpeed.begin()->first] += m_battleHelper.GetVoteForMaxSpeed();
  615. // get creature to attack
  616. int max_vote = 0;
  617. list<int> creatures;
  618. for (std::map<int, int>::iterator it = votes.begin(); it != votes.end(); ++it)
  619. {
  620. if (it->second > max_vote)
  621. {
  622. max_vote = it->second;
  623. creatures.push_front(it->first);
  624. }
  625. }
  626. additionalInfo = 0; // list contains creatures which shoud be attacked
  627. return creatures;
  628. }
  629. void CBattleLogic::PrintBattleAction(const BattleAction &action) // for debug purpose
  630. {
  631. std::string message("Battle action \n");
  632. message += "\taction type - ";
  633. switch (action.actionType)
  634. {
  635. case 0:
  636. message += "Cancel BattleAction\n";
  637. break;
  638. case 1:
  639. message += "Hero cast a spell\n";
  640. break;
  641. case 2:
  642. message += "Walk\n";
  643. break;
  644. case 3:
  645. message += "Defend\n";
  646. break;
  647. case 4:
  648. message += "Retreat from the battle\n";
  649. break;
  650. case 5:
  651. message += "Surrender\n";
  652. break;
  653. case 6:
  654. message += "Walk and Attack\n";
  655. break;
  656. case 7:
  657. message += "Shoot\n";
  658. break;
  659. case 8:
  660. message += "Wait\n";
  661. break;
  662. case 9:
  663. message += "Catapult\n";
  664. break;
  665. case 10:
  666. message += "Monster casts a spell\n";
  667. break;
  668. }
  669. message += "\tDestination tile: X = ";
  670. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.destinationTile));
  671. message += ", Y = " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.destinationTile));
  672. message += "\nAdditional info: ";
  673. if (action.actionType == 6)// || action.actionType == 7)
  674. {
  675. message += "stack - " + boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.additionalInfo));
  676. message += ", " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.additionalInfo));
  677. message += ", creature - ";
  678. CStack *c = m_cb->battleGetStackByPos(action.additionalInfo);
  679. if (c && c->creature)
  680. {
  681. message += c->creature->nameRef;
  682. }
  683. else
  684. {
  685. message += "NULL";
  686. }
  687. }
  688. else
  689. {
  690. message += boost::lexical_cast<std::string>(action.additionalInfo);
  691. }
  692. #ifdef _WIN32
  693. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  694. CONSOLE_SCREEN_BUFFER_INFO csbi;
  695. GetConsoleScreenBufferInfo(hConsole, &csbi);
  696. SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  697. #else
  698. std::string color;
  699. color = "\x1b[1;40;32m";
  700. std::cout << color;
  701. #endif
  702. std::cout << message.c_str() << std::endl;
  703. #ifdef _WIN32
  704. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  705. #else
  706. color = "\x1b[0m";
  707. std::cout << color;
  708. #endif
  709. }