2017-07-13 10:26:03 +02: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
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "CStack.h"
|
2013-03-02 21:41:25 +03:00
|
|
|
#include "VCMIDirs.h"
|
2007-10-21 19:45:13 +03:00
|
|
|
|
2016-09-10 02:32:40 +02:00
|
|
|
#include "serializer/BinaryDeserializer.h"
|
|
|
|
#include "serializer/BinarySerializer.h"
|
2007-10-21 19:45:13 +03:00
|
|
|
|
2023-02-26 09:39:36 +02:00
|
|
|
#ifdef STATIC_AI
|
|
|
|
# include "AI/VCAI/VCAI.h"
|
|
|
|
# include "AI/Nullkiller/AIGateway.h"
|
|
|
|
# include "AI/BattleAI/BattleAI.h"
|
2023-02-28 12:08:58 +02:00
|
|
|
# include "AI/StupidAI/StupidAI.h"
|
|
|
|
# include "AI/EmptyAI/CEmptyAI.h"
|
2023-02-26 09:39:36 +02:00
|
|
|
#else
|
|
|
|
# ifdef VCMI_WINDOWS
|
|
|
|
# include <windows.h> //for .dll libs
|
|
|
|
# else
|
|
|
|
# include <dlfcn.h>
|
|
|
|
# endif // VCMI_WINDOWS
|
|
|
|
#endif // STATIC_AI
|
2017-05-25 19:57:20 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2010-12-22 22:14:40 +02:00
|
|
|
template<typename rett>
|
2017-05-25 19:57:20 +02:00
|
|
|
std::shared_ptr<rett> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2023-02-26 09:39:36 +02:00
|
|
|
#ifdef STATIC_AI
|
2017-05-25 19:57:20 +02:00
|
|
|
// android currently doesn't support loading libs dynamically, so the access to the known libraries
|
|
|
|
// is possible only via specializations of this template
|
|
|
|
throw std::runtime_error("Could not resolve ai library " + libpath.generic_string());
|
|
|
|
#else
|
2023-03-13 23:26:44 +02:00
|
|
|
using TGetAIFun = void (*)(std::shared_ptr<rett> &);
|
|
|
|
using TGetNameFun = void (*)(char *);
|
2013-06-23 00:47:51 +03:00
|
|
|
|
|
|
|
char temp[150];
|
|
|
|
|
|
|
|
TGetAIFun getAI = nullptr;
|
|
|
|
TGetNameFun getName = nullptr;
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
#ifdef VCMI_WINDOWS
|
2023-01-22 20:05:52 +02:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
#endif
|
2014-08-21 23:26:28 +03:00
|
|
|
HMODULE dll = LoadLibraryW(libpath.c_str());
|
2011-06-28 17:19:16 +03:00
|
|
|
if (dll)
|
2007-10-21 19:45:13 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
getName = reinterpret_cast<TGetNameFun>(GetProcAddress(dll, "GetAiName"));
|
|
|
|
getAI = reinterpret_cast<TGetAIFun>(GetProcAddress(dll, methodName.c_str()));
|
2007-10-21 19:45:13 +03:00
|
|
|
}
|
2023-01-22 20:05:52 +02:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
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)
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
getName = reinterpret_cast<TGetNameFun>(dlsym(dll, "GetAiName"));
|
|
|
|
getAI = reinterpret_cast<TGetAIFun>(dlsym(dll, methodName.c_str()));
|
2011-06-28 17:19:16 +03:00
|
|
|
}
|
2014-09-01 00:02:52 +03:00
|
|
|
#endif // VCMI_WINDOWS
|
2017-05-25 19:57:20 +02:00
|
|
|
|
2009-04-13 21:52:20 +03:00
|
|
|
if (!dll)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
|
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)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("%s does not export method %s", libpath.string(), methodName);
|
2014-08-21 23:26:28 +03:00
|
|
|
#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-02-20 21:53:18 +03:00
|
|
|
|
2009-01-31 04:36:44 +02:00
|
|
|
getName(temp);
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->info("Loaded %s", 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)
|
2016-08-12 11:10:27 +02:00
|
|
|
logGlobal->error("Cannot get AI!");
|
2009-03-28 20:46:20 +02:00
|
|
|
|
2007-10-21 19:45:13 +03:00
|
|
|
return ret;
|
2023-02-26 09:39:36 +02:00
|
|
|
#endif // STATIC_AI
|
2007-10-21 19:45:13 +03:00
|
|
|
}
|
2011-07-06 17:25:12 +03:00
|
|
|
|
2023-02-26 09:39:36 +02:00
|
|
|
#ifdef STATIC_AI
|
2017-05-25 19:57:20 +02:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
|
|
|
|
{
|
2022-10-02 13:23:03 +02:00
|
|
|
if(libpath.stem() == "libNullkiller") {
|
|
|
|
return std::make_shared<NKAI::AIGateway>();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return std::make_shared<VCAI>();
|
|
|
|
}
|
2017-05-25 19:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
|
|
|
|
{
|
2023-02-28 12:08:58 +02:00
|
|
|
if(libpath.stem() == "libBattleAI")
|
|
|
|
return std::make_shared<CBattleAI>();
|
|
|
|
else if(libpath.stem() == "libStupidAI")
|
|
|
|
return std::make_shared<CStupidAI>();
|
|
|
|
return std::make_shared<CEmptyAI>();
|
2017-05-25 19:57:20 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 09:39:36 +02:00
|
|
|
#endif // STATIC_AI
|
2017-05-25 19:57:20 +02:00
|
|
|
|
2011-06-20 14:41:04 +03:00
|
|
|
template<typename rett>
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<rett> createAnyAI(const std::string & dllname, const std::string & methodName)
|
2011-06-20 14:41:04 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->info("Opening %s", dllname);
|
2017-05-25 19:57:20 +02:00
|
|
|
|
|
|
|
const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
|
2014-08-21 23:26:28 +03:00
|
|
|
auto ret = createAny<rett>(filePath, methodName);
|
2023-03-13 23:26:44 +02:00
|
|
|
ret->dllName = dllname;
|
2011-06-20 14:41:04 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(const 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
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(const 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
|
|
|
|
2022-09-21 18:31:14 +02:00
|
|
|
#if SCRIPTING_ENABLED
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
|
2011-06-20 14:41:04 +03:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return createAny<scripting::Module>(dllname, "GetNewModule");
|
2011-06-20 14:41:04 +03:00
|
|
|
}
|
2022-09-21 18:31:14 +02:00
|
|
|
#endif
|
2011-06-20 14:41:04 +03:00
|
|
|
|
2017-05-25 19:57:20 +02:00
|
|
|
BattleAction CGlobalAI::activeStack(const CStack * stack)
|
2010-12-23 22:18:10 +02:00
|
|
|
{
|
2017-05-25 19:57:20 +02:00
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.actionType = EActionType::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);
|
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02:00
|
|
|
void CAdventureAI::battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile,
|
|
|
|
const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool side)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
assert(!battleAI);
|
2011-07-05 22:05:41 +03:00
|
|
|
assert(cbc);
|
2012-09-29 13:59:43 +03:00
|
|
|
battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
|
2022-12-07 21:50:45 +02:00
|
|
|
battleAI->initBattleInterface(env, cbc);
|
2011-07-05 09:14:07 +03:00
|
|
|
battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:10:35 +02:00
|
|
|
void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2022-12-09 13:10:35 +02:00
|
|
|
battleAI->battleStacksAttacked(bsa, ranged);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02: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);
|
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02: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);
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void CAdventureAI::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
battleAI->battleObstaclesChanged(obstacles);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2022-12-18 18:26:43 +02:00
|
|
|
void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2022-12-18 18:26:43 +02:00
|
|
|
battleAI->battleStackMoved(stack, dest, distance, teleport);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02:00
|
|
|
void CAdventureAI::battleAttack(const BattleAttack * ba)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
battleAI->battleAttack(ba);
|
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02:00
|
|
|
void CAdventureAI::battleSpellCast(const BattleSpellCast * sc)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
battleAI->battleSpellCast(sc);
|
|
|
|
}
|
|
|
|
|
2017-05-25 19:57:20 +02:00
|
|
|
void CAdventureAI::battleEnd(const BattleResult * br)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
battleAI->battleEnd(br);
|
2013-06-23 22:35:54 +03:00
|
|
|
battleAI.reset();
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2022-12-17 17:35:15 +02:00
|
|
|
void CAdventureAI::battleUnitsChanged(const std::vector<UnitChanges> & units)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2022-12-17 17:35:15 +02:00
|
|
|
battleAI->battleUnitsChanged(units);
|
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
|
|
|
|
2016-09-10 02:32:40 +02:00
|
|
|
void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
|
2013-06-23 15:36:18 +03:00
|
|
|
bool hasBattleAI = static_cast<bool>(battleAI);
|
2016-09-10 02:32:40 +02:00
|
|
|
h & hasBattleAI;
|
2013-05-09 14:09:23 +03:00
|
|
|
if(hasBattleAI)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
h & battleAI->dllName;
|
2013-05-09 14:09:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 02:32:40 +02:00
|
|
|
void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
|
|
|
|
bool hasBattleAI = false;
|
2016-09-10 02:32:40 +02:00
|
|
|
h & hasBattleAI;
|
2013-05-09 14:09:23 +03:00
|
|
|
if(hasBattleAI)
|
|
|
|
{
|
|
|
|
std::string dllName;
|
2016-09-10 02:32:40 +02:00
|
|
|
h & dllName;
|
2013-05-09 14:09:23 +03:00
|
|
|
battleAI = CDynLibHandler::getNewBattleAI(dllName);
|
|
|
|
assert(cbc); //it should have been set by the one who new'ed us
|
2022-12-07 21:50:45 +02:00
|
|
|
battleAI->initBattleInterface(env, cbc);
|
2013-05-09 14:09:23 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|