2014-04-27 15:19:23 +03:00
|
|
|
#include "StdInc.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "CRewardableConstructor.h"
|
2014-04-27 15:19:23 +03:00
|
|
|
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../CRandomGenerator.h"
|
|
|
|
#include "../StringConstants.h"
|
|
|
|
#include "../CCreatureHandler.h"
|
2014-06-22 13:39:40 +03:00
|
|
|
#include "JsonRandom.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../IGameCallback.h"
|
2014-05-17 17:50:11 +03:00
|
|
|
|
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
|
|
|
|
*
|
|
|
|
*/
|
2014-04-27 20:37:53 +03:00
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
namespace {
|
|
|
|
MetaString loadMessage(const JsonNode & value)
|
|
|
|
{
|
|
|
|
MetaString ret;
|
|
|
|
if (value.getType() == JsonNode::DATA_FLOAT)
|
|
|
|
ret.addTxt(MetaString::ADVOB_TXT, value.Float());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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"];
|
|
|
|
si32 diceID = chance["dice"].Float();
|
|
|
|
|
|
|
|
if (thrownDice.count(diceID) == 0)
|
|
|
|
thrownDice[diceID] = rng.getIntRange(1, 100)();
|
|
|
|
|
|
|
|
if (!chance["min"].isNull())
|
|
|
|
{
|
|
|
|
int min = chance["min"].Float();
|
|
|
|
if (min > thrownDice[diceID])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!chance["max"].isNull())
|
|
|
|
{
|
|
|
|
int max = chance["max"].Float();
|
|
|
|
if (max < thrownDice[diceID])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
const JsonNode & limiter = reward["limiter"];
|
|
|
|
CVisitInfo info;
|
|
|
|
// load limiter
|
2014-06-22 13:39:40 +03:00
|
|
|
info.limiter.numOfGrants = JsonRandom::loadValue(limiter["numOfGrants"], rng);
|
|
|
|
info.limiter.dayOfWeek = JsonRandom::loadValue(limiter["dayOfWeek"], rng);
|
|
|
|
info.limiter.minLevel = JsonRandom::loadValue(limiter["minLevel"], rng);
|
|
|
|
info.limiter.resources = JsonRandom::loadResources(limiter["resources"], rng);
|
|
|
|
|
|
|
|
info.limiter.primary = JsonRandom::loadPrimary(limiter["primary"], rng);
|
|
|
|
info.limiter.secondary = JsonRandom::loadSecondary(limiter["secondary"], rng);
|
|
|
|
info.limiter.artifacts = JsonRandom::loadArtifacts(limiter["artifacts"], rng);
|
|
|
|
info.limiter.creatures = JsonRandom::loadCreatures(limiter["creatures"], rng);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.resources = JsonRandom::loadResources(reward["resources"], rng);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.gainedExp = JsonRandom::loadValue(reward["gainedExp"], rng);
|
|
|
|
info.reward.gainedLevels = JsonRandom::loadValue(reward["gainedLevels"], rng);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.manaDiff = JsonRandom::loadValue(reward["manaPoints"], rng);
|
|
|
|
info.reward.manaPercentage = JsonRandom::loadValue(reward["manaPercentage"], rng, -1);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.movePoints = JsonRandom::loadValue(reward["movePoints"], rng);
|
|
|
|
info.reward.movePercentage = JsonRandom::loadValue(reward["movePercentage"], rng, -1);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-26 23:05:27 +03:00
|
|
|
//FIXME: compile this line on Visual
|
|
|
|
//info.reward.bonuses = JsonRandom::loadBonuses(reward["bonuses"]);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.primary = JsonRandom::loadPrimary(reward["primary"], rng);
|
|
|
|
info.reward.secondary = JsonRandom::loadSecondary(reward["secondary"], rng);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
std::vector<SpellID> spells;
|
|
|
|
for (size_t i=0; i<6; i++)
|
|
|
|
IObjectInterface::cb->getAllowedSpells(spells, i);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
info.reward.artifacts = JsonRandom::loadArtifacts(reward["artifacts"], rng);
|
|
|
|
info.reward.spells = JsonRandom::loadSpells(reward["spells"], rng, spells);
|
|
|
|
info.reward.creatures = JsonRandom::loadCreatures(reward["creatures"], 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);
|
2014-05-17 17:50:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
object->onSelect = loadMessage(parameters["onSelectMessage"]);
|
|
|
|
object->onVisited = loadMessage(parameters["onVisitedMessage"]);
|
|
|
|
object->onEmpty = loadMessage(parameters["onEmptyMessage"]);
|
|
|
|
|
|
|
|
//TODO: visitMode and selectMode
|
2014-04-27 20:37:53 +03:00
|
|
|
|
2014-05-17 17:50:11 +03:00
|
|
|
object->soundID = parameters["soundID"].Float();
|
|
|
|
object->resetDuration = parameters["resetDuration"].Float();
|
|
|
|
object->canRefuse =parameters["canRefuse"].Bool();
|
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
|
|
|
{
|
2014-05-24 01:56:51 +03:00
|
|
|
AObjectTypeHandler::init(config);
|
2014-05-16 23:50:02 +03:00
|
|
|
objectInfo.init(config);
|
|
|
|
}
|
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
CGObjectInstance * CRewardableConstructor::create(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 15:50:29 +02: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
|
|
|
}
|
|
|
|
|
2014-06-22 13:39:40 +03:00
|
|
|
std::unique_ptr<IObjectInfo> CRewardableConstructor::getObjectInfo(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
|
|
|
}
|