| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 | 
							- /*
 
-  * CRewardableObject.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "CRewardableObject.h"
 
- #include "../CPlayerState.h"
 
- #include "../IGameSettings.h"
 
- #include "../battle/BattleLayout.h"
 
- #include "../callback/IGameInfoCallback.h"
 
- #include "../callback/IGameEventCallback.h"
 
- #include "../gameState/CGameState.h"
 
- #include "../mapObjectConstructors/AObjectTypeHandler.h"
 
- #include "../mapObjectConstructors/CRewardableConstructor.h"
 
- #include "../mapObjects/CGHeroInstance.h"
 
- #include "../networkPacks/PacksForClient.h"
 
- #include "../networkPacks/PacksForClientBattle.h"
 
- #include "../networkPacks/StackLocation.h"
 
- #include "../serializer/JsonSerializeFormat.h"
 
- #include <vstd/RNG.h>
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- const IObjectInterface * CRewardableObject::getObject() const
 
- {
 
- 	return this;
 
- }
 
- void CRewardableObject::markAsScouted(IGameEventCallback & gameEvents, const CGHeroInstance * hero) const
 
- {
 
- 	ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, hero->id);
 
- 	gameEvents.sendAndApply(cov);
 
- }
 
- bool CRewardableObject::isGuarded() const
 
- {
 
- 	return stacksCount() > 0;
 
- }
 
- void CRewardableObject::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance *hero) const
 
- {
 
- 	if(!wasScouted(hero->getOwner()))
 
- 	{
 
- 		ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_SCOUTED, id, hero->id);
 
- 		gameEvents.sendAndApply(cov);
 
- 	}
 
- 	if (!isGuarded())
 
- 	{
 
- 		doHeroVisit(gameEvents, hero);
 
- 	}
 
- 	else if (configuration.forceCombat)
 
- 	{
 
- 		doStartBattle(gameEvents, hero);
 
- 	}
 
- 	else
 
- 	{
 
- 		auto guardedIndexes = getAvailableRewards(hero, Rewardable::EEventType::EVENT_GUARDED);
 
- 		auto guardedReward = configuration.info.at(guardedIndexes.at(0));
 
- 		// ask player to confirm attack
 
- 		BlockingDialog bd(true, false);
 
- 		bd.player = hero->getOwner();
 
- 		bd.text = guardedReward.message;
 
- 		bd.components = getPopupComponents(hero->getOwner());
 
- 		gameEvents.showBlockingDialog(this, &bd);
 
- 	}
 
- }
 
- void CRewardableObject::heroLevelUpDone(IGameEventCallback & gameEvents, const CGHeroInstance *hero) const
 
- {
 
- 	grantRewardAfterLevelup(gameEvents, configuration.info.at(selectedReward), this, hero);
 
- }
 
- void CRewardableObject::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
 
- {
 
- 	if (result.winner == BattleSide::ATTACKER)
 
- 	{
 
- 		while(!stacks.empty())
 
- 		{
 
- 			logMod->warn("Attacker won, but defender still has remaining units in configurable map objects! Make sure that guards configuration is valid!");
 
- 			gameEvents.eraseStack(StackLocation(id, stacks.begin()->first));
 
- 		}
 
- 		doHeroVisit(gameEvents, hero);
 
- 	}
 
- }
 
- void CRewardableObject::garrisonDialogClosed(IGameEventCallback & gameEvents, const CGHeroInstance *hero) const
 
- {
 
- 	// if visitor received creatures as rewards, but does not have free slots, he will leave some units
 
- 	// inside rewardable object, which might get treated as guards later
 
- 	while(!stacks.empty())
 
- 		gameEvents.eraseStack(StackLocation(id, stacks.begin()->first));
 
- }
 
- void CRewardableObject::doStartBattle(IGameEventCallback & gameEvents, const CGHeroInstance * hero) const
 
- {
 
- 	auto layout = BattleLayout::createLayout(*cb, configuration.guardsLayout, hero, this);
 
- 	gameEvents.startBattle(hero, this, visitablePos(), hero, nullptr, layout, nullptr);
 
- }
 
- void CRewardableObject::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance * hero, int32_t answer) const
 
- {
 
- 	if(isGuarded())
 
- 	{
 
- 		if (answer)
 
- 			doStartBattle(gameEvents, hero);
 
- 	}
 
- 	else
 
- 	{
 
- 		onBlockingDialogAnswered(gameEvents, hero, answer);
 
- 	}
 
- }
 
- void CRewardableObject::markAsVisited(IGameEventCallback & gameEvents, const CGHeroInstance * hero) const
 
- {
 
- 	gameEvents.setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, true);
 
- 	ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_HERO, id, hero->id);
 
- 	gameEvents.sendAndApply(cov);
 
- }
 
- void CRewardableObject::grantReward(IGameEventCallback & gameEvents, ui32 rewardID, const CGHeroInstance * hero) const
 
- {
 
- 	gameEvents.setObjPropertyValue(id, ObjProperty::REWARD_SELECT, rewardID);
 
- 	grantRewardBeforeLevelup(gameEvents, configuration.info.at(rewardID), hero);
 
- 	
 
- 	// hero is not blocked by levelup dialog - grant remainder immediately
 
- 	if(!gameEvents.isVisitCoveredByAnotherQuery(this, hero))
 
- 	{
 
- 		grantRewardAfterLevelup(gameEvents, configuration.info.at(rewardID), this, hero);
 
- 	}
 
- }
 
- bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
 
- {
 
- 	switch (configuration.visitMode)
 
- 	{
 
- 		case Rewardable::VISIT_UNLIMITED:
 
- 			return false;
 
- 		case Rewardable::VISIT_ONCE:
 
- 			return onceVisitableObjectCleared;
 
- 		case Rewardable::VISIT_PLAYER:
 
- 			return cb->getPlayerState(contextHero->getOwner())->visitedObjects.count(ObjectInstanceID(id)) != 0;
 
- 		case Rewardable::VISIT_PLAYER_GLOBAL:
 
- 			return cb->getPlayerState(contextHero->getOwner())->visitedObjectsGlobal.count({ID, subID}) != 0;
 
- 		case Rewardable::VISIT_BONUS:
 
- 			return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
 
- 		case Rewardable::VISIT_HERO:
 
- 			return contextHero->visitedObjects.count(ObjectInstanceID(id));
 
- 		case Rewardable::VISIT_LIMITER:
 
- 			return configuration.visitLimiter.heroAllowed(contextHero);
 
- 		default:
 
- 			return false;
 
- 	}
 
- }
 
- bool CRewardableObject::wasVisited(PlayerColor player) const
 
- {
 
- 	switch (configuration.visitMode)
 
- 	{
 
- 		case Rewardable::VISIT_UNLIMITED:
 
- 		case Rewardable::VISIT_BONUS:
 
- 		case Rewardable::VISIT_HERO:
 
- 		case Rewardable::VISIT_LIMITER:
 
- 			return false;
 
- 		case Rewardable::VISIT_ONCE:
 
- 		case Rewardable::VISIT_PLAYER:
 
- 			return cb->getPlayerState(player)->visitedObjects.count(ObjectInstanceID(id)) != 0;
 
- 		case Rewardable::VISIT_PLAYER_GLOBAL:
 
- 			return cb->getPlayerState(player)->visitedObjectsGlobal.count({ID, subID}) != 0;
 
- 		default:
 
- 			return false;
 
- 	}
 
- }
 
- bool CRewardableObject::wasScouted(PlayerColor player) const
 
- {
 
- 	return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
 
- }
 
- bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
 
- {
 
- 	switch (configuration.visitMode)
 
- 	{
 
- 		case Rewardable::VISIT_BONUS:
 
- 			return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
 
- 		case Rewardable::VISIT_HERO:
 
- 			return h->visitedObjects.count(ObjectInstanceID(id));
 
- 		case Rewardable::VISIT_LIMITER:
 
- 			return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
 
- 		default:
 
- 			return wasVisited(h->getOwner());
 
- 	}
 
- }
 
- std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
 
- {
 
- 	std::string result = getObjectName();
 
- 	if (includeDescription && !getDescriptionMessage(player, hero).empty())
 
- 		result += "\n" + getDescriptionMessage(player, hero);
 
- 	if (hero)
 
- 	{
 
- 		if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
 
- 		{
 
- 			if (wasVisited(hero))
 
- 				result += "\n" + configuration.visitedTooltip.toString();
 
- 			else
 
- 				result += "\n " + configuration.notVisitedTooltip.toString();
 
- 		}
 
- 	}
 
- 	else
 
- 	{
 
- 		if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE || configuration.visitMode == Rewardable::VISIT_PLAYER_GLOBAL)
 
- 		{
 
- 			if (wasVisited(player))
 
- 				result += "\n" + configuration.visitedTooltip.toString();
 
- 			else
 
- 				result += "\n" + configuration.notVisitedTooltip.toString();
 
- 		}
 
- 	}
 
- 	return result;
 
- }
 
- std::string CRewardableObject::getHoverText(PlayerColor player) const
 
- {
 
- 	return getDisplayTextImpl(player, nullptr, false);
 
- }
 
- std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
 
- {
 
- 	return getDisplayTextImpl(hero->getOwner(), hero, false);
 
- }
 
- std::string CRewardableObject::getPopupText(PlayerColor player) const
 
- {
 
- 	return getDisplayTextImpl(player, nullptr, true);
 
- }
 
- std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
 
- {
 
- 	return getDisplayTextImpl(hero->getOwner(), hero, true);
 
- }
 
- std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
 
- {
 
- 	if (!wasScouted(player) || configuration.info.empty())
 
- 		return configuration.description.toString();
 
- 	auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
 
- 	if (rewardIndices.empty() || !configuration.info[0].description.empty())
 
- 		return configuration.info[0].description.toString();
 
- 	if (!configuration.info[rewardIndices.front()].description.empty())
 
- 		return configuration.info[rewardIndices.front()].description.toString();
 
- 	return configuration.description.toString();
 
- }
 
- std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
 
- {
 
- 	if (!wasScouted(player))
 
- 		return {};
 
- 	if (isGuarded())
 
- 	{
 
- 		if (!cb->getSettings().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(ComponentType::CREATURE, guard.first, guard.second);
 
- 			result.push_back(comp);
 
- 		}
 
- 		return result;
 
- 	}
 
- 	else
 
- 	{
 
- 		if (!configuration.showScoutedPreview)
 
- 			return {};
 
- 		auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
 
- 		if (rewardIndices.empty() && !configuration.info.empty())
 
- 		{
 
- 			// Object has valid config, but current hero has no rewards that he can receive.
 
- 			// Usually this happens if hero has already visited this object -> show reward using context without any hero
 
- 			// since reward may be context-sensitive - e.g. Witch Hut that gives 1 skill, but always at basic level
 
- 			return loadComponents(nullptr, {0});
 
- 		}
 
- 		if (rewardIndices.empty())
 
- 			return {};
 
- 		return loadComponents(hero, rewardIndices);
 
- 	}
 
- }
 
- std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
 
- {
 
- 	return getPopupComponentsImpl(player, nullptr);
 
- }
 
- std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
 
- {
 
- 	return getPopupComponentsImpl(hero->getOwner(), hero);
 
- }
 
- void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
 
- {
 
- 	switch (what)
 
- 	{
 
- 		case ObjProperty::REWARD_SELECT:
 
- 			selectedReward = identifier.getNum();
 
- 			break;
 
- 		case ObjProperty::REWARD_CLEARED:
 
- 			onceVisitableObjectCleared = identifier.getNum();
 
- 			break;
 
- 	}
 
- }
 
- void CRewardableObject::newTurn(IGameEventCallback & gameEvents, IGameRandomizer & gameRandomizer) const
 
- {
 
- 	if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
 
- 	{
 
- 		if (configuration.resetParameters.rewards)
 
- 		{
 
- 			auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
 
- 			auto newConfiguration = handler->generateConfiguration(cb, gameRandomizer, ID, configuration.variables.preset);
 
- 			gameEvents.setRewardableObjectConfiguration(id, newConfiguration);
 
- 		}
 
- 		if (configuration.resetParameters.visitors)
 
- 		{
 
- 			gameEvents.setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
 
- 			ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
 
- 			gameEvents.sendAndApply(cov);
 
- 		}
 
- 	}
 
- }
 
- void CRewardableObject::initObj(IGameRandomizer & gameRandomizer)
 
- {
 
- 	getObjectHandler()->configureObject(this, gameRandomizer);
 
- }
 
- CRewardableObject::CRewardableObject(IGameInfoCallback *cb)
 
- 	:CArmedInstance(cb)
 
- {}
 
- void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
 
- {
 
- 	CArmedInstance::serializeJsonOptions(handler);
 
- 	handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
 
- }
 
- void CRewardableObject::initializeGuards()
 
- {
 
- 	clearSlots();
 
- 	// Workaround for default creature banks strings that has placeholder for object name
 
- 	// TODO: find better location for this code
 
- 	for (auto & visitInfo : configuration.info)
 
- 		visitInfo.message.replaceRawString(getObjectName());
 
- 	for (auto const & visitInfo : configuration.info)
 
- 	{
 
- 		for (auto const & guard : visitInfo.reward.guards)
 
- 		{
 
- 			auto slotID = getFreeSlot();
 
- 			if (!slotID.validSlot())
 
- 				return;
 
- 			putStack(slotID, std::make_unique<CStackInstance>(cb, guard.getId(), guard.getCount()));
 
- 		}
 
- 	}
 
- }
 
- bool CRewardableObject::isCoastVisitable() const
 
- {
 
- 	return configuration.coastVisitable;
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |