CGameInterface.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include "Connection.h"
  12. /*
  13. * CGameInterface.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. template<typename rett>
  22. shared_ptr<rett> createAny(std::string dllname, std::string methodName)
  23. {
  24. typedef void(*TGetAIFun)(shared_ptr<rett>&);
  25. typedef void(*TGetNameFun)(char*);
  26. char temp[150];
  27. TGetAIFun getAI = nullptr;
  28. TGetNameFun getName = nullptr;
  29. #ifdef _WIN32
  30. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  31. if (dll)
  32. {
  33. getName = (TGetNameFun)GetProcAddress(dll,"GetAiName");
  34. getAI = (TGetAIFun)GetProcAddress(dll,methodName.c_str());
  35. }
  36. #else
  37. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  38. if (dll)
  39. {
  40. getName = (TGetNameFun)dlsym(dll,"GetAiName");
  41. getAI = (TGetAIFun)dlsym(dll,methodName.c_str());
  42. }
  43. else
  44. logGlobal->errorStream() << "Error: " << dlerror();
  45. #endif
  46. if (!dll)
  47. {
  48. logGlobal->errorStream() << "Cannot open dynamic library ("<<dllname<<"). Throwing...";
  49. throw std::runtime_error("Cannot open dynamic library");
  50. }
  51. else if(!getName || !getAI)
  52. {
  53. logGlobal->errorStream() << dllname << " does not export method " << methodName;
  54. #ifdef _WIN32
  55. FreeLibrary(dll);
  56. #else
  57. dlclose(dll);
  58. #endif
  59. throw std::runtime_error("Cannot find method " + methodName);
  60. }
  61. getName(temp);
  62. logGlobal->infoStream() << "Loaded " << temp;
  63. shared_ptr<rett> ret;
  64. getAI(ret);
  65. if(!ret)
  66. logGlobal->errorStream() << "Cannot get AI!";
  67. return ret;
  68. }
  69. template<typename rett>
  70. shared_ptr<rett> createAnyAI(std::string dllname, std::string methodName)
  71. {
  72. logGlobal->infoStream() << "Opening " << dllname;
  73. std::string filename = VCMIDirs::get().libraryName(dllname);
  74. auto ret = createAny<rett>(VCMIDirs::get().libraryPath() + "/AI/" + filename, methodName);
  75. ret->dllName = dllname;
  76. return ret;
  77. }
  78. shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  79. {
  80. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  81. }
  82. shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
  83. {
  84. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  85. }
  86. shared_ptr<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 = Battle::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. battleAI = nullptr;
  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. }
  177. void CAdventureAI::saveGame(COSer<CSaveFile> &h, const int version) /*saving */
  178. {
  179. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  180. CGlobalAI::saveGame(h, version);
  181. bool hasBattleAI = static_cast<bool>(battleAI);
  182. h << hasBattleAI;
  183. if(hasBattleAI)
  184. {
  185. h << std::string(battleAI->dllName);
  186. battleAI->saveGame(h, version);
  187. }
  188. }
  189. void CAdventureAI::loadGame(CISer<CLoadFile> &h, const int version) /*loading */
  190. {
  191. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  192. CGlobalAI::loadGame(h, version);
  193. bool hasBattleAI = false;
  194. h >> hasBattleAI;
  195. if(hasBattleAI)
  196. {
  197. std::string dllName;
  198. h >> dllName;
  199. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  200. assert(cbc); //it should have been set by the one who new'ed us
  201. battleAI->init(cbc);
  202. //battleAI->loadGame(h, version);
  203. }
  204. }
  205. void CBattleGameInterface::saveGame(COSer<CSaveFile> &h, const int version)
  206. {
  207. }
  208. void CBattleGameInterface::loadGame(CISer<CLoadFile> &h, const int version)
  209. {
  210. }