2
0

CGameInterface.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "CStack.h"
  4. #include "VCMIDirs.h"
  5. #ifdef VCMI_WINDOWS
  6. #include <windows.h> //for .dll libs
  7. #elif !defined VCMI_ANDROID
  8. #include <dlfcn.h>
  9. #endif
  10. #include "serializer/BinaryDeserializer.h"
  11. #include "serializer/BinarySerializer.h"
  12. #ifdef VCMI_ANDROID
  13. #include "AI/VCAI/VCAI.h"
  14. #include "AI/BattleAI/BattleAI.h"
  15. #endif
  16. /*
  17. * CGameInterface.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  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. else
  53. logGlobal->errorStream() << "Error: " << dlerror();
  54. #endif // VCMI_WINDOWS
  55. if (!dll)
  56. {
  57. logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). Throwing...";
  58. throw std::runtime_error("Cannot open dynamic library");
  59. }
  60. else if(!getName || !getAI)
  61. {
  62. logGlobal->errorStream() << libpath << " does not export method " << 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->infoStream() << "Loaded " << 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. return std::make_shared<VCAI>();
  84. }
  85. template<>
  86. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  87. {
  88. return std::make_shared<CBattleAI>();
  89. }
  90. #endif
  91. template<typename rett>
  92. std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string & methodName)
  93. {
  94. logGlobal->infoStream() << "Opening " << dllname;
  95. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  96. auto ret = createAny<rett>(filePath, methodName);
  97. ret->dllName = std::move(dllname);
  98. return ret;
  99. }
  100. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  101. {
  102. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  103. }
  104. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname)
  105. {
  106. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  107. }
  108. std::shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
  109. {
  110. return createAny<CScriptingModule>(dllname, "GetNewModule");
  111. }
  112. BattleAction CGlobalAI::activeStack(const CStack * stack)
  113. {
  114. BattleAction ba;
  115. ba.actionType = Battle::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(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::battleStacksRemoved(const BattleStacksRemoved & bsr)
  161. {
  162. battleAI->battleStacksRemoved(bsr);
  163. }
  164. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  165. {
  166. battleAI->battleObstaclesRemoved(removedObstacles);
  167. }
  168. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  169. {
  170. battleAI->battleNewStackAppeared(stack);
  171. }
  172. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  173. {
  174. battleAI->battleStackMoved(stack, dest, distance);
  175. }
  176. void CAdventureAI::battleAttack(const BattleAttack * ba)
  177. {
  178. battleAI->battleAttack(ba);
  179. }
  180. void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
  181. {
  182. battleAI->battleSpellCast(sc);
  183. }
  184. void CAdventureAI::battleEnd(const BattleResult * br)
  185. {
  186. battleAI->battleEnd(br);
  187. battleAI.reset();
  188. }
  189. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain,
  190. bool tentHeal, si32 lifeDrainFrom)
  191. {
  192. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  193. }
  194. BattleAction CAdventureAI::activeStack(const CStack * stack)
  195. {
  196. return battleAI->activeStack(stack);
  197. }
  198. void CAdventureAI::yourTacticPhase(int distance)
  199. {
  200. battleAI->yourTacticPhase(distance);
  201. }
  202. void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
  203. {
  204. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  205. CGlobalAI::saveGame(h, version);
  206. bool hasBattleAI = static_cast<bool>(battleAI);
  207. h & hasBattleAI;
  208. if(hasBattleAI)
  209. {
  210. h & std::string(battleAI->dllName);
  211. battleAI->saveGame(h, version);
  212. }
  213. }
  214. void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
  215. {
  216. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  217. CGlobalAI::loadGame(h, version);
  218. bool hasBattleAI = false;
  219. h & hasBattleAI;
  220. if(hasBattleAI)
  221. {
  222. std::string dllName;
  223. h & dllName;
  224. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  225. assert(cbc); //it should have been set by the one who new'ed us
  226. battleAI->init(cbc);
  227. //battleAI->loadGame(h, version);
  228. }
  229. }
  230. void CBattleGameInterface::saveGame(BinarySerializer & h, const int version)
  231. {
  232. }
  233. void CBattleGameInterface::loadGame(BinaryDeserializer & h, const int version)
  234. {
  235. }