1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Text of configurable/rewardable objects now uses translator

This commit is contained in:
Ivan Savenko 2023-07-19 22:25:52 +03:00
parent 280085b95d
commit 3a348abf4f
10 changed files with 99 additions and 31 deletions

View File

@ -63,14 +63,19 @@ std::string CBuilding::getDescriptionTranslated() const
return VLC->generaltexth->translate(getDescriptionTextID()); return VLC->generaltexth->translate(getDescriptionTextID());
} }
std::string CBuilding::getBaseTextID() const
{
return TextIdentifier("building", modScope, town->faction->identifier, identifier).get();
}
std::string CBuilding::getNameTextID() const std::string CBuilding::getNameTextID() const
{ {
return TextIdentifier("building", modScope, town->faction->identifier, identifier, "name").get(); return TextIdentifier(getBaseTextID(), "name").get();
} }
std::string CBuilding::getDescriptionTextID() const std::string CBuilding::getDescriptionTextID() const
{ {
return TextIdentifier("building", modScope, town->faction->identifier, identifier, "description").get(); return TextIdentifier(getBaseTextID(), "description").get();
} }
BuildingID CBuilding::getBase() const BuildingID CBuilding::getBase() const
@ -639,7 +644,7 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons
if(source["type"].String() == "configurable" && ret->subId == BuildingSubID::NONE) if(source["type"].String() == "configurable" && ret->subId == BuildingSubID::NONE)
{ {
ret->subId = BuildingSubID::CUSTOM_VISITING_REWARD; ret->subId = BuildingSubID::CUSTOM_VISITING_REWARD;
ret->rewardableObjectInfo.init(source); ret->rewardableObjectInfo.init(source, ret->getBaseTextID());
} }
} }
//MODS COMPATIBILITY FOR 0.96 //MODS COMPATIBILITY FOR 0.96

View File

@ -88,6 +88,7 @@ public:
std::string getNameTranslated() const; std::string getNameTranslated() const;
std::string getDescriptionTranslated() const; std::string getDescriptionTranslated() const;
std::string getBaseTextID() const;
std::string getNameTextID() const; std::string getNameTextID() const;
std::string getDescriptionTextID() const; std::string getDescriptionTextID() const;

View File

@ -460,6 +460,21 @@ const JsonNode & JsonNode::operator[](const std::string & child) const
return nullNode; return nullNode;
} }
JsonNode & JsonNode::operator[](size_t child)
{
if (child >= Vector().size() )
Vector().resize(child + 1);
return Vector()[child];
}
const JsonNode & JsonNode::operator[](size_t child) const
{
if (child < Vector().size() )
return Vector()[child];
return nullNode;
}
const JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer) const const JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer) const
{ {
return ::resolvePointer(*this, jsonPointer); return ::resolvePointer(*this, jsonPointer);

View File

@ -129,6 +129,9 @@ public:
JsonNode & operator[](const std::string & child); JsonNode & operator[](const std::string & child);
const JsonNode & operator[](const std::string & child) const; const JsonNode & operator[](const std::string & child) const;
JsonNode & operator[](size_t child);
const JsonNode & operator[](size_t child) const;
std::string toJson(bool compact = false) const; std::string toJson(bool compact = false) const;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -133,9 +133,14 @@ bool AObjectTypeHandler::hasNameTextID() const
return false; return false;
} }
std::string AObjectTypeHandler::getBaseTextID() const
{
return TextIdentifier("mapObject", modScope, typeName, subTypeName).get();
}
std::string AObjectTypeHandler::getNameTextID() const std::string AObjectTypeHandler::getNameTextID() const
{ {
return TextIdentifier("mapObject", modScope, typeName, subTypeName, "name").get(); return TextIdentifier(getBaseTextID(), "name").get();
} }
std::string AObjectTypeHandler::getNameTranslated() const std::string AObjectTypeHandler::getNameTranslated() const

View File

