2011-12-13 21:23:17 +00:00
|
|
|
#include "StdInc.h"
|
2007-07-28 09:44:10 +00:00
|
|
|
#include "CGameInterface.h"
|
2011-12-13 21:23:17 +00:00
|
|
|
|
2011-07-05 06:14:07 +00:00
|
|
|
#include "BattleState.h"
|
2013-03-02 18:41:25 +00:00
|
|
|
#include "VCMIDirs.h"
|
2007-10-21 16:45:13 +00:00
|
|
|
|
2014-08-11 21:24:31 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
2007-10-21 16:45:13 +00:00
|
|
|
#include <windows.h> //for .dll libs
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2013-05-09 11:09:23 +00:00
|
|
|
#include "Connection.h"
|
2007-10-21 16:45:13 +00:00
|
|
|
|
2009-04-15 14:03:31 +00: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 21:24:31 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2014-02-20 22:53:18 +04: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 05:43:33 +03:00
|
|
|
extern "C" DLL_EXPORT void VCAI_GetNewAI(std::shared_ptr<CGlobalAI> &out);
|
2013-06-22 21:47:51 +00:00
|
|
|
|
2014-02-20 22:53:18 +04:00
|
|
|
extern "C" DLL_EXPORT void StupidAI_GetAiName(char* name);
|
2015-12-29 05:43:33 +03:00
|
|
|
extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(std::shared_ptr<CGlobalAI> &out);
|
2014-02-20 22:53:18 +04:00
|
|
|
|
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
|
2015-12-29 05:43:33 +03:00
|
|
|
extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(std::shared_ptr<CBattleGameInterface> &out);
|
2014-02-20 22:53:18 +04:00
|
|
|
#endif
|
2013-06-22 21:47:51 +00:00
|
|
|
|
2010-12-22 20:14:40 +00:00
|
|
|
template<typename rett>
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<rett> createAny(const boost::filesystem::path& libpath, const std::string& methodName)
|
2007-10-21 16:45:13 +00:00
|
|
|
{
|
2016-08-12 12:10:27 +03:00
|
|
|
typedef void(*TGetAIFun)(std::shared_ptr<rett>&);
|
|
|
|
typedef void(*TGetNameFun)(char*);
|
2013-06-22 21:47:51 +00:00
|
|
|
|
|
|
|
char temp[150];
|
|
|
|
|
|
|
|
TGetAIFun getAI = nullptr;
|
|
|
|
TGetNameFun getName = nullptr;
|
|
|
|
|
2014-08-21 22:26:28 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2014-02-20 22:53:18 +04:00
|
|
|
// this is awful but it seems using shared libraries on some devices is even worse
|
2014-08-31 23:02:52 +02:00
|
|
|
const std::string filename = libpath.filename().string();
|
2014-09-02 19:18:47 +02:00
|
|
|
if (filename == "libVCAI.so")
|
2014-08-31 23:02:52 +02:00
|
|
|
{
|
2014-02-20 22:53:18 +04:00
|
|
|
getName = (TGetNameFun)VCAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)VCAI_GetNewAI;
|
2014-08-31 23:02:52 +02:00
|
|
|
}
|
2014-09-02 19:18:47 +02:00
|
|
|
else if (filename == "libStupidAI.so")
|
2014-08-31 23:02:52 +02:00
|
|
|
{
|
2014-02-20 22:53:18 +04:00
|
|
|
getName = (TGetNameFun)StupidAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
|
2014-08-31 23:02:52 +02:00
|
|
|
}
|
2014-09-02 19:18:47 +02:00
|
|
|
else if (filename == "libBattleAI.so")
|
2014-08-31 23:02:52 +02:00
|
|
|
{
|
2014-02-20 22:53:18 +04:00
|
|
|
getName = (TGetNameFun)BattleAI_GetAiName;
|
|
|
|
getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
|
|
|
|
}
|
2014-08-31 23:02:52 +02:00
|
|
|
else
|
|
|
|
throw std::runtime_error("Don't know what to do with " + libpath.string() + " and method " + methodName);
|
|
|
|
#else // !VCMI_ANDROID
|
2014-08-21 22:26:28 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
HMODULE dll = LoadLibraryW(libpath.c_str());
|
2011-06-28 14:19:16 +00:00
|
|
|
if (dll)
|
2007-10-21 16:45:13 +00:00
|
|
|
{
|
2014-08-21 22:26:28 +02:00
|
|
|
getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
|
|
|
|
getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
|
2007-10-21 16:45:13 +00:00
|
|
|
}
|
2014-08-31 23:02:52 +02:00
|
|
|
#else // !VCMI_WINDOWS
|
|
|
|
void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
|
2011-06-28 14:19:16 +00:00
|
|
|
if (dll)
|
|
|
|
{
|
2014-08-31 23:02:52 +02:00
|
|
|
getName = (TGetNameFun)dlsym(dll, "GetAiName");
|
|
|
|
getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
|
2011-06-28 14:19:16 +00:00
|
|
|
}
|
2012-03-12 20:11:46 +00:00
|
|
|
else
|
2016-03-12 04:41:27 +03:00
|
|
|
logGlobal->errorStream() << "Error: " << dlerror();
|
2014-08-31 23:02:52 +02:00
|
|
|
#endif // VCMI_WINDOWS
|
2009-04-13 18:52:20 +00:00
|
|
|
if (!dll)
|
|
|
|
{
|
2014-08-27 12:31:58 +02:00
|
|
|
logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). Throwing...";
|
2012-11-20 17:53:45 +00:00
|
|
|
throw std::runtime_error("Cannot open dynamic library");
|
2009-04-13 18:52:20 +00:00
|
|
|
}
|
2013-01-20 12:06:49 +00:00
|
|
|
else if(!getName || !getAI)
|
|
|
|
{
|
2014-08-21 22:26:28 +02:00
|
|
|
logGlobal->errorStream() << libpath << " does not export method " << methodName;
|
|
|
|
#ifdef VCMI_WINDOWS
|
2013-01-20 12:06:49 +00:00
|
|
|
FreeLibrary(dll);
|
2013-01-20 14:43:58 +00:00
|
|
|
#else
|
|
|
|
dlclose(dll);
|
|
|
|
#endif
|
2013-01-20 12:06:49 +00:00
|
|
|
throw std::runtime_error("Cannot find method " + methodName);
|
|
|
|
}
|
2014-08-21 22:26:28 +02:00
|
|
|
#endif // VCMI_ANDROID
|
2014-02-20 22:53:18 +04:00
|
|
|
|
2009-01-31 02:36:44 +00:00
|
|
|
getName(temp);
|
2016-03-12 04:41:27 +03:00
|
|
|
logGlobal->infoStream() << "Loaded " << temp;
|
2009-03-28 18:46:20 +00:00
|
|
|
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<rett> ret;
|
2013-06-22 21:47:51 +00:00
|
|
|
getAI(ret);
|
2009-03-28 18:46:20 +00:00
|
|
|
if(!ret)
|
2016-08-12 12:10:27 +03:00
|
|
|
logGlobal->error("Cannot get AI!");
|
2009-03-28 18:46:20 +00:00
|
|
|
|
2007-10-21 16:45:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-07-06 14:25:12 +00:00
|
|
|
|
2011-06-20 11:41:04 +00:00
|
|
|
template<typename rett>
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string& methodName)
|
2011-06-20 11:41:04 +00:00
|
|
|
{
|
2014-08-27 12:31:58 +02:00
|
|
|
logGlobal->infoStream() << "Opening " << dllname;
|
2014-08-11 21:24:31 +02:00
|
|
|
const boost::filesystem::path filePath =
|
2014-08-10 23:42:39 +02:00
|
|
|
VCMIDirs::get().libraryPath() / "AI" / VCMIDirs::get().libraryName(dllname);
|
2014-08-21 22:26:28 +02:00
|
|
|
auto ret = createAny<rett>(filePath, methodName);
|
|
|
|
ret->dllName = std::move(dllname);
|
2011-06-20 11:41:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
|
2010-12-22 20:14:40 +00:00
|
|
|
{
|
2011-02-23 03:57:45 +00:00
|
|
|
return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
|
2010-12-22 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
|
2010-12-22 20:14:40 +00:00
|
|
|
{
|
2011-02-23 03:57:45 +00:00
|
|
|
return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
|
2010-12-22 20:14:40 +00:00
|
|
|
}
|
2010-12-23 20:18:10 +00:00
|
|
|
|
2015-12-29 05:43:33 +03:00
|
|
|
std::shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
|
2011-06-20 11:41:04 +00:00
|
|
|
{
|
|
|
|
return createAny<CScriptingModule>(dllname, "GetNewModule");
|
|
|
|
}
|
|
|
|
|
2010-12-23 20:18:10 +00:00
|
|
|
BattleAction CGlobalAI::activeStack( const CStack * stack )
|
|
|
|
{
|
2013-02-03 21:05:44 +00:00
|
|
|
BattleAction ba; ba.actionType = Battle::DEFEND;
|
2010-12-23 20:18:10 +00:00
|
|
|
ba.stackNumber = stack->ID;
|
|
|
|
return ba;
|
|
|
|
}
|
2011-07-05 06:14:07 +00: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 19:05:41 +00:00
|
|
|
assert(cbc);
|
2012-09-29 10:59:43 +00:00
|
|
|
battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
|
2011-07-05 19:05:41 +00:00
|
|
|
battleAI->init(cbc);
|
2011-07-05 06:14:07 +00:00
|
|
|
battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
|
|
|
|
{
|
|
|
|
battleAI->battleStacksAttacked(bsa);
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:09:23 +00:00
|
|
|
void CAdventureAI::actionStarted(const BattleAction &action)
|
2011-07-05 06:14:07 +00:00
|
|
|
{
|
|
|
|
battleAI->actionStarted(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::battleNewRoundFirst(int round)
|
|
|
|
{
|
|
|
|
battleAI->battleNewRoundFirst(round);
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:09:23 +00:00
|
|
|
void CAdventureAI::actionFinished(const BattleAction &action)
|
2011-07-05 06:14:07 +00: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 13:05:19 +00:00
|
|
|
void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
|
2011-07-05 06:14:07 +00:00
|
|
|
{
|
2011-08-01 17:36:18 +00:00
|
|
|
battleAI->battleStackMoved(stack, dest, distance);
|
2011-07-05 06:14:07 +00: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 19:35:54 +00:00
|
|
|
battleAI.reset();
|
2011-07-05 06:14:07 +00: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 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction CAdventureAI::activeStack(const CStack * stack)
|
|
|
|
{
|
|
|
|
return battleAI->activeStack(stack);
|
2011-08-25 15:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAdventureAI::yourTacticPhase(int distance)
|
|
|
|
{
|
|
|
|
battleAI->yourTacticPhase(distance);
|
2011-12-13 21:23:17 +00:00
|
|
|
}
|
2013-05-09 11:09:23 +00:00
|
|
|
|
2014-12-21 17:27:50 +03:00
|
|
|
void CAdventureAI::saveGame(COSer & h, const int version) /*saving */
|
2013-05-09 11:09:23 +00:00
|
|
|
{
|
|
|
|
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
|
|
|
|
CGlobalAI::saveGame(h, version);
|
2013-06-23 12:36:18 +00:00
|
|
|
bool hasBattleAI = static_cast<bool>(battleAI);
|
2013-05-09 11:09:23 +00:00
|
|
|
h << hasBattleAI;
|
|
|
|
if(hasBattleAI)
|
|
|
|
{
|
|
|
|
h << std::string(battleAI->dllName);
|
|
|
|
battleAI->saveGame(h, version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:27:50 +03:00
|
|
|
void CAdventureAI::loadGame(CISer & h, const int version) /*loading */
|
2013-05-09 11:09:23 +00: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 17:27:50 +03:00
|
|
|
void CBattleGameInterface::saveGame(COSer & h, const int version)
|
2013-05-09 11:09:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:27:50 +03:00
|
|
|
void CBattleGameInterface::loadGame(CISer & h, const int version)
|
2013-05-09 11:09:23 +00:00
|
|
|
{
|
|
|
|
}
|