StupidAI.cpp 6.8 KB

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