Browse Source

Show preview for Refugee Camp

VCMI will now show preview for Refugee Camp similar to dwelling preview
that show available creatures if player have visited Refugee Camp on
this week, and no other player have visited the Refugee Camp since then
Ivan Savenko 4 months ago
parent
commit
de051cbd46
2 changed files with 42 additions and 23 deletions
  1. 41 23
      lib/mapObjects/CGDwelling.cpp
  2. 1 0
      lib/mapObjects/CGDwelling.h

+ 41 - 23
lib/mapObjects/CGDwelling.cpp

@@ -213,15 +213,27 @@ void CGDwelling::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
 
 
 void CGDwelling::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
 void CGDwelling::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
 {
 {
-	if(ID == Obj::REFUGEE_CAMP && !creatures[0].first) //Refugee Camp, no available cres
+	if(ID == Obj::REFUGEE_CAMP)
 	{
 	{
-		InfoWindow iw;
-		iw.type = EInfoWindowMode::AUTO;
-		iw.player = h->tempOwner;
-		iw.text.appendLocalString(EMetaText::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted.  Perhaps you should try next week.
-		iw.text.replaceName(ID, subID);
-		gameEvents.sendAndApply(iw);
-		return;
+		ChangeObjectVisitors cow;
+		cow.mode = ChangeObjectVisitors::VISITOR_CLEAR;
+		cow.object = id;
+		gameEvents.sendAndApply(cow);
+
+		cow.mode = ChangeObjectVisitors::VISITOR_ADD_PLAYER;
+		cow.hero = h->id;
+		gameEvents.sendAndApply(cow);
+
+		if (!creatures[0].first) //Refugee Camp, no available cres
+		{
+			InfoWindow iw;
+			iw.type = EInfoWindowMode::AUTO;
+			iw.player = h->tempOwner;
+			iw.text.appendLocalString(EMetaText::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted.  Perhaps you should try next week.
+			iw.text.replaceName(ID, subID);
+			gameEvents.sendAndApply(iw);
+			return;
+		}
 	}
 	}
 
 
 	PlayerRelations relations = cb->getPlayerRelations( h->tempOwner, tempOwner );
 	PlayerRelations relations = cb->getPlayerRelations( h->tempOwner, tempOwner );
@@ -290,6 +302,11 @@ void CGDwelling::newTurn(IGameEventCallback & gameEvents, IGameRandomizer & game
 
 
 	if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
 	if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
 	{
 	{
+		ChangeObjectVisitors cow;
+		cow.mode = ChangeObjectVisitors::VISITOR_CLEAR;
+		cow.object = id;
+		gameEvents.sendAndApply(cow);
+
 		gameEvents.setObjPropertyID(id, ObjProperty::AVAILABLE_CREATURE, gameRandomizer.rollCreature());
 		gameEvents.setObjPropertyID(id, ObjProperty::AVAILABLE_CREATURE, gameRandomizer.rollCreature());
 	}
 	}
 
 
@@ -325,33 +342,34 @@ void CGDwelling::newTurn(IGameEventCallback & gameEvents, IGameRandomizer & game
 	updateGuards(gameEvents);
 	updateGuards(gameEvents);
 }
 }
 
 
+bool CGDwelling::wasVisited (PlayerColor player) const
+{
+	return cb->getPlayerState(player)->visitedObjects.count(id) != 0;
+}
+
 std::vector<Component> CGDwelling::getPopupComponents(PlayerColor player) const
 std::vector<Component> CGDwelling::getPopupComponents(PlayerColor player) const
 {
 {
 	bool visitedByOwner = getOwner() == player;
 	bool visitedByOwner = getOwner() == player;
+	bool isDwelling = ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4;
+	bool isRefugeeCamp = ID == Obj::REFUGEE_CAMP;
+	bool canSeeUnitTypes = isDwelling || (isRefugeeCamp && wasVisited(player));
+	bool canSeeUnitAmount = (isDwelling && visitedByOwner) || (isRefugeeCamp && wasVisited(player));
 
 
 	std::vector<Component> result;
 	std::vector<Component> result;
 
 
-	if (ID == Obj::CREATURE_GENERATOR1 && !creatures.empty())
-	{
-		for (auto const & creature : creatures.front().second)
-		{
-			if (visitedByOwner)
-				result.emplace_back(ComponentType::CREATURE, creature, creatures.front().first);
-			else
-				result.emplace_back(ComponentType::CREATURE, creature);
-		}
-	}
+	if (creatures.empty())
+		return result;
 
 
-	if (ID == Obj::CREATURE_GENERATOR4)
+	if (canSeeUnitTypes)
 	{
 	{
 		for (auto const & creatureLevel : creatures)
 		for (auto const & creatureLevel : creatures)
 		{
 		{
-			if (!creatureLevel.second.empty())
+			for (auto const & creature : creatureLevel.second)
 			{
 			{
-				if (visitedByOwner)
-					result.emplace_back(ComponentType::CREATURE, creatureLevel.second.back(), creatureLevel.first);
+				if (canSeeUnitAmount)
+					result.emplace_back(ComponentType::CREATURE, creature, creatures.front().first);
 				else
 				else
-					result.emplace_back(ComponentType::CREATURE, creatureLevel.second.back());
+					result.emplace_back(ComponentType::CREATURE, creature);
 			}
 			}
 		}
 		}
 	}
 	}

+ 1 - 0
lib/mapObjects/CGDwelling.h

@@ -61,6 +61,7 @@ private:
 	void battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const override;
 	void battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const override;
 	void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
 	void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
 	std::vector<Component> getPopupComponents(PlayerColor player) const override;
 	std::vector<Component> getPopupComponents(PlayerColor player) const override;
+	bool wasVisited (PlayerColor player) const override;
 
 
 	void updateGuards(IGameEventCallback & gameEvents) const;
 	void updateGuards(IGameEventCallback & gameEvents) const;
 	void heroAcceptsCreatures(IGameEventCallback & gameEvents, const CGHeroInstance *h) const;
 	void heroAcceptsCreatures(IGameEventCallback & gameEvents, const CGHeroInstance *h) const;