Browse Source

Rumors: move code into CGameState and add backward compatability

ArseniyShestakov 10 years ago
parent
commit
98582d628c
5 changed files with 111 additions and 55 deletions
  1. 11 54
      lib/CGameInfoCallback.cpp
  2. 69 0
      lib/CGameState.cpp
  3. 27 0
      lib/CGameState.h
  4. 1 1
      lib/Connection.h
  5. 3 0
      lib/NetPacksLib.cpp

+ 11 - 54
lib/CGameInfoCallback.cpp

@@ -568,65 +568,22 @@ EPlayerStatus::EStatus CGameInfoCallback::getPlayerStatus(PlayerColor player, bo
 std::string CGameInfoCallback::getTavernRumor(const CGObjectInstance * townOrTavern) const
 {
 	std::string text = "";
-	auto & rand = gs->getRandomGenerator();
+	if(gs->rumor.type == RumorState::RUMOR_NONE) // (version < 755 backward compatability
+		return text;
 
-	static std::vector<int> rumorTypes = {0, 1, 2, 2};
-	auto & rumorType = *RandomGeneratorUtil::nextItem(rumorTypes, rand);
-	switch(rumorType)
+	auto rumor = gs->rumor.last[gs->rumor.type];
+	switch(gs->rumor.type)
 	{
-	case 0:
-	{
-		SThievesGuildInfo tgi;
-		gs->obtainPlayersStats(tgi, 20);
-		static std::vector<int> statRumorTypes = {208, 209, 210};// 211, 212};
-		std::vector<PlayerColor> players = {};
-		auto statRumorType = *RandomGeneratorUtil::nextItem(statRumorTypes, rand);
-		switch(statRumorType)
-		{
-		case 208:
-			players = tgi.obelisks[0];
-			break;
-
-		case 209:
-			players = tgi.artifacts[0];
-			break;
-
-		case 210:
-			players = tgi.army[0];
-			break;
-
-		case 211:
-			/// TODO: not implemented in obtainPlayersStats
-			players = tgi.income[0];
-			break;
-
-		case 212:
-			/// TODO: Check that ultimate artifact (grail) found
-			break;
-		}
-		auto & playerId = *RandomGeneratorUtil::nextItem(players, rand);
-		std::string playerName = VLC->generaltexth->colors[playerId.getNum()];
-		text = boost::str(boost::format(VLC->generaltexth->allTexts[statRumorType]) % playerName);
-
+	case RumorState::RUMOR_STATS:
+		text = boost::str(boost::format(VLC->generaltexth->allTexts[rumor.first]) % VLC->generaltexth->colors[rumor.second]);
 		break;
-	}
-	case 1:
-		if(gs->map->rumors.size())
-		{
-			auto & mapRumor = *RandomGeneratorUtil::nextItem(gs->map->rumors, rand);
-			text = mapRumor.text;
-			break;
-		}
-
-		/// don't break - if map don't have rumors we show predefined instead
 
-	case 2:
-		do
-		{
-			text = *RandomGeneratorUtil::nextItem(VLC->generaltexth->tavernRumors, rand);
-		}
-		while(!text.length());
+	case RumorState::RUMOR_MAP:
+		text = gs->map->rumors[rumor.first].text;
+		break;
 
+	case RumorState::RUMOR_RAND:
+		text = VLC->generaltexth->tavernRumors[rumor.first];
 		break;
 	}
 

+ 69 - 0
lib/CGameState.cpp

@@ -2133,6 +2133,75 @@ int3 CGameState::guardingCreaturePosition (int3 pos) const
 	return gs->map->guardingCreaturePositions[pos.x][pos.y][pos.z];
 }
 
+void CGameState::updateRumor()
+{
+	static std::vector<RumorState::ERumorType> rumorTypes = {RumorState::RUMOR_MAP, RumorState::RUMOR_STATS, RumorState::RUMOR_RAND, RumorState::RUMOR_RAND};
+	static std::vector<int> statsRumorTypes = {208, 209, 210};// 211, 212};
+
+	int rumorId = -1;
+	int rumorPlayer = PlayerColor::CANNOT_DETERMINE.getNum();
+	auto & rand = gs->getRandomGenerator();
+	rumor.type = *RandomGeneratorUtil::nextItem(rumorTypes, rand);
+	if(!gs->map->rumors.size() && rumor.type == RumorState::RUMOR_MAP)
+		rumor.type = RumorState::RUMOR_RAND;
+
+	switch(rumor.type)
+	{
+	case RumorState::RUMOR_STATS:
+	{
+		SThievesGuildInfo tgi;
+		gs->obtainPlayersStats(tgi, 20);
+		std::vector<PlayerColor> players = {};
+		rumorId = *RandomGeneratorUtil::nextItem(statsRumorTypes, rand);
+		switch(rumorId)
+		{
+		case 208:
+			players = tgi.obelisks[0];
+			break;
+
+		case 209:
+			players = tgi.artifacts[0];
+			break;
+
+		case 210:
+			players = tgi.army[0];
+			break;
+
+		case 211:
+			/// TODO: not implemented in obtainPlayersStats
+			players = tgi.income[0];
+			break;
+
+		case 212:
+			/// TODO: Check that ultimate artifact (grail) found
+			break;
+		}
+		rumorPlayer = RandomGeneratorUtil::nextItem(players, rand)->getNum();
+
+		break;
+	}
+	case RumorState::RUMOR_MAP:
+		rumorId = rand.nextInt(gs->map->rumors.size() - 1);
+
+		break;
+
+	case RumorState::RUMOR_RAND:
+		do
+		{
+			rumorId = rand.nextInt(VLC->generaltexth->tavernRumors.size() - 1);
+		}
+		while(!VLC->generaltexth->tavernRumors[rumorId].length());
+
+		break;
+	}
+
+	if(vstd::contains(rumor.last, rumor.type))
+	{
+		rumor.last.erase(rumor.type);
+	}
+	rumor.last[rumor.type] = std::make_pair(rumorId, rumorPlayer);
+}
+
 bool CGameState::isVisible(int3 pos, PlayerColor player)
 {
 	if(player == PlayerColor::NEUTRAL)

+ 27 - 0
lib/CGameState.h

@@ -208,6 +208,24 @@ public:
 
 };
 
+struct DLL_LINKAGE RumorState
+{
+	enum ERumorType : ui8
+	{
+		RUMOR_NONE = 0, RUMOR_RAND, RUMOR_STATS, RUMOR_MAP
+	};
+
+	ERumorType type;
+	std::map<ERumorType, std::pair<int, int>> last;
+
+	RumorState(){type = RUMOR_NONE; last = {};};
+
+	template <typename Handler> void serialize(Handler &h, const int version)
+	{
+		h & type & last;
+	}
+};
+
 struct UpgradeInfo
 {
 	CreatureID oldID; //creature to be upgraded
@@ -311,6 +329,7 @@ public:
 	std::map<PlayerColor, PlayerState> players;
 	std::map<TeamID, TeamState> teams;
 	CBonusSystemNode globalEffects;
+	RumorState rumor;
 
 	boost::shared_mutex *mx;
 
@@ -324,6 +343,7 @@ public:
 	void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out); //calculates possible paths for hero, by default uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
 	int3 guardingCreaturePosition (int3 pos) const;
 	std::vector<CGObjectInstance*> guardingCreatures (int3 pos) const;
+	void updateRumor();
 
 	// ----- victory, loss condition checks -----
 
@@ -347,6 +367,13 @@ public:
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
 		h & scenarioOps & initialOpts & currentPlayer & day & map & players & teams & hpool & globalEffects & rand;
+		if(version >= 755)
+		{
+			h & rumor;
+		}
+		else
+			rumor = RumorState();
+
 		BONUS_TREE_DESERIALIZATION_FIX
 	}
 

+ 1 - 1
lib/Connection.h

@@ -27,7 +27,7 @@
 #include "mapping/CCampaignHandler.h" //for CCampaignState
 #include "rmg/CMapGenerator.h" // for CMapGenOptions
 
-const ui32 version = 754;
+const ui32 version = 755;
 const ui32 minSupportedVersion = 753;
 
 class CISer;

+ 3 - 0
lib/NetPacksLib.cpp

@@ -1032,6 +1032,9 @@ DLL_LINKAGE void NewTurn::applyGs( CGameState *gs )
 
 	for(CGTownInstance* t : gs->map->towns)
 		t->builded = 0;
+
+	if(gs->getDate(Date::DAY_OF_WEEK) == 1)
+		gs->updateRumor();
 }
 
 DLL_LINKAGE void SetObjectProperty::applyGs( CGameState *gs )