From 0eba0ee6865f63962e6830fe6c29329c42e6faca Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sat, 21 Oct 2023 02:12:34 +0200 Subject: [PATCH] show random maps --- client/lobby/CSelectionBase.cpp | 6 ++++++ client/lobby/RandomMapTab.cpp | 12 ++++++++++++ client/lobby/SelectionTab.cpp | 10 ++++++++-- client/lobby/SelectionTab.h | 1 + lib/gameState/CGameState.cpp | 7 +++++-- lib/gameState/CGameState.h | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/client/lobby/CSelectionBase.cpp b/client/lobby/CSelectionBase.cpp index 57d3ced51..357bff3a8 100644 --- a/client/lobby/CSelectionBase.cpp +++ b/client/lobby/CSelectionBase.cpp @@ -111,6 +111,12 @@ void CSelectionBase::toggleTab(std::shared_ptr tab) { curTab.reset(); } + + if(tabSel->showRandom) + tabSel->curFolder = ""; + tabSel->showRandom = false; + tabSel->filter(0, true); + GH.windows().totalRedraw(); } diff --git a/client/lobby/RandomMapTab.cpp b/client/lobby/RandomMapTab.cpp index b5bdd9a61..65169dff7 100644 --- a/client/lobby/RandomMapTab.cpp +++ b/client/lobby/RandomMapTab.cpp @@ -11,6 +11,8 @@ #include "RandomMapTab.h" #include "CSelectionBase.h" +#include "CLobbyScreen.h" +#include "SelectionTab.h" #include "../CGameInfo.h" #include "../CServerHandler.h" @@ -121,6 +123,16 @@ RandomMapTab::RandomMapTab(): const JsonNode config(JsonPath::builtin("config/widgets/randomMapTab.json")); build(config); + if(auto w = widget("buttonShowRandomMaps")) + { + w->addCallback([&]() + { + (static_cast(parent))->toggleTab((static_cast(parent))->tabSel); + (static_cast(parent))->tabSel->showRandom = true; + (static_cast(parent))->tabSel->filter(0, true); + }); + } + //set combo box callbacks if(auto w = widget("templateList")) { diff --git a/client/lobby/SelectionTab.cpp b/client/lobby/SelectionTab.cpp index 6faa7fc2c..5e5e7fa68 100644 --- a/client/lobby/SelectionTab.cpp +++ b/client/lobby/SelectionTab.cpp @@ -147,7 +147,7 @@ static ESortBy getSortBySelectionScreen(ESelectionScreen Type) } SelectionTab::SelectionTab(ESelectionScreen Type) - : CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}, curFolder(""), currentMapSizeFilter(0) + : CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}, curFolder(""), currentMapSizeFilter(0), showRandom(false) { OBJ_CONSTRUCTION; @@ -427,9 +427,15 @@ void SelectionTab::filter(int size, bool selectFirst) { if((elem->mapHeader && (!size || elem->mapHeader->width == size)) || tabType == ESelectionScreen::campaignList) { + if(showRandom) + curFolder = "RANDOMMAPS/"; + auto [folderName, baseFolder, parentExists, fileInFolder] = checkSubfolder(elem->originalFileURI); - if(parentExists) + if((showRandom && baseFolder != "RANDOMMAPS") || (!showRandom && baseFolder == "RANDOMMAPS")) + continue; + + if(parentExists && !showRandom) { auto folder = std::make_shared(); folder->isFolder = true; diff --git a/client/lobby/SelectionTab.h b/client/lobby/SelectionTab.h index 50f501769..b457bcef8 100644 --- a/client/lobby/SelectionTab.h +++ b/client/lobby/SelectionTab.h @@ -78,6 +78,7 @@ public: ESortBy generalSortingBy; bool sortModeAscending; int currentMapSizeFilter = 0; + bool showRandom; std::shared_ptr inputName; diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index ffdd990df..183c6e4ec 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -566,17 +566,20 @@ void CGameState::initNewGame(const IMapService * mapService, bool allowSavingRan { try { - auto path = VCMIDirs::get().userCachePath() / "RandomMaps"; + auto path = VCMIDirs::get().userDataPath() / "Maps" / "RandomMaps"; boost::filesystem::create_directories(path); std::shared_ptr options = scenarioOps->mapGenOptions; const std::string templateName = options->getMapTemplate()->getName(); const ui32 seed = scenarioOps->seedToBeUsed; + const std::string dt = vstd::getDateTimeISO8601Basic(std::time(0)); - const std::string fileName = boost::str(boost::format("%s_%d.vmap") % templateName % seed ); + const std::string fileName = boost::str(boost::format("%s_%s_%d.vmap") % dt % templateName % seed ); const auto fullPath = path / fileName; + randomMap->name.appendRawString(boost::str(boost::format(" %s") % dt)); + mapService->saveMap(randomMap, fullPath); logGlobal->info("Random map has been saved to:"); diff --git a/lib/gameState/CGameState.h b/lib/gameState/CGameState.h index 8ba75e2f9..08b6863d3 100644 --- a/lib/gameState/CGameState.h +++ b/lib/gameState/CGameState.h @@ -99,7 +99,7 @@ public: void preInit(Services * services); - void init(const IMapService * mapService, StartInfo * si, Load::ProgressAccumulator &, bool allowSavingRandomMap = false); + void init(const IMapService * mapService, StartInfo * si, Load::ProgressAccumulator &, bool allowSavingRandomMap = true); void updateOnLoad(StartInfo * si); ConstTransitivePtr scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)