CGameInterface.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #define VCMI_DLL
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #ifdef _WIN32
  5. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  6. #include <windows.h> //for .dll libs
  7. #else
  8. #include <dlfcn.h>
  9. #endif
  10. /*
  11. * CGameInterface.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. template<typename rett>
  20. rett * createAny(std::string dllname, std::string methodName)
  21. {
  22. char temp[500];
  23. rett * ret=NULL;
  24. rett*(*getAI)();
  25. void(*getName)(char*);
  26. #ifdef _WIN32
  27. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  28. if (dll)
  29. {
  30. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  31. getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str());
  32. }
  33. #else
  34. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  35. if (dll)
  36. {
  37. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  38. getAI = (rett*(*)())dlsym(dll,methodName.c_str());
  39. }
  40. #endif
  41. if (!dll)
  42. {
  43. tlog1 << "Cannot open dynamic library ("<<dllname<<"). Throwing..."<<std::endl;
  44. throw new std::string("Cannot open dynamic library");
  45. }
  46. getName(temp);
  47. tlog0 << "Loaded " << temp << std::endl;
  48. ret = getAI();
  49. if(!ret)
  50. tlog1 << "Cannot get AI!\n";
  51. return ret;
  52. }
  53. //Currently AI libraries use "lib" prefix only on non-win systems.
  54. //May be applied to Win systems as well to remove this ifdef
  55. #ifdef _WIN32
  56. std::string getAIFileName(std::string input)
  57. {
  58. return input + '.' + LIB_EXT;
  59. }
  60. #else
  61. std::string getAIFileName(std::string input)
  62. {
  63. return "lib" + input + '.' + LIB_EXT;
  64. }
  65. #endif
  66. template<typename rett>
  67. rett * createAnyAI(std::string dllname, std::string methodName)
  68. {
  69. tlog1 << "Opening " << dllname<<"\n";
  70. if(vstd::contains(dllname, '/') || vstd::contains(dllname, '\\'))
  71. {
  72. tlog1 << "Assuming that AI is an absolute path.\n";
  73. }
  74. else
  75. {
  76. std::string filename = getAIFileName(dllname);
  77. dllname = LIB_DIR "/AI/" + filename;
  78. tlog1 << "The AI path will be " << dllname << std::endl;
  79. }
  80. rett* ret = createAny<rett>(dllname, methodName);
  81. ret->dllName = dllname;
  82. return ret;
  83. }
  84. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  85. {
  86. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  87. }
  88. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  89. {
  90. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  91. }
  92. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  93. {
  94. return createAny<CScriptingModule>(dllname, "GetNewModule");
  95. }
  96. BattleAction CGlobalAI::activeStack( const CStack * stack )
  97. {
  98. BattleAction ba; ba.actionType = BattleAction::DEFEND;
  99. ba.stackNumber = stack->ID;
  100. return ba;
  101. }
  102. CGlobalAI::CGlobalAI()
  103. {
  104. human = false;
  105. }
  106. void CAdventureAI::battleNewRound(int round)
  107. {
  108. battleAI->battleNewRound(round);
  109. }
  110. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  111. {
  112. battleAI->battleCatapultAttacked(ca);
  113. }
  114. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  115. {
  116. assert(!battleAI);
  117. assert(cbc);
  118. battleAI = CDynLibHandler::getNewBattleAI(battleAIName);
  119. battleAI->init(cbc);
  120. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  121. }
  122. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  123. {
  124. battleAI->battleStacksAttacked(bsa);
  125. }
  126. void CAdventureAI::actionStarted(const BattleAction *action)
  127. {
  128. battleAI->actionStarted(action);
  129. }
  130. void CAdventureAI::battleNewRoundFirst(int round)
  131. {
  132. battleAI->battleNewRoundFirst(round);
  133. }
  134. void CAdventureAI::actionFinished(const BattleAction *action)
  135. {
  136. battleAI->actionFinished(action);
  137. }
  138. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  139. {
  140. battleAI->battleStacksEffectsSet(sse);
  141. }
  142. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  143. {
  144. battleAI->battleStacksRemoved(bsr);
  145. }
  146. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  147. {
  148. battleAI->battleObstaclesRemoved(removedObstacles);
  149. }
  150. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  151. {
  152. battleAI->battleNewStackAppeared(stack);
  153. }
  154. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<THex> dest, int distance)
  155. {
  156. battleAI->battleStackMoved(stack, dest, distance);
  157. }
  158. void CAdventureAI::battleAttack(const BattleAttack *ba)
  159. {
  160. battleAI->battleAttack(ba);
  161. }
  162. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  163. {
  164. battleAI->battleSpellCast(sc);
  165. }
  166. void CAdventureAI::battleEnd(const BattleResult *br)
  167. {
  168. battleAI->battleEnd(br);
  169. delNull(battleAI);
  170. }
  171. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  172. {
  173. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  174. }
  175. BattleAction CAdventureAI::activeStack(const CStack * stack)
  176. {
  177. return battleAI->activeStack(stack);
  178. }
  179. void CAdventureAI::yourTacticPhase(int distance)
  180. {
  181. battleAI->yourTacticPhase(distance);
  182. }