@ -89,6 +89,9 @@ public:
/// returns true if this class provides custom text ID's instead of generic per-object name /// returns true if this class provides custom text ID's instead of generic per-object name
virtual bool hasNameTextID() const; virtual bool hasNameTextID() const;
/// returns base prefix for all translatable strings of this object
std::string getBaseTextID() const;
/// returns object's name in form of translatable text ID /// returns object's name in form of translatable text ID
virtual std::string getNameTextID() const; virtual std::string getNameTextID() const;

View File

@ -18,7 +18,7 @@ VCMI_LIB_NAMESPACE_BEGIN
void CRewardableConstructor::initTypeData(const JsonNode & config) void CRewardableConstructor::initTypeData(const JsonNode & config)
{ {
objectInfo.init(config); objectInfo.init(config, getBaseTextID());
blockVisit = config["blockedVisitable"].Bool(); blockVisit = config["blockedVisitable"].Bool();
if (!config["name"].isNull()) if (!config["name"].isNull())

View File

@ -297,7 +297,10 @@ CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, Build
void CTownRewardableBuilding::initObj(CRandomGenerator & rand) void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
{ {
assert(town && town->town); assert(town && town->town);
town->town->buildings.at(bID)->rewardableObjectInfo.configureObject(configuration, rand);
auto building = town->town->buildings.at(bID);
building->rewardableObjectInfo.configureObject(configuration, rand);
for(auto & rewardInfo : configuration.info) for(auto & rewardInfo : configuration.info)
{ {
for (auto & bonus : rewardInfo.reward.bonuses) for (auto & bonus : rewardInfo.reward.bonuses)

View File

@ -10,33 +10,33 @@
#include "StdInc.h" #include "StdInc.h"
#include "Info.h" #include "Info.h"
#include "Configuration.h"
#include "Limiter.h" #include "Limiter.h"
#include "Reward.h" #include "Reward.h"
#include "Configuration.h"
#include "../CCreatureSet.h"
#include "../CRandomGenerator.h"
#include "../StringConstants.h"
#include "../CCreatureHandler.h"
#include "../CModHandler.h"
#include "../NetPacksBase.h"
#include "../JsonRandom.h"
#include "../IGameCallback.h"
#include "../CGeneralTextHandler.h" #include "../CGeneralTextHandler.h"
#include "../JsonNode.h" #include "../CModHandler.h"
#include "../IGameCallback.h" #include "../IGameCallback.h"
#include "../JsonRandom.h"
#include "../mapObjects/IObjectInterface.h" #include "../mapObjects/IObjectInterface.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
namespace { namespace {
MetaString loadMessage(const JsonNode & value) MetaString loadMessage(const JsonNode & value, const TextIdentifier & textIdentifier )
{ {
MetaString ret; MetaString ret;
if (value.isNumber()) if (value.isNumber())
{
ret.appendLocalString(EMetaText::ADVOB_TXT, static_cast<ui32>(value.Float())); ret.appendLocalString(EMetaText::ADVOB_TXT, static_cast<ui32>(value.Float()));
else return ret;
ret.appendRawString(value.String()); }
if (value.String().empty())
return ret;
ret.appendTextID(textIdentifier.get());
return ret; return ret;
} }
@ -51,9 +51,38 @@ namespace {
} }
} }
void Rewardable::Info::init(const JsonNode & objectConfig) void Rewardable::Info::init(const JsonNode & objectConfig, const std::string & objectName)
{ {
objectTextID = objectName;
auto loadString = [&](const JsonNode & entry, const TextIdentifier & textID){
if (entry.isString() && !entry.String().empty())
VLC->generaltexth->registerString(entry.meta, textID, entry.String());
};
parameters = objectConfig; parameters = objectConfig;
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"));
} }
Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
@ -157,10 +186,13 @@ void Rewardable::Info::configureRewards(
CRandomGenerator & rng, const CRandomGenerator & rng, const
JsonNode & source, JsonNode & source,
std::map<si32, si32> & thrownDice, std::map<si32, si32> & thrownDice,
Rewardable::EEventType event ) const Rewardable::EEventType event,
const std::string & modeName) const
{ {
for (const JsonNode & reward : source.Vector()) for(size_t i = 0; i < source.Vector().size(); ++i)
{ {
const JsonNode reward = source.Vector()[i];
if (!reward["appearChance"].isNull()) if (!reward["appearChance"].isNull())
{ {
JsonNode chance = reward["appearChance"]; JsonNode chance = reward["appearChance"];
@ -188,7 +220,7 @@ void Rewardable::Info::configureRewards(
configureReward(object, rng, info.reward, reward); configureReward(object, rng, info.reward, reward);
info.visitType = event; info.visitType = event;
info.message = loadMessage(reward["message"]); info.message = loadMessage(reward["message"], TextIdentifier(objectTextID, modeName, i));
for (const auto & artifact : info.reward.artifacts ) for (const auto & artifact : info.reward.artifacts )
info.message.replaceLocalString(EMetaText::ART_NAMES, artifact.getNum()); info.message.replaceLocalString(EMetaText::ART_NAMES, artifact.getNum());
@ -206,17 +238,17 @@ void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRand
std::map<si32, si32> thrownDice; std::map<si32, si32> thrownDice;
configureRewards(object, rng, parameters["rewards"], thrownDice, Rewardable::EEventType::EVENT_FIRST_VISIT); configureRewards(object, rng, parameters["rewards"], thrownDice, Rewardable::EEventType::EVENT_FIRST_VISIT, "rewards");
configureRewards(object, rng, parameters["onVisited"], thrownDice, Rewardable::EEventType::EVENT_ALREADY_VISITED); configureRewards(object, rng, parameters["onVisited"], thrownDice, Rewardable::EEventType::EVENT_ALREADY_VISITED, "onVisited");
configureRewards(object, rng, parameters["onEmpty"], thrownDice, Rewardable::EEventType::EVENT_NOT_AVAILABLE); configureRewards(object, rng, parameters["onEmpty"], thrownDice, Rewardable::EEventType::EVENT_NOT_AVAILABLE, "onEmpty");
object.onSelect = loadMessage(parameters["onSelectMessage"]); object.onSelect = loadMessage(parameters["onSelectMessage"], TextIdentifier(objectTextID, "onSelect"));
if (!parameters["onVisitedMessage"].isNull()) if (!parameters["onVisitedMessage"].isNull())
{ {
Rewardable::VisitInfo onVisited; Rewardable::VisitInfo onVisited;
onVisited.visitType = Rewardable::EEventType::EVENT_ALREADY_VISITED; onVisited.visitType = Rewardable::EEventType::EVENT_ALREADY_VISITED;
onVisited.message = loadMessage(parameters["onVisitedMessage"]); onVisited.message = loadMessage(parameters["onVisitedMessage"], TextIdentifier(objectTextID, "onVisited"));
object.info.push_back(onVisited); object.info.push_back(onVisited);
} }
@ -224,7 +256,7 @@ void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRand
{ {
Rewardable::VisitInfo onEmpty; Rewardable::VisitInfo onEmpty;
onEmpty.visitType = Rewardable::EEventType::EVENT_NOT_AVAILABLE; onEmpty.visitType = Rewardable::EEventType::EVENT_NOT_AVAILABLE;
onEmpty.message = loadMessage(parameters["onEmptyMessage"]); onEmpty.message = loadMessage(parameters["onEmptyMessage"], TextIdentifier(objectTextID, "onEmpty"));
object.info.push_back(onEmpty); object.info.push_back(onEmpty);
} }

View File

@ -30,8 +30,9 @@ enum class EEventType;
class DLL_LINKAGE Info : public IObjectInfo class DLL_LINKAGE Info : public IObjectInfo
{ {
JsonNode parameters; JsonNode parameters;
std::string objectTextID;
void configureRewards(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source, std::map<si32, si32> & thrownDice, Rewardable::EEventType mode) const; void configureRewards(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source, std::map<si32, si32> & thrownDice, Rewardable::EEventType mode, const std::string & textPrefix) const;
void configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const; void configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const;
Rewardable::LimitersList configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const; Rewardable::LimitersList configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const;
@ -58,7 +59,7 @@ public:
void configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const; void configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const;
void init(const JsonNode & objectConfig); void init(const JsonNode & objectConfig, const std::string & objectTextID);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {