浏览代码

Moved DwellingInstanceConstructor to a new file

Ivan Savenko 2 年之前
父节点
当前提交
58661fc8ec

+ 2 - 0
cmake_modules/VCMI_lib.cmake

@@ -70,6 +70,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
 		${MAIN_LIB_DIR}/mapObjectConstructors/CObjectClassesHandler.cpp
 		${MAIN_LIB_DIR}/mapObjectConstructors/CommonConstructors.cpp
 		${MAIN_LIB_DIR}/mapObjectConstructors/CRewardableConstructor.cpp
+		${MAIN_LIB_DIR}/mapObjectConstructors/DwellingInstanceConstructor.cpp
 		${MAIN_LIB_DIR}/mapObjectConstructors/HillFortInstanceConstructor.cpp
 		${MAIN_LIB_DIR}/mapObjectConstructors/ShipyardInstanceConstructor.cpp
 		${MAIN_LIB_DIR}/mapObjectConstructors/ShrineInstanceConstructor.cpp
@@ -378,6 +379,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
 		${MAIN_LIB_DIR}/mapObjectConstructors/CObjectClassesHandler.h
 		${MAIN_LIB_DIR}/mapObjectConstructors/CommonConstructors.h
 		${MAIN_LIB_DIR}/mapObjectConstructors/CRewardableConstructor.h
+		${MAIN_LIB_DIR}/mapObjectConstructors/DwellingInstanceConstructor.h
 		${MAIN_LIB_DIR}/mapObjectConstructors/HillFortInstanceConstructor.h
 		${MAIN_LIB_DIR}/mapObjectConstructors/IObjectInfo.h
 		${MAIN_LIB_DIR}/mapObjectConstructors/RandomMapInfo.h

+ 1 - 1
lib/CGameState.cpp

@@ -621,7 +621,7 @@ std::pair<Obj,int> CGameState::pickObject (CGObjectInstance *obj)
 				auto dwellingIDs = VLC->objtypeh->knownSubObjects(primaryID);
 				for (si32 entry : dwellingIDs)
 				{
-					const auto * handler = dynamic_cast<const CDwellingInstanceConstructor *>(VLC->objtypeh->getHandlerFor(primaryID, entry).get());
+					const auto * handler = dynamic_cast<const DwellingInstanceConstructor *>(VLC->objtypeh->getHandlerFor(primaryID, entry).get());
 
 					if (handler->producesCreature(VLC->creh->objects[cid]))
 						result = std::make_pair(primaryID, entry);

+ 2 - 1
lib/mapObjectConstructors/CObjectClassesHandler.cpp

@@ -24,6 +24,7 @@
 #include "../mapObjectConstructors/CBankInstanceConstructor.h"
 #include "../mapObjectConstructors/CRewardableConstructor.h"
 #include "../mapObjectConstructors/CommonConstructors.h"
+#include "../mapObjectConstructors/DwellingInstanceConstructor.h"
 #include "../mapObjectConstructors/HillFortInstanceConstructor.h"
 #include "../mapObjectConstructors/ShipyardInstanceConstructor.h"
 #include "../mapObjectConstructors/ShrineInstanceConstructor.h"
@@ -46,7 +47,7 @@ CObjectClassesHandler::CObjectClassesHandler()
 	// list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
 	//Note: should be in sync with registerTypesMapObjectTypes function
 	SET_HANDLER_CLASS("configurable", CRewardableConstructor);
-	SET_HANDLER_CLASS("dwelling", CDwellingInstanceConstructor);
+	SET_HANDLER_CLASS("dwelling", DwellingInstanceConstructor);
 	SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
 	SET_HANDLER_CLASS("town", CTownInstanceConstructor);
 	SET_HANDLER_CLASS("bank", CBankInstanceConstructor);

+ 2 - 125
lib/mapObjectConstructors/CommonConstructors.cpp

@@ -10,16 +10,16 @@
 #include "StdInc.h"
 #include "CommonConstructors.h"
 
-#include "../CCreatureHandler.h"
 #include "../CGeneralTextHandler.h"
 #include "../CHeroHandler.h"
 #include "../CModHandler.h"
+#include "../CTownHandler.h"
 #include "../IGameCallback.h"
 #include "../JsonRandom.h"
 #include "../StringConstants.h"
 #include "../TerrainHandler.h"
+#include "../VCMI_Lib.h"
 
-#include "../mapObjects/CBank.h"
 #include "../mapObjects/CGHeroInstance.h"
 #include "../mapObjects/CGMarket.h"
 #include "../mapObjects/CGTownInstance.h"
@@ -133,129 +133,6 @@ void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomG
 
 }
 
