CGameInterface.cpp 4.7 KB

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