CGameInterface.cpp 5.2 KB

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