DwellingInstanceConstructor.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * DwellingInstanceConstructor.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 "DwellingInstanceConstructor.h"
  12. #include "../CCreatureHandler.h"
  13. #include "../texts/CGeneralTextHandler.h"
  14. #include "../json/JsonRandom.h"
  15. #include "../GameLibrary.h"
  16. #include "../mapObjects/CGDwelling.h"
  17. #include "../modding/IdentifierStorage.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. bool DwellingInstanceConstructor::hasNameTextID() const
  20. {
  21. return true;
  22. }
  23. void DwellingInstanceConstructor::initTypeData(const JsonNode & input)
  24. {
  25. if (input.Struct().count("name") == 0)
  26. logMod->warn("Dwelling %s missing name!", getJsonKey());
  27. LIBRARY->generaltexth->registerString( input.getModScope(), getNameTextID(), input["name"]);
  28. const JsonVector & levels = input["creatures"].Vector();
  29. const auto totalLevels = levels.size();
  30. availableCreatures.resize(totalLevels);
  31. for(int currentLevel = 0; currentLevel < totalLevels; currentLevel++)
  32. {
  33. const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
  34. const auto creaturesNumber = creaturesOnLevel.size();
  35. availableCreatures[currentLevel].resize(creaturesNumber);
  36. for(int currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
  37. {
  38. LIBRARY->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [this, currentLevel, currentCreature] (si32 index)
  39. {
  40. availableCreatures.at(currentLevel).at(currentCreature) = CreatureID(index).toCreature();
  41. });
  42. }
  43. assert(!availableCreatures[currentLevel].empty());
  44. }
  45. guards = input["guards"];
  46. bannedForRandomDwelling = input["bannedForRandomDwelling"].Bool();
  47. }
  48. bool DwellingInstanceConstructor::isBannedForRandomDwelling() const
  49. {
  50. return bannedForRandomDwelling;
  51. }
  52. bool DwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  53. {
  54. return false;
  55. }
  56. void DwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
  57. {
  58. obj->creatures.resize(availableCreatures.size());
  59. for(const auto & entry : availableCreatures)
  60. {
  61. for(const CCreature * cre : entry)
  62. obj->creatures.back().second.push_back(cre->getId());
  63. }
  64. }
  65. void DwellingInstanceConstructor::randomizeObject(CGDwelling * dwelling, vstd::RNG &rng) const
  66. {
  67. JsonRandom randomizer(dwelling->cb);
  68. dwelling->creatures.clear();
  69. dwelling->creatures.reserve(availableCreatures.size());
  70. for(const auto & entry : availableCreatures)
  71. {
  72. dwelling->creatures.resize(dwelling->creatures.size() + 1);
  73. for(const CCreature * cre : entry)
  74. dwelling->creatures.back().second.push_back(cre->getId());
  75. }
  76. bool guarded = false;
  77. if(guards.getType() == JsonNode::JsonType::DATA_BOOL)
  78. {
  79. //simple switch
  80. if(guards.Bool())
  81. {
  82. guarded = true;
  83. }
  84. }
  85. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR)
  86. {
  87. //custom guards (eg. Elemental Conflux)
  88. JsonRandom::Variables emptyVariables;
  89. for(auto & stack : randomizer.loadCreatures(guards, rng, emptyVariables))
  90. {
  91. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, stack.getId(), stack.getCount()));
  92. }
  93. }
  94. else if (dwelling->ID == Obj::CREATURE_GENERATOR1 || dwelling->ID == Obj::CREATURE_GENERATOR4)
  95. {
  96. //default condition - this is dwelling with creatures of level 5 or higher
  97. for(auto creatureEntry : availableCreatures)
  98. {
  99. if(creatureEntry.at(0)->getLevel() >= 5)
  100. {
  101. guarded = true;
  102. break;
  103. }
  104. }
  105. }
  106. if(guarded)
  107. {
  108. for(auto creatureEntry : availableCreatures)
  109. {
  110. const CCreature * crea = creatureEntry.at(0);
  111. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, crea->getId(), crea->getGrowth() * 3));
  112. }
  113. }
  114. }
  115. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  116. {
  117. for(const auto & entry : availableCreatures)
  118. {
  119. for(const CCreature * cre : entry)
  120. if(crea == cre)
  121. return true;
  122. }
  123. return false;
  124. }
  125. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  126. {
  127. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  128. for(const auto & entry : availableCreatures)
  129. {
  130. for(const CCreature * cre : entry)
  131. creatures.push_back(cre);
  132. }
  133. return creatures;
  134. }
  135. VCMI_LIB_NAMESPACE_END