Browse Source

Move ObjectConfig to separate file

Tomasz Zieliński 1 year ago
parent
commit
e89649ec1c

+ 2 - 0
lib/CMakeLists.txt

@@ -182,6 +182,7 @@ set(lib_MAIN_SRCS
 	rmg/Zone.cpp
 	rmg/Functions.cpp
 	rmg/ObjectInfo.cpp
+	rmg/ObjectConfig.cpp
 	rmg/RmgMap.cpp
 	rmg/PenroseTiling.cpp
 	rmg/modificators/Modificator.cpp
@@ -585,6 +586,7 @@ set(lib_MAIN_HEADERS
 	rmg/float3.h
 	rmg/Functions.h
 	rmg/ObjectInfo.h
+	rmg/ObjectConfig.h
 	rmg/PenroseTiling.h
 	rmg/modificators/Modificator.h
 	rmg/modificators/ObjectManager.h

+ 0 - 3
lib/mapObjectConstructors/CObjectClassesHandler.cpp

@@ -415,9 +415,6 @@ CompoundMapObjectID CObjectClassesHandler::getCompoundIdentifier(const std::stri
 
 CompoundMapObjectID CObjectClassesHandler::getCompoundIdentifier(const std::string & objectName) const
 {
-	// TODO: Use existing utilities for parsing id:
-	// CIdentifierStorage::ObjectCallback::fromNameAndType
-
 	std::string subtype = "object"; //Default for objects with no subIds
 	std::string type;
 

+ 2 - 1
lib/rmg/CRmgTemplate.cpp

@@ -11,9 +11,9 @@
 #include "StdInc.h"
 #include <vstd/ContainerUtils.h>
 #include <boost/bimap.hpp>
-
 #include "CRmgTemplate.h"
 #include "Functions.h"
+
 #include "../TerrainHandler.h"
 #include "../VCMI_Lib.h"
 #include "../constants/StringConstants.h"
@@ -351,6 +351,7 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
 	SERIALIZE_ZONE_LINK(terrainTypeLikeZone);
 	SERIALIZE_ZONE_LINK(treasureLikeZone);
 	SERIALIZE_ZONE_LINK(customObjectsLikeZone);
+
 	#undef SERIALIZE_ZONE_LINK
 
 	if(terrainTypeLikeZone == NO_ZONE)

+ 1 - 0
lib/rmg/CRmgTemplate.h

@@ -14,6 +14,7 @@
 #include "../GameConstants.h"
 #include "../ResourceSet.h"
 #include "ObjectInfo.h"
+#include "ObjectConfig.h"
 #include "../mapObjectConstructors/CObjectClassesHandler.h"
 
 VCMI_LIB_NAMESPACE_BEGIN

+ 187 - 0
lib/rmg/ObjectConfig.cpp

@@ -0,0 +1,187 @@
+/*
+ * ObjectConfig.cpp, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+#include "StdInc.h"
+#include <boost/bimap.hpp>
+#include <boost/assign.hpp>
+#include "ObjectInfo.h"
+#include "ObjectConfig.h"
+
+#include "../VCMI_Lib.h"
+#include "../mapObjectConstructors/CObjectClassesHandler.h"
+#include "../mapObjectConstructors/AObjectTypeHandler.h"
+#include "../serializer/JsonSerializeFormat.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
+{
+	// FIXME: We do not need to store the object info, just the id
+
+	bannedObjects.push_back(objid);
+
+	logGlobal->info("Banned object of type %d.%d", objid.primaryID, objid.secondaryID);
+}
+
+void ObjectConfig::addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid)
+{
+	// FIXME: Need id / subId
+
+	// FIXME: Add templates and possibly other info
+	customObjects.push_back(object);
+	auto & lastObject = customObjects.back();
+	lastObject.setAllTemplates(objid.primaryID, objid.secondaryID);
+
+	assert(lastObject.templates.size() > 0);
+	auto temp = lastObject.templates.front();
+	logGlobal->info("Added custom object of type %d.%d", temp->id, temp->subid);
+}
+
+void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
+{
+	// TODO: We need serializer utility for list of enum values
+
+	static const boost::bimap<EObjectCategory, std::string> OBJECT_CATEGORY_STRINGS = boost::assign::list_of<boost::bimap<EObjectCategory, std::string>::relation>
+		(EObjectCategory::OTHER, "other")
+		(EObjectCategory::ALL, "all")
+		(EObjectCategory::NONE, "none")
+		(EObjectCategory::CREATURE_BANK, "creatureBank")
+		(EObjectCategory::BONUS, "bonus")
+		(EObjectCategory::DWELLING, "dwelling")
+		(EObjectCategory::RESOURCE, "resource")
+		(EObjectCategory::RESOURCE_GENERATOR, "resourceGenerator")
+		(EObjectCategory::SPELL_SCROLL, "spellScroll")
+		(EObjectCategory::RANDOM_ARTIFACT, "randomArtifact")
+		(EObjectCategory::PANDORAS_BOX, "pandorasBox")
+		(EObjectCategory::QUEST_ARTIFACT, "questArtifact")
+		(EObjectCategory::SEER_HUT, "seerHut");
+
+
+	// TODO: Separate into individual methods to enforce RAII destruction?
+	{
+		auto categories = handler.enterArray("bannedCategories");
+		if (handler.saving)
+		{
+			for (const auto& category : bannedObjectCategories)
+			{
+				auto str = OBJECT_CATEGORY_STRINGS.left.at(category);
+				categories.serializeString(categories.size(), str);
+			}
+		}
+		else
+		{
+			std::vector<std::string> categoryNames;
+			categories.serializeArray(categoryNames);
+
+			for (const auto & categoryName : categoryNames)
+			{
+				auto it = OBJECT_CATEGORY_STRINGS.right.find(categoryName);
+				if (it != OBJECT_CATEGORY_STRINGS.right.end())
+				{
+					bannedObjectCategories.push_back(it->second);
+				}
+			}
+		}
+	}
+
+	// FIXME: Doesn't seem to use this field at all
+	
+	{
+		auto bannedObjectData = handler.enterArray("bannedObjects");	
+		if (handler.saving)
+		{
+
+			// FIXME: Do we even need to serialize / store banned objects?
+			/*
+			for (const auto & object : bannedObjects)
+			{
+				// TODO: Translate id back to string?
+
+
+				JsonNode node;
+				node.String() = VLC->objtypeh->getHandlerFor(object.primaryID, object.secondaryID);
+				// TODO: Check if AI-generated code is right
+
+
+			}
+			// handler.serializeRaw("bannedObjects", node, std::nullopt);
+
+			*/
+		}
+		else
+		{
+			std::vector<std::string> objectNames;
+			bannedObjectData.serializeArray(objectNames);
+
+			for (const auto & objectName : objectNames)
+			{
+				VLC->objtypeh->resolveObjectCompoundId(objectName,
+					[this](CompoundMapObjectID objid)
+					{
+						addBannedObject(objid);
+					}
+				);
+				
+			}
+		}
+	}
+
+	auto commonObjectData = handler.getCurrent()["commonObjects"].Vector();	
+	if (handler.saving)
+	{
+
+		//TODO?
+	}
+	else
+	{
+		for (const auto & objectConfig : commonObjectData)
+		{
+			auto objectName = objectConfig["id"].String();
+			auto rmg = objectConfig["rmg"].Struct();
+
+			// TODO: Use common code with default rmg config
+			auto objectValue = rmg["value"].Integer();
+			auto objectProbability = rmg["rarity"].Integer();
+			auto objectMaxPerZone = rmg["zoneLimit"].Integer();
+
+			VLC->objtypeh->resolveObjectCompoundId(objectName,
+				[this, objectValue, objectProbability, objectMaxPerZone](CompoundMapObjectID objid)
+				{
+					ObjectInfo object;
+					
+					// TODO: Configure basic generateObject function
+
+					object.value = objectValue;
+					object.probability = objectProbability;
+					object.maxPerZone = objectMaxPerZone;
+					addCustomObject(object, objid);
+				}
+			);
+			
+		}
+	}
+}
+
+const std::vector<ObjectInfo> & ObjectConfig::getConfiguredObjects() const
+{
+	return customObjects;
+}
+
+const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
+{
+	return bannedObjects;
+}
+
+const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
+{
+	return bannedObjectCategories;
+}
+
+VCMI_LIB_NAMESPACE_END

+ 57 - 0
lib/rmg/ObjectConfig.h

@@ -0,0 +1,57 @@
+/*
+ * ObjectInfo.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+#pragma once
+
+#include "../constants/EntityIdentifiers.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+class DLL_LINKAGE ObjectConfig
+{
+public:
+
+	enum class EObjectCategory
+	{
+		OTHER = -2,
+		ALL = -1,
+		NONE = 0,
+		CREATURE_BANK = 1,
+		BONUS,
+		DWELLING,
+		RESOURCE,
+		RESOURCE_GENERATOR,
+		SPELL_SCROLL,
+		RANDOM_ARTIFACT,
+		PANDORAS_BOX,
+		QUEST_ARTIFACT,
+		SEER_HUT
+	};
+
+	void addBannedObject(const CompoundMapObjectID & objid);
+	void addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid);
+	void clearBannedObjects();
+	void clearCustomObjects();
+	const std::vector<CompoundMapObjectID> & getBannedObjects() const;
+	const std::vector<EObjectCategory> & getBannedObjectCategories() const;
+	const std::vector<ObjectInfo> & getConfiguredObjects() const;
+
+	void serializeJson(JsonSerializeFormat & handler);
+private:
+	// TODO: Add convenience method for banning objects by name
+	std::vector<CompoundMapObjectID> bannedObjects;
+	std::vector<EObjectCategory> bannedObjectCategories;
+
+	// TODO: In what format should I store custom objects?
+	// Need to convert map serialization format to ObjectInfo
+	std::vector<ObjectInfo> customObjects;
+};
+
+VCMI_LIB_NAMESPACE_END

+ 0 - 165
lib/rmg/ObjectInfo.cpp

@@ -9,8 +9,6 @@
  */
 
 #include "StdInc.h"
-#include <boost/bimap.hpp>
-#include <boost/assign.hpp>
 #include "ObjectInfo.h"
 
 #include "../VCMI_Lib.h"
@@ -68,167 +66,4 @@ void ObjectInfo::setTemplates(MapObjectID type, MapObjectSubID subtype, TerrainI
 	templates = templHandler->getTemplates(terrainType);
 }
 
-void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
-{
-	// FIXME: We do not need to store the object info, just the id
-
-	bannedObjects.push_back(objid);
-
-	logGlobal->info("Banned object of type %d.%d", objid.primaryID, objid.secondaryID);
-}
-
-void ObjectConfig::addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid)
-{
-	// FIXME: Need id / subId
-
-	// FIXME: Add templates and possibly other info
-	customObjects.push_back(object);
-	auto & lastObject = customObjects.back();
-	lastObject.setAllTemplates(objid.primaryID, objid.secondaryID);
-
-	assert(lastObject.templates.size() > 0);
-	auto temp = lastObject.templates.front();
-	logGlobal->info("Added custom object of type %d.%d", temp->id, temp->subid);
-}
-
-void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
-{
-	// TODO: We need serializer utility for list of enum values
-
-	static const boost::bimap<EObjectCategory, std::string> OBJECT_CATEGORY_STRINGS = boost::assign::list_of<boost::bimap<EObjectCategory, std::string>::relation>
-		(EObjectCategory::OTHER, "other")
-		(EObjectCategory::ALL, "all")
-		(EObjectCategory::NONE, "none")
-		(EObjectCategory::CREATURE_BANK, "creatureBank")
-		(EObjectCategory::BONUS, "bonus")
-		(EObjectCategory::DWELLING, "dwelling")
-		(EObjectCategory::RESOURCE, "resource")
-		(EObjectCategory::RESOURCE_GENERATOR, "resourceGenerator")
-		(EObjectCategory::SPELL_SCROLL, "spellScroll")
-		(EObjectCategory::RANDOM_ARTIFACT, "randomArtifact")
-		(EObjectCategory::PANDORAS_BOX, "pandorasBox")
-		(EObjectCategory::QUEST_ARTIFACT, "questArtifact")
-		(EObjectCategory::SEER_HUT, "seerHut");
-
-
-	// TODO: Separate into individual methods to enforce RAII destruction?
-	{
-		auto categories = handler.enterArray("bannedCategories");
-		if (handler.saving)
-		{
-			for (const auto& category : bannedObjectCategories)
-			{
-				auto str = OBJECT_CATEGORY_STRINGS.left.at(category);
-				categories.serializeString(categories.size(), str);
-			}
-		}
-		else
-		{
-			std::vector<std::string> categoryNames;
-			categories.serializeArray(categoryNames);
-
-			for (const auto & categoryName : categoryNames)
-			{
-				auto it = OBJECT_CATEGORY_STRINGS.right.find(categoryName);
-				if (it != OBJECT_CATEGORY_STRINGS.right.end())
-				{
-					bannedObjectCategories.push_back(it->second);
-				}
-			}
-		}
-	}
-
-	// FIXME: Doesn't seem to use this field at all
-	
-	{
-		auto bannedObjectData = handler.enterArray("bannedObjects");	
-		if (handler.saving)
-		{
-
-			// FIXME: Do we even need to serialize / store banned objects?
-			/*
-			for (const auto & object : bannedObjects)
-			{
-				// TODO: Translate id back to string?
-
-
-				JsonNode node;
-				node.String() = VLC->objtypeh->getHandlerFor(object.primaryID, object.secondaryID);
-				// TODO: Check if AI-generated code is right
-
-
-			}
-			// handler.serializeRaw("bannedObjects", node, std::nullopt);
-
-			*/
-		}
-		else
-		{
-			std::vector<std::string> objectNames;
-			bannedObjectData.serializeArray(objectNames);
-
-			for (const auto & objectName : objectNames)
-			{
-				VLC->objtypeh->resolveObjectCompoundId(objectName,
-					[this](CompoundMapObjectID objid)
-					{
-						addBannedObject(objid);
-					}
-				);
-				
-			}
-		}
-	}
-
-	auto commonObjectData = handler.getCurrent()["commonObjects"].Vector();	
-	if (handler.saving)
-	{
-
-		//TODO?
-	}
-	else
-	{
-		for (const auto & objectConfig : commonObjectData)
-		{
-			auto objectName = objectConfig["id"].String();
-			auto rmg = objectConfig["rmg"].Struct();
-
-			// TODO: Use common code with default rmg config
-			auto objectValue = rmg["value"].Integer();
-			auto objectProbability = rmg["rarity"].Integer();
-			auto objectMaxPerZone = rmg["zoneLimit"].Integer();
-
-			VLC->objtypeh->resolveObjectCompoundId(objectName,
-				[this, objectValue, objectProbability, objectMaxPerZone](CompoundMapObjectID objid)
-				{
-					ObjectInfo object;
-					
-					// TODO: Configure basic generateObject function
-
-					object.value = objectValue;
-					object.probability = objectProbability;
-					object.maxPerZone = objectMaxPerZone;
-					addCustomObject(object, objid);
-				}
-			);
-			
-		}
-	}
-}
-
-const std::vector<ObjectInfo> & ObjectConfig::getConfiguredObjects() const
-{
-	return customObjects;
-}
-
-const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
-{
-	return bannedObjects;
-}
-
-const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
-{
-	return bannedObjectCategories;
-}
-
 VCMI_LIB_NAMESPACE_END

