BattleLogic.cpp 23 KB

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