StupidAI.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "stdafx.h"
  2. #include "StupidAI.h"
  3. #include "../../lib/BattleState.h"
  4. #include "../../CCallback.h"
  5. CStupidAI::CStupidAI(void)
  6. : side(-1), cb(NULL)
  7. {
  8. print("created");
  9. }
  10. CStupidAI::~CStupidAI(void)
  11. {
  12. print("destroyed");
  13. }
  14. void CStupidAI::init( IBattleCallback * CB )
  15. {
  16. print("init called, saving ptr to IBattleCallback");
  17. cb = CB;
  18. }
  19. void CStupidAI::actionFinished( const BattleAction *action )
  20. {
  21. print("actionFinished called");
  22. }
  23. void CStupidAI::actionStarted( const BattleAction *action )
  24. {
  25. print("actionStarted called");
  26. }
  27. BattleAction CStupidAI::activeStack( const CStack * stack )
  28. {
  29. print("activeStack called");
  30. std::vector<THex> avHexes = cb->battleGetAvailableHexes(stack, false);
  31. std::vector<const CStack *> avEnemies;
  32. for(int g=0; g<avHexes.size(); ++g)
  33. {
  34. const CStack * enemy = cb->battleGetStackByPos(avHexes[g]);
  35. if (enemy)
  36. {
  37. avEnemies.push_back(enemy);
  38. }
  39. }
  40. if(stack->position % 17 < 5) //move army little towards enemy
  41. {
  42. THex dest = stack->position + !side*2 - 1;
  43. print(stack->nodeName() + "will be moved to " + boost::lexical_cast<std::string>(dest));
  44. return BattleAction::makeMove(stack, dest);
  45. }
  46. if(avEnemies.size())
  47. {
  48. const CStack * enemy = avEnemies[0];
  49. //shooting
  50. if (cb->battleCanShoot(stack, enemy->position))
  51. {
  52. return BattleAction::makeShotAttack(stack, enemy);
  53. }
  54. //melee
  55. return BattleAction::makeMeleeAttack(stack, enemy, avHexes);
  56. }
  57. return BattleAction::makeDefend(stack);
  58. }
  59. void CStupidAI::battleAttack(const BattleAttack *ba)
  60. {
  61. print("battleAttack called");
  62. }
  63. void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  64. {
  65. print("battleStacksAttacked called");
  66. }
  67. void CStupidAI::battleEnd(const BattleResult *br)
  68. {
  69. print("battleEnd called");
  70. }
  71. void CStupidAI::battleResultsApplied()
  72. {
  73. print("battleResultsApplied called");
  74. }
  75. void CStupidAI::battleNewRoundFirst(int round)
  76. {
  77. print("battleNewRoundFirst called");
  78. }
  79. void CStupidAI::battleNewRound(int round)
  80. {
  81. print("battleNewRound called");
  82. }
  83. void CStupidAI::battleStackMoved(const CStack * stack, THex dest, int distance, bool end)
  84. {
  85. print("battleStackMoved called");;
  86. }
  87. void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
  88. {
  89. print("battleSpellCast called");
  90. }
  91. void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
  92. {
  93. print("battleStacksEffectsSet called");
  94. }
  95. void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  96. {
  97. print("battleStart called");
  98. side = Side;
  99. }
  100. void CStupidAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
  101. {
  102. print("battleStacksHealedRes called");
  103. }
  104. void CStupidAI::battleNewStackAppeared(const CStack * stack)
  105. {
  106. print("battleNewStackAppeared called");
  107. }
  108. void CStupidAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  109. {
  110. print("battleObstaclesRemoved called");
  111. }
  112. void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
  113. {
  114. print("battleCatapultAttacked called");
  115. }
  116. void CStupidAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  117. {
  118. print("battleStacksRemoved called");
  119. }
  120. void CStupidAI::print(const std::string &text) const
  121. {
  122. tlog0 << "CStupidAI [" << this <<"]: " << text << std::endl;
  123. }