-bool CDwellingInstanceConstructor::hasNameTextID() const
-{
-	return true;
-}
-
-void CDwellingInstanceConstructor::initTypeData(const JsonNode & input)
-{
-	if (input.Struct().count("name") == 0)
-		logMod->warn("Dwelling %s missing name!", getJsonKey());
-
-	VLC->generaltexth->registerString( input.meta, getNameTextID(), input["name"].String());
-
-	const JsonVector & levels = input["creatures"].Vector();
-	const auto totalLevels = levels.size();
-
-	availableCreatures.resize(totalLevels);
-	for(auto currentLevel = 0; currentLevel < totalLevels; currentLevel++)
-	{
-		const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
-		const auto creaturesNumber = creaturesOnLevel.size();
-		availableCreatures[currentLevel].resize(creaturesNumber);
-
-		for(auto currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
-		{
-			VLC->modh->identifiers.requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
-			{
-				availableCreatures[currentLevel][currentCreature] = VLC->creh->objects[index];
-			});
-		}
-		assert(!availableCreatures[currentLevel].empty());
-	}
-	guards = input["guards"];
-}
-
-bool CDwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
-{
-	return false;
-}
-
-void CDwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
-{
-	obj->creatures.resize(availableCreatures.size());
-	for(const auto & entry : availableCreatures)
-	{
-		for(const CCreature * cre : entry)
-			obj->creatures.back().second.push_back(cre->getId());
-	}
-}
-
-void CDwellingInstanceConstructor::randomizeObject(CGDwelling * object, CRandomGenerator &rng) const
-{
-	auto * dwelling = dynamic_cast<CGDwelling *>(object);
-
-	dwelling->creatures.clear();
-	dwelling->creatures.reserve(availableCreatures.size());
-
-	for(const auto & entry : availableCreatures)
-	{
-		dwelling->creatures.resize(dwelling->creatures.size() + 1);
-		for(const CCreature * cre : entry)
-			dwelling->creatures.back().second.push_back(cre->getId());
-	}
-
-	bool guarded = false; //TODO: serialize for sanity
-
-	if(guards.getType() == JsonNode::JsonType::DATA_BOOL) //simple switch
-	{
-		if(guards.Bool())
-		{
-			guarded = true;
-		}
-	}
-	else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR) //custom guards (eg. Elemental Conflux)
-	{
-		for(auto & stack : JsonRandom::loadCreatures(guards, rng))
-		{
-			dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->getId(), stack.count));
-		}
-	}
-	else //default condition - creatures are of level 5 or higher
-	{
-		for(auto creatureEntry : availableCreatures)
-		{
-			if(creatureEntry.at(0)->getLevel() >= 5)
-			{
-				guarded = true;
-				break;
-			}
-		}
-	}
-
-	if(guarded)
-	{
-		for(auto creatureEntry : availableCreatures)
-		{
-			const CCreature * crea = creatureEntry.at(0);
-			dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
-		}
-	}
-}
-
-bool CDwellingInstanceConstructor::producesCreature(const CCreature * crea) const
-{
-	for(const auto & entry : availableCreatures)
-	{
-		for(const CCreature * cre : entry)
-			if(crea == cre)
-				return true;
-	}
-	return false;
-}
-
-std::vector<const CCreature *> CDwellingInstanceConstructor::getProducedCreatures() const
-{
-	std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
-	for(const auto & entry : availableCreatures)
-	{
-		for(const CCreature * cre : entry)
-			creatures.push_back(cre);
-	}
-	return creatures;
-}
-
 void BoatInstanceConstructor::initTypeData(const JsonNode & input)
 {
 	layer = EPathfindingLayer::SAIL;

+ 2 - 30
lib/mapObjectConstructors/CommonConstructors.h

@@ -9,18 +9,17 @@
 */
 #pragma once
 
-#include "AObjectTypeHandler.h"
 #include "CDefaultObjectTypeHandler.h"
-
 #include "../LogicalExpression.h"
 
+#include "../mapObjects/MiscObjects.h"
+
 VCMI_LIB_NAMESPACE_BEGIN
 
 class CGArtifact;
 class CGObjectInstance;
 class CGTownInstance;
 class CGHeroInstance;
-class CGDwelling;
 class CGMarket;
 class CHeroClass;
 class CBank;
@@ -82,33 +81,6 @@ public:
 	}
 };
 
