瀏覽代碼

resource identifier

Laserlicht 1 月之前
父節點
當前提交
62ca532291

+ 4 - 0
config/gameConfig.json

@@ -111,6 +111,10 @@
 	[
 		"config/terrains.json"
 	],
+	"resources" :
+	[
+		"config/resources.json"
+	],
 	"roads":
 	[
 		"config/roads.json"

+ 39 - 2
config/resources.json

@@ -1,4 +1,41 @@
 {
-	// Price of each resource in gold, in usual resource order
-	"resources_prices": [ 250, 500, 250, 500, 500, 500, 1, 0 ]
+	"wood": {
+		"price": 250
+
+	},
+	
+	"mercury": {
+		"price": 500
+
+	},
+	
+	"ore": {
+		"price": 250
+
+	},
+	
+	"sulfur": {
+		"price": 500
+
+	},
+	
+	"crystal": {
+		"price": 500
+
+	},
+	
+	"gems": {
+		"price": 500
+
+	},
+	
+	"gold": {
+		"price": 1
+
+	},
+	
+	"mithril": {
+		"price": 0
+
+	}
 }

+ 4 - 0
config/schemas/mod.json

@@ -303,6 +303,10 @@
 			"description" : "List of configuration files for terrains",
 			"$ref" : "#/definitions/fileListOrObject"
 		},
