2014-04-27 15:19:23 +03:00
|
|
|
/*
|
2014-06-05 20:26:50 +03:00
|
|
|
* CRewardableConstructor.cpp, part of VCMI engine
|
2014-04-27 15:19:23 +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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 11:26:03 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CRewardableConstructor.h"
|
|
|
|
|
|
|
|
#include "../CRandomGenerator.h"
|
|
|
|
#include "../StringConstants.h"
|
|
|
|
#include "../CCreatureHandler.h"
|
|
|
|
#include "JsonRandom.h"
|
|
|
|
#include "../IGameCallback.h"
|
2014-04-27 20:37:53 +03:00
|
|
|
|
2022-07-26 16:07:42 +03:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
namespace {
|
|
|
|
MetaString loadMessage(const JsonNode & value)
|
|
|
|
{
|
|
|
|
MetaString ret;
|
2016-11-13 13:38:42 +03:00
|
|
|
if (value.isNumber())
|
2020-10-01 01:38:06 -07:00
|
|
|
ret.addTxt(MetaString::ADVOB_TXT, static_cast<ui32>(value.Float()));
|
2014-05-17 17:50:11 +03:00
|
|
|
else
|
|
|
|
ret << value.String();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool testForKey(const JsonNode & value, const std::string & key)
|
|
|
|
{
|
|
|
|
for( auto & reward : value["rewards"].Vector() )
|
|
|
|
{
|
|
|
|
if (!reward[key].isNull())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
void CRandomRewardObjectInfo::init(const JsonNode & objectConfig)
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-16 23:50:02 +03:00
|
|
|
parameters = objectConfig;
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2023-01-23 23:54:54 +02:00
|
|
|
TRewardLimitersList CRandomRewardObjectInfo::configureSublimiters(CRewardableObject * object, CRandomGenerator & rng, const JsonNode & source) const
|
|
|
|
{
|
|
|
|
TRewardLimitersList result;
|
|
|
|
for (const auto & input : source.Vector())
|
|
|
|
{
|
|
|
|
auto newLimiter = std::make_shared<CRewardLimiter>();
|
|
|
|
|
|
|
|
configureLimiter(object, rng, *newLimiter, input);
|
|
|
|
|
|
|
|
result.push_back(newLimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRandomRewardObjectInfo::configureLimiter(CRewardableObject * object, CRandomGenerator & rng, CRewardLimiter & limiter, const JsonNode & source) const
|
|
|
|
{
|
|
|
|
limiter.dayOfWeek = JsonRandom::loadValue(source["dayOfWeek"], rng);
|
|
|
|
limiter.minLevel = JsonRandom::loadValue(source["minLevel"], rng);
|
|
|
|
limiter.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng);
|
|
|
|
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.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
|
|
|
|
|
|
|
|
limiter.allOf = configureSublimiters(object, rng, source["allOf"] );
|
|
|
|
limiter.anyOf = configureSublimiters(object, rng, source["anyOf"] );
|
|
|
|
limiter.noneOf = configureSublimiters(object, rng, source["noneOf"] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRandomRewardObjectInfo::configureReward(CRewardableObject * object, CRandomGenerator & rng, CRewardInfo & reward, const JsonNode & source) const
|
|
|
|
{
|
|
|
|
reward.resources = JsonRandom::loadResources(source["resources"], rng);
|
|
|
|
|
|
|
|
reward.gainedExp = JsonRandom::loadValue(source["gainedExp"], rng);
|
|
|
|
reward.gainedLevels = JsonRandom::loadValue(source["gainedLevels"], rng);
|
|
|
|
|
|
|
|
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"]);
|
|
|
|
|
|
|
|
for (auto & bonus : reward.bonuses)
|
|
|
|
{
|
|
|
|
bonus.source = Bonus::OBJECT;
|
|
|
|
bonus.sid = object->ID;
|
|
|
|
//TODO: bonus.description = object->getObjectName();
|
|
|
|
if (bonus.type == Bonus::MORALE)
|
|
|
|
reward.extraComponents.push_back(Component(Component::MORALE, 0, bonus.val, 0));
|
|
|
|
if (bonus.type == Bonus::LUCK)
|
|
|
|
reward.extraComponents.push_back(Component(Component::LUCK, 0, bonus.val, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
reward.primary = JsonRandom::loadPrimary(source["primary"], rng);
|
|
|
|
reward.secondary = JsonRandom::loadSecondary(source["secondary"], rng);
|
|
|
|
|
|
|
|
std::vector<SpellID> spells;
|
|
|
|
for (size_t i=0; i<6; i++)
|
|
|
|
IObjectInterface::cb->getAllowedSpells(spells, static_cast<ui16>(i));
|
|
|
|
|
|
|
|
reward.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng);
|
|
|
|
reward.spells = JsonRandom::loadSpells(source["spells"], rng, spells);
|
|
|
|
reward.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRandomRewardObjectInfo::configureObject(CRewardableObject * object, CRandomGenerator & rng) const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-24 01:56:51 +03:00
|
|
|
std::map<si32, si32> thrownDice;
|
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
for (const JsonNode & reward : parameters["rewards"].Vector())
|
|
|
|
{
|
2014-05-24 01:56:51 +03:00
|
|
|
if (!reward["appearChance"].isNull())
|
|
|
|
{
|
|
|
|
JsonNode chance = reward["appearChance"];
|
2020-10-01 01:38:06 -07:00
|
|
|
si32 diceID = static_cast<si32>(chance["dice"].Float());
|
2014-05-24 01:56:51 +03:00
|
|
|
|
|
|
|
if (thrownDice.count(diceID) == 0)
|
2023-01-22 18:37:26 +02:00
|
|
|
thrownDice[diceID] = rng.getIntRange(0, 99)();
|
2014-05-24 01:56:51 +03:00
|
|
|
|
|
|
|
if (!chance["min"].isNull())
|
|
|
|
{
|
2020-10-01 01:38:06 -07:00
|
|
|
int min = static_cast<int>(chance["min"].Float());
|
2014-05-24 01:56:51 +03:00
|
|
|
if (min > thrownDice[diceID])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!chance["max"].isNull())
|
|
|
|
{
|
2020-10-01 01:38:06 -07:00
|
|
|
int max = static_cast<int>(chance["max"].Float());
|
2023-01-22 18:37:26 +02:00
|
|
|
if (max <= thrownDice[diceID])
|
2014-05-24 01:56:51 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
CVisitInfo info;
|
2023-01-23 23:54:54 +02:00
|
|
|
configureLimiter(object, rng, info.limiter, reward["limiter"]);
|
|
|
|
configureReward(object, rng, info.reward, reward);
|
2014-05-24 01:56:51 +03:00
|
|
|
|
2023-01-23 23:54:54 +02:00
|
|
|
info.numOfGrantsAllowed = JsonRandom::loadValue(reward["numOfGrants"], rng);
|
2014-05-24 01:56:51 +03:00
|
|
|
info.message = loadMessage(reward["message"]);
|
2014-06-22 13:39:40 +03:00
|
|
|
info.selectChance = JsonRandom::loadValue(reward["selectChance"], rng);
|
2023-01-22 18:37:26 +02:00
|
|
|
|
|
|
|
for (const auto & artifact : info.reward.artifacts )
|
|
|
|
info.message.addReplacement(MetaString::ART_NAMES, artifact.getNum());
|
2022-09-14 22:51:19 +04:00
|
|
|
|
2023-01-22 18:37:26 +02:00
|
|
|
for (const auto & artifact : info.reward.spells )
|
|
|
|
info.message.addReplacement(MetaString::SPELL_NAME, artifact.getNum());
|
|
|
|
|
2022-09-14 22:51:19 +04:00
|
|
|
object->info.push_back(info);
|
2014-05-17 17:50:11 +03:00
|
|
|
}
|
|
|
|
|
2023-01-22 18:37:26 +02:00
|
|
|
object->blockVisit= parameters["blockedVisitable"].Bool();
|
2014-05-17 17:50:11 +03:00
|
|
|
object->onSelect = loadMessage(parameters["onSelectMessage"]);
|
|
|
|
object->onVisited = loadMessage(parameters["onVisitedMessage"]);
|
|
|
|
object->onEmpty = loadMessage(parameters["onEmptyMessage"]);
|
2020-10-01 01:38:06 -07:00
|
|
|
object->resetDuration = static_cast<ui16>(parameters["resetDuration"].Float());
|
|
|
|
object->canRefuse = parameters["canRefuse"].Bool();
|
2022-09-14 22:51:19 +04:00
|
|
|
|
|
|
|
auto visitMode = parameters["visitMode"].String();
|
2022-12-07 18:13:54 +02:00
|
|
|
for(int i = 0; i < Rewardable::VisitModeString.size(); ++i)
|
2022-09-14 22:51:19 +04:00
|
|
|
{
|
|
|
|
if(Rewardable::VisitModeString[i] == visitMode)
|
|
|
|
{
|
|
|
|
object->visitMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto selectMode = parameters["selectMode"].String();
|
2022-12-07 18:13:54 +02:00
|
|
|
for(int i = 0; i < Rewardable::SelectModeString.size(); ++i)
|
2022-09-14 22:51:19 +04:00
|
|
|
{
|
|
|
|
if(Rewardable::SelectModeString[i] == selectMode)
|
|
|
|
{
|
|
|
|
object->selectMode = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
2014-04-27 20:37:53 +03:00
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
bool CRandomRewardObjectInfo::givesResources() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "resources");
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
bool CRandomRewardObjectInfo::givesExperience() const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "gainedExp") || testForKey(parameters, "gainedLevels");
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
bool CRandomRewardObjectInfo::givesMana() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "manaPoints") || testForKey(parameters, "manaPercentage");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesMovement() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "movePoints") || testForKey(parameters, "movePercentage");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesPrimarySkills() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "primary");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesSecondarySkills() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "secondary");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesArtifacts() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "artifacts");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesCreatures() const
|
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "spells");
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CRandomRewardObjectInfo::givesSpells() const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "creatures");
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
bool CRandomRewardObjectInfo::givesBonuses() const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-17 17:50:11 +03:00
|
|
|
return testForKey(parameters, "bonuses");
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
CRewardableConstructor::CRewardableConstructor()
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-05-16 23:50:02 +03:00
|
|
|
}
|
|
|
|
|
2014-06-14 18:42:13 +03:00
|
|
|
void CRewardableConstructor::initTypeData(const JsonNode & config)
|
2014-05-16 23:50:02 +03:00
|
|
|
{
|
|
|
|
objectInfo.init(config);
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
CGObjectInstance * CRewardableConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
|
2014-05-16 23:50:02 +03:00
|
|
|
{
|
2014-06-05 14:19:47 +03:00
|
|
|
auto ret = new CRewardableObject();
|
2015-11-14 16:50:29 +03:00
|
|
|
preInitObject(ret);
|
2014-05-16 23:50:02 +03:00
|
|
|
ret->appearance = tmpl;
|
|
|
|
return ret;
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
void CRewardableConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-06-05 14:19:47 +03:00
|
|
|
objectInfo.configureObject(dynamic_cast<CRewardableObject*>(object), rng);
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
|
|
|
|
2022-09-11 15:12:35 +02:00
|
|
|
std::unique_ptr<IObjectInfo> CRewardableConstructor::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
|
2014-04-27 20:37:53 +03:00
|
|
|
{
|
2014-06-22 13:39:40 +03:00
|
|
|
return std::unique_ptr<IObjectInfo>(new CRandomRewardObjectInfo(objectInfo));
|
2014-04-27 20:37:53 +03:00
|
|
|
}
|
2022-07-26 16:07:42 +03:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|