CGameInterface.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "VCMIDirs.h"
  5. #ifdef VCMI_WINDOWS
  6. #include <windows.h> //for .dll libs
  7. #else
  8. #include <dlfcn.h>
  9. #endif
  10. #include "Connection.h"
  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. #ifdef VCMI_ANDROID
  21. // we can't use shared libraries on Android so here's a hack
  22. extern "C" DLL_EXPORT void VCAI_GetAiName(char* name);
  23. extern "C" DLL_EXPORT void VCAI_GetNewAI(std::shared_ptr<CGlobalAI> &out);
  24. extern "C" DLL_EXPORT void StupidAI_GetAiName(char* name);
  25. extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(std::shared_ptr<CGlobalAI> &out);
  26. extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
  27. extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(std::shared_ptr<CBattleGameInterface> &out);
  28. #endif
  29. template<typename rett>
  30. std::shared_ptr<rett> createAny(const boost::filesystem::path& libpath, const std::string& methodName)
  31. {
  32. typedef void(*TGetAIFun)(std::shared_ptr<rett>&);
  33. typedef void(*TGetNameFun)(char*);
  34. char temp[150];
  35. TGetAIFun getAI = nullptr;
  36. TGetNameFun getName = nullptr;
  37. #ifdef VCMI_ANDROID
  38. // this is awful but it seems using shared libraries on some devices is even worse
  39. const std::string filename = libpath.filename().string();
  40. if (filename == "libVCAI.so")
  41. {
  42. getName = (TGetNameFun)VCAI_GetAiName;
  43. getAI = (TGetAIFun)VCAI_GetNewAI;
  44. }
  45. else if (filename == "libStupidAI.so")
  46. {
  47. getName = (TGetNameFun)StupidAI_GetAiName;
  48. getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
  49. }
  50. else if (filename == "libBattleAI.so")
  51. {
  52. getName = (TGetNameFun)BattleAI_GetAiName;
  53. getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
  54. }
  55. else
  56. throw std::runtime_error("Don't know what to do with " + libpath.string() + " and method " + methodName);
  57. #else // !VCMI_ANDROID
  58. #ifdef VCMI_WINDOWS
  59. HMODULE dll = LoadLibraryW(libpath.c_str());
  60. if (dll)
  61. {
  62. getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
  63. getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
  64. }
  65. #else // !VCMI_WINDOWS
  66. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  67. if (dll)
  68. {
  69. getName = (TGetNameFun)dlsym(dll, "GetAiName");
  70. getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
  71. }
  72. else
  73. logGlobal->errorStream() << "Error: " << dlerror();
  74. #endif // VCMI_WINDOWS
  75. if (!dll)
  76. {
  77. logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). Throwing...";
  78. throw std::runtime_error("Cannot open dynamic library");
  79. }
  80. else if(!getName || !getAI)
  81. {
  82. logGlobal->errorStream() << libpath << " does not export method " << methodName;
  83. #ifdef VCMI_WINDOWS
  84. FreeLibrary(dll);
  85. #else
  86. dlclose(dll);
  87. #endif
  88. throw std::runtime_error("Cannot find method " + methodName);
  89. }
  90. #endif // VCMI_ANDROID
  91. getName(temp);
  92. logGlobal->infoStream() << "Loaded " << temp;
  93. std::shared_ptr<rett> ret;
  94. getAI(ret);
  95. if(!ret)
  96. logGlobal->error("Cannot get AI!");
  97. return ret;
  98. }
  99. template<typename rett>
  100. std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string& methodName)
  101. {
  102. logGlobal->infoStream() << "Opening " << dllname;
  103. const boost::filesystem::path filePath =
  104. VCMIDirs::get().libraryPath() / "AI" / VCMIDirs::get().libraryName(dllname);
  105. auto ret = createAny<rett>(filePath, methodName);
  106. ret->dllName = std::move(dllname);
  107. return ret;
  108. }
  109. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  110. {
  111. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  112. }
  113. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
  114. {
  115. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  116. }
  117. std::shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
  118. {
  119. return createAny<CScriptingModule>(dllname, "GetNewModule");
  120. }
  121. BattleAction CGlobalAI::activeStack( const CStack * stack )
  122. {
  123. BattleAction ba; ba.actionType = Battle::DEFEND;
  124. ba.stackNumber = stack->ID;
  125. return ba;
  126. }
  127. CGlobalAI::CGlobalAI()
  128. {
  129. human = false;
  130. }
  131. void CAdventureAI::battleNewRound(int round)
  132. {
  133. battleAI->battleNewRound(round);
  134. }
  135. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  136. {
  137. battleAI->battleCatapultAttacked(ca);
  138. }
  139. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  140. {
  141. assert(!battleAI);
  142. assert(cbc);
  143. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  144. battleAI->init(cbc);
  145. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  146. }
  147. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  148. {
  149. battleAI->battleStacksAttacked(bsa);
  150. }
  151. void CAdventureAI::actionStarted(const BattleAction &action)
  152. {
  153. battleAI->actionStarted(action);
  154. }
  155. void CAdventureAI::battleNewRoundFirst(int round)
  156. {
  157. battleAI->battleNewRoundFirst(round);
  158. }
  159. void CAdventureAI::actionFinished(const BattleAction &action)
  160. {
  161. battleAI->actionFinished(action);
  162. }
  163. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  164. {
  165. battleAI->battleStacksEffectsSet(sse);
  166. }
  167. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  168. {
  169. battleAI->battleStacksRemoved(bsr);
  170. }
  171. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  172. {
  173. battleAI->battleObstaclesRemoved(removedObstacles);
  174. }
  175. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  176. {
  177. battleAI->battleNewStackAppeared(stack);
  178. }
  179. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  180. {
  181. battleAI->battleStackMoved(stack, dest, distance);
  182. }
  183. void CAdventureAI::battleAttack(const BattleAttack *ba)
  184. {
  185. battleAI->battleAttack(ba);
  186. }
  187. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  188. {
  189. battleAI->battleSpellCast(sc);
  190. }
  191. void CAdventureAI::battleEnd(const BattleResult *br)
  192. {
  193. battleAI->battleEnd(br);
  194. battleAI.reset();
  195. }
  196. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  197. {
  198. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  199. }
  200. BattleAction CAdventureAI::activeStack(const CStack * stack)
  201. {
  202. return battleAI->activeStack(stack);
  203. }
  204. void CAdventureAI::yourTacticPhase(int distance)
  205. {
  206. battleAI->yourTacticPhase(distance);
  207. }
  208. void CAdventureAI::saveGame(COSer & h, const int version) /*saving */
  209. {
  210. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  211. CGlobalAI::saveGame(h, version);
  212. bool hasBattleAI = static_cast<bool>(battleAI);
  213. h << hasBattleAI;
  214. if(hasBattleAI)
  215. {
  216. h << std::string(battleAI->dllName);
  217. battleAI->saveGame(h, version);
  218. }
  219. }
  220. void CAdventureAI::loadGame(CISer & h, const int version) /*loading */
  221. {
  222. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  223. CGlobalAI::loadGame(h, version);
  224. bool hasBattleAI = false;
  225. h >> hasBattleAI;
  226. if(hasBattleAI)
  227. {
  228. std::string dllName;
  229. h >> dllName;
  230. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  231. assert(cbc); //it should have been set by the one who new'ed us
  232. battleAI->init(cbc);
  233. //battleAI->loadGame(h, version);
  234. }
  235. }
  236. void CBattleGameInterface::saveGame(COSer & h, const int version)
  237. {
  238. }
  239. void CBattleGameInterface::loadGame(CISer & h, const int version)
  240. {
  241. }