GameInstance.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * GameInstance.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "GameInstance.h"
  12. #include "CPlayerInterface.h"
  13. #include "CMT.h"
  14. #include "CServerHandler.h"
  15. #include "mapView/mapHandler.h"
  16. #include "globalLobby/GlobalLobbyClient.h"
  17. #include "mainmenu/CMainMenu.h"
  18. #include "windows/InfoWindows.h"
  19. #include "../lib/CConfigHandler.h"
  20. #include "../lib/GameLibrary.h"
  21. #include "../lib/callback/CCallback.h"
  22. #include "../lib/texts/CGeneralTextHandler.h"
  23. std::unique_ptr<GameInstance> GAME = nullptr;
  24. GameInstance::GameInstance()
  25. : serverInstance(std::make_unique<CServerHandler>())
  26. , interfaceInstance(nullptr)
  27. {
  28. }
  29. GameInstance::~GameInstance() = default;
  30. CServerHandler & GameInstance::server()
  31. {
  32. if (!serverInstance)
  33. throw std::runtime_error("Invalid access to GameInstance::server");
  34. return *serverInstance;
  35. }
  36. CMapHandler & GameInstance::map()
  37. {
  38. if (!mapInstance)
  39. throw std::runtime_error("Invalid access to GameInstance::map");
  40. return *mapInstance;
  41. }
  42. std::shared_ptr<CMainMenu> GameInstance::mainmenu()
  43. {
  44. if(settings["session"]["headless"].Bool())
  45. return nullptr;
  46. if (!mainMenuInstance)
  47. mainMenuInstance = std::make_shared<CMainMenu>();
  48. return mainMenuInstance;
  49. }
  50. CPlayerInterface * GameInstance::interface()
  51. {
  52. return interfaceInstance;
  53. }
  54. void GameInstance::setServerInstance(std::unique_ptr<CServerHandler> ptr)
  55. {
  56. serverInstance = std::move(ptr);
  57. }
  58. void GameInstance::setMapInstance(std::unique_ptr<CMapHandler> ptr)
  59. {
  60. mapInstance = std::move(ptr);
  61. }
  62. void GameInstance::setInterfaceInstance(CPlayerInterface * ptr)
  63. {
  64. interfaceInstance = ptr;
  65. }
  66. void GameInstance::onGlobalLobbyInterfaceActivated()
  67. {
  68. server().getGlobalLobby().activateInterface();
  69. }
  70. void GameInstance::onUpdate()
  71. {
  72. if (interfaceInstance)
  73. interfaceInstance->update();
  74. }
  75. bool GameInstance::capturedAllEvents()
  76. {
  77. if (interfaceInstance)
  78. return interfaceInstance->capturedAllEvents();
  79. else
  80. return false;
  81. }
  82. void GameInstance::onShutdownRequested(bool ask)
  83. {
  84. auto doQuit = [](){ throw GameShutdownException(); };
  85. if(!ask)
  86. doQuit();
  87. else
  88. {
  89. if (interface())
  90. interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[69], doQuit, nullptr);
  91. else
  92. CInfoWindow::showYesNoDialog(LIBRARY->generaltexth->allTexts[69], {}, doQuit, {}, PlayerColor(1));
  93. }
  94. }
  95. void GameInstance::onAppPaused()
  96. {
  97. pauseAutoSave();
  98. }
  99. void GameInstance::pauseAutoSave()
  100. {
  101. const std::string autoSaveName = "Saves/PauseAutosave";
  102. logGlobal->info("Received pause save game request");
  103. if(!GAME->interface() || !GAME->interface()->cb)
  104. {
  105. logGlobal->info("... but no active player interface found!");
  106. return;
  107. }
  108. if (!GAME->server().logicConnection)
  109. {
  110. logGlobal->info("... but no active connection found!");
  111. return;
  112. }
  113. if(!GAME->interface()->cb->getActiveBattles().empty())
  114. {
  115. logGlobal->info("... but player is in battle, skipping autosave!");
  116. return;
  117. }
  118. GAME->interface()->cb->save(autoSaveName);
  119. }