CGameInterface.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. std::shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
  107. {
  108. return createAny<CScriptingModule>(dllname, "GetNewModule");
  109. }
  110. BattleAction CGlobalAI::activeStack(const CStack * stack)
  111. {
  112. BattleAction ba;
  113. ba.actionType = Battle::DEFEND;
  114. ba.stackNumber = stack->ID;
  115. return ba;
  116. }
  117. CGlobalAI::CGlobalAI()
  118. {
  119. human = false;
  120. }
  121. void CAdventureAI::battleNewRound(int round)
  122. {
  123. battleAI->battleNewRound(round);
  124. }
  125. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  126. {
  127. battleAI->battleCatapultAttacked(ca);
  128. }
  129. void CAdventureAI::battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile,
  130. const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool side)
  131. {
  132. assert(!battleAI);
  133. assert(cbc);
  134. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  135. battleAI->init(cbc);
  136. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  137. }
  138. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  139. {
  140. battleAI->battleStacksAttacked(bsa);
  141. }
  142. void CAdventureAI::actionStarted(const BattleAction & action)
  143. {
  144. battleAI->actionStarted(action);
  145. }
  146. void CAdventureAI::battleNewRoundFirst(int round)
  147. {
  148. battleAI->battleNewRoundFirst(round);
  149. }
  150. void CAdventureAI::actionFinished(const BattleAction & action)
  151. {
  152. battleAI->actionFinished(action);
  153. }
  154. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  155. {
  156. battleAI->battleStacksEffectsSet(sse);
  157. }
  158. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  159. {
  160. battleAI->battleStacksRemoved(bsr);
  161. }
  162. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  163. {
  164. battleAI->battleObstaclesRemoved(removedObstacles);
  165. }
  166. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  167. {
  168. battleAI->battleNewStackAppeared(stack);
  169. }
  170. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  171. {
  172. battleAI->battleStackMoved(stack, dest, distance);
  173. }
  174. void CAdventureAI::battleAttack(const BattleAttack * ba)
  175. {
  176. battleAI->battleAttack(ba);
  177. }
  178. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  179. {
  180. battleAI->battleSpellCast(sc);
  181. }
  182. void CAdventureAI::battleEnd(const BattleResult * br)
  183. {
  184. battleAI->battleEnd(br);
  185. battleAI.reset();
  186. }
  187. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain,
  188. bool tentHeal, si32 lifeDrainFrom)
  189. {
  190. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  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. CGlobalAI::saveGame(h, version);
  204. bool hasBattleAI = static_cast<bool>(battleAI);
  205. h & hasBattleAI;
  206. if(hasBattleAI)
  207. {
  208. h & std::string(battleAI->dllName);
  209. battleAI->saveGame(h, version);
  210. }
  211. }
  212. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  213. {
  214. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  215. CGlobalAI::loadGame(h, version);
  216. bool hasBattleAI = false;
  217. h & hasBattleAI;
  218. if(hasBattleAI)
  219. {
  220. std::string dllName;
  221. h & dllName;
  222. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  223. assert(cbc); //it should have been set by the one who new'ed us
  224. battleAI->init(cbc);
  225. //battleAI->loadGame(h, version);
  226. }
  227. }
  228. void CBattleGameInterface::saveGame(BinarySerializer & h, const int version)
  229. {
  230. }
  231. void CBattleGameInterface::loadGame(BinaryDeserializer & h, const int version)
  232. {
  233. }