MarketInstanceConstructor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * MarketInstanceConstructor.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MarketInstanceConstructor.h"
  12. #include "../CConfigHandler.h"
  13. #include "../GameLibrary.h"
  14. #include "../constants/StringConstants.h"
  15. #include "../json/JsonRandom.h"
  16. #include "../json/JsonUtils.h"
  17. #include "../texts/CGeneralTextHandler.h"
  18. #include "../texts/TextIdentifier.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  21. {
  22. if (settings["mods"]["validation"].String() != "off")
  23. JsonUtils::validate(input, "vcmi:market", getJsonKey());
  24. if (!input["description"].isNull())
  25. {
  26. std::string description = input["description"].String();
  27. if (!description.empty() && description.at(0) == '@')
  28. {
  29. descriptionTextID = description.substr(1);
  30. }
  31. else
  32. {
  33. descriptionTextID = TextIdentifier(getBaseTextID(), "description").get();
  34. LIBRARY->generaltexth->registerString( input.getModScope(), descriptionTextID, input["description"]);
  35. }
  36. }
  37. if (!input["speech"].isNull())
  38. {
  39. std::string speech = input["speech"].String();
  40. if (!speech.empty() && speech.at(0) == '@')
  41. {
  42. speechTextID = speech.substr(1);
  43. }
  44. else
  45. {
  46. speechTextID = TextIdentifier(getBaseTextID(), "speech").get();
  47. LIBRARY->generaltexth->registerString( input.getModScope(), speechTextID, input["speech"]);
  48. }
  49. }
  50. for(auto & element : input["modes"].Vector())
  51. {
  52. if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
  53. marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
  54. }
  55. marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
  56. predefinedOffer = input["offer"];
  57. }
  58. bool MarketInstanceConstructor::hasDescription() const
  59. {
  60. return !descriptionTextID.empty();
  61. }
  62. std::string MarketInstanceConstructor::getDescriptionTextID() const
  63. {
  64. return descriptionTextID;
  65. }
  66. std::shared_ptr<CGMarket> MarketInstanceConstructor::createObject(IGameInfoCallback * cb) const
  67. {
  68. if(marketModes.size() == 1)
  69. {
  70. switch(*marketModes.begin())
  71. {
  72. case EMarketMode::ARTIFACT_RESOURCE:
  73. case EMarketMode::RESOURCE_ARTIFACT:
  74. return std::make_shared<CGBlackMarket>(cb);
  75. case EMarketMode::RESOURCE_SKILL:
  76. return std::make_shared<CGUniversity>(cb);
  77. }
  78. }
  79. return std::make_shared<CGMarket>(cb);
  80. }
  81. const std::set<EMarketMode> & MarketInstanceConstructor::availableModes() const
  82. {
  83. return marketModes;
  84. }
  85. void MarketInstanceConstructor::randomizeObject(CGMarket * object, IGameRandomizer & gameRandomizer) const
  86. {
  87. JsonRandom randomizer(object->cb, gameRandomizer);
  88. JsonRandom::Variables emptyVariables;
  89. if(auto * university = dynamic_cast<CGUniversity *>(object))
  90. {
  91. for(auto skill : randomizer.loadSecondaries(predefinedOffer, emptyVariables))
  92. university->skills.push_back(skill.first);
  93. }
  94. }
  95. std::string MarketInstanceConstructor::getSpeechTranslated() const
  96. {
  97. assert(marketModes.count(EMarketMode::RESOURCE_SKILL));
  98. return LIBRARY->generaltexth->translate(speechTextID);
  99. }
  100. int MarketInstanceConstructor::getMarketEfficiency() const
  101. {
  102. return marketEfficiency;
  103. }
  104. VCMI_LIB_NAMESPACE_END