StupidAI.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * StupidAI.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../../lib/AI_Base.h"
  12. #include "StupidAI.h"
  13. #include "../../lib/CStack.h"
  14. #include "../../CCallback.h"
  15. #include "../../lib/CCreatureHandler.h"
  16. static std::shared_ptr<CBattleCallback> cbc;
  17. CStupidAI::CStupidAI()
  18. : side(-1)
  19. {
  20. print("created");
  21. }
  22. CStupidAI::~CStupidAI()
  23. {
  24. print("destroyed");
  25. }
  26. void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
  27. {
  28. print("init called, saving ptr to IBattleCallback");
  29. env = ENV;
  30. cbc = cb = CB;
  31. }
  32. void CStupidAI::actionFinished(const BattleAction &action)
  33. {
  34. print("actionFinished called");
  35. }
  36. void CStupidAI::actionStarted(const BattleAction &action)
  37. {
  38. print("actionStarted called");
  39. }
  40. class EnemyInfo
  41. {
  42. public:
  43. const CStack * s;
  44. int adi, adr;
  45. std::vector<BattleHex> attackFrom; //for melee fight
  46. EnemyInfo(const CStack * _s) : s(_s), adi(0), adr(0)
  47. {}
  48. void calcDmg(const CStack * ourStack)
  49. {
  50. // FIXME: provide distance info for Jousting bonus
  51. TDmgRange retal, dmg = cbc->battleEstimateDamage(ourStack, s, 0, &retal);
  52. adi = static_cast<int>((dmg.first + dmg.second) / 2);
  53. adr = static_cast<int>((retal.first + retal.second) / 2);
  54. }
  55. bool operator==(const EnemyInfo& ei) const
  56. {
  57. return s == ei.s;
  58. }
  59. };
  60. bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
  61. {
  62. return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
  63. }
  64. static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const BattleHex &h2)
  65. {
  66. int shooters[2] = {0}; //count of shooters on hexes
  67. for(int i = 0; i < 2; i++)
  68. {
  69. for (auto & neighbour : (i ? h2 : h1).neighbouringTiles())
  70. if(const CStack * s = cbc->battleGetStackByPos(neighbour))
  71. if(s->isShooter())
  72. shooters[i]++;
  73. }
  74. return shooters[0] < shooters[1];
  75. }
  76. BattleAction CStupidAI::activeStack( const CStack * stack )
  77. {
  78. //boost::this_thread::sleep(boost::posix_time::seconds(2));
  79. print("activeStack called for " + stack->nodeName());
  80. ReachabilityInfo dists = cb->getReachability(stack);
  81. std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
  82. if(stack->type->idNumber == CreatureID::CATAPULT)
  83. {
  84. BattleAction attack;
  85. static const std::vector<int> wallHexes = {50, 183, 182, 130, 78, 29, 12, 95};
  86. auto seletectedHex = *RandomGeneratorUtil::nextItem(wallHexes, CRandomGenerator::getDefault());
  87. attack.aimToHex(seletectedHex);
  88. attack.actionType = EActionType::CATAPULT;
  89. attack.side = side;
  90. attack.stackNumber = stack->ID;
  91. return attack;
  92. }
  93. else if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON))
  94. {
  95. return BattleAction::makeDefend(stack);
  96. }
  97. for (const CStack *s : cb->battleGetStacks(CBattleCallback::ONLY_ENEMY))
  98. {
  99. if(cb->battleCanShoot(stack, s->getPosition()))
  100. {
  101. enemiesShootable.push_back(s);
  102. }
  103. else
  104. {
  105. std::vector<BattleHex> avHexes = cb->battleGetAvailableHexes(stack);
  106. for (BattleHex hex : avHexes)
  107. {
  108. if(CStack::isMeleeAttackPossible(stack, s, hex))
  109. {
  110. std::vector<EnemyInfo>::iterator i = std::find(enemiesReachable.begin(), enemiesReachable.end(), s);
  111. if(i == enemiesReachable.end())
  112. {
  113. enemiesReachable.push_back(s);
  114. i = enemiesReachable.begin() + (enemiesReachable.size() - 1);
  115. }
  116. i->attackFrom.push_back(hex);
  117. }
  118. }
  119. if(!vstd::contains(enemiesReachable, s) && s->getPosition().isValid())
  120. enemiesUnreachable.push_back(s);
  121. }
  122. }
  123. for ( auto & enemy : enemiesReachable )
  124. enemy.calcDmg( stack );
  125. for ( auto & enemy : enemiesShootable )
  126. enemy.calcDmg( stack );
  127. if(enemiesShootable.size())
  128. {
  129. const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
  130. return BattleAction::makeShotAttack(stack, ei.s);
  131. }
  132. else if(enemiesReachable.size())
  133. {
  134. const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
  135. return BattleAction::makeMeleeAttack(stack, ei.s->getPosition(), *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), &willSecondHexBlockMoreEnemyShooters));
  136. }
  137. else if(enemiesUnreachable.size()) //due to #955 - a buggy battle may occur when there are no enemies
  138. {
  139. auto closestEnemy = vstd::minElementByFun(enemiesUnreachable, [&](const EnemyInfo & ei) -> int
  140. {
  141. return dists.distToNearestNeighbour(stack, ei.s);
  142. });
  143. if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
  144. {
  145. return goTowards(stack, closestEnemy->s->getAttackableHexes(stack));
  146. }
  147. }
  148. return BattleAction::makeDefend(stack);
  149. }
  150. void CStupidAI::battleAttack(const BattleAttack *ba)
  151. {
  152. print("battleAttack called");
  153. }
  154. void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
  155. {
  156. print("battleStacksAttacked called");
  157. }
  158. void CStupidAI::battleEnd(const BattleResult *br)
  159. {
  160. print("battleEnd called");
  161. }
  162. // void CStupidAI::battleResultsApplied()
  163. // {
  164. // print("battleResultsApplied called");
  165. // }
  166. void CStupidAI::battleNewRoundFirst(int round)
  167. {
  168. print("battleNewRoundFirst called");
  169. }
  170. void CStupidAI::battleNewRound(int round)
  171. {
  172. print("battleNewRound called");
  173. }
  174. void CStupidAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
  175. {
  176. print("battleStackMoved called");
  177. }
  178. void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
  179. {
  180. print("battleSpellCast called");
  181. }
  182. void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
  183. {
  184. print("battleStacksEffectsSet called");
  185. }
  186. void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  187. {
  188. print("battleStart called");
  189. side = Side;
  190. }
  191. void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
  192. {
  193. print("battleCatapultAttacked called");
  194. }
  195. void CStupidAI::print(const std::string &text) const
  196. {
  197. logAi->trace("CStupidAI [%p]: %s", this, text);
  198. }
  199. BattleAction CStupidAI::goTowards(const CStack * stack, std::vector<BattleHex> hexes) const
  200. {
  201. auto reachability = cb->getReachability(stack);
  202. auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
  203. if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
  204. {
  205. return BattleAction::makeDefend(stack);
  206. }
  207. std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool
  208. {
  209. return reachability.distances[h1] < reachability.distances[h2];
  210. });
  211. for(auto hex : hexes)
  212. {
  213. if(vstd::contains(avHexes, hex))
  214. return BattleAction::makeMove(stack, hex);
  215. if(stack->coversPos(hex))
  216. {
  217. logAi->warn("Warning: already standing on neighbouring tile!");
  218. //We shouldn't even be here...
  219. return BattleAction::makeDefend(stack);
  220. }
  221. }
  222. BattleHex bestNeighbor = hexes.front();
  223. if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE)
  224. {
  225. return BattleAction::makeDefend(stack);
  226. }
  227. if(stack->hasBonusOfType(Bonus::FLYING))
  228. {
  229. // Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
  230. // We just check all available hexes and pick the one closest to the target.
  231. auto nearestAvailableHex = vstd::minElementByFun(avHexes, [&](BattleHex hex) -> int
  232. {
  233. return BattleHex::getDistance(bestNeighbor, hex);
  234. });
  235. return BattleAction::makeMove(stack, *nearestAvailableHex);
  236. }
  237. else
  238. {
  239. BattleHex currentDest = bestNeighbor;
  240. while(1)
  241. {
  242. if(!currentDest.isValid())
  243. {
  244. logAi->error("CBattleAI::goTowards: internal error");
  245. return BattleAction::makeDefend(stack);
  246. }
  247. if(vstd::contains(avHexes, currentDest))
  248. return BattleAction::makeMove(stack, currentDest);
  249. currentDest = reachability.predecessors[currentDest];
  250. }
  251. }
  252. }