CGameInterface.cpp 7.6 KB

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