Browse Source

First version that works in lobby

Tomasz Zieliński 1 year ago
parent
commit
2c32c770f7

+ 6 - 0
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);

+ 52 - 1
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<CButton>("buttonOK");
 	buttonCancel = widget<CButton>("buttonCancel");
 }
+
+void RandomMapTab::saveOptions(const CMapGenOptions & options)
+{
+	JsonNode data;
+	JsonSerializer ser(nullptr, data);
+
+	ser.serializeStruct("lastSettings", const_cast<CMapGenOptions & >(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<ComboBox>("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();
+}

+ 6 - 2
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<CMapGenOptions> opts);
 	void setTemplate(const CRmgTemplate *);
+
+	void saveOptions(const CMapGenOptions & options) override;
+	void loadOptions() override;
 	CMapGenOptions & obtainMapGenOptions() {return *mapGenOptions;}
 
 	CFunctionList<void(std::shared_ptr<CMapInfo>, std::shared_ptr<CMapGenOptions>)> mapInfoChanged;
@@ -44,8 +48,8 @@ private:
 	void deactivateButtonsFrom(CToggleGroup & group, const std::set<int> & allowed);
 	std::vector<int> getPossibleMapSizes();
 
-	std::shared_ptr<CMapGenOptions> mapGenOptions;
 	std::shared_ptr<CMapInfo> mapInfo;
+	std::shared_ptr<CMapGenOptions> mapGenOptions;
 	
 	//options allowed - need to store as impact each other
 	std::set<int> playerCountAllowed;

+ 1 - 0
client/lobby/SelectionTab.cpp

@@ -167,6 +167,7 @@ SelectionTab::SelectionTab(ESelectionScreen Type)
 		inputName->filters += CTextInput::filenameFilter;
 		labelMapSizes = std::make_shared<CLabel>(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++)

+ 1 - 2
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<std::string, std::string> & help, const JsonNode & dropDownDescriptor, Point dropDownPosition, EShortcut key = {}, bool playerColoredButton = false);
@@ -67,6 +65,7 @@ public:
 	std::function<std::string(int, const void *)> getItemText;
 	
 	void setItem(int id);
+	void setItem(const void *);
 
 	void updateListItems();
 };

+ 1 - 0
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

+ 42 - 0
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

+ 2 - 0
lib/rmg/CMapGenOptions.h

@@ -210,6 +210,8 @@ public:
 
 		h & enabledRoads;
 	}
+
+	void serializeJson(JsonSerializeFormat & handler);
 };
 
 VCMI_LIB_NAMESPACE_END

+ 14 - 14
lib/rmg/CRmgTemplate.cpp

@@ -841,20 +841,20 @@ void CRmgTemplate::serializeSize(JsonSerializeFormat & handler, int3 & value, co
 {
 	static const std::map<std::string, int3> 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<int3, std::string> sizeReverseMapping = vstd::invertMap(sizeMapping);

+ 11 - 0
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();

+ 12 - 5
mapeditor/windownewmap.h

@@ -12,13 +12,14 @@
 
 #include <QDialog>
 #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<int, std::pair<int, int>> 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;