+		"resources" : {
+			"description" : "List of configuration files for resources",
+			"$ref" : "#/definitions/fileListOrObject"
+		},
 		"roads" : {
 			"description" : "List of configuration files for roads",
 			"$ref" : "#/definitions/fileListOrObject"

+ 2 - 0
lib/CMakeLists.txt

@@ -111,6 +111,7 @@ set(lib_MAIN_SRCS
 	entities/hero/CHeroClass.cpp
 	entities/hero/CHeroClassHandler.cpp
 	entities/hero/CHeroHandler.cpp
+	entities/ResourceTypeHandler.cpp
 
 	events/ApplyDamage.cpp
 	events/GameResumed.cpp
@@ -534,6 +535,7 @@ set(lib_MAIN_HEADERS
 	entities/hero/CHeroClassHandler.h
 	entities/hero/CHeroHandler.h
 	entities/hero/EHeroGender.h
+	entities/ResourceTypeHandler.h
 
 	events/ApplyDamage.h
 	events/GameResumed.h

+ 2 - 0
lib/GameLibrary.cpp

@@ -25,6 +25,7 @@
 #include "entities/faction/CTownHandler.h"
 #include "entities/hero/CHeroClassHandler.h"
 #include "entities/hero/CHeroHandler.h"
+#include "entities/ResourceTypeHandler.h"
 #include "texts/CGeneralTextHandler.h"
 #include "campaign/CampaignRegionsHandler.h"
 #include "mapping/MapFormatSettings.h"
@@ -171,6 +172,7 @@ void GameLibrary::initializeLibrary()
 
 	createHandler(generaltexth);
 	createHandler(bth);
+	createHandler(resourceTypeHandler);
 	createHandler(roadTypeHandler);
 	createHandler(riverTypeHandler);
 	createHandler(terrainTypeHandler);

+ 2 - 0
lib/GameLibrary.h

@@ -31,6 +31,7 @@ class BattleFieldHandler;
 class IBonusTypeHandler;
 class CBonusTypeHandler;
 class TerrainTypeHandler;
+class ResourceTypeHandler;
 class RoadTypeHandler;
 class RiverTypeHandler;
 class ObstacleHandler;
@@ -90,6 +91,7 @@ public:
 	std::unique_ptr<CGeneralTextHandler> generaltexth;
 	std::unique_ptr<CModHandler> modh;
 	std::unique_ptr<TerrainTypeHandler> terrainTypeHandler;
+	std::unique_ptr<ResourceTypeHandler> resourceTypeHandler;
 	std::unique_ptr<RoadTypeHandler> roadTypeHandler;
 	std::unique_ptr<RiverTypeHandler> riverTypeHandler;
 	std::unique_ptr<CIdentifierStorage> identifiersHandler;

+ 77 - 0
lib/entities/ResourceTypeHandler.cpp

@@ -0,0 +1,77 @@
+/*
+ * ResourceTypeHandler.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 "ResourceTypeHandler.h"
+
+#include "../GameLibrary.h"
+#include "../json/JsonNode.h"
+#include "../texts/CGeneralTextHandler.h"
+#include "../texts/TextIdentifier.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+std::string resources::ResourceType::getNameTextID() const
+{
+	return TextIdentifier( "resourceType", modScope, identifier, "name" ).get();
+}
+
+std::string resources::ResourceType::getNameTranslated() const
+{
+	return LIBRARY->generaltexth->translate(getNameTextID());
+}
+
+std::vector<JsonNode> ResourceTypeHandler::loadLegacyData()
+{
+	objects.resize(8);
+
+	return std::vector<JsonNode>(8, JsonNode(JsonMap()));
+}
+
+std::shared_ptr<resources::ResourceType> ResourceTypeHandler::loadObjectImpl(std::string scope, std::string name, const JsonNode & data, size_t index)
+{
+	auto ret = std::make_shared<resources::ResourceType>();
+
+	ret->id = GameResID(index);
+	ret->modScope = scope;
+	ret->identifier = name;
+
+	ret->price = data["price"].Integer();
+
+	LIBRARY->generaltexth->registerString(scope, ret->getNameTextID(), data["name"]);
+
+	return ret;
+}
+
+/// loads single object into game. Scope is namespace of this object, same as name of source mod
+void ResourceTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
+{
+	objects.push_back(loadObjectImpl(scope, name, data, objects.size()));
+	registerObject(scope, "resourceType", name, objects.back()->getIndex());
+}
+
+void ResourceTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
+{
+	assert(objects[index] == nullptr); // ensure that this id was not loaded before
+	objects[index] = loadObjectImpl(scope, name, data, index);
+	registerObject(scope, "resourceType", name, objects[index]->getIndex());
+}
+
+std::vector<GameResID> ResourceTypeHandler::getAllObjects() const
+{
+	std::vector<GameResID> result;
+
+	for (const auto & school : objects)
+		result.push_back(school->id);
+
+	return result;
+}
+
+VCMI_LIB_NAMESPACE_END

+ 76 - 0
lib/entities/ResourceTypeHandler.h

@@ -0,0 +1,76 @@
+/*
+ * ResourceTypeHandler.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 <vcmi/EntityService.h>
+#include <vcmi/Entity.h>
+#include "../constants/EntityIdentifiers.h"
+#include "../IHandlerBase.h"
+#include "../filesystem/ResourcePath.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+class ResourceTypeHandler;
+
+namespace resources
+{
+
+class DLL_LINKAGE ResourceType : public EntityT<GameResID>
+{
+	friend class VCMI_LIB_WRAP_NAMESPACE(ResourceTypeHandler);
+
+	GameResID id; //backlink
+
+	int price;
+
+	std::string identifier;
+	std::string modScope;
+
+public:
+	int getPrice() const
+	{
+		return price;
+	}
+
+	std::string getJsonKey() const override { return identifier; }
+	int32_t getIndex() const override { return id.getNum(); }
+	GameResID getId() const override { return id;}
+	int32_t getIconIndex() const override { return 0; }
+	std::string getModScope() const override { return modScope; };
+	void registerIcons(const IconRegistar & cb) const override {};
+	std::string getNameTextID() const override;
+	std::string getNameTranslated() const override;
+};
+
+}
+
+class DLL_LINKAGE ResourceTypeHandler : public IHandlerBase
+{
+	std::shared_ptr<resources::ResourceType> loadObjectImpl(std::string scope, std::string name, const JsonNode & data, size_t index);
+public:
+	std::vector<JsonNode> loadLegacyData() override;
+
+	/// loads single object into game. Scope is namespace of this object, same as name of source mod
+	void loadObject(std::string scope, std::string name, const JsonNode & data) override;
+	void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
+
+	std::vector<GameResID> getAllObjects() const;
+
+	const resources::ResourceType * getById(GameResID index) const
+	{
+		return objects.at(index).get();
+	}
+
+private:
+	std::vector<std::shared_ptr<resources::ResourceType>> objects;
+};
+
+VCMI_LIB_NAMESPACE_END

+ 5 - 3
lib/mapObjects/CObjectHandler.cpp

@@ -12,6 +12,8 @@
 #include "CObjectHandler.h"
 
 #include "CGObjectInstance.h"
+#include "../GameLibrary.h"
+#include "../entities/ResourceTypeHandler.h"
 #include "../filesystem/ResourcePath.h"
 #include "../json/JsonNode.h"
 
@@ -20,10 +22,10 @@ VCMI_LIB_NAMESPACE_BEGIN
 CObjectHandler::CObjectHandler()
 {
 	logGlobal->trace("\t\tReading resources prices ");
-	const JsonNode config2(JsonPath::builtin("config/resources.json"));
-	for(const JsonNode &price : config2["resources_prices"].Vector())
+	for(auto & res : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
-		resVals.push_back(static_cast<ui32>(price.Float()));
+		auto resType = LIBRARY->resourceTypeHandler->getById(res);
+		resVals[res] = static_cast<ui32>(resType->getPrice());
 	}
 	logGlobal->trace("\t\tDone loading resource prices!");
 }

+ 1 - 1
lib/mapObjects/CObjectHandler.h

@@ -19,7 +19,7 @@ class int3;
 class DLL_LINKAGE CObjectHandler
 {
 public:
-	std::vector<ui32> resVals; //default values of resources in gold
+	std::map<GameResID, ui32> resVals; //default values of resources in gold
 
 	CObjectHandler();
 };

+ 2 - 0
lib/modding/ContentTypeHandler.cpp

@@ -23,6 +23,7 @@
 #include "../entities/faction/CTownHandler.h"
 #include "../entities/hero/CHeroClassHandler.h"
 #include "../entities/hero/CHeroHandler.h"
+#include "../entities/ResourceTypeHandler.h"
 #include "../texts/CGeneralTextHandler.h"
 #include "../CBonusTypeHandler.h"
 #include "../CSkillHandler.h"
@@ -259,6 +260,7 @@ void CContentHandler::init()
 #endif
 	handlers.insert(std::make_pair("battlefields", ContentTypeHandler(LIBRARY->battlefieldsHandler.get(), "battlefield")));
 	handlers.insert(std::make_pair("terrains", ContentTypeHandler(LIBRARY->terrainTypeHandler.get(), "terrain")));
+	handlers.insert(std::make_pair("resources", ContentTypeHandler(LIBRARY->resourceTypeHandler.get(), "resources")));
 	handlers.insert(std::make_pair("rivers", ContentTypeHandler(LIBRARY->riverTypeHandler.get(), "river")));
 	handlers.insert(std::make_pair("roads", ContentTypeHandler(LIBRARY->roadTypeHandler.get(), "road")));
 	handlers.insert(std::make_pair("obstacles", ContentTypeHandler(LIBRARY->obstacleHandler.get(), "obstacle")));