CGameInterface.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * CGameInterface.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGameInterface.h"
  12. #include "CStack.h"
  13. #include "VCMIDirs.h"
  14. #ifdef VCMI_WINDOWS
  15. #include <windows.h> //for .dll libs
  16. #elif !defined VCMI_ANDROID
  17. #include <dlfcn.h>
  18. #endif
  19. #include "serializer/BinaryDeserializer.h"
  20. #include "serializer/BinarySerializer.h"
  21. #ifdef VCMI_ANDROID
  22. #include "AI/VCAI/VCAI.h"
  23. #include "AI/Nullkiller/AIGateway.h"
  24. #include "AI/BattleAI/BattleAI.h"
  25. #endif
  26. VCMI_LIB_NAMESPACE_BEGIN
  27. template<typename rett>
  28. std::shared_ptr<rett> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  29. {
  30. #ifdef VCMI_ANDROID
  31. // android currently doesn't support loading libs dynamically, so the access to the known libraries
  32. // is possible only via specializations of this template
  33. throw std::runtime_error("Could not resolve ai library " + libpath.generic_string());
  34. #else
  35. typedef void(* TGetAIFun)(std::shared_ptr<rett> &);
  36. typedef void(* TGetNameFun)(char *);
  37. char temp[150];
  38. TGetAIFun getAI = nullptr;
  39. TGetNameFun getName = nullptr;
  40. #ifdef VCMI_WINDOWS
  41. #ifdef __MINGW32__
  42. #pragma GCC diagnostic push
  43. #pragma GCC diagnostic ignored "-Wcast-function-type"
  44. #endif
  45. HMODULE dll = LoadLibraryW(libpath.c_str());
  46. if (dll)
  47. {
  48. getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
  49. getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
  50. }
  51. #ifdef __MINGW32__
  52. #pragma GCC diagnostic pop
  53. #endif
  54. #else // !VCMI_WINDOWS
  55. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  56. if (dll)
  57. {
  58. getName = (TGetNameFun)dlsym(dll, "GetAiName");
  59. getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
  60. }
  61. #endif // VCMI_WINDOWS
  62. if (!dll)
  63. {
  64. logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
  65. throw std::runtime_error("Cannot open dynamic library");
  66. }
  67. else if(!getName || !getAI)
  68. {
  69. logGlobal->error("%s does not export method %s", libpath.string(), methodName);
  70. #ifdef VCMI_WINDOWS
  71. FreeLibrary(dll);
  72. #else
  73. dlclose(dll);
  74. #endif
  75. throw std::runtime_error("Cannot find method " + methodName);
  76. }
  77. getName(temp);
  78. logGlobal->info("Loaded %s", temp);
  79. std::shared_ptr<rett> ret;
  80. getAI(ret);
  81. if(!ret)
  82. logGlobal->error("Cannot get AI!");
  83. return ret;
  84. #endif //!VCMI_ANDROID
  85. }
  86. #ifdef VCMI_ANDROID
  87. template<>
  88. std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  89. {
  90. if(libpath.stem() == "libNullkiller") {
  91. return std::make_shared<NKAI::AIGateway>();
  92. }
  93. else{
  94. return std::make_shared<VCAI>();
  95. }
  96. }
  97. template<>
  98. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  99. {
  100. return std::make_shared<CBattleAI>();
  101. }
  102. #endif
  103. template<typename rett>
  104. std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string & methodName)
  105. {
  106. logGlobal->info("Opening %s", dllname);
  107. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  108. auto ret = createAny<rett>(filePath, methodName);
  109. ret->dllName = std::move(dllname);
  110. return ret;
  111. }
  112. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  113. {
  114. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  115. }
  116. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname)
  117. {
  118. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  119. }
  120. #if SCRIPTING_ENABLED
  121. std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
  122. {
  123. return createAny<scripting::Module>(dllname, "GetNewModule");
  124. }
  125. #endif
  126. BattleAction CGlobalAI::activeStack(const CStack * stack)
  127. {
  128. BattleAction ba;
  129. ba.actionType = EActionType::DEFEND;
  130. ba.stackNumber = stack->ID;
  131. return ba;
  132. }
  133. CGlobalAI::CGlobalAI()
  134. {
  135. human = false;
  136. }
  137. void CAdventureAI::battleNewRound(int round)
  138. {
  139. battleAI->battleNewRound(round);
  140. }
  141. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  142. {
  143. battleAI->battleCatapultAttacked(ca);
  144. }
  145. void CAdventureAI::battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile,
  146. const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool side)
  147. {
  148. assert(!battleAI);
  149. assert(cbc);
  150. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  151. battleAI->initBattleInterface(env, cbc);
  152. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  153. }
  154. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
  155. {
  156. battleAI->battleStacksAttacked(bsa, ranged);
  157. }
  158. void CAdventureAI::actionStarted(const BattleAction & action)
  159. {
  160. battleAI->actionStarted(action);
  161. }
  162. void CAdventureAI::battleNewRoundFirst(int round)
  163. {
  164. battleAI->battleNewRoundFirst(round);
  165. }
  166. void CAdventureAI::actionFinished(const BattleAction & action)
  167. {
  168. battleAI->actionFinished(action);
  169. }
  170. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  171. {
  172. battleAI->battleStacksEffectsSet(sse);
  173. }
  174. void CAdventureAI::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
  175. {
  176. battleAI->battleObstaclesChanged(obstacles);
  177. }
  178. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
  179. {
  180. battleAI->battleStackMoved(stack, dest, distance, teleport);
  181. }
  182. void CAdventureAI::battleAttack(const BattleAttack * ba)
  183. {
  184. battleAI->battleAttack(ba);
  185. }
  186. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  187. {
  188. battleAI->battleSpellCast(sc);
  189. }
  190. void CAdventureAI::battleEnd(const BattleResult * br)
  191. {
  192. battleAI->battleEnd(br);
  193. battleAI.reset();
  194. }
  195. void CAdventureAI::battleUnitsChanged(const std::vector<UnitChanges> & units)
  196. {
  197. battleAI->battleUnitsChanged(units);
  198. }
  199. BattleAction CAdventureAI::activeStack(const CStack * stack)
  200. {
  201. return battleAI->activeStack(stack);
  202. }
  203. void CAdventureAI::yourTacticPhase(int distance)
  204. {
  205. battleAI->yourTacticPhase(distance);
  206. }
  207. void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
  208. {
  209. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  210. bool hasBattleAI = static_cast<bool>(battleAI);
  211. h & hasBattleAI;
  212. if(hasBattleAI)
  213. {
  214. h & battleAI->dllName;
  215. }
  216. }
  217. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  218. {
  219. LOG_TRACE_PARAMS(logAi, "version '%i'", 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->initBattleInterface(env, cbc);
  229. }
  230. }
  231. VCMI_LIB_NAMESPACE_END