2014-04-06 23:14:26 +03:00
|
|
|
/*
|
2014-06-05 14:19:47 +03:00
|
|
|
* CRewardableObject.cpp, part of VCMI engine
|
2014-04-06 23:14:26 +03:00
|
|
|
*
|
|
|
|
* 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"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "CRewardableObject.h"
|
2024-08-30 16:21:44 +02:00
|
|
|
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "../CPlayerState.h"
|
2024-08-30 16:21:44 +02:00
|
|
|
#include "../GameSettings.h"
|
2023-04-30 02:39:31 +02:00
|
|
|
#include "../IGameCallback.h"
|
2024-08-31 23:04:32 +02:00
|
|
|
#include "../battle/BattleLayout.h"
|
2024-08-30 16:21:44 +02:00
|
|
|
#include "../gameState/CGameState.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../mapObjectConstructors/CObjectClassesHandler.h"
|
2024-07-12 17:10:03 +02:00
|
|
|
#include "../mapObjectConstructors/CRewardableConstructor.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../networkPacks/PacksForClient.h"
|
2024-08-30 16:21:44 +02:00
|
|
|
#include "../networkPacks/PacksForClientBattle.h"
|
2023-09-15 10:06:06 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
2024-08-30 16:21:44 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-09-17 18:02:24 +02:00
|
|
|
void CRewardableObject::grantRewardWithMessage(const CGHeroInstance * contextHero, int index, bool markAsVisit) const
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
auto vi = configuration.info.at(index);
|
|
|
|
logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
|
|
|
|
// show message only if it is not empty or in infobox
|
|
|
|
if (configuration.infoWindowType != EInfoWindowMode::MODAL || !vi.message.toString().empty())
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
2023-09-17 18:02:24 +02:00
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = contextHero->tempOwner;
|
|
|
|
iw.text = vi.message;
|
|
|
|
vi.reward.loadComponents(iw.components, contextHero);
|
|
|
|
iw.type = configuration.infoWindowType;
|
|
|
|
if(!iw.components.empty() || !iw.text.toString().empty())
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
// grant reward afterwards. Note that it may remove object
|
|
|
|
if(markAsVisit)
|
|
|
|
markAsVisited(contextHero);
|
|
|
|
grantReward(index, contextHero);
|
|
|
|
}
|
2023-04-30 01:15:59 +02:00
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
void CRewardableObject::selectRewardWithMessage(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices, const MetaString & dialog) const
|
2023-09-17 18:02:24 +02:00
|
|
|
{
|
2023-09-19 17:11:03 +02:00
|
|
|
BlockingDialog sd(configuration.canRefuse, rewardIndices.size() > 1);
|
2023-09-17 18:02:24 +02:00
|
|
|
sd.player = contextHero->tempOwner;
|
|
|
|
sd.text = dialog;
|
2023-10-16 22:55:37 +02:00
|
|
|
sd.components = loadComponents(contextHero, rewardIndices);
|
2024-09-04 17:14:56 +02:00
|
|
|
cb->showBlockingDialog(this, &sd);
|
2023-10-31 19:21:50 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
void CRewardableObject::grantAllRewardsWithMessage(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices, bool markAsVisit) const
|
2023-10-31 19:21:50 +02:00
|
|
|
{
|
|
|
|
if (rewardIndices.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto index : rewardIndices)
|
|
|
|
{
|
2024-03-14 11:22:15 +02:00
|
|
|
// TODO: Merge all rewards of same type, with single message?
|
2024-03-09 08:38:11 +02:00
|
|
|
grantRewardWithMessage(contextHero, index, false);
|
2023-10-31 19:21:50 +02:00
|
|
|
}
|
2024-03-09 08:38:11 +02:00
|
|
|
// Mark visited only after all rewards were processed
|
|
|
|
if(markAsVisit)
|
|
|
|
markAsVisited(contextHero);
|
2023-10-16 22:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Component> CRewardableObject::loadComponents(const CGHeroInstance * contextHero, const std::vector<ui32> & rewardIndices) const
|
|
|
|
{
|
|
|
|
std::vector<Component> result;
|
2023-09-17 18:02:24 +02:00
|
|
|
|
2023-10-18 16:14:22 +02:00
|
|
|
if (rewardIndices.empty())
|
|
|
|
return result;
|
|
|
|
|
2023-12-10 19:48:44 +02:00
|
|
|
if (configuration.selectMode != Rewardable::SELECT_FIRST && rewardIndices.size() > 1)
|
2023-10-18 16:14:22 +02:00
|
|
|
{
|
2023-09-19 17:11:03 +02:00
|
|
|
for (auto index : rewardIndices)
|
2023-10-16 22:55:37 +02:00
|
|
|
result.push_back(configuration.info.at(index).reward.getDisplayedComponent(contextHero));
|
2023-10-18 16:14:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-16 22:55:37 +02:00
|
|
|
configuration.info.at(rewardIndices.front()).reward.loadComponents(result, contextHero);
|
2023-10-18 16:14:22 +02:00
|
|
|
}
|
2023-04-30 01:15:59 +02:00
|
|
|
|
2023-10-16 22:55:37 +02:00
|
|
|
return result;
|
2023-09-17 18:02:24 +02:00
|
|
|
}
|
2023-04-30 01:15:59 +02:00
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
bool CRewardableObject::guardedPotentially() const
|
|
|
|
{
|
|
|
|
for (auto const & visitInfo : configuration.info)
|
|
|
|
if (!visitInfo.reward.guards.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CRewardableObject::guardedPresently() const
|
|
|
|
{
|
|
|
|
return stacksCount() > 0;
|
|
|
|
}
|
|
|
|
|
2024-08-30 17:24:13 +02:00
|
|
|
void CRewardableObject::onHeroVisit(const CGHeroInstance *hero) const
|
2024-08-30 16:21:44 +02:00
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
if(!wasScouted(hero->getOwner()))
|
|
|
|
{
|
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_SCOUTED, id, hero->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
if (guardedPresently())
|
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
auto guardedIndexes = getAvailableRewards(hero, Rewardable::EEventType::EVENT_GUARDED);
|
2024-08-30 16:21:44 +02:00
|
|
|
auto guardedReward = configuration.info.at(guardedIndexes.at(0));
|
|
|
|
|
|
|
|
// ask player to confirm attack
|
|
|
|
BlockingDialog bd(true, false);
|
2024-08-30 17:24:13 +02:00
|
|
|
bd.player = hero->getOwner();
|
2024-08-30 16:21:44 +02:00
|
|
|
bd.text = guardedReward.message;
|
2024-08-30 17:24:13 +02:00
|
|
|
bd.components = getPopupComponents(hero->getOwner());
|
2024-08-30 16:21:44 +02:00
|
|
|
|
2024-08-31 23:04:32 +02:00
|
|
|
cb->showBlockingDialog(this, &bd);
|
2024-08-30 16:21:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
doHeroVisit(hero);
|
2024-08-30 16:21:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::doHeroVisit(const CGHeroInstance *h) const
|
2023-09-17 18:02:24 +02:00
|
|
|
{
|
2023-04-30 01:15:59 +02:00
|
|
|
if(!wasVisitedBefore(h))
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
2023-04-30 01:15:59 +02:00
|
|
|
bool objectRemovalPossible = false;
|
|
|
|
for(auto index : rewards)
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if(configuration.info.at(index).reward.removeObject)
|
2023-04-30 01:15:59 +02:00
|
|
|
objectRemovalPossible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
|
|
|
|
switch (rewards.size())
|
|
|
|
{
|
|
|
|
case 0: // no available rewards, e.g. visiting School of War without gold
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
|
2023-04-30 01:15:59 +02:00
|
|
|
if (!emptyRewards.empty())
|
2023-09-17 18:02:24 +02:00
|
|
|
grantRewardWithMessage(h, emptyRewards[0], false);
|
2023-04-30 01:15:59 +02:00
|
|
|
else
|
|
|
|
logMod->warn("No applicable message for visiting empty object!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: // one reward. Just give it with message
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.canRefuse)
|
2024-06-24 03:23:26 +02:00
|
|
|
selectRewardWithMessage(h, rewards, configuration.info.at(rewards.front()).message);
|
2023-04-30 01:15:59 +02:00
|
|
|
else
|
2023-09-17 18:02:24 +02:00
|
|
|
grantRewardWithMessage(h, rewards.front(), true);
|
2023-04-30 01:15:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: // multiple rewards. Act according to select mode
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
switch (configuration.selectMode) {
|
2023-04-30 01:15:59 +02:00
|
|
|
case Rewardable::SELECT_PLAYER: // player must select
|
2024-06-24 03:23:26 +02:00
|
|
|
selectRewardWithMessage(h, rewards, configuration.onSelect);
|
2023-04-30 01:15:59 +02:00
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_FIRST: // give first available
|
2023-12-23 00:35:51 +02:00
|
|
|
if (configuration.canRefuse)
|
2024-06-24 03:23:26 +02:00
|
|
|
selectRewardWithMessage(h, { rewards.front() }, configuration.info.at(rewards.front()).message);
|
2023-12-23 00:35:51 +02:00
|
|
|
else
|
|
|
|
grantRewardWithMessage(h, rewards.front(), true);
|
2023-04-30 01:15:59 +02:00
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_RANDOM: // give random
|
2023-12-23 00:35:51 +02:00
|
|
|
{
|
|
|
|
ui32 rewardIndex = *RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator());
|
|
|
|
if (configuration.canRefuse)
|
2024-06-24 03:23:26 +02:00
|
|
|
selectRewardWithMessage(h, { rewardIndex }, configuration.info.at(rewardIndex).message);
|
2023-12-23 00:35:51 +02:00
|
|
|
else
|
|
|
|
grantRewardWithMessage(h, rewardIndex, true);
|
2023-10-31 19:21:50 +02:00
|
|
|
break;
|
2023-12-23 00:35:51 +02:00
|
|
|
}
|
2024-03-09 08:38:11 +02:00
|
|
|
case Rewardable::SELECT_ALL: // grant all possible
|
2024-06-24 03:23:26 +02:00
|
|
|
grantAllRewardsWithMessage(h, rewards, true);
|
2024-03-09 08:38:11 +02:00
|
|
|
break;
|
2023-04-30 01:15:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
if(!objectRemovalPossible && getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, h->id);
|
2023-04-30 01:15:59 +02:00
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logGlobal->debug("Revisiting already visited object");
|
|
|
|
|
2023-10-16 16:55:31 +02:00
|
|
|
if (!wasVisited(h->getOwner()))
|
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, h->id);
|
2023-10-16 16:55:31 +02:00
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED);
|
2023-04-30 01:15:59 +02:00
|
|
|
if (!visitedRewards.empty())
|
2023-09-17 18:02:24 +02:00
|
|
|
grantRewardWithMessage(h, visitedRewards[0], false);
|
2023-04-30 01:15:59 +02:00
|
|
|
else
|
|
|
|
logMod->warn("No applicable message for visiting already visited object!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), this, hero);
|
2023-04-30 01:15:59 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
void CRewardableObject::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
|
|
|
|
{
|
|
|
|
if (result.winner == BattleSide::ATTACKER)
|
|
|
|
{
|
|
|
|
doHeroVisit(hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::blockingDialogAnswered(const CGHeroInstance * hero, int32_t answer) const
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
2024-08-30 16:21:44 +02:00
|
|
|
if(guardedPresently())
|
|
|
|
{
|
|
|
|
if (answer)
|
2024-08-31 23:04:32 +02:00
|
|
|
{
|
|
|
|
auto layout = BattleLayout::createLayout(cb, configuration.guardsLayout, hero, this);
|
|
|
|
cb->startBattle(hero, this, visitablePos(), hero, nullptr, layout, nullptr);
|
|
|
|
}
|
2024-08-30 16:21:44 +02:00
|
|
|
}
|
|
|
|
else
|
2024-08-02 17:03:48 +02:00
|
|
|
{
|
2024-09-15 12:32:29 +02:00
|
|
|
if (answer == 0)
|
|
|
|
return; //Player refused
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
if(answer > 0 && answer - 1 < configuration.info.size())
|
|
|
|
{
|
|
|
|
auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
|
|
|
markAsVisited(hero);
|
|
|
|
grantReward(list[answer - 1], hero);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unhandled choice");
|
|
|
|
}
|
2023-04-30 01:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::markAsVisited(const CGHeroInstance * hero) const
|
|
|
|
{
|
2023-11-06 18:27:16 +02:00
|
|
|
cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, true);
|
2023-04-30 01:15:59 +02:00
|
|
|
|
2024-08-30 17:24:13 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_HERO, id, hero->id);
|
2023-04-30 01:15:59 +02:00
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
|
|
|
|
{
|
2023-11-06 18:27:16 +02:00
|
|
|
cb->setObjPropertyValue(id, ObjProperty::REWARD_SELECT, rewardID);
|
2023-04-30 15:13:07 +02:00
|
|
|
grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
|
2023-04-30 01:15:59 +02:00
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
// hero is not blocked by levelup dialog - grant remainder immediately
|
2023-04-30 01:15:59 +02:00
|
|
|
if(!cb->isVisitCoveredByAnotherQuery(this, hero))
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
grantRewardAfterLevelup(cb, configuration.info.at(rewardID), this, hero);
|
2023-04-30 01:15:59 +02:00
|
|
|
}
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
switch (configuration.visitMode)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_UNLIMITED:
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_ONCE:
|
2023-04-29 23:43:02 +02:00
|
|
|
return onceVisitableObjectCleared;
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_PLAYER:
|
2023-01-24 23:31:07 +02:00
|
|
|
return vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_BONUS:
|
2023-10-21 14:06:18 +02:00
|
|
|
return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_HERO:
|
2023-01-24 23:31:07 +02:00
|
|
|
return contextHero->visitedObjects.count(ObjectInstanceID(id));
|
2023-09-30 20:22:31 +02:00
|
|
|
case Rewardable::VISIT_LIMITER:
|
|
|
|
return configuration.visitLimiter.heroAllowed(contextHero);
|
2023-01-24 23:31:07 +02:00
|
|
|
default:
|
2016-09-16 20:59:07 +02:00
|
|
|
return false;
|
2023-01-24 23:31:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CRewardableObject::wasVisited(PlayerColor player) const
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
switch (configuration.visitMode)
|
2023-01-24 23:31:07 +02:00
|
|
|
{
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_UNLIMITED:
|
|
|
|
case Rewardable::VISIT_BONUS:
|
|
|
|
case Rewardable::VISIT_HERO:
|
2023-09-30 20:22:31 +02:00
|
|
|
case Rewardable::VISIT_LIMITER:
|
2023-01-24 23:31:07 +02:00
|
|
|
return false;
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_ONCE:
|
|
|
|
case Rewardable::VISIT_PLAYER:
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
|
2014-04-06 23:14:26 +03:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 16:55:31 +02:00
|
|
|
bool CRewardableObject::wasScouted(PlayerColor player) const
|
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
|
2023-10-16 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
switch (configuration.visitMode)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_BONUS:
|
2023-10-21 14:06:18 +02:00
|
|
|
return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
|
2023-04-29 22:59:02 +02:00
|
|
|
case Rewardable::VISIT_HERO:
|
2014-06-29 20:13:25 +03:00
|
|
|
return h->visitedObjects.count(ObjectInstanceID(id));
|
2023-09-30 20:22:31 +02:00
|
|
|
case Rewardable::VISIT_LIMITER:
|
2023-10-16 16:55:31 +02:00
|
|
|
return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
|
2014-04-06 23:14:26 +03:00
|
|
|
default:
|
2023-10-16 16:55:31 +02:00
|
|
|
return wasVisited(h->getOwner());
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-10-16 22:55:37 +02:00
|
|
|
std::string result = getObjectName();
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
if (includeDescription && !getDescriptionMessage(player, hero).empty())
|
|
|
|
result += "\n" + getDescriptionMessage(player, hero);
|
2023-10-16 22:55:37 +02:00
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
if (hero)
|
2023-10-16 16:55:31 +02:00
|
|
|
{
|
2023-10-19 13:36:11 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (wasVisited(player))
|
|
|
|
result += "\n" + configuration.visitedTooltip.toString();
|
|
|
|
else
|
|
|
|
result += "\n" + configuration.notVisitedTooltip.toString();
|
|
|
|
}
|
2023-10-16 16:55:31 +02:00
|
|
|
}
|
2023-10-16 22:55:37 +02:00
|
|
|
return result;
|
2014-06-24 20:39:36 +03:00
|
|
|
}
|
2014-04-07 14:56:59 +03:00
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getHoverText(PlayerColor player) const
|
2014-06-24 20:39:36 +03:00
|
|
|
{
|
2023-10-19 13:36:11 +02:00
|
|
|
return getDisplayTextImpl(player, nullptr, false);
|
2023-10-16 22:55:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
|
2023-10-17 15:35:34 +02:00
|
|
|
{
|
2023-10-19 13:36:11 +02:00
|
|
|
return getDisplayTextImpl(hero->getOwner(), hero, false);
|
|
|
|
}
|
2023-10-17 15:35:34 +02:00
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getPopupText(PlayerColor player) const
|
|
|
|
{
|
|
|
|
return getDisplayTextImpl(player, nullptr, true);
|
|
|
|
}
|
2023-10-17 15:35:34 +02:00
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
return getDisplayTextImpl(hero->getOwner(), hero, true);
|
2023-10-17 15:35:34 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
|
2023-10-17 15:35:34 +02:00
|
|
|
{
|
2023-10-19 13:36:11 +02:00
|
|
|
if (!wasScouted(player) || configuration.info.empty())
|
2023-10-17 15:35:34 +02:00
|
|
|
return configuration.description.toString();
|
|
|
|
|
|
|
|
auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
2023-11-12 16:55:42 +02:00
|
|
|
if (rewardIndices.empty() || !configuration.info[0].description.empty())
|
2023-10-17 15:35:34 +02:00
|
|
|
return configuration.info[0].description.toString();
|
|
|
|
|
2023-10-19 14:17:58 +02:00
|
|
|
if (!configuration.info[rewardIndices.front()].description.empty())
|
|
|
|
return configuration.info[rewardIndices.front()].description.toString();
|
|
|
|
|
|
|
|
return configuration.description.toString();
|
2023-10-17 15:35:34 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
|
2023-10-16 22:55:37 +02:00
|
|
|
{
|
|
|
|
if (!wasScouted(player))
|
|
|
|
return {};
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
if (guardedPresently())
|
2024-07-10 16:34:38 +02:00
|
|
|
{
|
2024-08-31 23:04:32 +02:00
|
|
|
if (!cb->getSettings().getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
|
2024-08-30 16:21:44 +02:00
|
|
|
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;
|
2024-07-10 16:34:38 +02:00
|
|
|
}
|
2024-08-30 16:21:44 +02:00
|
|
|
else
|
|
|
|
{
|
2024-08-30 17:24:13 +02:00
|
|
|
if (!configuration.showScoutedPreview)
|
|
|
|
return {};
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
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});
|
|
|
|
}
|
2023-10-16 22:55:37 +02:00
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
if (rewardIndices.empty())
|
|
|
|
return {};
|
2023-10-16 22:55:37 +02:00
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
return loadComponents(hero, rewardIndices);
|
|
|
|
}
|
2023-10-16 22:55:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
|
2023-10-16 22:55:37 +02:00
|
|
|
{
|
2023-10-19 13:36:11 +02:00
|
|
|
return getPopupComponentsImpl(player, nullptr);
|
|
|
|
}
|
2023-10-16 22:55:37 +02:00
|
|
|
|
2023-10-19 13:36:11 +02:00
|
|
|
std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
return getPopupComponentsImpl(hero->getOwner(), hero);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2023-11-06 18:27:16 +02:00
|
|
|
void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case ObjProperty::REWARD_SELECT:
|
2023-11-06 18:27:16 +02:00
|
|
|
selectedReward = identifier.getNum();
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
2023-01-24 23:31:07 +02:00
|
|
|
case ObjProperty::REWARD_CLEARED:
|
2023-11-06 18:27:16 +02:00
|
|
|
onceVisitableObjectCleared = identifier.getNum();
|
2023-01-24 23:31:07 +02:00
|
|
|
break;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CRewardableObject::newTurn(vstd::RNG & rand) const
|
2016-10-09 18:38:13 +02:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
|
2023-01-24 13:54:03 +02:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.resetParameters.rewards)
|
2023-04-29 22:59:02 +02:00
|
|
|
{
|
2024-07-12 17:10:03 +02:00
|
|
|
auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
|
|
|
|
auto newConfiguration = handler->generateConfiguration(cb, rand, ID);
|
|
|
|
cb->setRewardableObjectConfiguration(id, newConfiguration);
|
2023-04-29 22:59:02 +02:00
|
|
|
}
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.resetParameters.visitors)
|
2023-04-29 22:59:02 +02:00
|
|
|
{
|
2023-11-06 18:27:16 +02:00
|
|
|
cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
|
2023-04-29 22:59:02 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
2023-01-24 13:54:03 +02:00
|
|
|
}
|
2016-10-09 18:38:13 +02:00
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CRewardableObject::initObj(vstd::RNG & rand)
|
2022-09-14 20:51:19 +02:00
|
|
|
{
|
2023-10-24 16:11:25 +02:00
|
|
|
getObjectHandler()->configureObject(this, rand);
|
2022-09-14 20:51:19 +02:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CRewardableObject::CRewardableObject(IGameCallback *cb)
|
|
|
|
:CArmedInstance(cb)
|
2014-04-06 23:14:26 +03:00
|
|
|
{}
|
|
|
|
|
2023-09-13 09:18:46 +02:00
|
|
|
void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
|
|
|
|
{
|
2023-09-15 10:06:06 +02:00
|
|
|
CArmedInstance::serializeJsonOptions(handler);
|
|
|
|
handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
|
2023-09-13 09:18:46 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
void CRewardableObject::initializeGuards()
|
|
|
|
{
|
|
|
|
clearSlots();
|
|
|
|
|
2024-09-02 21:15:12 +02:00
|
|
|
// 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());
|
|
|
|
|
2024-08-30 16:21:44 +02:00
|
|
|
for (auto const & visitInfo : configuration.info)
|
|
|
|
{
|
|
|
|
for (auto const & guard : visitInfo.reward.guards)
|
|
|
|
{
|
|
|
|
auto slotID = getFreeSlot();
|
|
|
|
if (!slotID.validSlot())
|
|
|
|
return;
|
|
|
|
|
|
|
|
putStack(slotID, new CStackInstance(guard.getId(), guard.getCount()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:28:58 +02:00
|
|
|
bool CRewardableObject::isCoastVisitable() const
|
|
|
|
{
|
|
|
|
return configuration.coastVisitable;
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|