DwellingInstanceConstructor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "../CGeneralTextHandler.h"
  14. #include "../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.meta, getNameTextID(), input["name"].String());
  28. const JsonVector & levels = input["creatures"].Vector();
  29. const auto totalLevels = levels.size();
  30. availableCreatures.resize(totalLevels);
  31. for(auto 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(auto currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
  37. {
  38. VLC->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
  39. {
  40. availableCreatures[currentLevel][currentCreature] = CreatureID(index).toCreature();
  41. });
  42. }
  43. assert(!availableCreatures[currentLevel].empty());
  44. }
  45. guards = input["guards"];
  46. }
  47. bool DwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  48. {
  49. return false;
  50. }
  51. void DwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
  52. {
  53. obj->creatures.resize(availableCreatures.size());
  54. for(const auto & entry : availableCreatures)
  55. {
  56. for(const CCreature * cre : entry)
  57. obj->creatures.back().second.push_back(cre->getId());
  58. }
  59. }
  60. void DwellingInstanceConstructor::randomizeObject(CGDwelling * dwelling, CRandomGenerator &rng) const
  61. {
  62. JsonRandom randomizer(dwelling->cb);
  63. dwelling->creatures.clear();
  64. dwelling->creatures.reserve(availableCreatures.size());
  65. for(const auto & entry : availableCreatures)
  66. {
  67. dwelling->creatures.resize(dwelling->creatures.size() + 1);
  68. for(const CCreature * cre : entry)
  69. dwelling->creatures.back().second.push_back(cre->getId());
  70. }
  71. bool guarded = false; //TODO: serialize for sanity
  72. if(guards.getType() == JsonNode::JsonType::DATA_BOOL) //simple switch
  73. {
  74. if(guards.Bool())
  75. {
  76. guarded = true;
  77. }
  78. }
  79. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR) //custom guards (eg. Elemental Conflux)
  80. {
  81. JsonRandom::Variables emptyVariables;
  82. for(auto & stack : randomizer.loadCreatures(guards, rng, emptyVariables))
  83. {
  84. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->getId(), stack.count));
  85. }
  86. }
  87. else //default condition - creatures are of level 5 or higher
  88. {
  89. for(auto creatureEntry : availableCreatures)
  90. {
  91. if(creatureEntry.at(0)->getLevel() >= 5)
  92. {
  93. guarded = true;
  94. break;
  95. }
  96. }
  97. }
  98. if(guarded)
  99. {
  100. for(auto creatureEntry : availableCreatures)
  101. {
  102. const CCreature * crea = creatureEntry.at(0);
  103. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
  104. }
  105. }
  106. }
  107. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  108. {
  109. for(const auto & entry : availableCreatures)
  110. {
  111. for(const CCreature * cre : entry)
  112. if(crea == cre)
  113. return true;
  114. }
  115. return false;
  116. }
  117. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  118. {
  119. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  120. for(const auto & entry : availableCreatures)
  121. {
  122. for(const CCreature * cre : entry)
  123. creatures.push_back(cre);
  124. }
  125. return creatures;
  126. }
  127. VCMI_LIB_NAMESPACE_END