StupidAI.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "stdafx.h"
  2. #include "StupidAI.h"
  3. #include "../../lib/BattleState.h"
  4. #include "../../CCallback.h"
  5. #include <boost/foreach.hpp>
  6. #include <boost/bind.hpp>
  7. #include "../../lib/CCreatureHandler.h"
  8. #include <algorithm>
  9. //#include <boost/thread.hpp>
  10. CBattleCallback * cbc;
  11. CStupidAI::CStupidAI(void)
  12. : side(-1), cb(NULL)
  13. {
  14. print("created");
  15. }
  16. CStupidAI::~CStupidAI(void)
  17. {
  18. print("destroyed");
  19. }
  20. void CStupidAI::init( CBattleCallback * CB )
  21. {
  22. print("init called, saving ptr to IBattleCallback");
  23. cbc = cb = CB;
  24. }
  25. void CStupidAI::actionFinished( const BattleAction *action )
  26. {
  27. print("actionFinished called");
  28. }
  29. void CStupidAI::actionStarted( const BattleAction *action )
  30. {
  31. print("actionStarted called");
  32. }
  33. struct EnemyInfo
  34. {
  35. const CStack * s;
  36. int adi, adr;
  37. std::vector<THex> attackFrom; //for melee fight
  38. EnemyInfo(const CStack * _s) : s(_s)
  39. {}
  40. void calcDmg(const CStack * ourStack)
  41. {
  42. TDmgRange retal, dmg = cbc->battleEstimateDamage(ourStack, s, &retal);
  43. adi = (dmg.first + dmg.second) / 2;
  44. adr = (retal.first + retal.second) / 2;
  45. }
  46. bool operator==(const EnemyInfo& ei) const
  47. {
  48. return s == ei.s;
  49. }
  50. };
  51. bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
  52. {
  53. return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
  54. }
  55. int distToNearestNeighbour(THex hex, const std::vector<int> & dists, THex *chosenHex = NULL)
  56. {
  57. int ret = 1000000;
  58. BOOST_FOREACH(THex n, hex.neighbouringTiles())
  59. {
  60. if(dists[n] >= 0 && dists[n] < ret)
  61. {
  62. ret = dists[n];
  63. if(chosenHex)
  64. *chosenHex = n;
  65. }
  66. }
  67. return ret;
  68. }
  69. bool isCloser(const EnemyInfo & ei1, const EnemyInfo & ei2, const std::vector<int> & dists)
  70. {
  71. return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
  72. }
  73. static bool willSecondHexBlockMoreEnemyShooters(const THex &h1, const THex &h2)
  74. {
  75. int shooters[2] = {0}; //count of shooters on hexes
  76. for(int i = 0; i < 2; i++)
  77. BOOST_FOREACH(THex neighbour, (i ? h2 : h1).neighbouringTiles())
  78. if(const CStack *s = cbc->battleGetStackByPos(neighbour))
  79. if(s->getCreature()->isShooting())
  80. shooters[i]++;
  81. return shooters[0] < shooters[1];
  82. }
  83. BattleAction CStupidAI::activeStack( const CStack * stack )
  84. {
  85. //boost::this_thread::sleep(boost::posix_time::seconds(2));
  86. print("activeStack called");
  87. std::vector<THex> avHexes = cb->battleGetAvailableHexes(stack, false);
  88. std::vector<int> dists = cb->battleGetDistances(stack);
  89. std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
  90. BOOST_FOREACH(const CStack *s, cb->battleGetStacks(CBattleCallback::ONLY_ENEMY))
  91. {
  92. if(cb->battleCanShoot(stack, s->position))
  93. {
  94. enemiesShootable.push_back(s);
  95. }
  96. else
  97. {
  98. BOOST_FOREACH(THex hex, avHexes)
  99. {
  100. if(CStack::isMeleeAttackPossible(stack, s, hex))
  101. {
  102. std::vector<EnemyInfo>::iterator i = std::find(enemiesReachable.begin(), enemiesReachable.end(), s);
  103. if(i == enemiesReachable.end())
  104. {
  105. enemiesReachable.push_back(s);
  106. i = enemiesReachable.begin() + (enemiesReachable.size() - 1);
  107. }
  108. i->attackFrom.push_back(hex);
  109. }
  110. }
  111. if(!vstd::contains(enemiesReachable, s))
  112. enemiesUnreachable.push_back(s);
  113. }
  114. }
  115. if(enemiesShootable.size())
  116. {
  117. const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
  118. return BattleAction::makeShotAttack(stack, ei.s);
  119. }
  120. else if(enemiesReachable.size())
  121. {
  122. const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
  123. return BattleAction::makeMeleeAttack(stack, ei.s, *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), &willSecondHexBlockMoreEnemyShooters));
  124. }
  125. else
  126. {
  127. const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), boost::bind(isCloser, _1, _2, boost::ref(dists)));
  128. if(distToNearestNeighbour(ei.s->position, dists) < BFIELD_SIZE)
  129. {
  130. return goTowards(stack, ei.s->position);
  131. }
  132. }
  133. return BattleAction::makeDefend(stack);
  134. }
  135. void CStupidAI::battleAttack(const BattleAttack *ba)
  136. {
  137. print("battleAttack called");
  138. }
  139. void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  140. {
  141. print("battleStacksAttacked called");
  142. }
  143. void CStupidAI::battleEnd(const BattleResult *br)
  144. {
  145. print("battleEnd called");
  146. }
  147. // void CStupidAI::battleResultsApplied()
  148. // {
  149. // print("battleResultsApplied called");
  150. // }
  151. void CStupidAI::battleNewRoundFirst(int round)
  152. {
  153. print("battleNewRoundFirst called");
  154. }
  155. void CStupidAI::battleNewRound(int round)
  156. {
  157. print("battleNewRound called");
  158. }
  159. void CStupidAI::battleStackMoved(const CStack * stack, THex dest, int distance, bool end)
  160. {
  161. print("battleStackMoved called");;
  162. }
  163. void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
  164. {
  165. print("battleSpellCast called");
  166. }
  167. void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
  168. {
  169. print("battleStacksEffectsSet called");
  170. }
  171. void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  172. {
  173. print("battleStart called");
  174. side = Side;
  175. }
  176. void CStupidAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  177. {
  178. print("battleStacksHealedRes called");
  179. }
  180. void CStupidAI::battleNewStackAppeared(const CStack * stack)
  181. {
  182. print("battleNewStackAppeared called");
  183. }
  184. void CStupidAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  185. {
  186. print("battleObstaclesRemoved called");
  187. }
  188. void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
  189. {
  190. print("battleCatapultAttacked called");
  191. }
  192. void CStupidAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  193. {
  194. print("battleStacksRemoved called");
  195. }
  196. void CStupidAI::print(const std::string &text) const
  197. {
  198. tlog0 << "CStupidAI [" << this <<"]: " << text << std::endl;
  199. }
  200. BattleAction CStupidAI::goTowards(const CStack * stack, THex hex)
  201. {
  202. THex realDest = hex;
  203. THex predecessors[BFIELD_SIZE];
  204. std::vector<int> dists = cb->battleGetDistances(stack, hex);
  205. if(distToNearestNeighbour(hex, dists, &realDest) > BFIELD_SIZE)
  206. {
  207. print("goTowards: Cannot reach");
  208. return BattleAction::makeDefend(stack);
  209. }
  210. dists = cb->battleGetDistances(stack, realDest, predecessors);
  211. std::vector<THex> avHexes = cb->battleGetAvailableHexes(stack, false);
  212. while(1)
  213. {
  214. assert(realDest.isValid());
  215. if(vstd::contains(avHexes, hex))
  216. return BattleAction::makeMove(stack, hex);
  217. hex = predecessors[hex];
  218. }
  219. }