CGeniusAI.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. #include "CGeniusAI.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <boost/lexical_cast.hpp>
  6. #include <boost/lambda/lambda.hpp>
  7. #include <boost/lambda/bind.hpp>
  8. #include <boost/lambda/if.hpp>
  9. #include <boost/algorithm/string/split.hpp>
  10. #include <boost/algorithm/string/classification.hpp>
  11. #include <boost/algorithm/string.hpp>
  12. using namespace boost::lambda;
  13. using namespace std;
  14. using namespace GeniusAI;
  15. #if defined (_MSC_VER) && (_MSC_VER >= 1020) || (__MINGW32__)
  16. #include <windows.h>
  17. #endif
  18. void MsgBox(const char *msg, bool messageBox = false)
  19. {
  20. #if defined _DEBUG
  21. # if defined (_MSC_VER) && (_MSC_VER >= 1020)
  22. if (messageBox)
  23. {
  24. MessageBoxA(NULL, msg, "Debug message", MB_OK | MB_ICONASTERISK);
  25. }
  26. # endif
  27. std::cout << msg << std::endl;
  28. #endif
  29. }
  30. void CGeniusAI::init(ICallback *CB)
  31. {
  32. m_cb = CB;
  33. human = false;
  34. playerID = m_cb->getMyColor();
  35. serialID = m_cb->getMySerial();
  36. std::string info = std::string("GeniusAI initialized for player ") + boost::lexical_cast<std::string>(playerID);
  37. m_battleLogic = NULL;
  38. MsgBox(info.c_str());
  39. }
  40. void CGeniusAI::yourTurn()
  41. {
  42. m_cb->endTurn();
  43. }
  44. void CGeniusAI::heroKilled(const CGHeroInstance *)
  45. {
  46. }
  47. void CGeniusAI::heroCreated(const CGHeroInstance *)
  48. {
  49. }
  50. void CGeniusAI::heroMoved(const HeroMoveDetails &)
  51. {
  52. }
  53. void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  54. {
  55. callback(rand() % skills.size());
  56. }
  57. void CGeniusAI::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, bool selection, bool cancel )
  58. {
  59. m_cb->selectionMade(cancel ? 0 : 1, askID);
  60. }
  61. /**
  62. * occurs AFTER every action taken by any stack or by the hero
  63. */
  64. void CGeniusAI::actionFinished(const BattleAction *action)
  65. {
  66. std::string message("\t\tCGeniusAI::actionFinished - type(");
  67. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  68. message += "), side(";
  69. message += boost::lexical_cast<std::string>((unsigned)action->side);
  70. message += ")";
  71. MsgBox(message.c_str());
  72. }
  73. /**
  74. * occurs BEFORE every action taken by any stack or by the hero
  75. */
  76. void CGeniusAI::actionStarted(const BattleAction *action)
  77. {
  78. std::string message("\t\tCGeniusAI::actionStarted - type(");
  79. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  80. message += "), side(";
  81. message += boost::lexical_cast<std::string>((unsigned)action->side);
  82. message += ")";
  83. MsgBox(message.c_str());
  84. }
  85. /**
  86. * called when stack is performing attack
  87. */
  88. void CGeniusAI::battleAttack(BattleAttack *ba)
  89. {
  90. MsgBox("\t\t\tCGeniusAI::battleAttack");
  91. }
  92. /**
  93. * called when stack receives damage (after battleAttack())
  94. */
  95. void CGeniusAI::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  96. {
  97. MsgBox("\t\t\tCGeniusAI::battleStacksAttacked");
  98. }
  99. /**
  100. * called by engine when battle starts; side=0 - left, side=1 - right
  101. */
  102. void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  103. {
  104. assert(!m_battleLogic);
  105. m_battleLogic = new CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
  106. assert(m_battleLogic);
  107. MsgBox("** CGeniusAI::battleStart **");
  108. }
  109. /**
  110. *
  111. */
  112. void CGeniusAI::battleEnd(BattleResult *br)
  113. {
  114. delete m_battleLogic;
  115. m_battleLogic = NULL;
  116. MsgBox("** CGeniusAI::battleEnd **");
  117. }
  118. /**
  119. * called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  120. */
  121. void CGeniusAI::battleNewRound(int round)
  122. {
  123. std::string message("\tCGeniusAI::battleNewRound - ");
  124. message += boost::lexical_cast<std::string>(round);
  125. MsgBox(message.c_str());
  126. m_battleLogic->SetCurrentTurn(round);
  127. }
  128. /**
  129. *
  130. */
  131. void CGeniusAI::battleStackMoved(int ID, int dest, int distance, bool end)
  132. {
  133. std::string message("\t\t\tCGeniusAI::battleStackMoved ID(");
  134. message += boost::lexical_cast<std::string>(ID);
  135. message += "), dest(";
  136. message += boost::lexical_cast<std::string>(dest);
  137. message += ")";
  138. MsgBox(message.c_str());
  139. }
  140. /**
  141. *
  142. */
  143. void CGeniusAI::battleSpellCasted(SpellCasted *sc)
  144. {
  145. MsgBox("\t\t\tCGeniusAI::battleSpellCasted");
  146. }
  147. /**
  148. * called when battlefield is prepared, prior the battle beginning
  149. */
  150. void CGeniusAI::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles)
  151. {
  152. MsgBox("CGeniusAI::battlefieldPrepared");
  153. }
  154. /**
  155. *
  156. */
  157. void CGeniusAI::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  158. {
  159. MsgBox("\t\t\tCGeniusAI::battleStackMoved");
  160. }
  161. /**
  162. *
  163. */
  164. void CGeniusAI::battleStackAttacking(int ID, int dest)
  165. {
  166. MsgBox("\t\t\tCGeniusAI::battleStackAttacking");
  167. }
  168. /**
  169. *
  170. */
  171. void CGeniusAI::battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  172. {
  173. MsgBox("\t\t\tCGeniusAI::battleStackIsAttacked");
  174. }
  175. /**
  176. * called when it's turn of that stack
  177. */
  178. BattleAction CGeniusAI::activeStack(int stackID)
  179. {
  180. std::string message("\t\t\tCGeniusAI::activeStack stackID(");
  181. message += boost::lexical_cast<std::string>(stackID);
  182. message += ")";
  183. MsgBox(message.c_str());
  184. return m_battleLogic->MakeDecision(stackID);
  185. };
  186. /*
  187. ui8 side; //who made this action: false - left, true - right player
  188. ui32 stackNumber;//stack ID, -1 left hero, -2 right hero,
  189. ui8 actionType; //
  190. 0 = Cancel BattleAction
  191. 1 = Hero cast a spell
  192. 2 = Walk
  193. 3 = Defend
  194. 4 = Retreat from the battle
  195. 5 = Surrender
  196. 6 = Walk and Attack
  197. 7 = Shoot
  198. 8 = Wait
  199. 9 = Catapult
  200. 10 = Monster casts a spell (i.e. Faerie Dragons)
  201. ui16 destinationTile;
  202. si32 additionalInfo; // e.g. spell number if type is 1 || 10; tile to attack if type is 6
  203. */
  204. /**
  205. * Implementation of CBattleHelper class.
  206. */
  207. CBattleHelper::CBattleHelper():
  208. InfiniteDistance(0xffff),
  209. BattlefieldHeight(11),
  210. BattlefieldWidth(15),
  211. m_voteForDistance(10),
  212. m_voteForDistanceFromShooters(20),
  213. m_voteForHitPoints(10),
  214. m_voteForMaxDamage(10),
  215. m_voteForMaxSpeed(10),
  216. m_voteForMinDamage(10)
  217. {
  218. // loads votes
  219. std::fstream f;
  220. f.open("AI\\CBattleHelper.txt", std::ios::in);
  221. if (f)
  222. {
  223. //char c_line[512];
  224. std::string line;
  225. while (std::getline(f, line, '\n'))
  226. {
  227. //f.getline(c_line, sizeof(c_line), '\n');
  228. //std::string line(c_line);
  229. //std::getline(f, line);
  230. std::vector<std::string> parts;
  231. boost::algorithm::split(parts, line, boost::algorithm::is_any_of("="));
  232. if (parts.size() >= 2)
  233. {
  234. boost::algorithm::trim(parts[0]);
  235. boost::algorithm::trim(parts[1]);
  236. if (parts[0].compare("m_voteForDistance") == 0)
  237. {
  238. try
  239. {
  240. m_voteForDistance = boost::lexical_cast<int>(parts[1]);
  241. }
  242. catch (boost::bad_lexical_cast &)
  243. {}
  244. }
  245. else if (parts[0].compare("m_voteForDistanceFromShooters") == 0)
  246. {
  247. try
  248. {
  249. m_voteForDistanceFromShooters = boost::lexical_cast<int>(parts[1]);
  250. }
  251. catch (boost::bad_lexical_cast &)
  252. {}
  253. }
  254. else if (parts[0].compare("m_voteForHitPoints") == 0)
  255. {
  256. try
  257. {
  258. m_voteForHitPoints = boost::lexical_cast<int>(parts[1]);
  259. }
  260. catch (boost::bad_lexical_cast &)
  261. {}
  262. }
  263. else if (parts[0].compare("m_voteForMaxDamage") == 0)
  264. {
  265. try
  266. {
  267. m_voteForMaxDamage = boost::lexical_cast<int>(parts[1]);
  268. }
  269. catch (boost::bad_lexical_cast &)
  270. {}
  271. }
  272. else if (parts[0].compare("m_voteForMaxSpeed") == 0)
  273. {
  274. try
  275. {
  276. m_voteForMaxSpeed = boost::lexical_cast<int>(parts[1]);
  277. }
  278. catch (boost::bad_lexical_cast &)
  279. {}
  280. }
  281. else if (parts[0].compare("m_voteForMinDamage") == 0)
  282. {
  283. try
  284. {
  285. m_voteForMinDamage = boost::lexical_cast<int>(parts[1]);
  286. }
  287. catch (boost::bad_lexical_cast &)
  288. {}
  289. }
  290. }
  291. }
  292. f.close();
  293. }
  294. }
  295. CBattleHelper::~CBattleHelper()
  296. {}
  297. int CBattleHelper::GetBattleFieldPosition(int x, int y)
  298. {
  299. return x + 17 * (y - 1);
  300. }
  301. int CBattleHelper::DecodeXPosition(int battleFieldPosition)
  302. {
  303. int pos = battleFieldPosition - (DecodeYPosition(battleFieldPosition) - 1) * 17;
  304. assert(pos > 0 && pos < 16);
  305. return pos;
  306. }
  307. int CBattleHelper::DecodeYPosition(int battleFieldPosition)
  308. {
  309. double y = (double)battleFieldPosition / 17.0;
  310. if (y - (int)y > 0.0)
  311. {
  312. return (int)y + 1;
  313. }
  314. assert((int)y > 0 && (int)y <= 11);
  315. return (int)y;
  316. }
  317. int CBattleHelper::GetShortestDistance(int pointA, int pointB)
  318. {
  319. /**
  320. * TODO: function hasn't been checked!
  321. */
  322. int x1 = DecodeXPosition(pointA);
  323. int y1 = DecodeYPosition(pointA);
  324. //
  325. int x2 = DecodeXPosition(pointB);
  326. //x2 += (x2 % 2)? 0 : 1;
  327. int y2 = DecodeYPosition(pointB);
  328. //
  329. double dx = x1 - x2;
  330. double dy = y1 - y2;
  331. return (int)sqrt(dx * dx + dy * dy);
  332. }
  333. int CBattleHelper::GetDistanceWithObstacles(int pointA, int pointB)
  334. {
  335. // TODO - implement this!
  336. return GetShortestDistance(pointA, pointB);
  337. }
  338. /**
  339. * Implementation of CBattleLogic class.
  340. */
  341. CBattleLogic::CBattleLogic(ICallback *cb, CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) :
  342. m_cb(cb),
  343. m_iCurrentTurn(-2),
  344. m_bIsAttacker(false),
  345. m_army1(army1),
  346. m_army2(army2),
  347. m_tile(tile),
  348. m_hero1(hero1),
  349. m_hero2(hero2),
  350. m_side(side)
  351. {
  352. const int max_enemy_creatures = 12;
  353. m_statMaxDamage.reserve(max_enemy_creatures);
  354. m_statMinDamage.reserve(max_enemy_creatures);
  355. m_statMaxSpeed.reserve(max_enemy_creatures);
  356. m_statDistance.reserve(max_enemy_creatures);
  357. m_statDistanceFromShooters.reserve(max_enemy_creatures);
  358. m_statHitPoints.reserve(max_enemy_creatures);
  359. }
  360. CBattleLogic::~CBattleLogic()
  361. {}
  362. void CBattleLogic::SetCurrentTurn(int turn)
  363. {
  364. m_iCurrentTurn = turn;
  365. }
  366. void CBattleLogic::MakeStatistics(int currentCreatureId)
  367. {
  368. typedef std::map<int, CStack> map_stacks;
  369. map_stacks allStacks = m_cb->battleGetStacks();
  370. const CStack *currentStack = m_cb->battleGetStackByID(currentCreatureId);
  371. /*
  372. // find all creatures belong to the enemy
  373. std::for_each(allStacks.begin(), allStacks.end(),
  374. if_(bind<ui8>(&CStack::attackerOwned, bind<CStack>(&map_stacks::value_type::second, _1)) == m_bIsAttacker)
  375. [
  376. var(enemy)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  377. ret<CStack>(bind<CStack>(&map_stacks::value_type::second, _1))
  378. ]
  379. );
  380. // fill other containers
  381. // max damage
  382. std::for_each(enemy.begin(), enemy.end(),
  383. var(m_statMaxDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  384. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  385. bind<CStack>(&map_stacks::value_type::second, _1))))
  386. );
  387. // min damage
  388. std::for_each(enemy.begin(), enemy.end(),
  389. var(m_statMinDamage)[ret<int>(bind<int>(&map_stacks::value_type::first, _1))] =
  390. ret<int>(bind<int>(&CCreature::damageMax, bind<CCreature*>(&CStack::creature,
  391. bind<CStack>(&map_stacks::value_type::second, _1))))
  392. );
  393. */
  394. m_statMaxDamage.clear();
  395. m_statMinDamage.clear();
  396. m_statHitPoints.clear();
  397. m_statMaxSpeed.clear();
  398. m_statDistanceFromShooters.clear();
  399. m_statDistance.clear();
  400. m_statDistance.clear();
  401. m_statCasualties.clear();
  402. int totalEnemyDamage = 0;
  403. int totalEnemyHitPoints = 0;
  404. int totalDamage = 0;
  405. int totalHitPoints = 0;
  406. for (map_stacks::const_iterator it = allStacks.begin(); it != allStacks.end(); ++it)
  407. {
  408. const CStack *st = &it->second;
  409. if ((it->second.attackerOwned != 0) != m_bIsAttacker)
  410. {
  411. int id = it->first;
  412. if (st->amount < 1)
  413. {
  414. continue;
  415. }
  416. // make stats
  417. int hitPoints = st->amount * st->creature->hitPoints - (st->creature->hitPoints - st->firstHPleft);
  418. m_statMaxDamage.push_back(std::pair<int, int>(id, st->creature->damageMax * st->amount));
  419. m_statMinDamage.push_back(std::pair<int, int>(id, st->creature->damageMin * st->amount));
  420. m_statHitPoints.push_back(std::pair<int, int>(id, hitPoints));
  421. m_statMaxSpeed.push_back(std::pair<int, int>(id, st->creature->speed));
  422. totalEnemyDamage += (st->creature->damageMax + st->creature->damageMin) * st->amount / 2;
  423. totalEnemyHitPoints += hitPoints;
  424. // calculate casualties
  425. SCreatureCasualties cs;
  426. // hp * amount - damage * ( (att - def)>=0 )
  427. // hit poionts
  428. assert(hitPoints >= 0 && "CGeniusAI - creature cannot have hit points less than zero");
  429. CGHeroInstance *attackerHero = (m_side)? m_hero1 : m_hero2;
  430. CGHeroInstance *defendingHero = (m_side)? m_hero2 : m_hero1;
  431. int attackDefenseBonus = currentStack->creature->attack + (attackerHero ? attackerHero->getPrimSkillLevel(0) : 0) - (st->creature->defence + (defendingHero ? defendingHero->getPrimSkillLevel(1) : 0));
  432. float damageFactor = 1.0f;
  433. if(attackDefenseBonus < 0) //decreasing dmg
  434. {
  435. if(0.02f * (-attackDefenseBonus) > 0.3f)
  436. {
  437. damageFactor += -0.3f;
  438. }
  439. else
  440. {
  441. damageFactor += 0.02f * attackDefenseBonus;
  442. }
  443. }
  444. else //increasing dmg
  445. {
  446. if(0.05f * attackDefenseBonus > 4.0f)
  447. {
  448. damageFactor += 4.0f;
  449. }
  450. else
  451. {
  452. damageFactor += 0.05f * attackDefenseBonus;
  453. }
  454. }
  455. cs.damage_max = (int)(currentStack->creature->damageMax * currentStack->amount * damageFactor);
  456. if (cs.damage_max > hitPoints)
  457. {
  458. cs.damage_max = hitPoints;
  459. }
  460. cs.damage_min = (int)(currentStack->creature->damageMin * currentStack->amount * damageFactor);
  461. if (cs.damage_min > hitPoints)
  462. {
  463. cs.damage_min = hitPoints;
  464. }
  465. cs.amount_max = cs.damage_max / st->creature->hitPoints;
  466. cs.amount_min = cs.damage_min / st->creature->hitPoints;
  467. cs.leftHitPoints_for_max = (hitPoints - cs.damage_max) % st->creature->hitPoints;
  468. cs.leftHitPoint_for_min = (hitPoints - cs.damage_min) % st->creature->hitPoints;
  469. m_statCasualties.push_back(std::pair<int, SCreatureCasualties>(id, cs));
  470. if (st->creature->isShooting() && st->shots > 0)
  471. {
  472. m_statDistanceFromShooters.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  473. }
  474. if (currentStack->creature->isFlying() || (currentStack->creature->isShooting() && currentStack->shots > 0))
  475. {
  476. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetShortestDistance(currentStack->position, st->position)));
  477. }
  478. else
  479. {
  480. m_statDistance.push_back(std::pair<int, int>(id, m_battleHelper.GetDistanceWithObstacles(currentStack->position, st->position)));
  481. }
  482. }
  483. else
  484. {
  485. if (st->amount < 1)
  486. {
  487. continue;
  488. }
  489. int hitPoints = st->amount * st->creature->hitPoints - (st->creature->hitPoints - st->firstHPleft);
  490. totalDamage += (st->creature->damageMax + st->creature->damageMin) * st->amount / 2;
  491. totalHitPoints += hitPoints;
  492. }
  493. }
  494. if ((float)totalDamage / (float)totalEnemyDamage < 0.5f &&
  495. (float)totalHitPoints / (float)totalEnemyHitPoints < 0.5f)
  496. {
  497. m_bEnemyDominates = true;
  498. MsgBox("** EnemyDominates!");
  499. }
  500. else
  501. {
  502. m_bEnemyDominates = false;
  503. }
  504. // sort max damage
  505. std::sort(m_statMaxDamage.begin(), m_statMaxDamage.end(),
  506. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  507. // sort min damage
  508. std::sort(m_statMinDamage.begin(), m_statMinDamage.end(),
  509. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  510. // sort max speed
  511. std::sort(m_statMaxSpeed.begin(), m_statMaxSpeed.end(),
  512. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  513. // sort distance
  514. std::sort(m_statDistance.begin(), m_statDistance.end(),
  515. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  516. // sort distance from shooters
  517. std::sort(m_statDistanceFromShooters.begin(), m_statDistanceFromShooters.end(),
  518. bind(&creature_stat::value_type::second, _1) < bind(&creature_stat::value_type::second, _2));
  519. // sort hit points
  520. std::sort(m_statHitPoints.begin(), m_statHitPoints.end(),
  521. bind(&creature_stat::value_type::second, _1) > bind(&creature_stat::value_type::second, _2));
  522. // sort casualties
  523. std::sort(m_statCasualties.begin(), m_statCasualties.end(),
  524. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _1))
  525. >
  526. bind(&creature_stat_casualties::value_type::second_type::damage_max, bind(&creature_stat_casualties::value_type::second, _2)));
  527. }
  528. BattleAction CBattleLogic::MakeDecision(int stackID)
  529. {
  530. MakeStatistics(stackID);
  531. list<int> creatures;
  532. int additionalInfo;
  533. if (m_bEnemyDominates)
  534. {
  535. creatures = PerformBerserkAttack(stackID, additionalInfo);
  536. }
  537. else
  538. {
  539. creatures = PerformDefaultAction(stackID, additionalInfo);
  540. }
  541. /*
  542. std::string message("Creature will be attacked - ");
  543. message += boost::lexical_cast<std::string>(creature_to_attack);
  544. MsgBox(message.c_str());
  545. */
  546. if (additionalInfo == -1 || creatures.empty())
  547. {
  548. // defend
  549. return MakeDefend(stackID);
  550. }
  551. else if (additionalInfo == -2)
  552. {
  553. return MakeWait(stackID);
  554. }
  555. list<int>::iterator it, eit;
  556. eit = creatures.end();
  557. for (it = creatures.begin(); it != eit; ++it)
  558. {
  559. BattleAction ba = MakeAttack(stackID, *it);
  560. if (ba.actionType != action_walk_and_attack)
  561. {
  562. continue;
  563. }
  564. else
  565. {
  566. PrintBattleAction(ba);
  567. return ba;
  568. }
  569. }
  570. BattleAction ba = MakeAttack(stackID, *creatures.begin());
  571. return ba;
  572. }
  573. std::vector<int> CBattleLogic::GetAvailableHexesForAttacker(CStack *defender, CStack *attacker)
  574. {
  575. int x = m_battleHelper.DecodeXPosition(defender->position);
  576. int y = m_battleHelper.DecodeYPosition(defender->position);
  577. bool defenderIsDW = defender->creature->isDoubleWide();
  578. bool attackerIsDW = attacker->creature->isDoubleWide();
  579. // TOTO: should be std::vector<int> but for debug purpose std::pair is used
  580. typedef std::pair<int, int> hexPoint;
  581. std::list<hexPoint> candidates;
  582. std::vector<int> fields;
  583. if (defenderIsDW)
  584. {
  585. if (defender->attackerOwned)
  586. {
  587. // from left side
  588. if (!(y % 2))
  589. {
  590. // up
  591. candidates.push_back(hexPoint(x - 2, y - 1));
  592. candidates.push_back(hexPoint(x - 1, y - 1));
  593. candidates.push_back(hexPoint(x, y - 1));
  594. // down
  595. candidates.push_back(hexPoint(x - 2, y + 1));
  596. candidates.push_back(hexPoint(x - 1, y + 1));
  597. candidates.push_back(hexPoint(x, y + 1));
  598. }
  599. else
  600. {
  601. // up
  602. candidates.push_back(hexPoint(x - 1, y - 1));
  603. candidates.push_back(hexPoint(x, y - 1));
  604. candidates.push_back(hexPoint(x + 1, y - 1));
  605. // down
  606. candidates.push_back(hexPoint(x - 1, y + 1));
  607. candidates.push_back(hexPoint(x, y + 1));
  608. candidates.push_back(hexPoint(x + 1, y + 1));
  609. }
  610. candidates.push_back(hexPoint(x - 2, y));
  611. candidates.push_back(hexPoint(x + 1, y));
  612. }
  613. else
  614. {
  615. // from right
  616. if (!(y % 2))
  617. {
  618. // up
  619. candidates.push_back(hexPoint(x - 1, y - 1));
  620. candidates.push_back(hexPoint(x, y - 1));
  621. candidates.push_back(hexPoint(x + 1, y - 1));
  622. // down
  623. candidates.push_back(hexPoint(x - 1, y + 1));
  624. candidates.push_back(hexPoint(x, y + 1));
  625. candidates.push_back(hexPoint(x + 1, y + 1));
  626. }
  627. else
  628. {
  629. // up
  630. candidates.push_back(hexPoint(x, y - 1));
  631. candidates.push_back(hexPoint(x + 1, y - 1));
  632. candidates.push_back(hexPoint(x + 2, y - 1));
  633. // down
  634. candidates.push_back(hexPoint(x, y + 1));
  635. candidates.push_back(hexPoint(x + 1, y + 1));
  636. candidates.push_back(hexPoint(x + 2, y + 1));
  637. }
  638. candidates.push_back(hexPoint(x - 1, y));
  639. candidates.push_back(hexPoint(x + 2, y));
  640. }
  641. }
  642. else
  643. {
  644. if (!(y % 2)) // even line
  645. {
  646. // up
  647. candidates.push_back(hexPoint(x - 1, y - 1));
  648. candidates.push_back(hexPoint(x, y - 1));
  649. // down
  650. candidates.push_back(hexPoint(x - 1, y + 1));
  651. candidates.push_back(hexPoint(x, y + 1));
  652. }
  653. else // odd line
  654. {
  655. // up
  656. candidates.push_back(hexPoint(x, y - 1));
  657. candidates.push_back(hexPoint(x + 1, y - 1));
  658. // down
  659. candidates.push_back(hexPoint(x, y + 1));
  660. candidates.push_back(hexPoint(x + 1, y + 1));
  661. }
  662. candidates.push_back(hexPoint(x + 1, y));
  663. candidates.push_back(hexPoint(x - 1, y));
  664. }
  665. // remove fields which are out of bounds or obstacles
  666. for (std::list<hexPoint>::iterator it = candidates.begin(); it != candidates.end(); ++it)
  667. {
  668. if (it->first < 1 || it->first > m_battleHelper.BattlefieldWidth ||
  669. it->second < 1 || it->second > m_battleHelper.BattlefieldHeight)
  670. {
  671. // field is out of bounds
  672. //it = candidates.erase(it);
  673. continue;
  674. }
  675. int new_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  676. CStack *st = m_cb->battleGetStackByPos(new_pos);
  677. if (st == NULL || st->amount < 1)
  678. {
  679. if (attackerIsDW)
  680. {
  681. int tail_pos = -1;
  682. if (attacker->attackerOwned) // left side
  683. {
  684. int tail_pos_x = it->first - 1;
  685. if (tail_pos_x < 1)
  686. {
  687. continue;
  688. }
  689. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  690. }
  691. else // right side
  692. {
  693. int tail_pos_x = it->first + 1;
  694. if (tail_pos_x > m_battleHelper.BattlefieldWidth)
  695. {
  696. continue;
  697. }
  698. tail_pos = m_battleHelper.GetBattleFieldPosition(it->first, it->second);
  699. }
  700. assert(tail_pos >= 0 && "Error during calculation position of double wide creature");
  701. CStack *tailStack = m_cb->battleGetStackByPos(tail_pos);
  702. if (st != NULL && st->amount >= 1)
  703. {
  704. continue;
  705. }
  706. }
  707. fields.push_back(new_pos);
  708. }
  709. else if (attacker)
  710. {
  711. if (attacker->ID == st->ID)
  712. {
  713. fields.push_back(new_pos);
  714. }
  715. }
  716. //
  717. //++it;
  718. }
  719. return fields;
  720. }
  721. BattleAction CBattleLogic::MakeDefend(int stackID)
  722. {
  723. BattleAction ba;
  724. ba.side = 1;
  725. ba.actionType = action_defend;
  726. ba.stackNumber = stackID;
  727. ba.additionalInfo = -1;
  728. return ba;
  729. }
  730. BattleAction CBattleLogic::MakeWait(int stackID)
  731. {
  732. BattleAction ba;
  733. ba.side = 1;
  734. ba.actionType = action_wait;
  735. ba.stackNumber = stackID;
  736. ba.additionalInfo = -1;
  737. return ba;
  738. }
  739. BattleAction CBattleLogic::MakeAttack(int attackerID, int destinationID)
  740. {
  741. if (m_cb->battleCanShoot(attackerID, m_cb->battleGetPos(destinationID)))
  742. {
  743. // shoot
  744. BattleAction ba;
  745. ba.side = 1;
  746. ba.additionalInfo = -1;
  747. ba.actionType = action_shoot; // shoot
  748. ba.stackNumber = attackerID;
  749. ba.destinationTile = (ui16)m_cb->battleGetPos(destinationID);
  750. return ba;
  751. }
  752. else
  753. {
  754. // go or go&attack
  755. int dest_tile = -1;
  756. std::vector<int> av_tiles = GetAvailableHexesForAttacker(m_cb->battleGetStackByID(destinationID), m_cb->battleGetStackByID(attackerID));
  757. if (av_tiles.size() < 1)
  758. {
  759. return MakeDefend(attackerID);
  760. }
  761. // get the best tile - now the nearest
  762. int prev_distance = m_battleHelper.InfiniteDistance;
  763. int currentPos = m_cb->battleGetPos(attackerID);
  764. for (std::vector<int>::iterator it = av_tiles.begin(); it != av_tiles.end(); ++it)
  765. {
  766. int dist = m_battleHelper.GetDistanceWithObstacles(*it, m_cb->battleGetPos(attackerID));
  767. if (dist < prev_distance)
  768. {
  769. prev_distance = dist;
  770. dest_tile = *it;
  771. }
  772. if (*it == currentPos)
  773. {
  774. dest_tile = currentPos;
  775. break;
  776. }
  777. }
  778. std::vector<int> fields = m_cb->battleGetAvailableHexes(attackerID, false);
  779. if(fields.size() == 0)
  780. {
  781. return MakeDefend(attackerID);
  782. }
  783. BattleAction ba;
  784. ba.side = 1;
  785. //ba.actionType = 6; // go and attack
  786. ba.stackNumber = attackerID;
  787. ba.destinationTile = (ui16)dest_tile;
  788. //simplified checking for possibility of attack (previous was too simplified)
  789. int destStackPos = m_cb->battleGetPos(destinationID);
  790. if(BattleInfo::mutualPosition(dest_tile, destStackPos) != -1)
  791. ba.additionalInfo = destStackPos;
  792. else if(BattleInfo::mutualPosition(dest_tile, destStackPos+1) != -1)
  793. ba.additionalInfo = destStackPos+1;
  794. else if(BattleInfo::mutualPosition(dest_tile, destStackPos-1) != -1)
  795. ba.additionalInfo = destStackPos-1;
  796. else
  797. MakeDefend(attackerID);
  798. int nearest_dist = m_battleHelper.InfiniteDistance;
  799. int nearest_pos = -1;
  800. // if double wide calculate tail
  801. CStack *attackerStack = m_cb->battleGetStackByID(attackerID);
  802. assert(attackerStack != NULL);
  803. int tail_pos = -1;
  804. if (attackerStack->creature->isDoubleWide())
  805. {
  806. int x_pos = m_battleHelper.DecodeXPosition(attackerStack->position);
  807. int y_pos = m_battleHelper.DecodeYPosition(attackerStack->position);
  808. if (attackerStack->attackerOwned)
  809. {
  810. x_pos -= 1;
  811. }
  812. else
  813. {
  814. x_pos += 1;
  815. }
  816. // if creature can perform attack without movement - do it!
  817. tail_pos = m_battleHelper.GetBattleFieldPosition(x_pos, y_pos);
  818. if (dest_tile == tail_pos)
  819. {
  820. ba.additionalInfo = dest_tile;
  821. ba.actionType = action_walk_and_attack;
  822. PrintBattleAction(ba);
  823. return ba;
  824. }
  825. }
  826. for (std::vector<int>::const_iterator it = fields.begin(); it != fields.end(); ++it)
  827. {
  828. if (*it == dest_tile)
  829. {
  830. // attack!
  831. ba.actionType = action_walk_and_attack;
  832. #if defined _DEBUG
  833. PrintBattleAction(ba);
  834. #endif
  835. return ba;
  836. }
  837. int d = m_battleHelper.GetDistanceWithObstacles(dest_tile, *it);
  838. if (d < nearest_dist)
  839. {
  840. nearest_dist = d;
  841. nearest_pos = *it;
  842. }
  843. }
  844. string message;
  845. message = "Attacker position X=";
  846. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(nearest_pos)) + ", Y=";
  847. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(nearest_pos));
  848. MsgBox(message.c_str());
  849. ba.actionType = action_walk;
  850. ba.destinationTile = (ui16)nearest_pos;
  851. ba.additionalInfo = -1;
  852. #if defined _DEBUG
  853. PrintBattleAction(ba);
  854. #endif
  855. return ba;
  856. }
  857. }
  858. /**
  859. * The main idea is to perform maximum casualties.
  860. */
  861. list<int> CBattleLogic::PerformBerserkAttack(int stackID, int &additionalInfo)
  862. {
  863. CCreature c = m_cb->battleGetCreature(stackID);
  864. // attack to make biggest damage
  865. list<int> creatures;
  866. if (!m_statCasualties.empty())
  867. {
  868. //creature_to_attack = m_statCasualties.begin()->first;
  869. creature_stat_casualties::iterator it = m_statCasualties.begin();
  870. for (; it != m_statCasualties.end(); ++it)
  871. {
  872. if (it->second.amount_min <= 0)
  873. {
  874. creatures.push_back(it->first);
  875. continue;
  876. }
  877. for (creature_stat::const_iterator it2 = m_statDistance.begin(); it2 != m_statDistance.end(); ++it2)
  878. {
  879. if (it2->first == it->first && it2->second - 1 <= c.speed)
  880. {
  881. creatures.push_front(it->first);
  882. }
  883. }
  884. }
  885. creatures.push_back(m_statCasualties.begin()->first);
  886. }
  887. return creatures;
  888. }
  889. list<int> CBattleLogic::PerformDefaultAction(int stackID, int &additionalInfo)
  890. {
  891. // first approach based on the statistics and weights
  892. // if this solution was fine we would develop this idea
  893. //
  894. std::map<int, int> votes;
  895. for (creature_stat::iterator it = m_statMaxDamage.begin(); it != m_statMaxDamage.end(); ++it)
  896. {
  897. votes[it->first] = 0;
  898. }
  899. votes[m_statMaxDamage.begin()->first] += m_battleHelper.GetVoteForMaxDamage();
  900. votes[m_statMinDamage.begin()->first] += m_battleHelper.GetVoteForMinDamage();
  901. if (m_statDistanceFromShooters.size())
  902. {
  903. votes[m_statDistanceFromShooters.begin()->first] += m_battleHelper.GetVoteForDistanceFromShooters();
  904. }
  905. votes[m_statDistance.begin()->first] += m_battleHelper.GetVoteForDistance();
  906. votes[m_statHitPoints.begin()->first] += m_battleHelper.GetVoteForHitPoints();
  907. votes[m_statMaxSpeed.begin()->first] += m_battleHelper.GetVoteForMaxSpeed();
  908. // get creature to attack
  909. int max_vote = 0;
  910. list<int> creatures;
  911. for (std::map<int, int>::iterator it = votes.begin(); it != votes.end(); ++it)
  912. {
  913. if (it->second > max_vote)
  914. {
  915. max_vote = it->second;
  916. creatures.push_front(it->first);
  917. }
  918. }
  919. additionalInfo = 0; // list contains creatures which shoud be attacked
  920. return creatures;
  921. }
  922. void CBattleLogic::PrintBattleAction(const BattleAction &action) // for debug purpose
  923. {
  924. std::string message("Battle action \n");
  925. message += "\taction type - ";
  926. switch (action.actionType)
  927. {
  928. case 0:
  929. message += "Cancel BattleAction\n";
  930. break;
  931. case 1:
  932. message += "Hero cast a spell\n";
  933. break;
  934. case 2:
  935. message += "Walk\n";
  936. break;
  937. case 3:
  938. message += "Defend\n";
  939. break;
  940. case 4:
  941. message += "Retreat from the battle\n";
  942. break;
  943. case 5:
  944. message += "Surrender\n";
  945. break;
  946. case 6:
  947. message += "Walk and Attack\n";
  948. break;
  949. case 7:
  950. message += "Shoot\n";
  951. break;
  952. case 8:
  953. message += "Wait\n";
  954. break;
  955. case 9:
  956. message += "Catapult\n";
  957. break;
  958. case 10:
  959. message += "Monster casts a spell\n";
  960. break;
  961. }
  962. message += "\tDestination tile: X = ";
  963. message += boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.destinationTile));
  964. message += ", Y = " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.destinationTile));
  965. message += "\nAdditional info: ";
  966. if (action.actionType == 6)// || action.actionType == 7)
  967. {
  968. message += "stack - " + boost::lexical_cast<std::string>(m_battleHelper.DecodeXPosition(action.additionalInfo));
  969. message += ", " + boost::lexical_cast<std::string>(m_battleHelper.DecodeYPosition(action.additionalInfo));
  970. message += ", creature - ";
  971. CStack *c = m_cb->battleGetStackByPos(action.additionalInfo);
  972. if (c && c->creature)
  973. {
  974. message += c->creature->nameRef;
  975. }
  976. else
  977. {
  978. message += "NULL";
  979. }
  980. }
  981. else
  982. {
  983. message += boost::lexical_cast<std::string>(action.additionalInfo);
  984. }
  985. #ifdef _WIN32
  986. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  987. CONSOLE_SCREEN_BUFFER_INFO csbi;
  988. GetConsoleScreenBufferInfo(hConsole, &csbi);
  989. SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  990. #else
  991. std::string color;
  992. color = "\x1b[1;40;32m";
  993. std::cout << color;
  994. #endif
  995. std::cout << message.c_str() << std::endl;
  996. #ifdef _WIN32
  997. SetConsoleTextAttribute(hConsole, csbi.wAttributes);
  998. #else
  999. color = "\x1b[0m";
  1000. std::cout << color;
  1001. #endif
  1002. }