2
0

DwellingInstanceConstructor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "../CModHandler.h"
  15. #include "../JsonRandom.h"
  16. #include "../VCMI_Lib.h"
  17. #include "../mapObjects/CGDwelling.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->modh->identifiers.requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
  39. {
  40. availableCreatures[currentLevel][currentCreature] = VLC->creh->objects[index];
  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 * object, CRandomGenerator &rng) const
  61. {
  62. auto * dwelling = dynamic_cast<CGDwelling *>(object);
  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. for(auto & stack : JsonRandom::loadCreatures(guards, rng))
  82. {
  83. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->getId(), stack.count));
  84. }
  85. }
  86. else //default condition - creatures are of level 5 or higher
  87. {
  88. for(auto creatureEntry : availableCreatures)
  89. {
  90. if(creatureEntry.at(0)->getLevel() >= 5)
  91. {
  92. guarded = true;
  93. break;
  94. }
  95. }
  96. }
  97. if(guarded)
  98. {
  99. for(auto creatureEntry : availableCreatures)
  100. {
  101. const CCreature * crea = creatureEntry.at(0);
  102. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
  103. }
  104. }
  105. }
  106. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  107. {
  108. for(const auto & entry : availableCreatures)
  109. {
  110. for(const CCreature * cre : entry)
  111. if(crea == cre)
  112. return true;
  113. }
  114. return false;
  115. }
  116. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  117. {
  118. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  119. for(const auto & entry : availableCreatures)
  120. {
  121. for(const CCreature * cre : entry)
  122. creatures.push_back(cre);
  123. }
  124. return creatures;
  125. }
  126. VCMI_LIB_NAMESPACE_END