CGameInterface.cpp 6.5 KB

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