BattleLogic.cpp 23 KB

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