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"
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../CHeroHandler.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2014-06-05 23:51:24 +03:00
|
|
|
#include "../CSoundBase.h"
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../NetPacks.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../IGameCallback.h"
|
|
|
|
#include "../CGameState.h"
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "../CPlayerState.h"
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "CObjectClassesHandler.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-04-06 23:14:26 +03:00
|
|
|
bool CRewardLimiter::heroAllowed(const CGHeroInstance * hero) const
|
|
|
|
{
|
2016-02-01 08:41:15 +02:00
|
|
|
if(dayOfWeek != 0)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
if (IObjectInterface::cb->getDate(Date::DAY_OF_WEEK) != dayOfWeek)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(auto & reqStack : creatures)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for (auto slot : hero->Slots())
|
|
|
|
{
|
|
|
|
const CStackInstance * heroStack = slot.second;
|
|
|
|
if (heroStack->type == reqStack.type)
|
|
|
|
count += heroStack->count;
|
|
|
|
}
|
|
|
|
if (count < reqStack.count) //not enough creatures of this kind
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if(!IObjectInterface::cb->getPlayerState(hero->tempOwner)->resources.canAfford(resources))
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
if(minLevel > (si32)hero->level)
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
|
|
|
|
2023-01-23 23:54:54 +02:00
|
|
|
if(manaPoints > hero->mana)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(manaPercentage > 100 * hero->mana / hero->manaLimit())
|
|
|
|
return false;
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(size_t i=0; i<primary.size(); i++)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-04-07 20:32:15 +03:00
|
|
|
if (primary[i] > hero->getPrimSkillLevel(PrimarySkill::PrimarySkill(i)))
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(auto & skill : secondary)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-04-07 20:32:15 +03:00
|
|
|
if (skill.second > hero->getSecSkillLevel(skill.first))
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(auto & art : artifacts)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
if (!hero->hasArt(art))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-23 23:54:54 +02:00
|
|
|
for(auto & sublimiter : noneOf)
|
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto & sublimiter : allOf)
|
|
|
|
{
|
|
|
|
if (!sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( anyOf.size() == 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for(auto & sublimiter : anyOf)
|
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
std::vector<ui32> CRewardableObject::getAvailableRewards(const CGHeroInstance * hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
std::vector<ui32> ret;
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(size_t i=0; i<info.size(); i++)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
const CVisitInfo & visit = info[i];
|
|
|
|
|
2023-01-23 23:54:54 +02:00
|
|
|
if((visit.numOfGrantsAllowed == 0 || visit.numOfGrantsPerformed < visit.numOfGrantsAllowed) // reward has unlimited uses or some are still available
|
2014-06-29 20:13:25 +03:00
|
|
|
&& visit.limiter.heroAllowed(hero))
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("Reward %d is allowed", i);
|
2020-10-01 10:38:06 +02:00
|
|
|
ret.push_back(static_cast<ui32>(i));
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-30 22:51:21 +02:00
|
|
|
CVisitInfo CRewardableObject::getVisitInfo(int index, const CGHeroInstance *) const
|
|
|
|
{
|
|
|
|
return info[index];
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::onHeroVisit(const CGHeroInstance *h) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-04-11 20:38:27 +03:00
|
|
|
auto grantRewardWithMessage = [&](int index) -> void
|
|
|
|
{
|
2016-01-30 22:51:21 +02:00
|
|
|
auto vi = getVisitInfo(index, h);
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
|
2014-04-11 22:49:25 +03:00
|
|
|
// show message only if it is not empty
|
2016-01-30 22:51:21 +02:00
|
|
|
if (!vi.message.toString().empty())
|
2014-04-11 22:49:25 +03:00
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
2016-01-30 22:51:21 +02:00
|
|
|
iw.text = vi.message;
|
|
|
|
vi.reward.loadComponents(iw.components, h);
|
2014-04-11 22:49:25 +03:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
2014-06-28 17:19:53 +03:00
|
|
|
// grant reward afterwards. Note that it may remove object
|
|
|
|
grantReward(index, h);
|
2014-04-11 20:38:27 +03:00
|
|
|
};
|
2023-01-23 15:27:01 +02:00
|
|
|
auto selectRewardsMessage = [&](std::vector<ui32> rewards, const MetaString & dialog) -> void
|
2014-04-12 14:47:20 +03:00
|
|
|
{
|
|
|
|
BlockingDialog sd(canRefuse, rewards.size() > 1);
|
|
|
|
sd.player = h->tempOwner;
|
2023-01-23 15:27:01 +02:00
|
|
|
sd.text = dialog;
|
2014-04-12 14:47:20 +03:00
|
|
|
for (auto index : rewards)
|
2016-01-30 22:51:21 +02:00
|
|
|
sd.components.push_back(getVisitInfo(index, h).reward.getDisplayedComponent(h));
|
2014-04-12 14:47:20 +03:00
|
|
|
cb->showBlockingDialog(&sd);
|
|
|
|
};
|
2014-04-11 20:38:27 +03:00
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(!wasVisited(h))
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
auto rewards = getAvailableRewards(h);
|
2016-09-23 10:47:07 +02:00
|
|
|
bool objectRemovalPossible = false;
|
|
|
|
for(auto index : rewards)
|
|
|
|
{
|
|
|
|
if(getVisitInfo(index, h).reward.removeObject)
|
|
|
|
objectRemovalPossible = true;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
|
2014-04-06 23:14:26 +03:00
|
|
|
switch (rewards.size())
|
|
|
|
{
|
2014-04-11 20:38:27 +03:00
|
|
|
case 0: // no available rewards, e.g. empty flotsam
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
2014-04-11 22:49:25 +03:00
|
|
|
if (!onEmpty.toString().empty())
|
|
|
|
iw.text = onEmpty;
|
|
|
|
else
|
|
|
|
iw.text = onVisited;
|
2014-04-06 23:14:26 +03:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: // one reward. Just give it with message
|
|
|
|
{
|
2014-04-12 14:47:20 +03:00
|
|
|
if (canRefuse)
|
2023-01-23 15:27:01 +02:00
|
|
|
selectRewardsMessage(rewards, getVisitInfo(rewards[0], h).message);
|
2014-04-12 14:47:20 +03:00
|
|
|
else
|
|
|
|
grantRewardWithMessage(rewards[0]);
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
|
|
|
}
|
2014-04-11 20:38:27 +03:00
|
|
|
default: // multiple rewards. Act according to select mode
|
|
|
|
{
|
|
|
|
switch (selectMode) {
|
|
|
|
case SELECT_PLAYER: // player must select
|
2023-01-23 15:27:01 +02:00
|
|
|
selectRewardsMessage(rewards, onSelect);
|
2014-04-11 20:38:27 +03:00
|
|
|
break;
|
|
|
|
case SELECT_FIRST: // give first available
|
|
|
|
grantRewardWithMessage(rewards[0]);
|
|
|
|
break;
|
2014-05-24 01:56:51 +03:00
|
|
|
case SELECT_RANDOM: // select one randomly //TODO: use weights
|
2020-10-01 10:38:06 +02:00
|
|
|
grantRewardWithMessage(rewards[CRandomGenerator::getDefault().nextInt((int)rewards.size()-1)]);
|
2014-04-11 20:38:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-09-20 22:05:44 +02:00
|
|
|
break;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 22:05:44 +02:00
|
|
|
|
2016-09-23 10:47:07 +02:00
|
|
|
if(!objectRemovalPossible && getAvailableRewards(h).size() == 0)
|
2016-09-20 22:05:44 +02:00
|
|
|
{
|
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_TEAM, id, h->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->debug("Revisiting already visited object");
|
2014-04-06 23:14:26 +03:00
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
2014-04-11 22:49:25 +03:00
|
|
|
if (!onVisited.toString().empty())
|
|
|
|
iw.text = onVisited;
|
|
|
|
else
|
|
|
|
iw.text = onEmpty;
|
2014-04-06 23:14:26 +03:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2016-01-30 22:51:21 +02:00
|
|
|
grantRewardAfterLevelup(getVisitInfo(selectedReward, hero), hero);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2016-02-01 08:41:15 +02:00
|
|
|
if(answer == 0)
|
2014-04-11 20:38:27 +03:00
|
|
|
return; // player refused
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(answer > 0 && answer-1 < info.size())
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
auto list = getAvailableRewards(hero);
|
2014-04-11 20:38:27 +03:00
|
|
|
grantReward(list[answer - 1], hero);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unhandled choice");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::onRewardGiven(const CGHeroInstance * hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
// no implementation, virtual function for overrides
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
|
2014-04-11 20:38:27 +03:00
|
|
|
{
|
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD, id, hero->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_SELECT, rewardID);
|
|
|
|
|
2016-01-30 22:51:21 +02:00
|
|
|
grantRewardBeforeLevelup(getVisitInfo(rewardID, hero), hero);
|
2014-04-11 20:38:27 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::grantRewardBeforeLevelup(const CVisitInfo & info, const CGHeroInstance * hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
assert(hero);
|
|
|
|
assert(hero->tempOwner.isValidPlayer());
|
|
|
|
assert(stacks.empty());
|
|
|
|
assert(info.reward.creatures.size() <= GameConstants::ARMY_SIZE);
|
|
|
|
assert(!cb->isVisitCoveredByAnotherQuery(this, hero));
|
|
|
|
|
|
|
|
cb->giveResources(hero->tempOwner, info.reward.resources);
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(auto & entry : info.reward.secondary)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
int current = hero->getSecSkillLevel(entry.first);
|
|
|
|
if( (current != 0 && current < entry.second) ||
|
|
|
|
(hero->canLearnSkill() ))
|
|
|
|
{
|
|
|
|
cb->changeSecSkill(hero, entry.first, entry.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0; i< info.reward.primary.size(); i++)
|
|
|
|
if(info.reward.primary[i] > 0)
|
|
|
|
cb->changePrimSkill(hero, static_cast<PrimarySkill::PrimarySkill>(i), info.reward.primary[i], false);
|
|
|
|
|
|
|
|
si64 expToGive = 0;
|
|
|
|
expToGive += VLC->heroh->reqExp(hero->level+info.reward.gainedLevels) - VLC->heroh->reqExp(hero->level);
|
|
|
|
expToGive += hero->calculateXp(info.reward.gainedExp);
|
2016-02-01 08:41:15 +02:00
|
|
|
if(expToGive)
|
2014-04-06 23:14:26 +03:00
|
|
|
cb->changePrimSkill(hero, PrimarySkill::EXPERIENCE, expToGive);
|
2014-04-12 16:47:48 +03:00
|
|
|
|
|
|
|
// hero is not blocked by levelup dialog - grant remainer immediately
|
2016-02-01 08:41:15 +02:00
|
|
|
if(!cb->isVisitCoveredByAnotherQuery(this, hero))
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
grantRewardAfterLevelup(info, hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::grantRewardAfterLevelup(const CVisitInfo & info, const CGHeroInstance * hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2016-02-01 08:41:15 +02:00
|
|
|
if(info.reward.manaDiff || info.reward.manaPercentage >= 0)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-22 02:08:58 +02:00
|
|
|
si32 manaScaled = hero->mana;
|
2014-04-06 23:14:26 +03:00
|
|
|
if (info.reward.manaPercentage >= 0)
|
2023-01-22 02:08:58 +02:00
|
|
|
manaScaled = hero->manaLimit() * info.reward.manaPercentage / 100;
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-01-23 15:27:01 +02:00
|
|
|
si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
|
2023-01-22 02:08:58 +02:00
|
|
|
si32 manaGranted = std::min(manaMissing, info.reward.manaDiff);
|
|
|
|
si32 manaOverflow = info.reward.manaDiff - manaGranted;
|
|
|
|
si32 manaOverLimit = manaOverflow * info.reward.manaOverflowFactor / 100;
|
|
|
|
si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
|
|
|
|
|
|
|
|
cb->setManaPoints(hero->id, manaOutput);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(info.reward.movePoints || info.reward.movePercentage >= 0)
|
|
|
|
{
|
|
|
|
SetMovePoints smp;
|
|
|
|
smp.hid = hero->id;
|
|
|
|
smp.val = hero->movement;
|
|
|
|
|
|
|
|
if (info.reward.movePercentage >= 0) // percent from max
|
|
|
|
smp.val = hero->maxMovePoints(hero->boat != nullptr) * info.reward.movePercentage / 100;
|
|
|
|
smp.val = std::max<si32>(0, smp.val + info.reward.movePoints);
|
|
|
|
|
|
|
|
cb->setMovePoints(&smp);
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(const Bonus & bonus : info.reward.bonuses)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-06-29 20:13:25 +03:00
|
|
|
assert(bonus.source == Bonus::OBJECT);
|
|
|
|
assert(bonus.sid == ID);
|
2014-04-06 23:14:26 +03:00
|
|
|
GiveBonus gb;
|
2014-06-29 20:13:25 +03:00
|
|
|
gb.who = GiveBonus::HERO;
|
2014-04-06 23:14:26 +03:00
|
|
|
gb.bonus = bonus;
|
|
|
|
gb.id = hero->id.getNum();
|
|
|
|
cb->giveHeroBonus(&gb);
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
for(ArtifactID art : info.reward.artifacts)
|
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
|
|
|
cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(!info.reward.spells.empty())
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
std::set<SpellID> spellsToGive(info.reward.spells.begin(), info.reward.spells.end());
|
|
|
|
cb->changeSpells(hero, true, spellsToGive);
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(!info.reward.creatures.empty())
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
CCreatureSet creatures;
|
|
|
|
for (auto & crea : info.reward.creatures)
|
|
|
|
creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
|
|
|
|
|
|
|
|
cb->giveCreatures(this, hero, creatures, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRewardGiven(hero);
|
2014-04-12 16:47:48 +03:00
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(info.reward.removeObject)
|
2014-04-12 16:47:48 +03:00
|
|
|
cb->removeObject(this);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
bool CRewardableObject::wasVisited(PlayerColor player) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
switch (visitMode)
|
|
|
|
{
|
|
|
|
case VISIT_UNLIMITED:
|
2014-07-01 17:19:08 +03:00
|
|
|
case VISIT_BONUS:
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
2016-09-20 22:05:44 +02:00
|
|
|
case VISIT_ONCE:
|
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));
|
2016-09-16 20:59:07 +02:00
|
|
|
case VISIT_HERO:
|
|
|
|
return false;
|
2016-09-16 22:01:21 +02:00
|
|
|
case 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
|
|
|
{
|
|
|
|
switch (visitMode)
|
|
|
|
{
|
2014-06-29 20:13:25 +03:00
|
|
|
case VISIT_UNLIMITED:
|
2014-07-01 17:19:08 +03:00
|
|
|
return false;
|
|
|
|
case VISIT_BONUS:
|
2014-06-29 20:13:25 +03:00
|
|
|
return h->hasBonusFrom(Bonus::OBJECT, ID);
|
2014-04-06 23:14:26 +03:00
|
|
|
case 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 22:58:53 +02:00
|
|
|
CRewardableObject::EVisitMode CRewardableObject::getVisitMode() const
|
|
|
|
{
|
|
|
|
return EVisitMode(visitMode);
|
|
|
|
}
|
|
|
|
|
2023-01-23 15:27:01 +02:00
|
|
|
ui16 CRewardableObject::getResetDuration() const
|
|
|
|
{
|
|
|
|
return resetDuration;
|
|
|
|
}
|
|
|
|
|
2016-01-31 09:12:01 +02:00
|
|
|
void CRewardInfo::loadComponents(std::vector<Component> & comps,
|
|
|
|
const CGHeroInstance * h) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-04-12 16:16:23 +03:00
|
|
|
for (auto comp : extraComponents)
|
|
|
|
comps.push_back(comp);
|
|
|
|
|
2016-01-31 09:12:01 +02:00
|
|
|
if (gainedExp)
|
|
|
|
{
|
|
|
|
comps.push_back(Component(
|
2020-10-01 10:38:06 +02:00
|
|
|
Component::EXPERIENCE, 0, (si32)h->calculateXp(gainedExp), 0));
|
2016-01-31 09:12:01 +02:00
|
|
|
}
|
2014-04-06 23:14:26 +03:00
|
|
|
if (gainedLevels) comps.push_back(Component(Component::EXPERIENCE, 0, gainedLevels, 0));
|
|
|
|
|
2016-01-31 09:12:01 +02:00
|
|
|
if (manaDiff) comps.push_back(Component(Component::PRIM_SKILL, 5, manaDiff, 0));
|
2014-04-06 23:14:26 +03:00
|
|
|
|
|
|
|
for (size_t i=0; i<primary.size(); i++)
|
|
|
|
{
|
2016-01-31 09:12:01 +02:00
|
|
|
if (primary[i] != 0)
|
2020-10-01 10:38:06 +02:00
|
|
|
comps.push_back(Component(Component::PRIM_SKILL, (ui16)i, primary[i], 0));
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & entry : secondary)
|
|
|
|
comps.push_back(Component(Component::SEC_SKILL, entry.first, entry.second, 0));
|
|
|
|
|
|
|
|
for (auto & entry : artifacts)
|
|
|
|
comps.push_back(Component(Component::ARTIFACT, entry, 1, 0));
|
|
|
|
|
|
|
|
for (auto & entry : spells)
|
|
|
|
comps.push_back(Component(Component::SPELL, entry, 1, 0));
|
|
|
|
|
|
|
|
for (auto & entry : creatures)
|
|
|
|
comps.push_back(Component(Component::CREATURE, entry.type->idNumber, entry.count, 0));
|
2014-06-29 20:13:25 +03:00
|
|
|
|
|
|
|
for (size_t i=0; i<resources.size(); i++)
|
|
|
|
{
|
|
|
|
if (resources[i] !=0)
|
2020-10-01 10:38:06 +02:00
|
|
|
comps.push_back(Component(Component::RESOURCE, (ui16)i, resources[i], 0));
|
2014-06-29 20:13:25 +03:00
|
|
|
}
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2016-01-31 09:12:01 +02:00
|
|
|
Component CRewardInfo::getDisplayedComponent(const CGHeroInstance * h) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
std::vector<Component> comps;
|
2016-01-31 09:12:01 +02:00
|
|
|
loadComponents(comps, h);
|
2014-04-06 23:14:26 +03:00
|
|
|
assert(!comps.empty());
|
|
|
|
return comps.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: copy-pasted from CObjectHandler
|
2023-01-01 20:54:05 +02:00
|
|
|
static std::string visitedTxt(const bool visited)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
int id = visited ? 352 : 353;
|
|
|
|
return VLC->generaltexth->allTexts[id];
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
std::string CRewardableObject::getHoverText(PlayerColor player) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2014-07-01 17:19:08 +03:00
|
|
|
if(visitMode == VISIT_PLAYER || visitMode == 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
|
|
|
|
{
|
|
|
|
if(visitMode != VISIT_UNLIMITED)
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
case ObjProperty::REWARD_RESET:
|
2014-04-07 14:56:59 +03:00
|
|
|
for (auto & visit : info)
|
2023-01-23 23:54:54 +02:00
|
|
|
visit.numOfGrantsPerformed = 0;
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
|
|
|
case ObjProperty::REWARD_SELECT:
|
|
|
|
selectedReward = val;
|
2023-01-23 23:54:54 +02:00
|
|
|
info[val].numOfGrantsPerformed++;
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 18:38:13 +02:00
|
|
|
void CRewardableObject::triggerRewardReset() const
|
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_RESET, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:30:36 +02:00
|
|
|
void CRewardableObject::newTurn(CRandomGenerator & rand) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2016-01-30 18:11:22 +02:00
|
|
|
if (resetDuration != 0 && cb->getDate(Date::DAY) > 1 && (cb->getDate(Date::DAY) % resetDuration) == 1)
|
2016-10-09 18:38:13 +02:00
|
|
|
triggerRewardReset();
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 20:51:19 +02:00
|
|
|
void CRewardableObject::initObj(CRandomGenerator & rand)
|
|
|
|
{
|
|
|
|
VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, rand);
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
CRewardableObject::CRewardableObject():
|
2014-04-06 23:14:26 +03:00
|
|
|
selectMode(0),
|
2016-11-27 21:07:01 +02:00
|
|
|
visitMode(0),
|
2014-04-06 23:14:26 +03:00
|
|
|
selectedReward(0),
|
2014-04-11 20:38:27 +03:00
|
|
|
resetDuration(0),
|
|
|
|
canRefuse(false)
|
2014-04-06 23:14:26 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2023-01-23 15:27:01 +02:00
|
|
|
/*
|
2014-04-06 23:14:26 +03:00
|
|
|
std::vector<int3> CGMagicSpring::getVisitableOffsets() const
|
|
|
|
{
|
|
|
|
std::vector <int3> visitableTiles;
|
|
|
|
|
|
|
|
for(int y = 0; y < 6; y++)
|
|
|
|
for (int x = 0; x < 8; x++) //starting from left
|
2022-09-11 15:12:35 +02:00
|
|
|
if (appearance->isVisitableAt(x, y))
|
2014-04-06 23:14:26 +03:00
|
|
|
visitableTiles.push_back (int3(x, y , 0));
|
|
|
|
|
|
|
|
return visitableTiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGMagicSpring::getVisitableOffset() const
|
|
|
|
{
|
|
|
|
auto visitableTiles = getVisitableOffsets();
|
|
|
|
|
|
|
|
if (visitableTiles.size() != info.size())
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->warn("Unexpected number of visitable tiles of Magic Spring at %s", pos.toString());
|
2014-04-06 23:14:26 +03:00
|
|
|
return int3(-1,-1,-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i=0; i<visitableTiles.size(); i++)
|
|
|
|
{
|
2014-04-07 14:56:59 +03:00
|
|
|
if (info[i].numOfGrants == 0)
|
2014-04-06 23:14:26 +03:00
|
|
|
return visitableTiles[i];
|
|
|
|
}
|
|
|
|
return visitableTiles[0]; // return *something*. This is valid visitable tile but already used
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ui32> CGMagicSpring::getAvailableRewards(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
auto tiles = getVisitableOffsets();
|
|
|
|
for (size_t i=0; i<tiles.size(); i++)
|
|
|
|
{
|
2022-12-07 21:50:45 +02:00
|
|
|
if (pos - tiles[i] == hero->visitablePos() && info[i].numOfGrants == 0)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
return std::vector<ui32>(1, (ui32)i);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// hero is either not on visitable tile (should not happen) or tile is already used
|
|
|
|
return std::vector<ui32>();
|
|
|
|
}
|
2023-01-23 15:27:01 +02:00
|
|
|
*/
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|