CGameInterface.cpp 6.8 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/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 = EActionType::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, const std::vector<MetaString> & battleLog)
  139. {
  140. battleAI->battleStacksAttacked(bsa, battleLog);
  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::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
  159. {
  160. battleAI->battleObstaclesChanged(obstacles);
  161. }
  162. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  163. {
  164. battleAI->battleStackMoved(stack, dest, distance);
  165. }
  166. void CAdventureAI::battleAttack(const BattleAttack * ba)
  167. {
  168. battleAI->battleAttack(ba);
  169. }
  170. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  171. {
  172. battleAI->battleSpellCast(sc);
  173. }
  174. void CAdventureAI::battleEnd(const BattleResult * br)
  175. {
  176. battleAI->battleEnd(br);
  177. battleAI.reset();
  178. }
  179. void CAdventureAI::battleUnitsChanged(const std::vector<UnitChanges> & units, const std::vector<CustomEffectInfo> & customEffects, const std::vector<MetaString> & battleLog)
  180. {
  181. battleAI->battleUnitsChanged(units, customEffects, battleLog);
  182. }
  183. BattleAction CAdventureAI::activeStack(const CStack * stack)
  184. {
  185. return battleAI->activeStack(stack);
  186. }
  187. void CAdventureAI::yourTacticPhase(int distance)
  188. {
  189. battleAI->yourTacticPhase(distance);
  190. }
  191. void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
  192. {
  193. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  194. CGlobalAI::saveGame(h, version);
  195. bool hasBattleAI = static_cast<bool>(battleAI);
  196. h & hasBattleAI;
  197. if(hasBattleAI)
  198. {
  199. h & std::string(battleAI->dllName);
  200. battleAI->saveGame(h, version);
  201. }
  202. }
  203. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  204. {
  205. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  206. CGlobalAI::loadGame(h, 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(cbc);
  216. //battleAI->loadGame(h, version);
  217. }
  218. }
  219. void CBattleGameInterface::saveGame(BinarySerializer & h, const int version)
  220. {
  221. }
  222. void CBattleGameInterface::loadGame(BinaryDeserializer & h, const int version)
  223. {
  224. }