From 2c32c770f7b7fce90fd339c203b67877678b1fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Thu, 29 Feb 2024 12:45:08 +0100 Subject: [PATCH] First version that works in lobby --- client/lobby/CLobbyScreen.cpp | 6 ++++ client/lobby/RandomMapTab.cpp | 53 ++++++++++++++++++++++++++++++++++- client/lobby/RandomMapTab.h | 8 ++++-- client/lobby/SelectionTab.cpp | 1 + client/widgets/ComboBox.h | 3 +- lib/CMakeLists.txt | 1 + lib/rmg/CMapGenOptions.cpp | 42 +++++++++++++++++++++++++++ lib/rmg/CMapGenOptions.h | 2 ++ lib/rmg/CRmgTemplate.cpp | 28 +++++++++--------- mapeditor/windownewmap.cpp | 11 ++++++++ mapeditor/windownewmap.h | 17 +++++++---- 11 files changed, 148 insertions(+), 24 deletions(-) diff --git a/client/lobby/CLobbyScreen.cpp b/client/lobby/CLobbyScreen.cpp index 63cda2fd1..f08aacdfe 100644 --- a/client/lobby/CLobbyScreen.cpp +++ b/client/lobby/CLobbyScreen.cpp @@ -156,6 +156,12 @@ void CLobbyScreen::startCampaign() void CLobbyScreen::startScenario(bool allowOnlyAI) { + if (tabRand && CSH->si->mapGenOptions) + { + // Save RMG settings at game start + tabRand->saveOptions(*CSH->si->mapGenOptions); + } + if (CSH->validateGameStart(allowOnlyAI)) { CSH->sendStartGame(allowOnlyAI); diff --git a/client/lobby/RandomMapTab.cpp b/client/lobby/RandomMapTab.cpp index 2d389095a..b6a7afb3c 100644 --- a/client/lobby/RandomMapTab.cpp +++ b/client/lobby/RandomMapTab.cpp @@ -38,6 +38,11 @@ #include "../../lib/filesystem/Filesystem.h" #include "../../lib/RoadHandler.h" +//#include "../../lib/GameSettings.h" +#include "../../lib/CConfigHandler.h" +#include "../../lib/serializer/JsonSerializer.h" +#include "../../lib/serializer/JsonDeserializer.h" + RandomMapTab::RandomMapTab(): InterfaceObjectConfigurable() { @@ -162,7 +167,8 @@ RandomMapTab::RandomMapTab(): }; } - updateMapInfoByHost(); + loadOptions(); + //updateMapInfoByHost(); } void RandomMapTab::updateMapInfoByHost() @@ -569,3 +575,48 @@ TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab): buttonOk = widget("buttonOK"); buttonCancel = widget("buttonCancel"); } + +void RandomMapTab::saveOptions(const CMapGenOptions & options) +{ + JsonNode data; + JsonSerializer ser(nullptr, data); + + ser.serializeStruct("lastSettings", const_cast(options)); + + // FIXME: Do not nest fields + Settings rmgSettings = persistentStorage.write["rmg"]; + rmgSettings["rmg"] = data; +} + +void RandomMapTab::loadOptions() +{ + // FIXME: Potential leak? + auto options = new CMapGenOptions(); + + + auto rmgSettings = persistentStorage["rmg"]["rmg"]; + if (!rmgSettings.Struct().empty()) + { + JsonDeserializer handler(nullptr, rmgSettings); + handler.serializeStruct("lastSettings", *options); + + // FIXME: Regenerate players, who are not saved + mapGenOptions.reset(options); + // Will check template and set other options as well + setTemplate(mapGenOptions->getMapTemplate()); + if(auto w = widget("templateList")) + { + // FIXME: Private function, need id + w->setItem(mapGenOptions->getMapTemplate()); + logGlobal->warn("Set RMG template on drop-down list"); + } + + // TODO: Else? Set default + logGlobal->warn("Loaded previous RMG settings"); + } + else + { + logGlobal->warn("Did not load previous RMG settings"); + } + updateMapInfoByHost(); +} \ No newline at end of file diff --git a/client/lobby/RandomMapTab.h b/client/lobby/RandomMapTab.h index 2ad559e80..a18f8acfc 100644 --- a/client/lobby/RandomMapTab.h +++ b/client/lobby/RandomMapTab.h @@ -15,6 +15,7 @@ #include "../../lib/GameConstants.h" #include "../../lib/rmg/CRmgTemplate.h" #include "../gui/InterfaceObjectConfigurable.h" +#include "../lib/rmg/MapGenOptionsSaver.h" VCMI_LIB_NAMESPACE_BEGIN @@ -28,7 +29,7 @@ class CLabelGroup; class CSlider; class CPicture; -class RandomMapTab : public InterfaceObjectConfigurable +class RandomMapTab : public InterfaceObjectConfigurable, public MapGenOptionsSaver { public: RandomMapTab(); @@ -36,6 +37,9 @@ public: void updateMapInfoByHost(); void setMapGenOptions(std::shared_ptr opts); void setTemplate(const CRmgTemplate *); + + void saveOptions(const CMapGenOptions & options) override; + void loadOptions() override; CMapGenOptions & obtainMapGenOptions() {return *mapGenOptions;} CFunctionList, std::shared_ptr)> mapInfoChanged; @@ -44,8 +48,8 @@ private: void deactivateButtonsFrom(CToggleGroup & group, const std::set & allowed); std::vector getPossibleMapSizes(); - std::shared_ptr mapGenOptions; std::shared_ptr mapInfo; + std::shared_ptr mapGenOptions; //options allowed - need to store as impact each other std::set playerCountAllowed; diff --git a/client/lobby/SelectionTab.cpp b/client/lobby/SelectionTab.cpp index 85e014c88..55858e72c 100644 --- a/client/lobby/SelectionTab.cpp +++ b/client/lobby/SelectionTab.cpp @@ -167,6 +167,7 @@ SelectionTab::SelectionTab(ESelectionScreen Type) inputName->filters += CTextInput::filenameFilter; labelMapSizes = std::make_shared(87, 62, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[510]); + // TODO: Global constants? int sizes[] = {36, 72, 108, 144, 0}; const char * filterIconNmes[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"}; for(int i = 0; i < 5; i++) diff --git a/client/widgets/ComboBox.h b/client/widgets/ComboBox.h index cd3c1e883..670734c10 100644 --- a/client/widgets/ComboBox.h +++ b/client/widgets/ComboBox.h @@ -51,8 +51,6 @@ class ComboBox : public CButton }; friend class DropDown; - - void setItem(const void *); public: ComboBox(Point position, const AnimationPath & defName, const std::pair & help, const JsonNode & dropDownDescriptor, Point dropDownPosition, EShortcut key = {}, bool playerColoredButton = false); @@ -67,6 +65,7 @@ public: std::function getItemText; void setItem(int id); + void setItem(const void *); void updateListItems(); }; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 566c2fdac..3740de13b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -534,6 +534,7 @@ set(lib_HEADERS rmg/RmgPath.h rmg/CMapGenerator.h rmg/CMapGenOptions.h + rmg/MapGenOptionsSaver.h rmg/CRmgTemplate.h rmg/CRmgTemplateStorage.h rmg/CZonePlacer.h diff --git a/lib/rmg/CMapGenOptions.cpp b/lib/rmg/CMapGenOptions.cpp index fe233bf99..26aef7842 100644 --- a/lib/rmg/CMapGenOptions.cpp +++ b/lib/rmg/CMapGenOptions.cpp @@ -17,6 +17,7 @@ #include "CRandomGenerator.h" #include "../VCMI_Lib.h" #include "../CTownHandler.h" +#include "serializer/JsonSerializeFormat.h" VCMI_LIB_NAMESPACE_BEGIN @@ -816,4 +817,45 @@ void CMapGenOptions::CPlayerSettings::setTeam(const TeamID & value) team = value; } +void CMapGenOptions::serializeJson(JsonSerializeFormat & handler) +{ + handler.serializeInt("width", width); + handler.serializeInt("height", height); + handler.serializeBool("haswoLevels", hasTwoLevels); + handler.serializeInt("humanOrCpuPlayerCount", humanOrCpuPlayerCount); + handler.serializeInt("teamCount", teamCount); + handler.serializeInt("compOnlyPlayerCount", compOnlyPlayerCount); + handler.serializeInt("compOnlyTeamCount", compOnlyTeamCount); + handler.serializeInt("waterContent", waterContent); + handler.serializeInt("monsterStrength", monsterStrength); + + std::string templateName; + if(mapTemplate && handler.saving) + { + templateName = mapTemplate->getId(); + } + handler.serializeString("templateName", templateName); + if(!handler.saving) + { + // FIXME: doesn't load correctly? Name is "Jebus Cross" + setMapTemplate(templateName); + if (mapTemplate) + { + logGlobal->warn("Loaded previous RMG template"); + // FIXME: Update dropdown menu + } + else + { + logGlobal->warn("Failed to deserialize previous map template"); + } + } + + handler.serializeIdArray("roads", enabledRoads); + //TODO: Serialize CMapGenOptions::CPlayerSettings ? This won't b saved between sessions + if (!handler.saving) + { + resetPlayersMap(); + } +} + VCMI_LIB_NAMESPACE_END diff --git a/lib/rmg/CMapGenOptions.h b/lib/rmg/CMapGenOptions.h index 47e33c797..2055d58e5 100644 --- a/lib/rmg/CMapGenOptions.h +++ b/lib/rmg/CMapGenOptions.h @@ -210,6 +210,8 @@ public: h & enabledRoads; } + + void serializeJson(JsonSerializeFormat & handler); }; VCMI_LIB_NAMESPACE_END diff --git a/lib/rmg/CRmgTemplate.cpp b/lib/rmg/CRmgTemplate.cpp index 75467694f..ac7a491e3 100644 --- a/lib/rmg/CRmgTemplate.cpp +++ b/lib/rmg/CRmgTemplate.cpp @@ -841,20 +841,20 @@ void CRmgTemplate::serializeSize(JsonSerializeFormat & handler, int3 & value, co { static const std::map sizeMapping = { - {"s", { 36, 36, 1}}, - {"s+u", { 36, 36, 2}}, - {"m", { 72, 72, 1}}, - {"m+u", { 72, 72, 2}}, - {"l", {108, 108, 1}}, - {"l+u", {108, 108, 2}}, - {"xl", {144, 144, 1}}, - {"xl+u", {144, 144, 2}}, - {"h", {180, 180, 1}}, - {"h+u", {180, 180, 2}}, - {"xh", {216, 216, 1}}, - {"xh+u", {216, 216, 2}}, - {"g", {252, 252, 1}}, - {"g+u", {252, 252, 2}} + {"s", {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_SMALL, 1}}, + {"s+u", {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_SMALL, 2}}, + {"m", {CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_MIDDLE, 1}}, + {"m+u", {CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_MIDDLE, 2}}, + {"l", {CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_LARGE, 1}}, + {"l+u", {CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_LARGE, 2}}, + {"xl", {CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_XLARGE, 1}} , + {"xl+u", {CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_XLARGE, 2}} , + {"h", {CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_HUGE, 1}}, + {"h+u", {CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_HUGE, 2}}, + {"xh", {CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_XHUGE, 1}}, + {"xh+u", {CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_XHUGE, 2}}, + {"g", {CMapHeader::MAP_SIZE_GIANT, CMapHeader::MAP_SIZE_GIANT, 1}}, + {"g+u", {CMapHeader::MAP_SIZE_GIANT, CMapHeader::MAP_SIZE_GIANT, 2}} }; static const std::map sizeReverseMapping = vstd::invertMap(sizeMapping); diff --git a/mapeditor/windownewmap.cpp b/mapeditor/windownewmap.cpp index cc673b4bd..e3ff7d798 100644 --- a/mapeditor/windownewmap.cpp +++ b/mapeditor/windownewmap.cpp @@ -219,6 +219,17 @@ void WindowNewMap::saveUserSettings() } } +void WindowNewMap::saveOptions(const CMapGenOptions & options) +{ + // TODO +} + +void WindowNewMap::loadOptions() +{ + mapGenOptions = CMapGenOptions(); + // TODO +} + void WindowNewMap::on_cancelButton_clicked() { close(); diff --git a/mapeditor/windownewmap.h b/mapeditor/windownewmap.h index 76bd5dc22..5622ef781 100644 --- a/mapeditor/windownewmap.h +++ b/mapeditor/windownewmap.h @@ -12,13 +12,14 @@ #include #include "../lib/rmg/CMapGenOptions.h" +#include "../lib/rmg/MapGenOptionsSaver.h" namespace Ui { class WindowNewMap; } -class WindowNewMap : public QDialog +class WindowNewMap : public QDialog, public MapGenOptionsSaver { Q_OBJECT @@ -64,10 +65,13 @@ class WindowNewMap : public QDialog const std::map> mapSizes { - {0, {36, 36}}, - {1, {72, 72}}, - {2, {108, 108}}, - {3, {144, 144}}, + {0, {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_SMALL}}, + {1, {CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_MIDDLE}}, + {2, {CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_LARGE}}, + {3, {CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_XLARGE}}, + {4, {CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_HUGE}}, + {5, {CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_XHUGE}}, + {6, {CMapHeader::MAP_SIZE_GIANT, CMapHeader::MAP_SIZE_GIANT}}, }; public: @@ -108,6 +112,9 @@ private: void loadUserSettings(); void saveUserSettings(); + void saveOptions(const CMapGenOptions & options) override; + void loadOptions() override; + private: Ui::WindowNewMap *ui;