+ 0 - 43
lib/rmg/ObjectInfo.h

@@ -38,47 +38,4 @@ struct DLL_LINKAGE ObjectInfo
 	//bool matchesId(const CompoundMapObjectID & id) const;
 };
 
-// TODO: Store config for custom objects to spawn in this zone
-// TODO: Read custom object config from zone file
-class DLL_LINKAGE ObjectConfig
-{
-public:
-
-	enum class EObjectCategory
-	{
-		OTHER = -2,
-		ALL = -1,
-		NONE = 0,
-		CREATURE_BANK = 1,
-		BONUS,
-		DWELLING,
-		RESOURCE,
-		RESOURCE_GENERATOR,
-		SPELL_SCROLL,
-		RANDOM_ARTIFACT,
-		PANDORAS_BOX,
-		QUEST_ARTIFACT,
-		SEER_HUT
-	};
-
-	void addBannedObject(const CompoundMapObjectID & objid);
-	void addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid);
-	void clearBannedObjects();
-	void clearCustomObjects();
-	const std::vector<CompoundMapObjectID> & getBannedObjects() const;
-	const std::vector<EObjectCategory> & getBannedObjectCategories() const;
-	const std::vector<ObjectInfo> & getConfiguredObjects() const;
-
-	void serializeJson(JsonSerializeFormat & handler);
-private:
-	// TODO: Add convenience method for banning objects by name
-	std::vector<CompoundMapObjectID> bannedObjects;
-	std::vector<EObjectCategory> bannedObjectCategories;
-
-	// TODO: In what format should I store custom objects?
-	// Need to convert map serialization format to ObjectInfo
-	std::vector<ObjectInfo> customObjects;
-};
-// TODO: Allow to copy all custom objects config from another zone
-
 VCMI_LIB_NAMESPACE_END