123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- /*
- * Client.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
- *
- */
- #include "StdInc.h"
- #include "Global.h"
- #include "Client.h"
- #include "CPlayerInterface.h"
- #include "PlayerLocalState.h"
- #include "CServerHandler.h"
- #include "ClientNetPackVisitors.h"
- #include "adventureMap/AdventureMapInterface.h"
- #include "battle/BattleInterface.h"
- #include "GameEngine.h"
- #include "GameInstance.h"
- #include "gui/WindowHandler.h"
- #include "mapView/mapHandler.h"
- #include "../CCallback.h"
- #include "../lib/CConfigHandler.h"
- #include "../lib/gameState/CGameState.h"
- #include "../lib/CPlayerState.h"
- #include "../lib/CThreadHelper.h"
- #include "../lib/VCMIDirs.h"
- #include "../lib/UnlockGuard.h"
- #include "../lib/battle/BattleInfo.h"
- #include "../lib/serializer/Connection.h"
- #include "../lib/mapping/CMapService.h"
- #include "../lib/pathfinder/CGPathNode.h"
- #include "../lib/filesystem/Filesystem.h"
- #include <memory>
- #include <vcmi/events/EventBus.h>
- #if SCRIPTING_ENABLED
- #include "../lib/ScriptHandler.h"
- #endif
- #ifdef VCMI_ANDROID
- #include "lib/CAndroidVMHelper.h"
- #endif
- ThreadSafeVector<int> CClient::waitingRequest;
- CPlayerEnvironment::CPlayerEnvironment(PlayerColor player_, CClient * cl_, std::shared_ptr<CCallback> mainCallback_)
- : player(player_),
- cl(cl_),
- mainCallback(mainCallback_)
- {
- }
- const Services * CPlayerEnvironment::services() const
- {
- return LIBRARY;
- }
- vstd::CLoggerBase * CPlayerEnvironment::logger() const
- {
- return logGlobal;
- }
- events::EventBus * CPlayerEnvironment::eventBus() const
- {
- return cl->eventBus();//always get actual value
- }
- const CPlayerEnvironment::BattleCb * CPlayerEnvironment::battle(const BattleID & battleID) const
- {
- return mainCallback->getBattle(battleID).get();
- }
- const CPlayerEnvironment::GameCb * CPlayerEnvironment::game() const
- {
- return mainCallback.get();
- }
- CClient::CClient()
- {
- waitingRequest.clear();
- gs = nullptr;
- }
- CClient::~CClient() = default;
- const Services * CClient::services() const
- {
- return LIBRARY; //todo: this should be LIBRARY
- }
- const CClient::BattleCb * CClient::battle(const BattleID & battleID) const
- {
- return nullptr; //todo?
- }
- const CClient::GameCb * CClient::game() const
- {
- return this;
- }
- vstd::CLoggerBase * CClient::logger() const
- {
- return logGlobal;
- }
- events::EventBus * CClient::eventBus() const
- {
- return clientEventBus.get();
- }
- void CClient::newGame(CGameState * initializedGameState)
- {
- GAME->server().th->update();
- CMapService mapService;
- assert(initializedGameState);
- gs = initializedGameState;
- gs->preInit(LIBRARY, this);
- logNetwork->trace("\tCreating gamestate: %i", GAME->server().th->getDiff());
- if(!initializedGameState)
- {
- Load::ProgressAccumulator progressTracking;
- gs->init(&mapService, GAME->server().si.get(), progressTracking, settings["general"]["saveRandomMaps"].Bool());
- }
- logNetwork->trace("Initializing GameState (together): %d ms", GAME->server().th->getDiff());
- initMapHandler();
- reinitScripting();
- initPlayerEnvironments();
- initPlayerInterfaces();
- }
- void CClient::loadGame(CGameState * initializedGameState)
- {
- logNetwork->info("Loading procedure started!");
- logNetwork->info("Game state was transferred over network, loading.");
- gs = initializedGameState;
- gs->preInit(LIBRARY, this);
- gs->updateOnLoad(GAME->server().si.get());
- logNetwork->info("Game loaded, initialize interfaces.");
- initMapHandler();
- reinitScripting();
- initPlayerEnvironments();
- initPlayerInterfaces();
- }
- void CClient::save(const std::string & fname)
- {
- if(!gs->currentBattles.empty())
- {
- logNetwork->error("Game cannot be saved during battle!");
- return;
- }
- SaveGame save_game(fname);
- sendRequest(save_game, PlayerColor::NEUTRAL);
- }
- void CClient::endNetwork()
- {
- GAME->map().endNetwork();
- if (CPlayerInterface::battleInt)
- CPlayerInterface::battleInt->endNetwork();
- for(auto & i : playerint)
- {
- auto interface = std::dynamic_pointer_cast<CPlayerInterface>(i.second);
- if (interface)
- interface->endNetwork();
- }
- }
- void CClient::endGame()
- {
- #if SCRIPTING_ENABLED
- clientScripts.reset();
- #endif
- //suggest interfaces to finish their stuff (AI should interrupt any bg working threads)
- for(auto & i : playerint)
- i.second->finish();
- ENGINE->curInt = nullptr;
- {
- logNetwork->info("Ending current game!");
- removeGUI();
- GAME->setMapInstance(nullptr);
- vstd::clear_pointer(gs);
- logNetwork->info("Deleted mapHandler and gameState.");
- }
- CPlayerInterface::battleInt.reset();
- playerint.clear();
- battleints.clear();
- battleCallbacks.clear();
- playerEnvironments.clear();
- logNetwork->info("Deleted playerInts.");
- logNetwork->info("Client stopped.");
- }
- void CClient::initMapHandler()
- {
- // TODO: CMapHandler initialization can probably go somewhere else
- // It's can't be before initialization of interfaces
- // During loading CPlayerInterface from serialized state it's depend on MH
- if(!settings["session"]["headless"].Bool())
- {
- GAME->setMapInstance(std::make_unique<CMapHandler>(gs->map));
- logNetwork->trace("Creating mapHandler: %d ms", GAME->server().th->getDiff());
- }
- }
- void CClient::initPlayerEnvironments()
- {
- playerEnvironments.clear();
- auto allPlayers = GAME->server().getAllClientPlayers(GAME->server().logicConnection->connectionID);
- bool hasHumanPlayer = false;
- for(auto & color : allPlayers)
- {
- logNetwork->info("Preparing environment for player %s", color.toString());
- playerEnvironments[color] = std::make_shared<CPlayerEnvironment>(color, this, std::make_shared<CCallback>(gs, color, this));
-
- if(!hasHumanPlayer && gs->players[color].isHuman())
- hasHumanPlayer = true;
- }
- if(!hasHumanPlayer && !settings["session"]["headless"].Bool())
- {
- Settings session = settings.write["session"];
- session["spectate"].Bool() = true;
- session["spectate-skip-battle-result"].Bool() = true;
- session["spectate-ignore-hero"].Bool() = true;
- }
-
- if(settings["session"]["spectate"].Bool())
- {
- playerEnvironments[PlayerColor::SPECTATOR] = std::make_shared<CPlayerEnvironment>(PlayerColor::SPECTATOR, this, std::make_shared<CCallback>(gs, std::nullopt, this));
- }
- }
- void CClient::initPlayerInterfaces()
- {
- for(auto & playerInfo : gs->scenarioOps->playerInfos)
- {
- PlayerColor color = playerInfo.first;
- if(!vstd::contains(GAME->server().getAllClientPlayers(GAME->server().logicConnection->connectionID), color))
- continue;
- if(!vstd::contains(playerint, color))
- {
- logNetwork->info("Preparing interface for player %s", color.toString());
- if(playerInfo.second.isControlledByAI() || settings["session"]["onlyai"].Bool())
- {
- bool alliedToHuman = false;
- for(auto & allyInfo : gs->scenarioOps->playerInfos)
- if (gs->getPlayerTeam(allyInfo.first) == gs->getPlayerTeam(playerInfo.first) && allyInfo.second.isControlledByHuman())
- alliedToHuman = true;
- auto AiToGive = aiNameForPlayer(playerInfo.second, false, alliedToHuman);
- logNetwork->info("Player %s will be lead by %s", color.toString(), AiToGive);
- installNewPlayerInterface(CDynLibHandler::getNewAI(AiToGive), color);
- }
- else
- {
- logNetwork->info("Player %s will be lead by human", color.toString());
- installNewPlayerInterface(std::make_shared<CPlayerInterface>(color), color);
- }
- }
- }
- if(settings["session"]["spectate"].Bool())
- {
- installNewPlayerInterface(std::make_shared<CPlayerInterface>(PlayerColor::SPECTATOR), PlayerColor::SPECTATOR, true);
- }
- if(GAME->server().getAllClientPlayers(GAME->server().logicConnection->connectionID).count(PlayerColor::NEUTRAL))
- installNewBattleInterface(CDynLibHandler::getNewBattleAI(settings["server"]["neutralAI"].String()), PlayerColor::NEUTRAL);
- logNetwork->trace("Initialized player interfaces %d ms", GAME->server().th->getDiff());
- }
- std::string CClient::aiNameForPlayer(const PlayerSettings & ps, bool battleAI, bool alliedToHuman) const
- {
- if(ps.name.size())
- {
- const boost::filesystem::path aiPath = VCMIDirs::get().fullLibraryPath("AI", ps.name);
- if(boost::filesystem::exists(aiPath))
- return ps.name;
- }
- return aiNameForPlayer(battleAI, alliedToHuman);
- }
- std::string CClient::aiNameForPlayer(bool battleAI, bool alliedToHuman) const
- {
- const int sensibleAILimit = settings["session"]["oneGoodAI"].Bool() ? 1 : PlayerColor::PLAYER_LIMIT_I;
- std::string goodAdventureAI = alliedToHuman ? settings["server"]["alliedAI"].String() : settings["server"]["playerAI"].String();
- std::string goodBattleAI = settings["server"]["neutralAI"].String();
- std::string goodAI = battleAI ? goodBattleAI : goodAdventureAI;
- std::string badAI = battleAI ? "StupidAI" : "EmptyAI";
- //TODO what about human players
- if(battleints.size() >= sensibleAILimit)
- return badAI;
- return goodAI;
- }
- void CClient::installNewPlayerInterface(std::shared_ptr<CGameInterface> gameInterface, PlayerColor color, bool battlecb)
- {
- playerint[color] = gameInterface;
- logGlobal->trace("\tInitializing the interface for player %s", color.toString());
- auto cb = std::make_shared<CCallback>(gs, color, this);
- battleCallbacks[color] = cb;
- gameInterface->initGameInterface(playerEnvironments.at(color), cb);
- installNewBattleInterface(gameInterface, color, battlecb);
- }
- void CClient::installNewBattleInterface(std::shared_ptr<CBattleGameInterface> battleInterface, PlayerColor color, bool needCallback)
- {
- battleints[color] = battleInterface;
- if(needCallback)
- {
- logGlobal->trace("\tInitializing the battle interface for player %s", color.toString());
- auto cbc = std::make_shared<CBattleCallback>(color, this);
- battleCallbacks[color] = cbc;
- battleInterface->initBattleInterface(playerEnvironments.at(color), cbc);
- }
- }
- void CClient::handlePack(CPackForClient & pack)
- {
- ApplyClientNetPackVisitor afterVisitor(*this, *gameState());
- ApplyFirstClientNetPackVisitor beforeVisitor(*this, *gameState());
- pack.visit(beforeVisitor);
- logNetwork->trace("\tMade first apply on cl: %s", typeid(pack).name());
- {
- boost::unique_lock lock(CGameState::mutex);
- gs->apply(pack);
- }
- logNetwork->trace("\tApplied on gs: %s", typeid(pack).name());
- pack.visit(afterVisitor);
- logNetwork->trace("\tMade second apply on cl: %s", typeid(pack).name());
- }
- int CClient::sendRequest(const CPackForServer & request, PlayerColor player)
- {
- static ui32 requestCounter = 1;
- ui32 requestID = requestCounter++;
- logNetwork->trace("Sending a request \"%s\". It'll have an ID=%d.", typeid(request).name(), requestID);
- waitingRequest.pushBack(requestID);
- request.requestID = requestID;
- request.player = player;
- GAME->server().logicConnection->sendPack(request);
- if(vstd::contains(playerint, player))
- playerint[player]->requestSent(&request, requestID);
- return requestID;
- }
- void CClient::battleStarted(const BattleInfo * info)
- {
- std::shared_ptr<CPlayerInterface> att;
- std::shared_ptr<CPlayerInterface> def;
- const auto & leftSide = info->getSide(BattleSide::LEFT_SIDE);
- const auto & rightSide = info->getSide(BattleSide::RIGHT_SIDE);
- for(auto & battleCb : battleCallbacks)
- {
- if(!battleCb.first.isValidPlayer() || battleCb.first == leftSide.color || battleCb.first == rightSide.color)
- battleCb.second->onBattleStarted(info);
- }
- //If quick combat is not, do not prepare interfaces for battleint
- auto callBattleStart = [&](PlayerColor color, BattleSide side)
- {
- if(vstd::contains(battleints, color))
- battleints[color]->battleStart(info->battleID, leftSide.armyObject, rightSide.armyObject, info->tile, leftSide.hero, rightSide.hero, side, info->replayAllowed);
- };
-
- callBattleStart(leftSide.color, BattleSide::LEFT_SIDE);
- callBattleStart(rightSide.color, BattleSide::RIGHT_SIDE);
- callBattleStart(PlayerColor::UNFLAGGABLE, BattleSide::RIGHT_SIDE);
- if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool())
- callBattleStart(PlayerColor::SPECTATOR, BattleSide::RIGHT_SIDE);
-
- if(vstd::contains(playerint, leftSide.color) && playerint[leftSide.color]->human)
- att = std::dynamic_pointer_cast<CPlayerInterface>(playerint[leftSide.color]);
- if(vstd::contains(playerint, rightSide.color) && playerint[rightSide.color]->human)
- def = std::dynamic_pointer_cast<CPlayerInterface>(playerint[rightSide.color]);
-
- //Remove player interfaces for auto battle (quickCombat option)
- if((att && att->isAutoFightOn) || (def && def->isAutoFightOn))
- {
- auto endTacticPhaseIfEligible = [info](const CPlayerInterface * interface)
- {
- if (interface->cb->getBattle(info->battleID)->battleGetTacticDist())
- {
- auto side = interface->cb->getBattle(info->battleID)->playerToSide(interface->playerID);
- if(interface->playerID == info->getSide(info->tacticsSide).color)
- {
- auto action = BattleAction::makeEndOFTacticPhase(side);
- interface->cb->battleMakeTacticAction(info->battleID, action);
- }
- }
- };
- if(att && att->isAutoFightOn)
- endTacticPhaseIfEligible(att.get());
- else // def && def->isAutoFightOn
- endTacticPhaseIfEligible(def.get());
- att.reset();
- def.reset();
- }
- if(!settings["session"]["headless"].Bool())
- {
- if(att || def)
- {
- CPlayerInterface::battleInt = std::make_shared<BattleInterface>(info->getBattleID(), leftSide.armyObject, rightSide.armyObject, leftSide.hero, rightSide.hero, att, def);
- }
- else if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool())
- {
- //TODO: This certainly need improvement
- auto spectratorInt = std::dynamic_pointer_cast<CPlayerInterface>(playerint[PlayerColor::SPECTATOR]);
- spectratorInt->cb->onBattleStarted(info);
- CPlayerInterface::battleInt = std::make_shared<BattleInterface>(info->getBattleID(), leftSide.armyObject, rightSide.armyObject, leftSide.hero, rightSide.hero, att, def, spectratorInt);
- }
- }
- if(info->tacticDistance)
- {
- auto tacticianColor = info->getSide(info->tacticsSide).color;
- if (vstd::contains(battleints, tacticianColor))
- battleints[tacticianColor]->yourTacticPhase(info->battleID, info->tacticDistance);
- }
- }
- void CClient::battleFinished(const BattleID & battleID)
- {
- for(auto side : { BattleSide::ATTACKER, BattleSide::DEFENDER })
- {
- if(battleCallbacks.count(gs->getBattle(battleID)->getSide(side).color))
- battleCallbacks[gs->getBattle(battleID)->getSide(side).color]->onBattleEnded(battleID);
- }
- if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool())
- battleCallbacks[PlayerColor::SPECTATOR]->onBattleEnded(battleID);
- }
- void CClient::startPlayerBattleAction(const BattleID & battleID, PlayerColor color)
- {
- if (battleints.count(color) == 0)
- return; // not our combat in MP
- auto battleint = battleints.at(color);
- if (!battleint->human)
- {
- // we want to avoid locking gamestate and causing UI to freeze while AI is making turn
- auto unlockInterface = vstd::makeUnlockGuard(ENGINE->interfaceMutex);
- battleint->activeStack(battleID, gs->getBattle(battleID)->battleGetStackByID(gs->getBattle(battleID)->activeStack, false));
- }
- else
- {
- battleint->activeStack(battleID, gs->getBattle(battleID)->battleGetStackByID(gs->getBattle(battleID)->activeStack, false));
- }
- }
- vstd::RNG & CClient::getRandomGenerator()
- {
- // Client should use CRandomGenerator::getDefault() for UI logic
- // Gamestate should never call this method on client!
- throw std::runtime_error("Illegal access to random number generator from client code!");
- }
- #if SCRIPTING_ENABLED
- scripting::Pool * CClient::getGlobalContextPool() const
- {
- return clientScripts.get();
- }
- #endif
- void CClient::reinitScripting()
- {
- clientEventBus = std::make_unique<events::EventBus>();
- #if SCRIPTING_ENABLED
- clientScripts.reset(new scripting::PoolImpl(this));
- #endif
- }
- void CClient::removeGUI() const
- {
- // CClient::endGame
- ENGINE->curInt = nullptr;
- ENGINE->windows().clear();
- adventureInt.reset();
- logGlobal->info("Removed GUI.");
- GAME->setInterfaceInstance(nullptr);
- }
- #ifdef VCMI_ANDROID
- extern "C" JNIEXPORT jboolean JNICALL Java_eu_vcmi_vcmi_NativeMethods_tryToSaveTheGame(JNIEnv * env, jclass cls)
- {
- boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
- logGlobal->info("Received emergency save game request");
- if(!GAME->interface() || !GAME->interface()->cb)
- {
- logGlobal->info("... but no active player interface found!");
- return false;
- }
- if (!GAME->server().logicConnection)
- {
- logGlobal->info("... but no active connection found!");
- return false;
- }
- GAME->interface()->cb->save("Saves/_Android_Autosave");
- return true;
- }
- #endif
|