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
|
|
|
|
2014-08-11 22:24:31 +03:00
|
|
|
#ifdef VCMI_WINDOWS
|
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-08-11 22:24:31 +03:00
|
|
|
#ifdef VCMI_ANDROID
|
2014-02-20 21:53:18 +03:00
|
|
|
// we can't use shared libraries on Android so here's a hack
|
|
|
|
extern "C" DLL_EXPORT void VCAI_GetAiName(char* name);
|
2015-12-29 04:43:33 +02:00
|
|
|
extern "C" DLL_EXPORT void VCAI_GetNewAI(std::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);
|
2015-12-29 04:43:33 +02:00
|
|
|
extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(std::shared_ptr<CGlobalAI> &out);
|
2014-02-20 21:53:18 +03:00
|
|
|
|
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
|
2015-12-29 04:43:33 +02:00
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(std::shared_ptr<CBattleGameInterface> &out);
|
2014-02-20 21:53:18 +03:00
|
|
|
#endif
|
2013-06-23 00:47:51 +03:00
|
|
|
|
2010-12-22 22:14:40 +02:00
|
|
|
template<typename rett>
|
2015-12-29 04:43:33 +02:00
|
|
|
std::shared_ptr<rett> createAny(const boost::filesystem::path& libpath, const std::string& methodName)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
typedef void(*TGetAIFun)(std::shared_ptr<rett>&);
|
2013-06-23 00:47:51 +03:00
|
|
|
typedef void(*TGetNameFun)(char*);
|
|
|
|
|
|
|
|
char temp[150];
|
|
|
|
|
|
|
|
TGetAIFun getAI = nullptr;
|
|
|
|
TGetNameFun getName = nullptr;
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
#ifdef VCMI_ANDROID
|
2014-02-20 21:53:18 +03:00
|
|
|
// this is awful but it seems using shared libraries on some devices is even worse
|
2014-09-01 00:02:52 +03:00
|
|
|
const std::string filename = libpath.filename().string();
|
2014-09-02 20:18:47 +03:00
|
|
|
if (filename == "libVCAI.so")
|
2014-09-01 00:02:52 +03:00
|
|
|
{
|
2014-02-20 21:53:18 +03:00
|
|
|
getName = (TGetNameFun)VCAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)VCAI_GetNewAI;
|
2014-09-01 00:02:52 +03:00
|
|
|
}
|
2014-09-02 20:18:47 +03:00
|
|
|
else if (filename == "libStupidAI.so")
|
2014-09-01 00:02:52 +03:00
|
|
|
{
|
2014-02-20 21:53:18 +03:00
|
|
|
getName = (TGetNameFun)StupidAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
|
2014-09-01 00:02:52 +03:00
|
|
|
}
|
2014-09-02 20:18:47 +03:00
|
|
|
else if (filename == "libBattleAI.so")
|
2014-09-01 00:02:52 +03:00
|
|
|
{
|
2014-02-20 21:53:18 +03:00
|
|
|
getName = (TGetNameFun)BattleAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
|
|
|
|
}
|
2014-09-01 00:02:52 +03:00
|
|
|
else
|
|
|
|
throw std::runtime_error("Don't know what to do with " + libpath.string() + " and method " + methodName);
|
|
|
|
#else // !VCMI_ANDROID
|
2014-08-21 23:26:28 +03:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
HMODULE dll = LoadLibraryW(libpath.c_str());
|
2011-06-28 17:19:16 +03:00
|
|
|
if (dll)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
|
|
|
|
getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
|
2007-10-21 19:45:13 +03:00
|
|
|
}
|
2014-09-01 00:02:52 +03:00
|
|
|
#else // !VCMI_WINDOWS
|
|
|
|
void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
|
2011-06-28 17:19:16 +03:00
|
|
|
if (dll)
|
|
|
|
{
|
2014-09-01 00:02:52 +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();
|
2014-09-01 00:02:52 +03:00
|
|
|
#endif // VCMI_WINDOWS
|
2009-04-13 21:52:20 +03:00
|
|
|
if (!dll)
|
|
|
|
{
|
2014-08-27 13:31:58 +03:00
|
|
|
logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). 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)
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
logGlobal->errorStream() << libpath << " does not export method " << methodName;
|
|
|
|
#ifdef VCMI_WINDOWS
|
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);
|
|
|
|
}
|
2014-08-21 23:26:28 +03:00
|
|
|
#endif // VCMI_ANDROID
|
2014-02-20 21:53:18 +03:00
|
|
|
|
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
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::shared_ptr<rett> ret;
|
2013-06-23 00:47:51 +03:00
|
|
|
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>
|
2015-12-29 04:43:33 +02:00
|
|
|
std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string& methodName)
|
2011-06-20 14:41:04 +03:00
|
|
|
{
|
2014-08-27 13:31:58 +03:00
|
|
|
logGlobal->infoStream() << "Opening " << dllname;
|
2014-08-11 22:24:31 +03:00
|
|
|
const boost::filesystem::path filePath =
|
2014-08-11 00:42:39 +03:00
|
|
|
VCMIDirs::get().libraryPath() / "AI" / VCMIDirs::get().libraryName(dllname);
|
2014-08-21 23:26:28 +03:00
|
|
|
auto ret = createAny<rett>(filePath, methodName);
|
|
|
|
ret->dllName = std::move(dllname);
|
2011-06-20 14:41:04 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::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
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::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
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::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
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CAdventureAI::saveGame(COSer & h, const int version) /*saving */
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CAdventureAI::loadGame(CISer & h, const int version) /*loading */
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CBattleGameInterface::saveGame(COSer & h, const int version)
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CBattleGameInterface::loadGame(CISer & h, const int version)
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
}
|