CGameInterface.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "VCMIDirs.h"
  5. #ifdef _WIN32
  6. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  7. #include <windows.h> //for .dll libs
  8. #else
  9. #include <dlfcn.h>
  10. #endif
  11. /*
  12. * CGameInterface.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. template<typename rett>
  21. rett * createAny(std::string dllname, std::string methodName)
  22. {
  23. char temp[50];
  24. rett * ret=NULL;
  25. rett*(*getAI)();
  26. void(*getName)(char*);
  27. #ifdef _WIN32
  28. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  29. if (dll)
  30. {
  31. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  32. getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str());
  33. }
  34. #else
  35. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  36. if (dll)
  37. {
  38. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  39. getAI = (rett*(*)())dlsym(dll,methodName.c_str());
  40. }
  41. else
  42. logGlobal->errorStream() << "Error: " << dlerror();
  43. #endif
  44. if (!dll)
  45. {
  46. logGlobal->errorStream() << "Cannot open dynamic library ("<<dllname<<"). Throwing...";
  47. throw std::runtime_error("Cannot open dynamic library");
  48. }
  49. else if(!getName || !getAI)
  50. {
  51. logGlobal->errorStream() << dllname << " does not export method " << methodName;
  52. #ifdef _WIN32
  53. FreeLibrary(dll);
  54. #else
  55. dlclose(dll);
  56. #endif
  57. throw std::runtime_error("Cannot find method " + methodName);
  58. }
  59. getName(temp);
  60. logGlobal->infoStream() << "Loaded " << temp;
  61. ret = getAI();
  62. if(!ret)
  63. logGlobal->errorStream() << "Cannot get AI!";
  64. return ret;
  65. }
  66. template<typename rett>
  67. rett * createAnyAI(std::string dllname, std::string methodName)
  68. {
  69. logGlobal->infoStream() << "Opening " << dllname;
  70. std::string filename = VCMIDirs::get().libraryName(dllname);
  71. rett* ret = createAny<rett>(VCMIDirs::get().libraryPath() + "/AI/" + filename, methodName);
  72. ret->dllName = dllname;
  73. return ret;
  74. }
  75. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  76. {
  77. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  78. }
  79. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  80. {
  81. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  82. }
  83. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  84. {
  85. return createAny<CScriptingModule>(dllname, "GetNewModule");
  86. }
  87. BattleAction CGlobalAI::activeStack( const CStack * stack )
  88. {
  89. BattleAction ba; ba.actionType = Battle::DEFEND;
  90. ba.stackNumber = stack->ID;
  91. return ba;
  92. }
  93. CGlobalAI::CGlobalAI()
  94. {
  95. human = false;
  96. }
  97. void CAdventureAI::battleNewRound(int round)
  98. {
  99. battleAI->battleNewRound(round);
  100. }
  101. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  102. {
  103. battleAI->battleCatapultAttacked(ca);
  104. }
  105. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  106. {
  107. assert(!battleAI);
  108. assert(cbc);
  109. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  110. battleAI->init(cbc);
  111. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  112. }
  113. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  114. {
  115. battleAI->battleStacksAttacked(bsa);
  116. }
  117. void CAdventureAI::actionStarted(const BattleAction *action)
  118. {
  119. battleAI->actionStarted(action);
  120. }
  121. void CAdventureAI::battleNewRoundFirst(int round)
  122. {
  123. battleAI->battleNewRoundFirst(round);
  124. }
  125. void CAdventureAI::actionFinished(const BattleAction *action)
  126. {
  127. battleAI->actionFinished(action);
  128. }
  129. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  130. {
  131. battleAI->battleStacksEffectsSet(sse);
  132. }
  133. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  134. {
  135. battleAI->battleStacksRemoved(bsr);
  136. }
  137. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  138. {
  139. battleAI->battleObstaclesRemoved(removedObstacles);
  140. }
  141. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  142. {
  143. battleAI->battleNewStackAppeared(stack);
  144. }
  145. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  146. {
  147. battleAI->battleStackMoved(stack, dest, distance);
  148. }
  149. void CAdventureAI::battleAttack(const BattleAttack *ba)
  150. {
  151. battleAI->battleAttack(ba);
  152. }
  153. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  154. {
  155. battleAI->battleSpellCast(sc);
  156. }
  157. void CAdventureAI::battleEnd(const BattleResult *br)
  158. {
  159. battleAI->battleEnd(br);
  160. vstd::clear_pointer(battleAI);
  161. }
  162. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  163. {
  164. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  165. }
  166. BattleAction CAdventureAI::activeStack(const CStack * stack)
  167. {
  168. return battleAI->activeStack(stack);
  169. }
  170. void CAdventureAI::yourTacticPhase(int distance)
  171. {
  172. battleAI->yourTacticPhase(distance);
  173. }