CGameInterface.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. HMODULE dll = LoadLibraryW(libpath.c_str());
  42. if (dll)
  43. {
  44. getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
  45. getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
  46. }
  47. #else // !VCMI_WINDOWS
  48. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  49. if (dll)
  50. {
  51. getName = (TGetNameFun)dlsym(dll, "GetAiName");
  52. getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
  53. }
  54. #endif // VCMI_WINDOWS
  55. if (!dll)
  56. {
  57. logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
  58. throw std::runtime_error("Cannot open dynamic library");
  59. }
  60. else if(!getName || !getAI)
  61. {
  62. logGlobal->error("%s does not export method %s", libpath.string(), methodName);
  63. #ifdef VCMI_WINDOWS
  64. FreeLibrary(dll);
  65. #else
  66. dlclose(dll);
  67. #endif
  68. throw std::runtime_error("Cannot find method " + methodName);
  69. }
  70. getName(temp);
  71. logGlobal->info("Loaded %s", temp);
  72. std::shared_ptr<rett> ret;
  73. getAI(ret);
  74. if(!ret)
  75. logGlobal->error("Cannot get AI!");
  76. return ret;
  77. #endif //!VCMI_ANDROID
  78. }
  79. #ifdef VCMI_ANDROID
  80. template<>
  81. std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  82. {
  83. if(libpath.stem() == "libNullkiller") {
  84. return std::make_shared<NKAI::AIGateway>();
  85. }
  86. else{
  87. return std::make_shared<VCAI>();
  88. }
  89. }
  90. template<>
  91. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  92. {
  93. return std::make_shared<CBattleAI>();
  94. }
  95. #endif
  96. template<typename rett>
  97. std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string & methodName)
  98. {
  99. logGlobal->info("Opening %s", dllname);
  100. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  101. auto ret = createAny<rett>(filePath, methodName);
  102. ret->dllName = std::move(dllname);
  103. return ret;
  104. }
  105. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  106. {
  107. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  108. }
  109. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname)
  110. {
  111. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  112. }
  113. #if SCRIPTING_ENABLED
  114. std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
  115. {
  116. return createAny<scripting::Module>(dllname, "GetNewModule");
  117. }
  118. #endif
  119. BattleAction CGlobalAI::activeStack(const CStack * stack)
  120. {
  121. BattleAction ba;
  122. ba.actionType = EActionType::DEFEND;
  123. ba.stackNumber = stack->ID;
  124. return ba;
  125. }
  126. CGlobalAI::CGlobalAI()
  127. {
  128. human = false;
  129. }
  130. void CAdventureAI::battleNewRound(int round)
  131. {
  132. battleAI->battleNewRound(round);
  133. }
  134. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  135. {
  136. battleAI->battleCatapultAttacked(ca);
  137. }
  138. void CAdventureAI::battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile,
  139. const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool side)
  140. {
  141. assert(!battleAI);
  142. assert(cbc);
  143. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  144. battleAI->initBattleInterface(env, cbc);
  145. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  146. }
  147. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
  148. {
  149. battleAI->battleStacksAttacked(bsa, ranged);
  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::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
  168. {
  169. battleAI->battleObstaclesChanged(obstacles);
  170. }
  171. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
  172. {
  173. battleAI->battleStackMoved(stack, dest, distance, teleport);
  174. }
  175. void CAdventureAI::battleAttack(const BattleAttack * ba)
  176. {
  177. battleAI->battleAttack(ba);
  178. }
  179. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  180. {
  181. battleAI->battleSpellCast(sc);
  182. }
  183. void CAdventureAI::battleEnd(const BattleResult * br)
  184. {
  185. battleAI->battleEnd(br);
  186. battleAI.reset();
  187. }
  188. void CAdventureAI::battleUnitsChanged(const std::vector<UnitChanges> & units)
  189. {
  190. battleAI->battleUnitsChanged(units);
  191. }
  192. BattleAction CAdventureAI::activeStack(const CStack * stack)
  193. {
  194. return battleAI->activeStack(stack);
  195. }
  196. void CAdventureAI::yourTacticPhase(int distance)
  197. {
  198. battleAI->yourTacticPhase(distance);
  199. }
  200. void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
  201. {
  202. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  203. bool hasBattleAI = static_cast<bool>(battleAI);
  204. h & hasBattleAI;
  205. if(hasBattleAI)
  206. {
  207. h & battleAI->dllName;
  208. }
  209. }
  210. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  211. {
  212. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  213. bool hasBattleAI = false;
  214. h & hasBattleAI;
  215. if(hasBattleAI)
  216. {
  217. std::string dllName;
  218. h & dllName;
  219. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  220. assert(cbc); //it should have been set by the one who new'ed us
  221. battleAI->initBattleInterface(env, cbc);
  222. }
  223. }
  224. VCMI_LIB_NAMESPACE_END