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;
|
|
|
|
}
|
|
|
|
|
2023-01-24 16:54:14 +02:00
|
|
|
if(daysPassed != 0)
|
|
|
|
{
|
|
|
|
if (IObjectInterface::cb->getDate(Date::DAY) < daysPassed)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & reqStack : creatures)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & slot : hero->Slots())
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
if(heroLevel > static_cast<si32>(hero->level))
|
2023-01-25 00:38:50 +02:00
|
|
|
return false;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
if(static_cast<TExpType>(heroExperience) > hero->exp)
|
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
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
if(primary[i] > hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)))
|
2014-04-06 23:14:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const 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;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & spell : spells)
|
2023-01-25 00:38:50 +02:00
|
|
|
{
|
|
|
|
if (!hero->spellbookContainsSpell(spell))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & art : artifacts)
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
if (!hero->hasArt(art))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & sublimiter : noneOf)
|
2023-01-23 23:54:54 +02:00
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & sublimiter : allOf)
|
2023-01-23 23:54:54 +02:00
|
|
|
{
|
|
|
|
if (!sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
if(anyOf.empty())
|
2023-01-23 23:54:54 +02:00
|
|
|
return true;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & sublimiter : anyOf)
|
2023-01-23 23:54:54 +02:00
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
std::vector<ui32> CRewardableObject::getAvailableRewards(const CGHeroInstance * hero, CRewardVisitInfo::ERewardEventType event) 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
|
|
|
{
|
2023-01-24 13:54:03 +02:00
|
|
|
const CRewardVisitInfo & visit = info[i];
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
if(event == visit.visitType && 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;
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::onHeroVisit(const CGHeroInstance *h) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
auto grantRewardWithMessage = [&](int index, bool markAsVisit) -> void
|
2014-04-11 20:38:27 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
auto vi = info[index];
|
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
|
2023-01-24 23:31:07 +02:00
|
|
|
grantReward(index, h, markAsVisit);
|
2014-04-11 20:38:27 +03:00
|
|
|
};
|
2023-02-12 22:39:17 +02:00
|
|
|
auto selectRewardsMessage = [&](const 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)
|
2023-01-24 23:31:07 +02:00
|
|
|
sd.components.push_back(info[index].reward.getDisplayedComponent(h));
|
2014-04-12 14:47:20 +03:00
|
|
|
cb->showBlockingDialog(&sd);
|
|
|
|
};
|
2014-04-11 20:38:27 +03:00
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
if(!wasVisitedBefore(h))
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
auto rewards = getAvailableRewards(h, CRewardVisitInfo::EVENT_FIRST_VISIT);
|
2016-09-23 10:47:07 +02:00
|
|
|
bool objectRemovalPossible = false;
|
|
|
|
for(auto index : rewards)
|
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
if(info[index].reward.removeObject)
|
2016-09-23 10:47:07 +02:00
|
|
|
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())
|
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
case 0: // no available rewards, e.g. visiting School of War without gold
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
auto emptyRewards = getAvailableRewards(h, CRewardVisitInfo::EVENT_NOT_AVAILABLE);
|
|
|
|
if (!emptyRewards.empty())
|
|
|
|
grantRewardWithMessage(emptyRewards[0], false);
|
2014-04-11 22:49:25 +03:00
|
|
|
else
|
2023-01-24 23:31:07 +02:00
|
|
|
logMod->warn("No applicable message for visiting empty object!");
|
2014-04-06 23:14:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: // one reward. Just give it with message
|
|
|
|
{
|
2014-04-12 14:47:20 +03:00
|
|
|
if (canRefuse)
|
2023-01-24 23:31:07 +02:00
|
|
|
selectRewardsMessage(rewards, info[rewards[0]].message);
|
2014-04-12 14:47:20 +03:00
|
|
|
else
|
2023-01-24 23:31:07 +02:00
|
|
|
grantRewardWithMessage(rewards[0], true);
|
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
|
2023-01-24 23:31:07 +02:00
|
|
|
grantRewardWithMessage(rewards[0], true);
|
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
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
if(!objectRemovalPossible && getAvailableRewards(h, CRewardVisitInfo::EVENT_FIRST_VISIT).empty())
|
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");
|
2023-01-24 23:31:07 +02:00
|
|
|
|
|
|
|
auto visitedRewards = getAvailableRewards(h, CRewardVisitInfo::EVENT_ALREADY_VISITED);
|
|
|
|
if (!visitedRewards.empty())
|
|
|
|
grantRewardWithMessage(visitedRewards[0], false);
|
2014-04-11 22:49:25 +03:00
|
|
|
else
|
2023-01-24 23:31:07 +02:00
|
|
|
logMod->warn("No applicable message for visiting already visited object!");
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
grantRewardAfterLevelup(info[selectedReward], 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
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
auto list = getAvailableRewards(hero, CRewardVisitInfo::EVENT_FIRST_VISIT);
|
|
|
|
grantReward(list[answer - 1], hero, true);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unhandled choice");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero, bool markVisited) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
if (markVisited)
|
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_CLEARED, true);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD, id, hero->id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
2014-04-11 20:38:27 +03:00
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_SELECT, rewardID);
|
|
|
|
grantRewardBeforeLevelup(info[rewardID], hero);
|
2014-04-11 20:38:27 +03:00
|
|
|
}
|
|
|
|
|
2023-01-24 13:54:03 +02:00
|
|
|
void CRewardableObject::grantRewardBeforeLevelup(const CRewardVisitInfo & 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);
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const 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++)
|
2023-01-25 00:38:50 +02:00
|
|
|
cb->changePrimSkill(hero, static_cast<PrimarySkill::PrimarySkill>(i), info.reward.primary[i], false);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
|
|
|
si64 expToGive = 0;
|
2023-01-25 00:38:50 +02:00
|
|
|
|
|
|
|
if (info.reward.heroLevel > 0)
|
|
|
|
expToGive += VLC->heroh->reqExp(hero->level+info.reward.heroLevel) - VLC->heroh->reqExp(hero->level);
|
|
|
|
|
|
|
|
if (info.reward.heroExperience > 0)
|
|
|
|
expToGive += hero->calculateXp(info.reward.heroExperience);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:54:03 +02:00
|
|
|
void CRewardableObject::grantRewardAfterLevelup(const CRewardVisitInfo & 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);
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const 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);
|
|
|
|
}
|
|
|
|
|
2023-01-24 16:18:59 +02:00
|
|
|
if(!info.reward.creaturesChange.empty())
|
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & slot : hero->Slots())
|
2023-01-24 16:18:59 +02:00
|
|
|
{
|
|
|
|
const CStackInstance * heroStack = slot.second;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & change : info.reward.creaturesChange)
|
2023-01-24 16:18:59 +02:00
|
|
|
{
|
|
|
|
if (heroStack->type->getId() == change.first)
|
|
|
|
{
|
|
|
|
StackLocation location(hero, slot.first);
|
|
|
|
cb->changeStackType(location, change.second.toCreature());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:41:15 +02:00
|
|
|
if(!info.reward.creatures.empty())
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
CCreatureSet creatures;
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & crea : info.reward.creatures)
|
2014-04-06 23:14:26 +03:00
|
|
|
creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
|
|
|
|
|
|
|
|
cb->giveCreatures(this, hero, creatures, false);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
|
|
|
switch (visitMode)
|
|
|
|
{
|
|
|
|
case VISIT_UNLIMITED:
|
|
|
|
return false;
|
2016-09-20 22:05:44 +02:00
|
|
|
case VISIT_ONCE:
|
2023-01-24 23:31:07 +02:00
|
|
|
return onceVisitableObjectCleared;
|
|
|
|
case VISIT_PLAYER:
|
|
|
|
return vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
|
|
|
|
case VISIT_BONUS:
|
|
|
|
return contextHero->hasBonusFrom(Bonus::OBJECT, ID);
|
2016-09-16 20:59:07 +02:00
|
|
|
case 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
|
|
|
|
{
|
|
|
|
switch (visitMode)
|
|
|
|
{
|
|
|
|
case VISIT_UNLIMITED:
|
|
|
|
case VISIT_BONUS:
|
|
|
|
case VISIT_HERO:
|
|
|
|
return false;
|
|
|
|
case VISIT_ONCE:
|
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-07-01 17:19:08 +03:00
|
|
|
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
|
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
return static_cast<EVisitMode>(visitMode);
|
2023-01-22 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 15:27:01 +02:00
|
|
|
ui16 CRewardableObject::getResetDuration() const
|
|
|
|
{
|
2023-01-24 13:54:03 +02:00
|
|
|
return resetParameters.period;
|
2023-01-23 15:27:01 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2023-01-25 00:38:50 +02:00
|
|
|
if (heroExperience)
|
2016-01-31 09:12:01 +02:00
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
comps.emplace_back(Component::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
|
2016-01-31 09:12:01 +02:00
|
|
|
}
|
2023-01-25 00:38:50 +02:00
|
|
|
if (heroLevel)
|
2023-02-12 22:39:17 +02:00
|
|
|
comps.emplace_back(Component::EXPERIENCE, 1, heroLevel, 0);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-01-24 23:31:07 +02:00
|
|
|
if (manaDiff || manaPercentage >= 0)
|
2023-02-12 22:39:17 +02:00
|
|
|
comps.emplace_back(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)
|
2023-02-12 22:39:17 +02:00
|
|
|
comps.emplace_back(Component::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : secondary)
|
|
|
|
comps.emplace_back(Component::SEC_SKILL, entry.first, entry.second, 0);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : artifacts)
|
|
|
|
comps.emplace_back(Component::ARTIFACT, entry, 1, 0);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : spells)
|
|
|
|
comps.emplace_back(Component::SPELL, entry, 1, 0);
|
2014-04-06 23:14:26 +03:00
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & entry : creatures)
|
|
|
|
comps.emplace_back(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)
|
2023-02-12 22:39:17 +02:00
|
|
|
comps.emplace_back(Component::RESOURCE, static_cast<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)
|
|
|
|
{
|
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:
|
|
|
|
selectedReward = val;
|
|
|
|
break;
|
2023-01-24 23:31:07 +02:00
|
|
|
case ObjProperty::REWARD_CLEARED:
|
|
|
|
onceVisitableObjectCleared = val;
|
|
|
|
break;
|
2014-04-06 23:14:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:54:03 +02:00
|
|
|
void CRewardableObject::triggerReset() const
|
2016-10-09 18:38:13 +02:00
|
|
|
{
|
2023-01-24 13:54:03 +02:00
|
|
|
if (resetParameters.rewards)
|
|
|
|
{
|
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_RANDOMIZE, 0);
|
|
|
|
}
|
|
|
|
if (resetParameters.visitors)
|
|
|
|
{
|
2023-01-24 23:31:07 +02:00
|
|
|
cb->setObjProperty(id, ObjProperty::REWARD_CLEARED, false);
|
|
|
|
|
2023-01-24 13:54:03 +02:00
|
|
|
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
|
|
|
|
cb->sendAndApply(&cov);
|
|
|
|
}
|
2016-10-09 18:38:13 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:30:36 +02:00
|
|
|
void CRewardableObject::newTurn(CRandomGenerator & rand) const
|
2014-04-06 23:14:26 +03:00
|
|
|
{
|
2023-01-27 00:59:10 +02:00
|
|
|
if (resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % resetParameters.period) == 0)
|
2023-01-24 13:54:03 +02:00
|
|
|
triggerReset();
|
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),
|
2023-01-24 23:31:07 +02:00
|
|
|
onceVisitableObjectCleared(false),
|
2014-04-11 20:38:27 +03:00
|
|
|
canRefuse(false)
|
2014-04-06 23:14:26 +03:00
|
|
|
{}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|