-class CDwellingInstanceConstructor : public CDefaultObjectTypeHandler<CGDwelling>
-{
-	std::vector<std::vector<const CCreature *>> availableCreatures;
-
-	JsonNode guards;
-
-protected:
-	bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const override;
-	void initTypeData(const JsonNode & input) override;
-
-public:
-	bool hasNameTextID() const override;
-
-	void initializeObject(CGDwelling * object) const override;
-	void randomizeObject(CGDwelling * object, CRandomGenerator & rng) const override;
-
-	bool producesCreature(const CCreature * crea) const;
-	std::vector<const CCreature *> getProducedCreatures() const;
-
-	template <typename Handler> void serialize(Handler &h, const int version)
-	{
-		h & availableCreatures;
-		h & guards;
-		h & static_cast<CDefaultObjectTypeHandler<CGDwelling>&>(*this);
-	}
-};
-
 class DLL_LINKAGE BoatInstanceConstructor : public CDefaultObjectTypeHandler<CGBoat>
 {
 protected:

+ 146 - 0
lib/mapObjectConstructors/DwellingInstanceConstructor.cpp

@@ -0,0 +1,146 @@
+/*
+* DwellingInstanceConstructor.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 "DwellingInstanceConstructor.h"
+
+#include "../CCreatureHandler.h"
+#include "../CGeneralTextHandler.h"
+#include "../CModHandler.h"
+#include "../JsonRandom.h"
+#include "../VCMI_Lib.h"
+#include "../mapObjects/CGDwelling.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+bool DwellingInstanceConstructor::hasNameTextID() const
+{
+	return true;
+}
+
+void DwellingInstanceConstructor::initTypeData(const JsonNode & input)
+{
+	if (input.Struct().count("name") == 0)
+		logMod->warn("Dwelling %s missing name!", getJsonKey());
+
+	VLC->generaltexth->registerString( input.meta, getNameTextID(), input["name"].String());
+
+	const JsonVector & levels = input["creatures"].Vector();
+	const auto totalLevels = levels.size();
+
+	availableCreatures.resize(totalLevels);
+	for(auto currentLevel = 0; currentLevel < totalLevels; currentLevel++)
+	{
+		const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
+		const auto creaturesNumber = creaturesOnLevel.size();
+		availableCreatures[currentLevel].resize(creaturesNumber);
+
+		for(auto currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
+		{
+			VLC->modh->identifiers.requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
+			{
+				availableCreatures[currentLevel][currentCreature] = VLC->creh->objects[index];
+			});
+		}
+		assert(!availableCreatures[currentLevel].empty());
+	}
+	guards = input["guards"];
+}
+
+bool DwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
+{
+	return false;
+}
+
+void DwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
+{
+	obj->creatures.resize(availableCreatures.size());
+	for(const auto & entry : availableCreatures)
+	{
+		for(const CCreature * cre : entry)
+			obj->creatures.back().second.push_back(cre->getId());
+	}
+}
+
+void DwellingInstanceConstructor::randomizeObject(CGDwelling * object, CRandomGenerator &rng) const
+{
+	auto * dwelling = dynamic_cast<CGDwelling *>(object);
+
+	dwelling->creatures.clear();
+	dwelling->creatures.reserve(availableCreatures.size());
+
+	for(const auto & entry : availableCreatures)
+	{
+		dwelling->creatures.resize(dwelling->creatures.size() + 1);
+		for(const CCreature * cre : entry)
+			dwelling->creatures.back().second.push_back(cre->getId());
+	}
+
+	bool guarded = false; //TODO: serialize for sanity
+
+	if(guards.getType() == JsonNode::JsonType::DATA_BOOL) //simple switch
+	{
+		if(guards.Bool())
+		{
+			guarded = true;
+		}
+	}
+	else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR) //custom guards (eg. Elemental Conflux)
+	{
+		for(auto & stack : JsonRandom::loadCreatures(guards, rng))
+		{
+			dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->getId(), stack.count));
+		}
+	}
+	else //default condition - creatures are of level 5 or higher
+	{
+		for(auto creatureEntry : availableCreatures)
+		{
+			if(creatureEntry.at(0)->getLevel() >= 5)
+			{
+				guarded = true;
+				break;
+			}
+		}
+	}
+
+	if(guarded)
+	{
+		for(auto creatureEntry : availableCreatures)
+		{
+			const CCreature * crea = creatureEntry.at(0);
+			dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
+		}
+	}
+}
+
+bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
+{
+	for(const auto & entry : availableCreatures)
+	{
+		for(const CCreature * cre : entry)
+			if(crea == cre)
+				return true;
+	}
+	return false;
+}
+
+std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
+{
+	std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
+	for(const auto & entry : availableCreatures)
+	{
+		for(const CCreature * cre : entry)
+			creatures.push_back(cre);
+	}
+	return creatures;
+}
+
+
+VCMI_LIB_NAMESPACE_END

+ 45 - 0
lib/mapObjectConstructors/DwellingInstanceConstructor.h

@@ -0,0 +1,45 @@
+/*
+* DwellingInstanceConstructor.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 "CDefaultObjectTypeHandler.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+class CGDwelling;
+
+class DwellingInstanceConstructor : public CDefaultObjectTypeHandler<CGDwelling>
+{
+	std::vector<std::vector<const CCreature *>> availableCreatures;
+
+	JsonNode guards;
+
+protected:
+	bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const override;
+	void initTypeData(const JsonNode & input) override;
+
+public:
+	bool hasNameTextID() const override;
+
+	void initializeObject(CGDwelling * object) const override;
+	void randomizeObject(CGDwelling * object, CRandomGenerator & rng) const override;
+
+	bool producesCreature(const CCreature * crea) const;
+	std::vector<const CCreature *> getProducedCreatures() const;
+
+	template <typename Handler> void serialize(Handler &h, const int version)
+	{
+		h & availableCreatures;
+		h & guards;
+		h & static_cast<CDefaultObjectTypeHandler<CGDwelling>&>(*this);
+	}
+};
+
+VCMI_LIB_NAMESPACE_END

+ 2 - 1
lib/registerTypes/RegisterTypes.h

@@ -21,6 +21,7 @@
 #include "../mapObjectConstructors/CRewardableConstructor.h"
 #include "../mapObjectConstructors/CommonConstructors.h"
 #include "../mapObjectConstructors/CBankInstanceConstructor.h"
+#include "../mapObjectConstructors/DwellingInstanceConstructor.h"
 #include "../mapObjectConstructors/HillFortInstanceConstructor.h"
 #include "../mapObjectConstructors/ShipyardInstanceConstructor.h"
 #include "../mapObjectConstructors/ShrineInstanceConstructor.h"
@@ -97,7 +98,7 @@ void registerTypesMapObjectTypes(Serializer &s)
 	s.template registerType<AObjectTypeHandler, CRewardableConstructor>();
 	s.template registerType<AObjectTypeHandler, CHeroInstanceConstructor>();
 	s.template registerType<AObjectTypeHandler, CTownInstanceConstructor>();
-	s.template registerType<AObjectTypeHandler, CDwellingInstanceConstructor>();
+	s.template registerType<AObjectTypeHandler, DwellingInstanceConstructor>();
 	s.template registerType<AObjectTypeHandler, CBankInstanceConstructor>();
 	s.template registerType<AObjectTypeHandler, BoatInstanceConstructor>();
 	s.template registerType<AObjectTypeHandler, MarketInstanceConstructor>();

+ 2 - 2
lib/rmg/modificators/TreasurePlacer.cpp

@@ -22,7 +22,7 @@
 #include "../../ArtifactUtils.h"
 #include "../../mapObjectConstructors/AObjectTypeHandler.h"
 #include "../../mapObjectConstructors/CObjectClassesHandler.h"
-#include "../../mapObjectConstructors/CommonConstructors.h"
+#include "../../mapObjectConstructors/DwellingInstanceConstructor.h"
 #include "../../mapObjects/CGHeroInstance.h"
 #include "../../mapObjects/CGPandoraBox.h"
 #include "../../CCreatureHandler.h"
@@ -172,7 +172,7 @@ void TreasurePlacer::addAllPossibleObjects()
 		
 		for(auto secondaryID : subObjects)
 		{
-			const auto * dwellingHandler = dynamic_cast<const CDwellingInstanceConstructor *>(VLC->objtypeh->getHandlerFor(dwellingType, secondaryID).get());
+			const auto * dwellingHandler = dynamic_cast<const DwellingInstanceConstructor *>(VLC->objtypeh->getHandlerFor(dwellingType, secondaryID).get());
 			auto creatures = dwellingHandler->getProducedCreatures();
 			if(creatures.empty())
 				continue;