CGeniusAI.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "CGeniusAI.h"
  2. #include <iostream>
  3. using namespace std;
  4. using namespace GeniusAI;
  5. #if defined (_MSC_VER) && (_MSC_VER >= 1020) || (__MINGW32__)
  6. #include <windows.h>
  7. #endif
  8. void MsgBox(const char *msg, bool messageBox)
  9. {
  10. #if defined _DEBUG
  11. # if defined (_MSC_VER) && (_MSC_VER >= 1020)
  12. if (messageBox)
  13. {
  14. MessageBoxA(NULL, msg, "Debug message", MB_OK | MB_ICONASTERISK);
  15. }
  16. # endif
  17. std::cout << msg << std::endl;
  18. #endif
  19. }
  20. CGeniusAI::CGeniusAI()
  21. : m_generalAI()
  22. {
  23. }
  24. CGeniusAI::~CGeniusAI()
  25. {
  26. }
  27. void CGeniusAI::init(ICallback *CB)
  28. {
  29. m_cb = CB;
  30. m_generalAI.init(CB);
  31. human = false;
  32. playerID = m_cb->getMyColor();
  33. serialID = m_cb->getMySerial();
  34. std::string info = std::string("GeniusAI initialized for player ") + boost::lexical_cast<std::string>(playerID);
  35. m_battleLogic = NULL;
  36. MsgBox(info.c_str());
  37. }
  38. void CGeniusAI::yourTurn()
  39. {
  40. m_cb->endTurn();
  41. }
  42. void CGeniusAI::heroKilled(const CGHeroInstance *)
  43. {
  44. }
  45. void CGeniusAI::heroCreated(const CGHeroInstance *)
  46. {
  47. }
  48. void CGeniusAI::heroMoved(const HeroMoveDetails &)
  49. {
  50. }
  51. void CGeniusAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  52. {
  53. callback(rand() % skills.size());
  54. }
  55. void GeniusAI::CGeniusAI::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd )
  56. {
  57. onEnd();
  58. }
  59. void CGeniusAI::showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel)
  60. {
  61. m_cb->selectionMade(cancel ? 0 : 1, askID);
  62. }
  63. /**
  64. * occurs AFTER every action taken by any stack or by the hero
  65. */
  66. void CGeniusAI::actionFinished(const BattleAction *action)
  67. {
  68. std::string message("\t\tCGeniusAI::actionFinished - type(");
  69. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  70. message += "), side(";
  71. message += boost::lexical_cast<std::string>((unsigned)action->side);
  72. message += ")";
  73. MsgBox(message.c_str());
  74. }
  75. /**
  76. * occurs BEFORE every action taken by any stack or by the hero
  77. */
  78. void CGeniusAI::actionStarted(const BattleAction *action)
  79. {
  80. std::string message("\t\tCGeniusAI::actionStarted - type(");
  81. message += boost::lexical_cast<std::string>((unsigned)action->actionType);
  82. message += "), side(";
  83. message += boost::lexical_cast<std::string>((unsigned)action->side);
  84. message += ")";
  85. MsgBox(message.c_str());
  86. }
  87. /**
  88. * called when stack is performing attack
  89. */
  90. void CGeniusAI::battleAttack(BattleAttack *ba)
  91. {
  92. MsgBox("\t\t\tCGeniusAI::battleAttack");
  93. }
  94. /**
  95. * called when stack receives damage (after battleAttack())
  96. */
  97. void CGeniusAI::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  98. {
  99. MsgBox("\t\t\tCGeniusAI::battleStacksAttacked");
  100. }
  101. /**
  102. * called by engine when battle starts; side=0 - left, side=1 - right
  103. */
  104. void CGeniusAI::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side)
  105. {
  106. assert(!m_battleLogic);
  107. m_battleLogic = new BattleAI::CBattleLogic(m_cb, army1, army2, tile, hero1, hero2, side);
  108. MsgBox("** CGeniusAI::battleStart **");
  109. }
  110. /**
  111. *
  112. */
  113. void CGeniusAI::battleEnd(BattleResult *br)
  114. {
  115. delete m_battleLogic;
  116. m_battleLogic = NULL;
  117. MsgBox("** CGeniusAI::battleEnd **");
  118. }
  119. /**
  120. * called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  121. */
  122. void CGeniusAI::battleNewRound(int round)
  123. {
  124. std::string message("\tCGeniusAI::battleNewRound - ");
  125. message += boost::lexical_cast<std::string>(round);
  126. MsgBox(message.c_str());
  127. m_battleLogic->SetCurrentTurn(round);
  128. }
  129. /**
  130. *
  131. */
  132. void CGeniusAI::battleStackMoved(int ID, int dest, int distance, bool end)
  133. {
  134. std::string message("\t\t\tCGeniusAI::battleStackMoved ID(");
  135. message += boost::lexical_cast<std::string>(ID);
  136. message += "), dest(";
  137. message += boost::lexical_cast<std::string>(dest);
  138. message += ")";
  139. MsgBox(message.c_str());
  140. }
  141. /**
  142. *
  143. */
  144. void CGeniusAI::battleSpellCast(SpellCast *sc)
  145. {
  146. MsgBox("\t\t\tCGeniusAI::battleSpellCast");
  147. }
  148. /**
  149. * called when battlefield is prepared, prior the battle beginning
  150. */
  151. void CGeniusAI::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles)
  152. {
  153. MsgBox("CGeniusAI::battlefieldPrepared");
  154. }
  155. /**
  156. *
  157. */
  158. void CGeniusAI::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  159. {
  160. MsgBox("\t\t\tCGeniusAI::battleStackMoved");
  161. }
  162. /**
  163. *
  164. */
  165. void CGeniusAI::battleStackAttacking(int ID, int dest)
  166. {
  167. MsgBox("\t\t\tCGeniusAI::battleStackAttacking");
  168. }
  169. /**
  170. *
  171. */
  172. void CGeniusAI::battleStackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  173. {
  174. MsgBox("\t\t\tCGeniusAI::battleStackIsAttacked");
  175. }
  176. /**
  177. * called when it's turn of that stack
  178. */
  179. BattleAction CGeniusAI::activeStack(int stackID)
  180. {
  181. std::string message("\t\t\tCGeniusAI::activeStack stackID(");
  182. message += boost::lexical_cast<std::string>(stackID);
  183. message += ")";
  184. MsgBox(message.c_str());
  185. return m_battleLogic->MakeDecision(stackID);
  186. };