CGameInterface.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. std::string filename = VCMIDirs::get().libraryName(dllname);
  99. auto ret = createAny<rett>(VCMIDirs::get().libraryPath() + "/AI/" + filename, methodName);
  100. ret->dllName = dllname;
  101. return ret;
  102. }
  103. shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  104. {
  105. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  106. }
  107. shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
  108. {
  109. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  110. }
  111. shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
  112. {
  113. return createAny<CScriptingModule>(dllname, "GetNewModule");
  114. }
  115. BattleAction CGlobalAI::activeStack( const CStack * stack )
  116. {
  117. BattleAction ba; ba.actionType = Battle::DEFEND;
  118. ba.stackNumber = stack->ID;
  119. return ba;
  120. }
  121. CGlobalAI::CGlobalAI()
  122. {
  123. human = false;
  124. }
  125. void CAdventureAI::battleNewRound(int round)
  126. {
  127. battleAI->battleNewRound(round);
  128. }
  129. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  130. {
  131. battleAI->battleCatapultAttacked(ca);
  132. }
  133. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  134. {
  135. assert(!battleAI);
  136. assert(cbc);
  137. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  138. battleAI->init(cbc);
  139. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  140. }
  141. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  142. {
  143. battleAI->battleStacksAttacked(bsa);
  144. }
  145. void CAdventureAI::actionStarted(const BattleAction &action)
  146. {
  147. battleAI->actionStarted(action);
  148. }
  149. void CAdventureAI::battleNewRoundFirst(int round)
  150. {
  151. battleAI->battleNewRoundFirst(round);
  152. }
  153. void CAdventureAI::actionFinished(const BattleAction &action)
  154. {
  155. battleAI->actionFinished(action);
  156. }
  157. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  158. {
  159. battleAI->battleStacksEffectsSet(sse);
  160. }
  161. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  162. {
  163. battleAI->battleStacksRemoved(bsr);
  164. }
  165. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  166. {
  167. battleAI->battleObstaclesRemoved(removedObstacles);
  168. }
  169. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  170. {
  171. battleAI->battleNewStackAppeared(stack);
  172. }
  173. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  174. {
  175. battleAI->battleStackMoved(stack, dest, distance);
  176. }
  177. void CAdventureAI::battleAttack(const BattleAttack *ba)
  178. {
  179. battleAI->battleAttack(ba);
  180. }
  181. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  182. {
  183. battleAI->battleSpellCast(sc);
  184. }
  185. void CAdventureAI::battleEnd(const BattleResult *br)
  186. {
  187. battleAI->battleEnd(br);
  188. battleAI.reset();
  189. }
  190. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  191. {
  192. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  193. }
  194. BattleAction CAdventureAI::activeStack(const CStack * stack)
  195. {
  196. return battleAI->activeStack(stack);
  197. }
  198. void CAdventureAI::yourTacticPhase(int distance)
  199. {
  200. battleAI->yourTacticPhase(distance);
  201. }
  202. void CAdventureAI::saveGame(COSer<CSaveFile> &h, const int version) /*saving */
  203. {
  204. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  205. CGlobalAI::saveGame(h, version);
  206. bool hasBattleAI = static_cast<bool>(battleAI);
  207. h << hasBattleAI;
  208. if(hasBattleAI)
  209. {
  210. h << std::string(battleAI->dllName);
  211. battleAI->saveGame(h, version);
  212. }
  213. }
  214. void CAdventureAI::loadGame(CISer<CLoadFile> &h, const int version) /*loading */
  215. {
  216. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  217. CGlobalAI::loadGame(h, version);
  218. bool hasBattleAI = false;
  219. h >> hasBattleAI;
  220. if(hasBattleAI)
  221. {
  222. std::string dllName;
  223. h >> dllName;
  224. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  225. assert(cbc); //it should have been set by the one who new'ed us
  226. battleAI->init(cbc);
  227. //battleAI->loadGame(h, version);
  228. }
  229. }
  230. void CBattleGameInterface::saveGame(COSer<CSaveFile> &h, const int version)
  231. {
  232. }
  233. void CBattleGameInterface::loadGame(CISer<CLoadFile> &h, const int version)
  234. {
  235. }