CGameInterface.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/BattleAI/BattleAI.h"
  24. #endif
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. template<typename rett>
  27. std::shared_ptr<rett> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  28. {
  29. #ifdef VCMI_ANDROID
  30. // android currently doesn't support loading libs dynamically, so the access to the known libraries
  31. // is possible only via specializations of this template
  32. throw std::runtime_error("Could not resolve ai library " + libpath.generic_string());
  33. #else
  34. typedef void(* TGetAIFun)(std::shared_ptr<rett> &);
  35. typedef void(* TGetNameFun)(char *);
  36. char temp[150];
  37. TGetAIFun getAI = nullptr;
  38. TGetNameFun getName = nullptr;
  39. #ifdef VCMI_WINDOWS
  40. HMODULE dll = LoadLibraryW(libpath.c_str());
  41. if (dll)
  42. {
  43. getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
  44. getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
  45. }
  46. #else // !VCMI_WINDOWS
  47. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  48. if (dll)
  49. {
  50. getName = (TGetNameFun)dlsym(dll, "GetAiName");
  51. getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
  52. }
  53. #endif // VCMI_WINDOWS
  54. if (!dll)
  55. {
  56. logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
  57. throw std::runtime_error("Cannot open dynamic library");
  58. }
  59. else if(!getName || !getAI)
  60. {
  61. logGlobal->error("%s does not export method %s", libpath.string(), methodName);
  62. #ifdef VCMI_WINDOWS
  63. FreeLibrary(dll);
  64. #else
  65. dlclose(dll);
  66. #endif
  67. throw std::runtime_error("Cannot find method " + methodName);
  68. }
  69. getName(temp);
  70. logGlobal->info("Loaded %s", temp);
  71. std::shared_ptr<rett> ret;
  72. getAI(ret);
  73. if(!ret)
  74. logGlobal->error("Cannot get AI!");
  75. return ret;
  76. #endif //!VCMI_ANDROID
  77. }
  78. #ifdef VCMI_ANDROID
  79. template<>
  80. std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  81. {
  82. return std::make_shared<VCAI>();
  83. }
  84. template<>
  85. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  86. {
  87. return std::make_shared<CBattleAI>();
  88. }
  89. #endif
  90. template<typename rett>
  91. std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string & methodName)
  92. {
  93. logGlobal->info("Opening %s", dllname);
  94. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  95. auto ret = createAny<rett>(filePath, methodName);
  96. ret->dllName = std::move(dllname);
  97. return ret;
  98. }
  99. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  100. {
  101. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  102. }
  103. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname)
  104. {
  105. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  106. }
  107. #if SCRIPTING_ENABLED
  108. std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
  109. {
  110. return createAny<scripting::Module>(dllname, "GetNewModule");
  111. }
  112. #endif
  113. BattleAction CGlobalAI::activeStack(const CStack * stack)
  114. {
  115. BattleAction ba;
  116. ba.actionType = EActionType::DEFEND;
  117. ba.stackNumber = stack->ID;
  118. return ba;
  119. }
  120. CGlobalAI::CGlobalAI()
  121. {
  122. human = false;
  123. }
  124. void CAdventureAI::battleNewRound(int round)
  125. {
  126. battleAI->battleNewRound(round);
  127. }
  128. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  129. {
  130. battleAI->battleCatapultAttacked(ca);
  131. }
  132. void CAdventureAI::battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile,
  133. const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool side)
  134. {
  135. assert(!battleAI);
  136. assert(cbc);
  137. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  138. battleAI->init(env, 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::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
  162. {
  163. battleAI->battleObstaclesChanged(obstacles);
  164. }
  165. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  166. {
  167. battleAI->battleStackMoved(stack, dest, distance);
  168. }
  169. void CAdventureAI::battleAttack(const BattleAttack * ba)
  170. {
  171. battleAI->battleAttack(ba);
  172. }
  173. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  174. {
  175. battleAI->battleSpellCast(sc);
  176. }
  177. void CAdventureAI::battleEnd(const BattleResult * br)
  178. {
  179. battleAI->battleEnd(br);
  180. battleAI.reset();
  181. }
  182. void CAdventureAI::battleUnitsChanged(const std::vector<UnitChanges> & units, const std::vector<CustomEffectInfo> & customEffects)
  183. {
  184. battleAI->battleUnitsChanged(units, customEffects);
  185. }
  186. BattleAction CAdventureAI::activeStack(const CStack * stack)
  187. {
  188. return battleAI->activeStack(stack);
  189. }
  190. void CAdventureAI::yourTacticPhase(int distance)
  191. {
  192. battleAI->yourTacticPhase(distance);
  193. }
  194. void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
  195. {
  196. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  197. bool hasBattleAI = static_cast<bool>(battleAI);
  198. h & hasBattleAI;
  199. if(hasBattleAI)
  200. {
  201. h & battleAI->dllName;
  202. }
  203. }
  204. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  205. {
  206. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  207. bool hasBattleAI = false;
  208. h & hasBattleAI;
  209. if(hasBattleAI)
  210. {
  211. std::string dllName;
  212. h & dllName;
  213. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  214. assert(cbc); //it should have been set by the one who new'ed us
  215. battleAI->init(env, cbc);
  216. }
  217. }
  218. VCMI_LIB_NAMESPACE_END