1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Added GameInstance class

- available as global GAME
- integrates LOCPLINT (CPlayerInterface)
- integrates CGI->mh (CMapHandler)
- integrates CSH (CServerHandler)
This commit is contained in:
Ivan Savenko
2025-02-11 15:23:33 +00:00
parent ffd37a8fa8
commit 156de5b17e
98 changed files with 1288 additions and 1131 deletions

View File

@@ -19,6 +19,7 @@
#include "../CServerHandler.h"
#include "../GameEngine.h"
#include "../GameInstance.h"
#include "../gui/Shortcut.h"
#include "../widgets/Buttons.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
@@ -45,14 +46,14 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
auto initLobby = [&]()
{
tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, nullptr);
buttonSelect = std::make_shared<CButton>(Point(411, 80), AnimationPath::builtin("GSPBUTT.DEF"), VLC->generaltexth->zelp[45], 0, EShortcut::LOBBY_SELECT_SCENARIO);
buttonSelect->addCallback([=]()
{
toggleTab(tabSel);
if (getMapInfo() && getMapInfo()->isRandomMap)
CSH->setMapInfo(tabSel->getSelectedMapInfo());
GAME->server().setMapInfo(tabSel->getSelectedMapInfo());
});
buttonOptions = std::make_shared<CButton>(Point(411, 510), AnimationPath::builtin("GSPBUTT.DEF"), VLC->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabOpt), EShortcut::LOBBY_ADDITIONAL_OPTIONS);
@@ -74,7 +75,7 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
tabTurnOptions = std::make_shared<TurnOptionsTab>();
tabExtraOptions = std::make_shared<ExtraOptionsTab>();
tabRand = std::make_shared<RandomMapTab>();
tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, CSH, _1, _2);
tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, _2);
buttonRMG = std::make_shared<CButton>(Point(411, 105), AnimationPath::builtin("GSPBUTT.DEF"), VLC->generaltexth->zelp[47], 0, EShortcut::LOBBY_RANDOM_MAP);
buttonRMG->addCallback([this]()
{
@@ -83,7 +84,7 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
tabRand->updateMapInfoByHost();
});
card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, CSH, _1));
card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, &GAME->server(), _1));
buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRBEG.DEF"), VLC->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, false), EShortcut::LOBBY_BEGIN_STANDARD_GAME);
initLobby();
@@ -99,7 +100,7 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
break;
}
case ESelectionScreen::campaignList:
tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, CSH, _1, nullptr);
tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, nullptr);
buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_CAMPAIGN);
break;
}
@@ -108,12 +109,12 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), VLC->generaltexth->zelp[105], [&]()
{
bool wasInLobbyRoom = CSH->inLobbyRoom();
CSH->sendClientDisconnecting();
bool wasInLobbyRoom = GAME->server().inLobbyRoom();
GAME->server().sendClientDisconnecting();
close();
if (wasInLobbyRoom)
CSH->getGlobalLobby().activateInterface();
GAME->server().getGlobalLobby().activateInterface();
}, EShortcut::GLOBAL_CANCEL);
if(hideScreen) // workaround to avoid confusing players by custom campaign list displaying for a few ms -> instead of this draw a black screen while "loading"
@@ -126,35 +127,35 @@ CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
CLobbyScreen::~CLobbyScreen()
{
// TODO: For now we always destroy whole lobby when leaving bonus selection screen
if(CSH->getState() == EClientState::LOBBY_CAMPAIGN)
CSH->sendClientDisconnecting();
if(GAME->server().getState() == EClientState::LOBBY_CAMPAIGN)
GAME->server().sendClientDisconnecting();
}
void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
{
if(tab == curTab)
CSH->sendGuiAction(LobbyGuiAction::NO_TAB);
GAME->server().sendGuiAction(LobbyGuiAction::NO_TAB);
else if(tab == tabOpt)
CSH->sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
GAME->server().sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
else if(tab == tabSel)
CSH->sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
GAME->server().sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
else if(tab == tabRand)
CSH->sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
GAME->server().sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
else if(tab == tabTurnOptions)
CSH->sendGuiAction(LobbyGuiAction::OPEN_TURN_OPTIONS);
GAME->server().sendGuiAction(LobbyGuiAction::OPEN_TURN_OPTIONS);
else if(tab == tabExtraOptions)
CSH->sendGuiAction(LobbyGuiAction::OPEN_EXTRA_OPTIONS);
GAME->server().sendGuiAction(LobbyGuiAction::OPEN_EXTRA_OPTIONS);
CSelectionBase::toggleTab(tab);
}
void CLobbyScreen::startCampaign()
{
if(!CSH->mi)
if(!GAME->server().mi)
return;
try {
auto ourCampaign = CampaignHandler::getCampaign(CSH->mi->fileURI);
CSH->setCampaignState(ourCampaign);
auto ourCampaign = CampaignHandler::getCampaign(GAME->server().mi->fileURI);
GAME->server().setCampaignState(ourCampaign);
}
catch (const std::runtime_error &e)
{
@@ -170,19 +171,19 @@ void CLobbyScreen::startCampaign()
void CLobbyScreen::startScenario(bool allowOnlyAI)
{
if (tabRand && CSH->si->mapGenOptions)
if (tabRand && GAME->server().si->mapGenOptions)
{
// Save RMG settings at game start
tabRand->saveOptions(*CSH->si->mapGenOptions);
tabRand->saveOptions(*GAME->server().si->mapGenOptions);
}
// Save chosen difficulty
Settings lastDifficulty = settings.write["general"]["lastDifficulty"];
lastDifficulty->Integer() = getCurrentDifficulty();
if (CSH->validateGameStart(allowOnlyAI))
if (GAME->server().validateGameStart(allowOnlyAI))
{
CSH->sendStartGame(allowOnlyAI);
GAME->server().sendStartGame(allowOnlyAI);
buttonStart->block(true);
}
}
@@ -218,7 +219,7 @@ void CLobbyScreen::toggleMode(bool host)
if (buttonExtraOptions)
buttonExtraOptions->block(!host);
if(CSH->mi)
if(GAME->server().mi)
{
tabOpt->recreate();
tabTurnOptions->recreate();
@@ -237,17 +238,17 @@ void CLobbyScreen::toggleChat()
void CLobbyScreen::updateAfterStateChange()
{
if(CSH->isHost() && screenType == ESelectionScreen::newGame)
if(GAME->server().isHost() && screenType == ESelectionScreen::newGame)
{
bool isMultiplayer = CSH->loadMode == ELoadMode::MULTI;
bool isMultiplayer = GAME->server().loadMode == ELoadMode::MULTI;
ExtraOptionsInfo info = SEL->getStartInfo()->extraOptionsInfo;
info.cheatsAllowed = isMultiplayer ? persistentStorage["startExtraOptions"]["multiPlayer"]["cheatsAllowed"].Bool() : !persistentStorage["startExtraOptions"]["singlePlayer"]["cheatsNotAllowed"].Bool();
info.unlimitedReplay = persistentStorage["startExtraOptions"][isMultiplayer ? "multiPlayer" : "singlePlayer"]["unlimitedReplay"].Bool();
if(info.cheatsAllowed != CSH->si->extraOptionsInfo.cheatsAllowed || info.unlimitedReplay != CSH->si->extraOptionsInfo.unlimitedReplay)
CSH->setExtraOptionsInfo(info);
if(info.cheatsAllowed != GAME->server().si->extraOptionsInfo.cheatsAllowed || info.unlimitedReplay != GAME->server().si->extraOptionsInfo.unlimitedReplay)
GAME->server().setExtraOptionsInfo(info);
}
if(CSH->mi)
if(GAME->server().mi)
{
if (tabOpt)
tabOpt->recreate();
@@ -257,7 +258,7 @@ void CLobbyScreen::updateAfterStateChange()
tabExtraOptions->recreate();
}
buttonStart->block(CSH->mi == nullptr || CSH->isGuest());
buttonStart->block(GAME->server().mi == nullptr || GAME->server().isGuest());
card->changeSelection();
if (card->iconDifficulty)
@@ -265,24 +266,24 @@ void CLobbyScreen::updateAfterStateChange()
if (screenType == ESelectionScreen::loadGame)
{
// When loading the game, only one button in the difficulty toggle group should be enabled, so here disable all other buttons first, then make selection
card->iconDifficulty->setSelectedOnly(CSH->si->difficulty);
card->iconDifficulty->setSelectedOnly(GAME->server().si->difficulty);
}
else
{
card->iconDifficulty->setSelected(CSH->si->difficulty);
card->iconDifficulty->setSelected(GAME->server().si->difficulty);
}
}
if(curTab && curTab == tabRand && CSH->si->mapGenOptions)
tabRand->setMapGenOptions(CSH->si->mapGenOptions);
if(curTab && curTab == tabRand && GAME->server().si->mapGenOptions)
tabRand->setMapGenOptions(GAME->server().si->mapGenOptions);
}
const StartInfo * CLobbyScreen::getStartInfo()
{
return CSH->si.get();
return GAME->server().si.get();
}
const CMapInfo * CLobbyScreen::getMapInfo()
{
return CSH->mi.get();
return GAME->server().mi.get();
}