CGameInterface.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "GameConstants.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. /*
  12. * CGameInterface.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. template<typename rett>
  21. rett * createAny(std::string dllname, std::string methodName)
  22. {
  23. char temp[50];
  24. rett * ret=NULL;
  25. rett*(*getAI)();
  26. void(*getName)(char*);
  27. #ifdef _WIN32
  28. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  29. if (dll)
  30. {
  31. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  32. getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str());
  33. }
  34. #else
  35. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  36. if (dll)
  37. {
  38. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  39. getAI = (rett*(*)())dlsym(dll,methodName.c_str());
  40. }
  41. #endif
  42. if (!dll)
  43. {
  44. tlog1 << "Cannot open dynamic library ("<<dllname<<"). Throwing..."<<std::endl;
  45. throw new std::string("Cannot open dynamic library");
  46. }
  47. getName(temp);
  48. tlog0 << "Loaded " << temp << std::endl;
  49. ret = getAI();
  50. if(!ret)
  51. tlog1 << "Cannot get AI!\n";
  52. return ret;
  53. }
  54. //Currently AI libraries use "lib" prefix only on non-win systems.
  55. //May be applied to Win systems as well to remove this ifdef
  56. #ifdef _WIN32
  57. std::string getAIFileName(std::string input)
  58. {
  59. return input + '.' + GameConstants::LIB_EXT;
  60. }
  61. #else
  62. std::string getAIFileName(std::string input)
  63. {
  64. return "lib" + input + '.' + GameConstants::LIB_EXT;
  65. }
  66. #endif
  67. template<typename rett>
  68. rett * createAnyAI(std::string dllname, std::string methodName)
  69. {
  70. tlog1<<"Opening "<<dllname<<"\n";
  71. std::string filename = getAIFileName(dllname);
  72. rett* ret = createAny<rett>(GameConstants::LIB_DIR + "/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 = BattleAction::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(battleAIName);
  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. }