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"
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../gameState/CGameState.h"
|
2023-04-30 02:39:31 +02:00
|
|
|
#include "../CGeneralTextHandler.h"
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "../CPlayerState.h"
|
2023-04-30 02:39:31 +02:00
|
|
|
#include "../IGameCallback.h"
|
|
|
|
#include "../NetPacks.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../mapObjectConstructors/CObjectClassesHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-04-29 22:59:02 +02:00
|
|
|
// FIXME: copy-pasted from CObjectHandler
|
|
|
|
static std::string visitedTxt(const bool visited)
|
|
|
|
{
|
|
|
|
int id = visited ? 352 : 353;
|
|
|
|
return VLC->generaltexth->allTexts[id];
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:15:59 +02:00
|
|
|
void CRewardableObject::onHeroVisit(const CGHeroInstance *h) const
|
|
|
|
{
|
|
|
|
auto grantRewardWithMessage = [&](int index, bool markAsVisit) -> void
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
auto vi = configuration.info.at(index);
|
2023-04-30 01:15:59 +02:00
|
|
|
logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
|
|
|
|
// show message only if it is not empty or in infobox
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.infoWindowType != EInfoWindowMode::MODAL || !vi.message.toString().empty())
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
|
|
|
iw.text = vi.message;
|
|
|
|
vi.reward.loadComponents(iw.components, h);
|
2023-04-30 15:13:07 +02:00
|
|
|
iw.type = configuration.infoWindowType;
|
2023-04-30 01:15:59 +02:00
|
|
|
if(!iw.components.empty() || !iw.text.toString().empty())
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
// grant reward afterwards. Note that it may remove object
|
|
|
|
if(markAsVisit)
|
|
|
|
markAsVisited(h);
|
|
|
|
grantReward(index, h);
|
|
|
|
};
|
|
|
|
auto selectRewardsMessage = [&](const std::vector<ui32> & rewards, const MetaString & dialog) -> void
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
BlockingDialog sd(configuration.canRefuse, rewards.size() > 1);
|
2023-04-30 01:15:59 +02:00
|
|
|
sd.player = h->tempOwner;
|
|
|
|
sd.text = dialog;
|
|
|
|
|
|
|
|
if (rewards.size() > 1)
|
|
|
|
for (auto index : rewards)
|
2023-04-30 15:13:07 +02:00
|
|
|
sd.components.push_back(configuration.info.at(index).reward.getDisplayedComponent(h));
|
2023-04-30 01:15:59 +02:00
|
|
|
|
|
|
|
if (rewards.size() == 1)
|
2023-04-30 15:13:07 +02:00
|
|
|
configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h);
|
2023-04-30 01:15:59 +02:00
|
|
|
|
|
|
|
cb->showBlockingDialog(&sd);
|
|
|
|
};
|
|
|
|
|
|
|
|
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())
|
|
|
|
grantRewardWithMessage(emptyRewards[0], false);
|
|
|
|
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)
|
|
|
|
selectRewardsMessage(rewards, configuration.info.at(rewards.front()).message);
|
2023-04-30 01:15:59 +02:00
|
|
|
else
|
|
|
|
grantRewardWithMessage(rewards.front(), true);
|
|
|
|
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
|
2023-04-30 15:13:07 +02:00
|
|
|
selectRewardsMessage(rewards, configuration.onSelect);
|
2023-04-30 01:15:59 +02:00
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_FIRST: // give first available
|
|
|
|
grantRewardWithMessage(rewards.front(), true);
|
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_RANDOM: // give random
|
|
|
|
grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()), true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_TEAM, id, h->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logGlobal->debug("Revisiting already visited object");
|
|
|
|
|
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())
|
|
|
|
grantRewardWithMessage(visitedRewards[0], false);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
|
|
|
|
{
|
|
|
|
if(answer == 0)
|
|
|
|
return; // player refused
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
if(answer > 0 && answer-1 < configuration.info.size())
|
2023-04-30 01:15:59 +02:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
2023-04-30 01:15:59 +02:00
|
|
|
markAsVisited(hero);
|
|
|
|
grantReward(list[answer - 1], hero);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unhandled choice");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::markAsVisited(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_CLEARED, true);
|
|
|
|
|
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD, id, hero->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
cb->setObjProperty(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
|
|
|
|
|
|
|
// hero is not blocked by levelup dialog - grant remainer immediately
|
|
|
|
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-05-01 00:20:01 +02:00
|
|
|
return contextHero->hasBonusFrom(BonusSource::OBJECT, 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));
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-01 00:20:01 +02:00
|
|
|
return h->hasBonusFrom(BonusSource::OBJECT, 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));
|
2014-04-06 23:14:26 +03:00
|
|
|
default:
|
|
|
|
return wasVisited(h->tempOwner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
std::string CRewardableObject::getHoverText(PlayerColor player) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE)
|
2014-06-24 20:39:36 +03:00
|
|
|
return getObjectName() + " " + visitedTxt(wasVisited(player));
|
|
|
|
return getObjectName();
|
|
|
|
}
|
2014-04-07 14:56:59 +03:00
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
|
2014-06-24 20:39:36 +03:00
|
|
|
return getObjectName() + " " + visitedTxt(wasVisited(hero));
|
|
|
|
return getObjectName();
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::setPropertyDer(ui8 what, ui32 val)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
2023-01-24 13:54:03 +02:00
|
|
|
case ObjProperty::REWARD_RANDOMIZE:
|
|
|
|
initObj(cb->gameState()->getRandomGenerator());
|
|
|
|
break;
|
2014-04-06 23:14:26 +03:00
|
|
|
case ObjProperty::REWARD_SELECT:
|
2023-04-29 23:43:02 +02:00
|
|
|
selectedReward = val;
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
2023-01-24 23:31:07 +02:00
|
|
|
case ObjProperty::REWARD_CLEARED:
|
2023-04-29 23:43:02 +02:00
|
|
|
onceVisitableObjectCleared = val;
|
2023-01-24 23:31:07 +02:00
|
|
|
break;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:59:02 +02:00
|
|
|
void CRewardableObject::newTurn(CRandomGenerator & 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
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_RANDOMIZE, 0);
|
|
|
|
}
|
2023-04-30 15:13:07 +02:00
|
|
|
if (configuration.resetParameters.visitors)
|
2023-04-29 22:59:02 +02:00
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_CLEARED, false);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-14 20:51:19 +02:00
|
|
|
void CRewardableObject::initObj(CRandomGenerator & rand)
|
|
|
|
{
|
|
|
|
VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, rand);
|
2023-04-30 15:13:07 +02:00
|
|
|
assert(!configuration.info.empty());
|
2022-09-14 20:51:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 22:59:02 +02:00
|
|
|
CRewardableObject::CRewardableObject()
|
2014-04-06 23:14:26 +03:00
|
|
|
{}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|