CGameInterface.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "VCMIDirs.h"
  5. #ifdef _WIN32
  6. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  7. #include <windows.h> //for .dll libs
  8. #else
  9. #include <dlfcn.h>
  10. #endif
  11. #include "Connection.h"
  12. /*
  13. * CGameInterface.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. template<typename rett>
  22. rett * createAny(std::string dllname, std::string methodName)
  23. {
  24. char temp[50];
  25. rett * ret=NULL;
  26. rett*(*getAI)();
  27. void(*getName)(char*);
  28. #ifdef _WIN32
  29. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  30. if (dll)
  31. {
  32. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  33. getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str());
  34. }
  35. #else
  36. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  37. if (dll)
  38. {
  39. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  40. getAI = (rett*(*)())dlsym(dll,methodName.c_str());
  41. }
  42. else
  43. logGlobal->errorStream() << "Error: " << dlerror();
  44. #endif
  45. if (!dll)
  46. {
  47. logGlobal->errorStream() << "Cannot open dynamic library ("<<dllname<<"). Throwing...";
  48. throw std::runtime_error("Cannot open dynamic library");
  49. }
  50. else if(!getName || !getAI)
  51. {
  52. logGlobal->errorStream() << dllname << " does not export method " << methodName;
  53. #ifdef _WIN32
  54. FreeLibrary(dll);
  55. #else
  56. dlclose(dll);
  57. #endif
  58. throw std::runtime_error("Cannot find method " + methodName);
  59. }
  60. getName(temp);
  61. logGlobal->infoStream() << "Loaded " << temp;
  62. ret = getAI();
  63. if(!ret)
  64. logGlobal->errorStream() << "Cannot get AI!";
  65. return ret;
  66. }
  67. template<typename rett>
  68. rett * createAnyAI(std::string dllname, std::string methodName)
  69. {
  70. logGlobal->infoStream() << "Opening " << dllname;
  71. std::string filename = VCMIDirs::get().libraryName(dllname);
  72. rett* ret = createAny<rett>(VCMIDirs::get().libraryPath() + "/AI/" + filename, methodName);
  73. ret->dllName = dllname;
  74. return ret;
  75. }
  76. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  77. {
  78. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  79. }
  80. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  81. {
  82. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  83. }
  84. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  85. {
  86. return createAny<CScriptingModule>(dllname, "GetNewModule");
  87. }
  88. BattleAction CGlobalAI::activeStack( const CStack * stack )
  89. {
  90. BattleAction ba; ba.actionType = Battle::DEFEND;
  91. ba.stackNumber = stack->ID;
  92. return ba;
  93. }
  94. CGlobalAI::CGlobalAI()
  95. {
  96. human = false;
  97. }
  98. void CAdventureAI::battleNewRound(int round)
  99. {
  100. battleAI->battleNewRound(round);
  101. }
  102. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  103. {
  104. battleAI->battleCatapultAttacked(ca);
  105. }
  106. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  107. {
  108. assert(!battleAI);
  109. assert(cbc);
  110. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  111. battleAI->init(cbc);
  112. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  113. }
  114. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  115. {
  116. battleAI->battleStacksAttacked(bsa);
  117. }
  118. void CAdventureAI::actionStarted(const BattleAction &action)
  119. {
  120. battleAI->actionStarted(action);
  121. }
  122. void CAdventureAI::battleNewRoundFirst(int round)
  123. {
  124. battleAI->battleNewRoundFirst(round);
  125. }
  126. void CAdventureAI::actionFinished(const BattleAction &action)
  127. {
  128. battleAI->actionFinished(action);
  129. }
  130. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  131. {
  132. battleAI->battleStacksEffectsSet(sse);
  133. }
  134. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  135. {
  136. battleAI->battleStacksRemoved(bsr);
  137. }
  138. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  139. {
  140. battleAI->battleObstaclesRemoved(removedObstacles);
  141. }
  142. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  143. {
  144. battleAI->battleNewStackAppeared(stack);
  145. }
  146. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  147. {
  148. battleAI->battleStackMoved(stack, dest, distance);
  149. }
  150. void CAdventureAI::battleAttack(const BattleAttack *ba)
  151. {
  152. battleAI->battleAttack(ba);
  153. }
  154. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  155. {
  156. battleAI->battleSpellCast(sc);
  157. }
  158. void CAdventureAI::battleEnd(const BattleResult *br)
  159. {
  160. battleAI->battleEnd(br);
  161. vstd::clear_pointer(battleAI);
  162. }
  163. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  164. {
  165. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  166. }
  167. BattleAction CAdventureAI::activeStack(const CStack * stack)
  168. {
  169. return battleAI->activeStack(stack);
  170. }
  171. void CAdventureAI::yourTacticPhase(int distance)
  172. {
  173. battleAI->yourTacticPhase(distance);
  174. }
  175. void CAdventureAI::saveGame(COSer<CSaveFile> &h, const int version) /*saving */
  176. {
  177. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  178. CGlobalAI::saveGame(h, version);
  179. bool hasBattleAI = battleAI;
  180. h << hasBattleAI;
  181. if(hasBattleAI)
  182. {
  183. h << std::string(battleAI->dllName);
  184. battleAI->saveGame(h, version);
  185. }
  186. }
  187. void CAdventureAI::loadGame(CISer<CLoadFile> &h, const int version) /*loading */
  188. {
  189. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  190. CGlobalAI::loadGame(h, version);
  191. bool hasBattleAI = false;
  192. h >> hasBattleAI;
  193. if(hasBattleAI)
  194. {
  195. std::string dllName;
  196. h >> dllName;
  197. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  198. assert(cbc); //it should have been set by the one who new'ed us
  199. battleAI->init(cbc);
  200. //battleAI->loadGame(h, version);
  201. }
  202. }
  203. void CBattleGameInterface::saveGame(COSer<CSaveFile> &h, const int version)
  204. {
  205. }
  206. void CBattleGameInterface::loadGame(CISer<CLoadFile> &h, const int version)
  207. {
  208. }