2023-04-30 15:13:07 +02:00
|
|
|
/*
|
|
|
|
* Limiter.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Limiter.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
#include "../IGameCallback.h"
|
|
|
|
#include "../CPlayerState.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../networkPacks/Component.h"
|
2023-09-15 10:06:06 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
2023-09-15 23:00:33 +02:00
|
|
|
#include "../constants/StringConstants.h"
|
2023-10-09 04:19:12 +02:00
|
|
|
#include "../CHeroHandler.h"
|
2023-09-15 23:00:33 +02:00
|
|
|
#include "../CSkillHandler.h"
|
2023-10-09 02:33:16 +02:00
|
|
|
#include "../ArtifactUtils.h"
|
2023-04-30 15:13:07 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
Rewardable::Limiter::Limiter()
|
|
|
|
: dayOfWeek(0)
|
|
|
|
, daysPassed(0)
|
|
|
|
, heroExperience(0)
|
2023-10-09 19:15:34 +02:00
|
|
|
, heroLevel(-1)
|
2023-10-11 01:43:24 +02:00
|
|
|
, manaPercentage(0)
|
2023-06-02 20:47:37 +02:00
|
|
|
, manaPoints(0)
|
2023-12-10 19:48:27 +02:00
|
|
|
, canLearnSkills(false)
|
2023-06-02 20:47:37 +02:00
|
|
|
, primary(GameConstants::PRIMARY_SKILLS, 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Rewardable::Limiter::~Limiter() = default;
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
bool operator==(const Rewardable::Limiter & l, const Rewardable::Limiter & r)
|
2023-10-10 01:20:55 +02:00
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
return l.dayOfWeek == r.dayOfWeek
|
|
|
|
&& l.daysPassed == r.daysPassed
|
|
|
|
&& l.heroLevel == r.heroLevel
|
|
|
|
&& l.heroExperience == r.heroExperience
|
|
|
|
&& l.manaPoints == r.manaPoints
|
|
|
|
&& l.manaPercentage == r.manaPercentage
|
|
|
|
&& l.secondary == r.secondary
|
2023-12-10 19:48:27 +02:00
|
|
|
&& l.canLearnSkills == r.canLearnSkills
|
2023-10-11 00:47:19 +02:00
|
|
|
&& l.creatures == r.creatures
|
|
|
|
&& l.spells == r.spells
|
|
|
|
&& l.artifacts == r.artifacts
|
|
|
|
&& l.players == r.players
|
|
|
|
&& l.heroes == r.heroes
|
|
|
|
&& l.heroClasses == r.heroClasses
|
|
|
|
&& l.resources == r.resources
|
|
|
|
&& l.primary == r.primary
|
|
|
|
&& l.noneOf == r.noneOf
|
|
|
|
&& l.allOf == r.allOf
|
|
|
|
&& l.anyOf == r.anyOf;
|
2023-10-10 01:20:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 21:10:42 +02:00
|
|
|
bool operator!=(const Rewardable::Limiter & l, const Rewardable::Limiter & r)
|
|
|
|
{
|
|
|
|
return !(l == r);
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Limiter::heroAllowed(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
if(dayOfWeek != 0)
|
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
if (hero->cb->getDate(Date::DAY_OF_WEEK) != dayOfWeek)
|
2023-04-30 15:13:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(daysPassed != 0)
|
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
if (hero->cb->getDate(Date::DAY) < daysPassed)
|
2023-04-30 15:13:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & reqStack : creatures)
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for(const 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;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
if(!hero->cb->getPlayerState(hero->tempOwner)->resources.canAfford(resources))
|
2023-04-30 15:13:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if(heroLevel > static_cast<si32>(hero->level))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(static_cast<TExpType>(heroExperience) > hero->exp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(manaPoints > hero->mana)
|
|
|
|
return false;
|
|
|
|
|
2023-09-30 20:22:31 +02:00
|
|
|
if (canLearnSkills && !hero->canLearnSkill())
|
|
|
|
return false;
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
if(manaPercentage > 100 * hero->mana / hero->manaLimit())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(size_t i=0; i<primary.size(); i++)
|
|
|
|
{
|
2023-08-19 20:43:50 +02:00
|
|
|
if(primary[i] > hero->getPrimSkillLevel(static_cast<PrimarySkill>(i)))
|
2023-04-30 15:13:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & skill : secondary)
|
|
|
|
{
|
|
|
|
if (skill.second > hero->getSecSkillLevel(skill.first))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & spell : spells)
|
|
|
|
{
|
|
|
|
if (!hero->spellbookContainsSpell(spell))
|
|
|
|
return false;
|
2023-10-04 15:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & spell : canLearnSpells)
|
|
|
|
{
|
2023-11-04 17:04:53 +02:00
|
|
|
if (!hero->canLearnSpell(spell.toEntity(VLC), true))
|
2023-10-04 15:49:17 +02:00
|
|
|
return false;
|
2023-04-30 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
std::unordered_map<ArtifactID, unsigned int, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
|
2023-10-09 02:33:16 +02:00
|
|
|
for(const auto & art : artifacts)
|
|
|
|
++artifactsRequirements[art];
|
|
|
|
|
|
|
|
size_t reqSlots = 0;
|
|
|
|
for(const auto & elem : artifactsRequirements)
|
|
|
|
{
|
|
|
|
// check required amount of artifacts
|
|
|
|
if(hero->getArtPosCount(elem.first, false, true, true) < elem.second)
|
|
|
|
return false;
|
|
|
|
if(!hero->hasArt(elem.first))
|
|
|
|
reqSlots += hero->getAssemblyByConstituent(elem.first)->getPartsInfo().size() - 2;
|
|
|
|
}
|
|
|
|
if(!ArtifactUtils::isBackpackFreeSlots(hero, reqSlots))
|
2023-04-30 15:13:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-10-09 02:33:16 +02:00
|
|
|
|
2023-10-09 04:19:12 +02:00
|
|
|
if(!players.empty() && !vstd::contains(players, hero->getOwner()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!heroes.empty() && !vstd::contains(heroes, hero->type->getId()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!heroClasses.empty() && !vstd::contains(heroClasses, hero->type->heroClass->getId()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
for(const auto & sublimiter : noneOf)
|
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & sublimiter : allOf)
|
|
|
|
{
|
|
|
|
if (!sublimiter->heroAllowed(hero))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(anyOf.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for(const auto & sublimiter : anyOf)
|
|
|
|
{
|
|
|
|
if (sublimiter->heroAllowed(hero))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-09 05:24:40 +02:00
|
|
|
void Rewardable::Limiter::loadComponents(std::vector<Component> & comps,
|
|
|
|
const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
if (heroExperience)
|
2023-11-02 15:50:24 +02:00
|
|
|
comps.emplace_back(ComponentType::EXPERIENCE, static_cast<si32>(h ? h->calculateXp(heroExperience) : heroExperience));
|
2023-10-10 01:20:55 +02:00
|
|
|
|
2023-10-12 12:52:01 +02:00
|
|
|
if (heroLevel > 0)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::EXPERIENCE, heroLevel);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
2023-10-11 01:43:24 +02:00
|
|
|
if (manaPoints || manaPercentage > 0)
|
2023-10-10 01:20:55 +02:00
|
|
|
{
|
2023-11-02 15:50:24 +02:00
|
|
|
int absoluteMana = (h && h->manaLimit()) ? (manaPercentage * h->mana / h->manaLimit() / 100) : 0;
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::MANA, absoluteMana + manaPoints);
|
2023-10-10 01:20:55 +02:00
|
|
|
}
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for (size_t i=0; i<primary.size(); i++)
|
|
|
|
{
|
|
|
|
if (primary[i] != 0)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill(i), primary[i]);
|
2023-10-09 05:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & entry : secondary)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::SEC_SKILL, entry.first, entry.second);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for(const auto & entry : artifacts)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::ARTIFACT, entry);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for(const auto & entry : spells)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::SPELL, entry);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for(const auto & entry : creatures)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::CREATURE, entry.type->getId(), entry.count);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for(const auto & entry : players)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::FLAG, entry);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for(const auto & entry : heroes)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::HERO_PORTRAIT, entry);
|
2023-10-09 05:24:40 +02:00
|
|
|
|
|
|
|
for (size_t i=0; i<resources.size(); i++)
|
|
|
|
{
|
|
|
|
if (resources[i] !=0)
|
2023-10-31 11:09:56 +02:00
|
|
|
comps.emplace_back(ComponentType::RESOURCE, GameResID(i), resources[i]);
|
2023-10-09 05:24:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 10:06:06 +02:00
|
|
|
void Rewardable::Limiter::serializeJson(JsonSerializeFormat & handler)
|
|
|
|
{
|
2023-09-15 23:00:33 +02:00
|
|
|
handler.serializeInt("dayOfWeek", dayOfWeek);
|
|
|
|
handler.serializeInt("daysPassed", daysPassed);
|
|
|
|
resources.serializeJson(handler, "resources");
|
2023-10-11 01:43:24 +02:00
|
|
|
handler.serializeInt("manaPercentage", manaPercentage);
|
2023-09-15 23:00:33 +02:00
|
|
|
handler.serializeInt("heroExperience", heroExperience);
|
|
|
|
handler.serializeInt("heroLevel", heroLevel);
|
2023-10-09 04:26:08 +02:00
|
|
|
handler.serializeIdArray("heroes", heroes);
|
|
|
|
handler.serializeIdArray("heroClasses", heroClasses);
|
|
|
|
handler.serializeIdArray("colors", players);
|
2023-09-15 23:00:33 +02:00
|
|
|
handler.serializeInt("manaPoints", manaPoints);
|
|
|
|
handler.serializeIdArray("artifacts", artifacts);
|
|
|
|
handler.enterArray("creatures").serializeStruct(creatures);
|
2023-09-17 22:19:45 +02:00
|
|
|
handler.enterArray("primary").serializeArray(primary);
|
2023-09-15 23:00:33 +02:00
|
|
|
{
|
|
|
|
auto a = handler.enterArray("secondary");
|
2023-09-17 22:19:45 +02:00
|
|
|
std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
|
|
|
|
a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
|
2023-09-15 23:00:33 +02:00
|
|
|
{
|
2023-11-02 16:31:12 +02:00
|
|
|
h.serializeId("skill", e.first, SecondarySkill(SecondarySkill::NONE));
|
2023-09-17 22:19:45 +02:00
|
|
|
h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
|
|
|
|
});
|
2023-09-15 23:00:33 +02:00
|
|
|
a.syncSize(fieldValue);
|
2023-09-17 22:19:45 +02:00
|
|
|
secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
|
2023-09-15 23:00:33 +02:00
|
|
|
}
|
|
|
|
//sublimiters
|
|
|
|
auto serializeSublimitersList = [&handler](const std::string & field, LimitersList & container)
|
|
|
|
{
|
|
|
|
auto a = handler.enterArray(field);
|
|
|
|
a.syncSize(container);
|
|
|
|
for(int i = 0; i < container.size(); ++i)
|
|
|
|
{
|
|
|
|
if(!handler.saving)
|
|
|
|
container[i] = std::make_shared<Rewardable::Limiter>();
|
|
|
|
auto e = a.enterStruct(i);
|
|
|
|
container[i]->serializeJson(handler);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
serializeSublimitersList("allOf", allOf);
|
|
|
|
serializeSublimitersList("anyOf", anyOf);
|
|
|
|
serializeSublimitersList("noneOf", noneOf);
|
2023-09-15 10:06:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|