Browse Source

Show guards preview for visited banks on right click

Ivan Savenko 2 years ago
parent
commit
4c0eabf20c
3 changed files with 40 additions and 23 deletions
  1. 37 22
      lib/mapObjects/CBank.cpp
  2. 2 0
      lib/mapObjects/CBank.h
  3. 1 1
      lib/mapObjects/CGObjectInstance.cpp

+ 37 - 22
lib/mapObjects/CBank.cpp

@@ -18,6 +18,7 @@
 #include "../CGeneralTextHandler.h"
 #include "../CSoundBase.h"
 #include "../GameSettings.h"
+#include "../CPlayerState.h"
 #include "../mapObjectConstructors/CObjectClassesHandler.h"
 #include "../mapObjectConstructors/CBankInstanceConstructor.h"
 #include "../IGameCallback.h"
@@ -51,8 +52,37 @@ bool CBank::isCoastVisitable() const
 
 std::string CBank::getHoverText(PlayerColor player) const
 {
-	// TODO: record visited players
-	return getObjectName() + " " + visitedTxt(bc == nullptr);
+	if (!wasVisited(player))
+		return getObjectName();
+
+	return getObjectName() + "\n" + visitedTxt(bc == nullptr);
+}
+
+std::vector<Component> CBank::getPopupComponents(PlayerColor player) const
+{
+	if (!wasVisited(player))
+		return {};
+
+	if (!VLC->settings()->getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
+		return {};
+
+	std::map<CreatureID, int> guardsAmounts;
+	std::vector<Component> result;
+
+	for (auto const & slot : Slots())
+		if (slot.second)
+			guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
+
+	for (auto const & guard : guardsAmounts)
+	{
+		Component comp;
+		comp.id = Component::EComponentType::CREATURE;
+		comp.subtype = guard.first.getNum();
+		comp.val = guard.second;
+
+		result.push_back(comp);
+	}
+	return result;
 }
 
 void CBank::setConfig(const BankConfig & config)
@@ -98,11 +128,14 @@ void CBank::newTurn(CRandomGenerator & rand) const
 
 bool CBank::wasVisited (PlayerColor player) const
 {
-	return !bc; //FIXME: player A should not know about visit done by player B
+	return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
 }
 
 void CBank::onHeroVisit(const CGHeroInstance * h) const
 {
+	ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_TEAM, id, h->id);
+	cb->sendAndApply(&cov);
+
 	int banktext = 0;
 	switch (ID)
 	{
@@ -130,28 +163,10 @@ void CBank::onHeroVisit(const CGHeroInstance * h) const
 	bd.player = h->getOwner();
 	bd.soundID = soundBase::invalid; // Sound is handled in json files, else two sounds are played
 	bd.text.appendLocalString(EMetaText::ADVOB_TXT, banktext);
+	bd.components = getPopupComponents(h->getOwner());
 	if (banktext == 32)
 		bd.text.replaceRawString(getObjectName());
 
-	if (VLC->settings()->getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
-	{
-		std::map<CreatureID, int> guardsAmounts;
-
-		for (auto const & slot : Slots())
-			if (slot.second)
-				guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
-
-		for (auto const & guard : guardsAmounts)
-		{
-			Component comp;
-			comp.id = Component::EComponentType::CREATURE;
-			comp.subtype = guard.first.getNum();
-			comp.val = guard.second;
-
-			bd.components.push_back(comp);
-		}
-	}
-
 	cb->showBlockingDialog(&bd);
 }
 

+ 2 - 0
lib/mapObjects/CBank.h

@@ -41,6 +41,8 @@ public:
 	void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
 	void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
 
+	std::vector<Component> getPopupComponents(PlayerColor player) const override;
+
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
 		h & static_cast<CArmedInstance&>(*this);

+ 1 - 1
lib/mapObjects/CGObjectInstance.cpp

@@ -287,7 +287,7 @@ std::vector<Component> CGObjectInstance::getPopupComponents(PlayerColor player)
 
 std::vector<Component> CGObjectInstance::getPopupComponents(const CGHeroInstance * hero) const
 {
-	return {};
+	return getPopupComponents(hero->getOwner());
 }
 
 void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const