CGameInterface.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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[50];
  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. std::string filename = getAIFileName(dllname);
  71. rett* ret = createAny<rett>(LIB_DIR "/AI/" + filename, methodName);
  72. ret->dllName = dllname;
  73. return ret;
  74. }
  75. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  76. {
  77. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  78. }
  79. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  80. {
  81. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  82. }
  83. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  84. {
  85. return createAny<CScriptingModule>(dllname, "GetNewModule");
  86. }
  87. BattleAction CGlobalAI::activeStack( const CStack * stack )
  88. {
  89. BattleAction ba; ba.actionType = BattleAction::DEFEND;
  90. ba.stackNumber = stack->ID;
  91. return ba;
  92. }
  93. CGlobalAI::CGlobalAI()
  94. {
  95. human = false;
  96. }
  97. void CAdventureAI::battleNewRound(int round)
  98. {
  99. battleAI->battleNewRound(round);
  100. }
  101. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  102. {
  103. battleAI->battleCatapultAttacked(ca);
  104. }
  105. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  106. {
  107. assert(!battleAI);
  108. assert(cbc);
  109. battleAI = CDynLibHandler::getNewBattleAI(battleAIName);
  110. battleAI->init(cbc);
  111. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  112. }
  113. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  114. {
  115. battleAI->battleStacksAttacked(bsa);
  116. }
  117. void CAdventureAI::actionStarted(const BattleAction *action)
  118. {
  119. battleAI->actionStarted(action);
  120. }
  121. void CAdventureAI::battleNewRoundFirst(int round)
  122. {
  123. battleAI->battleNewRoundFirst(round);
  124. }
  125. void CAdventureAI::actionFinished(const BattleAction *action)
  126. {
  127. battleAI->actionFinished(action);
  128. }
  129. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  130. {
  131. battleAI->battleStacksEffectsSet(sse);
  132. }
  133. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  134. {
  135. battleAI->battleStacksRemoved(bsr);
  136. }
  137. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  138. {
  139. battleAI->battleObstaclesRemoved(removedObstacles);
  140. }
  141. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  142. {
  143. battleAI->battleNewStackAppeared(stack);
  144. }
  145. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<THex> dest, int distance)
  146. {
  147. battleAI->battleStackMoved(stack, dest, distance);
  148. }
  149. void CAdventureAI::battleAttack(const BattleAttack *ba)
  150. {
  151. battleAI->battleAttack(ba);
  152. }
  153. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  154. {
  155. battleAI->battleSpellCast(sc);
  156. }
  157. void CAdventureAI::battleEnd(const BattleResult *br)
  158. {
  159. battleAI->battleEnd(br);
  160. delNull(battleAI);
  161. }
  162. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  163. {
  164. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  165. }
  166. BattleAction CAdventureAI::activeStack(const CStack * stack)
  167. {
  168. return battleAI->activeStack(stack);
  169. }
  170. void CAdventureAI::yourTacticPhase(int distance)
  171. {
  172. battleAI->yourTacticPhase(distance);
  173. }