DwellingInstanceConstructor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "../VCMI_Lib.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. VLC->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. VLC->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; //TODO: serialize for sanity
  77. if(guards.getType() == JsonNode::JsonType::DATA_BOOL) //simple switch
  78. {
  79. if(guards.Bool())
  80. {
  81. guarded = true;
  82. }
  83. }
  84. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR) //custom guards (eg. Elemental Conflux)
  85. {
  86. JsonRandom::Variables emptyVariables;
  87. for(auto & stack : randomizer.loadCreatures(guards, rng, emptyVariables))
  88. {
  89. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.getId(), stack.count));
  90. }
  91. }
  92. else //default condition - creatures are of level 5 or higher
  93. {
  94. for(auto creatureEntry : availableCreatures)
  95. {
  96. if(creatureEntry.at(0)->getLevel() >= 5)
  97. {
  98. guarded = true;
  99. break;
  100. }
  101. }
  102. }
  103. if(guarded)
  104. {
  105. for(auto creatureEntry : availableCreatures)
  106. {
  107. const CCreature * crea = creatureEntry.at(0);
  108. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
  109. }
  110. }
  111. }
  112. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  113. {
  114. for(const auto & entry : availableCreatures)
  115. {
  116. for(const CCreature * cre : entry)
  117. if(crea == cre)
  118. return true;
  119. }
  120. return false;
  121. }
  122. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  123. {
  124. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  125. for(const auto & entry : availableCreatures)
  126. {
  127. for(const CCreature * cre : entry)
  128. creatures.push_back(cre);
  129. }
  130. return creatures;
  131. }
  132. VCMI_LIB_NAMESPACE_END