MarketInstanceConstructor.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  20. {
  21. if (settings["mods"]["validation"].String() != "off")
  22. JsonUtils::validate(input, "vcmi:market", getJsonKey());
  23. if (!input["description"].isNull())
  24. {
  25. std::string description = input["description"].String();
  26. descriptionTextID = TextIdentifier(getBaseTextID(), "description").get();
  27. LIBRARY->generaltexth->registerString( input.getModScope(), descriptionTextID, input["description"]);
  28. }
  29. if (!input["speech"].isNull())
  30. {
  31. std::string speech = input["speech"].String();
  32. if (!speech.empty() && speech.at(0) == '@')
  33. {
  34. speechTextID = speech.substr(1);
  35. }
  36. else
  37. {
  38. speechTextID = TextIdentifier(getBaseTextID(), "speech").get();
  39. LIBRARY->generaltexth->registerString( input.getModScope(), speechTextID, input["speech"]);
  40. }
  41. }
  42. for(auto & element : input["modes"].Vector())
  43. {
  44. if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
  45. marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
  46. }
  47. marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
  48. predefinedOffer = input["offer"];
  49. }
  50. bool MarketInstanceConstructor::hasDescription() const
  51. {
  52. return !descriptionTextID.empty();
  53. }
  54. std::shared_ptr<CGMarket> MarketInstanceConstructor::createObject(IGameInfoCallback * cb) const
  55. {
  56. if(marketModes.size() == 1)
  57. {
  58. switch(*marketModes.begin())
  59. {
  60. case EMarketMode::ARTIFACT_RESOURCE:
  61. case EMarketMode::RESOURCE_ARTIFACT:
  62. return std::make_shared<CGBlackMarket>(cb);
  63. case EMarketMode::RESOURCE_SKILL:
  64. return std::make_shared<CGUniversity>(cb);
  65. }
  66. }
  67. return std::make_shared<CGMarket>(cb);
  68. }
  69. const std::set<EMarketMode> & MarketInstanceConstructor::availableModes() const
  70. {
  71. return marketModes;
  72. }
  73. void MarketInstanceConstructor::randomizeObject(CGMarket * object, IGameRandomizer & gameRandomizer) const
  74. {
  75. JsonRandom randomizer(object->cb, gameRandomizer);
  76. JsonRandom::Variables emptyVariables;
  77. if(auto * university = dynamic_cast<CGUniversity *>(object))
  78. {
  79. for(auto skill : randomizer.loadSecondaries(predefinedOffer, emptyVariables))
  80. university->skills.push_back(skill.first);
  81. }
  82. }
  83. std::string MarketInstanceConstructor::getSpeechTranslated() const
  84. {
  85. assert(marketModes.count(EMarketMode::RESOURCE_SKILL));
  86. return LIBRARY->generaltexth->translate(speechTextID);
  87. }
  88. int MarketInstanceConstructor::getMarketEfficiency() const
  89. {
  90. return marketEfficiency;
  91. }