BattleLogic.cpp 23 KB

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