CGameInterface.cpp 6.9 KB

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