Browse Source

Events serialization

nordsoft 2 years ago
parent
commit
6e3817f18c

+ 29 - 1
lib/mapping/CMap.cpp

@@ -56,9 +56,37 @@ bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
 	return firstOccurence <= other.firstOccurence;
 }
 
-CCastleEvent::CCastleEvent() : town(nullptr)
+void CMapEvent::serializeJson(JsonSerializeFormat & handler)
 {
+	handler.serializeString("name", name);
+	handler.serializeString("message", message);
+	handler.serializeInt("players", players);
+	handler.serializeInt("humanAffected", humanAffected);
+	handler.serializeInt("computerAffected", computerAffected);
+	handler.serializeInt("firstOccurence", firstOccurence);
+	handler.serializeInt("nextOccurence", nextOccurence);
+	resources.serializeJson(handler, "resources");
+}
 
+void CCastleEvent::serializeJson(JsonSerializeFormat & handler)
+{
+	CMapEvent::serializeJson(handler);
+	{
+		std::vector<BuildingID> temp(buildings.begin(), buildings.end());
+		auto a = handler.enterArray("buildings");
+		a.syncSize(temp);
+		for(int i = 0; i < temp.size(); ++i)
+		{
+			a.serializeInt(i, temp[i]);
+			buildings.insert(temp[i]);
+		}
+	}
+	{
+		auto a = handler.enterArray("creatures");
+		a.syncSize(creatures);
+		for(int i = 0; i < creatures.size(); ++i)
+			a.serializeInt(i, creatures[i]);
+	}
 }
 
 TerrainTile::TerrainTile():

+ 7 - 2
lib/mapping/CMapDefines.h

@@ -19,6 +19,7 @@ class RiverType;
 class RoadType;
 class CGObjectInstance;
 class CGTownInstance;
+class JsonSerializeFormat;
 
 /// The map event is an event which e.g. gives or takes resources of a specific
 /// amount to/from players and can appear regularly or once a time.
@@ -26,6 +27,7 @@ class DLL_LINKAGE CMapEvent
 {
 public:
 	CMapEvent();
+	virtual ~CMapEvent() = default;
 
 	bool earlierThan(const CMapEvent & other) const;
 	bool earlierThanOrEqual(const CMapEvent & other) const;
@@ -51,17 +53,18 @@ public:
 		h & firstOccurence;
 		h & nextOccurence;
 	}
+	
+	virtual void serializeJson(JsonSerializeFormat & handler);
 };
 
 /// The castle event builds/adds buildings/creatures for a specific town.
 class DLL_LINKAGE CCastleEvent: public CMapEvent
 {
 public:
-	CCastleEvent();
+	CCastleEvent() = default;
 
 	std::set<BuildingID> buildings;
 	std::vector<si32> creatures;
-	CGTownInstance * town;
 
 	template <typename Handler>
 	void serialize(Handler & h, const int version)
@@ -70,6 +73,8 @@ public:
 		h & buildings;
 		h & creatures;
 	}
+	
+	void serializeJson(JsonSerializeFormat & handler) override;
 };
 
 /// The terrain tile describes the terrain type and the visual representation of the terrain.

+ 0 - 1
lib/mapping/MapFormatH3M.cpp

@@ -2097,7 +2097,6 @@ CGObjectInstance * CMapLoaderH3M::readTown(const int3 & position, std::shared_pt
 	for(int eventID = 0; eventID < eventsCount; ++eventID)
 	{
 		CCastleEvent event;
-		event.town = object;
 		event.name = readBasicString();
 		event.message = readLocalizedString(TextIdentifier("town", position.x, position.y, position.z, "event", eventID, "description"));
 

+ 11 - 1
lib/mapping/MapFormatJson.cpp

@@ -342,7 +342,7 @@ namespace TerrainDetail
 
 ///CMapFormatJson
 const int CMapFormatJson::VERSION_MAJOR = 1;
-const int CMapFormatJson::VERSION_MINOR = 1;
+const int CMapFormatJson::VERSION_MINOR = 2;
 
 const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
 const std::string CMapFormatJson::OBJECTS_FILE_NAME = "objects.json";
@@ -775,6 +775,14 @@ void CMapFormatJson::serializeRumors(JsonSerializeFormat & handler)
 	rumors.serializeStruct(map->rumors);
 }
 
+void CMapFormatJson::serializeTimedEvents(JsonSerializeFormat & handler)
+{
+	auto events = handler.enterArray("events");
+	std::vector<CMapEvent> temp(map->events.begin(), map->events.end());
+	events.serializeStruct(temp);
+	map->events.assign(temp.begin(), temp.end());
+}
+
 void CMapFormatJson::serializePredefinedHeroes(JsonSerializeFormat & handler)
 {
     //todo:serializePredefinedHeroes
@@ -816,6 +824,8 @@ void CMapFormatJson::serializePredefinedHeroes(JsonSerializeFormat & handler)
 void CMapFormatJson::serializeOptions(JsonSerializeFormat & handler)
 {
 	serializeRumors(handler);
+	
+	serializeTimedEvents(handler);
 
 	serializePredefinedHeroes(handler);
 

+ 2 - 0
lib/mapping/MapFormatJson.h

@@ -109,6 +109,8 @@ protected:
 	void serializePredefinedHeroes(JsonSerializeFormat & handler);
 
 	void serializeRumors(JsonSerializeFormat & handler);
+	
+	void serializeTimedEvents(JsonSerializeFormat & handler);
 
 	///common part of map attributes saving/loading
 	void serializeOptions(JsonSerializeFormat & handler);