浏览代码

CGMine, CGArtifact, CGGarrison serialization

AlexVinS 9 年之前
父节点
当前提交
f9d0b20234

+ 0 - 2
lib/mapObjects/CArmedInstance.cpp

@@ -135,12 +135,10 @@ CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
 
 
 void CArmedInstance::writeJsonOptions(JsonNode& json) const
 void CArmedInstance::writeJsonOptions(JsonNode& json) const
 {
 {
-	CGObjectInstance::writeJsonOptions(json);
 	CCreatureSet::writeJson(json["army"]);
 	CCreatureSet::writeJson(json["army"]);
 }
 }
 
 
 void CArmedInstance::readJsonOptions(const JsonNode& json)
 void CArmedInstance::readJsonOptions(const JsonNode& json)
 {
 {
-	CGObjectInstance::readJsonOptions(json);
 	CCreatureSet::readJson(json["army"]);
 	CCreatureSet::readJson(json["army"]);
 }
 }

+ 19 - 7
lib/mapObjects/CGTownInstance.cpp

@@ -313,6 +313,18 @@ void CGDwelling::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer)
 	}
 	}
 }
 }
 
 
+void CGDwelling::writeJsonOptions(JsonNode& json) const
+{
+	//todo:CGDwelling::writeJsonOptions
+	CGObjectInstance::writeOwner(json);
+}
+
+void CGDwelling::readJsonOptions(const JsonNode& json)
+{
+	//todo:CGDwelling::readJsonOptions
+	CGObjectInstance::readOwner(json);
+}
+
 int CGTownInstance::getSightRadious() const //returns sight distance
 int CGTownInstance::getSightRadious() const //returns sight distance
 {
 {
 	if (subID == ETownType::TOWER)
 	if (subID == ETownType::TOWER)
@@ -360,7 +372,7 @@ CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - for
 
 
 int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
 int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
 {
 {
-	
+
 	if (hasBuilt(BuildingID::CAPITOL))
 	if (hasBuilt(BuildingID::CAPITOL))
 		return 3;
 		return 3;
 	if (hasBuilt(BuildingID::CITY_HALL))
 	if (hasBuilt(BuildingID::CITY_HALL))
@@ -455,12 +467,12 @@ TResources CGTownInstance::dailyIncome() const
 {
 {
 	TResources ret;
 	TResources ret;
 
 
-	for (auto & p : town->buildings) 
-	{ 
+	for (auto & p : town->buildings)
+	{
 		BuildingID buildingUpgrade;
 		BuildingID buildingUpgrade;
 
 
-		for (auto & p2 : town->buildings) 
-		{ 
+		for (auto & p2 : town->buildings)
+		{
 			if (p2.second->upgrade == p.first)
 			if (p2.second->upgrade == p.first)
 			{
 			{
 				buildingUpgrade = p2.first;
 				buildingUpgrade = p2.first;
@@ -471,7 +483,7 @@ TResources CGTownInstance::dailyIncome() const
 		{
 		{
 			ret += p.second->produce;
 			ret += p.second->produce;
 		}
 		}
-	
+
 	}
 	}
 
 
 	return ret;
 	return ret;
@@ -964,7 +976,7 @@ void CGTownInstance::setVisitingHero(CGHeroInstance *h)
 	//{
 	//{
 	//	logGlobal->warnStream() << boost::format("Hero visiting town %s is %s ") % name % (visitingHero.get() ? visitingHero->name : "NULL");
 	//	logGlobal->warnStream() << boost::format("Hero visiting town %s is %s ") % name % (visitingHero.get() ? visitingHero->name : "NULL");
 	//	logGlobal->warnStream() << boost::format("New hero will be %s ") % (h ? h->name : "NULL");
 	//	logGlobal->warnStream() << boost::format("New hero will be %s ") % (h ? h->name : "NULL");
-	//	
+	//
 	//}
 	//}
 	assert(!!visitingHero == !h);
 	assert(!!visitingHero == !h);
 
 

+ 4 - 0
lib/mapObjects/CGTownInstance.h

@@ -52,6 +52,10 @@ public:
 	CSpecObjInfo * info; //h3m info about dewlling
 	CSpecObjInfo * info; //h3m info about dewlling
 	TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
 	TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
 
 
+protected:
+	void writeJsonOptions(JsonNode & json) const override;
+	void readJsonOptions(const JsonNode & json) override;
+
 private:
 private:
 	void initObj() override;
 	void initObj() override;
 	void onHeroVisit(const CGHeroInstance * h) const override;
 	void onHeroVisit(const CGHeroInstance * h) const override;

+ 18 - 3
lib/mapObjects/CObjectHandler.cpp

@@ -362,7 +362,23 @@ void CGObjectInstance::writeJsonOptions(JsonNode & json) const
 {
 {
 	json.setType(JsonNode::DATA_STRUCT);
 	json.setType(JsonNode::DATA_STRUCT);
 
 
-	//todo: move up to descendants
+//	//todo: move up to descendants
+//	if(tempOwner != PlayerColor::UNFLAGGABLE)
+//	{
+//		PlayerColor p (tempOwner);
+//		if(p.isValidPlayer())
+//			json["owner"].String() = GameConstants::PLAYER_COLOR_NAMES[p.getNum()];
+//	}
+}
+
+void CGObjectInstance::readJsonOptions(const JsonNode & json)
+{
+//	if(json["owner"].getType() == JsonNode::DATA_STRING)
+//		tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
+}
+
+void CGObjectInstance::writeOwner(JsonNode & json) const
+{
 	if(tempOwner != PlayerColor::UNFLAGGABLE)
 	if(tempOwner != PlayerColor::UNFLAGGABLE)
 	{
 	{
 		PlayerColor p (tempOwner);
 		PlayerColor p (tempOwner);
@@ -371,13 +387,12 @@ void CGObjectInstance::writeJsonOptions(JsonNode & json) const
 	}
 	}
 }
 }
 
 
-void CGObjectInstance::readJsonOptions(const JsonNode & json)
+void CGObjectInstance::readOwner(const JsonNode & json)
 {
 {
 	if(json["owner"].getType() == JsonNode::DATA_STRING)
 	if(json["owner"].getType() == JsonNode::DATA_STRING)
 		tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
 		tempOwner = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, json["owner"].String()));
 }
 }
 
 
-
 CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
 CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
 {
 {
 
 

+ 3 - 1
lib/mapObjects/CObjectHandler.h

@@ -1,4 +1,4 @@
-#pragma once
+#pragma once
 
 
 #include "ObjectTemplate.h"
 #include "ObjectTemplate.h"
 
 
@@ -206,6 +206,8 @@ protected:
 	///(!) do not forget to call inherited method  first when overriding
 	///(!) do not forget to call inherited method  first when overriding
 	virtual void readJsonOptions(const JsonNode & json);
 	virtual void readJsonOptions(const JsonNode & json);
 
 
+	void writeOwner(JsonNode & json) const;
+	void readOwner(const JsonNode & json);
 private:
 private:
 	mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
 	mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
 };
 };

+ 74 - 8
lib/mapObjects/MiscObjects.cpp

@@ -11,6 +11,7 @@
 #include "StdInc.h"
 #include "StdInc.h"
 #include "MiscObjects.h"
 #include "MiscObjects.h"
 
 
+#include "../StringConstants.h"
 #include "../NetPacks.h"
 #include "../NetPacks.h"
 #include "../CGeneralTextHandler.h"
 #include "../CGeneralTextHandler.h"
 #include "../CSoundBase.h"
 #include "../CSoundBase.h"
@@ -606,7 +607,7 @@ void CGMine::newTurn() const
 
 
 void CGMine::initObj()
 void CGMine::initObj()
 {
 {
-	if(subID >= 7) //Abandoned Mine
+	if(isAbandoned())
 	{
 	{
 		//set guardians
 		//set guardians
 		int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
 		int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
@@ -633,6 +634,11 @@ void CGMine::initObj()
 	producedQuantity = defaultResProduction();
 	producedQuantity = defaultResProduction();
 }
 }
 
 
+bool CGMine::isAbandoned() const
+{
+	return (subID >= 7);
+}
+
 std::string CGMine::getObjectName() const
 std::string CGMine::getObjectName() const
 {
 {
 	return VLC->generaltexth->mines.at(subID).first;
 	return VLC->generaltexth->mines.at(subID).first;
@@ -690,7 +696,7 @@ void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &resu
 {
 {
 	if(result.winner == 0) //attacker won
 	if(result.winner == 0) //attacker won
 	{
 	{
-		if(subID == 7)
+		if(isAbandoned())
 		{
 		{
 			showInfoDialog(hero->tempOwner, 85, 0);
 			showInfoDialog(hero->tempOwner, 85, 0);
 		}
 		}
@@ -704,14 +710,68 @@ void CGMine::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) con
 		cb->startBattleI(hero, this);
 		cb->startBattleI(hero, this);
 }
 }
 
 
-void CGMine::writeJsonOptions(JsonNode& json) const
+void CGMine::writeJsonOptions(JsonNode & json) const
 {
 {
+	CArmedInstance::writeJsonOptions(json);
+
+	if(isAbandoned())
+	{
+		JsonNode & node = json["possibleResources"];
 
 
+		for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
+			if(tempOwner.getNum() & 1<<i)
+			{
+				JsonNode one(JsonNode::DATA_STRING);
+				one.String() = GameConstants::RESOURCE_NAMES[i];
+				node.Vector().push_back(one);
+			}
+	}
+	else
+	{
+		CGObjectInstance::writeOwner(json);
+	}
 }
 }
 
 
-void CGMine::readJsonOptions(const JsonNode& json)
+void CGMine::readJsonOptions(const JsonNode & json)
 {
 {
+	CArmedInstance::readJsonOptions(json);
+
+	if(isAbandoned())
+	{
+		const JsonNode & node = json["possibleResources"];
 
 
+		std::set<int> possibleResources;
+
+		if(node.Vector().size() == 0)
+		{
+			//assume all allowed
+			for(int i = (int)Res::WOOD; i < (int) Res::GOLD; i++)
+				possibleResources.insert(i);
+		}
+		else
+		{
+            auto names = node.convertTo<std::vector<std::string>>();
+
+            for(const std::string & s : names)
+			{
+                int raw_res = vstd::find_pos(GameConstants::RESOURCE_NAMES, s);
+                if(raw_res < 0)
+					logGlobal->errorStream() << "Invalid resource name: "+s;
+				else
+					possibleResources.insert(raw_res);
+			}
+
+			int tmp = 0;
+
+			for(int r : possibleResources)
+				tmp |=  (1<<r);
+			tempOwner = PlayerColor(tmp);
+		}
+	}
+	else
+	{
+		CGObjectInstance::readOwner(json);
+	}
 }
 }
 
 
 
 
@@ -1298,12 +1358,14 @@ void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer)
 
 
 void CGArtifact::writeJsonOptions(JsonNode& json) const
 void CGArtifact::writeJsonOptions(JsonNode& json) const
 {
 {
-
+	CCreatureSet::writeJson(json["guards"]);
+	json["guardMessage"].String() = message;
 }
 }
 
 
 void CGArtifact::readJsonOptions(const JsonNode& json)
 void CGArtifact::readJsonOptions(const JsonNode& json)
 {
 {
-
+	CCreatureSet::readJson(json["guards"]);
+	message = json["guardMessage"].String();
 }
 }
 
 
 void CGWitchHut::initObj()
 void CGWitchHut::initObj()
@@ -1681,12 +1743,16 @@ void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &
 
 
 void CGGarrison::writeJsonOptions(JsonNode& json) const
 void CGGarrison::writeJsonOptions(JsonNode& json) const
 {
 {
-
+	CArmedInstance::writeJsonOptions(json);
+	CGObjectInstance::writeOwner(json);
+	json["removableUnits"].Bool() = removableUnits;
 }
 }
 
 
 void CGGarrison::readJsonOptions(const JsonNode& json)
 void CGGarrison::readJsonOptions(const JsonNode& json)
 {
 {
-
+	CArmedInstance::readJsonOptions(json);
+	CGObjectInstance::readOwner(json);
+	removableUnits = json["removableUnits"].Bool();
 }
 }
 
 
 void CGMagi::initObj()
 void CGMagi::initObj()

+ 1 - 0
lib/mapObjects/MiscObjects.h

@@ -263,6 +263,7 @@ private:
 	std::string getObjectName() const override;
 	std::string getObjectName() const override;
 	std::string getHoverText(PlayerColor player) const override;
 	std::string getHoverText(PlayerColor player) const override;
 
 
+	bool isAbandoned() const;
 public:
 public:
 	template <typename Handler> void serialize(Handler &h, const int version)
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
 	{