CGameInterface.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "GameConstants.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. tlog0 << "Error: " << dlerror() <<"\n";
  43. #endif
  44. if (!dll)
  45. {
  46. tlog1 << "Cannot open dynamic library ("<<dllname<<"). Throwing..."<<std::endl;
  47. throw std::runtime_error("Cannot open dynamic library");
  48. }
  49. else if(!getName || !getAI)
  50. {
  51. tlog1 << dllname << " does not export method " << methodName << std::endl;
  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. tlog0 << "Loaded " << temp << std::endl;
  61. ret = getAI();
  62. if(!ret)
  63. tlog1 << "Cannot get AI!\n";
  64. return ret;
  65. }
  66. //Currently AI libraries use "lib" prefix only on non-win systems.
  67. //May be applied to Win systems as well to remove this ifdef
  68. #ifdef _WIN32
  69. std::string getAIFileName(std::string input)
  70. {
  71. return input + '.' + GameConstants::LIB_EXT;
  72. }
  73. #else
  74. std::string getAIFileName(std::string input)
  75. {
  76. return "lib" + input + '.' + GameConstants::LIB_EXT;
  77. }
  78. #endif
  79. template<typename rett>
  80. rett * createAnyAI(std::string dllname, std::string methodName)
  81. {
  82. tlog1<<"Opening "<<dllname<<"\n";
  83. std::string filename = getAIFileName(dllname);
  84. rett* ret = createAny<rett>(GameConstants::LIB_DIR + "/AI/" + filename, methodName);
  85. ret->dllName = dllname;
  86. return ret;
  87. }
  88. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  89. {
  90. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  91. }
  92. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  93. {
  94. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  95. }
  96. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  97. {
  98. return createAny<CScriptingModule>(dllname, "GetNewModule");
  99. }
  100. BattleAction CGlobalAI::activeStack( const CStack * stack )
  101. {
  102. BattleAction ba; ba.actionType = BattleAction::DEFEND;
  103. ba.stackNumber = stack->ID;
  104. return ba;
  105. }
  106. CGlobalAI::CGlobalAI()
  107. {
  108. human = false;
  109. }
  110. void CAdventureAI::battleNewRound(int round)
  111. {
  112. battleAI->battleNewRound(round);
  113. }
  114. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  115. {
  116. battleAI->battleCatapultAttacked(ca);
  117. }
  118. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  119. {
  120. assert(!battleAI);
  121. assert(cbc);
  122. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  123. battleAI->init(cbc);
  124. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  125. }
  126. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  127. {
  128. battleAI->battleStacksAttacked(bsa);
  129. }
  130. void CAdventureAI::actionStarted(const BattleAction *action)
  131. {
  132. battleAI->actionStarted(action);
  133. }
  134. void CAdventureAI::battleNewRoundFirst(int round)
  135. {
  136. battleAI->battleNewRoundFirst(round);
  137. }
  138. void CAdventureAI::actionFinished(const BattleAction *action)
  139. {
  140. battleAI->actionFinished(action);
  141. }
  142. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  143. {
  144. battleAI->battleStacksEffectsSet(sse);
  145. }
  146. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  147. {
  148. battleAI->battleStacksRemoved(bsr);
  149. }
  150. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  151. {
  152. battleAI->battleObstaclesRemoved(removedObstacles);
  153. }
  154. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  155. {
  156. battleAI->battleNewStackAppeared(stack);
  157. }
  158. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  159. {
  160. battleAI->battleStackMoved(stack, dest, distance);
  161. }
  162. void CAdventureAI::battleAttack(const BattleAttack *ba)
  163. {
  164. battleAI->battleAttack(ba);
  165. }
  166. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  167. {
  168. battleAI->battleSpellCast(sc);
  169. }
  170. void CAdventureAI::battleEnd(const BattleResult *br)
  171. {
  172. battleAI->battleEnd(br);
  173. vstd::clear_pointer(battleAI);
  174. }
  175. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  176. {
  177. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  178. }
  179. BattleAction CAdventureAI::activeStack(const CStack * stack)
  180. {
  181. return battleAI->activeStack(stack);
  182. }
  183. void CAdventureAI::yourTacticPhase(int distance)
  184. {
  185. battleAI->yourTacticPhase(distance);
  186. }