BattleLogic.cpp 23 KB

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