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