DwellingInstanceConstructor.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/army/CStackInstance.h"
  17. #include "../mapObjects/CGDwelling.h"
  18. #include "../mapObjects/ObjectTemplate.h"
  19. #include "../modding/IdentifierStorage.h"
  20. #include "../CConfigHandler.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. bool DwellingInstanceConstructor::hasNameTextID() const
  23. {
  24. return true;
  25. }
  26. void DwellingInstanceConstructor::initTypeData(const JsonNode & input)
  27. {
  28. if (input.Struct().count("name") == 0)
  29. logMod->warn("Dwelling %s missing name!", getJsonKey());
  30. LIBRARY->generaltexth->registerString( input.getModScope(), getNameTextID(), input["name"]);
  31. const JsonVector & levels = input["creatures"].Vector();
  32. const auto totalLevels = levels.size();
  33. availableCreatures.resize(totalLevels);
  34. for(int currentLevel = 0; currentLevel < totalLevels; currentLevel++)
  35. {
  36. const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
  37. const auto creaturesNumber = creaturesOnLevel.size();
  38. availableCreatures[currentLevel].resize(creaturesNumber);
  39. for(int currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
  40. {
  41. LIBRARY->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [this, currentLevel, currentCreature] (si32 index)
  42. {
  43. availableCreatures.at(currentLevel).at(currentCreature) = CreatureID(index).toCreature();
  44. });
  45. }
  46. assert(!availableCreatures[currentLevel].empty());
  47. }
  48. guards = input["guards"];
  49. bannedForRandomDwelling = input["bannedForRandomDwelling"].Bool();
  50. for (const auto & mapTemplate : getTemplates())
  51. onTemplateAdded(mapTemplate);
  52. }
  53. void DwellingInstanceConstructor::onTemplateAdded(const std::shared_ptr<const ObjectTemplate> mapTemplate)
  54. {
  55. if (bannedForRandomDwelling || settings["mods"]["validation"].String() == "off")
  56. return;
  57. bool invalidForRandomDwelling = false;
  58. int3 corner = mapTemplate->getCornerOffset();
  59. for (const auto & tile : mapTemplate->getBlockedOffsets())
  60. invalidForRandomDwelling |= (tile.x != -corner.x && tile.x != -corner.x-1) || (tile.y != -corner.y && tile.y != -corner.y-1);
  61. for (const auto & tile : {mapTemplate->getVisitableOffset()})
  62. invalidForRandomDwelling |= (tile.x != corner.x && tile.x != corner.x+1) || tile.y != corner.y;
  63. invalidForRandomDwelling |= !mapTemplate->isBlockedAt(corner.x+0, corner.y) && !mapTemplate->isVisibleAt(corner.x+0, corner.y);
  64. invalidForRandomDwelling |= !mapTemplate->isBlockedAt(corner.x+1, corner.y) && !mapTemplate->isVisibleAt(corner.x+1, corner.y);
  65. if (invalidForRandomDwelling)
  66. logMod->warn("Dwelling %s has template %s which is not valid for a random dwelling! Dwellings must not block tiles outside 2x2 range and must be visitable in bottom row. Change dwelling mask or mark dwelling as 'bannedForRandomDwelling'", getJsonKey(), mapTemplate->animationFile.getOriginalName());
  67. }
  68. bool DwellingInstanceConstructor::isBannedForRandomDwelling() const
  69. {
  70. return bannedForRandomDwelling;
  71. }
  72. bool DwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  73. {
  74. return false;
  75. }
  76. void DwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
  77. {
  78. obj->creatures.resize(availableCreatures.size());
  79. for(const auto & entry : availableCreatures)
  80. {
  81. for(const CCreature * cre : entry)
  82. obj->creatures.back().second.push_back(cre->getId());
  83. }
  84. }
  85. void DwellingInstanceConstructor::randomizeObject(CGDwelling * dwelling, IGameRandomizer & gameRandomizer) const
  86. {
  87. JsonRandom randomizer(dwelling->cb, gameRandomizer);
  88. dwelling->creatures.clear();
  89. dwelling->creatures.reserve(availableCreatures.size());
  90. for(const auto & entry : availableCreatures)
  91. {
  92. dwelling->creatures.resize(dwelling->creatures.size() + 1);
  93. for(const CCreature * cre : entry)
  94. dwelling->creatures.back().second.push_back(cre->getId());
  95. }
  96. bool guarded = false;
  97. if(guards.getType() == JsonNode::JsonType::DATA_BOOL)
  98. {
  99. //simple switch
  100. if(guards.Bool())
  101. {
  102. guarded = true;
  103. }
  104. }
  105. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR)
  106. {
  107. //custom guards (eg. Elemental Conflux)
  108. JsonRandom::Variables emptyVariables;
  109. for(auto & stack : randomizer.loadCreatures(guards, emptyVariables))
  110. {
  111. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, stack.getId(), stack.getCount()));
  112. }
  113. }
  114. else if (dwelling->ID == Obj::CREATURE_GENERATOR1 || dwelling->ID == Obj::CREATURE_GENERATOR4)
  115. {
  116. //default condition - this is dwelling with creatures of level 5 or higher
  117. for(auto creatureEntry : availableCreatures)
  118. {
  119. if(creatureEntry.at(0)->getLevel() >= 5)
  120. {
  121. guarded = true;
  122. break;
  123. }
  124. }
  125. }
  126. if(guarded)
  127. {
  128. for(auto creatureEntry : availableCreatures)
  129. {
  130. const CCreature * crea = creatureEntry.at(0);
  131. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, crea->getId(), crea->getGrowth() * 3));
  132. }
  133. }
  134. }
  135. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  136. {
  137. for(const auto & entry : availableCreatures)
  138. {
  139. for(const CCreature * cre : entry)
  140. if(crea == cre)
  141. return true;
  142. }
  143. return false;
  144. }
  145. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  146. {
  147. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  148. for(const auto & entry : availableCreatures)
  149. {
  150. for(const CCreature * cre : entry)
  151. creatures.push_back(cre);
  152. }
  153. return creatures;
  154. }
  155. VCMI_LIB_NAMESPACE_END