2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-07-28 12:44:10 +03:00
|
|
|
#include "CGameInterface.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
#include "BattleState.h"
|
2013-03-02 21:41:25 +03:00
|
|
|
#include "VCMIDirs.h"
|
2007-10-21 19:45:13 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2009-08-17 16:08:05 +03:00
|
|
|
#define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
|
2007-10-21 19:45:13 +03:00
|
|
|
#include <windows.h> //for .dll libs
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2013-05-09 14:09:23 +03:00
|
|
|
#include "Connection.h"
|
2007-10-21 19:45:13 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CGameInterface.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
// we can't use shared libraries on Android so here's a hack
|
|
|
|
extern "C" DLL_EXPORT void VCAI_GetAiName(char* name);
|
|
|
|
extern "C" DLL_EXPORT void VCAI_GetNewAI(shared_ptr<CGlobalAI> &out);
|
2013-06-23 00:47:51 +03:00
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
extern "C" DLL_EXPORT void StupidAI_GetAiName(char* name);
|
|
|
|
extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(shared_ptr<CGlobalAI> &out);
|
|
|
|
|
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
|
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(shared_ptr<CBattleGameInterface> &out);
|
|
|
|
#endif
|
2013-06-23 00:47:51 +03:00
|
|
|
|
2010-12-22 22:14:40 +02:00
|
|
|
template<typename rett>
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<rett> createAny(std::string dllname, std::string methodName)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2013-06-23 00:47:51 +03:00
|
|
|
typedef void(*TGetAIFun)(shared_ptr<rett>&);
|
|
|
|
typedef void(*TGetNameFun)(char*);
|
|
|
|
|
|
|
|
char temp[150];
|
|
|
|
|
|
|
|
TGetAIFun getAI = nullptr;
|
|
|
|
TGetNameFun getName = nullptr;
|
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
// this is awful but it seems using shared libraries on some devices is even worse
|
|
|
|
if (dllname.find("libVCAI.so") != std::string::npos) {
|
|
|
|
getName = (TGetNameFun)VCAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)VCAI_GetNewAI;
|
|
|
|
} else if (dllname.find("libStupidAI.so") != std::string::npos) {
|
|
|
|
getName = (TGetNameFun)StupidAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
|
|
|
|
} else if (dllname.find("libBattleAI.so") != std::string::npos) {
|
|
|
|
getName = (TGetNameFun)BattleAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
|
|
|
|
} else {
|
|
|
|
throw std::runtime_error("Don't know what to do with " + dllname + " and method " + methodName);
|
|
|
|
}
|
|
|
|
#else
|
2008-08-02 18:08:03 +03:00
|
|
|
|
2007-10-21 19:45:13 +03:00
|
|
|
#ifdef _WIN32
|
2011-06-28 17:19:16 +03:00
|
|
|
HINSTANCE dll = LoadLibraryA(dllname.c_str());
|
|
|
|
if (dll)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2013-06-23 00:47:51 +03:00
|
|
|
getName = (TGetNameFun)GetProcAddress(dll,"GetAiName");
|
|
|
|
getAI = (TGetAIFun)GetProcAddress(dll,methodName.c_str());
|
2007-10-21 19:45:13 +03:00
|
|
|
}
|
|
|
|
#else
|
2011-06-28 17:19:16 +03:00
|
|
|
void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
|
|
|
|
if (dll)
|
|
|
|
{
|
2013-06-23 00:47:51 +03:00
|
|
|
getName = (TGetNameFun)dlsym(dll,"GetAiName");
|
|
|
|
getAI = (TGetAIFun)dlsym(dll,methodName.c_str());
|
2011-06-28 17:19:16 +03:00
|
|
|
}
|
2012-03-12 23:11:46 +03:00
|
|
|
else
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: " << dlerror();
|
2011-06-28 17:19:16 +03:00
|
|
|
#endif
|
2009-04-13 21:52:20 +03:00
|
|
|
if (!dll)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot open dynamic library ("<<dllname<<"). Throwing...";
|
2012-11-20 20:53:45 +03:00
|
|
|
throw std::runtime_error("Cannot open dynamic library");
|
2009-04-13 21:52:20 +03:00
|
|
|
}
|
2013-01-20 15:06:49 +03:00
|
|
|
else if(!getName || !getAI)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << dllname << " does not export method " << methodName;
|
2013-01-20 17:43:58 +03:00
|
|
|
#ifdef _WIN32
|
2013-01-20 15:06:49 +03:00
|
|
|
FreeLibrary(dll);
|
2013-01-20 17:43:58 +03:00
|
|
|
#else
|
|
|
|
dlclose(dll);
|
|
|
|
#endif
|
2013-01-20 15:06:49 +03:00
|
|
|
throw std::runtime_error("Cannot find method " + methodName);
|
|
|
|
}
|
2011-06-28 17:19:16 +03:00
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
#endif // __ANDROID__
|
|
|
|
|
2009-01-31 04:36:44 +02:00
|
|
|
getName(temp);
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "Loaded " << temp;
|
2009-03-28 20:46:20 +02:00
|
|
|
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<rett> ret;
|
|
|
|
getAI(ret);
|
2009-03-28 20:46:20 +02:00
|
|
|
if(!ret)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot get AI!";
|
2009-03-28 20:46:20 +02:00
|
|
|
|
2007-10-21 19:45:13 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2011-07-06 17:25:12 +03:00
|
|
|
|
2011-06-20 14:41:04 +03:00
|
|
|
template<typename rett>
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<rett> createAnyAI(std::string dllname, std::string methodName)
|
2011-06-20 14:41:04 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->infoStream() << "Opening " << dllname;
|
2013-03-02 21:41:25 +03:00
|
|
|
std::string filename = VCMIDirs::get().libraryName(dllname);
|
2013-06-23 00:47:51 +03:00
|
|
|
|
|
|
|
auto ret = createAny<rett>(VCMIDirs::get().libraryPath() + "/AI/" + filename, methodName);
|
2011-07-23 02:34:15 +03:00
|
|
|
ret->dllName = dllname;
|
2011-06-20 14:41:04 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2011-02-23 05:57:45 +02:00
|
|
|
return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2011-02-23 05:57:45 +02:00
|
|
|
return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
2010-12-23 22:18:10 +02:00
|
|
|
|
2013-06-23 00:47:51 +03:00
|
|
|
shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
|
2011-06-20 14:41:04 +03:00
|
|
|
{
|
|
|
|
return createAny<CScriptingModule>(dllname, "GetNewModule");
|
|
|
|
}
|
|
|
|
|
2010-12-23 22:18:10 +02:00
|
|
|
BattleAction CGlobalAI::activeStack( const CStack * stack )
|
|
|
|
{
|
2013-02-04 00:05:44 +03:00
|
|
|
BattleAction ba; ba.actionType = Battle::DEFEND;
|
2010-12-23 22:18:10 +02:00
|
|
|
ba.stackNumber = stack->ID;
|
|
|
|
return ba;
|
|
|
|
}
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
CGlobalAI::CGlobalAI()
|
|
|
|
{
|
|
|
|
human = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleNewRound(int round)
|
|
|
|
{
|
|
|
|
battleAI->battleNewRound(round);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
|
|
|
|
{
|
|
|
|
battleAI->battleCatapultAttacked(ca);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
|
|
|
|
{
|
|
|
|
assert(!battleAI);
|
2011-07-05 22:05:41 +03:00
|
|
|
assert(cbc);
|
2012-09-29 13:59:43 +03:00
|
|
|
battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
|
2011-07-05 22:05:41 +03:00
|
|
|
battleAI->init(cbc);
|
2011-07-05 09:14:07 +03:00
|
|
|
battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
|
|
|
|
{
|
|
|
|
battleAI->battleStacksAttacked(bsa);
|
|
|
|
}
|
|
|
|
|
2013-05-09 14:09:23 +03:00
|
|
|
void CAdventureAI::actionStarted(const BattleAction &action)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
battleAI->actionStarted(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleNewRoundFirst(int round)
|
|
|
|
{
|
|
|
|
battleAI->battleNewRoundFirst(round);
|
|
|
|
}
|
|
|
|
|
2013-05-09 14:09:23 +03:00
|
|
|
void CAdventureAI::actionFinished(const BattleAction &action)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
battleAI->actionFinished(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
|
|
|
|
{
|
|
|
|
battleAI->battleStacksEffectsSet(sse);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
|
|
|
|
{
|
|
|
|
battleAI->battleStacksRemoved(bsr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
|
|
|
|
{
|
|
|
|
battleAI->battleObstaclesRemoved(removedObstacles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleNewStackAppeared(const CStack * stack)
|
|
|
|
{
|
|
|
|
battleAI->battleNewStackAppeared(stack);
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2011-08-01 20:36:18 +03:00
|
|
|
battleAI->battleStackMoved(stack, dest, distance);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleAttack(const BattleAttack *ba)
|
|
|
|
{
|
|
|
|
battleAI->battleAttack(ba);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
|
|
|
|
{
|
|
|
|
battleAI->battleSpellCast(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleEnd(const BattleResult *br)
|
|
|
|
{
|
|
|
|
battleAI->battleEnd(br);
|
2013-06-23 22:35:54 +03:00
|
|
|
battleAI.reset();
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
|
|
|
|
{
|
|
|
|
battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
|
2011-07-05 22:05:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction CAdventureAI::activeStack(const CStack * stack)
|
|
|
|
{
|
|
|
|
return battleAI->activeStack(stack);
|
2011-08-25 18:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::yourTacticPhase(int distance)
|
|
|
|
{
|
|
|
|
battleAI->yourTacticPhase(distance);
|
2011-12-14 00:23:17 +03:00
|
|
|
}
|
2013-05-09 14:09:23 +03:00
|
|
|
|
|
|
|
void CAdventureAI::saveGame(COSer<CSaveFile> &h, const int version) /*saving */
|
|
|
|
{
|
|
|
|
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
|
|
|
|
CGlobalAI::saveGame(h, version);
|
2013-06-23 15:36:18 +03:00
|
|
|
bool hasBattleAI = static_cast<bool>(battleAI);
|
2013-05-09 14:09:23 +03:00
|
|
|
h << hasBattleAI;
|
|
|
|
if(hasBattleAI)
|
|
|
|
{
|
|
|
|
h << std::string(battleAI->dllName);
|
|
|
|
battleAI->saveGame(h, version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::loadGame(CISer<CLoadFile> &h, const int version) /*loading */
|
|
|
|
{
|
|
|
|
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
|
|
|
|
CGlobalAI::loadGame(h, version);
|
|
|
|
bool hasBattleAI = false;
|
|
|
|
h >> hasBattleAI;
|
|
|
|
if(hasBattleAI)
|
|
|
|
{
|
|
|
|
std::string dllName;
|
|
|
|
h >> dllName;
|
|
|
|
battleAI = CDynLibHandler::getNewBattleAI(dllName);
|
|
|
|
assert(cbc); //it should have been set by the one who new'ed us
|
|
|
|
battleAI->init(cbc);
|
|
|
|
//battleAI->loadGame(h, version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleGameInterface::saveGame(COSer<CSaveFile> &h, const int version)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleGameInterface::loadGame(CISer<CLoadFile> &h, const int version)
|
|
|
|
{
|
|
|
|
}
|