StupidAI.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. CPlayerBattleCallback * 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( CPlayerBattleCallback * 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<BattleHex> 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(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = NULL)
  53. {
  54. int ret = 1000000;
  55. BOOST_FOREACH(BattleHex 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 ReachabilityInfo::TDistances & dists)
  67. {
  68. return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
  69. }
  70. static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const BattleHex &h2)
  71. {
  72. int shooters[2] = {0}; //count of shooters on hexes
  73. for(int i = 0; i < 2; i++)
  74. BOOST_FOREACH(BattleHex 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 for " + stack->nodeName());
  84. auto dists = cb->battleGetDistances(stack);
  85. std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
  86. if(stack->type->idNumber == 145) //catapult
  87. {
  88. BattleAction attack;
  89. static const int wallHexes[] = {50, 183, 182, 130, 62, 29, 12, 95};
  90. attack.destinationTile = wallHexes[ rand()%ARRAY_COUNT(wallHexes) ];
  91. attack.actionType = BattleAction::CATAPULT;
  92. attack.additionalInfo = 0;
  93. attack.side = side;
  94. attack.stackNumber = stack->ID;
  95. return attack;
  96. }
  97. BOOST_FOREACH(const CStack *s, cb->battleGetStacks(CBattleCallback::ONLY_ENEMY))
  98. {
  99. if(cb->battleCanShoot(stack, s->position))
  100. {
  101. enemiesShootable.push_back(s);
  102. }
  103. else
  104. {
  105. std::vector<BattleHex> avHexes = cb->battleGetAvailableHexes(stack, false);
  106. boost::copy(stack->getHexes(), std::back_inserter(avHexes)); //add current stack position - we can attack from it
  107. BOOST_FOREACH(BattleHex hex, avHexes)
  108. {
  109. if(CStack::isMeleeAttackPossible(stack, s, hex))
  110. {
  111. std::vector<EnemyInfo>::iterator i = std::find(enemiesReachable.begin(), enemiesReachable.end(), s);
  112. if(i == enemiesReachable.end())
  113. {
  114. enemiesReachable.push_back(s);
  115. i = enemiesReachable.begin() + (enemiesReachable.size() - 1);
  116. }
  117. i->attackFrom.push_back(hex);
  118. }
  119. }
  120. if(!vstd::contains(enemiesReachable, s) && s->position.isValid())
  121. enemiesUnreachable.push_back(s);
  122. }
  123. }
  124. if(enemiesShootable.size())
  125. {
  126. const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
  127. return BattleAction::makeShotAttack(stack, ei.s);
  128. }
  129. else if(enemiesReachable.size())
  130. {
  131. const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
  132. return BattleAction::makeMeleeAttack(stack, ei.s, *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), &willSecondHexBlockMoreEnemyShooters));
  133. }
  134. else
  135. {
  136. const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), boost::bind(isCloser, _1, _2, boost::ref(dists)));
  137. if(distToNearestNeighbour(ei.s->position, dists) < GameConstants::BFIELD_SIZE)
  138. {
  139. return goTowards(stack, ei.s->position);
  140. }
  141. }
  142. return BattleAction::makeDefend(stack);
  143. }
  144. void CStupidAI::battleAttack(const BattleAttack *ba)
  145. {
  146. print("battleAttack called");
  147. }
  148. void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  149. {
  150. print("battleStacksAttacked called");
  151. }
  152. void CStupidAI::battleEnd(const BattleResult *br)
  153. {
  154. print("battleEnd called");
  155. }
  156. // void CStupidAI::battleResultsApplied()
  157. // {
  158. // print("battleResultsApplied called");
  159. // }
  160. void CStupidAI::battleNewRoundFirst(int round)
  161. {
  162. print("battleNewRoundFirst called");
  163. }
  164. void CStupidAI::battleNewRound(int round)
  165. {
  166. print("battleNewRound called");
  167. }
  168. void CStupidAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  169. {
  170. print("battleStackMoved called");;
  171. }
  172. void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
  173. {
  174. print("battleSpellCast called");
  175. }
  176. void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
  177. {
  178. print("battleStacksEffectsSet called");
  179. }
  180. void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  181. {
  182. print("battleStart called");
  183. side = Side;
  184. }
  185. void CStupidAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  186. {
  187. print("battleStacksHealedRes called");
  188. }
  189. void CStupidAI::battleNewStackAppeared(const CStack * stack)
  190. {
  191. print("battleNewStackAppeared called");
  192. }
  193. void CStupidAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  194. {
  195. print("battleObstaclesRemoved called");
  196. }
  197. void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
  198. {
  199. print("battleCatapultAttacked called");
  200. }
  201. void CStupidAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  202. {
  203. print("battleStacksRemoved called");
  204. }
  205. void CStupidAI::print(const std::string &text) const
  206. {
  207. tlog6 << "CStupidAI [" << this <<"]: " << text << std::endl;
  208. }
  209. BattleAction CStupidAI::goTowards(const CStack * stack, BattleHex destination)
  210. {
  211. assert(destination.isValid());
  212. auto avHexes = cb->battleGetAvailableHexes(stack, false);
  213. auto reachability = cb->getReachability(stack);
  214. if(vstd::contains(avHexes, destination))
  215. return BattleAction::makeMove(stack, destination);
  216. auto destNeighbours = destination.neighbouringTiles();
  217. if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
  218. {
  219. tlog3 << "Warning: already standing on neighbouring tile!" << std::endl;
  220. //We shouldn't even be here...
  221. return BattleAction::makeDefend(stack);
  222. }
  223. vstd::erase_if(destNeighbours, [&](BattleHex hex){ return !reachability.accessibility.accessible(hex, stack); });
  224. if(!avHexes.size() || !destNeighbours.size()) //we are blocked or dest is blocked
  225. {
  226. print("goTowards: Stack cannot move! That's " + stack->nodeName());
  227. return BattleAction::makeDefend(stack);
  228. }
  229. if(stack->hasBonusOfType(Bonus::FLYING))
  230. {
  231. // Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
  232. // We just check all available hexes and pick the one closest to the target.
  233. auto distToDestNeighbour = [&](BattleHex hex) -> int
  234. {
  235. auto nearestNeighbourToHex = vstd::minElementByFun(destNeighbours, [&](BattleHex a)
  236. {
  237. return BattleHex::getDistance(a, hex);
  238. });
  239. return BattleHex::getDistance(*nearestNeighbourToHex, hex);
  240. };
  241. auto nearestAvailableHex = vstd::minElementByFun(avHexes, distToDestNeighbour);
  242. return BattleAction::makeMove(stack, *nearestAvailableHex);
  243. }
  244. else
  245. {
  246. BattleHex bestNeighbor = destination;
  247. if(distToNearestNeighbour(destination, reachability.distances, &bestNeighbor) > GameConstants::BFIELD_SIZE)
  248. {
  249. print("goTowards: Cannot reach");
  250. return BattleAction::makeDefend(stack);
  251. }
  252. BattleHex currentDest = bestNeighbor;
  253. while(1)
  254. {
  255. assert(currentDest.isValid());
  256. if(vstd::contains(avHexes, currentDest))
  257. return BattleAction::makeMove(stack, currentDest);
  258. currentDest = reachability.predecessors[currentDest];
  259. }
  260. }
  261. }