2023-04-30 02:39:31 +02:00
|
|
|
/*
|
2023-04-30 15:13:07 +02:00
|
|
|
* Info.cpp, part of VCMI engine
|
2023-04-30 02:39:31 +02: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"
|
2023-04-30 15:13:07 +02:00
|
|
|
#include "Info.h"
|
2023-07-19 21:25:52 +02:00
|
|
|
|
|
|
|
#include "Configuration.h"
|
2023-04-30 15:13:07 +02:00
|
|
|
#include "Limiter.h"
|
|
|
|
#include "Reward.h"
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2023-04-30 15:13:07 +02:00
|
|
|
#include "../IGameCallback.h"
|
2023-07-19 21:25:52 +02:00
|
|
|
#include "../JsonRandom.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/IObjectInterface.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/IdentifierStorage.h"
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
namespace {
|
2023-07-19 21:25:52 +02:00
|
|
|
MetaString loadMessage(const JsonNode & value, const TextIdentifier & textIdentifier )
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
MetaString ret;
|
|
|
|
if (value.isNumber())
|
2023-07-19 21:25:52 +02:00
|
|
|
{
|
2023-06-18 11:18:25 +02:00
|
|
|
ret.appendLocalString(EMetaText::ADVOB_TXT, static_cast<ui32>(value.Float()));
|
2023-07-19 21:25:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.String().empty())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret.appendTextID(textIdentifier.get());
|
2023-04-30 02:39:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool testForKey(const JsonNode & value, const std::string & key)
|
|
|
|
{
|
|
|
|
for(const auto & reward : value["rewards"].Vector())
|
|
|
|
{
|
|
|
|
if (!reward[key].isNull())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:25:52 +02:00
|
|
|
void Rewardable::Info::init(const JsonNode & objectConfig, const std::string & objectName)
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
2023-07-19 21:25:52 +02:00
|
|
|
objectTextID = objectName;
|
|
|
|
|
|
|
|
auto loadString = [&](const JsonNode & entry, const TextIdentifier & textID){
|
|
|
|
if (entry.isString() && !entry.String().empty())
|
|
|
|
VLC->generaltexth->registerString(entry.meta, textID, entry.String());
|
|
|
|
};
|
|
|
|
|
2023-04-30 02:39:31 +02:00
|
|
|
parameters = objectConfig;
|
2023-07-19 21:25:52 +02:00
|
|
|
|
|
|
|
for(size_t i = 0; i < parameters["rewards"].Vector().size(); ++i)
|
|
|
|
{
|
|
|
|
const JsonNode message = parameters["rewards"][i]["message"];
|
|
|
|
loadString(message, TextIdentifier(objectName, "rewards", i));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < parameters["onVisited"].Vector().size(); ++i)
|
|
|
|
{
|
|
|
|
const JsonNode message = parameters["onVisited"][i]["message"];
|
|
|
|
loadString(message, TextIdentifier(objectName, "onVisited", i));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < parameters["onEmpty"].Vector().size(); ++i)
|
|
|
|
{
|
|
|
|
const JsonNode message = parameters["onEmpty"][i]["message"];
|
|
|
|
loadString(message, TextIdentifier(objectName, "onEmpty", i));
|
|
|
|
}
|
|
|
|
|
|
|
|
loadString(parameters["onSelectMessage"], TextIdentifier(objectName, "onSelect"));
|
|
|
|
loadString(parameters["onVisitedMessage"], TextIdentifier(objectName, "onVisited"));
|
|
|
|
loadString(parameters["onEmptyMessage"], TextIdentifier(objectName, "onEmpty"));
|
2023-04-30 02:39:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
Rewardable::LimitersList result;
|
2023-04-30 02:39:31 +02:00
|
|
|
for (const auto & input : source.Vector())
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
auto newLimiter = std::make_shared<Rewardable::Limiter>();
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
configureLimiter(object, rng, *newLimiter, input);
|
|
|
|
|
|
|
|
result.push_back(newLimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
std::vector<SpellID> spells;
|
2023-06-06 17:34:04 +02:00
|
|
|
IObjectInterface::cb->getAllowedSpells(spells);
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
limiter.dayOfWeek = JsonRandom::loadValue(source["dayOfWeek"], rng);
|
|
|
|
limiter.daysPassed = JsonRandom::loadValue(source["daysPassed"], rng);
|
|
|
|
limiter.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng);
|
|
|
|
limiter.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng)
|
|
|
|
+ JsonRandom::loadValue(source["minLevel"], rng); // VCMI 1.1 compatibilty
|
|
|
|
|
2023-10-11 01:43:24 +02:00
|
|
|
limiter.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng);
|
2023-04-30 02:39:31 +02:00
|
|
|
limiter.manaPoints = JsonRandom::loadValue(source["manaPoints"], rng);
|
|
|
|
|
|
|
|
limiter.resources = JsonRandom::loadResources(source["resources"], rng);
|
|
|
|
|
|
|
|
limiter.primary = JsonRandom::loadPrimary(source["primary"], rng);
|
|
|
|
limiter.secondary = JsonRandom::loadSecondary(source["secondary"], rng);
|
|
|
|
limiter.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng);
|
|
|
|
limiter.spells = JsonRandom::loadSpells(source["spells"], rng, spells);
|
|
|
|
limiter.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
|
2023-10-09 04:19:12 +02:00
|
|
|
|
|
|
|
limiter.players = JsonRandom::loadColors(source["colors"], rng);
|
|
|
|
limiter.heroes = JsonRandom::loadHeroes(source["heroes"], rng);
|
|
|
|
limiter.heroClasses = JsonRandom::loadHeroClasses(source["heroClasses"], rng);
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
limiter.allOf = configureSublimiters(object, rng, source["allOf"] );
|
|
|
|
limiter.anyOf = configureSublimiters(object, rng, source["anyOf"] );
|
|
|
|
limiter.noneOf = configureSublimiters(object, rng, source["noneOf"] );
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
void Rewardable::Info::configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Reward & reward, const JsonNode & source) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
reward.resources = JsonRandom::loadResources(source["resources"], rng);
|
|
|
|
|
|
|
|
reward.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng)
|
|
|
|
+ JsonRandom::loadValue(source["gainedExp"], rng); // VCMI 1.1 compatibilty
|
|
|
|
|
|
|
|
reward.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng)
|
|
|
|
+ JsonRandom::loadValue(source["gainedLevels"], rng); // VCMI 1.1 compatibilty
|
|
|
|
|
|
|
|
reward.manaDiff = JsonRandom::loadValue(source["manaPoints"], rng);
|
|
|
|
reward.manaOverflowFactor = JsonRandom::loadValue(source["manaOverflowFactor"], rng);
|
|
|
|
reward.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng, -1);
|
|
|
|
|
|
|
|
reward.movePoints = JsonRandom::loadValue(source["movePoints"], rng);
|
|
|
|
reward.movePercentage = JsonRandom::loadValue(source["movePercentage"], rng, -1);
|
|
|
|
|
|
|
|
reward.removeObject = source["removeObject"].Bool();
|
|
|
|
reward.bonuses = JsonRandom::loadBonuses(source["bonuses"]);
|
|
|
|
|
|
|
|
reward.primary = JsonRandom::loadPrimary(source["primary"], rng);
|
|
|
|
reward.secondary = JsonRandom::loadSecondary(source["secondary"], rng);
|
|
|
|
|
|
|
|
std::vector<SpellID> spells;
|
2023-06-06 17:34:04 +02:00
|
|
|
IObjectInterface::cb->getAllowedSpells(spells);
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
reward.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng);
|
|
|
|
reward.spells = JsonRandom::loadSpells(source["spells"], rng, spells);
|
|
|
|
reward.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
|
|
|
|
if(!source["spellCast"].isNull() && source["spellCast"].isStruct())
|
|
|
|
{
|
|
|
|
reward.spellCast.first = JsonRandom::loadSpell(source["spellCast"]["spell"], rng);
|
|
|
|
reward.spellCast.second = source["spellCast"]["schoolLevel"].Integer();
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto node : source["changeCreatures"].Struct() )
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
CreatureID from(VLC->identifiers()->getIdentifier(node.second.meta, "creature", node.first).value());
|
|
|
|
CreatureID dest(VLC->identifiers()->getIdentifier(node.second.meta, "creature", node.second.String()).value());
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
reward.extraComponents.emplace_back(Component::EComponentType::CREATURE, dest.getNum(), 0, 0);
|
|
|
|
|
|
|
|
reward.creaturesChange[from] = dest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
void Rewardable::Info::configureResetInfo(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::ResetInfo & resetParameters, const JsonNode & source) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
resetParameters.period = static_cast<ui32>(source["period"].Float());
|
|
|
|
resetParameters.visitors = source["visitors"].Bool();
|
|
|
|
resetParameters.rewards = source["rewards"].Bool();
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
void Rewardable::Info::configureRewards(
|
2023-04-30 02:39:31 +02:00
|
|
|
Rewardable::Configuration & object,
|
|
|
|
CRandomGenerator & rng, const
|
|
|
|
JsonNode & source,
|
|
|
|
std::map<si32, si32> & thrownDice,
|
2023-07-19 21:25:52 +02:00
|
|
|
Rewardable::EEventType event,
|
|
|
|
const std::string & modeName) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
2023-07-19 21:25:52 +02:00
|
|
|
for(size_t i = 0; i < source.Vector().size(); ++i)
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
2023-07-19 21:25:52 +02:00
|
|
|
const JsonNode reward = source.Vector()[i];
|
|
|
|
|
2023-04-30 02:39:31 +02:00
|
|
|
if (!reward["appearChance"].isNull())
|
|
|
|
{
|
|
|
|
JsonNode chance = reward["appearChance"];
|
|
|
|
si32 diceID = static_cast<si32>(chance["dice"].Float());
|
|
|
|
|
|
|
|
if (thrownDice.count(diceID) == 0)
|
|
|
|
thrownDice[diceID] = rng.getIntRange(0, 99)();
|
|
|
|
|
|
|
|
if (!chance["min"].isNull())
|
|
|
|
{
|
|
|
|
int min = static_cast<int>(chance["min"].Float());
|
|
|
|
if (min > thrownDice[diceID])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!chance["max"].isNull())
|
|
|
|
{
|
|
|
|
int max = static_cast<int>(chance["max"].Float());
|
|
|
|
if (max <= thrownDice[diceID])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
Rewardable::VisitInfo info;
|
2023-04-30 02:39:31 +02:00
|
|
|
configureLimiter(object, rng, info.limiter, reward["limiter"]);
|
|
|
|
configureReward(object, rng, info.reward, reward);
|
|
|
|
|
|
|
|
info.visitType = event;
|
2023-07-19 21:25:52 +02:00
|
|
|
info.message = loadMessage(reward["message"], TextIdentifier(objectTextID, modeName, i));
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
for (const auto & artifact : info.reward.artifacts )
|
2023-06-18 11:18:25 +02:00
|
|
|
info.message.replaceLocalString(EMetaText::ART_NAMES, artifact.getNum());
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
for (const auto & artifact : info.reward.spells )
|
2023-06-18 11:18:25 +02:00
|
|
|
info.message.replaceLocalString(EMetaText::SPELL_NAME, artifact.getNum());
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
object.info.push_back(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
object.info.clear();
|
|
|
|
|
|
|
|
std::map<si32, si32> thrownDice;
|
|
|
|
|
2023-07-19 21:25:52 +02:00
|
|
|
configureRewards(object, rng, parameters["rewards"], thrownDice, Rewardable::EEventType::EVENT_FIRST_VISIT, "rewards");
|
|
|
|
configureRewards(object, rng, parameters["onVisited"], thrownDice, Rewardable::EEventType::EVENT_ALREADY_VISITED, "onVisited");
|
|
|
|
configureRewards(object, rng, parameters["onEmpty"], thrownDice, Rewardable::EEventType::EVENT_NOT_AVAILABLE, "onEmpty");
|
2023-04-30 02:39:31 +02:00
|
|
|
|
2023-07-19 21:25:52 +02:00
|
|
|
object.onSelect = loadMessage(parameters["onSelectMessage"], TextIdentifier(objectTextID, "onSelect"));
|
2023-04-30 02:39:31 +02:00
|
|
|
|
|
|
|
if (!parameters["onVisitedMessage"].isNull())
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
Rewardable::VisitInfo onVisited;
|
|
|
|
onVisited.visitType = Rewardable::EEventType::EVENT_ALREADY_VISITED;
|
2023-07-19 21:25:52 +02:00
|
|
|
onVisited.message = loadMessage(parameters["onVisitedMessage"], TextIdentifier(objectTextID, "onVisited"));
|
2023-04-30 02:39:31 +02:00
|
|
|
object.info.push_back(onVisited);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parameters["onEmptyMessage"].isNull())
|
|
|
|
{
|
2023-04-30 15:13:07 +02:00
|
|
|
Rewardable::VisitInfo onEmpty;
|
|
|
|
onEmpty.visitType = Rewardable::EEventType::EVENT_NOT_AVAILABLE;
|
2023-07-19 21:25:52 +02:00
|
|
|
onEmpty.message = loadMessage(parameters["onEmptyMessage"], TextIdentifier(objectTextID, "onEmpty"));
|
2023-04-30 02:39:31 +02:00
|
|
|
object.info.push_back(onEmpty);
|
|
|
|
}
|
|
|
|
|
|
|
|
configureResetInfo(object, rng, object.resetParameters, parameters["resetParameters"]);
|
|
|
|
|
|
|
|
object.canRefuse = parameters["canRefuse"].Bool();
|
|
|
|
|
|
|
|
if(parameters["showInInfobox"].isNull())
|
|
|
|
object.infoWindowType = EInfoWindowMode::AUTO;
|
|
|
|
else
|
|
|
|
object.infoWindowType = parameters["showInInfobox"].Bool() ? EInfoWindowMode::INFO : EInfoWindowMode::MODAL;
|
|
|
|
|
|
|
|
auto visitMode = parameters["visitMode"].String();
|
|
|
|
for(int i = 0; i < Rewardable::VisitModeString.size(); ++i)
|
|
|
|
{
|
|
|
|
if(Rewardable::VisitModeString[i] == visitMode)
|
|
|
|
{
|
|
|
|
object.visitMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto selectMode = parameters["selectMode"].String();
|
|
|
|
for(int i = 0; i < Rewardable::SelectModeString.size(); ++i)
|
|
|
|
{
|
|
|
|
if(Rewardable::SelectModeString[i] == selectMode)
|
|
|
|
{
|
|
|
|
object.selectMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesResources() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "resources");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesExperience() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "gainedExp") || testForKey(parameters, "gainedLevels");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesMana() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "manaPoints") || testForKey(parameters, "manaPercentage");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesMovement() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "movePoints") || testForKey(parameters, "movePercentage");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesPrimarySkills() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "primary");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesSecondarySkills() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "secondary");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesArtifacts() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "artifacts");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesCreatures() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "spells");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesSpells() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "creatures");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
bool Rewardable::Info::givesBonuses() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return testForKey(parameters, "bonuses");
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:13:07 +02:00
|
|
|
const JsonNode & Rewardable::Info::getParameters() const
|
2023-04-30 02:39:31 +02:00
|
|
|
{
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|